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 | 8da415e | 2010-12-05 01:30:14 +0100 | [diff] [blame] | 84 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 85 | || defined(__APPLE__) \ |
| 86 | ) |
| 87 | # include <malloc.h> /* for malloc_trim */ |
| 88 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 89 | #include <glob.h> |
| 90 | /* #include <dmalloc.h> */ |
| 91 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 92 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 93 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 94 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 95 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 96 | #include "unicode.h" |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 97 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 98 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 99 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 100 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 101 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 102 | #else |
| 103 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 104 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 105 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 106 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 107 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 108 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 109 | //config:config HUSH |
| 110 | //config: bool "hush" |
| 111 | //config: default y |
| 112 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 113 | //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] | 114 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 115 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 116 | //config: and functions are supported. |
| 117 | //config: |
| 118 | //config: It will compile and work on no-mmu systems. |
| 119 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 120 | //config: It does not handle select, aliases, tilde expansion, |
| 121 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 122 | //config: |
| 123 | //config:config HUSH_BASH_COMPAT |
| 124 | //config: bool "bash-compatible extensions" |
| 125 | //config: default y |
| 126 | //config: depends on HUSH |
| 127 | //config: help |
| 128 | //config: Enable bash-compatible extensions. |
| 129 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 130 | //config:config HUSH_BRACE_EXPANSION |
| 131 | //config: bool "Brace expansion" |
| 132 | //config: default y |
| 133 | //config: depends on HUSH_BASH_COMPAT |
| 134 | //config: help |
| 135 | //config: Enable {abc,def} extension. |
| 136 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 137 | //config:config HUSH_HELP |
| 138 | //config: bool "help builtin" |
| 139 | //config: default y |
| 140 | //config: depends on HUSH |
| 141 | //config: help |
| 142 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 143 | //config: |
| 144 | //config:config HUSH_INTERACTIVE |
| 145 | //config: bool "Interactive mode" |
| 146 | //config: default y |
| 147 | //config: depends on HUSH |
| 148 | //config: help |
| 149 | //config: Enable interactive mode (prompt and command editing). |
| 150 | //config: Without this, hush simply reads and executes commands |
| 151 | //config: from stdin just like a shell script from a file. |
| 152 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 153 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 154 | //config:config HUSH_SAVEHISTORY |
| 155 | //config: bool "Save command history to .hush_history" |
| 156 | //config: default y |
| 157 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 158 | //config: help |
| 159 | //config: Enable history saving in hush. |
| 160 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 161 | //config:config HUSH_JOB |
| 162 | //config: bool "Job control" |
| 163 | //config: default y |
| 164 | //config: depends on HUSH_INTERACTIVE |
| 165 | //config: help |
| 166 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 167 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 168 | //config: "cmd &" still works by simply spawning a process and immediately |
| 169 | //config: prompting for next command (or executing next command in a script), |
| 170 | //config: but no separate process group is formed. |
| 171 | //config: |
| 172 | //config:config HUSH_TICK |
| 173 | //config: bool "Process substitution" |
| 174 | //config: default y |
| 175 | //config: depends on HUSH |
| 176 | //config: help |
| 177 | //config: Enable process substitution `command` and $(command) in hush. |
| 178 | //config: |
| 179 | //config:config HUSH_IF |
| 180 | //config: bool "Support if/then/elif/else/fi" |
| 181 | //config: default y |
| 182 | //config: depends on HUSH |
| 183 | //config: help |
| 184 | //config: Enable if/then/elif/else/fi in hush. |
| 185 | //config: |
| 186 | //config:config HUSH_LOOPS |
| 187 | //config: bool "Support for, while and until loops" |
| 188 | //config: default y |
| 189 | //config: depends on HUSH |
| 190 | //config: help |
| 191 | //config: Enable for, while and until loops in hush. |
| 192 | //config: |
| 193 | //config:config HUSH_CASE |
| 194 | //config: bool "Support case ... esac statement" |
| 195 | //config: default y |
| 196 | //config: depends on HUSH |
| 197 | //config: help |
| 198 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 199 | //config: |
| 200 | //config:config HUSH_FUNCTIONS |
| 201 | //config: bool "Support funcname() { commands; } syntax" |
| 202 | //config: default y |
| 203 | //config: depends on HUSH |
| 204 | //config: help |
| 205 | //config: Enable support for shell functions in hush. +800 bytes. |
| 206 | //config: |
| 207 | //config:config HUSH_LOCAL |
| 208 | //config: bool "Support local builtin" |
| 209 | //config: default y |
| 210 | //config: depends on HUSH_FUNCTIONS |
| 211 | //config: help |
| 212 | //config: Enable support for local variables in functions. |
| 213 | //config: |
| 214 | //config:config HUSH_RANDOM_SUPPORT |
| 215 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 216 | //config: default y |
| 217 | //config: depends on HUSH |
| 218 | //config: help |
| 219 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 220 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 221 | //config: |
| 222 | //config:config HUSH_EXPORT_N |
| 223 | //config: bool "Support 'export -n' option" |
| 224 | //config: default y |
| 225 | //config: depends on HUSH |
| 226 | //config: help |
| 227 | //config: export -n unexports variables. It is a bash extension. |
| 228 | //config: |
| 229 | //config:config HUSH_MODE_X |
| 230 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 231 | //config: default y |
| 232 | //config: depends on HUSH |
| 233 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 234 | //config: This instructs hush to print commands before execution. |
| 235 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 236 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 237 | //config:config MSH |
| 238 | //config: bool "msh (deprecated: aliased to hush)" |
| 239 | //config: default n |
| 240 | //config: select HUSH |
| 241 | //config: help |
| 242 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 243 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 244 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 245 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
| 246 | //applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP)) |
| 247 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 248 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 249 | |
| 250 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 251 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 252 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 253 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 254 | * but currently do nothing, therefore aren't shown in help. |
| 255 | * NOMMU-specific options are not meant to be used by users, |
| 256 | * therefore we don't show them either. |
| 257 | */ |
| 258 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 259 | //usage: "[-nxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 260 | //usage:#define hush_full_usage "\n\n" |
| 261 | //usage: "Unix shell interpreter" |
| 262 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 263 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 264 | //usage:#define msh_full_usage hush_full_usage |
| 265 | |
| 266 | //usage:#if ENABLE_FEATURE_SH_IS_HUSH |
| 267 | //usage:# define sh_trivial_usage hush_trivial_usage |
| 268 | //usage:# define sh_full_usage hush_full_usage |
| 269 | //usage:#endif |
| 270 | //usage:#if ENABLE_FEATURE_BASH_IS_HUSH |
| 271 | //usage:# define bash_trivial_usage hush_trivial_usage |
| 272 | //usage:# define bash_full_usage hush_full_usage |
| 273 | //usage:#endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 274 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 275 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 276 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 277 | #define LEAK_HUNTING 0 |
| 278 | #define BUILD_AS_NOMMU 0 |
| 279 | /* Enable/disable sanity checks. Ok to enable in production, |
| 280 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 281 | * Keeping 1 for now even in released versions. |
| 282 | */ |
| 283 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 284 | /* Slightly bigger (+200 bytes), but faster hush. |
| 285 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 286 | * which allows us to do fewer waitpid's. |
| 287 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 288 | * and therefore waitpid will return the same result as last time) |
| 289 | */ |
| 290 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 291 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 292 | * So far ${var%...} ops are always enabled: |
| 293 | */ |
| 294 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 295 | |
| 296 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 297 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 298 | # undef BB_MMU |
| 299 | # undef USE_FOR_NOMMU |
| 300 | # undef USE_FOR_MMU |
| 301 | # define BB_MMU 0 |
| 302 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 303 | # define USE_FOR_MMU(...) |
| 304 | #endif |
| 305 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 306 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 307 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 308 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 309 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 310 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 311 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 312 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 313 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 314 | # define IF_FEATURE_SH_STANDALONE(...) |
| 315 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 316 | #endif |
| 317 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 318 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 319 | # undef ENABLE_FEATURE_EDITING |
| 320 | # define ENABLE_FEATURE_EDITING 0 |
| 321 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 322 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 323 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 324 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 325 | #endif |
| 326 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 327 | /* Do we support ANY keywords? */ |
| 328 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 329 | # define HAS_KEYWORDS 1 |
| 330 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 331 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 332 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 333 | # define HAS_KEYWORDS 0 |
| 334 | # define IF_HAS_KEYWORDS(...) |
| 335 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 336 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 338 | /* If you comment out one of these below, it will be #defined later |
| 339 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 340 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 341 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 342 | #define debug_printf_parse(...) do {} while (0) |
| 343 | #define debug_print_tree(a, b) do {} while (0) |
| 344 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 345 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 346 | #define debug_printf_jobs(...) do {} while (0) |
| 347 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 348 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 349 | #define debug_printf_glob(...) do {} while (0) |
| 350 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 351 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 352 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 353 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 354 | #define ERR_PTR ((void*)(long)1) |
| 355 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 356 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 357 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 358 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 359 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 360 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 361 | #if ENABLE_HUSH_BASH_COMPAT |
| 362 | /* Support / and // replace ops */ |
| 363 | /* Note that // is stored as \ in "encoded" string representation */ |
| 364 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 365 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 366 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 367 | #else |
| 368 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 369 | # define VAR_SUBST_OPS "%#:-=+?" |
| 370 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 371 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 372 | |
| 373 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 374 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 375 | struct variable; |
| 376 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 377 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 378 | |
| 379 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 380 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 381 | */ |
| 382 | #if !BB_MMU |
| 383 | typedef struct nommu_save_t { |
| 384 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 385 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 386 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 387 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 388 | } nommu_save_t; |
| 389 | #endif |
| 390 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 391 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 392 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 393 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 394 | RES_IF , |
| 395 | RES_THEN , |
| 396 | RES_ELIF , |
| 397 | RES_ELSE , |
| 398 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 399 | #endif |
| 400 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 401 | RES_FOR , |
| 402 | RES_WHILE , |
| 403 | RES_UNTIL , |
| 404 | RES_DO , |
| 405 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 406 | #endif |
| 407 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 408 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 409 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 410 | #if ENABLE_HUSH_CASE |
| 411 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 412 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 413 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 414 | RES_MATCH , /* "word)" */ |
| 415 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 416 | RES_ESAC , |
| 417 | #endif |
| 418 | RES_XXXX , |
| 419 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 420 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 421 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 422 | typedef struct o_string { |
| 423 | char *data; |
| 424 | int length; /* position where data is appended */ |
| 425 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 426 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 427 | /* At least some part of the string was inside '' or "", |
| 428 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 429 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 430 | smallint has_empty_slot; |
| 431 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 432 | } o_string; |
| 433 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 434 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 435 | EXP_FLAG_GLOB = 0x2, |
| 436 | /* Protect newly added chars against globbing |
| 437 | * by prepending \ to *, ?, [, \ */ |
| 438 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 439 | }; |
| 440 | enum { |
| 441 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 442 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 443 | NOT_ASSIGNMENT = 2, |
| 444 | /* Not an assigment, but next word may be: "if v=xyz cmd;" */ |
| 445 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 446 | }; |
| 447 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 448 | #define NULL_O_STRING { NULL } |
| 449 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 450 | #ifndef debug_printf_parse |
| 451 | static const char *const assignment_flag[] = { |
| 452 | "MAYBE_ASSIGNMENT", |
| 453 | "DEFINITELY_ASSIGNMENT", |
| 454 | "NOT_ASSIGNMENT", |
| 455 | "WORD_IS_KEYWORD", |
| 456 | }; |
| 457 | #endif |
| 458 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 459 | typedef struct in_str { |
| 460 | const char *p; |
| 461 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 462 | char eof_flag; /* meaningless if ->p == NULL */ |
| 463 | char peek_buf[2]; |
| 464 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 465 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 466 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 467 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 468 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 469 | int (*get) (struct in_str *) FAST_FUNC; |
| 470 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 471 | } in_str; |
| 472 | #define i_getch(input) ((input)->get(input)) |
| 473 | #define i_peek(input) ((input)->peek(input)) |
| 474 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 475 | /* The descrip member of this structure is only used to make |
| 476 | * debugging output pretty */ |
| 477 | static const struct { |
| 478 | int mode; |
| 479 | signed char default_fd; |
| 480 | char descrip[3]; |
| 481 | } redir_table[] = { |
| 482 | { O_RDONLY, 0, "<" }, |
| 483 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 484 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 485 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 486 | { O_RDONLY, 0, "<<" }, |
| 487 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 488 | /* { O_RDONLY, 77, "<<" }, */ |
| 489 | }; |
| 490 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 491 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 492 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 493 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 494 | int rd_fd; /* fd to redirect */ |
| 495 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 496 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 497 | smallint rd_type; /* (enum redir_type) */ |
| 498 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 499 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 500 | * bit 0: do we need to trim leading tabs? |
| 501 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 502 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 503 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 504 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 505 | REDIRECT_INPUT = 0, |
| 506 | REDIRECT_OVERWRITE = 1, |
| 507 | REDIRECT_APPEND = 2, |
| 508 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 509 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 510 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 511 | |
| 512 | REDIRFD_CLOSE = -3, |
| 513 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 514 | REDIRFD_TO_FILE = -1, |
| 515 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 516 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 517 | HEREDOC_SKIPTABS = 1, |
| 518 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 519 | } redir_type; |
| 520 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 521 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 522 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 523 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 524 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 525 | smallint cmd_type; /* CMD_xxx */ |
| 526 | #define CMD_NORMAL 0 |
| 527 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 528 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 529 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 530 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 531 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 532 | #if ENABLE_HUSH_FUNCTIONS |
| 533 | # define CMD_FUNCDEF 3 |
| 534 | #endif |
| 535 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 536 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 537 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 538 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 539 | #if !BB_MMU |
| 540 | char *group_as_string; |
| 541 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 542 | #if ENABLE_HUSH_FUNCTIONS |
| 543 | struct function *child_func; |
| 544 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 545 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 546 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 547 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 548 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 549 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 550 | * we put those fields back into cmd->xxx |
| 551 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 552 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 553 | * Without this trick, loop would execute a;b;b;b;... |
| 554 | * instead of correct sequence a;b;a;b;... |
| 555 | * When command is freed, it severs the link |
| 556 | * (sets ->child_func->parent_cmd to NULL). |
| 557 | */ |
| 558 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 559 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 560 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 561 | * and on execution these are substituted with their values. |
| 562 | * Substitution can make _several_ words out of one argv[n]! |
| 563 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 564 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 565 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 566 | struct redir_struct *redirects; /* I/O redirections */ |
| 567 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 568 | /* Is there anything in this command at all? */ |
| 569 | #define IS_NULL_CMD(cmd) \ |
| 570 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 571 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 572 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 573 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 574 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 575 | int alive_cmds; /* number of commands running (not exited) */ |
| 576 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 577 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 578 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 579 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 580 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 581 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 582 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 583 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 584 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 585 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 586 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 587 | typedef enum pipe_style { |
| 588 | PIPE_SEQ = 1, |
| 589 | PIPE_AND = 2, |
| 590 | PIPE_OR = 3, |
| 591 | PIPE_BG = 4, |
| 592 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 593 | /* Is there anything in this pipe at all? */ |
| 594 | #define IS_NULL_PIPE(pi) \ |
| 595 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 596 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 597 | /* This holds pointers to the various results of parsing */ |
| 598 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 599 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 600 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 601 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 602 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 603 | /* last command in pipe (being constructed right now) */ |
| 604 | struct command *command; |
| 605 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 606 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 607 | #if !BB_MMU |
| 608 | o_string as_string; |
| 609 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 610 | #if HAS_KEYWORDS |
| 611 | smallint ctx_res_w; |
| 612 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 613 | #if ENABLE_HUSH_CASE |
| 614 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 615 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 616 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 617 | int old_flag; |
| 618 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 619 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 620 | * when we see "if" or "then", we malloc and copy current context, |
| 621 | * and make ->stack point to it. then we parse pipeN. |
| 622 | * when closing "then" / fi" / whatever is found, |
| 623 | * we move list_head into ->stack->command->group, |
| 624 | * copy ->stack into current context, and delete ->stack. |
| 625 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 626 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 627 | struct parse_context *stack; |
| 628 | #endif |
| 629 | }; |
| 630 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 631 | /* On program start, environ points to initial environment. |
| 632 | * putenv adds new pointers into it, unsetenv removes them. |
| 633 | * Neither of these (de)allocates the strings. |
| 634 | * setenv allocates new strings in malloc space and does putenv, |
| 635 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 636 | #define setenv(...) setenv_is_leaky_dont_use() |
| 637 | struct variable { |
| 638 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 639 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 640 | #if ENABLE_HUSH_LOCAL |
| 641 | unsigned func_nest_level; |
| 642 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 643 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 644 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 645 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 648 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 649 | BC_BREAK = 1, |
| 650 | BC_CONTINUE = 2, |
| 651 | }; |
| 652 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 653 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 654 | struct function { |
| 655 | struct function *next; |
| 656 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 657 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 658 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 659 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 660 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 661 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 662 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 663 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 664 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 665 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 666 | /* set -/+o OPT support. (TODO: make it optional) |
| 667 | * bash supports the following opts: |
| 668 | * allexport off |
| 669 | * braceexpand on |
| 670 | * emacs on |
| 671 | * errexit off |
| 672 | * errtrace off |
| 673 | * functrace off |
| 674 | * hashall on |
| 675 | * histexpand off |
| 676 | * history on |
| 677 | * ignoreeof off |
| 678 | * interactive-comments on |
| 679 | * keyword off |
| 680 | * monitor on |
| 681 | * noclobber off |
| 682 | * noexec off |
| 683 | * noglob off |
| 684 | * nolog off |
| 685 | * notify off |
| 686 | * nounset off |
| 687 | * onecmd off |
| 688 | * physical off |
| 689 | * pipefail off |
| 690 | * posix off |
| 691 | * privileged off |
| 692 | * verbose off |
| 693 | * vi off |
| 694 | * xtrace off |
| 695 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 696 | static const char o_opt_strings[] ALIGN1 = |
| 697 | "pipefail\0" |
| 698 | "noexec\0" |
| 699 | #if ENABLE_HUSH_MODE_X |
| 700 | "xtrace\0" |
| 701 | #endif |
| 702 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 703 | enum { |
| 704 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 705 | OPT_O_NOEXEC, |
| 706 | #if ENABLE_HUSH_MODE_X |
| 707 | OPT_O_XTRACE, |
| 708 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 709 | NUM_OPT_O |
| 710 | }; |
| 711 | |
| 712 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 713 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 714 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 715 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 716 | /* interactive_fd != 0 means we are an interactive shell. |
| 717 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 718 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 719 | * job control still works, but terminal signals |
| 720 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 721 | * process groups can only be created with "cmd &". |
| 722 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 723 | * to give tty to the foreground process group, |
| 724 | * and will take it back when the group is stopped (^Z) |
| 725 | * or killed (^C). |
| 726 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 727 | #if ENABLE_HUSH_INTERACTIVE |
| 728 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 729 | * _AND_ if we decided to act interactively */ |
| 730 | int interactive_fd; |
| 731 | const char *PS1; |
| 732 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 733 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 734 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 735 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 736 | #endif |
| 737 | #if ENABLE_FEATURE_EDITING |
| 738 | line_input_t *line_input_state; |
| 739 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 740 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 741 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 742 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 743 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 744 | random_t random_gen; |
| 745 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 746 | #if ENABLE_HUSH_JOB |
| 747 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 748 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 749 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 750 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 751 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 752 | #else |
| 753 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 754 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 755 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 756 | #if ENABLE_HUSH_MODE_X |
| 757 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 758 | #else |
| 759 | # define G_x_mode 0 |
| 760 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 761 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 762 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 763 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 764 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 765 | #if ENABLE_HUSH_FUNCTIONS |
| 766 | /* 0: outside of a function (or sourced file) |
| 767 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 768 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 769 | */ |
| 770 | smallint flag_return_in_progress; |
| 771 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 772 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 773 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 774 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 775 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 776 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 777 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 778 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 779 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 780 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 781 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 782 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 783 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 784 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 785 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 786 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 787 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 788 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 789 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 790 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 791 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 792 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 793 | # if ENABLE_HUSH_LOCAL |
| 794 | struct variable **shadowed_vars_pp; |
| 795 | unsigned func_nest_level; |
| 796 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 797 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 798 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 799 | #if ENABLE_HUSH_FAST |
| 800 | unsigned count_SIGCHLD; |
| 801 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 802 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 803 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 804 | /* Which signals have non-DFL handler (even with no traps set)? |
| 805 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 806 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 807 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 808 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 809 | * Other than these two times, never modified. |
| 810 | */ |
| 811 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 812 | #if ENABLE_HUSH_JOB |
| 813 | unsigned fatal_sig_mask; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 814 | # define G_fatal_sig_mask G.fatal_sig_mask |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 815 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 816 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 817 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 818 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 819 | sigset_t pending_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 820 | #if HUSH_DEBUG |
| 821 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 822 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 823 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 824 | struct sigaction sa; |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 825 | 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] | 826 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 827 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 828 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 829 | * (too many defines). Also, I actually prefer to see when a variable |
| 830 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 831 | #define INIT_G() do { \ |
| 832 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 833 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 834 | sigfillset(&G.sa.sa_mask); \ |
| 835 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 836 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 837 | |
| 838 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 839 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 840 | static int builtin_cd(char **argv) FAST_FUNC; |
| 841 | static int builtin_echo(char **argv) FAST_FUNC; |
| 842 | static int builtin_eval(char **argv) FAST_FUNC; |
| 843 | static int builtin_exec(char **argv) FAST_FUNC; |
| 844 | static int builtin_exit(char **argv) FAST_FUNC; |
| 845 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 846 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 847 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 848 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 849 | #endif |
| 850 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 851 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 852 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 853 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 854 | static int builtin_history(char **argv) FAST_FUNC; |
| 855 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 856 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 857 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 858 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 859 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 860 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 861 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 862 | #if ENABLE_PRINTF |
| 863 | static int builtin_printf(char **argv) FAST_FUNC; |
| 864 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 865 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 866 | static int builtin_read(char **argv) FAST_FUNC; |
| 867 | static int builtin_set(char **argv) FAST_FUNC; |
| 868 | static int builtin_shift(char **argv) FAST_FUNC; |
| 869 | static int builtin_source(char **argv) FAST_FUNC; |
| 870 | static int builtin_test(char **argv) FAST_FUNC; |
| 871 | static int builtin_trap(char **argv) FAST_FUNC; |
| 872 | static int builtin_type(char **argv) FAST_FUNC; |
| 873 | static int builtin_true(char **argv) FAST_FUNC; |
| 874 | static int builtin_umask(char **argv) FAST_FUNC; |
| 875 | static int builtin_unset(char **argv) FAST_FUNC; |
| 876 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 877 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 878 | static int builtin_break(char **argv) FAST_FUNC; |
| 879 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 880 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 881 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 882 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 883 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 884 | |
| 885 | /* Table of built-in functions. They can be forked or not, depending on |
| 886 | * context: within pipes, they fork. As simple commands, they do not. |
| 887 | * When used in non-forking context, they can change global variables |
| 888 | * in the parent shell process. If forked, of course they cannot. |
| 889 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 890 | * still be set at the end. */ |
| 891 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 892 | const char *b_cmd; |
| 893 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 894 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 895 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 896 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 897 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 898 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 899 | #endif |
| 900 | }; |
| 901 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 902 | static const struct built_in_command bltins1[] = { |
| 903 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 904 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 905 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 906 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 907 | #endif |
| 908 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 909 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 910 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 911 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 912 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 913 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 914 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 915 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 916 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 917 | BLTIN("exit" , builtin_exit , "Exit"), |
| 918 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 919 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 920 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 921 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 922 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 923 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 924 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 925 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 926 | BLTIN("history" , builtin_history , "Show command history"), |
| 927 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 928 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 929 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 930 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 931 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 932 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 933 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 934 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 935 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 936 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 937 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 938 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 939 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 940 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 941 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 942 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 943 | #if ENABLE_HUSH_BASH_COMPAT |
| 944 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 945 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 946 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 947 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 948 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 949 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 950 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 951 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 952 | }; |
| 953 | /* For now, echo and test are unconditionally enabled. |
| 954 | * Maybe make it configurable? */ |
| 955 | static const struct built_in_command bltins2[] = { |
| 956 | BLTIN("[" , builtin_test , NULL), |
| 957 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 958 | #if ENABLE_PRINTF |
| 959 | BLTIN("printf" , builtin_printf , NULL), |
| 960 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 961 | BLTIN("pwd" , builtin_pwd , NULL), |
| 962 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 963 | }; |
| 964 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 965 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 966 | /* Debug printouts. |
| 967 | */ |
| 968 | #if HUSH_DEBUG |
| 969 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 970 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 971 | # define debug_enter() (G.debug_indent++) |
| 972 | # define debug_leave() (G.debug_indent--) |
| 973 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 974 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 975 | # define debug_enter() ((void)0) |
| 976 | # define debug_leave() ((void)0) |
| 977 | #endif |
| 978 | |
| 979 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 980 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 981 | #endif |
| 982 | |
| 983 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 984 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 985 | #endif |
| 986 | |
| 987 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 988 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 989 | #endif |
| 990 | |
| 991 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 992 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 993 | #endif |
| 994 | |
| 995 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 996 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 997 | # define DEBUG_JOBS 1 |
| 998 | #else |
| 999 | # define DEBUG_JOBS 0 |
| 1000 | #endif |
| 1001 | |
| 1002 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1003 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1004 | # define DEBUG_EXPAND 1 |
| 1005 | #else |
| 1006 | # define DEBUG_EXPAND 0 |
| 1007 | #endif |
| 1008 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1009 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1010 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1011 | #endif |
| 1012 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1013 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1014 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1015 | # define DEBUG_GLOB 1 |
| 1016 | #else |
| 1017 | # define DEBUG_GLOB 0 |
| 1018 | #endif |
| 1019 | |
| 1020 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1021 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1022 | #endif |
| 1023 | |
| 1024 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1025 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1026 | #endif |
| 1027 | |
| 1028 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1029 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1030 | # define DEBUG_CLEAN 1 |
| 1031 | #else |
| 1032 | # define DEBUG_CLEAN 0 |
| 1033 | #endif |
| 1034 | |
| 1035 | #if DEBUG_EXPAND |
| 1036 | static void debug_print_strings(const char *prefix, char **vv) |
| 1037 | { |
| 1038 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1039 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1040 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1041 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1042 | } |
| 1043 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1044 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1045 | #endif |
| 1046 | |
| 1047 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1048 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1049 | */ |
| 1050 | #if LEAK_HUNTING |
| 1051 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1052 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1053 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1054 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1055 | return ptr; |
| 1056 | } |
| 1057 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1058 | { |
| 1059 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1060 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1061 | return ptr; |
| 1062 | } |
| 1063 | static char *xxstrdup(int lineno, const char *str) |
| 1064 | { |
| 1065 | char *ptr = xstrdup(str); |
| 1066 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1067 | return ptr; |
| 1068 | } |
| 1069 | static void xxfree(void *ptr) |
| 1070 | { |
| 1071 | fdprintf(2, "free %p\n", ptr); |
| 1072 | free(ptr); |
| 1073 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1074 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1075 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1076 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1077 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1078 | #endif |
| 1079 | |
| 1080 | |
| 1081 | /* Syntax and runtime errors. They always abort scripts. |
| 1082 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1083 | * and return to the prompt. |
| 1084 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1085 | */ |
| 1086 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1087 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1088 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1089 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1090 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1091 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1092 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1093 | #endif |
| 1094 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1095 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1096 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1097 | va_list p; |
| 1098 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1099 | #if HUSH_DEBUG >= 2 |
| 1100 | bb_error_msg("hush.c:%u", lineno); |
| 1101 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1102 | va_start(p, fmt); |
| 1103 | bb_verror_msg(fmt, p, NULL); |
| 1104 | va_end(p); |
| 1105 | if (!G_interactive_fd) |
| 1106 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1107 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1108 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1109 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1110 | { |
| 1111 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1112 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1113 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1114 | bb_error_msg("syntax error"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1117 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1118 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1119 | bb_error_msg("syntax error at '%s'", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1122 | static void syntax_error_unterm_str(unsigned lineno UNUSED_PARAM, const char *s) |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1123 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1124 | bb_error_msg("syntax error: unterminated %s", s); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1125 | } |
| 1126 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1127 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1128 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1129 | char msg[2] = { ch, '\0' }; |
| 1130 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1133 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1134 | { |
| 1135 | char msg[2]; |
| 1136 | msg[0] = ch; |
| 1137 | msg[1] = '\0'; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1138 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1141 | #if HUSH_DEBUG < 2 |
| 1142 | # undef die_if_script |
| 1143 | # undef syntax_error |
| 1144 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1145 | # undef syntax_error_unterm_ch |
| 1146 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1147 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1148 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1149 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1150 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1151 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1152 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1153 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1154 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1155 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1156 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1157 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1158 | #if ENABLE_HUSH_INTERACTIVE |
| 1159 | static void cmdedit_update_prompt(void); |
| 1160 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1161 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1162 | #endif |
| 1163 | |
| 1164 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1165 | /* Utility functions |
| 1166 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1167 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1168 | static char *unbackslash(char *src) |
| 1169 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1170 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1171 | while (1) { |
| 1172 | if (*src == '\\') |
| 1173 | src++; |
| 1174 | if ((*dst++ = *src++) == '\0') |
| 1175 | break; |
| 1176 | } |
| 1177 | return dst; |
| 1178 | } |
| 1179 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1180 | 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] | 1181 | { |
| 1182 | int i; |
| 1183 | unsigned count1; |
| 1184 | unsigned count2; |
| 1185 | char **v; |
| 1186 | |
| 1187 | v = strings; |
| 1188 | count1 = 0; |
| 1189 | if (v) { |
| 1190 | while (*v) { |
| 1191 | count1++; |
| 1192 | v++; |
| 1193 | } |
| 1194 | } |
| 1195 | count2 = 0; |
| 1196 | v = add; |
| 1197 | while (*v) { |
| 1198 | count2++; |
| 1199 | v++; |
| 1200 | } |
| 1201 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1202 | v[count1 + count2] = NULL; |
| 1203 | i = count2; |
| 1204 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1205 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1206 | return v; |
| 1207 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1208 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1209 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1210 | { |
| 1211 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1212 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1213 | return ptr; |
| 1214 | } |
| 1215 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1216 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1217 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1218 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1219 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1220 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1221 | { |
| 1222 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1223 | v[0] = add; |
| 1224 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1225 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1226 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1227 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1228 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1229 | { |
| 1230 | char **ptr = add_string_to_strings(strings, add); |
| 1231 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1232 | return ptr; |
| 1233 | } |
| 1234 | #define add_string_to_strings(strings, add) \ |
| 1235 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1236 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1237 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1238 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1239 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1240 | char **v; |
| 1241 | |
| 1242 | if (!strings) |
| 1243 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1244 | v = strings; |
| 1245 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1246 | free(*v); |
| 1247 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1248 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1249 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1252 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1253 | /* Helpers for setting new $n and restoring them back |
| 1254 | */ |
| 1255 | typedef struct save_arg_t { |
| 1256 | char *sv_argv0; |
| 1257 | char **sv_g_argv; |
| 1258 | int sv_g_argc; |
| 1259 | smallint sv_g_malloced; |
| 1260 | } save_arg_t; |
| 1261 | |
| 1262 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1263 | { |
| 1264 | int n; |
| 1265 | |
| 1266 | sv->sv_argv0 = argv[0]; |
| 1267 | sv->sv_g_argv = G.global_argv; |
| 1268 | sv->sv_g_argc = G.global_argc; |
| 1269 | sv->sv_g_malloced = G.global_args_malloced; |
| 1270 | |
| 1271 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1272 | G.global_argv = argv; |
| 1273 | G.global_args_malloced = 0; |
| 1274 | |
| 1275 | n = 1; |
| 1276 | while (*++argv) |
| 1277 | n++; |
| 1278 | G.global_argc = n; |
| 1279 | } |
| 1280 | |
| 1281 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1282 | { |
| 1283 | char **pp; |
| 1284 | |
| 1285 | if (G.global_args_malloced) { |
| 1286 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1287 | pp = G.global_argv; |
| 1288 | while (*++pp) /* note: does not free $0 */ |
| 1289 | free(*pp); |
| 1290 | free(G.global_argv); |
| 1291 | } |
| 1292 | argv[0] = sv->sv_argv0; |
| 1293 | G.global_argv = sv->sv_g_argv; |
| 1294 | G.global_argc = sv->sv_g_argc; |
| 1295 | G.global_args_malloced = sv->sv_g_malloced; |
| 1296 | } |
| 1297 | |
| 1298 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1299 | /* Basic theory of signal handling in shell |
| 1300 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1301 | * This does not describe what hush does, rather, it is current understanding |
| 1302 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1303 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1304 | * |
| 1305 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1306 | * is finished or backgrounded. It is the same in interactive and |
| 1307 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1308 | * 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] | 1309 | * ^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] | 1310 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1311 | * pipe. |
| 1312 | * |
| 1313 | * Wait builtin in interruptible by signals for which user trap is set |
| 1314 | * or by SIGINT in interactive shell. |
| 1315 | * |
| 1316 | * Trap handlers will execute even within trap handlers. (right?) |
| 1317 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1318 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1319 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1320 | * |
| 1321 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1322 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1323 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1324 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1325 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1326 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1327 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1328 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1329 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1330 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1331 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1332 | * |
| 1333 | * SIGQUIT: ignore |
| 1334 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1335 | * SIGHUP (interactive): |
| 1336 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1337 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1338 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1339 | * that all pipe members are stopped. Try this in bash: |
| 1340 | * while :; do :; done - ^Z does not background it |
| 1341 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1342 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1343 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1344 | * to interactive shell while shell is waiting for a pipe, |
| 1345 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1346 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1347 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1348 | * Example 2: this does not wait and does not execute ls: |
| 1349 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1350 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1351 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1352 | * Example 4: this does not wait and does not execute ls: |
| 1353 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1354 | * |
| 1355 | * (What happens to signals which are IGN on shell start?) |
| 1356 | * (What happens with signal mask on shell start?) |
| 1357 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1358 | * Old implementation |
| 1359 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1360 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1361 | * We block all signals which we don't want to take action immediately, |
| 1362 | * i.e. we block all signals which need to have special handling as described |
| 1363 | * above, and all signals which have traps set. |
| 1364 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1365 | * and act on them. |
| 1366 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1367 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1368 | * sigset_t blocked_set: current blocked signal set |
| 1369 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1370 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1371 | * clear bit in blocked_set unless it is also in special_sig_mask |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1372 | * "trap 'cmd' SIGxxx": |
| 1373 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1374 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1375 | * unblock signals with special interactive handling |
| 1376 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1377 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1378 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1379 | * 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] | 1380 | * 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] | 1381 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1382 | * 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] | 1383 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1384 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1385 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1386 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1387 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1388 | * 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] | 1389 | * are set to '' (ignore) are NOT reset to defaults. We do the same. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1390 | * |
| 1391 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1392 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1393 | * masked signals are not visible! |
| 1394 | * |
| 1395 | * New implementation |
| 1396 | * ================== |
| 1397 | * We record each signal we are interested in by installing signal handler |
| 1398 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1399 | * We are interested in: signals which need to have special handling |
| 1400 | * as described above, and all signals which have traps set. |
| 1401 | * Signals are rocorded in pending_set. |
| 1402 | * After each pipe execution, we extract any pending signals |
| 1403 | * and act on them. |
| 1404 | * |
| 1405 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1406 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1407 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1408 | * sigset_t pending_set: set of sigs we received. |
| 1409 | * |
| 1410 | * "trap - SIGxxx": |
| 1411 | * if sig is in special_sig_mask, set handler back to: |
| 1412 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1413 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1414 | * else: set handler back to SIG_DFL |
| 1415 | * "trap 'cmd' SIGxxx": |
| 1416 | * set handler to record_pending_signo. |
| 1417 | * "trap '' SIGxxx": |
| 1418 | * set handler to SIG_IGN. |
| 1419 | * after [v]fork, if we plan to be a shell: |
| 1420 | * set signals with special interactive handling to SIG_DFL |
| 1421 | * (because child shell is not interactive), |
| 1422 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1423 | * after [v]fork, if we plan to exec: |
| 1424 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1425 | * |
| 1426 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1427 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1428 | * |
| 1429 | * Note (compat): |
| 1430 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1431 | * are set to the default actions". bash interprets it so that traps which |
| 1432 | * 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] | 1433 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1434 | enum { |
| 1435 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1436 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1437 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1438 | | (1 << SIGHUP) |
| 1439 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1440 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1441 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1442 | | (1 << SIGTTIN) |
| 1443 | | (1 << SIGTTOU) |
| 1444 | | (1 << SIGTSTP) |
| 1445 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1446 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1447 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1448 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1449 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1450 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1451 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1452 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1453 | if (sig == SIGCHLD) { |
| 1454 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1455 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1456 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1457 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1458 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1459 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1460 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1461 | { |
| 1462 | struct sigaction old_sa; |
| 1463 | |
| 1464 | /* We could use signal() to install handlers... almost: |
| 1465 | * except that we need to mask ALL signals while handlers run. |
| 1466 | * I saw signal nesting in strace, race window isn't small. |
| 1467 | * SA_RESTART is also needed, but in Linux, signal() |
| 1468 | * sets SA_RESTART too. |
| 1469 | */ |
| 1470 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1471 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1472 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1473 | G.sa.sa_handler = handler; |
| 1474 | sigaction(sig, &G.sa, &old_sa); |
| 1475 | return old_sa.sa_handler; |
| 1476 | } |
| 1477 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1478 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1479 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1480 | /* 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] | 1481 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1482 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1483 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1484 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1485 | /* Restores tty foreground process group, and exits. |
| 1486 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1487 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1488 | * or called directly with -EXITCODE. |
| 1489 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1490 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1491 | static void sigexit(int sig) |
| 1492 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1493 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1494 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1495 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1496 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1497 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1498 | */ |
| 1499 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1500 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1501 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1502 | |
| 1503 | /* Not a signal, just exit */ |
| 1504 | if (sig <= 0) |
| 1505 | _exit(- sig); |
| 1506 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1507 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1508 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1509 | #else |
| 1510 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1511 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1512 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1513 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1514 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1515 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1516 | static sighandler_t pick_sighandler(unsigned sig) |
| 1517 | { |
| 1518 | sighandler_t handler = SIG_DFL; |
| 1519 | if (sig < sizeof(unsigned)*8) { |
| 1520 | unsigned sigmask = (1 << sig); |
| 1521 | |
| 1522 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1523 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1524 | if (G_fatal_sig_mask & sigmask) |
| 1525 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1526 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1527 | #endif |
| 1528 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1529 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1530 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1531 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1532 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1533 | * in an endless loop when we try to do some |
| 1534 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1535 | */ |
| 1536 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1537 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1538 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1539 | } |
| 1540 | return handler; |
| 1541 | } |
| 1542 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1543 | /* Restores tty foreground process group, and exits. */ |
| 1544 | static void hush_exit(int exitcode) NORETURN; |
| 1545 | static void hush_exit(int exitcode) |
| 1546 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1547 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1548 | save_history(G.line_input_state); |
| 1549 | #endif |
| 1550 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1551 | fflush_all(); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1552 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1553 | char *argv[3]; |
| 1554 | /* argv[0] is unused */ |
| 1555 | argv[1] = G.traps[0]; |
| 1556 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1557 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1558 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1559 | * "trap" will still show it, if executed |
| 1560 | * in the handler */ |
| 1561 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1562 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1563 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1564 | #if ENABLE_FEATURE_CLEAN_UP |
| 1565 | { |
| 1566 | struct variable *cur_var; |
| 1567 | if (G.cwd != bb_msg_unknown) |
| 1568 | free((char*)G.cwd); |
| 1569 | cur_var = G.top_var; |
| 1570 | while (cur_var) { |
| 1571 | struct variable *tmp = cur_var; |
| 1572 | if (!cur_var->max_len) |
| 1573 | free(cur_var->varstr); |
| 1574 | cur_var = cur_var->next; |
| 1575 | free(tmp); |
| 1576 | } |
| 1577 | } |
| 1578 | #endif |
| 1579 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1580 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1581 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1582 | sigexit(- (exitcode & 0xff)); |
| 1583 | #else |
| 1584 | exit(exitcode); |
| 1585 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1588 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1589 | //TODO: return a mask of ALL handled sigs? |
| 1590 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1591 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1592 | int last_sig = 0; |
| 1593 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1594 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1595 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1596 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1597 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1598 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1599 | sig = 0; |
| 1600 | do { |
| 1601 | sig++; |
| 1602 | if (sigismember(&G.pending_set, sig)) { |
| 1603 | sigdelset(&G.pending_set, sig); |
| 1604 | goto got_sig; |
| 1605 | } |
| 1606 | } while (sig < NSIG); |
| 1607 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1608 | got_sig: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1609 | if (G.traps && G.traps[sig]) { |
| 1610 | if (G.traps[sig][0]) { |
| 1611 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1612 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1613 | char *argv[3]; |
| 1614 | /* argv[0] is unused */ |
| 1615 | argv[1] = G.traps[sig]; |
| 1616 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1617 | save_rcode = G.last_exitcode; |
| 1618 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1619 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1620 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1621 | } /* else: "" trap, ignoring signal */ |
| 1622 | continue; |
| 1623 | } |
| 1624 | /* not a trap: special action */ |
| 1625 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1626 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1627 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1628 | bb_putchar('\n'); |
| 1629 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1630 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1631 | break; |
| 1632 | #if ENABLE_HUSH_JOB |
| 1633 | case SIGHUP: { |
| 1634 | struct pipe *job; |
| 1635 | /* bash is observed to signal whole process groups, |
| 1636 | * not individual processes */ |
| 1637 | for (job = G.job_list; job; job = job->next) { |
| 1638 | if (job->pgrp <= 0) |
| 1639 | continue; |
| 1640 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1641 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1642 | kill(- job->pgrp, SIGCONT); |
| 1643 | } |
| 1644 | sigexit(SIGHUP); |
| 1645 | } |
| 1646 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1647 | #if ENABLE_HUSH_FAST |
| 1648 | case SIGCHLD: |
| 1649 | G.count_SIGCHLD++; |
| 1650 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1651 | /* Note: |
| 1652 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1653 | * This simplifies wait builtin a bit. |
| 1654 | */ |
| 1655 | break; |
| 1656 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1657 | default: /* ignored: */ |
| 1658 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1659 | /* Note: |
| 1660 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1661 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1662 | * in interactive shell, because TERM is ignored. |
| 1663 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | return last_sig; |
| 1668 | } |
| 1669 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1670 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1671 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1672 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1673 | if (force || G.cwd == NULL) { |
| 1674 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1675 | * we must not try to free(bb_msg_unknown) */ |
| 1676 | if (G.cwd == bb_msg_unknown) |
| 1677 | G.cwd = NULL; |
| 1678 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1679 | if (!G.cwd) |
| 1680 | G.cwd = bb_msg_unknown; |
| 1681 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1682 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1685 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1686 | /* |
| 1687 | * Shell and environment variable support |
| 1688 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1689 | 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] | 1690 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1691 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1692 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1693 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1694 | pp = &G.top_var; |
| 1695 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1696 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1697 | return pp; |
| 1698 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1699 | } |
| 1700 | return NULL; |
| 1701 | } |
| 1702 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1703 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1704 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1705 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1706 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1707 | |
| 1708 | if (G.expanded_assignments) { |
| 1709 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1710 | while (*cpp) { |
| 1711 | char *cp = *cpp; |
| 1712 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1713 | return cp + len + 1; |
| 1714 | cpp++; |
| 1715 | } |
| 1716 | } |
| 1717 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1718 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1719 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1720 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1721 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1722 | if (strcmp(name, "PPID") == 0) |
| 1723 | return utoa(G.root_ppid); |
| 1724 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1725 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1726 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1727 | return utoa(next_random(&G.random_gen)); |
| 1728 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1729 | return NULL; |
| 1730 | } |
| 1731 | |
| 1732 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1733 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1734 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1735 | * 0: do not change export flag |
| 1736 | * (if creating new variable, flag will be 0) |
| 1737 | * 1: set export flag and putenv the variable |
| 1738 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1739 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1740 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1741 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1742 | /* all params are used */ |
| 1743 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1744 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1745 | set_local_var(str, flg_export, local_lvl) |
| 1746 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1747 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1748 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1749 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1750 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1751 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1752 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1753 | 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] | 1754 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1755 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1756 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1757 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1758 | int name_len; |
| 1759 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1760 | eq_sign = strchr(str, '='); |
| 1761 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1762 | free(str); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1766 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1767 | var_pp = &G.top_var; |
| 1768 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1769 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1770 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1771 | continue; |
| 1772 | } |
| 1773 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1774 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1775 | #if !BB_MMU |
| 1776 | if (!flg_read_only) |
| 1777 | #endif |
| 1778 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1779 | free(str); |
| 1780 | return -1; |
| 1781 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1782 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1783 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1784 | *eq_sign = '\0'; |
| 1785 | unsetenv(str); |
| 1786 | *eq_sign = '='; |
| 1787 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1788 | #if ENABLE_HUSH_LOCAL |
| 1789 | if (cur->func_nest_level < local_lvl) { |
| 1790 | /* New variable is declared as local, |
| 1791 | * and existing one is global, or local |
| 1792 | * from enclosing function. |
| 1793 | * Remove and save old one: */ |
| 1794 | *var_pp = cur->next; |
| 1795 | cur->next = *G.shadowed_vars_pp; |
| 1796 | *G.shadowed_vars_pp = cur; |
| 1797 | /* bash 3.2.33(1) and exported vars: |
| 1798 | * # export z=z |
| 1799 | * # f() { local z=a; env | grep ^z; } |
| 1800 | * # f |
| 1801 | * z=a |
| 1802 | * # env | grep ^z |
| 1803 | * z=z |
| 1804 | */ |
| 1805 | if (cur->flg_export) |
| 1806 | flg_export = 1; |
| 1807 | break; |
| 1808 | } |
| 1809 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1810 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1811 | free_and_exp: |
| 1812 | free(str); |
| 1813 | goto exp; |
| 1814 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1815 | if (cur->max_len != 0) { |
| 1816 | if (cur->max_len >= strlen(str)) { |
| 1817 | /* This one is from startup env, reuse space */ |
| 1818 | strcpy(cur->varstr, str); |
| 1819 | goto free_and_exp; |
| 1820 | } |
| 1821 | } else { |
| 1822 | /* max_len == 0 signifies "malloced" var, which we can |
| 1823 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1824 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1825 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1826 | cur->max_len = 0; |
| 1827 | goto set_str_and_exp; |
| 1828 | } |
| 1829 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1830 | /* Not found - create new variable struct */ |
| 1831 | cur = xzalloc(sizeof(*cur)); |
| 1832 | #if ENABLE_HUSH_LOCAL |
| 1833 | cur->func_nest_level = local_lvl; |
| 1834 | #endif |
| 1835 | cur->next = *var_pp; |
| 1836 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1837 | |
| 1838 | set_str_and_exp: |
| 1839 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1840 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1841 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1842 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1843 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1844 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1845 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1846 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1847 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1848 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1849 | if (flg_export == -1) { |
| 1850 | cur->flg_export = 0; |
| 1851 | /* unsetenv was already done */ |
| 1852 | } else { |
| 1853 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1854 | return putenv(cur->varstr); |
| 1855 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1856 | } |
| 1857 | return 0; |
| 1858 | } |
| 1859 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1860 | /* Used at startup and after each cd */ |
| 1861 | static void set_pwd_var(int exp) |
| 1862 | { |
| 1863 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1864 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1865 | } |
| 1866 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1867 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1868 | { |
| 1869 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1870 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1871 | |
| 1872 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1873 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1874 | var_pp = &G.top_var; |
| 1875 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1876 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1877 | if (cur->flg_read_only) { |
| 1878 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1879 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1880 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1881 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1882 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1883 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1884 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1885 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1886 | if (!cur->max_len) |
| 1887 | free(cur->varstr); |
| 1888 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1889 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1890 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1891 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1892 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1893 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1896 | static int unset_local_var(const char *name) |
| 1897 | { |
| 1898 | return unset_local_var_len(name, strlen(name)); |
| 1899 | } |
| 1900 | |
| 1901 | static void unset_vars(char **strings) |
| 1902 | { |
| 1903 | char **v; |
| 1904 | |
| 1905 | if (!strings) |
| 1906 | return; |
| 1907 | v = strings; |
| 1908 | while (*v) { |
| 1909 | const char *eq = strchrnul(*v, '='); |
| 1910 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1911 | v++; |
| 1912 | } |
| 1913 | free(strings); |
| 1914 | } |
| 1915 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1916 | 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] | 1917 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1918 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1919 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1920 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1921 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1922 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1923 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1924 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1925 | */ |
| 1926 | static void add_vars(struct variable *var) |
| 1927 | { |
| 1928 | struct variable *next; |
| 1929 | |
| 1930 | while (var) { |
| 1931 | next = var->next; |
| 1932 | var->next = G.top_var; |
| 1933 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1934 | if (var->flg_export) { |
| 1935 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1936 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1937 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1938 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1939 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1940 | var = next; |
| 1941 | } |
| 1942 | } |
| 1943 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1944 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1945 | { |
| 1946 | char **s; |
| 1947 | struct variable *old = NULL; |
| 1948 | |
| 1949 | if (!strings) |
| 1950 | return old; |
| 1951 | s = strings; |
| 1952 | while (*s) { |
| 1953 | struct variable *var_p; |
| 1954 | struct variable **var_pp; |
| 1955 | char *eq; |
| 1956 | |
| 1957 | eq = strchr(*s, '='); |
| 1958 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1959 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1960 | if (var_pp) { |
| 1961 | /* Remove variable from global linked list */ |
| 1962 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1963 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1964 | *var_pp = var_p->next; |
| 1965 | /* Add it to returned list */ |
| 1966 | var_p->next = old; |
| 1967 | old = var_p; |
| 1968 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1969 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1970 | } |
| 1971 | s++; |
| 1972 | } |
| 1973 | return old; |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1978 | * in_str support |
| 1979 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1980 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1981 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1982 | int ch = *i->p; |
| 1983 | if (ch != '\0') { |
| 1984 | i->p++; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1985 | i->last_char = ch; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1986 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1987 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1988 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1991 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1992 | { |
| 1993 | return *i->p; |
| 1994 | } |
| 1995 | |
| 1996 | #if ENABLE_HUSH_INTERACTIVE |
| 1997 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1998 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1999 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2000 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2001 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2002 | if (G.PS1 == NULL) |
| 2003 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2004 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2005 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2006 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2007 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2008 | if (G.PS2 == NULL) |
| 2009 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2010 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2011 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2012 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2013 | { |
| 2014 | const char *prompt_str; |
| 2015 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2016 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2017 | /* Set up the prompt */ |
| 2018 | if (promptmode == 0) { /* PS1 */ |
| 2019 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2020 | /* bash uses $PWD value, even if it is set by user. |
| 2021 | * It uses current dir only if PWD is unset. |
| 2022 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2023 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2024 | prompt_str = G.PS1; |
| 2025 | } else |
| 2026 | prompt_str = G.PS2; |
| 2027 | } else |
| 2028 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2029 | debug_printf("result '%s'\n", prompt_str); |
| 2030 | return prompt_str; |
| 2031 | } |
| 2032 | |
| 2033 | static void get_user_input(struct in_str *i) |
| 2034 | { |
| 2035 | int r; |
| 2036 | const char *prompt_str; |
| 2037 | |
| 2038 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2039 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2040 | /* Enable command line editing only while a command line |
| 2041 | * is actually being read */ |
| 2042 | do { |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 2043 | /* Unicode support should be activated even if LANG is set |
| 2044 | * _during_ shell execution, not only if it was set when |
| 2045 | * shell was started. Therefore, re-check LANG every time: |
| 2046 | */ |
Denys Vlasenko | 3e7ecb1 | 2013-07-02 17:30:23 +0200 | [diff] [blame] | 2047 | const char *s = get_local_var_value("LC_ALL"); |
Denys Vlasenko | 2301d12 | 2013-07-05 22:00:57 +0200 | [diff] [blame] | 2048 | if (!s) s = get_local_var_value("LC_CTYPE"); |
Denys Vlasenko | 3e7ecb1 | 2013-07-02 17:30:23 +0200 | [diff] [blame] | 2049 | if (!s) s = get_local_var_value("LANG"); |
| 2050 | reinit_unicode(s); |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 2051 | |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2052 | G.flag_SIGINT = 0; |
| 2053 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 2054 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 2055 | r = read_line_input(G.line_input_state, prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, /*timeout*/ -1); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2056 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2057 | check_and_run_traps(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2058 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2059 | i->eof_flag = (r < 0); |
| 2060 | if (i->eof_flag) { /* EOF/error detected */ |
| 2061 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 2062 | G.user_input_buf[1] = '\0'; |
| 2063 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2064 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2065 | do { |
| 2066 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2067 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2068 | /* Why check_and_run_traps here? Try this interactively: |
| 2069 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2070 | * $ <[enter], repeatedly...> |
| 2071 | * Without check_and_run_traps, handler never runs. |
| 2072 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2073 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2074 | fputs(prompt_str, stdout); |
| 2075 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2076 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2077 | G.user_input_buf[0] = r = fgetc(i->file); |
| 2078 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2079 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2080 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2081 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2082 | i->p = G.user_input_buf; |
| 2083 | } |
| 2084 | |
| 2085 | #endif /* INTERACTIVE */ |
| 2086 | |
| 2087 | /* This is the magic location that prints prompts |
| 2088 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2089 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2090 | { |
| 2091 | int ch; |
| 2092 | |
| 2093 | /* If there is data waiting, eat it up */ |
| 2094 | if (i->p && *i->p) { |
| 2095 | #if ENABLE_HUSH_INTERACTIVE |
| 2096 | take_cached: |
| 2097 | #endif |
| 2098 | ch = *i->p++; |
| 2099 | if (i->eof_flag && !*i->p) |
| 2100 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2101 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2102 | } else { |
| 2103 | /* need to double check i->file because we might be doing something |
| 2104 | * more complicated by now, like sourcing or substituting. */ |
| 2105 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2106 | if (G_interactive_fd && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2107 | do { |
| 2108 | get_user_input(i); |
| 2109 | } while (!*i->p); /* need non-empty line */ |
| 2110 | i->promptmode = 1; /* PS2 */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2111 | goto take_cached; |
| 2112 | } |
| 2113 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2114 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2115 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2116 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2117 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2118 | return ch; |
| 2119 | } |
| 2120 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2121 | /* All callers guarantee this routine will never |
| 2122 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2123 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2124 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2125 | { |
| 2126 | int ch; |
| 2127 | if (i->p && *i->p) { |
| 2128 | if (i->eof_flag && !i->p[1]) |
| 2129 | return EOF; |
| 2130 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2131 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2132 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2133 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2134 | i->eof_flag = (ch == EOF); |
| 2135 | i->peek_buf[0] = ch; |
| 2136 | i->peek_buf[1] = '\0'; |
| 2137 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2138 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2139 | return ch; |
| 2140 | } |
| 2141 | |
| 2142 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2143 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2144 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2145 | i->peek = file_peek; |
| 2146 | i->get = file_get; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2147 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2148 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2149 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2153 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2154 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2155 | i->peek = static_peek; |
| 2156 | i->get = static_get; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2157 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2158 | i->p = s; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2159 | /* i->eof_flag = 0; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2163 | /* |
| 2164 | * o_string support |
| 2165 | */ |
| 2166 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2167 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2168 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2169 | { |
| 2170 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2171 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2172 | if (o->data) |
| 2173 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2176 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2177 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2178 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2179 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2182 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2183 | { |
| 2184 | free(o->data); |
| 2185 | } |
| 2186 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2187 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2188 | { |
| 2189 | if (o->length + len > o->maxlen) { |
| 2190 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 2191 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2192 | } |
| 2193 | } |
| 2194 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2195 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2196 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2197 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 2198 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2199 | o->data[o->length] = ch; |
| 2200 | o->length++; |
| 2201 | o->data[o->length] = '\0'; |
| 2202 | } |
| 2203 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2204 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2205 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2206 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2207 | memcpy(&o->data[o->length], str, len); |
| 2208 | o->length += len; |
| 2209 | o->data[o->length] = '\0'; |
| 2210 | } |
| 2211 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2212 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2213 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2214 | o_addblock(o, str, strlen(str)); |
| 2215 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2216 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2217 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2218 | static void nommu_addchr(o_string *o, int ch) |
| 2219 | { |
| 2220 | if (o) |
| 2221 | o_addchr(o, ch); |
| 2222 | } |
| 2223 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2224 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2225 | #endif |
| 2226 | |
| 2227 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2228 | { |
| 2229 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2232 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2233 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2234 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2235 | * Apparently, on unquoted $v bash still does globbing |
| 2236 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2237 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2238 | * quoting mechanisms on $v expansion side: one protects |
| 2239 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2240 | * We have only second one. |
| 2241 | */ |
| 2242 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2243 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2244 | # define MAYBE_BRACES "{}" |
| 2245 | #else |
| 2246 | # define MAYBE_BRACES "" |
| 2247 | #endif |
| 2248 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2249 | /* My analysis of quoting semantics tells me that state information |
| 2250 | * is associated with a destination, not a source. |
| 2251 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2252 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2253 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2254 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2255 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2256 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2257 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2258 | o_grow_by(o, sz); |
| 2259 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2260 | o->data[o->length] = '\\'; |
| 2261 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2262 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2263 | o->data[o->length] = ch; |
| 2264 | o->length++; |
| 2265 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2268 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2269 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2270 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2271 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2272 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2273 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2274 | sz++; |
| 2275 | o->data[o->length] = '\\'; |
| 2276 | o->length++; |
| 2277 | } |
| 2278 | o_grow_by(o, sz); |
| 2279 | o->data[o->length] = ch; |
| 2280 | o->length++; |
| 2281 | o->data[o->length] = '\0'; |
| 2282 | } |
| 2283 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2284 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2285 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2286 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2287 | char ch; |
| 2288 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2289 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2290 | if (ordinary_cnt > len) /* paranoia */ |
| 2291 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2292 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2293 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2294 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2295 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2296 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2297 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2298 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2299 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2300 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2301 | sz++; |
| 2302 | o->data[o->length] = '\\'; |
| 2303 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2304 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2305 | o_grow_by(o, sz); |
| 2306 | o->data[o->length] = ch; |
| 2307 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2308 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2309 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2312 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2313 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2314 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2315 | o_addblock(o, str, len); |
| 2316 | return; |
| 2317 | } |
| 2318 | o_addqblock(o, str, len); |
| 2319 | } |
| 2320 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2321 | static void o_addQstr(o_string *o, const char *str) |
| 2322 | { |
| 2323 | o_addQblock(o, str, strlen(str)); |
| 2324 | } |
| 2325 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2326 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2327 | * 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] | 2328 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2329 | * list[i] contains an INDEX (int!) into this string data. |
| 2330 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2331 | * but list[i]'s need not be modified. |
| 2332 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2333 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2334 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2335 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2336 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2337 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2338 | { |
| 2339 | char **list = (char**)o->data; |
| 2340 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2341 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2342 | |
| 2343 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2344 | fdprintf(2, "%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] | 2345 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2346 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2347 | o->has_quoted_part, |
| 2348 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2349 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2350 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2351 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2352 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2353 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2354 | i++; |
| 2355 | } |
| 2356 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2357 | const char *p = o->data + (int)(uintptr_t)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2358 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2359 | fdprintf(2, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2363 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2364 | #endif |
| 2365 | |
| 2366 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2367 | * in list[n] so that it points past last stored byte so far. |
| 2368 | * It returns n+1. */ |
| 2369 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2370 | { |
| 2371 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2372 | int string_start; |
| 2373 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2376 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2377 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2378 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2379 | 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] | 2380 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2381 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2382 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2383 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2384 | memmove(list + n + 0x10, list + n, string_len); |
| 2385 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2386 | } else { |
| 2387 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2388 | n, string_len, string_start); |
| 2389 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2390 | } else { |
| 2391 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2392 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2393 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2394 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2395 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2396 | o->has_empty_slot = 0; |
| 2397 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2398 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2399 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2400 | return n + 1; |
| 2401 | } |
| 2402 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2403 | /* "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] | 2404 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2405 | { |
| 2406 | char **list = (char**)o->data; |
| 2407 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2408 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2409 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2412 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2413 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2414 | * first, it processes even {a} (no commas), second, |
| 2415 | * 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] | 2416 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2417 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* Helper */ |
| 2420 | static int glob_needed(const char *s) |
| 2421 | { |
| 2422 | while (*s) { |
| 2423 | if (*s == '\\') { |
| 2424 | if (!s[1]) |
| 2425 | return 0; |
| 2426 | s += 2; |
| 2427 | continue; |
| 2428 | } |
| 2429 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2430 | return 1; |
| 2431 | s++; |
| 2432 | } |
| 2433 | return 0; |
| 2434 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2435 | /* Return pointer to next closing brace or to comma */ |
| 2436 | static const char *next_brace_sub(const char *cp) |
| 2437 | { |
| 2438 | unsigned depth = 0; |
| 2439 | cp++; |
| 2440 | while (*cp != '\0') { |
| 2441 | if (*cp == '\\') { |
| 2442 | if (*++cp == '\0') |
| 2443 | break; |
| 2444 | cp++; |
| 2445 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2446 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2447 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2448 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2449 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2450 | depth++; |
| 2451 | } |
| 2452 | |
| 2453 | return *cp != '\0' ? cp : NULL; |
| 2454 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2455 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2456 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2457 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2458 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2459 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2460 | const char *next; |
| 2461 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2462 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2463 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2464 | |
| 2465 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2466 | |
| 2467 | begin = pattern; |
| 2468 | while (1) { |
| 2469 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2470 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2471 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2472 | /* Find the first sub-pattern and at the same time |
| 2473 | * find the rest after the closing brace */ |
| 2474 | next = next_brace_sub(begin); |
| 2475 | if (next == NULL) { |
| 2476 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2477 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2478 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2479 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2480 | /* "{abc}" with no commas - illegal |
| 2481 | * brace expr, disregard and skip it */ |
| 2482 | begin = next + 1; |
| 2483 | continue; |
| 2484 | } |
| 2485 | break; |
| 2486 | } |
| 2487 | if (*begin == '\\' && begin[1] != '\0') |
| 2488 | begin++; |
| 2489 | begin++; |
| 2490 | } |
| 2491 | debug_printf_glob("begin:%s\n", begin); |
| 2492 | debug_printf_glob("next:%s\n", next); |
| 2493 | |
| 2494 | /* Now find the end of the whole brace expression */ |
| 2495 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2496 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2497 | rest = next_brace_sub(rest); |
| 2498 | if (rest == NULL) { |
| 2499 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2500 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2501 | } |
| 2502 | debug_printf_glob("rest:%s\n", rest); |
| 2503 | } |
| 2504 | rest_len = strlen(++rest) + 1; |
| 2505 | |
| 2506 | /* We are sure the brace expression is well-formed */ |
| 2507 | |
| 2508 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2509 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2510 | |
| 2511 | /* We have a brace expression. BEGIN points to the opening {, |
| 2512 | * NEXT points past the terminator of the first element, and REST |
| 2513 | * points past the final }. We will accumulate result names from |
| 2514 | * recursive runs for each brace alternative in the buffer using |
| 2515 | * GLOB_APPEND. */ |
| 2516 | |
| 2517 | p = begin + 1; |
| 2518 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2519 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2520 | memcpy( |
| 2521 | mempcpy( |
| 2522 | mempcpy(new_pattern_buf, |
| 2523 | /* We know the prefix for all sub-patterns */ |
| 2524 | pattern, begin - pattern), |
| 2525 | p, next - p), |
| 2526 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2527 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2528 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2529 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2530 | */ |
| 2531 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2532 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2533 | /* We saw the last entry */ |
| 2534 | break; |
| 2535 | } |
| 2536 | p = next + 1; |
| 2537 | next = next_brace_sub(next); |
| 2538 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2539 | free(new_pattern_buf); |
| 2540 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2541 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2542 | simple_glob: |
| 2543 | { |
| 2544 | int gr; |
| 2545 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2546 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2547 | memset(&globdata, 0, sizeof(globdata)); |
| 2548 | gr = glob(pattern, 0, NULL, &globdata); |
| 2549 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2550 | if (gr != 0) { |
| 2551 | if (gr == GLOB_NOMATCH) { |
| 2552 | globfree(&globdata); |
| 2553 | /* NB: garbles parameter */ |
| 2554 | unbackslash(pattern); |
| 2555 | o_addstr_with_NUL(o, pattern); |
| 2556 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2557 | return o_save_ptr_helper(o, n); |
| 2558 | } |
| 2559 | if (gr == GLOB_NOSPACE) |
| 2560 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2561 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2562 | * but we didn't specify it. Paranoia again. */ |
| 2563 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2564 | } |
| 2565 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2566 | char **argv = globdata.gl_pathv; |
| 2567 | while (1) { |
| 2568 | o_addstr_with_NUL(o, *argv); |
| 2569 | n = o_save_ptr_helper(o, n); |
| 2570 | argv++; |
| 2571 | if (!*argv) |
| 2572 | break; |
| 2573 | } |
| 2574 | } |
| 2575 | globfree(&globdata); |
| 2576 | } |
| 2577 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2578 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2579 | /* Performs globbing on last list[], |
| 2580 | * saving each result as a new list[]. |
| 2581 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2582 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2583 | { |
| 2584 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2585 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2586 | 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] | 2587 | if (!o->data) |
| 2588 | return o_save_ptr_helper(o, n); |
| 2589 | pattern = o->data + o_get_last_ptr(o, n); |
| 2590 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2591 | if (!glob_needed(pattern)) { |
| 2592 | /* unbackslash last string in o in place, fix length */ |
| 2593 | o->length = unbackslash(pattern) - o->data; |
| 2594 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2595 | return o_save_ptr_helper(o, n); |
| 2596 | } |
| 2597 | |
| 2598 | copy = xstrdup(pattern); |
| 2599 | /* "forget" pattern in o */ |
| 2600 | o->length = pattern - o->data; |
| 2601 | n = glob_brace(copy, o, n); |
| 2602 | free(copy); |
| 2603 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2604 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2605 | return n; |
| 2606 | } |
| 2607 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2608 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* Helper */ |
| 2611 | static int glob_needed(const char *s) |
| 2612 | { |
| 2613 | while (*s) { |
| 2614 | if (*s == '\\') { |
| 2615 | if (!s[1]) |
| 2616 | return 0; |
| 2617 | s += 2; |
| 2618 | continue; |
| 2619 | } |
| 2620 | if (*s == '*' || *s == '[' || *s == '?') |
| 2621 | return 1; |
| 2622 | s++; |
| 2623 | } |
| 2624 | return 0; |
| 2625 | } |
| 2626 | /* Performs globbing on last list[], |
| 2627 | * saving each result as a new list[]. |
| 2628 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2629 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2630 | { |
| 2631 | glob_t globdata; |
| 2632 | int gr; |
| 2633 | char *pattern; |
| 2634 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2635 | 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] | 2636 | if (!o->data) |
| 2637 | return o_save_ptr_helper(o, n); |
| 2638 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2639 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2640 | if (!glob_needed(pattern)) { |
| 2641 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2642 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2643 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2644 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2645 | return o_save_ptr_helper(o, n); |
| 2646 | } |
| 2647 | |
| 2648 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2649 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2650 | * If we glob "*.\*" and don't find anything, we need |
| 2651 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2652 | * will return "*.\*"! |
| 2653 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2654 | gr = glob(pattern, 0, NULL, &globdata); |
| 2655 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2656 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2657 | if (gr == GLOB_NOMATCH) { |
| 2658 | globfree(&globdata); |
| 2659 | goto literal; |
| 2660 | } |
| 2661 | if (gr == GLOB_NOSPACE) |
| 2662 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2663 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2664 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2665 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2666 | } |
| 2667 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2668 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2669 | /* "forget" pattern in o */ |
| 2670 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2671 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2672 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2673 | n = o_save_ptr_helper(o, n); |
| 2674 | argv++; |
| 2675 | if (!*argv) |
| 2676 | break; |
| 2677 | } |
| 2678 | } |
| 2679 | globfree(&globdata); |
| 2680 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2681 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2682 | return n; |
| 2683 | } |
| 2684 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2685 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2686 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2687 | /* 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] | 2688 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2689 | static int o_save_ptr(o_string *o, int n) |
| 2690 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2691 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2692 | /* If o->has_empty_slot, list[n] was already globbed |
| 2693 | * (if it was requested back then when it was filled) |
| 2694 | * so don't do that again! */ |
| 2695 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2696 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2697 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2698 | return o_save_ptr_helper(o, n); |
| 2699 | } |
| 2700 | |
| 2701 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2702 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2703 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2704 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2705 | int string_start; |
| 2706 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2707 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2708 | if (DEBUG_EXPAND) |
| 2709 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2710 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2711 | list = (char**)o->data; |
| 2712 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2713 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2714 | while (n) { |
| 2715 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2716 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2717 | } |
| 2718 | return list; |
| 2719 | } |
| 2720 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2721 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2722 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2723 | /* Returns pi->next - next pipe in the list */ |
| 2724 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2725 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2726 | struct pipe *next; |
| 2727 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2728 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2729 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2730 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2731 | struct command *command; |
| 2732 | struct redir_struct *r, *rnext; |
| 2733 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2734 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2735 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2736 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2737 | if (DEBUG_CLEAN) { |
| 2738 | int a; |
| 2739 | char **p; |
| 2740 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2741 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2742 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2743 | } |
| 2744 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2745 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2746 | } |
| 2747 | /* not "else if": on syntax error, we may have both! */ |
| 2748 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2749 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2750 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2751 | free_pipe_list(command->group); |
| 2752 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2753 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2754 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2755 | /* else is crucial here. |
| 2756 | * If group != NULL, child_func is meaningless */ |
| 2757 | #if ENABLE_HUSH_FUNCTIONS |
| 2758 | else if (command->child_func) { |
| 2759 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2760 | command->child_func->parent_cmd = NULL; |
| 2761 | } |
| 2762 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2763 | #if !BB_MMU |
| 2764 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2765 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2766 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2767 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2768 | debug_printf_clean(" redirect %d%s", |
| 2769 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2770 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2771 | if (r->rd_filename) { |
| 2772 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2773 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2774 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2775 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2776 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2777 | rnext = r->next; |
| 2778 | free(r); |
| 2779 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2780 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2781 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2782 | 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] | 2783 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2784 | #if ENABLE_HUSH_JOB |
| 2785 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2786 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2787 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2788 | |
| 2789 | next = pi->next; |
| 2790 | free(pi); |
| 2791 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2792 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2793 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2794 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2795 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2796 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2797 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2798 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2799 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2800 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2801 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2802 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 2806 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2807 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2808 | #ifndef debug_print_tree |
| 2809 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2810 | { |
| 2811 | static const char *const PIPE[] = { |
| 2812 | [PIPE_SEQ] = "SEQ", |
| 2813 | [PIPE_AND] = "AND", |
| 2814 | [PIPE_OR ] = "OR" , |
| 2815 | [PIPE_BG ] = "BG" , |
| 2816 | }; |
| 2817 | static const char *RES[] = { |
| 2818 | [RES_NONE ] = "NONE" , |
| 2819 | # if ENABLE_HUSH_IF |
| 2820 | [RES_IF ] = "IF" , |
| 2821 | [RES_THEN ] = "THEN" , |
| 2822 | [RES_ELIF ] = "ELIF" , |
| 2823 | [RES_ELSE ] = "ELSE" , |
| 2824 | [RES_FI ] = "FI" , |
| 2825 | # endif |
| 2826 | # if ENABLE_HUSH_LOOPS |
| 2827 | [RES_FOR ] = "FOR" , |
| 2828 | [RES_WHILE] = "WHILE", |
| 2829 | [RES_UNTIL] = "UNTIL", |
| 2830 | [RES_DO ] = "DO" , |
| 2831 | [RES_DONE ] = "DONE" , |
| 2832 | # endif |
| 2833 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 2834 | [RES_IN ] = "IN" , |
| 2835 | # endif |
| 2836 | # if ENABLE_HUSH_CASE |
| 2837 | [RES_CASE ] = "CASE" , |
| 2838 | [RES_CASE_IN ] = "CASE_IN" , |
| 2839 | [RES_MATCH] = "MATCH", |
| 2840 | [RES_CASE_BODY] = "CASE_BODY", |
| 2841 | [RES_ESAC ] = "ESAC" , |
| 2842 | # endif |
| 2843 | [RES_XXXX ] = "XXXX" , |
| 2844 | [RES_SNTX ] = "SNTX" , |
| 2845 | }; |
| 2846 | static const char *const CMDTYPE[] = { |
| 2847 | "{}", |
| 2848 | "()", |
| 2849 | "[noglob]", |
| 2850 | # if ENABLE_HUSH_FUNCTIONS |
| 2851 | "func()", |
| 2852 | # endif |
| 2853 | }; |
| 2854 | |
| 2855 | int pin, prn; |
| 2856 | |
| 2857 | pin = 0; |
| 2858 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2859 | fdprintf(2, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2860 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 2861 | prn = 0; |
| 2862 | while (prn < pi->num_cmds) { |
| 2863 | struct command *command = &pi->cmds[prn]; |
| 2864 | char **argv = command->argv; |
| 2865 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2866 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2867 | lvl*2, "", prn, |
| 2868 | command->assignment_cnt); |
| 2869 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2870 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2871 | CMDTYPE[command->cmd_type], |
| 2872 | argv |
| 2873 | # if !BB_MMU |
| 2874 | , " group_as_string:", command->group_as_string |
| 2875 | # else |
| 2876 | , "", "" |
| 2877 | # endif |
| 2878 | ); |
| 2879 | debug_print_tree(command->group, lvl+1); |
| 2880 | prn++; |
| 2881 | continue; |
| 2882 | } |
| 2883 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2884 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2885 | argv++; |
| 2886 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2887 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2888 | prn++; |
| 2889 | } |
| 2890 | pi = pi->next; |
| 2891 | pin++; |
| 2892 | } |
| 2893 | } |
| 2894 | #endif /* debug_print_tree */ |
| 2895 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2896 | static struct pipe *new_pipe(void) |
| 2897 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2898 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2899 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2900 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2901 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2902 | return pi; |
| 2903 | } |
| 2904 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2905 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 2906 | * if ctx->command is NULL. |
| 2907 | * No errors possible here. |
| 2908 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2909 | static int done_command(struct parse_context *ctx) |
| 2910 | { |
| 2911 | /* The command is really already in the pipe structure, so |
| 2912 | * advance the pipe counter and make a new, null command. */ |
| 2913 | struct pipe *pi = ctx->pipe; |
| 2914 | struct command *command = ctx->command; |
| 2915 | |
| 2916 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2917 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2918 | 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] | 2919 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2920 | } |
| 2921 | pi->num_cmds++; |
| 2922 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2923 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2924 | } else { |
| 2925 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 2926 | } |
| 2927 | |
| 2928 | /* Only real trickiness here is that the uncommitted |
| 2929 | * command structure is not counted in pi->num_cmds. */ |
| 2930 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2931 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 2932 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2933 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2934 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 2935 | } |
| 2936 | |
| 2937 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 2938 | { |
| 2939 | int not_null; |
| 2940 | |
| 2941 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 2942 | /* Close previous command */ |
| 2943 | not_null = done_command(ctx); |
| 2944 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2945 | #if HAS_KEYWORDS |
| 2946 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 2947 | ctx->ctx_inverted = 0; |
| 2948 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 2949 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2950 | |
| 2951 | /* Without this check, even just <enter> on command line generates |
| 2952 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2953 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2954 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2955 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2956 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2957 | #endif |
| 2958 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2959 | || ctx->ctx_res_w == RES_DONE |
| 2960 | || ctx->ctx_res_w == RES_FOR |
| 2961 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2962 | #endif |
| 2963 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2964 | || ctx->ctx_res_w == RES_ESAC |
| 2965 | #endif |
| 2966 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2967 | struct pipe *new_p; |
| 2968 | debug_printf_parse("done_pipe: adding new pipe: " |
| 2969 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 2970 | not_null, ctx->ctx_res_w); |
| 2971 | new_p = new_pipe(); |
| 2972 | ctx->pipe->next = new_p; |
| 2973 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2974 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2975 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2976 | * This is used to control execution. |
| 2977 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 2978 | * cases where variable or value happens to match a keyword): |
| 2979 | */ |
| 2980 | #if ENABLE_HUSH_LOOPS |
| 2981 | if (ctx->ctx_res_w == RES_FOR |
| 2982 | || ctx->ctx_res_w == RES_IN) |
| 2983 | ctx->ctx_res_w = RES_NONE; |
| 2984 | #endif |
| 2985 | #if ENABLE_HUSH_CASE |
| 2986 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2987 | ctx->ctx_res_w = RES_CASE_BODY; |
| 2988 | if (ctx->ctx_res_w == RES_CASE) |
| 2989 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2990 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2991 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2992 | /* Create the memory for command, roughly: |
| 2993 | * ctx->pipe->cmds = new struct command; |
| 2994 | * ctx->command = &ctx->pipe->cmds[0]; |
| 2995 | */ |
| 2996 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2997 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2998 | } |
| 2999 | debug_printf_parse("done_pipe return\n"); |
| 3000 | } |
| 3001 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3002 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3003 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3004 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3005 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3006 | /* Create the memory for command, roughly: |
| 3007 | * ctx->pipe->cmds = new struct command; |
| 3008 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3009 | */ |
| 3010 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3013 | /* If a reserved word is found and processed, parse context is modified |
| 3014 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3015 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3016 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3017 | struct reserved_combo { |
| 3018 | char literal[6]; |
| 3019 | unsigned char res; |
| 3020 | unsigned char assignment_flag; |
| 3021 | int flag; |
| 3022 | }; |
| 3023 | enum { |
| 3024 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3025 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3026 | FLAG_IF = (1 << RES_IF ), |
| 3027 | FLAG_THEN = (1 << RES_THEN ), |
| 3028 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3029 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3030 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3031 | # endif |
| 3032 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3033 | FLAG_FOR = (1 << RES_FOR ), |
| 3034 | FLAG_WHILE = (1 << RES_WHILE), |
| 3035 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3036 | FLAG_DO = (1 << RES_DO ), |
| 3037 | FLAG_DONE = (1 << RES_DONE ), |
| 3038 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3039 | # endif |
| 3040 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3041 | FLAG_MATCH = (1 << RES_MATCH), |
| 3042 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3043 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3044 | FLAG_START = (1 << RES_XXXX ), |
| 3045 | }; |
| 3046 | |
| 3047 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3048 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3049 | /* Mostly a list of accepted follow-up reserved words. |
| 3050 | * FLAG_END means we are done with the sequence, and are ready |
| 3051 | * to turn the compound list into a command. |
| 3052 | * FLAG_START means the word must start a new compound list. |
| 3053 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3054 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3055 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3056 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3057 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3058 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3059 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3060 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3061 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3062 | # endif |
| 3063 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3064 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3065 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3066 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3067 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3068 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3069 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3070 | # endif |
| 3071 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3072 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3073 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3074 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3075 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3076 | const struct reserved_combo *r; |
| 3077 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3078 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3079 | if (strcmp(word->data, r->literal) == 0) |
| 3080 | return r; |
| 3081 | } |
| 3082 | return NULL; |
| 3083 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3084 | /* Return 0: not a keyword, 1: keyword |
| 3085 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3086 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3087 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3088 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3089 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3090 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3091 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3092 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3093 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3094 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3095 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3096 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3097 | r = match_reserved_word(word); |
| 3098 | if (!r) |
| 3099 | return 0; |
| 3100 | |
| 3101 | 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] | 3102 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3103 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3104 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3105 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3106 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3107 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3108 | if (r->flag == 0) { /* '!' */ |
| 3109 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3110 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3111 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3112 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3113 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3114 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3115 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3116 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3117 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3118 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3119 | old = xmalloc(sizeof(*old)); |
| 3120 | debug_printf_parse("push stack %p\n", old); |
| 3121 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3122 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3123 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3124 | } 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] | 3125 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3126 | ctx->ctx_res_w = RES_SNTX; |
| 3127 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3128 | } else { |
| 3129 | /* "{...} fi" is ok. "{...} if" is not |
| 3130 | * Example: |
| 3131 | * if { echo foo; } then { echo bar; } fi */ |
| 3132 | if (ctx->command->group) |
| 3133 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3134 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3135 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3136 | ctx->ctx_res_w = r->res; |
| 3137 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3138 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3139 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3140 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3141 | if (ctx->old_flag & FLAG_END) { |
| 3142 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3143 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3144 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3145 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3146 | old = ctx->stack; |
| 3147 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3148 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3149 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3150 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3151 | o_free_unsafe(&ctx->as_string); |
| 3152 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 3153 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3154 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3155 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3156 | *ctx = *old; /* physical copy */ |
| 3157 | free(old); |
| 3158 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3159 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3160 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3161 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3162 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3163 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3164 | * Normal return is 0. Syntax errors return 1. |
| 3165 | * Note: on return, word is reset, but not o_free'd! |
| 3166 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3167 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3168 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3169 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3170 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3171 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3172 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3173 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3174 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3175 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3176 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3177 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3178 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3179 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3180 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3181 | * "2.7 Redirection |
| 3182 | * ...the word that follows the redirection operator |
| 3183 | * shall be subjected to tilde expansion, parameter expansion, |
| 3184 | * command substitution, arithmetic expansion, and quote |
| 3185 | * removal. Pathname expansion shall not be performed |
| 3186 | * on the word by a non-interactive shell; an interactive |
| 3187 | * shell may perform it, but shall do so only when |
| 3188 | * the expansion would result in one word." |
| 3189 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3190 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3191 | /* Cater for >\file case: |
| 3192 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3193 | * Same with heredocs: |
| 3194 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3195 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3196 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3197 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3198 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3199 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3200 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3201 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3202 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3203 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3204 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3205 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3206 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3207 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3208 | if (ctx->ctx_dsemicolon |
| 3209 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3210 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3211 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3212 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3213 | ctx->ctx_dsemicolon = 0; |
| 3214 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3215 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3216 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3217 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3218 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3219 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3220 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3221 | # if ENABLE_HUSH_CASE |
| 3222 | && ctx->ctx_res_w != RES_CASE |
| 3223 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3224 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3225 | int reserved = reserved_word(word, ctx); |
| 3226 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3227 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3228 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3229 | debug_printf_parse("done_word return %d\n", |
| 3230 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3231 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3232 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3233 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3234 | if (strcmp(word->data, "[[") == 0) { |
| 3235 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3236 | } |
| 3237 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3238 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3239 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3240 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3241 | if (command->group) { |
| 3242 | /* "{ echo foo; } echo bar" - bad */ |
| 3243 | syntax_error_at(word->data); |
| 3244 | debug_printf_parse("done_word return 1: syntax error, " |
| 3245 | "groups and arglists don't mix\n"); |
| 3246 | return 1; |
| 3247 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3248 | |
| 3249 | /* If this word wasn't an assignment, next ones definitely |
| 3250 | * can't be assignments. Even if they look like ones. */ |
| 3251 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3252 | && word->o_assignment != WORD_IS_KEYWORD |
| 3253 | ) { |
| 3254 | word->o_assignment = NOT_ASSIGNMENT; |
| 3255 | } else { |
| 3256 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3257 | command->assignment_cnt++; |
| 3258 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3259 | } |
| 3260 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3261 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3262 | } |
| 3263 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3264 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3265 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3266 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3267 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3268 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3269 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3270 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3271 | char *p = word->data; |
| 3272 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3273 | && (p[1] & 0x7f) == '@' |
| 3274 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3275 | ) { |
| 3276 | p += 3; |
| 3277 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3278 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3279 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3280 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3281 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3282 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3283 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3284 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3285 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3286 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3287 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3288 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3289 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3290 | return 1; |
| 3291 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3292 | /* Force FOR to have just one word (variable name) */ |
| 3293 | /* NB: basically, this makes hush see "for v in ..." |
| 3294 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3295 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3296 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3297 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3298 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3299 | #if ENABLE_HUSH_CASE |
| 3300 | /* Force CASE to have just one word */ |
| 3301 | if (ctx->ctx_res_w == RES_CASE) { |
| 3302 | done_pipe(ctx, PIPE_SEQ); |
| 3303 | } |
| 3304 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3305 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3306 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3307 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3308 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3309 | return 0; |
| 3310 | } |
| 3311 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3312 | |
| 3313 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3314 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3315 | * Return: |
| 3316 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3317 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3318 | * REDIRFD_TO_FILE if no & was seen, |
| 3319 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3320 | */ |
| 3321 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3322 | #define parse_redir_right_fd(as_string, input) \ |
| 3323 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3324 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3325 | 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] | 3326 | { |
| 3327 | int ch, d, ok; |
| 3328 | |
| 3329 | ch = i_peek(input); |
| 3330 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3331 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3332 | |
| 3333 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3334 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3335 | ch = i_peek(input); |
| 3336 | if (ch == '-') { |
| 3337 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3338 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3339 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3340 | } |
| 3341 | d = 0; |
| 3342 | ok = 0; |
| 3343 | while (ch != EOF && isdigit(ch)) { |
| 3344 | d = d*10 + (ch-'0'); |
| 3345 | ok = 1; |
| 3346 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3347 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3348 | ch = i_peek(input); |
| 3349 | } |
| 3350 | if (ok) return d; |
| 3351 | |
| 3352 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3353 | |
| 3354 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3355 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3358 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3359 | */ |
| 3360 | static int parse_redirect(struct parse_context *ctx, |
| 3361 | int fd, |
| 3362 | redir_type style, |
| 3363 | struct in_str *input) |
| 3364 | { |
| 3365 | struct command *command = ctx->command; |
| 3366 | struct redir_struct *redir; |
| 3367 | struct redir_struct **redirp; |
| 3368 | int dup_num; |
| 3369 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3370 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3371 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3372 | /* Check for a '>&1' type redirect */ |
| 3373 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3374 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3375 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3376 | } else { |
| 3377 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3378 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3379 | if (dup_num) { /* <<-... */ |
| 3380 | ch = i_getch(input); |
| 3381 | nommu_addchr(&ctx->as_string, ch); |
| 3382 | ch = i_peek(input); |
| 3383 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3384 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3385 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3386 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3387 | int ch = i_peek(input); |
| 3388 | if (ch == '|') { |
| 3389 | /* >|FILE redirect ("clobbering" >). |
| 3390 | * Since we do not support "set -o noclobber" yet, |
| 3391 | * >| and > are the same for now. Just eat |. |
| 3392 | */ |
| 3393 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3394 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | /* Create a new redir_struct and append it to the linked list */ |
| 3399 | redirp = &command->redirects; |
| 3400 | while ((redir = *redirp) != NULL) { |
| 3401 | redirp = &(redir->next); |
| 3402 | } |
| 3403 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3404 | /* redir->next = NULL; */ |
| 3405 | /* redir->rd_filename = NULL; */ |
| 3406 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3407 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3408 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3409 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3410 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3411 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3412 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3413 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3414 | /* Erik had a check here that the file descriptor in question |
| 3415 | * is legit; I postpone that to "run time" |
| 3416 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3417 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3418 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3419 | } else { |
| 3420 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3421 | * end of the next parsed word. */ |
| 3422 | ctx->pending_redirect = redir; |
| 3423 | } |
| 3424 | return 0; |
| 3425 | } |
| 3426 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3427 | /* If a redirect is immediately preceded by a number, that number is |
| 3428 | * supposed to tell which file descriptor to redirect. This routine |
| 3429 | * looks for such preceding numbers. In an ideal world this routine |
| 3430 | * needs to handle all the following classes of redirects... |
| 3431 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3432 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3433 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3434 | * 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] | 3435 | * |
| 3436 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3437 | * "2.7 Redirection |
| 3438 | * ... If n is quoted, the number shall not be recognized as part of |
| 3439 | * the redirection expression. For example: |
| 3440 | * echo \2>a |
| 3441 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3442 | * 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] | 3443 | * |
| 3444 | * A -1 return means no valid number was found, |
| 3445 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3446 | */ |
| 3447 | static int redirect_opt_num(o_string *o) |
| 3448 | { |
| 3449 | int num; |
| 3450 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3451 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3452 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3453 | num = bb_strtou(o->data, NULL, 10); |
| 3454 | if (errno || num < 0) |
| 3455 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3456 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3457 | return num; |
| 3458 | } |
| 3459 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3460 | #if BB_MMU |
| 3461 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3462 | fetch_till_str(input, word, skip_tabs) |
| 3463 | #endif |
| 3464 | static char *fetch_till_str(o_string *as_string, |
| 3465 | struct in_str *input, |
| 3466 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3467 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3468 | { |
| 3469 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3470 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3471 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3472 | int ch; |
| 3473 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3474 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 3475 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3476 | while (1) { |
| 3477 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3478 | if (ch != EOF) |
| 3479 | nommu_addchr(as_string, ch); |
| 3480 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3481 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3482 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3483 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3484 | heredoc.data[past_EOL] = '\0'; |
| 3485 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3486 | return heredoc.data; |
| 3487 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3488 | while (ch == '\n') { |
| 3489 | o_addchr(&heredoc, ch); |
| 3490 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3491 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3492 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3493 | do { |
| 3494 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3495 | if (ch != EOF) |
| 3496 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3497 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3498 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3499 | } |
| 3500 | if (ch == EOF) { |
| 3501 | o_free_unsafe(&heredoc); |
| 3502 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3503 | } |
| 3504 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3505 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3506 | if (prev == '\\' && ch == '\\') |
| 3507 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3508 | prev = 0; /* not \ */ |
| 3509 | else |
| 3510 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3511 | } |
| 3512 | } |
| 3513 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3514 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3515 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3516 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3517 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3518 | { |
| 3519 | struct pipe *pi = ctx->list_head; |
| 3520 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3521 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3522 | int i; |
| 3523 | struct command *cmd = pi->cmds; |
| 3524 | |
| 3525 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3526 | pi->num_cmds, |
| 3527 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3528 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3529 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3530 | |
| 3531 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3532 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3533 | while (redir) { |
| 3534 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3535 | char *p; |
| 3536 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3537 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3538 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3539 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3540 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3541 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3542 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3543 | return 1; |
| 3544 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3545 | free(redir->rd_filename); |
| 3546 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3547 | heredoc_cnt--; |
| 3548 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3549 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3550 | } |
| 3551 | cmd++; |
| 3552 | } |
| 3553 | pi = pi->next; |
| 3554 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3555 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3556 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3557 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3558 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3559 | #endif |
| 3560 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3564 | static int run_list(struct pipe *pi); |
| 3565 | #if BB_MMU |
| 3566 | #define parse_stream(pstring, input, end_trigger) \ |
| 3567 | parse_stream(input, end_trigger) |
| 3568 | #endif |
| 3569 | static struct pipe *parse_stream(char **pstring, |
| 3570 | struct in_str *input, |
| 3571 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3572 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3573 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3574 | #if !ENABLE_HUSH_FUNCTIONS |
| 3575 | #define parse_group(dest, ctx, input, ch) \ |
| 3576 | parse_group(ctx, input, ch) |
| 3577 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3578 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3579 | struct in_str *input, int ch) |
| 3580 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3581 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3582 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3583 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3584 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3585 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3586 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3587 | |
| 3588 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3589 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3590 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3591 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3592 | if (done_word(dest, ctx)) |
| 3593 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3594 | if (!command->argv) |
| 3595 | goto skip; /* (... */ |
| 3596 | if (command->argv[1]) { /* word word ... (... */ |
| 3597 | syntax_error_unexpected_ch('('); |
| 3598 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3599 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3600 | /* it is "word(..." or "word (..." */ |
| 3601 | do |
| 3602 | ch = i_getch(input); |
| 3603 | while (ch == ' ' || ch == '\t'); |
| 3604 | if (ch != ')') { |
| 3605 | syntax_error_unexpected_ch(ch); |
| 3606 | return 1; |
| 3607 | } |
| 3608 | nommu_addchr(&ctx->as_string, ch); |
| 3609 | do |
| 3610 | ch = i_getch(input); |
| 3611 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3612 | if (ch != '{') { |
| 3613 | syntax_error_unexpected_ch(ch); |
| 3614 | return 1; |
| 3615 | } |
| 3616 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3617 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3618 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3619 | } |
| 3620 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3621 | |
| 3622 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3623 | if (command->argv /* word [word]{... */ |
| 3624 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3625 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3626 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3627 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3628 | debug_printf_parse("parse_group return 1: " |
| 3629 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3630 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3631 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3632 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3633 | |
| 3634 | #if ENABLE_HUSH_FUNCTIONS |
| 3635 | skip: |
| 3636 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3637 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3638 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3639 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3640 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3641 | } else { |
| 3642 | /* bash does not allow "{echo...", requires whitespace */ |
| 3643 | ch = i_getch(input); |
| 3644 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3645 | syntax_error_unexpected_ch(ch); |
| 3646 | return 1; |
| 3647 | } |
| 3648 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3649 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3650 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3651 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3652 | #if BB_MMU |
| 3653 | # define as_string NULL |
| 3654 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3655 | char *as_string = NULL; |
| 3656 | #endif |
| 3657 | pipe_list = parse_stream(&as_string, input, endch); |
| 3658 | #if !BB_MMU |
| 3659 | if (as_string) |
| 3660 | o_addstr(&ctx->as_string, as_string); |
| 3661 | #endif |
| 3662 | /* empty ()/{} or parse error? */ |
| 3663 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3664 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3665 | if (!BB_MMU) |
| 3666 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3667 | debug_printf_parse("parse_group return 1: " |
| 3668 | "parse_stream returned %p\n", pipe_list); |
| 3669 | return 1; |
| 3670 | } |
| 3671 | command->group = pipe_list; |
| 3672 | #if !BB_MMU |
| 3673 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3674 | command->group_as_string = as_string; |
| 3675 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3676 | command->group_as_string); |
| 3677 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3678 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3679 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3680 | debug_printf_parse("parse_group return 0\n"); |
| 3681 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3682 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3685 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3686 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3687 | static int 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] | 3688 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3689 | static int add_till_single_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3690 | { |
| 3691 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3692 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3693 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3694 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3695 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3696 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3697 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3698 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3699 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3703 | static int add_till_double_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3704 | { |
| 3705 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3706 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3707 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3708 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3709 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3710 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3711 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3712 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3713 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3714 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3715 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3716 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3717 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3718 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3719 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 3720 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3721 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3722 | continue; |
| 3723 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3724 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3728 | * \` quoting. |
| 3729 | * "Within the backquoted style of command substitution, backslash |
| 3730 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3731 | * The search for the matching backquote shall be satisfied by the first |
| 3732 | * backquote found without a preceding backslash; during this search, |
| 3733 | * if a non-escaped backquote is encountered within a shell comment, |
| 3734 | * a here-document, an embedded command substitution of the $(command) |
| 3735 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3736 | * or double-quoted string that begins, but does not end, within the |
| 3737 | * "`...`" sequence produces undefined results." |
| 3738 | * Example Output |
| 3739 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3740 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3741 | static int 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] | 3742 | { |
| 3743 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3744 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3745 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3746 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3747 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3748 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 3749 | ch = i_getch(input); |
| 3750 | if (ch != '`' |
| 3751 | && ch != '$' |
| 3752 | && ch != '\\' |
| 3753 | && (!in_dquote || ch != '"') |
| 3754 | ) { |
| 3755 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3756 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3757 | } |
| 3758 | if (ch == EOF) { |
| 3759 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3760 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3761 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3762 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } |
| 3765 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3766 | * quoting and nested ()s. |
| 3767 | * "With the $(command) style of command substitution, all characters |
| 3768 | * following the open parenthesis to the matching closing parenthesis |
| 3769 | * constitute the command. Any valid shell script can be used for command, |
| 3770 | * except a script consisting solely of redirections which produces |
| 3771 | * unspecified results." |
| 3772 | * Example Output |
| 3773 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3774 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3775 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3776 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3777 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3778 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3779 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 3780 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3781 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3782 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3783 | 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] | 3784 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3785 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3786 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3787 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3788 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3789 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3790 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 3791 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3792 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3793 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3794 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3795 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3796 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3797 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3798 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3799 | if (!dbl) |
| 3800 | break; |
| 3801 | /* we look for closing )) of $((EXPR)) */ |
| 3802 | if (i_peek(input) == end_ch) { |
| 3803 | i_getch(input); /* eat second ')' */ |
| 3804 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3805 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3806 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3807 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3808 | if (ch == '(' || ch == '{') { |
| 3809 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3810 | if (!add_till_closing_bracket(dest, input, ch)) |
| 3811 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3812 | o_addchr(dest, ch); |
| 3813 | continue; |
| 3814 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3815 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3816 | if (!add_till_single_quote(dest, input)) |
| 3817 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3818 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3819 | continue; |
| 3820 | } |
| 3821 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3822 | if (!add_till_double_quote(dest, input)) |
| 3823 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3824 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3825 | continue; |
| 3826 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3827 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3828 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 3829 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3830 | o_addchr(dest, ch); |
| 3831 | continue; |
| 3832 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3833 | if (ch == '\\') { |
| 3834 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3835 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3836 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3837 | syntax_error_unterm_ch(')'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3838 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3839 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3840 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3841 | continue; |
| 3842 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3843 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3844 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3845 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3846 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3847 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3848 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3849 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3850 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 3851 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3852 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3853 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3854 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3855 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3856 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3857 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3858 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3859 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3860 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3861 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3862 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3863 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3864 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3865 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3866 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3867 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3868 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3869 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3870 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3871 | if (!isalnum(ch) && ch != '_') |
| 3872 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3873 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3874 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3875 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3876 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3877 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3878 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3879 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3880 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3881 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3882 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3883 | o_addchr(dest, ch | quote_mask); |
| 3884 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3885 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3886 | case '$': /* pid */ |
| 3887 | case '!': /* last bg pid */ |
| 3888 | case '?': /* last exit code */ |
| 3889 | case '#': /* number of args */ |
| 3890 | case '*': /* args */ |
| 3891 | case '@': /* args */ |
| 3892 | goto make_one_char_var; |
| 3893 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 3894 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3895 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3896 | ch = i_getch(input); /* eat '{' */ |
| 3897 | nommu_addchr(as_string, ch); |
| 3898 | |
| 3899 | ch = i_getch(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3900 | /* It should be ${?}, or ${#var}, |
| 3901 | * or even ${?+subst} - operator acting on a special variable, |
| 3902 | * or the beginning of variable name. |
| 3903 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3904 | if (ch == EOF |
| 3905 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 3906 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3907 | bad_dollar_syntax: |
| 3908 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3909 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 3910 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3911 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3912 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3913 | ch |= quote_mask; |
| 3914 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3915 | /* 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] | 3916 | * However, this regresses some of our testsuite cases |
| 3917 | * which check invalid constructs like ${%}. |
| 3918 | * Oh well... let's check that the var name part is fine... */ |
| 3919 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3920 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3921 | unsigned pos; |
| 3922 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3923 | o_addchr(dest, ch); |
| 3924 | debug_printf_parse(": '%c'\n", ch); |
| 3925 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3926 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3927 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3928 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3929 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3930 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3931 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3932 | unsigned end_ch; |
| 3933 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3934 | /* handle parameter expansions |
| 3935 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3936 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3937 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3938 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3939 | |
| 3940 | /* Eat everything until closing '}' (or ':') */ |
| 3941 | end_ch = '}'; |
| 3942 | if (ENABLE_HUSH_BASH_COMPAT |
| 3943 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3944 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3945 | ) { |
| 3946 | /* It's ${var:N[:M]} thing */ |
| 3947 | end_ch = '}' * 0x100 + ':'; |
| 3948 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3949 | if (ENABLE_HUSH_BASH_COMPAT |
| 3950 | && ch == '/' |
| 3951 | ) { |
| 3952 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 3953 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 3954 | i_getch(input); |
| 3955 | nommu_addchr(as_string, '/'); |
| 3956 | ch = '\\'; |
| 3957 | } |
| 3958 | end_ch = '}' * 0x100 + '/'; |
| 3959 | } |
| 3960 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3961 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3962 | if (!BB_MMU) |
| 3963 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3964 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3965 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3966 | if (last_ch == 0) /* error? */ |
| 3967 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3968 | #else |
| 3969 | #error Simple code to only allow ${var} is not implemented |
| 3970 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3971 | if (as_string) { |
| 3972 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3973 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3974 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3975 | |
| 3976 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 3977 | /* close the first block: */ |
| 3978 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3979 | /* while parsing N from ${var:N[:M]} |
| 3980 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3981 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3982 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3983 | end_ch = '}'; |
| 3984 | goto again; |
| 3985 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3986 | /* got '}' */ |
| 3987 | if (end_ch == '}' * 0x100 + ':') { |
| 3988 | /* it's ${var:N} - emulate :999999999 */ |
| 3989 | o_addstr(dest, "999999999"); |
| 3990 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3991 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3992 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3993 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3994 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3995 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3996 | break; |
| 3997 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3998 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3999 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4000 | unsigned pos; |
| 4001 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4002 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4003 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4004 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4005 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4006 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4007 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4008 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4009 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4010 | if (!BB_MMU) |
| 4011 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4012 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4013 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4014 | if (as_string) { |
| 4015 | o_addstr(as_string, dest->data + pos); |
| 4016 | o_addchr(as_string, ')'); |
| 4017 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4018 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4019 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4020 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4021 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4022 | # endif |
| 4023 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4024 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4025 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4026 | if (!BB_MMU) |
| 4027 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4028 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4029 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4030 | if (as_string) { |
| 4031 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4032 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4033 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4034 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4035 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4036 | break; |
| 4037 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4038 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4039 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4040 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4041 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4042 | ch = i_peek(input); |
| 4043 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4044 | ch = '_'; |
| 4045 | goto make_var; |
| 4046 | } |
| 4047 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4048 | /* TODO: $_ and $-: */ |
| 4049 | /* $_ Shell or shell script name; or last argument of last command |
| 4050 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4051 | * 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] | 4052 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4053 | default: |
| 4054 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4055 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4056 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4057 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4058 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4059 | } |
| 4060 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4061 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4062 | # if ENABLE_HUSH_BASH_COMPAT |
| 4063 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4064 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4065 | # else |
| 4066 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4067 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4068 | encode_string(dest, input, dquote_end) |
| 4069 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4070 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4071 | |
| 4072 | #else /* !MMU */ |
| 4073 | |
| 4074 | # if ENABLE_HUSH_BASH_COMPAT |
| 4075 | /* all parameters are needed, no macro tricks */ |
| 4076 | # else |
| 4077 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4078 | encode_string(as_string, dest, input, dquote_end) |
| 4079 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4080 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4081 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4082 | o_string *dest, |
| 4083 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4084 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4085 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4086 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4087 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4088 | const int process_bkslash = 1; |
| 4089 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4090 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4091 | int next; |
| 4092 | |
| 4093 | again: |
| 4094 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4095 | if (ch != EOF) |
| 4096 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4097 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4098 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4099 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4100 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4101 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4102 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4103 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4104 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4105 | } |
| 4106 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4107 | if (ch != '\n') { |
| 4108 | next = i_peek(input); |
| 4109 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4110 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4111 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4112 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4113 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4114 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4115 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4116 | } |
| 4117 | /* bash: |
| 4118 | * "The backslash retains its special meaning [in "..."] |
| 4119 | * only when followed by one of the following characters: |
| 4120 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4121 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4122 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4123 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4124 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4125 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4126 | ch = i_getch(input); /* eat next */ |
| 4127 | if (ch == '\n') |
| 4128 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4129 | } /* else: ch remains == '\\', and we double it below: */ |
| 4130 | 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] | 4131 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4132 | goto again; |
| 4133 | } |
| 4134 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4135 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4136 | debug_printf_parse("encode_string return 0: " |
| 4137 | "parse_dollar returned 0 (error)\n"); |
| 4138 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4139 | } |
| 4140 | goto again; |
| 4141 | } |
| 4142 | #if ENABLE_HUSH_TICK |
| 4143 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4144 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4145 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4146 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4147 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4148 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4149 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4150 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4151 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4152 | } |
| 4153 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4154 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4155 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4156 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4159 | /* |
| 4160 | * Scan input until EOF or end_trigger char. |
| 4161 | * Return a list of pipes to execute, or NULL on EOF |
| 4162 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4163 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4164 | * reset parsing machinery and start parsing anew, |
| 4165 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4166 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4167 | static struct pipe *parse_stream(char **pstring, |
| 4168 | struct in_str *input, |
| 4169 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4170 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4171 | struct parse_context ctx; |
| 4172 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4173 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4174 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4175 | /* 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] | 4176 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4177 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4178 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4179 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4180 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4181 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4182 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4183 | * Preventing this: */ |
| 4184 | o_addchr(&dest, '\0'); |
| 4185 | dest.length = 0; |
| 4186 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4187 | /* We used to separate words on $IFS here. This was wrong. |
| 4188 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4189 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4190 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4191 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4192 | if (MAYBE_ASSIGNMENT != 0) |
| 4193 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4194 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4195 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4196 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4197 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4198 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4199 | int ch; |
| 4200 | int next; |
| 4201 | int redir_fd; |
| 4202 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4203 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4204 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4205 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4206 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4207 | if (ch == EOF) { |
| 4208 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4209 | |
| 4210 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4211 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4212 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4213 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4214 | /* end_trigger == '}' case errors out earlier, |
| 4215 | * checking only ')' */ |
| 4216 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4217 | syntax_error_unterm_ch('('); |
| 4218 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4219 | } |
| 4220 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4221 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4222 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4223 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4224 | o_free(&dest); |
| 4225 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4226 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4227 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4228 | /* (this makes bare "&" cmd a no-op. |
| 4229 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4230 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4231 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4232 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4233 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4234 | pi = NULL; |
| 4235 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4236 | #if !BB_MMU |
| 4237 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4238 | if (pstring) |
| 4239 | *pstring = ctx.as_string.data; |
| 4240 | else |
| 4241 | o_free_unsafe(&ctx.as_string); |
| 4242 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4243 | debug_leave(); |
| 4244 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4245 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4246 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4247 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4248 | |
| 4249 | next = '\0'; |
| 4250 | if (ch != '\n') |
| 4251 | next = i_peek(input); |
| 4252 | |
| 4253 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4254 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4255 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4256 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4257 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4258 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4259 | || (next != ';' /* }; - special */ |
| 4260 | && next != ')' /* }) - special */ |
| 4261 | && next != '&' /* }& and }&& ... - special */ |
| 4262 | && next != '|' /* }|| ... - special */ |
| 4263 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4264 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4265 | ) { |
| 4266 | /* They are not special, skip "{}" */ |
| 4267 | is_special += 2; |
| 4268 | } |
| 4269 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4270 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4271 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4272 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4273 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4274 | o_addQchr(&dest, ch); |
| 4275 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4276 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4277 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4278 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4279 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4280 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4281 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4282 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4283 | continue; |
| 4284 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4285 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4286 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4287 | if (done_word(&dest, &ctx)) { |
| 4288 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4289 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4290 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4291 | /* Is this a case when newline is simply ignored? |
| 4292 | * Some examples: |
| 4293 | * "cmd | <newline> cmd ..." |
| 4294 | * "case ... in <newline> word) ..." |
| 4295 | */ |
| 4296 | if (IS_NULL_CMD(ctx.command) |
| 4297 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4298 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4299 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4300 | * Without check #1, interactive shell |
| 4301 | * ignores even bare <newline>, |
| 4302 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4303 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4304 | * ps2> _ <=== wrong, should be ps1 |
| 4305 | * Without check #2, "cmd & <newline>" |
| 4306 | * is similarly mistreated. |
| 4307 | * (BTW, this makes "cmd & cmd" |
| 4308 | * and "cmd && cmd" non-orthogonal. |
| 4309 | * Really, ask yourself, why |
| 4310 | * "cmd && <newline>" doesn't start |
| 4311 | * cmd but waits for more input? |
| 4312 | * No reason...) |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4313 | */ |
| 4314 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4315 | if (pi->num_cmds != 0 /* check #1 */ |
| 4316 | && pi->followup != PIPE_BG /* check #2 */ |
| 4317 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4318 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4319 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4320 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4321 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4322 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4323 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4324 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4325 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4326 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4327 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4328 | heredoc_cnt = 0; |
| 4329 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4330 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4331 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4332 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4333 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4334 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4335 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4336 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4337 | |
| 4338 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4339 | * } is an ordinary char in this case, even inside { cmd; } |
| 4340 | * Pathological example: { ""}; } should exec "}" cmd |
| 4341 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4342 | if (ch == '}') { |
| 4343 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 4344 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4345 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4346 | ) { |
| 4347 | goto ordinary_char; |
| 4348 | } |
| 4349 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 4350 | goto skip_end_trigger; |
| 4351 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4354 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4355 | && (ch != ';' || heredoc_cnt == 0) |
| 4356 | #if ENABLE_HUSH_CASE |
| 4357 | && (ch != ')' |
| 4358 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4359 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4360 | ) |
| 4361 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4362 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4363 | if (heredoc_cnt) { |
| 4364 | /* This is technically valid: |
| 4365 | * { cat <<HERE; }; echo Ok |
| 4366 | * heredoc |
| 4367 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4368 | * HERE |
| 4369 | * but we don't support this. |
| 4370 | * We require heredoc to be in enclosing {}/(), |
| 4371 | * if any. |
| 4372 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4373 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4374 | goto parse_error; |
| 4375 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4376 | if (done_word(&dest, &ctx)) { |
| 4377 | goto parse_error; |
| 4378 | } |
| 4379 | done_pipe(&ctx, PIPE_SEQ); |
| 4380 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4381 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4382 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4383 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4384 | 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] | 4385 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4386 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4387 | #if !BB_MMU |
| 4388 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4389 | if (pstring) |
| 4390 | *pstring = ctx.as_string.data; |
| 4391 | else |
| 4392 | o_free_unsafe(&ctx.as_string); |
| 4393 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4394 | debug_leave(); |
| 4395 | debug_printf_parse("parse_stream return %p: " |
| 4396 | "end_trigger char found\n", |
| 4397 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4398 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4399 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4400 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4401 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4402 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4403 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4404 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4405 | /* Catch <, > before deciding whether this word is |
| 4406 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4407 | switch (ch) { |
| 4408 | case '>': |
| 4409 | redir_fd = redirect_opt_num(&dest); |
| 4410 | if (done_word(&dest, &ctx)) { |
| 4411 | goto parse_error; |
| 4412 | } |
| 4413 | redir_style = REDIRECT_OVERWRITE; |
| 4414 | if (next == '>') { |
| 4415 | redir_style = REDIRECT_APPEND; |
| 4416 | ch = i_getch(input); |
| 4417 | nommu_addchr(&ctx.as_string, ch); |
| 4418 | } |
| 4419 | #if 0 |
| 4420 | else if (next == '(') { |
| 4421 | syntax_error(">(process) not supported"); |
| 4422 | goto parse_error; |
| 4423 | } |
| 4424 | #endif |
| 4425 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4426 | goto parse_error; |
| 4427 | continue; /* back to top of while (1) */ |
| 4428 | case '<': |
| 4429 | redir_fd = redirect_opt_num(&dest); |
| 4430 | if (done_word(&dest, &ctx)) { |
| 4431 | goto parse_error; |
| 4432 | } |
| 4433 | redir_style = REDIRECT_INPUT; |
| 4434 | if (next == '<') { |
| 4435 | redir_style = REDIRECT_HEREDOC; |
| 4436 | heredoc_cnt++; |
| 4437 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4438 | ch = i_getch(input); |
| 4439 | nommu_addchr(&ctx.as_string, ch); |
| 4440 | } else if (next == '>') { |
| 4441 | redir_style = REDIRECT_IO; |
| 4442 | ch = i_getch(input); |
| 4443 | nommu_addchr(&ctx.as_string, ch); |
| 4444 | } |
| 4445 | #if 0 |
| 4446 | else if (next == '(') { |
| 4447 | syntax_error("<(process) not supported"); |
| 4448 | goto parse_error; |
| 4449 | } |
| 4450 | #endif |
| 4451 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4452 | goto parse_error; |
| 4453 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4454 | case '#': |
| 4455 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4456 | /* skip "#comment" */ |
| 4457 | while (1) { |
| 4458 | ch = i_peek(input); |
| 4459 | if (ch == EOF || ch == '\n') |
| 4460 | break; |
| 4461 | i_getch(input); |
| 4462 | /* note: we do not add it to &ctx.as_string */ |
| 4463 | } |
| 4464 | nommu_addchr(&ctx.as_string, '\n'); |
| 4465 | continue; /* back to top of while (1) */ |
| 4466 | } |
| 4467 | break; |
| 4468 | case '\\': |
| 4469 | if (next == '\n') { |
| 4470 | /* It's "\<newline>" */ |
| 4471 | #if !BB_MMU |
| 4472 | /* Remove trailing '\' from ctx.as_string */ |
| 4473 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4474 | #endif |
| 4475 | ch = i_getch(input); /* eat it */ |
| 4476 | continue; /* back to top of while (1) */ |
| 4477 | } |
| 4478 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4482 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4483 | && !ctx.pending_redirect |
| 4484 | ) { |
| 4485 | /* ch is a special char and thus this word |
| 4486 | * cannot be an assignment */ |
| 4487 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4488 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4491 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4492 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4493 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4494 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4495 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4496 | break; |
| 4497 | case '\\': |
| 4498 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4499 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4500 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4501 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4502 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4503 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4504 | o_addchr(&dest, '\\'); |
| 4505 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4506 | o_addchr(&dest, ch); |
| 4507 | nommu_addchr(&ctx.as_string, ch); |
| 4508 | /* Example: echo Hello \2>file |
| 4509 | * we need to know that word 2 is quoted */ |
| 4510 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4511 | break; |
| 4512 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4513 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4514 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4515 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4516 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4517 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4518 | break; |
| 4519 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4520 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4521 | if (next == '\'' && !ctx.pending_redirect) { |
| 4522 | insert_empty_quoted_str_marker: |
| 4523 | nommu_addchr(&ctx.as_string, next); |
| 4524 | i_getch(input); /* eat second ' */ |
| 4525 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4526 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4527 | } else { |
| 4528 | while (1) { |
| 4529 | ch = i_getch(input); |
| 4530 | if (ch == EOF) { |
| 4531 | syntax_error_unterm_ch('\''); |
| 4532 | goto parse_error; |
| 4533 | } |
| 4534 | nommu_addchr(&ctx.as_string, ch); |
| 4535 | if (ch == '\'') |
| 4536 | break; |
| 4537 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4538 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4539 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4540 | break; |
| 4541 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4542 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4543 | if (next == '"' && !ctx.pending_redirect) |
| 4544 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4545 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4546 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4547 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4548 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4549 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4550 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4551 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4552 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4553 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4554 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4555 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4556 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4557 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4558 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 4559 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4560 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4561 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4562 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4563 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4564 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4565 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4566 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4567 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4568 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4569 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4570 | #if ENABLE_HUSH_CASE |
| 4571 | case_semi: |
| 4572 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4573 | if (done_word(&dest, &ctx)) { |
| 4574 | goto parse_error; |
| 4575 | } |
| 4576 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4577 | #if ENABLE_HUSH_CASE |
| 4578 | /* Eat multiple semicolons, detect |
| 4579 | * whether it means something special */ |
| 4580 | while (1) { |
| 4581 | ch = i_peek(input); |
| 4582 | if (ch != ';') |
| 4583 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4584 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4585 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4586 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4587 | ctx.ctx_dsemicolon = 1; |
| 4588 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4589 | break; |
| 4590 | } |
| 4591 | } |
| 4592 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4593 | new_cmd: |
| 4594 | /* We just finished a cmd. New one may start |
| 4595 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4596 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4597 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4598 | break; |
| 4599 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4600 | if (done_word(&dest, &ctx)) { |
| 4601 | goto parse_error; |
| 4602 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4603 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4604 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4605 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4606 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4607 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4608 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4609 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4610 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4611 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4612 | if (done_word(&dest, &ctx)) { |
| 4613 | goto parse_error; |
| 4614 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4615 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4616 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4617 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4618 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4619 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4620 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4621 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4622 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4623 | } else { |
| 4624 | /* we could pick up a file descriptor choice here |
| 4625 | * with redirect_opt_num(), but bash doesn't do it. |
| 4626 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4627 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4628 | #if !BB_MMU |
| 4629 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 4630 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4631 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4632 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4633 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4634 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4635 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4636 | if (ctx.ctx_res_w == RES_MATCH |
| 4637 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4638 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4639 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4640 | ) { |
| 4641 | continue; |
| 4642 | } |
| 4643 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4644 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4645 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4646 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4647 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4648 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4649 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4650 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4651 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4652 | goto case_semi; |
| 4653 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4654 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4655 | /* proper use of this character is caught by end_trigger: |
| 4656 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4657 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4658 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4659 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4660 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4661 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4662 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4663 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4664 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4665 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4666 | parse_error: |
| 4667 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4668 | struct parse_context *pctx; |
| 4669 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4670 | |
| 4671 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4672 | * Sample for finding leaks on syntax error recovery path. |
| 4673 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4674 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4675 | * Samples to catch leaks at execution: |
| 4676 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4677 | * 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] | 4678 | */ |
| 4679 | pctx = &ctx; |
| 4680 | do { |
| 4681 | /* Update pipe/command counts, |
| 4682 | * otherwise freeing may miss some */ |
| 4683 | done_pipe(pctx, PIPE_SEQ); |
| 4684 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4685 | pctx->list_head, pctx); |
| 4686 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4687 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4688 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4689 | #if !BB_MMU |
| 4690 | o_free_unsafe(&pctx->as_string); |
| 4691 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4692 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4693 | if (pctx != &ctx) { |
| 4694 | free(pctx); |
| 4695 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4696 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4697 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4698 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 4699 | o_free(&dest); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4700 | G.last_exitcode = 1; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4701 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4702 | if (pstring) |
| 4703 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4704 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4705 | debug_leave(); |
| 4706 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4707 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4710 | |
| 4711 | /*** Execution routines ***/ |
| 4712 | |
| 4713 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4714 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4715 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4716 | #define expand_string_to_string(str, do_unbackslash) \ |
| 4717 | expand_string_to_string(str) |
| 4718 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4719 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4720 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4721 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4722 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4723 | |
| 4724 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 4725 | * all variable references within and returns a pointer to |
| 4726 | * a list of expanded strings, possibly with larger number |
| 4727 | * of strings. (Think VAR="a b"; echo $VAR). |
| 4728 | * This new list is allocated as a single malloc block. |
| 4729 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4730 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4731 | * Caller can deallocate entire list by single free(list). */ |
| 4732 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4733 | /* A horde of its helpers come first: */ |
| 4734 | |
| 4735 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 4736 | { |
| 4737 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4738 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4739 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4740 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 4741 | if (c == '{' || c == '}') { |
| 4742 | /* { -> \{, } -> \} */ |
| 4743 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4744 | /* And now we want to add { or } and continue: |
| 4745 | * o_addchr(o, c); |
| 4746 | * continue; |
| 4747 | * luckily, just falling throught achieves this. |
| 4748 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4749 | } |
| 4750 | #endif |
| 4751 | o_addchr(o, c); |
| 4752 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4753 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 4754 | o_addchr(o, '\\'); |
| 4755 | if (len) { |
| 4756 | len--; |
| 4757 | o_addchr(o, '\\'); |
| 4758 | o_addchr(o, *str++); |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | } |
| 4763 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4764 | /* Store given string, finalizing the word and starting new one whenever |
| 4765 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4766 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 4767 | * Return in *ended_with_ifs: |
| 4768 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 4769 | */ |
| 4770 | static int expand_on_ifs(int *ended_with_ifs, o_string *output, int n, const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4771 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4772 | int last_is_ifs = 0; |
| 4773 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4774 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4775 | int word_len; |
| 4776 | |
| 4777 | if (!*str) /* EOL - do not finalize word */ |
| 4778 | break; |
| 4779 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4780 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4781 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4782 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4783 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4784 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4785 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 4786 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 4787 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4788 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4789 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4790 | /*/ Why can't we do it easier? */ |
| 4791 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 4792 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 4793 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4794 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4795 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4796 | if (!*str) /* EOL - do not finalize word */ |
| 4797 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4798 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4799 | |
| 4800 | /* We know str here points to at least one IFS char */ |
| 4801 | last_is_ifs = 1; |
| 4802 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 4803 | if (!*str) /* EOL - do not finalize word */ |
| 4804 | break; |
| 4805 | |
| 4806 | /* Start new word... but not always! */ |
| 4807 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4808 | if (output->has_quoted_part |
| 4809 | /* Case "v=' a'; echo $v": |
| 4810 | * here nothing precedes the space in $v expansion, |
| 4811 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4812 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4813 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4814 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4815 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4816 | o_addchr(output, '\0'); |
| 4817 | debug_print_list("expand_on_ifs", output, n); |
| 4818 | n = o_save_ptr(output, n); |
| 4819 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4820 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4821 | |
| 4822 | if (ended_with_ifs) |
| 4823 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4824 | debug_print_list("expand_on_ifs[1]", output, n); |
| 4825 | return n; |
| 4826 | } |
| 4827 | |
| 4828 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 4829 | * they are in double quotes, with the exception that they are not :). |
| 4830 | * Just the rules are similar: "expand only $var and `cmd`" |
| 4831 | * |
| 4832 | * Returns malloced string. |
| 4833 | * As an optimization, we return NULL if expansion is not needed. |
| 4834 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4835 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4836 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4837 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 4838 | encode_then_expand_string(str) |
| 4839 | #endif |
| 4840 | 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] | 4841 | { |
| 4842 | char *exp_str; |
| 4843 | struct in_str input; |
| 4844 | o_string dest = NULL_O_STRING; |
| 4845 | |
| 4846 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4847 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4848 | #if ENABLE_HUSH_TICK |
| 4849 | && !strchr(str, '`') |
| 4850 | #endif |
| 4851 | ) { |
| 4852 | return NULL; |
| 4853 | } |
| 4854 | |
| 4855 | /* We need to expand. Example: |
| 4856 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 4857 | */ |
| 4858 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4859 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4860 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4861 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4862 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4863 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 4864 | o_free_unsafe(&dest); |
| 4865 | return exp_str; |
| 4866 | } |
| 4867 | |
| 4868 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4869 | 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] | 4870 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4871 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4872 | arith_t res; |
| 4873 | char *exp_str; |
| 4874 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4875 | math_state.lookupvar = get_local_var_value; |
| 4876 | math_state.setvar = set_local_var_from_halves; |
| 4877 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4878 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4879 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4880 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4881 | if (errmsg_p) |
| 4882 | *errmsg_p = math_state.errmsg; |
| 4883 | if (math_state.errmsg) |
| 4884 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4885 | return res; |
| 4886 | } |
| 4887 | #endif |
| 4888 | |
| 4889 | #if ENABLE_HUSH_BASH_COMPAT |
| 4890 | /* ${var/[/]pattern[/repl]} helpers */ |
| 4891 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 4892 | { |
| 4893 | while (1) { |
| 4894 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 4895 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 4896 | if (end) { |
| 4897 | *size = end - val; |
| 4898 | return val; |
| 4899 | } |
| 4900 | if (*val == '\0') |
| 4901 | return NULL; |
| 4902 | /* Optimization: if "*pat" did not match the start of "string", |
| 4903 | * we know that "tring", "ring" etc will not match too: |
| 4904 | */ |
| 4905 | if (pattern[0] == '*') |
| 4906 | return NULL; |
| 4907 | val++; |
| 4908 | } |
| 4909 | } |
| 4910 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 4911 | { |
| 4912 | char *result = NULL; |
| 4913 | unsigned res_len = 0; |
| 4914 | unsigned repl_len = strlen(repl); |
| 4915 | |
| 4916 | while (1) { |
| 4917 | int size; |
| 4918 | char *s = strstr_pattern(val, pattern, &size); |
| 4919 | if (!s) |
| 4920 | break; |
| 4921 | |
| 4922 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 4923 | memcpy(result + res_len, val, s - val); |
| 4924 | res_len += s - val; |
| 4925 | strcpy(result + res_len, repl); |
| 4926 | res_len += repl_len; |
| 4927 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 4928 | |
| 4929 | val = s + size; |
| 4930 | if (exp_op == '/') |
| 4931 | break; |
| 4932 | } |
| 4933 | if (val[0] && result) { |
| 4934 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 4935 | strcpy(result + res_len, val); |
| 4936 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 4937 | } |
| 4938 | debug_printf_varexp("result:'%s'\n", result); |
| 4939 | return result; |
| 4940 | } |
| 4941 | #endif |
| 4942 | |
| 4943 | /* Helper: |
| 4944 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 4945 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4946 | 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] | 4947 | { |
| 4948 | const char *val = NULL; |
| 4949 | char *to_be_freed = NULL; |
| 4950 | char *p = *pp; |
| 4951 | char *var; |
| 4952 | char first_char; |
| 4953 | char exp_op; |
| 4954 | char exp_save = exp_save; /* for compiler */ |
| 4955 | char *exp_saveptr; /* points to expansion operator */ |
| 4956 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4957 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4958 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4959 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4960 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4961 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4962 | arg0 = arg[0]; |
| 4963 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4964 | exp_op = 0; |
| 4965 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4966 | if (first_char == '#' /* ${#... */ |
| 4967 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 4968 | ) { |
| 4969 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4970 | var++; |
| 4971 | exp_op = 'L'; |
| 4972 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4973 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4974 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 4975 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 4976 | ) { |
| 4977 | /* ${?:0}, ${#[:]%0} etc */ |
| 4978 | exp_saveptr = var + 1; |
| 4979 | } else { |
| 4980 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 4981 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 4982 | } |
| 4983 | exp_op = exp_save = *exp_saveptr; |
| 4984 | if (exp_op) { |
| 4985 | exp_word = exp_saveptr + 1; |
| 4986 | if (exp_op == ':') { |
| 4987 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4988 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4989 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4990 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4991 | ) { |
| 4992 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 4993 | exp_op = ':'; |
| 4994 | exp_word--; |
| 4995 | } |
| 4996 | } |
| 4997 | *exp_saveptr = '\0'; |
| 4998 | } /* else: it's not an expansion op, but bare ${var} */ |
| 4999 | } |
| 5000 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5001 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5002 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5003 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5004 | int n = xatoi_positive(var); |
| 5005 | if (n < G.global_argc) |
| 5006 | val = G.global_argv[n]; |
| 5007 | /* else val remains NULL: $N with too big N */ |
| 5008 | } else { |
| 5009 | switch (var[0]) { |
| 5010 | case '$': /* pid */ |
| 5011 | val = utoa(G.root_pid); |
| 5012 | break; |
| 5013 | case '!': /* bg pid */ |
| 5014 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5015 | break; |
| 5016 | case '?': /* exitcode */ |
| 5017 | val = utoa(G.last_exitcode); |
| 5018 | break; |
| 5019 | case '#': /* argc */ |
| 5020 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5021 | break; |
| 5022 | default: |
| 5023 | val = get_local_var_value(var); |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | /* Handle any expansions */ |
| 5028 | if (exp_op == 'L') { |
| 5029 | debug_printf_expand("expand: length(%s)=", val); |
| 5030 | val = utoa(val ? strlen(val) : 0); |
| 5031 | debug_printf_expand("%s\n", val); |
| 5032 | } else if (exp_op) { |
| 5033 | if (exp_op == '%' || exp_op == '#') { |
| 5034 | /* Standard-mandated substring removal ops: |
| 5035 | * ${parameter%word} - remove smallest suffix pattern |
| 5036 | * ${parameter%%word} - remove largest suffix pattern |
| 5037 | * ${parameter#word} - remove smallest prefix pattern |
| 5038 | * ${parameter##word} - remove largest prefix pattern |
| 5039 | * |
| 5040 | * Word is expanded to produce a glob pattern. |
| 5041 | * Then var's value is matched to it and matching part removed. |
| 5042 | */ |
| 5043 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5044 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5045 | char *exp_exp_word; |
| 5046 | char *loc; |
| 5047 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5048 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5049 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5050 | 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] | 5051 | if (exp_exp_word) |
| 5052 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5053 | /* HACK ALERT. We depend here on the fact that |
| 5054 | * G.global_argv and results of utoa and get_local_var_value |
| 5055 | * are actually in writable memory: |
| 5056 | * scan_and_match momentarily stores NULs there. */ |
| 5057 | t = (char*)val; |
| 5058 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5059 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5060 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5061 | free(exp_exp_word); |
| 5062 | if (loc) { /* match was found */ |
| 5063 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5064 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5065 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5066 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5067 | } |
| 5068 | } |
| 5069 | } |
| 5070 | #if ENABLE_HUSH_BASH_COMPAT |
| 5071 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5072 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5073 | * Note that in encoded form it has TWO parts: |
| 5074 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5075 | * and if // is used, it is encoded as \: |
| 5076 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5077 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5078 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5079 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5080 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5081 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5082 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5083 | * by the usual expansion rules: |
| 5084 | * >az; >bz; |
| 5085 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5086 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5087 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5088 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5089 | * (note that a*z _pattern_ is never globbed!) |
| 5090 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5091 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5092 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5093 | if (!pattern) |
| 5094 | pattern = xstrdup(exp_word); |
| 5095 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5096 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5097 | exp_word = p; |
| 5098 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5099 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5100 | 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] | 5101 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5102 | /* HACK ALERT. We depend here on the fact that |
| 5103 | * G.global_argv and results of utoa and get_local_var_value |
| 5104 | * are actually in writable memory: |
| 5105 | * replace_pattern momentarily stores NULs there. */ |
| 5106 | t = (char*)val; |
| 5107 | to_be_freed = replace_pattern(t, |
| 5108 | pattern, |
| 5109 | (repl ? repl : exp_word), |
| 5110 | exp_op); |
| 5111 | if (to_be_freed) /* at least one replace happened */ |
| 5112 | val = to_be_freed; |
| 5113 | free(pattern); |
| 5114 | free(repl); |
| 5115 | } |
| 5116 | } |
| 5117 | #endif |
| 5118 | else if (exp_op == ':') { |
| 5119 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 5120 | /* It's ${var:N[:M]} bashism. |
| 5121 | * Note that in encoded form it has TWO parts: |
| 5122 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5123 | */ |
| 5124 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5125 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5126 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5127 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5128 | if (errmsg) |
| 5129 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5130 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5131 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5132 | exp_word = p; |
| 5133 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5134 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5135 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5136 | if (errmsg) |
| 5137 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5138 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5139 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5140 | if (beg < 0) /* bash compat */ |
| 5141 | beg = 0; |
| 5142 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5143 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5144 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5145 | val = NULL; |
| 5146 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5147 | /* Paranoia. What if user entered 9999999999999 |
| 5148 | * which fits in arith_t but not int? */ |
| 5149 | if (len >= INT_MAX) |
| 5150 | len = INT_MAX; |
| 5151 | val = to_be_freed = xstrndup(val + beg, len); |
| 5152 | } |
| 5153 | debug_printf_varexp("val:'%s'\n", val); |
| 5154 | } else |
| 5155 | #endif |
| 5156 | { |
| 5157 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5158 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5159 | } |
| 5160 | } else { /* one of "-=+?" */ |
| 5161 | /* Standard-mandated substitution ops: |
| 5162 | * ${var?word} - indicate error if unset |
| 5163 | * If var is unset, word (or a message indicating it is unset |
| 5164 | * if word is null) is written to standard error |
| 5165 | * and the shell exits with a non-zero exit status. |
| 5166 | * Otherwise, the value of var is substituted. |
| 5167 | * ${var-word} - use default value |
| 5168 | * If var is unset, word is substituted. |
| 5169 | * ${var=word} - assign and use default value |
| 5170 | * If var is unset, word is assigned to var. |
| 5171 | * In all cases, final value of var is substituted. |
| 5172 | * ${var+word} - use alternative value |
| 5173 | * If var is unset, null is substituted. |
| 5174 | * Otherwise, word is substituted. |
| 5175 | * |
| 5176 | * Word is subjected to tilde expansion, parameter expansion, |
| 5177 | * command substitution, and arithmetic expansion. |
| 5178 | * If word is not needed, it is not expanded. |
| 5179 | * |
| 5180 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5181 | * but also treat null var as if it is unset. |
| 5182 | */ |
| 5183 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5184 | if (exp_op == '+') |
| 5185 | use_word = !use_word; |
| 5186 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5187 | (exp_save == ':') ? "true" : "false", use_word); |
| 5188 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5189 | 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] | 5190 | if (to_be_freed) |
| 5191 | exp_word = to_be_freed; |
| 5192 | if (exp_op == '?') { |
| 5193 | /* mimic bash message */ |
| 5194 | die_if_script("%s: %s", |
| 5195 | var, |
| 5196 | exp_word[0] ? exp_word : "parameter null or not set" |
| 5197 | ); |
| 5198 | //TODO: how interactive bash aborts expansion mid-command? |
| 5199 | } else { |
| 5200 | val = exp_word; |
| 5201 | } |
| 5202 | |
| 5203 | if (exp_op == '=') { |
| 5204 | /* ${var=[word]} or ${var:=[word]} */ |
| 5205 | if (isdigit(var[0]) || var[0] == '#') { |
| 5206 | /* mimic bash message */ |
| 5207 | die_if_script("$%s: cannot assign in this way", var); |
| 5208 | val = NULL; |
| 5209 | } else { |
| 5210 | char *new_var = xasprintf("%s=%s", var, val); |
| 5211 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5212 | } |
| 5213 | } |
| 5214 | } |
| 5215 | } /* one of "-=+?" */ |
| 5216 | |
| 5217 | *exp_saveptr = exp_save; |
| 5218 | } /* if (exp_op) */ |
| 5219 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5220 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5221 | |
| 5222 | *pp = p; |
| 5223 | *to_be_freed_pp = to_be_freed; |
| 5224 | return val; |
| 5225 | } |
| 5226 | |
| 5227 | /* Expand all variable references in given string, adding words to list[] |
| 5228 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5229 | * to be filled). This routine is extremely tricky: has to deal with |
| 5230 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5231 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5232 | 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] | 5233 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5234 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5235 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5236 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5237 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5238 | int ended_in_ifs = 0; /* did last unquoted expansion end with IFS chars? */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5239 | char *p; |
| 5240 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5241 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5242 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5243 | debug_print_list("expand_vars_to_list", output, n); |
| 5244 | n = o_save_ptr(output, n); |
| 5245 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5246 | |
| 5247 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5248 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5249 | char *to_be_freed = NULL; |
| 5250 | const char *val = NULL; |
| 5251 | #if ENABLE_HUSH_TICK |
| 5252 | o_string subst_result = NULL_O_STRING; |
| 5253 | #endif |
| 5254 | #if ENABLE_SH_MATH_SUPPORT |
| 5255 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5256 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5257 | |
| 5258 | if (ended_in_ifs) { |
| 5259 | o_addchr(output, '\0'); |
| 5260 | n = o_save_ptr(output, n); |
| 5261 | ended_in_ifs = 0; |
| 5262 | } |
| 5263 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5264 | o_addblock(output, arg, p - arg); |
| 5265 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5266 | arg = ++p; |
| 5267 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5268 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5269 | /* Fetch special var name (if it is indeed one of them) |
| 5270 | * and quote bit, force the bit on if singleword expansion - |
| 5271 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5272 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5273 | |
| 5274 | /* Is this variable quoted and thus expansion can't be null? |
| 5275 | * "$@" is special. Even if quoted, it can still |
| 5276 | * expand to nothing (not even an empty string), |
| 5277 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5278 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5279 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5280 | |
| 5281 | switch (first_ch & 0x7f) { |
| 5282 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5283 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5284 | case '@': { |
| 5285 | int i; |
| 5286 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5287 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5288 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5289 | 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] | 5290 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5291 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5292 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5293 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5294 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5295 | /* this argv[] is not empty and not last: |
| 5296 | * put terminating NUL, start new word */ |
| 5297 | o_addchr(output, '\0'); |
| 5298 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5299 | n = o_save_ptr(output, n); |
| 5300 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5301 | } |
| 5302 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5303 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5304 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5305 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5306 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5307 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5308 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5309 | while (1) { |
| 5310 | o_addQstr(output, G.global_argv[i]); |
| 5311 | if (++i >= G.global_argc) |
| 5312 | break; |
| 5313 | o_addchr(output, '\0'); |
| 5314 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5315 | n = o_save_ptr(output, n); |
| 5316 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5317 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5318 | while (1) { |
| 5319 | o_addQstr(output, G.global_argv[i]); |
| 5320 | if (!G.global_argv[++i]) |
| 5321 | break; |
| 5322 | if (G.ifs[0]) |
| 5323 | o_addchr(output, G.ifs[0]); |
| 5324 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5325 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5326 | } |
| 5327 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5328 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5329 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5330 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5331 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5332 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5333 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5334 | break; |
| 5335 | #if ENABLE_HUSH_TICK |
| 5336 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5337 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5338 | arg++; |
| 5339 | /* Can't just stuff it into output o_string, |
| 5340 | * expanded result may need to be globbed |
| 5341 | * and $IFS-splitted */ |
| 5342 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5343 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5344 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5345 | val = subst_result.data; |
| 5346 | goto store_val; |
| 5347 | #endif |
| 5348 | #if ENABLE_SH_MATH_SUPPORT |
| 5349 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5350 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5351 | |
| 5352 | arg++; /* skip '+' */ |
| 5353 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5354 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5355 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5356 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5357 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5358 | val = arith_buf; |
| 5359 | break; |
| 5360 | } |
| 5361 | #endif |
| 5362 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5363 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5364 | IF_HUSH_TICK(store_val:) |
| 5365 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5366 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5367 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5368 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5369 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5370 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5371 | } |
| 5372 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5373 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5374 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5375 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5376 | } |
| 5377 | break; |
| 5378 | |
| 5379 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5380 | |
| 5381 | if (val && val[0]) { |
| 5382 | o_addQstr(output, val); |
| 5383 | } |
| 5384 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5385 | |
| 5386 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5387 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5388 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5389 | *p = SPECIAL_VAR_SYMBOL; |
| 5390 | |
| 5391 | #if ENABLE_HUSH_TICK |
| 5392 | o_free(&subst_result); |
| 5393 | #endif |
| 5394 | arg = ++p; |
| 5395 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5396 | |
| 5397 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5398 | if (ended_in_ifs) { |
| 5399 | o_addchr(output, '\0'); |
| 5400 | n = o_save_ptr(output, n); |
| 5401 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5402 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5403 | /* this part is literal, and it was already pre-quoted |
| 5404 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5405 | o_addstr_with_NUL(output, arg); |
| 5406 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5407 | } 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] | 5408 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5409 | ) { |
| 5410 | n--; |
| 5411 | /* allow to reuse list[n] later without re-growth */ |
| 5412 | output->has_empty_slot = 1; |
| 5413 | } else { |
| 5414 | o_addchr(output, '\0'); |
| 5415 | } |
| 5416 | |
| 5417 | return n; |
| 5418 | } |
| 5419 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5420 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5421 | { |
| 5422 | int n; |
| 5423 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5424 | o_string output = NULL_O_STRING; |
| 5425 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5426 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5427 | |
| 5428 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5429 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5430 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5431 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5432 | } |
| 5433 | debug_print_list("expand_variables", &output, n); |
| 5434 | |
| 5435 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5436 | list = o_finalize_list(&output, n); |
| 5437 | debug_print_strings("expand_variables[1]", list); |
| 5438 | return list; |
| 5439 | } |
| 5440 | |
| 5441 | static char **expand_strvec_to_strvec(char **argv) |
| 5442 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5443 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5444 | } |
| 5445 | |
| 5446 | #if ENABLE_HUSH_BASH_COMPAT |
| 5447 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5448 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5449 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5450 | } |
| 5451 | #endif |
| 5452 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5453 | /* Used for expansion of right hand of assignments, |
| 5454 | * $((...)), heredocs, variable espansion parts. |
| 5455 | * |
| 5456 | * NB: should NOT do globbing! |
| 5457 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5458 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5459 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5460 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5461 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5462 | const int do_unbackslash = 1; |
| 5463 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5464 | char *argv[2], **list; |
| 5465 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5466 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5467 | /* This is generally an optimization, but it also |
| 5468 | * handles "", which otherwise trips over !list[0] check below. |
| 5469 | * (is this ever happens that we actually get str="" here?) |
| 5470 | */ |
| 5471 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5472 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5473 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5474 | return xstrdup(str); |
| 5475 | } |
| 5476 | |
| 5477 | argv[0] = (char*)str; |
| 5478 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5479 | list = expand_variables(argv, do_unbackslash |
| 5480 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5481 | : EXP_FLAG_SINGLEWORD |
| 5482 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5483 | if (HUSH_DEBUG) |
| 5484 | if (!list[0] || list[1]) |
| 5485 | bb_error_msg_and_die("BUG in varexp2"); |
| 5486 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5487 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5488 | if (do_unbackslash) |
| 5489 | unbackslash((char*)list); |
| 5490 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5491 | return (char*)list; |
| 5492 | } |
| 5493 | |
| 5494 | /* Used for "eval" builtin */ |
| 5495 | static char* expand_strvec_to_string(char **argv) |
| 5496 | { |
| 5497 | char **list; |
| 5498 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5499 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5500 | /* Convert all NULs to spaces */ |
| 5501 | if (list[0]) { |
| 5502 | int n = 1; |
| 5503 | while (list[n]) { |
| 5504 | if (HUSH_DEBUG) |
| 5505 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5506 | bb_error_msg_and_die("BUG in varexp3"); |
| 5507 | /* bash uses ' ' regardless of $IFS contents */ |
| 5508 | list[n][-1] = ' '; |
| 5509 | n++; |
| 5510 | } |
| 5511 | } |
| 5512 | overlapping_strcpy((char*)list, list[0]); |
| 5513 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5514 | return (char*)list; |
| 5515 | } |
| 5516 | |
| 5517 | static char **expand_assignments(char **argv, int count) |
| 5518 | { |
| 5519 | int i; |
| 5520 | char **p; |
| 5521 | |
| 5522 | G.expanded_assignments = p = NULL; |
| 5523 | /* Expand assignments into one string each */ |
| 5524 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5525 | 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] | 5526 | } |
| 5527 | G.expanded_assignments = NULL; |
| 5528 | return p; |
| 5529 | } |
| 5530 | |
| 5531 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5532 | static void switch_off_special_sigs(unsigned mask) |
| 5533 | { |
| 5534 | unsigned sig = 0; |
| 5535 | while ((mask >>= 1) != 0) { |
| 5536 | sig++; |
| 5537 | if (!(mask & 1)) |
| 5538 | continue; |
| 5539 | if (G.traps) { |
| 5540 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5541 | /* trap is '', has to remain SIG_IGN */ |
| 5542 | continue; |
| 5543 | free(G.traps[sig]); |
| 5544 | G.traps[sig] = NULL; |
| 5545 | } |
| 5546 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5547 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5548 | } |
| 5549 | } |
| 5550 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 5551 | #if BB_MMU |
| 5552 | /* never called */ |
| 5553 | void re_execute_shell(char ***to_free, const char *s, |
| 5554 | char *g_argv0, char **g_argv, |
| 5555 | char **builtin_argv) NORETURN; |
| 5556 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5557 | static void reset_traps_to_defaults(void) |
| 5558 | { |
| 5559 | /* This function is always called in a child shell |
| 5560 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5561 | */ |
| 5562 | unsigned sig; |
| 5563 | unsigned mask; |
| 5564 | |
| 5565 | /* Child shells are not interactive. |
| 5566 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5567 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5568 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5569 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5570 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
| 5571 | if (!G.traps && !mask) |
| 5572 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5573 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5574 | /* Switch off special sigs */ |
| 5575 | switch_off_special_sigs(mask); |
| 5576 | #if ENABLE_HUSH_JOB |
| 5577 | G_fatal_sig_mask = 0; |
| 5578 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 5579 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 5580 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 5581 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5582 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5583 | if (!G.traps) |
| 5584 | return; |
| 5585 | |
| 5586 | /* Reset all sigs to default except ones with empty traps */ |
| 5587 | for (sig = 0; sig < NSIG; sig++) { |
| 5588 | if (!G.traps[sig]) |
| 5589 | continue; /* no trap: nothing to do */ |
| 5590 | if (!G.traps[sig][0]) |
| 5591 | continue; /* empty trap: has to remain SIG_IGN */ |
| 5592 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5593 | free(G.traps[sig]); |
| 5594 | G.traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5595 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5596 | if (sig == 0) |
| 5597 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5598 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5599 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5600 | } |
| 5601 | |
| 5602 | #else /* !BB_MMU */ |
| 5603 | |
| 5604 | static void re_execute_shell(char ***to_free, const char *s, |
| 5605 | char *g_argv0, char **g_argv, |
| 5606 | char **builtin_argv) NORETURN; |
| 5607 | static void re_execute_shell(char ***to_free, const char *s, |
| 5608 | char *g_argv0, char **g_argv, |
| 5609 | char **builtin_argv) |
| 5610 | { |
| 5611 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5612 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5613 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5614 | char *heredoc_argv[4]; |
| 5615 | struct variable *cur; |
| 5616 | # if ENABLE_HUSH_FUNCTIONS |
| 5617 | struct function *funcp; |
| 5618 | # endif |
| 5619 | char **argv, **pp; |
| 5620 | unsigned cnt; |
| 5621 | unsigned long long empty_trap_mask; |
| 5622 | |
| 5623 | if (!g_argv0) { /* heredoc */ |
| 5624 | argv = heredoc_argv; |
| 5625 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5626 | argv[1] = (char *) "-<"; |
| 5627 | argv[2] = (char *) s; |
| 5628 | argv[3] = NULL; |
| 5629 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5630 | goto do_exec; |
| 5631 | } |
| 5632 | |
| 5633 | cnt = 0; |
| 5634 | pp = builtin_argv; |
| 5635 | if (pp) while (*pp++) |
| 5636 | cnt++; |
| 5637 | |
| 5638 | empty_trap_mask = 0; |
| 5639 | if (G.traps) { |
| 5640 | int sig; |
| 5641 | for (sig = 1; sig < NSIG; sig++) { |
| 5642 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5643 | empty_trap_mask |= 1LL << sig; |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5648 | , (unsigned) G.root_pid |
| 5649 | , (unsigned) G.root_ppid |
| 5650 | , (unsigned) G.last_bg_pid |
| 5651 | , (unsigned) G.last_exitcode |
| 5652 | , cnt |
| 5653 | , empty_trap_mask |
| 5654 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5655 | ); |
| 5656 | # undef NOMMU_HACK_FMT |
| 5657 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5658 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5659 | */ |
| 5660 | cnt += 6; |
| 5661 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5662 | if (!cur->flg_export || cur->flg_read_only) |
| 5663 | cnt += 2; |
| 5664 | } |
| 5665 | # if ENABLE_HUSH_FUNCTIONS |
| 5666 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5667 | cnt += 3; |
| 5668 | # endif |
| 5669 | pp = g_argv; |
| 5670 | while (*pp++) |
| 5671 | cnt++; |
| 5672 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5673 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5674 | *pp++ = param_buf; |
| 5675 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5676 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5677 | continue; |
| 5678 | if (cur->flg_read_only) { |
| 5679 | *pp++ = (char *) "-R"; |
| 5680 | *pp++ = cur->varstr; |
| 5681 | } else if (!cur->flg_export) { |
| 5682 | *pp++ = (char *) "-V"; |
| 5683 | *pp++ = cur->varstr; |
| 5684 | } |
| 5685 | } |
| 5686 | # if ENABLE_HUSH_FUNCTIONS |
| 5687 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 5688 | *pp++ = (char *) "-F"; |
| 5689 | *pp++ = funcp->name; |
| 5690 | *pp++ = funcp->body_as_string; |
| 5691 | } |
| 5692 | # endif |
| 5693 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 5694 | * |
| 5695 | * However, POSIX says that subshells reset signals with traps |
| 5696 | * to SIG_DFL. |
| 5697 | * I tested bash-3.2 and it not only does that with true subshells |
| 5698 | * of the form ( list ), but with any forked children shells. |
| 5699 | * I set trap "echo W" WINCH; and then tried: |
| 5700 | * |
| 5701 | * { echo 1; sleep 20; echo 2; } & |
| 5702 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 5703 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 5704 | * |
| 5705 | * In all these cases sending SIGWINCH to the child shell |
| 5706 | * did not run the trap. If I add trap "echo V" WINCH; |
| 5707 | * _inside_ group (just before echo 1), it works. |
| 5708 | * |
| 5709 | * I conclude it means we don't need to pass active traps here. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5710 | */ |
| 5711 | *pp++ = (char *) "-c"; |
| 5712 | *pp++ = (char *) s; |
| 5713 | if (builtin_argv) { |
| 5714 | while (*++builtin_argv) |
| 5715 | *pp++ = *builtin_argv; |
| 5716 | *pp++ = (char *) ""; |
| 5717 | } |
| 5718 | *pp++ = g_argv0; |
| 5719 | while (*g_argv) |
| 5720 | *pp++ = *g_argv++; |
| 5721 | /* *pp = NULL; - is already there */ |
| 5722 | pp = environ; |
| 5723 | |
| 5724 | do_exec: |
| 5725 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 5726 | /* Don't propagate SIG_IGN to the child */ |
| 5727 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 5728 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5729 | execve(bb_busybox_exec_path, argv, pp); |
| 5730 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 5731 | if (argv[0][0] == '/') |
| 5732 | execve(argv[0], argv, pp); |
| 5733 | xfunc_error_retval = 127; |
| 5734 | bb_error_msg_and_die("can't re-execute the shell"); |
| 5735 | } |
| 5736 | #endif /* !BB_MMU */ |
| 5737 | |
| 5738 | |
| 5739 | static int run_and_free_list(struct pipe *pi); |
| 5740 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5741 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5742 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5743 | * end_trigger controls how often we stop parsing |
| 5744 | * NUL: parse all, execute, return |
| 5745 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5746 | */ |
| 5747 | 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] | 5748 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5749 | /* Why we need empty flag? |
| 5750 | * An obscure corner case "false; ``; echo $?": |
| 5751 | * empty command in `` should still set $? to 0. |
| 5752 | * But we can't just set $? to 0 at the start, |
| 5753 | * this breaks "false; echo `echo $?`" case. |
| 5754 | */ |
| 5755 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5756 | while (1) { |
| 5757 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5758 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 5759 | #if ENABLE_HUSH_INTERACTIVE |
| 5760 | if (end_trigger == ';') |
| 5761 | inp->promptmode = 0; /* PS1 */ |
| 5762 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5763 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5764 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 5765 | /* If we are in "big" script |
| 5766 | * (not in `cmd` or something similar)... |
| 5767 | */ |
| 5768 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 5769 | /* Discard cached input (rest of line) */ |
| 5770 | int ch = inp->last_char; |
| 5771 | while (ch != EOF && ch != '\n') { |
| 5772 | //bb_error_msg("Discarded:'%c'", ch); |
| 5773 | ch = i_getch(inp); |
| 5774 | } |
| 5775 | /* Force prompt */ |
| 5776 | inp->p = NULL; |
| 5777 | /* This stream isn't empty */ |
| 5778 | empty = 0; |
| 5779 | continue; |
| 5780 | } |
| 5781 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5782 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5783 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5784 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5785 | debug_print_tree(pipe_list, 0); |
| 5786 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5787 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5788 | empty = 0; |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 5789 | #if ENABLE_HUSH_FUNCTIONS |
| 5790 | if (G.flag_return_in_progress == 1) |
| 5791 | break; |
| 5792 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5793 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5794 | } |
| 5795 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5796 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5797 | { |
| 5798 | struct in_str input; |
| 5799 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5800 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5801 | } |
| 5802 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5803 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5804 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5805 | struct in_str input; |
| 5806 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5807 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5810 | #if ENABLE_HUSH_TICK |
| 5811 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 5812 | { |
| 5813 | pid_t pid; |
| 5814 | int channel[2]; |
| 5815 | # if !BB_MMU |
| 5816 | char **to_free = NULL; |
| 5817 | # endif |
| 5818 | |
| 5819 | xpipe(channel); |
| 5820 | pid = BB_MMU ? xfork() : xvfork(); |
| 5821 | if (pid == 0) { /* child */ |
| 5822 | disable_restore_tty_pgrp_on_exit(); |
| 5823 | /* Process substitution is not considered to be usual |
| 5824 | * 'command execution'. |
| 5825 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5826 | */ |
| 5827 | bb_signals(0 |
| 5828 | + (1 << SIGTSTP) |
| 5829 | + (1 << SIGTTIN) |
| 5830 | + (1 << SIGTTOU) |
| 5831 | , SIG_IGN); |
| 5832 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 5833 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5834 | xmove_fd(channel[1], 1); |
| 5835 | /* Prevent it from trying to handle ctrl-z etc */ |
| 5836 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 5837 | /* Awful hack for `trap` or $(trap). |
| 5838 | * |
| 5839 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5840 | * contains an example where "trap" is executed in a subshell: |
| 5841 | * |
| 5842 | * save_traps=$(trap) |
| 5843 | * ... |
| 5844 | * eval "$save_traps" |
| 5845 | * |
| 5846 | * Standard does not say that "trap" in subshell shall print |
| 5847 | * parent shell's traps. It only says that its output |
| 5848 | * must have suitable form, but then, in the above example |
| 5849 | * (which is not supposed to be normative), it implies that. |
| 5850 | * |
| 5851 | * bash (and probably other shell) does implement it |
| 5852 | * (traps are reset to defaults, but "trap" still shows them), |
| 5853 | * but as a result, "trap" logic is hopelessly messed up: |
| 5854 | * |
| 5855 | * # trap |
| 5856 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5857 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5858 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5859 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5860 | * trap -- 'echo Ho' SIGWINCH |
| 5861 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5862 | * trap -- 'echo Ho' SIGWINCH |
| 5863 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5864 | * trap -- 'echo Ho' SIGWINCH |
| 5865 | * |
| 5866 | * The rules when to forget and when to not forget traps |
| 5867 | * get really complex and nonsensical. |
| 5868 | * |
| 5869 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5870 | */ |
| 5871 | s = skip_whitespace(s); |
| 5872 | if (strncmp(s, "trap", 4) == 0 |
| 5873 | && skip_whitespace(s + 4)[0] == '\0' |
| 5874 | ) { |
| 5875 | static const char *const argv[] = { NULL, NULL }; |
| 5876 | builtin_trap((char**)argv); |
| 5877 | exit(0); /* not _exit() - we need to fflush */ |
| 5878 | } |
| 5879 | # if BB_MMU |
| 5880 | reset_traps_to_defaults(); |
| 5881 | parse_and_run_string(s); |
| 5882 | _exit(G.last_exitcode); |
| 5883 | # else |
| 5884 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 5885 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5886 | * huge=`cat BIG` # was blocking here forever |
| 5887 | * echo OK |
| 5888 | */ |
| 5889 | re_execute_shell(&to_free, |
| 5890 | s, |
| 5891 | G.global_argv[0], |
| 5892 | G.global_argv + 1, |
| 5893 | NULL); |
| 5894 | # endif |
| 5895 | } |
| 5896 | |
| 5897 | /* parent */ |
| 5898 | *pid_p = pid; |
| 5899 | # if ENABLE_HUSH_FAST |
| 5900 | G.count_SIGCHLD++; |
| 5901 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 5902 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 5903 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5904 | # endif |
| 5905 | enable_restore_tty_pgrp_on_exit(); |
| 5906 | # if !BB_MMU |
| 5907 | free(to_free); |
| 5908 | # endif |
| 5909 | close(channel[1]); |
| 5910 | close_on_exec_on(channel[0]); |
| 5911 | return xfdopen_for_read(channel[0]); |
| 5912 | } |
| 5913 | |
| 5914 | /* Return code is exit status of the process that is run. */ |
| 5915 | static int process_command_subs(o_string *dest, const char *s) |
| 5916 | { |
| 5917 | FILE *fp; |
| 5918 | struct in_str pipe_str; |
| 5919 | pid_t pid; |
| 5920 | int status, ch, eol_cnt; |
| 5921 | |
| 5922 | fp = generate_stream_from_string(s, &pid); |
| 5923 | |
| 5924 | /* Now send results of command back into original context */ |
| 5925 | setup_file_in_str(&pipe_str, fp); |
| 5926 | eol_cnt = 0; |
| 5927 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 5928 | if (ch == '\n') { |
| 5929 | eol_cnt++; |
| 5930 | continue; |
| 5931 | } |
| 5932 | while (eol_cnt) { |
| 5933 | o_addchr(dest, '\n'); |
| 5934 | eol_cnt--; |
| 5935 | } |
| 5936 | o_addQchr(dest, ch); |
| 5937 | } |
| 5938 | |
| 5939 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
| 5940 | fclose(fp); |
| 5941 | /* We need to extract exitcode. Test case |
| 5942 | * "true; echo `sleep 1; false` $?" |
| 5943 | * should print 1 */ |
| 5944 | safe_waitpid(pid, &status, 0); |
| 5945 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5946 | return WEXITSTATUS(status); |
| 5947 | } |
| 5948 | #endif /* ENABLE_HUSH_TICK */ |
| 5949 | |
| 5950 | |
| 5951 | static void setup_heredoc(struct redir_struct *redir) |
| 5952 | { |
| 5953 | struct fd_pair pair; |
| 5954 | pid_t pid; |
| 5955 | int len, written; |
| 5956 | /* the _body_ of heredoc (misleading field name) */ |
| 5957 | const char *heredoc = redir->rd_filename; |
| 5958 | char *expanded; |
| 5959 | #if !BB_MMU |
| 5960 | char **to_free; |
| 5961 | #endif |
| 5962 | |
| 5963 | expanded = NULL; |
| 5964 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5965 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5966 | if (expanded) |
| 5967 | heredoc = expanded; |
| 5968 | } |
| 5969 | len = strlen(heredoc); |
| 5970 | |
| 5971 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 5972 | xpiped_pair(pair); |
| 5973 | xmove_fd(pair.rd, redir->rd_fd); |
| 5974 | |
| 5975 | /* Try writing without forking. Newer kernels have |
| 5976 | * dynamically growing pipes. Must use non-blocking write! */ |
| 5977 | ndelay_on(pair.wr); |
| 5978 | while (1) { |
| 5979 | written = write(pair.wr, heredoc, len); |
| 5980 | if (written <= 0) |
| 5981 | break; |
| 5982 | len -= written; |
| 5983 | if (len == 0) { |
| 5984 | close(pair.wr); |
| 5985 | free(expanded); |
| 5986 | return; |
| 5987 | } |
| 5988 | heredoc += written; |
| 5989 | } |
| 5990 | ndelay_off(pair.wr); |
| 5991 | |
| 5992 | /* Okay, pipe buffer was not big enough */ |
| 5993 | /* Note: we must not create a stray child (bastard? :) |
| 5994 | * for the unsuspecting parent process. Child creates a grandchild |
| 5995 | * and exits before parent execs the process which consumes heredoc |
| 5996 | * (that exec happens after we return from this function) */ |
| 5997 | #if !BB_MMU |
| 5998 | to_free = NULL; |
| 5999 | #endif |
| 6000 | pid = xvfork(); |
| 6001 | if (pid == 0) { |
| 6002 | /* child */ |
| 6003 | disable_restore_tty_pgrp_on_exit(); |
| 6004 | pid = BB_MMU ? xfork() : xvfork(); |
| 6005 | if (pid != 0) |
| 6006 | _exit(0); |
| 6007 | /* grandchild */ |
| 6008 | close(redir->rd_fd); /* read side of the pipe */ |
| 6009 | #if BB_MMU |
| 6010 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6011 | _exit(0); |
| 6012 | #else |
| 6013 | /* Delegate blocking writes to another process */ |
| 6014 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6015 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6016 | #endif |
| 6017 | } |
| 6018 | /* parent */ |
| 6019 | #if ENABLE_HUSH_FAST |
| 6020 | G.count_SIGCHLD++; |
| 6021 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6022 | #endif |
| 6023 | enable_restore_tty_pgrp_on_exit(); |
| 6024 | #if !BB_MMU |
| 6025 | free(to_free); |
| 6026 | #endif |
| 6027 | close(pair.wr); |
| 6028 | free(expanded); |
| 6029 | wait(NULL); /* wait till child has died */ |
| 6030 | } |
| 6031 | |
| 6032 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6033 | * and stderr if they are redirected. */ |
| 6034 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 6035 | { |
| 6036 | int openfd, mode; |
| 6037 | struct redir_struct *redir; |
| 6038 | |
| 6039 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 6040 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
| 6041 | /* rd_fd<<HERE case */ |
| 6042 | if (squirrel && redir->rd_fd < 3 |
| 6043 | && squirrel[redir->rd_fd] < 0 |
| 6044 | ) { |
| 6045 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 6046 | } |
| 6047 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6048 | * of the heredoc */ |
| 6049 | debug_printf_parse("set heredoc '%s'\n", |
| 6050 | redir->rd_filename); |
| 6051 | setup_heredoc(redir); |
| 6052 | continue; |
| 6053 | } |
| 6054 | |
| 6055 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 6056 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
| 6057 | char *p; |
| 6058 | if (redir->rd_filename == NULL) { |
| 6059 | /* Something went wrong in the parse. |
| 6060 | * Pretend it didn't happen */ |
| 6061 | bb_error_msg("bug in redirect parse"); |
| 6062 | continue; |
| 6063 | } |
| 6064 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6065 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6066 | openfd = open_or_warn(p, mode); |
| 6067 | free(p); |
| 6068 | if (openfd < 0) { |
| 6069 | /* this could get lost if stderr has been redirected, but |
| 6070 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 6071 | //what the above comment tries to say? |
| 6072 | return 1; |
| 6073 | } |
| 6074 | } else { |
| 6075 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
| 6076 | openfd = redir->rd_dup; |
| 6077 | } |
| 6078 | |
| 6079 | if (openfd != redir->rd_fd) { |
| 6080 | if (squirrel && redir->rd_fd < 3 |
| 6081 | && squirrel[redir->rd_fd] < 0 |
| 6082 | ) { |
| 6083 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 6084 | } |
| 6085 | if (openfd == REDIRFD_CLOSE) { |
| 6086 | /* "n>-" means "close me" */ |
| 6087 | close(redir->rd_fd); |
| 6088 | } else { |
| 6089 | xdup2(openfd, redir->rd_fd); |
| 6090 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 6091 | close(openfd); |
| 6092 | } |
| 6093 | } |
| 6094 | } |
| 6095 | return 0; |
| 6096 | } |
| 6097 | |
| 6098 | static void restore_redirects(int squirrel[]) |
| 6099 | { |
| 6100 | int i, fd; |
| 6101 | for (i = 0; i < 3; i++) { |
| 6102 | fd = squirrel[i]; |
| 6103 | if (fd != -1) { |
| 6104 | /* We simply die on error */ |
| 6105 | xmove_fd(fd, i); |
| 6106 | } |
| 6107 | } |
| 6108 | } |
| 6109 | |
| 6110 | static char *find_in_path(const char *arg) |
| 6111 | { |
| 6112 | char *ret = NULL; |
| 6113 | const char *PATH = get_local_var_value("PATH"); |
| 6114 | |
| 6115 | if (!PATH) |
| 6116 | return NULL; |
| 6117 | |
| 6118 | while (1) { |
| 6119 | const char *end = strchrnul(PATH, ':'); |
| 6120 | int sz = end - PATH; /* must be int! */ |
| 6121 | |
| 6122 | free(ret); |
| 6123 | if (sz != 0) { |
| 6124 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6125 | } else { |
| 6126 | /* We have xxx::yyyy in $PATH, |
| 6127 | * it means "use current dir" */ |
| 6128 | ret = xstrdup(arg); |
| 6129 | } |
| 6130 | if (access(ret, F_OK) == 0) |
| 6131 | break; |
| 6132 | |
| 6133 | if (*end == '\0') { |
| 6134 | free(ret); |
| 6135 | return NULL; |
| 6136 | } |
| 6137 | PATH = end + 1; |
| 6138 | } |
| 6139 | |
| 6140 | return ret; |
| 6141 | } |
| 6142 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6143 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6144 | const struct built_in_command *x, |
| 6145 | const struct built_in_command *end) |
| 6146 | { |
| 6147 | while (x != end) { |
| 6148 | if (strcmp(name, x->b_cmd) != 0) { |
| 6149 | x++; |
| 6150 | continue; |
| 6151 | } |
| 6152 | debug_printf_exec("found builtin '%s'\n", name); |
| 6153 | return x; |
| 6154 | } |
| 6155 | return NULL; |
| 6156 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6157 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6158 | { |
| 6159 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 6160 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6161 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6162 | { |
| 6163 | const struct built_in_command *x = find_builtin1(name); |
| 6164 | if (x) |
| 6165 | return x; |
| 6166 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 6167 | } |
| 6168 | |
| 6169 | #if ENABLE_HUSH_FUNCTIONS |
| 6170 | static struct function **find_function_slot(const char *name) |
| 6171 | { |
| 6172 | struct function **funcpp = &G.top_func; |
| 6173 | while (*funcpp) { |
| 6174 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 6175 | break; |
| 6176 | } |
| 6177 | funcpp = &(*funcpp)->next; |
| 6178 | } |
| 6179 | return funcpp; |
| 6180 | } |
| 6181 | |
| 6182 | static const struct function *find_function(const char *name) |
| 6183 | { |
| 6184 | const struct function *funcp = *find_function_slot(name); |
| 6185 | if (funcp) |
| 6186 | debug_printf_exec("found function '%s'\n", name); |
| 6187 | return funcp; |
| 6188 | } |
| 6189 | |
| 6190 | /* Note: takes ownership on name ptr */ |
| 6191 | static struct function *new_function(char *name) |
| 6192 | { |
| 6193 | struct function **funcpp = find_function_slot(name); |
| 6194 | struct function *funcp = *funcpp; |
| 6195 | |
| 6196 | if (funcp != NULL) { |
| 6197 | struct command *cmd = funcp->parent_cmd; |
| 6198 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 6199 | if (!cmd) { |
| 6200 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 6201 | free(funcp->name); |
| 6202 | /* Note: if !funcp->body, do not free body_as_string! |
| 6203 | * This is a special case of "-F name body" function: |
| 6204 | * body_as_string was not malloced! */ |
| 6205 | if (funcp->body) { |
| 6206 | free_pipe_list(funcp->body); |
| 6207 | # if !BB_MMU |
| 6208 | free(funcp->body_as_string); |
| 6209 | # endif |
| 6210 | } |
| 6211 | } else { |
| 6212 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 6213 | cmd->argv[0] = funcp->name; |
| 6214 | cmd->group = funcp->body; |
| 6215 | # if !BB_MMU |
| 6216 | cmd->group_as_string = funcp->body_as_string; |
| 6217 | # endif |
| 6218 | } |
| 6219 | } else { |
| 6220 | debug_printf_exec("remembering new function '%s'\n", name); |
| 6221 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 6222 | /*funcp->next = NULL;*/ |
| 6223 | } |
| 6224 | |
| 6225 | funcp->name = name; |
| 6226 | return funcp; |
| 6227 | } |
| 6228 | |
| 6229 | static void unset_func(const char *name) |
| 6230 | { |
| 6231 | struct function **funcpp = find_function_slot(name); |
| 6232 | struct function *funcp = *funcpp; |
| 6233 | |
| 6234 | if (funcp != NULL) { |
| 6235 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 6236 | *funcpp = funcp->next; |
| 6237 | /* funcp is unlinked now, deleting it. |
| 6238 | * Note: if !funcp->body, the function was created by |
| 6239 | * "-F name body", do not free ->body_as_string |
| 6240 | * and ->name as they were not malloced. */ |
| 6241 | if (funcp->body) { |
| 6242 | free_pipe_list(funcp->body); |
| 6243 | free(funcp->name); |
| 6244 | # if !BB_MMU |
| 6245 | free(funcp->body_as_string); |
| 6246 | # endif |
| 6247 | } |
| 6248 | free(funcp); |
| 6249 | } |
| 6250 | } |
| 6251 | |
| 6252 | # if BB_MMU |
| 6253 | #define exec_function(to_free, funcp, argv) \ |
| 6254 | exec_function(funcp, argv) |
| 6255 | # endif |
| 6256 | static void exec_function(char ***to_free, |
| 6257 | const struct function *funcp, |
| 6258 | char **argv) NORETURN; |
| 6259 | static void exec_function(char ***to_free, |
| 6260 | const struct function *funcp, |
| 6261 | char **argv) |
| 6262 | { |
| 6263 | # if BB_MMU |
| 6264 | int n = 1; |
| 6265 | |
| 6266 | argv[0] = G.global_argv[0]; |
| 6267 | G.global_argv = argv; |
| 6268 | while (*++argv) |
| 6269 | n++; |
| 6270 | G.global_argc = n; |
| 6271 | /* On MMU, funcp->body is always non-NULL */ |
| 6272 | n = run_list(funcp->body); |
| 6273 | fflush_all(); |
| 6274 | _exit(n); |
| 6275 | # else |
| 6276 | re_execute_shell(to_free, |
| 6277 | funcp->body_as_string, |
| 6278 | G.global_argv[0], |
| 6279 | argv + 1, |
| 6280 | NULL); |
| 6281 | # endif |
| 6282 | } |
| 6283 | |
| 6284 | static int run_function(const struct function *funcp, char **argv) |
| 6285 | { |
| 6286 | int rc; |
| 6287 | save_arg_t sv; |
| 6288 | smallint sv_flg; |
| 6289 | |
| 6290 | save_and_replace_G_args(&sv, argv); |
| 6291 | |
| 6292 | /* "we are in function, ok to use return" */ |
| 6293 | sv_flg = G.flag_return_in_progress; |
| 6294 | G.flag_return_in_progress = -1; |
| 6295 | # if ENABLE_HUSH_LOCAL |
| 6296 | G.func_nest_level++; |
| 6297 | # endif |
| 6298 | |
| 6299 | /* On MMU, funcp->body is always non-NULL */ |
| 6300 | # if !BB_MMU |
| 6301 | if (!funcp->body) { |
| 6302 | /* Function defined by -F */ |
| 6303 | parse_and_run_string(funcp->body_as_string); |
| 6304 | rc = G.last_exitcode; |
| 6305 | } else |
| 6306 | # endif |
| 6307 | { |
| 6308 | rc = run_list(funcp->body); |
| 6309 | } |
| 6310 | |
| 6311 | # if ENABLE_HUSH_LOCAL |
| 6312 | { |
| 6313 | struct variable *var; |
| 6314 | struct variable **var_pp; |
| 6315 | |
| 6316 | var_pp = &G.top_var; |
| 6317 | while ((var = *var_pp) != NULL) { |
| 6318 | if (var->func_nest_level < G.func_nest_level) { |
| 6319 | var_pp = &var->next; |
| 6320 | continue; |
| 6321 | } |
| 6322 | /* Unexport */ |
| 6323 | if (var->flg_export) |
| 6324 | bb_unsetenv(var->varstr); |
| 6325 | /* Remove from global list */ |
| 6326 | *var_pp = var->next; |
| 6327 | /* Free */ |
| 6328 | if (!var->max_len) |
| 6329 | free(var->varstr); |
| 6330 | free(var); |
| 6331 | } |
| 6332 | G.func_nest_level--; |
| 6333 | } |
| 6334 | # endif |
| 6335 | G.flag_return_in_progress = sv_flg; |
| 6336 | |
| 6337 | restore_G_args(&sv, argv); |
| 6338 | |
| 6339 | return rc; |
| 6340 | } |
| 6341 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6342 | |
| 6343 | |
| 6344 | #if BB_MMU |
| 6345 | #define exec_builtin(to_free, x, argv) \ |
| 6346 | exec_builtin(x, argv) |
| 6347 | #else |
| 6348 | #define exec_builtin(to_free, x, argv) \ |
| 6349 | exec_builtin(to_free, argv) |
| 6350 | #endif |
| 6351 | static void exec_builtin(char ***to_free, |
| 6352 | const struct built_in_command *x, |
| 6353 | char **argv) NORETURN; |
| 6354 | static void exec_builtin(char ***to_free, |
| 6355 | const struct built_in_command *x, |
| 6356 | char **argv) |
| 6357 | { |
| 6358 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6359 | int rcode; |
| 6360 | fflush_all(); |
| 6361 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6362 | fflush_all(); |
| 6363 | _exit(rcode); |
| 6364 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6365 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6366 | /* On NOMMU, we must never block! |
| 6367 | * Example: { sleep 99 | read line; } & echo Ok |
| 6368 | */ |
| 6369 | re_execute_shell(to_free, |
| 6370 | argv[0], |
| 6371 | G.global_argv[0], |
| 6372 | G.global_argv + 1, |
| 6373 | argv); |
| 6374 | #endif |
| 6375 | } |
| 6376 | |
| 6377 | |
| 6378 | static void execvp_or_die(char **argv) NORETURN; |
| 6379 | static void execvp_or_die(char **argv) |
| 6380 | { |
| 6381 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6382 | /* Don't propagate SIG_IGN to the child */ |
| 6383 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6384 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6385 | execvp(argv[0], argv); |
| 6386 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 6387 | _exit(127); /* bash compat */ |
| 6388 | } |
| 6389 | |
| 6390 | #if ENABLE_HUSH_MODE_X |
| 6391 | static void dump_cmd_in_x_mode(char **argv) |
| 6392 | { |
| 6393 | if (G_x_mode && argv) { |
| 6394 | /* We want to output the line in one write op */ |
| 6395 | char *buf, *p; |
| 6396 | int len; |
| 6397 | int n; |
| 6398 | |
| 6399 | len = 3; |
| 6400 | n = 0; |
| 6401 | while (argv[n]) |
| 6402 | len += strlen(argv[n++]) + 1; |
| 6403 | buf = xmalloc(len); |
| 6404 | buf[0] = '+'; |
| 6405 | p = buf + 1; |
| 6406 | n = 0; |
| 6407 | while (argv[n]) |
| 6408 | p += sprintf(p, " %s", argv[n++]); |
| 6409 | *p++ = '\n'; |
| 6410 | *p = '\0'; |
| 6411 | fputs(buf, stderr); |
| 6412 | free(buf); |
| 6413 | } |
| 6414 | } |
| 6415 | #else |
| 6416 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6417 | #endif |
| 6418 | |
| 6419 | #if BB_MMU |
| 6420 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6421 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6422 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6423 | pseudo_exec(command, argv_expanded) |
| 6424 | #endif |
| 6425 | |
| 6426 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6427 | * Never returns. |
| 6428 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6429 | * The at_exit handlers apparently confuse the calling process, |
| 6430 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
| 6431 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6432 | char **argv, int assignment_cnt, |
| 6433 | char **argv_expanded) NORETURN; |
| 6434 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6435 | char **argv, int assignment_cnt, |
| 6436 | char **argv_expanded) |
| 6437 | { |
| 6438 | char **new_env; |
| 6439 | |
| 6440 | new_env = expand_assignments(argv, assignment_cnt); |
| 6441 | dump_cmd_in_x_mode(new_env); |
| 6442 | |
| 6443 | if (!argv[assignment_cnt]) { |
| 6444 | /* Case when we are here: ... | var=val | ... |
| 6445 | * (note that we do not exit early, i.e., do not optimize out |
| 6446 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6447 | */ |
| 6448 | free_strings(new_env); |
| 6449 | _exit(EXIT_SUCCESS); |
| 6450 | } |
| 6451 | |
| 6452 | #if BB_MMU |
| 6453 | set_vars_and_save_old(new_env); |
| 6454 | free(new_env); /* optional */ |
| 6455 | /* we can also destroy set_vars_and_save_old's return value, |
| 6456 | * to save memory */ |
| 6457 | #else |
| 6458 | nommu_save->new_env = new_env; |
| 6459 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6460 | #endif |
| 6461 | |
| 6462 | if (argv_expanded) { |
| 6463 | argv = argv_expanded; |
| 6464 | } else { |
| 6465 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6466 | #if !BB_MMU |
| 6467 | nommu_save->argv = argv; |
| 6468 | #endif |
| 6469 | } |
| 6470 | dump_cmd_in_x_mode(argv); |
| 6471 | |
| 6472 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6473 | if (strchr(argv[0], '/') != NULL) |
| 6474 | goto skip; |
| 6475 | #endif |
| 6476 | |
| 6477 | /* Check if the command matches any of the builtins. |
| 6478 | * Depending on context, this might be redundant. But it's |
| 6479 | * easier to waste a few CPU cycles than it is to figure out |
| 6480 | * if this is one of those cases. |
| 6481 | */ |
| 6482 | { |
| 6483 | /* On NOMMU, it is more expensive to re-execute shell |
| 6484 | * just in order to run echo or test builtin. |
| 6485 | * It's better to skip it here and run corresponding |
| 6486 | * non-builtin later. */ |
| 6487 | const struct built_in_command *x; |
| 6488 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6489 | if (x) { |
| 6490 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6491 | } |
| 6492 | } |
| 6493 | #if ENABLE_HUSH_FUNCTIONS |
| 6494 | /* Check if the command matches any functions */ |
| 6495 | { |
| 6496 | const struct function *funcp = find_function(argv[0]); |
| 6497 | if (funcp) { |
| 6498 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6499 | } |
| 6500 | } |
| 6501 | #endif |
| 6502 | |
| 6503 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6504 | /* Check if the command matches any busybox applets */ |
| 6505 | { |
| 6506 | int a = find_applet_by_name(argv[0]); |
| 6507 | if (a >= 0) { |
| 6508 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6509 | if (APPLET_IS_NOEXEC(a)) { |
| 6510 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6511 | run_applet_no_and_exit(a, argv); |
| 6512 | } |
| 6513 | # endif |
| 6514 | /* Re-exec ourselves */ |
| 6515 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6516 | /* Don't propagate SIG_IGN to the child */ |
| 6517 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6518 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6519 | execv(bb_busybox_exec_path, argv); |
| 6520 | /* If they called chroot or otherwise made the binary no longer |
| 6521 | * executable, fall through */ |
| 6522 | } |
| 6523 | } |
| 6524 | #endif |
| 6525 | |
| 6526 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6527 | skip: |
| 6528 | #endif |
| 6529 | execvp_or_die(argv); |
| 6530 | } |
| 6531 | |
| 6532 | /* Called after [v]fork() in run_pipe |
| 6533 | */ |
| 6534 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6535 | struct command *command, |
| 6536 | char **argv_expanded) NORETURN; |
| 6537 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6538 | struct command *command, |
| 6539 | char **argv_expanded) |
| 6540 | { |
| 6541 | if (command->argv) { |
| 6542 | pseudo_exec_argv(nommu_save, command->argv, |
| 6543 | command->assignment_cnt, argv_expanded); |
| 6544 | } |
| 6545 | |
| 6546 | if (command->group) { |
| 6547 | /* Cases when we are here: |
| 6548 | * ( list ) |
| 6549 | * { list } & |
| 6550 | * ... | ( list ) | ... |
| 6551 | * ... | { list } | ... |
| 6552 | */ |
| 6553 | #if BB_MMU |
| 6554 | int rcode; |
| 6555 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6556 | reset_traps_to_defaults(); |
| 6557 | rcode = run_list(command->group); |
| 6558 | /* OK to leak memory by not calling free_pipe_list, |
| 6559 | * since this process is about to exit */ |
| 6560 | _exit(rcode); |
| 6561 | #else |
| 6562 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6563 | command->group_as_string, |
| 6564 | G.global_argv[0], |
| 6565 | G.global_argv + 1, |
| 6566 | NULL); |
| 6567 | #endif |
| 6568 | } |
| 6569 | |
| 6570 | /* Case when we are here: ... | >file */ |
| 6571 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6572 | _exit(EXIT_SUCCESS); |
| 6573 | } |
| 6574 | |
| 6575 | #if ENABLE_HUSH_JOB |
| 6576 | static const char *get_cmdtext(struct pipe *pi) |
| 6577 | { |
| 6578 | char **argv; |
| 6579 | char *p; |
| 6580 | int len; |
| 6581 | |
| 6582 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6583 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6584 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6585 | if (pi->cmdtext) |
| 6586 | return pi->cmdtext; |
| 6587 | argv = pi->cmds[0].argv; |
| 6588 | if (!argv || !argv[0]) { |
| 6589 | pi->cmdtext = xzalloc(1); |
| 6590 | return pi->cmdtext; |
| 6591 | } |
| 6592 | |
| 6593 | len = 0; |
| 6594 | do { |
| 6595 | len += strlen(*argv) + 1; |
| 6596 | } while (*++argv); |
| 6597 | p = xmalloc(len); |
| 6598 | pi->cmdtext = p; |
| 6599 | argv = pi->cmds[0].argv; |
| 6600 | do { |
| 6601 | len = strlen(*argv); |
| 6602 | memcpy(p, *argv, len); |
| 6603 | p += len; |
| 6604 | *p++ = ' '; |
| 6605 | } while (*++argv); |
| 6606 | p[-1] = '\0'; |
| 6607 | return pi->cmdtext; |
| 6608 | } |
| 6609 | |
| 6610 | static void insert_bg_job(struct pipe *pi) |
| 6611 | { |
| 6612 | struct pipe *job, **jobp; |
| 6613 | int i; |
| 6614 | |
| 6615 | /* Linear search for the ID of the job to use */ |
| 6616 | pi->jobid = 1; |
| 6617 | for (job = G.job_list; job; job = job->next) |
| 6618 | if (job->jobid >= pi->jobid) |
| 6619 | pi->jobid = job->jobid + 1; |
| 6620 | |
| 6621 | /* Add job to the list of running jobs */ |
| 6622 | jobp = &G.job_list; |
| 6623 | while ((job = *jobp) != NULL) |
| 6624 | jobp = &job->next; |
| 6625 | job = *jobp = xmalloc(sizeof(*job)); |
| 6626 | |
| 6627 | *job = *pi; /* physical copy */ |
| 6628 | job->next = NULL; |
| 6629 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 6630 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 6631 | for (i = 0; i < pi->num_cmds; i++) { |
| 6632 | job->cmds[i].pid = pi->cmds[i].pid; |
| 6633 | /* all other fields are not used and stay zero */ |
| 6634 | } |
| 6635 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 6636 | |
| 6637 | if (G_interactive_fd) |
| 6638 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 6639 | G.last_jobid = job->jobid; |
| 6640 | } |
| 6641 | |
| 6642 | static void remove_bg_job(struct pipe *pi) |
| 6643 | { |
| 6644 | struct pipe *prev_pipe; |
| 6645 | |
| 6646 | if (pi == G.job_list) { |
| 6647 | G.job_list = pi->next; |
| 6648 | } else { |
| 6649 | prev_pipe = G.job_list; |
| 6650 | while (prev_pipe->next != pi) |
| 6651 | prev_pipe = prev_pipe->next; |
| 6652 | prev_pipe->next = pi->next; |
| 6653 | } |
| 6654 | if (G.job_list) |
| 6655 | G.last_jobid = G.job_list->jobid; |
| 6656 | else |
| 6657 | G.last_jobid = 0; |
| 6658 | } |
| 6659 | |
| 6660 | /* Remove a backgrounded job */ |
| 6661 | static void delete_finished_bg_job(struct pipe *pi) |
| 6662 | { |
| 6663 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6664 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6665 | } |
| 6666 | #endif /* JOB */ |
| 6667 | |
| 6668 | /* Check to see if any processes have exited -- if they |
| 6669 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 6670 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6671 | { |
| 6672 | int attributes; |
| 6673 | int status; |
| 6674 | #if ENABLE_HUSH_JOB |
| 6675 | struct pipe *pi; |
| 6676 | #endif |
| 6677 | pid_t childpid; |
| 6678 | int rcode = 0; |
| 6679 | |
| 6680 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 6681 | |
| 6682 | attributes = WUNTRACED; |
| 6683 | if (fg_pipe == NULL) |
| 6684 | attributes |= WNOHANG; |
| 6685 | |
| 6686 | errno = 0; |
| 6687 | #if ENABLE_HUSH_FAST |
| 6688 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 6689 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 6690 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 6691 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 6692 | /* Avoid doing waitpid syscall if possible */ |
| 6693 | if (!G.we_have_children) { |
| 6694 | errno = ECHILD; |
| 6695 | return -1; |
| 6696 | } |
| 6697 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 6698 | /* We have children, but they did not exit |
| 6699 | * or stop yet (we saw no SIGCHLD) */ |
| 6700 | return 0; |
| 6701 | } |
| 6702 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 6703 | } |
| 6704 | #endif |
| 6705 | |
| 6706 | /* Do we do this right? |
| 6707 | * bash-3.00# sleep 20 | false |
| 6708 | * <ctrl-Z pressed> |
| 6709 | * [3]+ Stopped sleep 20 | false |
| 6710 | * bash-3.00# echo $? |
| 6711 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 6712 | * [hush 1.14.0: yes we do it right] |
| 6713 | */ |
| 6714 | wait_more: |
| 6715 | while (1) { |
| 6716 | int i; |
| 6717 | int dead; |
| 6718 | |
| 6719 | #if ENABLE_HUSH_FAST |
| 6720 | i = G.count_SIGCHLD; |
| 6721 | #endif |
| 6722 | childpid = waitpid(-1, &status, attributes); |
| 6723 | if (childpid <= 0) { |
| 6724 | if (childpid && errno != ECHILD) |
| 6725 | bb_perror_msg("waitpid"); |
| 6726 | #if ENABLE_HUSH_FAST |
| 6727 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 6728 | G.we_have_children = (childpid == 0); |
| 6729 | G.handled_SIGCHLD = i; |
| 6730 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6731 | } |
| 6732 | #endif |
| 6733 | break; |
| 6734 | } |
| 6735 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 6736 | |
| 6737 | #if DEBUG_JOBS |
| 6738 | if (WIFSTOPPED(status)) |
| 6739 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 6740 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 6741 | if (WIFSIGNALED(status)) |
| 6742 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 6743 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 6744 | if (WIFEXITED(status)) |
| 6745 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 6746 | childpid, WEXITSTATUS(status)); |
| 6747 | #endif |
| 6748 | /* Were we asked to wait for fg pipe? */ |
| 6749 | if (fg_pipe) { |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6750 | i = fg_pipe->num_cmds; |
| 6751 | while (--i >= 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6752 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 6753 | if (fg_pipe->cmds[i].pid != childpid) |
| 6754 | continue; |
| 6755 | if (dead) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6756 | int ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6757 | fg_pipe->cmds[i].pid = 0; |
| 6758 | fg_pipe->alive_cmds--; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6759 | ex = WEXITSTATUS(status); |
| 6760 | /* bash prints killer signal's name for *last* |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 6761 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6762 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 6763 | */ |
| 6764 | if (WIFSIGNALED(status)) { |
| 6765 | int sig = WTERMSIG(status); |
| 6766 | if (i == fg_pipe->num_cmds-1) |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 6767 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 6768 | printf("%s\n", sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 6769 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6770 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 6771 | * Maybe we need to use sig | 128? */ |
| 6772 | ex = sig + 128; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6773 | } |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6774 | fg_pipe->cmds[i].cmd_exitcode = ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6775 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6776 | fg_pipe->stopped_cmds++; |
| 6777 | } |
| 6778 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 6779 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6780 | if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6781 | /* All processes in fg pipe have exited or stopped */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6782 | i = fg_pipe->num_cmds; |
| 6783 | while (--i >= 0) { |
| 6784 | rcode = fg_pipe->cmds[i].cmd_exitcode; |
| 6785 | /* usually last process gives overall exitstatus, |
| 6786 | * but with "set -o pipefail", last *failed* process does */ |
| 6787 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 6788 | break; |
| 6789 | } |
| 6790 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6791 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 6792 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 6793 | * and "killall -STOP cat" */ |
| 6794 | if (G_interactive_fd) { |
| 6795 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6796 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6797 | insert_bg_job(fg_pipe); |
| 6798 | #endif |
| 6799 | return rcode; |
| 6800 | } |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6801 | if (fg_pipe->alive_cmds == 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6802 | return rcode; |
| 6803 | } |
| 6804 | /* There are still running processes in the fg pipe */ |
| 6805 | goto wait_more; /* do waitpid again */ |
| 6806 | } |
| 6807 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 6808 | } |
| 6809 | |
| 6810 | #if ENABLE_HUSH_JOB |
| 6811 | /* We asked to wait for bg or orphaned children */ |
| 6812 | /* No need to remember exitcode in this case */ |
| 6813 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6814 | for (i = 0; i < pi->num_cmds; i++) { |
| 6815 | if (pi->cmds[i].pid == childpid) |
| 6816 | goto found_pi_and_prognum; |
| 6817 | } |
| 6818 | } |
| 6819 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 6820 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 6821 | continue; /* do waitpid again */ |
| 6822 | |
| 6823 | found_pi_and_prognum: |
| 6824 | if (dead) { |
| 6825 | /* child exited */ |
| 6826 | pi->cmds[i].pid = 0; |
| 6827 | pi->alive_cmds--; |
| 6828 | if (!pi->alive_cmds) { |
| 6829 | if (G_interactive_fd) |
| 6830 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 6831 | "Done", pi->cmdtext); |
| 6832 | delete_finished_bg_job(pi); |
| 6833 | } |
| 6834 | } else { |
| 6835 | /* child stopped */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6836 | pi->stopped_cmds++; |
| 6837 | } |
| 6838 | #endif |
| 6839 | } /* while (waitpid succeeds)... */ |
| 6840 | |
| 6841 | return rcode; |
| 6842 | } |
| 6843 | |
| 6844 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6845 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6846 | { |
| 6847 | pid_t p; |
| 6848 | int rcode = checkjobs(fg_pipe); |
| 6849 | if (G_saved_tty_pgrp) { |
| 6850 | /* Job finished, move the shell to the foreground */ |
| 6851 | p = getpgrp(); /* our process group id */ |
| 6852 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 6853 | tcsetpgrp(G_interactive_fd, p); |
| 6854 | } |
| 6855 | return rcode; |
| 6856 | } |
| 6857 | #endif |
| 6858 | |
| 6859 | /* Start all the jobs, but don't wait for anything to finish. |
| 6860 | * See checkjobs(). |
| 6861 | * |
| 6862 | * Return code is normally -1, when the caller has to wait for children |
| 6863 | * to finish to determine the exit status of the pipe. If the pipe |
| 6864 | * is a simple builtin command, however, the action is done by the |
| 6865 | * time run_pipe returns, and the exit code is provided as the |
| 6866 | * return value. |
| 6867 | * |
| 6868 | * Returns -1 only if started some children. IOW: we have to |
| 6869 | * mask out retvals of builtins etc with 0xff! |
| 6870 | * |
| 6871 | * The only case when we do not need to [v]fork is when the pipe |
| 6872 | * is single, non-backgrounded, non-subshell command. Examples: |
| 6873 | * cmd ; ... { list } ; ... |
| 6874 | * cmd && ... { list } && ... |
| 6875 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 6876 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6877 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 6878 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 6879 | * |
| 6880 | * Cases when we must fork: |
| 6881 | * non-single: cmd | cmd |
| 6882 | * backgrounded: cmd & { list } & |
| 6883 | * subshell: ( list ) [&] |
| 6884 | */ |
| 6885 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 6886 | #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] | 6887 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 6888 | #endif |
| 6889 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 6890 | struct variable **old_vars_p, |
| 6891 | struct command *command, |
| 6892 | int squirrel[3], |
| 6893 | char **argv_expanded) |
| 6894 | { |
| 6895 | /* setup_redirects acts on file descriptors, not FILEs. |
| 6896 | * This is perfect for work that comes after exec(). |
| 6897 | * Is it really safe for inline use? Experimentally, |
| 6898 | * things seem to work. */ |
| 6899 | int rcode = setup_redirects(command, squirrel); |
| 6900 | if (rcode == 0) { |
| 6901 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 6902 | *new_env_p = new_env; |
| 6903 | dump_cmd_in_x_mode(new_env); |
| 6904 | dump_cmd_in_x_mode(argv_expanded); |
| 6905 | if (old_vars_p) |
| 6906 | *old_vars_p = set_vars_and_save_old(new_env); |
| 6907 | } |
| 6908 | return rcode; |
| 6909 | } |
| 6910 | static NOINLINE int run_pipe(struct pipe *pi) |
| 6911 | { |
| 6912 | static const char *const null_ptr = NULL; |
| 6913 | |
| 6914 | int cmd_no; |
| 6915 | int next_infd; |
| 6916 | struct command *command; |
| 6917 | char **argv_expanded; |
| 6918 | char **argv; |
| 6919 | /* it is not always needed, but we aim to smaller code */ |
| 6920 | int squirrel[] = { -1, -1, -1 }; |
| 6921 | int rcode; |
| 6922 | |
| 6923 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 6924 | debug_enter(); |
| 6925 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6926 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 6927 | * Result should be 3 lines: q w e, qwe, q w e |
| 6928 | */ |
| 6929 | G.ifs = get_local_var_value("IFS"); |
| 6930 | if (!G.ifs) |
| 6931 | G.ifs = defifs; |
| 6932 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6933 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 6934 | pi->stopped_cmds = 0; |
| 6935 | command = &pi->cmds[0]; |
| 6936 | argv_expanded = NULL; |
| 6937 | |
| 6938 | if (pi->num_cmds != 1 |
| 6939 | || pi->followup == PIPE_BG |
| 6940 | || command->cmd_type == CMD_SUBSHELL |
| 6941 | ) { |
| 6942 | goto must_fork; |
| 6943 | } |
| 6944 | |
| 6945 | pi->alive_cmds = 1; |
| 6946 | |
| 6947 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 6948 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 6949 | |
| 6950 | if (command->group) { |
| 6951 | #if ENABLE_HUSH_FUNCTIONS |
| 6952 | if (command->cmd_type == CMD_FUNCDEF) { |
| 6953 | /* "executing" func () { list } */ |
| 6954 | struct function *funcp; |
| 6955 | |
| 6956 | funcp = new_function(command->argv[0]); |
| 6957 | /* funcp->name is already set to argv[0] */ |
| 6958 | funcp->body = command->group; |
| 6959 | # if !BB_MMU |
| 6960 | funcp->body_as_string = command->group_as_string; |
| 6961 | command->group_as_string = NULL; |
| 6962 | # endif |
| 6963 | command->group = NULL; |
| 6964 | command->argv[0] = NULL; |
| 6965 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 6966 | funcp->parent_cmd = command; |
| 6967 | command->child_func = funcp; |
| 6968 | |
| 6969 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 6970 | debug_leave(); |
| 6971 | return EXIT_SUCCESS; |
| 6972 | } |
| 6973 | #endif |
| 6974 | /* { list } */ |
| 6975 | debug_printf("non-subshell group\n"); |
| 6976 | rcode = 1; /* exitcode if redir failed */ |
| 6977 | if (setup_redirects(command, squirrel) == 0) { |
| 6978 | debug_printf_exec(": run_list\n"); |
| 6979 | rcode = run_list(command->group) & 0xff; |
| 6980 | } |
| 6981 | restore_redirects(squirrel); |
| 6982 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6983 | debug_leave(); |
| 6984 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6985 | return rcode; |
| 6986 | } |
| 6987 | |
| 6988 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 6989 | { |
| 6990 | const struct built_in_command *x; |
| 6991 | #if ENABLE_HUSH_FUNCTIONS |
| 6992 | const struct function *funcp; |
| 6993 | #else |
| 6994 | enum { funcp = 0 }; |
| 6995 | #endif |
| 6996 | char **new_env = NULL; |
| 6997 | struct variable *old_vars = NULL; |
| 6998 | |
| 6999 | if (argv[command->assignment_cnt] == NULL) { |
| 7000 | /* Assignments, but no command */ |
| 7001 | /* Ensure redirects take effect (that is, create files). |
| 7002 | * Try "a=t >file" */ |
| 7003 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 7004 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 7005 | /* Set shell variables */ |
| 7006 | if (new_env) { |
| 7007 | argv = new_env; |
| 7008 | while (*argv) { |
| 7009 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7010 | /* Do we need to flag set_local_var() errors? |
| 7011 | * "assignment to readonly var" and "putenv error" |
| 7012 | */ |
| 7013 | argv++; |
| 7014 | } |
| 7015 | } |
| 7016 | /* Redirect error sets $? to 1. Otherwise, |
| 7017 | * if evaluating assignment value set $?, retain it. |
| 7018 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7019 | if (rcode == 0) |
| 7020 | rcode = G.last_exitcode; |
| 7021 | /* Exit, _skipping_ variable restoring code: */ |
| 7022 | goto clean_up_and_ret0; |
| 7023 | |
| 7024 | #else /* Older, bigger, but more correct code */ |
| 7025 | |
| 7026 | rcode = setup_redirects(command, squirrel); |
| 7027 | restore_redirects(squirrel); |
| 7028 | /* Set shell variables */ |
| 7029 | if (G_x_mode) |
| 7030 | bb_putchar_stderr('+'); |
| 7031 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7032 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7033 | if (G_x_mode) |
| 7034 | fprintf(stderr, " %s", p); |
| 7035 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 7036 | *argv, p); |
| 7037 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7038 | /* Do we need to flag set_local_var() errors? |
| 7039 | * "assignment to readonly var" and "putenv error" |
| 7040 | */ |
| 7041 | argv++; |
| 7042 | } |
| 7043 | if (G_x_mode) |
| 7044 | bb_putchar_stderr('\n'); |
| 7045 | /* Redirect error sets $? to 1. Otherwise, |
| 7046 | * if evaluating assignment value set $?, retain it. |
| 7047 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7048 | if (rcode == 0) |
| 7049 | rcode = G.last_exitcode; |
| 7050 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7051 | debug_leave(); |
| 7052 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7053 | return rcode; |
| 7054 | #endif |
| 7055 | } |
| 7056 | |
| 7057 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7058 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7059 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7060 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7061 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7062 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7063 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7064 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 7065 | } |
| 7066 | |
| 7067 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 7068 | if (!argv_expanded[0]) { |
| 7069 | free(argv_expanded); |
| 7070 | debug_leave(); |
| 7071 | return G.last_exitcode; |
| 7072 | } |
| 7073 | |
| 7074 | x = find_builtin(argv_expanded[0]); |
| 7075 | #if ENABLE_HUSH_FUNCTIONS |
| 7076 | funcp = NULL; |
| 7077 | if (!x) |
| 7078 | funcp = find_function(argv_expanded[0]); |
| 7079 | #endif |
| 7080 | if (x || funcp) { |
| 7081 | if (!funcp) { |
| 7082 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 7083 | debug_printf("exec with redirects only\n"); |
| 7084 | rcode = setup_redirects(command, NULL); |
| 7085 | goto clean_up_and_ret1; |
| 7086 | } |
| 7087 | } |
| 7088 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7089 | if (rcode == 0) { |
| 7090 | if (!funcp) { |
| 7091 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 7092 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7093 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7094 | rcode = x->b_function(argv_expanded) & 0xff; |
| 7095 | fflush_all(); |
| 7096 | } |
| 7097 | #if ENABLE_HUSH_FUNCTIONS |
| 7098 | else { |
| 7099 | # if ENABLE_HUSH_LOCAL |
| 7100 | struct variable **sv; |
| 7101 | sv = G.shadowed_vars_pp; |
| 7102 | G.shadowed_vars_pp = &old_vars; |
| 7103 | # endif |
| 7104 | debug_printf_exec(": function '%s' '%s'...\n", |
| 7105 | funcp->name, argv_expanded[1]); |
| 7106 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 7107 | # if ENABLE_HUSH_LOCAL |
| 7108 | G.shadowed_vars_pp = sv; |
| 7109 | # endif |
| 7110 | } |
| 7111 | #endif |
| 7112 | } |
| 7113 | clean_up_and_ret: |
| 7114 | unset_vars(new_env); |
| 7115 | add_vars(old_vars); |
| 7116 | /* clean_up_and_ret0: */ |
| 7117 | restore_redirects(squirrel); |
| 7118 | clean_up_and_ret1: |
| 7119 | free(argv_expanded); |
| 7120 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7121 | debug_leave(); |
| 7122 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 7123 | return rcode; |
| 7124 | } |
| 7125 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7126 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7127 | int n = find_applet_by_name(argv_expanded[0]); |
| 7128 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 7129 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7130 | if (rcode == 0) { |
| 7131 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 7132 | argv_expanded[0], argv_expanded[1]); |
| 7133 | rcode = run_nofork_applet(n, argv_expanded); |
| 7134 | } |
| 7135 | goto clean_up_and_ret; |
| 7136 | } |
| 7137 | } |
| 7138 | /* It is neither builtin nor applet. We must fork. */ |
| 7139 | } |
| 7140 | |
| 7141 | must_fork: |
| 7142 | /* NB: argv_expanded may already be created, and that |
| 7143 | * might include `cmd` runs! Do not rerun it! We *must* |
| 7144 | * use argv_expanded if it's non-NULL */ |
| 7145 | |
| 7146 | /* Going to fork a child per each pipe member */ |
| 7147 | pi->alive_cmds = 0; |
| 7148 | next_infd = 0; |
| 7149 | |
| 7150 | cmd_no = 0; |
| 7151 | while (cmd_no < pi->num_cmds) { |
| 7152 | struct fd_pair pipefds; |
| 7153 | #if !BB_MMU |
| 7154 | volatile nommu_save_t nommu_save; |
| 7155 | nommu_save.new_env = NULL; |
| 7156 | nommu_save.old_vars = NULL; |
| 7157 | nommu_save.argv = NULL; |
| 7158 | nommu_save.argv_from_re_execing = NULL; |
| 7159 | #endif |
| 7160 | command = &pi->cmds[cmd_no]; |
| 7161 | cmd_no++; |
| 7162 | if (command->argv) { |
| 7163 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 7164 | command->argv[0], command->argv[1]); |
| 7165 | } else { |
| 7166 | debug_printf_exec(": pipe member with no argv\n"); |
| 7167 | } |
| 7168 | |
| 7169 | /* pipes are inserted between pairs of commands */ |
| 7170 | pipefds.rd = 0; |
| 7171 | pipefds.wr = 1; |
| 7172 | if (cmd_no < pi->num_cmds) |
| 7173 | xpiped_pair(pipefds); |
| 7174 | |
| 7175 | command->pid = BB_MMU ? fork() : vfork(); |
| 7176 | if (!command->pid) { /* child */ |
| 7177 | #if ENABLE_HUSH_JOB |
| 7178 | disable_restore_tty_pgrp_on_exit(); |
| 7179 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7180 | |
| 7181 | /* Every child adds itself to new process group |
| 7182 | * with pgid == pid_of_first_child_in_pipe */ |
| 7183 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7184 | pid_t pgrp; |
| 7185 | pgrp = pi->pgrp; |
| 7186 | if (pgrp < 0) /* true for 1st process only */ |
| 7187 | pgrp = getpid(); |
| 7188 | if (setpgid(0, pgrp) == 0 |
| 7189 | && pi->followup != PIPE_BG |
| 7190 | && G_saved_tty_pgrp /* we have ctty */ |
| 7191 | ) { |
| 7192 | /* We do it in *every* child, not just first, |
| 7193 | * to avoid races */ |
| 7194 | tcsetpgrp(G_interactive_fd, pgrp); |
| 7195 | } |
| 7196 | } |
| 7197 | #endif |
| 7198 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 7199 | /* 1st cmd in backgrounded pipe |
| 7200 | * should have its stdin /dev/null'ed */ |
| 7201 | close(0); |
| 7202 | if (open(bb_dev_null, O_RDONLY)) |
| 7203 | xopen("/", O_RDONLY); |
| 7204 | } else { |
| 7205 | xmove_fd(next_infd, 0); |
| 7206 | } |
| 7207 | xmove_fd(pipefds.wr, 1); |
| 7208 | if (pipefds.rd > 1) |
| 7209 | close(pipefds.rd); |
| 7210 | /* Like bash, explicit redirects override pipes, |
| 7211 | * and the pipe fd is available for dup'ing. */ |
| 7212 | if (setup_redirects(command, NULL)) |
| 7213 | _exit(1); |
| 7214 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7215 | /* Stores to nommu_save list of env vars putenv'ed |
| 7216 | * (NOMMU, on MMU we don't need that) */ |
| 7217 | /* cast away volatility... */ |
| 7218 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 7219 | /* pseudo_exec() does not return */ |
| 7220 | } |
| 7221 | |
| 7222 | /* parent or error */ |
| 7223 | #if ENABLE_HUSH_FAST |
| 7224 | G.count_SIGCHLD++; |
| 7225 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7226 | #endif |
| 7227 | enable_restore_tty_pgrp_on_exit(); |
| 7228 | #if !BB_MMU |
| 7229 | /* Clean up after vforked child */ |
| 7230 | free(nommu_save.argv); |
| 7231 | free(nommu_save.argv_from_re_execing); |
| 7232 | unset_vars(nommu_save.new_env); |
| 7233 | add_vars(nommu_save.old_vars); |
| 7234 | #endif |
| 7235 | free(argv_expanded); |
| 7236 | argv_expanded = NULL; |
| 7237 | if (command->pid < 0) { /* [v]fork failed */ |
| 7238 | /* Clearly indicate, was it fork or vfork */ |
| 7239 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 7240 | } else { |
| 7241 | pi->alive_cmds++; |
| 7242 | #if ENABLE_HUSH_JOB |
| 7243 | /* Second and next children need to know pid of first one */ |
| 7244 | if (pi->pgrp < 0) |
| 7245 | pi->pgrp = command->pid; |
| 7246 | #endif |
| 7247 | } |
| 7248 | |
| 7249 | if (cmd_no > 1) |
| 7250 | close(next_infd); |
| 7251 | if (cmd_no < pi->num_cmds) |
| 7252 | close(pipefds.wr); |
| 7253 | /* Pass read (output) pipe end to next iteration */ |
| 7254 | next_infd = pipefds.rd; |
| 7255 | } |
| 7256 | |
| 7257 | if (!pi->alive_cmds) { |
| 7258 | debug_leave(); |
| 7259 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7260 | return 1; |
| 7261 | } |
| 7262 | |
| 7263 | debug_leave(); |
| 7264 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7265 | return -1; |
| 7266 | } |
| 7267 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7268 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7269 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7270 | static int run_list(struct pipe *pi) |
| 7271 | { |
| 7272 | #if ENABLE_HUSH_CASE |
| 7273 | char *case_word = NULL; |
| 7274 | #endif |
| 7275 | #if ENABLE_HUSH_LOOPS |
| 7276 | struct pipe *loop_top = NULL; |
| 7277 | char **for_lcur = NULL; |
| 7278 | char **for_list = NULL; |
| 7279 | #endif |
| 7280 | smallint last_followup; |
| 7281 | smalluint rcode; |
| 7282 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7283 | smalluint cond_code = 0; |
| 7284 | #else |
| 7285 | enum { cond_code = 0 }; |
| 7286 | #endif |
| 7287 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7288 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7289 | smallint last_rword; /* ditto */ |
| 7290 | #endif |
| 7291 | |
| 7292 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7293 | debug_enter(); |
| 7294 | |
| 7295 | #if ENABLE_HUSH_LOOPS |
| 7296 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7297 | { |
| 7298 | struct pipe *cpipe; |
| 7299 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7300 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7301 | continue; |
| 7302 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7303 | if (cpipe->next == NULL) { |
| 7304 | syntax_error("malformed for"); |
| 7305 | debug_leave(); |
| 7306 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7307 | return 1; |
| 7308 | } |
| 7309 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7310 | if (cpipe->next->res_word == RES_DO) |
| 7311 | continue; |
| 7312 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7313 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7314 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7315 | ) { |
| 7316 | syntax_error("malformed for"); |
| 7317 | debug_leave(); |
| 7318 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7319 | return 1; |
| 7320 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7321 | } |
| 7322 | } |
| 7323 | #endif |
| 7324 | |
| 7325 | /* Past this point, all code paths should jump to ret: label |
| 7326 | * in order to return, no direct "return" statements please. |
| 7327 | * This helps to ensure that no memory is leaked. */ |
| 7328 | |
| 7329 | #if ENABLE_HUSH_JOB |
| 7330 | G.run_list_level++; |
| 7331 | #endif |
| 7332 | |
| 7333 | #if HAS_KEYWORDS |
| 7334 | rword = RES_NONE; |
| 7335 | last_rword = RES_XXXX; |
| 7336 | #endif |
| 7337 | last_followup = PIPE_SEQ; |
| 7338 | rcode = G.last_exitcode; |
| 7339 | |
| 7340 | /* Go through list of pipes, (maybe) executing them. */ |
| 7341 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 7342 | if (G.flag_SIGINT) |
| 7343 | break; |
| 7344 | |
| 7345 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7346 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7347 | rword, cond_code, last_rword); |
| 7348 | #if ENABLE_HUSH_LOOPS |
| 7349 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7350 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7351 | ) { |
| 7352 | /* start of a loop: remember where loop starts */ |
| 7353 | loop_top = pi; |
| 7354 | G.depth_of_loop++; |
| 7355 | } |
| 7356 | #endif |
| 7357 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7358 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7359 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7360 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7361 | ) { |
| 7362 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7363 | * and we should not execute CMD */ |
| 7364 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7365 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7366 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7367 | } |
| 7368 | } |
| 7369 | last_followup = pi->followup; |
| 7370 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7371 | #if ENABLE_HUSH_IF |
| 7372 | if (cond_code) { |
| 7373 | if (rword == RES_THEN) { |
| 7374 | /* if false; then ... fi has exitcode 0! */ |
| 7375 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7376 | /* "if <false> THEN cmd": skip cmd */ |
| 7377 | continue; |
| 7378 | } |
| 7379 | } else { |
| 7380 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7381 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7382 | * skip cmd and all following ones */ |
| 7383 | break; |
| 7384 | } |
| 7385 | } |
| 7386 | #endif |
| 7387 | #if ENABLE_HUSH_LOOPS |
| 7388 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7389 | if (!for_lcur) { |
| 7390 | /* first loop through for */ |
| 7391 | |
| 7392 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7393 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7394 | }; /* encoded representation of "$@" */ |
| 7395 | static const char *const encoded_dollar_at_argv[] = { |
| 7396 | encoded_dollar_at, NULL |
| 7397 | }; /* argv list with one element: "$@" */ |
| 7398 | char **vals; |
| 7399 | |
| 7400 | vals = (char**)encoded_dollar_at_argv; |
| 7401 | if (pi->next->res_word == RES_IN) { |
| 7402 | /* if no variable values after "in" we skip "for" */ |
| 7403 | if (!pi->next->cmds[0].argv) { |
| 7404 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7405 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7406 | break; |
| 7407 | } |
| 7408 | vals = pi->next->cmds[0].argv; |
| 7409 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7410 | /* create list of variable values */ |
| 7411 | debug_print_strings("for_list made from", vals); |
| 7412 | for_list = expand_strvec_to_strvec(vals); |
| 7413 | for_lcur = for_list; |
| 7414 | debug_print_strings("for_list", for_list); |
| 7415 | } |
| 7416 | if (!*for_lcur) { |
| 7417 | /* "for" loop is over, clean up */ |
| 7418 | free(for_list); |
| 7419 | for_list = NULL; |
| 7420 | for_lcur = NULL; |
| 7421 | break; |
| 7422 | } |
| 7423 | /* Insert next value from for_lcur */ |
| 7424 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7425 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7426 | continue; |
| 7427 | } |
| 7428 | if (rword == RES_IN) { |
| 7429 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7430 | } |
| 7431 | if (rword == RES_DONE) { |
| 7432 | continue; /* "done" has no cmds too */ |
| 7433 | } |
| 7434 | #endif |
| 7435 | #if ENABLE_HUSH_CASE |
| 7436 | if (rword == RES_CASE) { |
| 7437 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7438 | continue; |
| 7439 | } |
| 7440 | if (rword == RES_MATCH) { |
| 7441 | char **argv; |
| 7442 | |
| 7443 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7444 | break; |
| 7445 | /* all prev words didn't match, does this one match? */ |
| 7446 | argv = pi->cmds->argv; |
| 7447 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7448 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7449 | /* TODO: which FNM_xxx flags to use? */ |
| 7450 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7451 | free(pattern); |
| 7452 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7453 | free(case_word); /* make future "word)" stop */ |
| 7454 | case_word = NULL; |
| 7455 | break; |
| 7456 | } |
| 7457 | argv++; |
| 7458 | } |
| 7459 | continue; |
| 7460 | } |
| 7461 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7462 | if (cond_code != 0) |
| 7463 | continue; /* not matched yet, skip this pipe */ |
| 7464 | } |
| 7465 | #endif |
| 7466 | /* Just pressing <enter> in shell should check for jobs. |
| 7467 | * OTOH, in non-interactive shell this is useless |
| 7468 | * and only leads to extra job checks */ |
| 7469 | if (pi->num_cmds == 0) { |
| 7470 | if (G_interactive_fd) |
| 7471 | goto check_jobs_and_continue; |
| 7472 | continue; |
| 7473 | } |
| 7474 | |
| 7475 | /* After analyzing all keywords and conditions, we decided |
| 7476 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7477 | * after run_pipe to collect any background children, |
| 7478 | * even if list execution is to be stopped. */ |
| 7479 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7480 | { |
| 7481 | int r; |
| 7482 | #if ENABLE_HUSH_LOOPS |
| 7483 | G.flag_break_continue = 0; |
| 7484 | #endif |
| 7485 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7486 | if (r != -1) { |
| 7487 | /* We ran a builtin, function, or group. |
| 7488 | * rcode is already known |
| 7489 | * and we don't need to wait for anything. */ |
| 7490 | G.last_exitcode = rcode; |
| 7491 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7492 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7493 | #if ENABLE_HUSH_LOOPS |
| 7494 | /* Was it "break" or "continue"? */ |
| 7495 | if (G.flag_break_continue) { |
| 7496 | smallint fbc = G.flag_break_continue; |
| 7497 | /* We might fall into outer *loop*, |
| 7498 | * don't want to break it too */ |
| 7499 | if (loop_top) { |
| 7500 | G.depth_break_continue--; |
| 7501 | if (G.depth_break_continue == 0) |
| 7502 | G.flag_break_continue = 0; |
| 7503 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7504 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7505 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
| 7506 | checkjobs(NULL); |
| 7507 | break; |
| 7508 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7509 | /* "continue": simulate end of loop */ |
| 7510 | rword = RES_DONE; |
| 7511 | continue; |
| 7512 | } |
| 7513 | #endif |
| 7514 | #if ENABLE_HUSH_FUNCTIONS |
| 7515 | if (G.flag_return_in_progress == 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7516 | checkjobs(NULL); |
| 7517 | break; |
| 7518 | } |
| 7519 | #endif |
| 7520 | } else if (pi->followup == PIPE_BG) { |
| 7521 | /* What does bash do with attempts to background builtins? */ |
| 7522 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7523 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7524 | * I'm NOT treating inner &'s as jobs */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7525 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7526 | #if ENABLE_HUSH_JOB |
| 7527 | if (G.run_list_level == 1) |
| 7528 | insert_bg_job(pi); |
| 7529 | #endif |
| 7530 | /* Last command's pid goes to $! */ |
| 7531 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7532 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7533 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7534 | } else { |
| 7535 | #if ENABLE_HUSH_JOB |
| 7536 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7537 | /* Waits for completion, then fg's main shell */ |
| 7538 | rcode = checkjobs_and_fg_shell(pi); |
| 7539 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7540 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7541 | } else |
| 7542 | #endif |
| 7543 | { /* This one just waits for completion */ |
| 7544 | rcode = checkjobs(pi); |
| 7545 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7546 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7547 | } |
| 7548 | G.last_exitcode = rcode; |
| 7549 | } |
| 7550 | } |
| 7551 | |
| 7552 | /* Analyze how result affects subsequent commands */ |
| 7553 | #if ENABLE_HUSH_IF |
| 7554 | if (rword == RES_IF || rword == RES_ELIF) |
| 7555 | cond_code = rcode; |
| 7556 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7557 | check_jobs_and_continue: |
| 7558 | checkjobs(NULL); |
| 7559 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7560 | #if ENABLE_HUSH_LOOPS |
| 7561 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7562 | if (pi->next |
| 7563 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 7564 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7565 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7566 | if (rword == RES_WHILE) { |
| 7567 | if (rcode) { |
| 7568 | /* "while false; do...done" - exitcode 0 */ |
| 7569 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7570 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7571 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7572 | } |
| 7573 | } |
| 7574 | if (rword == RES_UNTIL) { |
| 7575 | if (!rcode) { |
| 7576 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7577 | break; |
| 7578 | } |
| 7579 | } |
| 7580 | } |
| 7581 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7582 | } /* for (pi) */ |
| 7583 | |
| 7584 | #if ENABLE_HUSH_JOB |
| 7585 | G.run_list_level--; |
| 7586 | #endif |
| 7587 | #if ENABLE_HUSH_LOOPS |
| 7588 | if (loop_top) |
| 7589 | G.depth_of_loop--; |
| 7590 | free(for_list); |
| 7591 | #endif |
| 7592 | #if ENABLE_HUSH_CASE |
| 7593 | free(case_word); |
| 7594 | #endif |
| 7595 | debug_leave(); |
| 7596 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7597 | return rcode; |
| 7598 | } |
| 7599 | |
| 7600 | /* Select which version we will use */ |
| 7601 | static int run_and_free_list(struct pipe *pi) |
| 7602 | { |
| 7603 | int rcode = 0; |
| 7604 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7605 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7606 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7607 | rcode = run_list(pi); |
| 7608 | } |
| 7609 | /* free_pipe_list has the side effect of clearing memory. |
| 7610 | * In the long run that function can be merged with run_list, |
| 7611 | * but doing that now would hobble the debugging effort. */ |
| 7612 | free_pipe_list(pi); |
| 7613 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 7614 | return rcode; |
| 7615 | } |
| 7616 | |
| 7617 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7618 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7619 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7620 | sighandler_t old_handler; |
| 7621 | unsigned sig = 0; |
| 7622 | while ((mask >>= 1) != 0) { |
| 7623 | sig++; |
| 7624 | if (!(mask & 1)) |
| 7625 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7626 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7627 | /* POSIX allows shell to re-enable SIGCHLD |
| 7628 | * even if it was SIG_IGN on entry. |
| 7629 | * Therefore we skip IGN check for it: |
| 7630 | */ |
| 7631 | if (sig == SIGCHLD) |
| 7632 | continue; |
| 7633 | if (old_handler == SIG_IGN) { |
| 7634 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7635 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7636 | if (!G.traps) |
| 7637 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7638 | free(G.traps[sig]); |
| 7639 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7640 | } |
| 7641 | } |
| 7642 | } |
| 7643 | |
| 7644 | /* Called a few times only (or even once if "sh -c") */ |
| 7645 | static void install_special_sighandlers(void) |
| 7646 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7647 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7648 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7649 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7650 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7651 | if (G_interactive_fd) { |
| 7652 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 7653 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7654 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7655 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7656 | /* Careful, do not re-install handlers we already installed */ |
| 7657 | if (G.special_sig_mask != mask) { |
| 7658 | unsigned diff = mask & ~G.special_sig_mask; |
| 7659 | G.special_sig_mask = mask; |
| 7660 | install_sighandlers(diff); |
| 7661 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7662 | } |
| 7663 | |
| 7664 | #if ENABLE_HUSH_JOB |
| 7665 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7666 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7667 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7668 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7669 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7670 | |
| 7671 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7672 | mask = 0 |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7673 | + (1 << SIGILL ) * HUSH_DEBUG |
| 7674 | + (1 << SIGFPE ) * HUSH_DEBUG |
| 7675 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 7676 | + (1 << SIGSEGV) * HUSH_DEBUG |
| 7677 | + (1 << SIGTRAP) * HUSH_DEBUG |
| 7678 | + (1 << SIGABRT) |
| 7679 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7680 | + (1 << SIGPIPE) |
| 7681 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7682 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7683 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7684 | * we never want to restore pgrp on exit, and this fn is not called |
| 7685 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7686 | /*+ (1 << SIGHUP )*/ |
| 7687 | /*+ (1 << SIGTERM)*/ |
| 7688 | /*+ (1 << SIGINT )*/ |
| 7689 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7690 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7691 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7692 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7693 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7694 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7695 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7696 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7697 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7698 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7699 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7700 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7701 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7702 | break; |
| 7703 | case 'x': |
| 7704 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 7705 | break; |
| 7706 | case 'o': |
| 7707 | if (!o_opt) { |
| 7708 | /* "set -+o" without parameter. |
| 7709 | * in bash, set -o produces this output: |
| 7710 | * pipefail off |
| 7711 | * and set +o: |
| 7712 | * set +o pipefail |
| 7713 | * We always use the second form. |
| 7714 | */ |
| 7715 | const char *p = o_opt_strings; |
| 7716 | idx = 0; |
| 7717 | while (*p) { |
| 7718 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 7719 | idx++; |
| 7720 | p += strlen(p) + 1; |
| 7721 | } |
| 7722 | break; |
| 7723 | } |
| 7724 | idx = index_in_strings(o_opt_strings, o_opt); |
| 7725 | if (idx >= 0) { |
| 7726 | G.o_opt[idx] = state; |
| 7727 | break; |
| 7728 | } |
| 7729 | default: |
| 7730 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7731 | } |
| 7732 | return EXIT_SUCCESS; |
| 7733 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7734 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7735 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7736 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7737 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7738 | enum { |
| 7739 | OPT_login = (1 << 0), |
| 7740 | }; |
| 7741 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7742 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7743 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7744 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7745 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7746 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7747 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7748 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7749 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7750 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7751 | #if ENABLE_HUSH_FAST |
| 7752 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 7753 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7754 | #if !BB_MMU |
| 7755 | G.argv0_for_re_execing = argv[0]; |
| 7756 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7757 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7758 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 7759 | shell_ver->flg_export = 1; |
| 7760 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 7761 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 7762 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7763 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7764 | /* Create shell local variables from the values |
| 7765 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7766 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7767 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7768 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7769 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7770 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7771 | if (e) while (*e) { |
| 7772 | char *value = strchr(*e, '='); |
| 7773 | if (value) { /* paranoia */ |
| 7774 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7775 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7776 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7777 | cur_var->max_len = strlen(*e); |
| 7778 | cur_var->flg_export = 1; |
| 7779 | } |
| 7780 | e++; |
| 7781 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7782 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7783 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 7784 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7785 | |
| 7786 | /* Export PWD */ |
| 7787 | set_pwd_var(/*exp:*/ 1); |
| 7788 | /* bash also exports SHLVL and _, |
| 7789 | * and sets (but doesn't export) the following variables: |
| 7790 | * BASH=/bin/bash |
| 7791 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7792 | * BASH_VERSION='3.2.0(1)-release' |
| 7793 | * HOSTTYPE=i386 |
| 7794 | * MACHTYPE=i386-pc-linux-gnu |
| 7795 | * OSTYPE=linux-gnu |
| 7796 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7797 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7798 | * EUID=<NNNNN> |
| 7799 | * UID=<NNNNN> |
| 7800 | * GROUPS=() |
| 7801 | * LINES=<NNN> |
| 7802 | * COLUMNS=<NNN> |
| 7803 | * BASH_ARGC=() |
| 7804 | * BASH_ARGV=() |
| 7805 | * BASH_LINENO=() |
| 7806 | * BASH_SOURCE=() |
| 7807 | * DIRSTACK=() |
| 7808 | * PIPESTATUS=([0]="0") |
| 7809 | * HISTFILE=/<xxx>/.bash_history |
| 7810 | * HISTFILESIZE=500 |
| 7811 | * HISTSIZE=500 |
| 7812 | * MAILCHECK=60 |
| 7813 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7814 | * SHELL=/bin/bash |
| 7815 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7816 | * TERM=dumb |
| 7817 | * OPTERR=1 |
| 7818 | * OPTIND=1 |
| 7819 | * IFS=$' \t\n' |
| 7820 | * PS1='\s-\v\$ ' |
| 7821 | * PS2='> ' |
| 7822 | * PS4='+ ' |
| 7823 | */ |
| 7824 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7825 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 7826 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7827 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7828 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7829 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7830 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7831 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7832 | if (setjmp(die_jmp)) { |
| 7833 | /* xfunc has failed! die die die */ |
| 7834 | /* no EXIT traps, this is an escape hatch! */ |
| 7835 | G.exiting = 1; |
| 7836 | hush_exit(xfunc_error_retval); |
| 7837 | } |
| 7838 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7839 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7840 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7841 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7842 | * If we later decide that we are interactive, we run install_special_sighandlers() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7843 | * in order to intercept (more) signals. |
| 7844 | */ |
| 7845 | |
| 7846 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7847 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7848 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7849 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7850 | while (1) { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7851 | opt = getopt(argc, argv, "+c:xinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7852 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7853 | "<:$:R:V:" |
| 7854 | # if ENABLE_HUSH_FUNCTIONS |
| 7855 | "F:" |
| 7856 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7857 | #endif |
| 7858 | ); |
| 7859 | if (opt <= 0) |
| 7860 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7861 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7862 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7863 | /* Possibilities: |
| 7864 | * sh ... -c 'script' |
| 7865 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7866 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7867 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7868 | * "" needs to be replaced with NULL |
| 7869 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7870 | * Note: the form without ARG0 never happens: |
| 7871 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7872 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7873 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7874 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7875 | G.root_ppid = getppid(); |
| 7876 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7877 | G.global_argv = argv + optind; |
| 7878 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7879 | if (builtin_argc) { |
| 7880 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7881 | const struct built_in_command *x; |
| 7882 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7883 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7884 | x = find_builtin(optarg); |
| 7885 | if (x) { /* paranoia */ |
| 7886 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7887 | G.global_argv += builtin_argc; |
| 7888 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7889 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7890 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7891 | } |
| 7892 | goto final_return; |
| 7893 | } |
| 7894 | if (!G.global_argv[0]) { |
| 7895 | /* -c 'script' (no params): prevent empty $0 */ |
| 7896 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7897 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7898 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7899 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7900 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7901 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7902 | goto final_return; |
| 7903 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7904 | /* Well, we cannot just declare interactiveness, |
| 7905 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7906 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7907 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7908 | case 's': |
| 7909 | /* "-s" means "read from stdin", but this is how we always |
| 7910 | * operate, so simply do nothing here. */ |
| 7911 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7912 | case 'l': |
| 7913 | flags |= OPT_login; |
| 7914 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7915 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7916 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7917 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7918 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7919 | case '$': { |
| 7920 | unsigned long long empty_trap_mask; |
| 7921 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7922 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7923 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7924 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7925 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7926 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7927 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7928 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7929 | optarg++; |
| 7930 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7931 | optarg++; |
| 7932 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7933 | if (empty_trap_mask != 0) { |
| 7934 | int sig; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7935 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7936 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7937 | for (sig = 1; sig < NSIG; sig++) { |
| 7938 | if (empty_trap_mask & (1LL << sig)) { |
| 7939 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7940 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7941 | } |
| 7942 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7943 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7944 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7945 | optarg++; |
| 7946 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7947 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7948 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7949 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7950 | case 'R': |
| 7951 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7952 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7953 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7954 | # if ENABLE_HUSH_FUNCTIONS |
| 7955 | case 'F': { |
| 7956 | struct function *funcp = new_function(optarg); |
| 7957 | /* funcp->name is already set to optarg */ |
| 7958 | /* funcp->body is set to NULL. It's a special case. */ |
| 7959 | funcp->body_as_string = argv[optind]; |
| 7960 | optind++; |
| 7961 | break; |
| 7962 | } |
| 7963 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7964 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7965 | case 'n': |
| 7966 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7967 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7968 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7969 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7970 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7971 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7972 | " or: sh -c command [args]...\n\n"); |
| 7973 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7974 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7975 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7976 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7977 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7978 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7979 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7980 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 7981 | G.global_argc = argc - (optind - 1); |
| 7982 | G.global_argv = argv + (optind - 1); |
| 7983 | G.global_argv[0] = argv[0]; |
| 7984 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7985 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7986 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7987 | G.root_ppid = getppid(); |
| 7988 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7989 | |
| 7990 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7991 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7992 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7993 | debug_printf("sourcing /etc/profile\n"); |
| 7994 | input = fopen_for_read("/etc/profile"); |
| 7995 | if (input != NULL) { |
| 7996 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7997 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7998 | parse_and_run_file(input); |
| 7999 | fclose(input); |
| 8000 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8001 | /* bash: after sourcing /etc/profile, |
| 8002 | * tries to source (in the given order): |
| 8003 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8004 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8005 | * bash also sources ~/.bash_logout on exit. |
| 8006 | * If called as sh, skips .bash_XXX files. |
| 8007 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8008 | } |
| 8009 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8010 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8011 | FILE *input; |
| 8012 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8013 | * "bash <script>" (which is never interactive (unless -i?)) |
| 8014 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8015 | * If called as sh, does the same but with $ENV. |
| 8016 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8017 | G.global_argc--; |
| 8018 | G.global_argv++; |
| 8019 | debug_printf("running script '%s'\n", G.global_argv[0]); |
| 8020 | input = xfopen_for_read(G.global_argv[0]); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8021 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8022 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8023 | parse_and_run_file(input); |
| 8024 | #if ENABLE_FEATURE_CLEAN_UP |
| 8025 | fclose(input); |
| 8026 | #endif |
| 8027 | goto final_return; |
| 8028 | } |
| 8029 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8030 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8031 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8032 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8033 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8034 | /* A shell is interactive if the '-i' flag was given, |
| 8035 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 8036 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8037 | * no arguments remaining or the -s flag given |
| 8038 | * standard input is a terminal |
| 8039 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8040 | * Refer to Posix.2, the description of the 'sh' utility. |
| 8041 | */ |
| 8042 | #if ENABLE_HUSH_JOB |
| 8043 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8044 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 8045 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 8046 | if (G_saved_tty_pgrp < 0) |
| 8047 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8048 | |
| 8049 | /* try to dup stdin to high fd#, >= 255 */ |
| 8050 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8051 | if (G_interactive_fd < 0) { |
| 8052 | /* try to dup to any fd */ |
| 8053 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8054 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8055 | /* give up */ |
| 8056 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8057 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 8058 | } |
| 8059 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8060 | // TODO: track & disallow any attempts of user |
| 8061 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8062 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8063 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8064 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8065 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8066 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8067 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8068 | /* If we were run as 'hush &', sleep until we are |
| 8069 | * in the foreground (tty pgrp == our pgrp). |
| 8070 | * If we get started under a job aware app (like bash), |
| 8071 | * make sure we are now in charge so we don't fight over |
| 8072 | * who gets the foreground */ |
| 8073 | while (1) { |
| 8074 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8075 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 8076 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8077 | break; |
| 8078 | /* send TTIN to ourself (should stop us) */ |
| 8079 | kill(- shell_pgrp, SIGTTIN); |
| 8080 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8081 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8082 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8083 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8084 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8085 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8086 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8087 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8088 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8089 | /* Put ourselves in our own process group |
| 8090 | * (bash, too, does this only if ctty is available) */ |
| 8091 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 8092 | /* Grab control of the terminal */ |
| 8093 | tcsetpgrp(G_interactive_fd, getpid()); |
| 8094 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 8095 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 8096 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 8097 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8098 | |
| 8099 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 8100 | { |
| 8101 | const char *hp = get_local_var_value("HISTFILE"); |
| 8102 | if (!hp) { |
| 8103 | hp = get_local_var_value("HOME"); |
| 8104 | if (hp) |
| 8105 | hp = concat_path_file(hp, ".hush_history"); |
| 8106 | } else { |
| 8107 | hp = xstrdup(hp); |
| 8108 | } |
| 8109 | if (hp) { |
| 8110 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8111 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 8112 | } |
| 8113 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 8114 | hp = get_local_var_value("HISTFILESIZE"); |
| 8115 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 8116 | # endif |
| 8117 | } |
| 8118 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8119 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8120 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8121 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8122 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8123 | /* No job control compiled in, only prompt/line editing */ |
| 8124 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8125 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8126 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8127 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8128 | G_interactive_fd = dup(STDIN_FILENO); |
| 8129 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8130 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8131 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8132 | } |
| 8133 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8134 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8135 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8136 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8137 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8138 | #else |
| 8139 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8140 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8141 | #endif |
| 8142 | /* bash: |
| 8143 | * if interactive but not a login shell, sources ~/.bashrc |
| 8144 | * (--norc turns this off, --rcfile <file> overrides) |
| 8145 | */ |
| 8146 | |
| 8147 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8148 | /* note: ash and hush share this string */ |
| 8149 | printf("\n\n%s %s\n" |
| 8150 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8151 | "\n", |
| 8152 | bb_banner, |
| 8153 | "hush - the humble shell" |
| 8154 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 8155 | } |
| 8156 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8157 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8158 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8159 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8160 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8161 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 8162 | |
| 8163 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8164 | #if ENABLE_MSH |
| 8165 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 8166 | int msh_main(int argc, char **argv) |
| 8167 | { |
| 8168 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 8169 | return hush_main(argc, argv); |
| 8170 | } |
| 8171 | #endif |
| 8172 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8173 | |
| 8174 | /* |
| 8175 | * Built-ins |
| 8176 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8177 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8178 | { |
| 8179 | return 0; |
| 8180 | } |
| 8181 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8182 | 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] | 8183 | { |
| 8184 | int argc = 0; |
| 8185 | while (*argv) { |
| 8186 | argc++; |
| 8187 | argv++; |
| 8188 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8189 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 8190 | } |
| 8191 | |
| 8192 | static int FAST_FUNC builtin_test(char **argv) |
| 8193 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8194 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8195 | } |
| 8196 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8197 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8198 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8199 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8200 | } |
| 8201 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8202 | #if ENABLE_PRINTF |
| 8203 | static int FAST_FUNC builtin_printf(char **argv) |
| 8204 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8205 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8206 | } |
| 8207 | #endif |
| 8208 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8209 | static char **skip_dash_dash(char **argv) |
| 8210 | { |
| 8211 | argv++; |
| 8212 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 8213 | argv++; |
| 8214 | return argv; |
| 8215 | } |
| 8216 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8217 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8218 | { |
| 8219 | int rcode = EXIT_SUCCESS; |
| 8220 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8221 | argv = skip_dash_dash(argv); |
| 8222 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8223 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8224 | /* bash: |
| 8225 | * eval "echo Hi; done" ("done" is syntax error): |
| 8226 | * "echo Hi" will not execute too. |
| 8227 | */ |
| 8228 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8229 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8230 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8231 | } |
| 8232 | return rcode; |
| 8233 | } |
| 8234 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8235 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8236 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8237 | const char *newdir; |
| 8238 | |
| 8239 | argv = skip_dash_dash(argv); |
| 8240 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8241 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8242 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8243 | * bash says "bash: cd: HOME not set" and does nothing |
| 8244 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8245 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 8246 | const char *home = get_local_var_value("HOME"); |
| 8247 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8248 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8249 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8250 | /* Mimic bash message exactly */ |
| 8251 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8252 | return EXIT_FAILURE; |
| 8253 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8254 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 8255 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 8256 | * set it again, but do not export. bash does the same. |
| 8257 | */ |
| 8258 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8259 | return EXIT_SUCCESS; |
| 8260 | } |
| 8261 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8262 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8263 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8264 | argv = skip_dash_dash(argv); |
| 8265 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8266 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8267 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8268 | /* Careful: we can end up here after [v]fork. Do not restore |
| 8269 | * tty pgrp then, only top-level shell process does that */ |
| 8270 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 8271 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 8272 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8273 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8274 | * We'll need to undo trap cleanup (it's inside execvp_or_die) |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8275 | * and tcsetpgrp, and this is inherently racy. |
| 8276 | */ |
| 8277 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8278 | } |
| 8279 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8280 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8281 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8282 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8283 | |
| 8284 | /* interactive bash: |
| 8285 | * # trap "echo EEE" EXIT |
| 8286 | * # exit |
| 8287 | * exit |
| 8288 | * There are stopped jobs. |
| 8289 | * (if there are _stopped_ jobs, running ones don't count) |
| 8290 | * # exit |
| 8291 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 8292 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8293 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8294 | * 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] | 8295 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8296 | |
| 8297 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8298 | argv = skip_dash_dash(argv); |
| 8299 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8300 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8301 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8302 | xfunc_error_retval = 255; |
| 8303 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8304 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8305 | } |
| 8306 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8307 | static void print_escaped(const char *s) |
| 8308 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8309 | if (*s == '\'') |
| 8310 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8311 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8312 | const char *p = strchrnul(s, '\''); |
| 8313 | /* print 'xxxx', possibly just '' */ |
| 8314 | printf("'%.*s'", (int)(p - s), s); |
| 8315 | if (*p == '\0') |
| 8316 | break; |
| 8317 | s = p; |
| 8318 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8319 | /* s points to '; print "'''...'''" */ |
| 8320 | putchar('"'); |
| 8321 | do putchar('\''); while (*++s == '\''); |
| 8322 | putchar('"'); |
| 8323 | } while (*s); |
| 8324 | } |
| 8325 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8326 | #if !ENABLE_HUSH_LOCAL |
| 8327 | #define helper_export_local(argv, exp, lvl) \ |
| 8328 | helper_export_local(argv, exp) |
| 8329 | #endif |
| 8330 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8331 | { |
| 8332 | do { |
| 8333 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8334 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8335 | |
| 8336 | /* So far we do not check that name is valid (TODO?) */ |
| 8337 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8338 | if (*name_end == '\0') { |
| 8339 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8340 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8341 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8342 | var = vpp ? *vpp : NULL; |
| 8343 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8344 | if (exp == -1) { /* unexporting? */ |
| 8345 | /* export -n NAME (without =VALUE) */ |
| 8346 | if (var) { |
| 8347 | var->flg_export = 0; |
| 8348 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8349 | unsetenv(name); |
| 8350 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8351 | continue; |
| 8352 | } |
| 8353 | if (exp == 1) { /* exporting? */ |
| 8354 | /* export NAME (without =VALUE) */ |
| 8355 | if (var) { |
| 8356 | var->flg_export = 1; |
| 8357 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8358 | putenv(var->varstr); |
| 8359 | continue; |
| 8360 | } |
| 8361 | } |
| 8362 | /* Exporting non-existing variable. |
| 8363 | * bash does not put it in environment, |
| 8364 | * but remembers that it is exported, |
| 8365 | * and does put it in env when it is set later. |
| 8366 | * We just set it to "" and export. */ |
| 8367 | /* Or, it's "local NAME" (without =VALUE). |
| 8368 | * bash sets the value to "". */ |
| 8369 | name = xasprintf("%s=", name); |
| 8370 | } else { |
| 8371 | /* (Un)exporting/making local NAME=VALUE */ |
| 8372 | name = xstrdup(name); |
| 8373 | } |
| 8374 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8375 | } while (*++argv); |
| 8376 | } |
| 8377 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8378 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8379 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8380 | unsigned opt_unexport; |
| 8381 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8382 | #if ENABLE_HUSH_EXPORT_N |
| 8383 | /* "!": do not abort on errors */ |
| 8384 | opt_unexport = getopt32(argv, "!n"); |
| 8385 | if (opt_unexport == (uint32_t)-1) |
| 8386 | return EXIT_FAILURE; |
| 8387 | argv += optind; |
| 8388 | #else |
| 8389 | opt_unexport = 0; |
| 8390 | argv++; |
| 8391 | #endif |
| 8392 | |
| 8393 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8394 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8395 | if (e) { |
| 8396 | while (*e) { |
| 8397 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8398 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8399 | #else |
| 8400 | /* ash emits: export VAR='VAL' |
| 8401 | * bash: declare -x VAR="VAL" |
| 8402 | * we follow ash example */ |
| 8403 | const char *s = *e++; |
| 8404 | const char *p = strchr(s, '='); |
| 8405 | |
| 8406 | if (!p) /* wtf? take next variable */ |
| 8407 | continue; |
| 8408 | /* export var= */ |
| 8409 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8410 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8411 | putchar('\n'); |
| 8412 | #endif |
| 8413 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8414 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8415 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8416 | return EXIT_SUCCESS; |
| 8417 | } |
| 8418 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8419 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8420 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8421 | return EXIT_SUCCESS; |
| 8422 | } |
| 8423 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8424 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8425 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8426 | { |
| 8427 | if (G.func_nest_level == 0) { |
| 8428 | bb_error_msg("%s: not in a function", argv[0]); |
| 8429 | return EXIT_FAILURE; /* bash compat */ |
| 8430 | } |
| 8431 | helper_export_local(argv, 0, G.func_nest_level); |
| 8432 | return EXIT_SUCCESS; |
| 8433 | } |
| 8434 | #endif |
| 8435 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8436 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8437 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8438 | int sig; |
| 8439 | char *new_cmd; |
| 8440 | |
| 8441 | if (!G.traps) |
| 8442 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8443 | |
| 8444 | argv++; |
| 8445 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8446 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8447 | /* No args: print all trapped */ |
| 8448 | for (i = 0; i < NSIG; ++i) { |
| 8449 | if (G.traps[i]) { |
| 8450 | printf("trap -- "); |
| 8451 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 8452 | /* note: bash adds "SIG", but only if invoked |
| 8453 | * as "bash". If called as "sh", or if set -o posix, |
| 8454 | * then it prints short signal names. |
| 8455 | * We are printing short names: */ |
| 8456 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8457 | } |
| 8458 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8459 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8460 | return EXIT_SUCCESS; |
| 8461 | } |
| 8462 | |
| 8463 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8464 | /* If first arg is a number: reset all specified signals */ |
| 8465 | sig = bb_strtou(*argv, NULL, 10); |
| 8466 | if (errno == 0) { |
| 8467 | int ret; |
| 8468 | process_sig_list: |
| 8469 | ret = EXIT_SUCCESS; |
| 8470 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8471 | sighandler_t handler; |
| 8472 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8473 | sig = get_signum(*argv++); |
| 8474 | if (sig < 0 || sig >= NSIG) { |
| 8475 | ret = EXIT_FAILURE; |
| 8476 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8477 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8478 | continue; |
| 8479 | } |
| 8480 | |
| 8481 | free(G.traps[sig]); |
| 8482 | G.traps[sig] = xstrdup(new_cmd); |
| 8483 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8484 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8485 | get_signame(sig), sig, G.traps[sig]); |
| 8486 | |
| 8487 | /* There is no signal for 0 (EXIT) */ |
| 8488 | if (sig == 0) |
| 8489 | continue; |
| 8490 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8491 | if (new_cmd) |
| 8492 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 8493 | else |
| 8494 | /* We are removing trap handler */ |
| 8495 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8496 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8497 | } |
| 8498 | return ret; |
| 8499 | } |
| 8500 | |
| 8501 | if (!argv[1]) { /* no second arg */ |
| 8502 | bb_error_msg("trap: invalid arguments"); |
| 8503 | return EXIT_FAILURE; |
| 8504 | } |
| 8505 | |
| 8506 | /* First arg is "-": reset all specified to default */ |
| 8507 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 8508 | /* Everything else: set arg as signal handler |
| 8509 | * (includes "" case, which ignores signal) */ |
| 8510 | if (argv[0][0] == '-') { |
| 8511 | if (argv[0][1] == '\0') { /* "-" */ |
| 8512 | /* new_cmd remains NULL: "reset these sigs" */ |
| 8513 | goto reset_traps; |
| 8514 | } |
| 8515 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 8516 | argv++; |
| 8517 | } |
| 8518 | /* else: "-something", no special meaning */ |
| 8519 | } |
| 8520 | new_cmd = *argv; |
| 8521 | reset_traps: |
| 8522 | argv++; |
| 8523 | goto process_sig_list; |
| 8524 | } |
| 8525 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8526 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8527 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8528 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8529 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8530 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8531 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8532 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 8533 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8534 | |
| 8535 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8536 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8537 | type = "an alias";*/ |
| 8538 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8539 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8540 | type = "a function"; |
| 8541 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8542 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8543 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8544 | else if ((path = find_in_path(*argv)) != NULL) |
| 8545 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8546 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8547 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8548 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8549 | continue; |
| 8550 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8551 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8552 | printf("%s is %s\n", *argv, type); |
| 8553 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8554 | } |
| 8555 | |
| 8556 | return ret; |
| 8557 | } |
| 8558 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8559 | #if ENABLE_HUSH_JOB |
| 8560 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8561 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8562 | { |
| 8563 | int i, jobnum; |
| 8564 | struct pipe *pi; |
| 8565 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8566 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8567 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8568 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8569 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8570 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8571 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8572 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8573 | goto found; |
| 8574 | } |
| 8575 | } |
| 8576 | bb_error_msg("%s: no current job", argv[0]); |
| 8577 | return EXIT_FAILURE; |
| 8578 | } |
| 8579 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8580 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8581 | return EXIT_FAILURE; |
| 8582 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8583 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8584 | if (pi->jobid == jobnum) { |
| 8585 | goto found; |
| 8586 | } |
| 8587 | } |
| 8588 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8589 | return EXIT_FAILURE; |
| 8590 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8591 | /* TODO: bash prints a string representation |
| 8592 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8593 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8594 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8595 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8596 | } |
| 8597 | |
| 8598 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8599 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8600 | for (i = 0; i < pi->num_cmds; i++) { |
| 8601 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8602 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8603 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8604 | |
| 8605 | i = kill(- pi->pgrp, SIGCONT); |
| 8606 | if (i < 0) { |
| 8607 | if (errno == ESRCH) { |
| 8608 | delete_finished_bg_job(pi); |
| 8609 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8610 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8611 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8612 | } |
| 8613 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8614 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8615 | remove_bg_job(pi); |
| 8616 | return checkjobs_and_fg_shell(pi); |
| 8617 | } |
| 8618 | return EXIT_SUCCESS; |
| 8619 | } |
| 8620 | #endif |
| 8621 | |
| 8622 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8623 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8624 | { |
| 8625 | const struct built_in_command *x; |
| 8626 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8627 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8628 | "Built-in commands:\n" |
| 8629 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8630 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8631 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8632 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8633 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8634 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8635 | return EXIT_SUCCESS; |
| 8636 | } |
| 8637 | #endif |
| 8638 | |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 8639 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 8640 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 8641 | { |
| 8642 | show_history(G.line_input_state); |
| 8643 | return EXIT_SUCCESS; |
| 8644 | } |
| 8645 | #endif |
| 8646 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8647 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8648 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8649 | { |
| 8650 | struct pipe *job; |
| 8651 | const char *status_string; |
| 8652 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8653 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8654 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8655 | status_string = "Stopped"; |
| 8656 | else |
| 8657 | status_string = "Running"; |
| 8658 | |
| 8659 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8660 | } |
| 8661 | return EXIT_SUCCESS; |
| 8662 | } |
| 8663 | #endif |
| 8664 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8665 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8666 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8667 | { |
| 8668 | void *p; |
| 8669 | unsigned long l; |
| 8670 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8671 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8672 | /* Optional. Reduces probability of false positives */ |
| 8673 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8674 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8675 | /* Crude attempt to find where "free memory" starts, |
| 8676 | * sans fragmentation. */ |
| 8677 | p = malloc(240); |
| 8678 | l = (unsigned long)p; |
| 8679 | free(p); |
| 8680 | p = malloc(3400); |
| 8681 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8682 | free(p); |
| 8683 | |
| 8684 | if (!G.memleak_value) |
| 8685 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8686 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8687 | l -= G.memleak_value; |
| 8688 | if ((long)l < 0) |
| 8689 | l = 0; |
| 8690 | l /= 1024; |
| 8691 | if (l > 127) |
| 8692 | l = 127; |
| 8693 | |
| 8694 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8695 | return l; |
| 8696 | } |
| 8697 | #endif |
| 8698 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8699 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8700 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8701 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8702 | return EXIT_SUCCESS; |
| 8703 | } |
| 8704 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8705 | /* Interruptibility of read builtin in bash |
| 8706 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 8707 | * |
| 8708 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 8709 | * |
| 8710 | * SIGINT: |
| 8711 | * - terminates non-interactive shell; |
| 8712 | * - interrupts read in interactive shell; |
| 8713 | * if it has non-empty trap: |
| 8714 | * - executes trap and returns to command prompt in interactive shell; |
| 8715 | * - executes trap and returns to read in non-interactive shell; |
| 8716 | * SIGTERM: |
| 8717 | * - is ignored (does not interrupt) read in interactive shell; |
| 8718 | * - terminates non-interactive shell; |
| 8719 | * if it has non-empty trap: |
| 8720 | * - executes trap and returns to read; |
| 8721 | * SIGHUP: |
| 8722 | * - terminates shell (regardless of interactivity); |
| 8723 | * if it has non-empty trap: |
| 8724 | * - executes trap and returns to read; |
| 8725 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8726 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8727 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8728 | const char *r; |
| 8729 | char *opt_n = NULL; |
| 8730 | char *opt_p = NULL; |
| 8731 | char *opt_t = NULL; |
| 8732 | char *opt_u = NULL; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8733 | const char *ifs; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8734 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8735 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8736 | /* "!": do not abort on errors. |
| 8737 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8738 | */ |
| 8739 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8740 | if (read_flags == (uint32_t)-1) |
| 8741 | return EXIT_FAILURE; |
| 8742 | argv += optind; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8743 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 8744 | |
| 8745 | again: |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8746 | r = shell_builtin_read(set_local_var_from_halves, |
| 8747 | argv, |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8748 | ifs, |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8749 | read_flags, |
| 8750 | opt_n, |
| 8751 | opt_p, |
| 8752 | opt_t, |
| 8753 | opt_u |
| 8754 | ); |
| 8755 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8756 | if ((uintptr_t)r == 1 && errno == EINTR) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8757 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8758 | if (sig && sig != SIGINT) |
| 8759 | goto again; |
| 8760 | } |
| 8761 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8762 | if ((uintptr_t)r > 1) { |
| 8763 | bb_error_msg("%s", r); |
| 8764 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8765 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8766 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8767 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8768 | } |
| 8769 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8770 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8771 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8772 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8773 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8774 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8775 | * set -- [argument...] |
| 8776 | * set -o |
| 8777 | * set +o |
| 8778 | * Implementations shall support the options in both their hyphen and |
| 8779 | * plus-sign forms. These options can also be specified as options to sh. |
| 8780 | * Examples: |
| 8781 | * Write out all variables and their values: set |
| 8782 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8783 | * Turn on the -x and -v options: set -xv |
| 8784 | * Unset all positional parameters: set -- |
| 8785 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8786 | * Set the positional parameters to the expansion of x, even if x expands |
| 8787 | * with a leading '-' or '+': set -- $x |
| 8788 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8789 | * 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] | 8790 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8791 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8792 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8793 | int n; |
| 8794 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8795 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8796 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8797 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8798 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8799 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8800 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8801 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8802 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8803 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8804 | do { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8805 | if (strcmp(arg, "--") == 0) { |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8806 | ++argv; |
| 8807 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8808 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8809 | if (arg[0] != '+' && arg[0] != '-') |
| 8810 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8811 | for (n = 1; arg[n]; ++n) { |
| 8812 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8813 | goto error; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8814 | if (arg[n] == 'o' && argv[1]) |
| 8815 | argv++; |
| 8816 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8817 | } while ((arg = *++argv) != NULL); |
| 8818 | /* Now argv[0] is 1st argument */ |
| 8819 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8820 | if (arg == NULL) |
| 8821 | return EXIT_SUCCESS; |
| 8822 | set_argv: |
| 8823 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8824 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8825 | g_argv = G.global_argv; |
| 8826 | if (G.global_args_malloced) { |
| 8827 | pp = g_argv; |
| 8828 | while (*++pp) |
| 8829 | free(*pp); |
| 8830 | g_argv[1] = NULL; |
| 8831 | } else { |
| 8832 | G.global_args_malloced = 1; |
| 8833 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8834 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8835 | g_argv = pp; |
| 8836 | } |
| 8837 | /* This realloc's G.global_argv */ |
| 8838 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8839 | |
| 8840 | n = 1; |
| 8841 | while (*++pp) |
| 8842 | n++; |
| 8843 | G.global_argc = n; |
| 8844 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8845 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8846 | |
| 8847 | /* Nothing known, so abort */ |
| 8848 | error: |
| 8849 | bb_error_msg("set: %s: invalid option", arg); |
| 8850 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8853 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8854 | { |
| 8855 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8856 | argv = skip_dash_dash(argv); |
| 8857 | if (argv[0]) { |
| 8858 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8859 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8860 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8861 | if (G.global_args_malloced) { |
| 8862 | int m = 1; |
| 8863 | while (m <= n) |
| 8864 | free(G.global_argv[m++]); |
| 8865 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8866 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8867 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8868 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8869 | return EXIT_SUCCESS; |
| 8870 | } |
| 8871 | return EXIT_FAILURE; |
| 8872 | } |
| 8873 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8874 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8875 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8876 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8877 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8878 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8879 | #if ENABLE_HUSH_FUNCTIONS |
| 8880 | smallint sv_flg; |
| 8881 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8882 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8883 | argv = skip_dash_dash(argv); |
| 8884 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8885 | if (!filename) { |
| 8886 | /* bash says: "bash: .: filename argument required" */ |
| 8887 | return 2; /* bash compat */ |
| 8888 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8889 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8890 | if (!strchr(filename, '/')) { |
| 8891 | arg_path = find_in_path(filename); |
| 8892 | if (arg_path) |
| 8893 | filename = arg_path; |
| 8894 | } |
| 8895 | input = fopen_or_warn(filename, "r"); |
| 8896 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8897 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8898 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8899 | /* POSIX: non-interactive shell should abort here, |
| 8900 | * not merely fail. So far no one complained :) |
| 8901 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8902 | return EXIT_FAILURE; |
| 8903 | } |
| 8904 | close_on_exec_on(fileno(input)); |
| 8905 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8906 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8907 | sv_flg = G.flag_return_in_progress; |
| 8908 | /* "we are inside sourced file, ok to use return" */ |
| 8909 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8910 | #endif |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8911 | if (argv[1]) |
| 8912 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8913 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8914 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8915 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8916 | |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8917 | if (argv[1]) |
| 8918 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8919 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8920 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8921 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8922 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8923 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8924 | } |
| 8925 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8926 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8927 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8928 | int rc; |
| 8929 | mode_t mask; |
| 8930 | |
| 8931 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8932 | argv = skip_dash_dash(argv); |
| 8933 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8934 | mode_t old_mask = mask; |
| 8935 | |
| 8936 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8937 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8938 | mask ^= 0777; |
| 8939 | if (rc == 0) { |
| 8940 | mask = old_mask; |
| 8941 | /* bash messages: |
| 8942 | * bash: umask: 'q': invalid symbolic mode operator |
| 8943 | * bash: umask: 999: octal number out of range |
| 8944 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8945 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8946 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8947 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8948 | rc = 1; |
| 8949 | /* Mimic bash */ |
| 8950 | printf("%04o\n", (unsigned) mask); |
| 8951 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8952 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8953 | umask(mask); |
| 8954 | |
| 8955 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8956 | } |
| 8957 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8958 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8959 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8960 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8961 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8962 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8963 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8964 | /* "!": do not abort on errors */ |
| 8965 | /* "+": stop at 1st non-option */ |
| 8966 | opts = getopt32(argv, "!+vf"); |
| 8967 | if (opts == (unsigned)-1) |
| 8968 | return EXIT_FAILURE; |
| 8969 | if (opts == 3) { |
| 8970 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8971 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8972 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8973 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8974 | |
| 8975 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8976 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8977 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8978 | if (unset_local_var(*argv)) { |
| 8979 | /* unset <nonexistent_var> doesn't fail. |
| 8980 | * Error is when one tries to unset RO var. |
| 8981 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8982 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8983 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8984 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8985 | #if ENABLE_HUSH_FUNCTIONS |
| 8986 | else { |
| 8987 | unset_func(*argv); |
| 8988 | } |
| 8989 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8990 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8991 | } |
| 8992 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8993 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8994 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8995 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8996 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8997 | { |
| 8998 | int ret = EXIT_SUCCESS; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8999 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9000 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9001 | argv = skip_dash_dash(argv); |
| 9002 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9003 | /* Don't care about wait results */ |
| 9004 | /* Note 1: must wait until there are no more children */ |
| 9005 | /* Note 2: must be interruptible */ |
| 9006 | /* Examples: |
| 9007 | * $ sleep 3 & sleep 6 & wait |
| 9008 | * [1] 30934 sleep 3 |
| 9009 | * [2] 30935 sleep 6 |
| 9010 | * [1] Done sleep 3 |
| 9011 | * [2] Done sleep 6 |
| 9012 | * $ sleep 3 & sleep 6 & wait |
| 9013 | * [1] 30936 sleep 3 |
| 9014 | * [2] 30937 sleep 6 |
| 9015 | * [1] Done sleep 3 |
| 9016 | * ^C <-- after ~4 sec from keyboard |
| 9017 | * $ |
| 9018 | */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9019 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9020 | int sig; |
| 9021 | sigset_t oldset, allsigs; |
| 9022 | |
| 9023 | /* waitpid is not interruptible by SA_RESTARTed |
| 9024 | * signals which we use. Thus, this ugly dance: |
| 9025 | */ |
| 9026 | |
| 9027 | /* Make sure possible SIGCHLD is stored in kernel's |
| 9028 | * pending signal mask before we call waitpid. |
| 9029 | * Or else we may race with SIGCHLD, lose it, |
| 9030 | * and get stuck in sigwaitinfo... |
| 9031 | */ |
| 9032 | sigfillset(&allsigs); |
| 9033 | sigprocmask(SIG_SETMASK, &allsigs, &oldset); |
| 9034 | |
| 9035 | if (!sigisemptyset(&G.pending_set)) { |
| 9036 | /* Crap! we raced with some signal! */ |
| 9037 | // sig = 0; |
| 9038 | goto restore; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9039 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9040 | |
| 9041 | checkjobs(NULL); /* waitpid(WNOHANG) inside */ |
| 9042 | if (errno == ECHILD) { |
| 9043 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9044 | break; |
| 9045 | } |
| 9046 | |
| 9047 | /* Wait for SIGCHLD or any other signal */ |
| 9048 | //sig = sigwaitinfo(&allsigs, NULL); |
| 9049 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 9050 | /* Note: sigsuspend invokes signal handler */ |
| 9051 | sigsuspend(&oldset); |
| 9052 | restore: |
| 9053 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9054 | |
| 9055 | /* So, did we get a signal? */ |
| 9056 | //if (sig > 0) |
| 9057 | // raise(sig); /* run handler */ |
| 9058 | sig = check_and_run_traps(); |
| 9059 | if (sig /*&& sig != SIGCHLD - always true */) { |
| 9060 | /* see note 2 */ |
| 9061 | ret = 128 + sig; |
| 9062 | break; |
| 9063 | } |
| 9064 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9065 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9066 | return ret; |
| 9067 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9068 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9069 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9070 | while (*argv) { |
| 9071 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 9072 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9073 | /* mimic bash message */ |
| 9074 | 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] | 9075 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9076 | } |
| 9077 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9078 | if (WIFSIGNALED(status)) |
| 9079 | ret = 128 + WTERMSIG(status); |
| 9080 | else if (WIFEXITED(status)) |
| 9081 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9082 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9083 | ret = EXIT_FAILURE; |
| 9084 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9085 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9086 | ret = 127; |
| 9087 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9088 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9089 | } |
| 9090 | |
| 9091 | return ret; |
| 9092 | } |
| 9093 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9094 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 9095 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 9096 | { |
| 9097 | if (argv[1]) { |
| 9098 | def = bb_strtou(argv[1], NULL, 10); |
| 9099 | if (errno || def < def_min || argv[2]) { |
| 9100 | bb_error_msg("%s: bad arguments", argv[0]); |
| 9101 | def = UINT_MAX; |
| 9102 | } |
| 9103 | } |
| 9104 | return def; |
| 9105 | } |
| 9106 | #endif |
| 9107 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9108 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9109 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9110 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9111 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9112 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9113 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 9114 | return EXIT_SUCCESS; /* bash compat */ |
| 9115 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9116 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9117 | |
| 9118 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 9119 | if (depth == UINT_MAX) |
| 9120 | G.flag_break_continue = BC_BREAK; |
| 9121 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9122 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9123 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9124 | return EXIT_SUCCESS; |
| 9125 | } |
| 9126 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9127 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9128 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9129 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 9130 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9131 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9132 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9133 | |
| 9134 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9135 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9136 | { |
| 9137 | int rc; |
| 9138 | |
| 9139 | if (G.flag_return_in_progress != -1) { |
| 9140 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 9141 | return EXIT_FAILURE; /* bash compat */ |
| 9142 | } |
| 9143 | |
| 9144 | G.flag_return_in_progress = 1; |
| 9145 | |
| 9146 | /* bash: |
| 9147 | * out of range: wraps around at 256, does not error out |
| 9148 | * non-numeric param: |
| 9149 | * f() { false; return qwe; }; f; echo $? |
| 9150 | * bash: return: qwe: numeric argument required <== we do this |
| 9151 | * 255 <== we also do this |
| 9152 | */ |
| 9153 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 9154 | return rc; |
| 9155 | } |
| 9156 | #endif |