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 | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 94 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 95 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 96 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 97 | #include "unicode.h" |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 98 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 99 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 100 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 101 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 102 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 103 | #else |
| 104 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 105 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 106 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 107 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 108 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 109 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 110 | //config:config HUSH |
| 111 | //config: bool "hush" |
| 112 | //config: default y |
| 113 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 114 | //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] | 115 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 116 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 117 | //config: and functions are supported. |
| 118 | //config: |
| 119 | //config: It will compile and work on no-mmu systems. |
| 120 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 121 | //config: It does not handle select, aliases, tilde expansion, |
| 122 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 123 | //config: |
| 124 | //config:config HUSH_BASH_COMPAT |
| 125 | //config: bool "bash-compatible extensions" |
| 126 | //config: default y |
| 127 | //config: depends on HUSH |
| 128 | //config: help |
| 129 | //config: Enable bash-compatible extensions. |
| 130 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 131 | //config:config HUSH_BRACE_EXPANSION |
| 132 | //config: bool "Brace expansion" |
| 133 | //config: default y |
| 134 | //config: depends on HUSH_BASH_COMPAT |
| 135 | //config: help |
| 136 | //config: Enable {abc,def} extension. |
| 137 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 138 | //config:config HUSH_HELP |
| 139 | //config: bool "help builtin" |
| 140 | //config: default y |
| 141 | //config: depends on HUSH |
| 142 | //config: help |
| 143 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 144 | //config: |
| 145 | //config:config HUSH_INTERACTIVE |
| 146 | //config: bool "Interactive mode" |
| 147 | //config: default y |
| 148 | //config: depends on HUSH |
| 149 | //config: help |
| 150 | //config: Enable interactive mode (prompt and command editing). |
| 151 | //config: Without this, hush simply reads and executes commands |
| 152 | //config: from stdin just like a shell script from a file. |
| 153 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 154 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 155 | //config:config HUSH_SAVEHISTORY |
| 156 | //config: bool "Save command history to .hush_history" |
| 157 | //config: default y |
| 158 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 159 | //config: help |
| 160 | //config: Enable history saving in hush. |
| 161 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 162 | //config:config HUSH_JOB |
| 163 | //config: bool "Job control" |
| 164 | //config: default y |
| 165 | //config: depends on HUSH_INTERACTIVE |
| 166 | //config: help |
| 167 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 168 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 169 | //config: "cmd &" still works by simply spawning a process and immediately |
| 170 | //config: prompting for next command (or executing next command in a script), |
| 171 | //config: but no separate process group is formed. |
| 172 | //config: |
| 173 | //config:config HUSH_TICK |
| 174 | //config: bool "Process substitution" |
| 175 | //config: default y |
| 176 | //config: depends on HUSH |
| 177 | //config: help |
| 178 | //config: Enable process substitution `command` and $(command) in hush. |
| 179 | //config: |
| 180 | //config:config HUSH_IF |
| 181 | //config: bool "Support if/then/elif/else/fi" |
| 182 | //config: default y |
| 183 | //config: depends on HUSH |
| 184 | //config: help |
| 185 | //config: Enable if/then/elif/else/fi in hush. |
| 186 | //config: |
| 187 | //config:config HUSH_LOOPS |
| 188 | //config: bool "Support for, while and until loops" |
| 189 | //config: default y |
| 190 | //config: depends on HUSH |
| 191 | //config: help |
| 192 | //config: Enable for, while and until loops in hush. |
| 193 | //config: |
| 194 | //config:config HUSH_CASE |
| 195 | //config: bool "Support case ... esac statement" |
| 196 | //config: default y |
| 197 | //config: depends on HUSH |
| 198 | //config: help |
| 199 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 200 | //config: |
| 201 | //config:config HUSH_FUNCTIONS |
| 202 | //config: bool "Support funcname() { commands; } syntax" |
| 203 | //config: default y |
| 204 | //config: depends on HUSH |
| 205 | //config: help |
| 206 | //config: Enable support for shell functions in hush. +800 bytes. |
| 207 | //config: |
| 208 | //config:config HUSH_LOCAL |
| 209 | //config: bool "Support local builtin" |
| 210 | //config: default y |
| 211 | //config: depends on HUSH_FUNCTIONS |
| 212 | //config: help |
| 213 | //config: Enable support for local variables in functions. |
| 214 | //config: |
| 215 | //config:config HUSH_RANDOM_SUPPORT |
| 216 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 217 | //config: default y |
| 218 | //config: depends on HUSH |
| 219 | //config: help |
| 220 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 221 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 222 | //config: |
| 223 | //config:config HUSH_EXPORT_N |
| 224 | //config: bool "Support 'export -n' option" |
| 225 | //config: default y |
| 226 | //config: depends on HUSH |
| 227 | //config: help |
| 228 | //config: export -n unexports variables. It is a bash extension. |
| 229 | //config: |
| 230 | //config:config HUSH_MODE_X |
| 231 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 232 | //config: default y |
| 233 | //config: depends on HUSH |
| 234 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 235 | //config: This instructs hush to print commands before execution. |
| 236 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 237 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 238 | //config:config MSH |
| 239 | //config: bool "msh (deprecated: aliased to hush)" |
| 240 | //config: default n |
| 241 | //config: select HUSH |
| 242 | //config: help |
| 243 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 244 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 245 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 246 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
| 247 | //applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP)) |
| 248 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 249 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 250 | |
| 251 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 252 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 253 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 254 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 255 | * but currently do nothing, therefore aren't shown in help. |
| 256 | * NOMMU-specific options are not meant to be used by users, |
| 257 | * therefore we don't show them either. |
| 258 | */ |
| 259 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 260 | //usage: "[-nxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 261 | //usage:#define hush_full_usage "\n\n" |
| 262 | //usage: "Unix shell interpreter" |
| 263 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 264 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 265 | //usage:#define msh_full_usage hush_full_usage |
| 266 | |
| 267 | //usage:#if ENABLE_FEATURE_SH_IS_HUSH |
| 268 | //usage:# define sh_trivial_usage hush_trivial_usage |
| 269 | //usage:# define sh_full_usage hush_full_usage |
| 270 | //usage:#endif |
| 271 | //usage:#if ENABLE_FEATURE_BASH_IS_HUSH |
| 272 | //usage:# define bash_trivial_usage hush_trivial_usage |
| 273 | //usage:# define bash_full_usage hush_full_usage |
| 274 | //usage:#endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 275 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 276 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 277 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 278 | #define LEAK_HUNTING 0 |
| 279 | #define BUILD_AS_NOMMU 0 |
| 280 | /* Enable/disable sanity checks. Ok to enable in production, |
| 281 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 282 | * Keeping 1 for now even in released versions. |
| 283 | */ |
| 284 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 285 | /* Slightly bigger (+200 bytes), but faster hush. |
| 286 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 287 | * which allows us to do fewer waitpid's. |
| 288 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 289 | * and therefore waitpid will return the same result as last time) |
| 290 | */ |
| 291 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 292 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 293 | * So far ${var%...} ops are always enabled: |
| 294 | */ |
| 295 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 296 | |
| 297 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 298 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 299 | # undef BB_MMU |
| 300 | # undef USE_FOR_NOMMU |
| 301 | # undef USE_FOR_MMU |
| 302 | # define BB_MMU 0 |
| 303 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 304 | # define USE_FOR_MMU(...) |
| 305 | #endif |
| 306 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 307 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 308 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 309 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 310 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 311 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 312 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 313 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 314 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 315 | # define IF_FEATURE_SH_STANDALONE(...) |
| 316 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 317 | #endif |
| 318 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 319 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 320 | # undef ENABLE_FEATURE_EDITING |
| 321 | # define ENABLE_FEATURE_EDITING 0 |
| 322 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 323 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 324 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 325 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 326 | #endif |
| 327 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 328 | /* Do we support ANY keywords? */ |
| 329 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 330 | # define HAS_KEYWORDS 1 |
| 331 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 332 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 333 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 334 | # define HAS_KEYWORDS 0 |
| 335 | # define IF_HAS_KEYWORDS(...) |
| 336 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 337 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 338 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 339 | /* If you comment out one of these below, it will be #defined later |
| 340 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 341 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 342 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 343 | #define debug_printf_parse(...) do {} while (0) |
| 344 | #define debug_print_tree(a, b) do {} while (0) |
| 345 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 346 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 347 | #define debug_printf_jobs(...) do {} while (0) |
| 348 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 349 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 350 | #define debug_printf_glob(...) do {} while (0) |
| 351 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 352 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 353 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 355 | #define ERR_PTR ((void*)(long)1) |
| 356 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 357 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 358 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 359 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 360 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 361 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 362 | #if ENABLE_HUSH_BASH_COMPAT |
| 363 | /* Support / and // replace ops */ |
| 364 | /* Note that // is stored as \ in "encoded" string representation */ |
| 365 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 366 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 367 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 368 | #else |
| 369 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 370 | # define VAR_SUBST_OPS "%#:-=+?" |
| 371 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 372 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 373 | |
| 374 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 375 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 376 | struct variable; |
| 377 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 378 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 379 | |
| 380 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 381 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 382 | */ |
| 383 | #if !BB_MMU |
| 384 | typedef struct nommu_save_t { |
| 385 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 386 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 387 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 388 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 389 | } nommu_save_t; |
| 390 | #endif |
| 391 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 392 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 393 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 394 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 395 | RES_IF , |
| 396 | RES_THEN , |
| 397 | RES_ELIF , |
| 398 | RES_ELSE , |
| 399 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 400 | #endif |
| 401 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 402 | RES_FOR , |
| 403 | RES_WHILE , |
| 404 | RES_UNTIL , |
| 405 | RES_DO , |
| 406 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 407 | #endif |
| 408 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 409 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 410 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 411 | #if ENABLE_HUSH_CASE |
| 412 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 413 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 414 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 415 | RES_MATCH , /* "word)" */ |
| 416 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 417 | RES_ESAC , |
| 418 | #endif |
| 419 | RES_XXXX , |
| 420 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 421 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 422 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 423 | typedef struct o_string { |
| 424 | char *data; |
| 425 | int length; /* position where data is appended */ |
| 426 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 427 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 428 | /* At least some part of the string was inside '' or "", |
| 429 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 430 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 431 | smallint has_empty_slot; |
| 432 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 433 | } o_string; |
| 434 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 435 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 436 | EXP_FLAG_GLOB = 0x2, |
| 437 | /* Protect newly added chars against globbing |
| 438 | * by prepending \ to *, ?, [, \ */ |
| 439 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 440 | }; |
| 441 | enum { |
| 442 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 443 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 444 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 445 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 446 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 447 | }; |
| 448 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 449 | #define NULL_O_STRING { NULL } |
| 450 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 451 | #ifndef debug_printf_parse |
| 452 | static const char *const assignment_flag[] = { |
| 453 | "MAYBE_ASSIGNMENT", |
| 454 | "DEFINITELY_ASSIGNMENT", |
| 455 | "NOT_ASSIGNMENT", |
| 456 | "WORD_IS_KEYWORD", |
| 457 | }; |
| 458 | #endif |
| 459 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 460 | typedef struct in_str { |
| 461 | const char *p; |
| 462 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 463 | char eof_flag; /* meaningless if ->p == NULL */ |
| 464 | char peek_buf[2]; |
| 465 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 466 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 467 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 468 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 469 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 470 | int (*get) (struct in_str *) FAST_FUNC; |
| 471 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 472 | } in_str; |
| 473 | #define i_getch(input) ((input)->get(input)) |
| 474 | #define i_peek(input) ((input)->peek(input)) |
| 475 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 476 | /* The descrip member of this structure is only used to make |
| 477 | * debugging output pretty */ |
| 478 | static const struct { |
| 479 | int mode; |
| 480 | signed char default_fd; |
| 481 | char descrip[3]; |
| 482 | } redir_table[] = { |
| 483 | { O_RDONLY, 0, "<" }, |
| 484 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 485 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 486 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 487 | { O_RDONLY, 0, "<<" }, |
| 488 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 489 | /* { O_RDONLY, 77, "<<" }, */ |
| 490 | }; |
| 491 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 492 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 493 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 494 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 495 | int rd_fd; /* fd to redirect */ |
| 496 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 497 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 498 | smallint rd_type; /* (enum redir_type) */ |
| 499 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 500 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 501 | * bit 0: do we need to trim leading tabs? |
| 502 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 503 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 504 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 505 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 506 | REDIRECT_INPUT = 0, |
| 507 | REDIRECT_OVERWRITE = 1, |
| 508 | REDIRECT_APPEND = 2, |
| 509 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 510 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 511 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 512 | |
| 513 | REDIRFD_CLOSE = -3, |
| 514 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 515 | REDIRFD_TO_FILE = -1, |
| 516 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 517 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 518 | HEREDOC_SKIPTABS = 1, |
| 519 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 520 | } redir_type; |
| 521 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 522 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 523 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 524 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 525 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 526 | smallint cmd_type; /* CMD_xxx */ |
| 527 | #define CMD_NORMAL 0 |
| 528 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 529 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 530 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 531 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 532 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 533 | #if ENABLE_HUSH_FUNCTIONS |
| 534 | # define CMD_FUNCDEF 3 |
| 535 | #endif |
| 536 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 537 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 538 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 539 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 540 | #if !BB_MMU |
| 541 | char *group_as_string; |
| 542 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 543 | #if ENABLE_HUSH_FUNCTIONS |
| 544 | struct function *child_func; |
| 545 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 546 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 547 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 548 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 549 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 550 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 551 | * we put those fields back into cmd->xxx |
| 552 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 553 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 554 | * Without this trick, loop would execute a;b;b;b;... |
| 555 | * instead of correct sequence a;b;a;b;... |
| 556 | * When command is freed, it severs the link |
| 557 | * (sets ->child_func->parent_cmd to NULL). |
| 558 | */ |
| 559 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 560 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 561 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 562 | * and on execution these are substituted with their values. |
| 563 | * Substitution can make _several_ words out of one argv[n]! |
| 564 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 565 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 566 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 567 | struct redir_struct *redirects; /* I/O redirections */ |
| 568 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 569 | /* Is there anything in this command at all? */ |
| 570 | #define IS_NULL_CMD(cmd) \ |
| 571 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 572 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 573 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 574 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 575 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 576 | int alive_cmds; /* number of commands running (not exited) */ |
| 577 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 578 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 579 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 580 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 581 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 582 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 583 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 584 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 585 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 586 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 587 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 588 | typedef enum pipe_style { |
| 589 | PIPE_SEQ = 1, |
| 590 | PIPE_AND = 2, |
| 591 | PIPE_OR = 3, |
| 592 | PIPE_BG = 4, |
| 593 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 594 | /* Is there anything in this pipe at all? */ |
| 595 | #define IS_NULL_PIPE(pi) \ |
| 596 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 597 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 598 | /* This holds pointers to the various results of parsing */ |
| 599 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 600 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 601 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 602 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 603 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 604 | /* last command in pipe (being constructed right now) */ |
| 605 | struct command *command; |
| 606 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 607 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 608 | #if !BB_MMU |
| 609 | o_string as_string; |
| 610 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 611 | #if HAS_KEYWORDS |
| 612 | smallint ctx_res_w; |
| 613 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 614 | #if ENABLE_HUSH_CASE |
| 615 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 616 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 617 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 618 | int old_flag; |
| 619 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 620 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 621 | * when we see "if" or "then", we malloc and copy current context, |
| 622 | * and make ->stack point to it. then we parse pipeN. |
| 623 | * when closing "then" / fi" / whatever is found, |
| 624 | * we move list_head into ->stack->command->group, |
| 625 | * copy ->stack into current context, and delete ->stack. |
| 626 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 627 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 628 | struct parse_context *stack; |
| 629 | #endif |
| 630 | }; |
| 631 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 632 | /* On program start, environ points to initial environment. |
| 633 | * putenv adds new pointers into it, unsetenv removes them. |
| 634 | * Neither of these (de)allocates the strings. |
| 635 | * setenv allocates new strings in malloc space and does putenv, |
| 636 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 637 | #define setenv(...) setenv_is_leaky_dont_use() |
| 638 | struct variable { |
| 639 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 640 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 641 | #if ENABLE_HUSH_LOCAL |
| 642 | unsigned func_nest_level; |
| 643 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 644 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 645 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 646 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 647 | }; |
| 648 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 649 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 650 | BC_BREAK = 1, |
| 651 | BC_CONTINUE = 2, |
| 652 | }; |
| 653 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 654 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 655 | struct function { |
| 656 | struct function *next; |
| 657 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 658 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 659 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 660 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 661 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 662 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 663 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 664 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 665 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 666 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 667 | /* set -/+o OPT support. (TODO: make it optional) |
| 668 | * bash supports the following opts: |
| 669 | * allexport off |
| 670 | * braceexpand on |
| 671 | * emacs on |
| 672 | * errexit off |
| 673 | * errtrace off |
| 674 | * functrace off |
| 675 | * hashall on |
| 676 | * histexpand off |
| 677 | * history on |
| 678 | * ignoreeof off |
| 679 | * interactive-comments on |
| 680 | * keyword off |
| 681 | * monitor on |
| 682 | * noclobber off |
| 683 | * noexec off |
| 684 | * noglob off |
| 685 | * nolog off |
| 686 | * notify off |
| 687 | * nounset off |
| 688 | * onecmd off |
| 689 | * physical off |
| 690 | * pipefail off |
| 691 | * posix off |
| 692 | * privileged off |
| 693 | * verbose off |
| 694 | * vi off |
| 695 | * xtrace off |
| 696 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 697 | static const char o_opt_strings[] ALIGN1 = |
| 698 | "pipefail\0" |
| 699 | "noexec\0" |
| 700 | #if ENABLE_HUSH_MODE_X |
| 701 | "xtrace\0" |
| 702 | #endif |
| 703 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 704 | enum { |
| 705 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 706 | OPT_O_NOEXEC, |
| 707 | #if ENABLE_HUSH_MODE_X |
| 708 | OPT_O_XTRACE, |
| 709 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 710 | NUM_OPT_O |
| 711 | }; |
| 712 | |
| 713 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 714 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 715 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 716 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 717 | /* interactive_fd != 0 means we are an interactive shell. |
| 718 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 719 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 720 | * job control still works, but terminal signals |
| 721 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 722 | * process groups can only be created with "cmd &". |
| 723 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 724 | * to give tty to the foreground process group, |
| 725 | * and will take it back when the group is stopped (^Z) |
| 726 | * or killed (^C). |
| 727 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 728 | #if ENABLE_HUSH_INTERACTIVE |
| 729 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 730 | * _AND_ if we decided to act interactively */ |
| 731 | int interactive_fd; |
| 732 | const char *PS1; |
| 733 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 734 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 735 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 736 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 737 | #endif |
| 738 | #if ENABLE_FEATURE_EDITING |
| 739 | line_input_t *line_input_state; |
| 740 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 741 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 742 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 743 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 744 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 745 | random_t random_gen; |
| 746 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 747 | #if ENABLE_HUSH_JOB |
| 748 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 749 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 750 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 751 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 752 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 753 | #else |
| 754 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 755 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 756 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 757 | #if ENABLE_HUSH_MODE_X |
| 758 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 759 | #else |
| 760 | # define G_x_mode 0 |
| 761 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 762 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 763 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 764 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 765 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 766 | #if ENABLE_HUSH_FUNCTIONS |
| 767 | /* 0: outside of a function (or sourced file) |
| 768 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 769 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 770 | */ |
| 771 | smallint flag_return_in_progress; |
| 772 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 773 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 774 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 775 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 776 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 777 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 778 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 779 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 780 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 781 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 782 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 783 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 784 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 785 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 786 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 787 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 788 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 789 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 790 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 791 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 792 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 793 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 794 | # if ENABLE_HUSH_LOCAL |
| 795 | struct variable **shadowed_vars_pp; |
| 796 | unsigned func_nest_level; |
| 797 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 798 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 799 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 800 | #if ENABLE_HUSH_FAST |
| 801 | unsigned count_SIGCHLD; |
| 802 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 803 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 804 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 805 | /* Which signals have non-DFL handler (even with no traps set)? |
| 806 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 807 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 808 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 809 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 810 | * Other than these two times, never modified. |
| 811 | */ |
| 812 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 813 | #if ENABLE_HUSH_JOB |
| 814 | unsigned fatal_sig_mask; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 815 | # define G_fatal_sig_mask G.fatal_sig_mask |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 816 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 817 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 818 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 819 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 820 | sigset_t pending_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 821 | #if HUSH_DEBUG |
| 822 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 823 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 824 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 825 | struct sigaction sa; |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 826 | 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] | 827 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 828 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 829 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 830 | * (too many defines). Also, I actually prefer to see when a variable |
| 831 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 832 | #define INIT_G() do { \ |
| 833 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 834 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 835 | sigfillset(&G.sa.sa_mask); \ |
| 836 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 837 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 838 | |
| 839 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 840 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 841 | static int builtin_cd(char **argv) FAST_FUNC; |
| 842 | static int builtin_echo(char **argv) FAST_FUNC; |
| 843 | static int builtin_eval(char **argv) FAST_FUNC; |
| 844 | static int builtin_exec(char **argv) FAST_FUNC; |
| 845 | static int builtin_exit(char **argv) FAST_FUNC; |
| 846 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 847 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 848 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 849 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 850 | #endif |
| 851 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 852 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 853 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 854 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 855 | static int builtin_history(char **argv) FAST_FUNC; |
| 856 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 857 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 858 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 859 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 860 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 861 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 862 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 863 | #if ENABLE_PRINTF |
| 864 | static int builtin_printf(char **argv) FAST_FUNC; |
| 865 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 866 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 867 | static int builtin_read(char **argv) FAST_FUNC; |
| 868 | static int builtin_set(char **argv) FAST_FUNC; |
| 869 | static int builtin_shift(char **argv) FAST_FUNC; |
| 870 | static int builtin_source(char **argv) FAST_FUNC; |
| 871 | static int builtin_test(char **argv) FAST_FUNC; |
| 872 | static int builtin_trap(char **argv) FAST_FUNC; |
| 873 | static int builtin_type(char **argv) FAST_FUNC; |
| 874 | static int builtin_true(char **argv) FAST_FUNC; |
| 875 | static int builtin_umask(char **argv) FAST_FUNC; |
| 876 | static int builtin_unset(char **argv) FAST_FUNC; |
| 877 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 878 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 879 | static int builtin_break(char **argv) FAST_FUNC; |
| 880 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 881 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 882 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 883 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 884 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 885 | |
| 886 | /* Table of built-in functions. They can be forked or not, depending on |
| 887 | * context: within pipes, they fork. As simple commands, they do not. |
| 888 | * When used in non-forking context, they can change global variables |
| 889 | * in the parent shell process. If forked, of course they cannot. |
| 890 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 891 | * still be set at the end. */ |
| 892 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 893 | const char *b_cmd; |
| 894 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 895 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 896 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 897 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 898 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 899 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 900 | #endif |
| 901 | }; |
| 902 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 903 | static const struct built_in_command bltins1[] = { |
| 904 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 905 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 906 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 907 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 908 | #endif |
| 909 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 910 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 911 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 912 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 913 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 914 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 915 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 916 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 917 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 918 | BLTIN("exit" , builtin_exit , "Exit"), |
| 919 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 920 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 921 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 922 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 923 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 924 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 925 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 926 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 927 | BLTIN("history" , builtin_history , "Show command history"), |
| 928 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 929 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 930 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 931 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 932 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 933 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 934 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 935 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 936 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 937 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 938 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 939 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 940 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 941 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 942 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 943 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 944 | #if ENABLE_HUSH_BASH_COMPAT |
| 945 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 946 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 947 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 948 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 949 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 950 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 951 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 952 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 953 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 954 | }; |
| 955 | /* For now, echo and test are unconditionally enabled. |
| 956 | * Maybe make it configurable? */ |
| 957 | static const struct built_in_command bltins2[] = { |
| 958 | BLTIN("[" , builtin_test , NULL), |
| 959 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 960 | #if ENABLE_PRINTF |
| 961 | BLTIN("printf" , builtin_printf , NULL), |
| 962 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 963 | BLTIN("pwd" , builtin_pwd , NULL), |
| 964 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 965 | }; |
| 966 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 967 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 968 | /* Debug printouts. |
| 969 | */ |
| 970 | #if HUSH_DEBUG |
| 971 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 972 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 973 | # define debug_enter() (G.debug_indent++) |
| 974 | # define debug_leave() (G.debug_indent--) |
| 975 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 976 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 977 | # define debug_enter() ((void)0) |
| 978 | # define debug_leave() ((void)0) |
| 979 | #endif |
| 980 | |
| 981 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 982 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 983 | #endif |
| 984 | |
| 985 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 986 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 987 | #endif |
| 988 | |
| 989 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 990 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 991 | #endif |
| 992 | |
| 993 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 994 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 995 | #endif |
| 996 | |
| 997 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 998 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 999 | # define DEBUG_JOBS 1 |
| 1000 | #else |
| 1001 | # define DEBUG_JOBS 0 |
| 1002 | #endif |
| 1003 | |
| 1004 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1005 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1006 | # define DEBUG_EXPAND 1 |
| 1007 | #else |
| 1008 | # define DEBUG_EXPAND 0 |
| 1009 | #endif |
| 1010 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1011 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1012 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1013 | #endif |
| 1014 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1015 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1016 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1017 | # define DEBUG_GLOB 1 |
| 1018 | #else |
| 1019 | # define DEBUG_GLOB 0 |
| 1020 | #endif |
| 1021 | |
| 1022 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1023 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1024 | #endif |
| 1025 | |
| 1026 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1027 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1028 | #endif |
| 1029 | |
| 1030 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1031 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1032 | # define DEBUG_CLEAN 1 |
| 1033 | #else |
| 1034 | # define DEBUG_CLEAN 0 |
| 1035 | #endif |
| 1036 | |
| 1037 | #if DEBUG_EXPAND |
| 1038 | static void debug_print_strings(const char *prefix, char **vv) |
| 1039 | { |
| 1040 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1041 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1042 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1043 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1044 | } |
| 1045 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1046 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1047 | #endif |
| 1048 | |
| 1049 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1050 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1051 | */ |
| 1052 | #if LEAK_HUNTING |
| 1053 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1054 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1055 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1056 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1057 | return ptr; |
| 1058 | } |
| 1059 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1060 | { |
| 1061 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1062 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1063 | return ptr; |
| 1064 | } |
| 1065 | static char *xxstrdup(int lineno, const char *str) |
| 1066 | { |
| 1067 | char *ptr = xstrdup(str); |
| 1068 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1069 | return ptr; |
| 1070 | } |
| 1071 | static void xxfree(void *ptr) |
| 1072 | { |
| 1073 | fdprintf(2, "free %p\n", ptr); |
| 1074 | free(ptr); |
| 1075 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1076 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1077 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1078 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1079 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1080 | #endif |
| 1081 | |
| 1082 | |
| 1083 | /* Syntax and runtime errors. They always abort scripts. |
| 1084 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1085 | * and return to the prompt. |
| 1086 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1087 | */ |
| 1088 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1089 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1090 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1091 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1092 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1093 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1094 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1095 | #endif |
| 1096 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1097 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1098 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1099 | va_list p; |
| 1100 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1101 | #if HUSH_DEBUG >= 2 |
| 1102 | bb_error_msg("hush.c:%u", lineno); |
| 1103 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1104 | va_start(p, fmt); |
| 1105 | bb_verror_msg(fmt, p, NULL); |
| 1106 | va_end(p); |
| 1107 | if (!G_interactive_fd) |
| 1108 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1109 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1110 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1111 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1112 | { |
| 1113 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1114 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1115 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1116 | bb_error_msg("syntax error"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1119 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1120 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1121 | bb_error_msg("syntax error at '%s'", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1124 | 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] | 1125 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1126 | bb_error_msg("syntax error: unterminated %s", s); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1127 | } |
| 1128 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1129 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1130 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1131 | char msg[2] = { ch, '\0' }; |
| 1132 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1135 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1136 | { |
| 1137 | char msg[2]; |
| 1138 | msg[0] = ch; |
| 1139 | msg[1] = '\0'; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1140 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1143 | #if HUSH_DEBUG < 2 |
| 1144 | # undef die_if_script |
| 1145 | # undef syntax_error |
| 1146 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1147 | # undef syntax_error_unterm_ch |
| 1148 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1149 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1150 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1151 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1152 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1153 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1154 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1155 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1156 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1157 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1158 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1159 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1160 | #if ENABLE_HUSH_INTERACTIVE |
| 1161 | static void cmdedit_update_prompt(void); |
| 1162 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1163 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1164 | #endif |
| 1165 | |
| 1166 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1167 | /* Utility functions |
| 1168 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1169 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1170 | static char *unbackslash(char *src) |
| 1171 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1172 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1173 | while (1) { |
| 1174 | if (*src == '\\') |
| 1175 | src++; |
| 1176 | if ((*dst++ = *src++) == '\0') |
| 1177 | break; |
| 1178 | } |
| 1179 | return dst; |
| 1180 | } |
| 1181 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1182 | 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] | 1183 | { |
| 1184 | int i; |
| 1185 | unsigned count1; |
| 1186 | unsigned count2; |
| 1187 | char **v; |
| 1188 | |
| 1189 | v = strings; |
| 1190 | count1 = 0; |
| 1191 | if (v) { |
| 1192 | while (*v) { |
| 1193 | count1++; |
| 1194 | v++; |
| 1195 | } |
| 1196 | } |
| 1197 | count2 = 0; |
| 1198 | v = add; |
| 1199 | while (*v) { |
| 1200 | count2++; |
| 1201 | v++; |
| 1202 | } |
| 1203 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1204 | v[count1 + count2] = NULL; |
| 1205 | i = count2; |
| 1206 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1207 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1208 | return v; |
| 1209 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1210 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1211 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1212 | { |
| 1213 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1214 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1215 | return ptr; |
| 1216 | } |
| 1217 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1218 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1219 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1220 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1221 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1222 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1223 | { |
| 1224 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1225 | v[0] = add; |
| 1226 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1227 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1228 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1229 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1230 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1231 | { |
| 1232 | char **ptr = add_string_to_strings(strings, add); |
| 1233 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1234 | return ptr; |
| 1235 | } |
| 1236 | #define add_string_to_strings(strings, add) \ |
| 1237 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1238 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1239 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1240 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1241 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1242 | char **v; |
| 1243 | |
| 1244 | if (!strings) |
| 1245 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1246 | v = strings; |
| 1247 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1248 | free(*v); |
| 1249 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1250 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1251 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1254 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1255 | /* Helpers for setting new $n and restoring them back |
| 1256 | */ |
| 1257 | typedef struct save_arg_t { |
| 1258 | char *sv_argv0; |
| 1259 | char **sv_g_argv; |
| 1260 | int sv_g_argc; |
| 1261 | smallint sv_g_malloced; |
| 1262 | } save_arg_t; |
| 1263 | |
| 1264 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1265 | { |
| 1266 | int n; |
| 1267 | |
| 1268 | sv->sv_argv0 = argv[0]; |
| 1269 | sv->sv_g_argv = G.global_argv; |
| 1270 | sv->sv_g_argc = G.global_argc; |
| 1271 | sv->sv_g_malloced = G.global_args_malloced; |
| 1272 | |
| 1273 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1274 | G.global_argv = argv; |
| 1275 | G.global_args_malloced = 0; |
| 1276 | |
| 1277 | n = 1; |
| 1278 | while (*++argv) |
| 1279 | n++; |
| 1280 | G.global_argc = n; |
| 1281 | } |
| 1282 | |
| 1283 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1284 | { |
| 1285 | char **pp; |
| 1286 | |
| 1287 | if (G.global_args_malloced) { |
| 1288 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1289 | pp = G.global_argv; |
| 1290 | while (*++pp) /* note: does not free $0 */ |
| 1291 | free(*pp); |
| 1292 | free(G.global_argv); |
| 1293 | } |
| 1294 | argv[0] = sv->sv_argv0; |
| 1295 | G.global_argv = sv->sv_g_argv; |
| 1296 | G.global_argc = sv->sv_g_argc; |
| 1297 | G.global_args_malloced = sv->sv_g_malloced; |
| 1298 | } |
| 1299 | |
| 1300 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1301 | /* Basic theory of signal handling in shell |
| 1302 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1303 | * This does not describe what hush does, rather, it is current understanding |
| 1304 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1305 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1306 | * |
| 1307 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1308 | * is finished or backgrounded. It is the same in interactive and |
| 1309 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1310 | * 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] | 1311 | * ^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] | 1312 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1313 | * pipe. |
| 1314 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1315 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1316 | * or by SIGINT in interactive shell. |
| 1317 | * |
| 1318 | * Trap handlers will execute even within trap handlers. (right?) |
| 1319 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1320 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1321 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1322 | * |
| 1323 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1324 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1325 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1326 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1327 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1328 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1329 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1330 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1331 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1332 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1333 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1334 | * |
| 1335 | * SIGQUIT: ignore |
| 1336 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1337 | * SIGHUP (interactive): |
| 1338 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1339 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1340 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1341 | * that all pipe members are stopped. Try this in bash: |
| 1342 | * while :; do :; done - ^Z does not background it |
| 1343 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1344 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1345 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1346 | * to interactive shell while shell is waiting for a pipe, |
| 1347 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1348 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1349 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1350 | * Example 2: this does not wait and does not execute ls: |
| 1351 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1352 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1353 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1354 | * Example 4: this does not wait and does not execute ls: |
| 1355 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1356 | * |
| 1357 | * (What happens to signals which are IGN on shell start?) |
| 1358 | * (What happens with signal mask on shell start?) |
| 1359 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1360 | * Old implementation |
| 1361 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1362 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1363 | * We block all signals which we don't want to take action immediately, |
| 1364 | * i.e. we block all signals which need to have special handling as described |
| 1365 | * above, and all signals which have traps set. |
| 1366 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1367 | * and act on them. |
| 1368 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1369 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1370 | * sigset_t blocked_set: current blocked signal set |
| 1371 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1372 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1373 | * 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] | 1374 | * "trap 'cmd' SIGxxx": |
| 1375 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1376 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1377 | * unblock signals with special interactive handling |
| 1378 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1379 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1380 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1381 | * 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] | 1382 | * 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] | 1383 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1384 | * 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] | 1385 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1386 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1387 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1388 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1389 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1390 | * 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] | 1391 | * 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] | 1392 | * |
| 1393 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1394 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1395 | * masked signals are not visible! |
| 1396 | * |
| 1397 | * New implementation |
| 1398 | * ================== |
| 1399 | * We record each signal we are interested in by installing signal handler |
| 1400 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1401 | * We are interested in: signals which need to have special handling |
| 1402 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1403 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1404 | * After each pipe execution, we extract any pending signals |
| 1405 | * and act on them. |
| 1406 | * |
| 1407 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1408 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1409 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1410 | * sigset_t pending_set: set of sigs we received. |
| 1411 | * |
| 1412 | * "trap - SIGxxx": |
| 1413 | * if sig is in special_sig_mask, set handler back to: |
| 1414 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1415 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1416 | * else: set handler back to SIG_DFL |
| 1417 | * "trap 'cmd' SIGxxx": |
| 1418 | * set handler to record_pending_signo. |
| 1419 | * "trap '' SIGxxx": |
| 1420 | * set handler to SIG_IGN. |
| 1421 | * after [v]fork, if we plan to be a shell: |
| 1422 | * set signals with special interactive handling to SIG_DFL |
| 1423 | * (because child shell is not interactive), |
| 1424 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1425 | * after [v]fork, if we plan to exec: |
| 1426 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1427 | * |
| 1428 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1429 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1430 | * |
| 1431 | * Note (compat): |
| 1432 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1433 | * are set to the default actions". bash interprets it so that traps which |
| 1434 | * 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] | 1435 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1436 | enum { |
| 1437 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1438 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1439 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1440 | | (1 << SIGHUP) |
| 1441 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1442 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1443 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1444 | | (1 << SIGTTIN) |
| 1445 | | (1 << SIGTTOU) |
| 1446 | | (1 << SIGTSTP) |
| 1447 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1448 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1449 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1450 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1451 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1452 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1453 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1454 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1455 | if (sig == SIGCHLD) { |
| 1456 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1457 | //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] | 1458 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1459 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1460 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1461 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1462 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1463 | { |
| 1464 | struct sigaction old_sa; |
| 1465 | |
| 1466 | /* We could use signal() to install handlers... almost: |
| 1467 | * except that we need to mask ALL signals while handlers run. |
| 1468 | * I saw signal nesting in strace, race window isn't small. |
| 1469 | * SA_RESTART is also needed, but in Linux, signal() |
| 1470 | * sets SA_RESTART too. |
| 1471 | */ |
| 1472 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1473 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1474 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1475 | G.sa.sa_handler = handler; |
| 1476 | sigaction(sig, &G.sa, &old_sa); |
| 1477 | return old_sa.sa_handler; |
| 1478 | } |
| 1479 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1480 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1481 | |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 1482 | static void xfunc_has_died(void); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1483 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 1484 | # define disable_restore_tty_pgrp_on_exit() (die_func = NULL) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1485 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 1486 | # define enable_restore_tty_pgrp_on_exit() (die_func = xfunc_has_died) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1487 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1488 | /* Restores tty foreground process group, and exits. |
| 1489 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1490 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1491 | * or called directly with -EXITCODE. |
| 1492 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1493 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1494 | static void sigexit(int sig) |
| 1495 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1496 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1497 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1498 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1499 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1500 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1501 | */ |
| 1502 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1503 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1504 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1505 | |
| 1506 | /* Not a signal, just exit */ |
| 1507 | if (sig <= 0) |
| 1508 | _exit(- sig); |
| 1509 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1510 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1511 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1512 | #else |
| 1513 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1514 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1515 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1516 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1517 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1518 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1519 | static sighandler_t pick_sighandler(unsigned sig) |
| 1520 | { |
| 1521 | sighandler_t handler = SIG_DFL; |
| 1522 | if (sig < sizeof(unsigned)*8) { |
| 1523 | unsigned sigmask = (1 << sig); |
| 1524 | |
| 1525 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1526 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1527 | if (G_fatal_sig_mask & sigmask) |
| 1528 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1529 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1530 | #endif |
| 1531 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1532 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1533 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1534 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1535 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1536 | * in an endless loop when we try to do some |
| 1537 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1538 | */ |
| 1539 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1540 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1541 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1542 | } |
| 1543 | return handler; |
| 1544 | } |
| 1545 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1546 | /* Restores tty foreground process group, and exits. */ |
| 1547 | static void hush_exit(int exitcode) NORETURN; |
| 1548 | static void hush_exit(int exitcode) |
| 1549 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1550 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1551 | save_history(G.line_input_state); |
| 1552 | #endif |
| 1553 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1554 | fflush_all(); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1555 | 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] | 1556 | char *argv[3]; |
| 1557 | /* argv[0] is unused */ |
| 1558 | argv[1] = G.traps[0]; |
| 1559 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1560 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1561 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1562 | * "trap" will still show it, if executed |
| 1563 | * in the handler */ |
| 1564 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1565 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1566 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1567 | #if ENABLE_FEATURE_CLEAN_UP |
| 1568 | { |
| 1569 | struct variable *cur_var; |
| 1570 | if (G.cwd != bb_msg_unknown) |
| 1571 | free((char*)G.cwd); |
| 1572 | cur_var = G.top_var; |
| 1573 | while (cur_var) { |
| 1574 | struct variable *tmp = cur_var; |
| 1575 | if (!cur_var->max_len) |
| 1576 | free(cur_var->varstr); |
| 1577 | cur_var = cur_var->next; |
| 1578 | free(tmp); |
| 1579 | } |
| 1580 | } |
| 1581 | #endif |
| 1582 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1583 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1584 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1585 | sigexit(- (exitcode & 0xff)); |
| 1586 | #else |
| 1587 | exit(exitcode); |
| 1588 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 1591 | static void xfunc_has_died(void) NORETURN; |
| 1592 | static void xfunc_has_died(void) |
| 1593 | { |
| 1594 | /* xfunc has failed! die die die */ |
| 1595 | /* no EXIT traps, this is an escape hatch! */ |
| 1596 | G.exiting = 1; |
| 1597 | hush_exit(xfunc_error_retval); |
| 1598 | } |
| 1599 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1600 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1601 | //TODO: return a mask of ALL handled sigs? |
| 1602 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1603 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1604 | int last_sig = 0; |
| 1605 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1606 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1607 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1608 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1609 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1610 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1611 | sig = 0; |
| 1612 | do { |
| 1613 | sig++; |
| 1614 | if (sigismember(&G.pending_set, sig)) { |
| 1615 | sigdelset(&G.pending_set, sig); |
| 1616 | goto got_sig; |
| 1617 | } |
| 1618 | } while (sig < NSIG); |
| 1619 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1620 | got_sig: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1621 | if (G.traps && G.traps[sig]) { |
| 1622 | if (G.traps[sig][0]) { |
| 1623 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1624 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1625 | char *argv[3]; |
| 1626 | /* argv[0] is unused */ |
| 1627 | argv[1] = G.traps[sig]; |
| 1628 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1629 | save_rcode = G.last_exitcode; |
| 1630 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1631 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1632 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1633 | } /* else: "" trap, ignoring signal */ |
| 1634 | continue; |
| 1635 | } |
| 1636 | /* not a trap: special action */ |
| 1637 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1638 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1639 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1640 | bb_putchar('\n'); |
| 1641 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1642 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | #if ENABLE_HUSH_JOB |
| 1645 | case SIGHUP: { |
| 1646 | struct pipe *job; |
| 1647 | /* bash is observed to signal whole process groups, |
| 1648 | * not individual processes */ |
| 1649 | for (job = G.job_list; job; job = job->next) { |
| 1650 | if (job->pgrp <= 0) |
| 1651 | continue; |
| 1652 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1653 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1654 | kill(- job->pgrp, SIGCONT); |
| 1655 | } |
| 1656 | sigexit(SIGHUP); |
| 1657 | } |
| 1658 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1659 | #if ENABLE_HUSH_FAST |
| 1660 | case SIGCHLD: |
| 1661 | G.count_SIGCHLD++; |
| 1662 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1663 | /* Note: |
| 1664 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1665 | * This simplifies wait builtin a bit. |
| 1666 | */ |
| 1667 | break; |
| 1668 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1669 | default: /* ignored: */ |
| 1670 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1671 | /* Note: |
| 1672 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1673 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1674 | * in interactive shell, because TERM is ignored. |
| 1675 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | return last_sig; |
| 1680 | } |
| 1681 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1682 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1683 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1684 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1685 | if (force || G.cwd == NULL) { |
| 1686 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1687 | * we must not try to free(bb_msg_unknown) */ |
| 1688 | if (G.cwd == bb_msg_unknown) |
| 1689 | G.cwd = NULL; |
| 1690 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1691 | if (!G.cwd) |
| 1692 | G.cwd = bb_msg_unknown; |
| 1693 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1694 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1697 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1698 | /* |
| 1699 | * Shell and environment variable support |
| 1700 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1701 | 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] | 1702 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1703 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1704 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1705 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1706 | pp = &G.top_var; |
| 1707 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1708 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1709 | return pp; |
| 1710 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1711 | } |
| 1712 | return NULL; |
| 1713 | } |
| 1714 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1715 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1716 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1717 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1718 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1719 | |
| 1720 | if (G.expanded_assignments) { |
| 1721 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1722 | while (*cpp) { |
| 1723 | char *cp = *cpp; |
| 1724 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1725 | return cp + len + 1; |
| 1726 | cpp++; |
| 1727 | } |
| 1728 | } |
| 1729 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1730 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1731 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1732 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1733 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1734 | if (strcmp(name, "PPID") == 0) |
| 1735 | return utoa(G.root_ppid); |
| 1736 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1737 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1738 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1739 | return utoa(next_random(&G.random_gen)); |
| 1740 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1741 | return NULL; |
| 1742 | } |
| 1743 | |
| 1744 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1745 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1746 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1747 | * 0: do not change export flag |
| 1748 | * (if creating new variable, flag will be 0) |
| 1749 | * 1: set export flag and putenv the variable |
| 1750 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1751 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1752 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1753 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1754 | /* all params are used */ |
| 1755 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1756 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1757 | set_local_var(str, flg_export, local_lvl) |
| 1758 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1759 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1760 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1761 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1762 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1763 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1764 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1765 | 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] | 1766 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1767 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1768 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1769 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1770 | int name_len; |
| 1771 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1772 | eq_sign = strchr(str, '='); |
| 1773 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1774 | free(str); |
| 1775 | return -1; |
| 1776 | } |
| 1777 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1778 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1779 | var_pp = &G.top_var; |
| 1780 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1781 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1782 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1783 | continue; |
| 1784 | } |
| 1785 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1786 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1787 | #if !BB_MMU |
| 1788 | if (!flg_read_only) |
| 1789 | #endif |
| 1790 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1791 | free(str); |
| 1792 | return -1; |
| 1793 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1794 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1795 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1796 | *eq_sign = '\0'; |
| 1797 | unsetenv(str); |
| 1798 | *eq_sign = '='; |
| 1799 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1800 | #if ENABLE_HUSH_LOCAL |
| 1801 | if (cur->func_nest_level < local_lvl) { |
| 1802 | /* New variable is declared as local, |
| 1803 | * and existing one is global, or local |
| 1804 | * from enclosing function. |
| 1805 | * Remove and save old one: */ |
| 1806 | *var_pp = cur->next; |
| 1807 | cur->next = *G.shadowed_vars_pp; |
| 1808 | *G.shadowed_vars_pp = cur; |
| 1809 | /* bash 3.2.33(1) and exported vars: |
| 1810 | * # export z=z |
| 1811 | * # f() { local z=a; env | grep ^z; } |
| 1812 | * # f |
| 1813 | * z=a |
| 1814 | * # env | grep ^z |
| 1815 | * z=z |
| 1816 | */ |
| 1817 | if (cur->flg_export) |
| 1818 | flg_export = 1; |
| 1819 | break; |
| 1820 | } |
| 1821 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1822 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1823 | free_and_exp: |
| 1824 | free(str); |
| 1825 | goto exp; |
| 1826 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1827 | if (cur->max_len != 0) { |
| 1828 | if (cur->max_len >= strlen(str)) { |
| 1829 | /* This one is from startup env, reuse space */ |
| 1830 | strcpy(cur->varstr, str); |
| 1831 | goto free_and_exp; |
| 1832 | } |
| 1833 | } else { |
| 1834 | /* max_len == 0 signifies "malloced" var, which we can |
| 1835 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1836 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1837 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1838 | cur->max_len = 0; |
| 1839 | goto set_str_and_exp; |
| 1840 | } |
| 1841 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1842 | /* Not found - create new variable struct */ |
| 1843 | cur = xzalloc(sizeof(*cur)); |
| 1844 | #if ENABLE_HUSH_LOCAL |
| 1845 | cur->func_nest_level = local_lvl; |
| 1846 | #endif |
| 1847 | cur->next = *var_pp; |
| 1848 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1849 | |
| 1850 | set_str_and_exp: |
| 1851 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1852 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1853 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1854 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1855 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1856 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1857 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1858 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1859 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1860 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1861 | if (flg_export == -1) { |
| 1862 | cur->flg_export = 0; |
| 1863 | /* unsetenv was already done */ |
| 1864 | } else { |
| 1865 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1866 | return putenv(cur->varstr); |
| 1867 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1868 | } |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1872 | /* Used at startup and after each cd */ |
| 1873 | static void set_pwd_var(int exp) |
| 1874 | { |
| 1875 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1876 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1877 | } |
| 1878 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1879 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1880 | { |
| 1881 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1882 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1883 | |
| 1884 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1885 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1886 | var_pp = &G.top_var; |
| 1887 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1888 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1889 | if (cur->flg_read_only) { |
| 1890 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1891 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1892 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1893 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1894 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1895 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1896 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1897 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1898 | if (!cur->max_len) |
| 1899 | free(cur->varstr); |
| 1900 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1901 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1902 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1903 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1904 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1905 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1908 | static int unset_local_var(const char *name) |
| 1909 | { |
| 1910 | return unset_local_var_len(name, strlen(name)); |
| 1911 | } |
| 1912 | |
| 1913 | static void unset_vars(char **strings) |
| 1914 | { |
| 1915 | char **v; |
| 1916 | |
| 1917 | if (!strings) |
| 1918 | return; |
| 1919 | v = strings; |
| 1920 | while (*v) { |
| 1921 | const char *eq = strchrnul(*v, '='); |
| 1922 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1923 | v++; |
| 1924 | } |
| 1925 | free(strings); |
| 1926 | } |
| 1927 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1928 | 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] | 1929 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1930 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1931 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1932 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1933 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1934 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1935 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1936 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1937 | */ |
| 1938 | static void add_vars(struct variable *var) |
| 1939 | { |
| 1940 | struct variable *next; |
| 1941 | |
| 1942 | while (var) { |
| 1943 | next = var->next; |
| 1944 | var->next = G.top_var; |
| 1945 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1946 | if (var->flg_export) { |
| 1947 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1948 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1949 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1950 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1951 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1952 | var = next; |
| 1953 | } |
| 1954 | } |
| 1955 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1956 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1957 | { |
| 1958 | char **s; |
| 1959 | struct variable *old = NULL; |
| 1960 | |
| 1961 | if (!strings) |
| 1962 | return old; |
| 1963 | s = strings; |
| 1964 | while (*s) { |
| 1965 | struct variable *var_p; |
| 1966 | struct variable **var_pp; |
| 1967 | char *eq; |
| 1968 | |
| 1969 | eq = strchr(*s, '='); |
| 1970 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1971 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1972 | if (var_pp) { |
| 1973 | /* Remove variable from global linked list */ |
| 1974 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1975 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1976 | *var_pp = var_p->next; |
| 1977 | /* Add it to returned list */ |
| 1978 | var_p->next = old; |
| 1979 | old = var_p; |
| 1980 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1981 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1982 | } |
| 1983 | s++; |
| 1984 | } |
| 1985 | return old; |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 1990 | * Unicode helper |
| 1991 | */ |
| 1992 | static void reinit_unicode_for_hush(void) |
| 1993 | { |
| 1994 | /* Unicode support should be activated even if LANG is set |
| 1995 | * _during_ shell execution, not only if it was set when |
| 1996 | * shell was started. Therefore, re-check LANG every time: |
| 1997 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 1998 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 1999 | || ENABLE_UNICODE_USING_LOCALE |
| 2000 | ) { |
| 2001 | const char *s = get_local_var_value("LC_ALL"); |
| 2002 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2003 | if (!s) s = get_local_var_value("LANG"); |
| 2004 | reinit_unicode(s); |
| 2005 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2010 | * in_str support |
| 2011 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2012 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2013 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2014 | int ch = *i->p; |
| 2015 | if (ch != '\0') { |
| 2016 | i->p++; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2017 | i->last_char = ch; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2018 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2019 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2020 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2023 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2024 | { |
| 2025 | return *i->p; |
| 2026 | } |
| 2027 | |
| 2028 | #if ENABLE_HUSH_INTERACTIVE |
| 2029 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2030 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2031 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2032 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2033 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2034 | if (G.PS1 == NULL) |
| 2035 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2036 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2037 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2038 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2039 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2040 | if (G.PS2 == NULL) |
| 2041 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2042 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2043 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2044 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2045 | { |
| 2046 | const char *prompt_str; |
| 2047 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2048 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2049 | /* Set up the prompt */ |
| 2050 | if (promptmode == 0) { /* PS1 */ |
| 2051 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2052 | /* bash uses $PWD value, even if it is set by user. |
| 2053 | * It uses current dir only if PWD is unset. |
| 2054 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2055 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2056 | prompt_str = G.PS1; |
| 2057 | } else |
| 2058 | prompt_str = G.PS2; |
| 2059 | } else |
| 2060 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2061 | debug_printf("result '%s'\n", prompt_str); |
| 2062 | return prompt_str; |
| 2063 | } |
| 2064 | |
| 2065 | static void get_user_input(struct in_str *i) |
| 2066 | { |
| 2067 | int r; |
| 2068 | const char *prompt_str; |
| 2069 | |
| 2070 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2071 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2072 | /* Enable command line editing only while a command line |
| 2073 | * is actually being read */ |
| 2074 | do { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2075 | reinit_unicode_for_hush(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2076 | G.flag_SIGINT = 0; |
| 2077 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 2078 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 2079 | 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] | 2080 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2081 | check_and_run_traps(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2082 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2083 | i->eof_flag = (r < 0); |
| 2084 | if (i->eof_flag) { /* EOF/error detected */ |
| 2085 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 2086 | G.user_input_buf[1] = '\0'; |
| 2087 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2088 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2089 | do { |
| 2090 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2091 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2092 | /* Why check_and_run_traps here? Try this interactively: |
| 2093 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2094 | * $ <[enter], repeatedly...> |
| 2095 | * Without check_and_run_traps, handler never runs. |
| 2096 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2097 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2098 | fputs(prompt_str, stdout); |
| 2099 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2100 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2101 | G.user_input_buf[0] = r = fgetc(i->file); |
| 2102 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2103 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2104 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2105 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2106 | i->p = G.user_input_buf; |
| 2107 | } |
| 2108 | |
| 2109 | #endif /* INTERACTIVE */ |
| 2110 | |
| 2111 | /* This is the magic location that prints prompts |
| 2112 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2113 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2114 | { |
| 2115 | int ch; |
| 2116 | |
| 2117 | /* If there is data waiting, eat it up */ |
| 2118 | if (i->p && *i->p) { |
| 2119 | #if ENABLE_HUSH_INTERACTIVE |
| 2120 | take_cached: |
| 2121 | #endif |
| 2122 | ch = *i->p++; |
| 2123 | if (i->eof_flag && !*i->p) |
| 2124 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2125 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2126 | } else { |
| 2127 | /* need to double check i->file because we might be doing something |
| 2128 | * more complicated by now, like sourcing or substituting. */ |
| 2129 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2130 | if (G_interactive_fd && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2131 | do { |
| 2132 | get_user_input(i); |
| 2133 | } while (!*i->p); /* need non-empty line */ |
| 2134 | i->promptmode = 1; /* PS2 */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2135 | goto take_cached; |
| 2136 | } |
| 2137 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2138 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2139 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2140 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2141 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2142 | return ch; |
| 2143 | } |
| 2144 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2145 | /* All callers guarantee this routine will never |
| 2146 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2147 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2148 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2149 | { |
| 2150 | int ch; |
| 2151 | if (i->p && *i->p) { |
| 2152 | if (i->eof_flag && !i->p[1]) |
| 2153 | return EOF; |
| 2154 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2155 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2156 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2157 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2158 | i->eof_flag = (ch == EOF); |
| 2159 | i->peek_buf[0] = ch; |
| 2160 | i->peek_buf[1] = '\0'; |
| 2161 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2162 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2163 | return ch; |
| 2164 | } |
| 2165 | |
| 2166 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2167 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2168 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2169 | i->peek = file_peek; |
| 2170 | i->get = file_get; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2171 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2172 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2173 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2177 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2178 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2179 | i->peek = static_peek; |
| 2180 | i->get = static_get; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2181 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2182 | i->p = s; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2183 | /* i->eof_flag = 0; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2187 | /* |
| 2188 | * o_string support |
| 2189 | */ |
| 2190 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2191 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2192 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2193 | { |
| 2194 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2195 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2196 | if (o->data) |
| 2197 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2200 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2201 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2202 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2203 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2206 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2207 | { |
| 2208 | free(o->data); |
| 2209 | } |
| 2210 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2211 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2212 | { |
| 2213 | if (o->length + len > o->maxlen) { |
| 2214 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 2215 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2216 | } |
| 2217 | } |
| 2218 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2219 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2220 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2221 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 2222 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2223 | o->data[o->length] = ch; |
| 2224 | o->length++; |
| 2225 | o->data[o->length] = '\0'; |
| 2226 | } |
| 2227 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2228 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2229 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2230 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2231 | memcpy(&o->data[o->length], str, len); |
| 2232 | o->length += len; |
| 2233 | o->data[o->length] = '\0'; |
| 2234 | } |
| 2235 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2236 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2237 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2238 | o_addblock(o, str, strlen(str)); |
| 2239 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2240 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2241 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2242 | static void nommu_addchr(o_string *o, int ch) |
| 2243 | { |
| 2244 | if (o) |
| 2245 | o_addchr(o, ch); |
| 2246 | } |
| 2247 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2248 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2249 | #endif |
| 2250 | |
| 2251 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2252 | { |
| 2253 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2256 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2257 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2258 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2259 | * Apparently, on unquoted $v bash still does globbing |
| 2260 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2261 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2262 | * quoting mechanisms on $v expansion side: one protects |
| 2263 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2264 | * We have only second one. |
| 2265 | */ |
| 2266 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2267 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2268 | # define MAYBE_BRACES "{}" |
| 2269 | #else |
| 2270 | # define MAYBE_BRACES "" |
| 2271 | #endif |
| 2272 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2273 | /* My analysis of quoting semantics tells me that state information |
| 2274 | * is associated with a destination, not a source. |
| 2275 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2276 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2277 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2278 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2279 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2280 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2281 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2282 | o_grow_by(o, sz); |
| 2283 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2284 | o->data[o->length] = '\\'; |
| 2285 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2286 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2287 | o->data[o->length] = ch; |
| 2288 | o->length++; |
| 2289 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2292 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2293 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2294 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2295 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2296 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2297 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2298 | sz++; |
| 2299 | o->data[o->length] = '\\'; |
| 2300 | o->length++; |
| 2301 | } |
| 2302 | o_grow_by(o, sz); |
| 2303 | o->data[o->length] = ch; |
| 2304 | o->length++; |
| 2305 | o->data[o->length] = '\0'; |
| 2306 | } |
| 2307 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2308 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2309 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2310 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2311 | char ch; |
| 2312 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2313 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2314 | if (ordinary_cnt > len) /* paranoia */ |
| 2315 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2316 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2317 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2318 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2319 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2320 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2321 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2322 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2323 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2324 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2325 | sz++; |
| 2326 | o->data[o->length] = '\\'; |
| 2327 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2328 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2329 | o_grow_by(o, sz); |
| 2330 | o->data[o->length] = ch; |
| 2331 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2332 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2333 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2336 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2337 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2338 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2339 | o_addblock(o, str, len); |
| 2340 | return; |
| 2341 | } |
| 2342 | o_addqblock(o, str, len); |
| 2343 | } |
| 2344 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2345 | static void o_addQstr(o_string *o, const char *str) |
| 2346 | { |
| 2347 | o_addQblock(o, str, strlen(str)); |
| 2348 | } |
| 2349 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2350 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2351 | * 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] | 2352 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2353 | * list[i] contains an INDEX (int!) into this string data. |
| 2354 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2355 | * but list[i]'s need not be modified. |
| 2356 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2357 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2358 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2359 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2360 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2361 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2362 | { |
| 2363 | char **list = (char**)o->data; |
| 2364 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2365 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2366 | |
| 2367 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2368 | 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] | 2369 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2370 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2371 | o->has_quoted_part, |
| 2372 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2373 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2374 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2375 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2376 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2377 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2378 | i++; |
| 2379 | } |
| 2380 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2381 | 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] | 2382 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2383 | 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] | 2384 | } |
| 2385 | } |
| 2386 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2387 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2388 | #endif |
| 2389 | |
| 2390 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2391 | * in list[n] so that it points past last stored byte so far. |
| 2392 | * It returns n+1. */ |
| 2393 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2394 | { |
| 2395 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2396 | int string_start; |
| 2397 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2398 | |
| 2399 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2400 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2401 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2402 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2403 | 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] | 2404 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2405 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2406 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2407 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2408 | memmove(list + n + 0x10, list + n, string_len); |
| 2409 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2410 | } else { |
| 2411 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2412 | n, string_len, string_start); |
| 2413 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2414 | } else { |
| 2415 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2416 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2417 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2418 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2419 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2420 | o->has_empty_slot = 0; |
| 2421 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2422 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2423 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2424 | return n + 1; |
| 2425 | } |
| 2426 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2427 | /* "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] | 2428 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2429 | { |
| 2430 | char **list = (char**)o->data; |
| 2431 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2432 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2433 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2436 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2437 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2438 | * first, it processes even {a} (no commas), second, |
| 2439 | * 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] | 2440 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2441 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2442 | |
| 2443 | /* Helper */ |
| 2444 | static int glob_needed(const char *s) |
| 2445 | { |
| 2446 | while (*s) { |
| 2447 | if (*s == '\\') { |
| 2448 | if (!s[1]) |
| 2449 | return 0; |
| 2450 | s += 2; |
| 2451 | continue; |
| 2452 | } |
| 2453 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2454 | return 1; |
| 2455 | s++; |
| 2456 | } |
| 2457 | return 0; |
| 2458 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2459 | /* Return pointer to next closing brace or to comma */ |
| 2460 | static const char *next_brace_sub(const char *cp) |
| 2461 | { |
| 2462 | unsigned depth = 0; |
| 2463 | cp++; |
| 2464 | while (*cp != '\0') { |
| 2465 | if (*cp == '\\') { |
| 2466 | if (*++cp == '\0') |
| 2467 | break; |
| 2468 | cp++; |
| 2469 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2470 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2471 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2472 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2473 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2474 | depth++; |
| 2475 | } |
| 2476 | |
| 2477 | return *cp != '\0' ? cp : NULL; |
| 2478 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2479 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2480 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2481 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2482 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2483 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2484 | const char *next; |
| 2485 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2486 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2487 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2488 | |
| 2489 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2490 | |
| 2491 | begin = pattern; |
| 2492 | while (1) { |
| 2493 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2494 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2495 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2496 | /* Find the first sub-pattern and at the same time |
| 2497 | * find the rest after the closing brace */ |
| 2498 | next = next_brace_sub(begin); |
| 2499 | if (next == NULL) { |
| 2500 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2501 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2502 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2503 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2504 | /* "{abc}" with no commas - illegal |
| 2505 | * brace expr, disregard and skip it */ |
| 2506 | begin = next + 1; |
| 2507 | continue; |
| 2508 | } |
| 2509 | break; |
| 2510 | } |
| 2511 | if (*begin == '\\' && begin[1] != '\0') |
| 2512 | begin++; |
| 2513 | begin++; |
| 2514 | } |
| 2515 | debug_printf_glob("begin:%s\n", begin); |
| 2516 | debug_printf_glob("next:%s\n", next); |
| 2517 | |
| 2518 | /* Now find the end of the whole brace expression */ |
| 2519 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2520 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2521 | rest = next_brace_sub(rest); |
| 2522 | if (rest == NULL) { |
| 2523 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2524 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2525 | } |
| 2526 | debug_printf_glob("rest:%s\n", rest); |
| 2527 | } |
| 2528 | rest_len = strlen(++rest) + 1; |
| 2529 | |
| 2530 | /* We are sure the brace expression is well-formed */ |
| 2531 | |
| 2532 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2533 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2534 | |
| 2535 | /* We have a brace expression. BEGIN points to the opening {, |
| 2536 | * NEXT points past the terminator of the first element, and REST |
| 2537 | * points past the final }. We will accumulate result names from |
| 2538 | * recursive runs for each brace alternative in the buffer using |
| 2539 | * GLOB_APPEND. */ |
| 2540 | |
| 2541 | p = begin + 1; |
| 2542 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2543 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2544 | memcpy( |
| 2545 | mempcpy( |
| 2546 | mempcpy(new_pattern_buf, |
| 2547 | /* We know the prefix for all sub-patterns */ |
| 2548 | pattern, begin - pattern), |
| 2549 | p, next - p), |
| 2550 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2551 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2552 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2553 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2554 | */ |
| 2555 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2556 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2557 | /* We saw the last entry */ |
| 2558 | break; |
| 2559 | } |
| 2560 | p = next + 1; |
| 2561 | next = next_brace_sub(next); |
| 2562 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2563 | free(new_pattern_buf); |
| 2564 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2565 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2566 | simple_glob: |
| 2567 | { |
| 2568 | int gr; |
| 2569 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2570 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2571 | memset(&globdata, 0, sizeof(globdata)); |
| 2572 | gr = glob(pattern, 0, NULL, &globdata); |
| 2573 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2574 | if (gr != 0) { |
| 2575 | if (gr == GLOB_NOMATCH) { |
| 2576 | globfree(&globdata); |
| 2577 | /* NB: garbles parameter */ |
| 2578 | unbackslash(pattern); |
| 2579 | o_addstr_with_NUL(o, pattern); |
| 2580 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2581 | return o_save_ptr_helper(o, n); |
| 2582 | } |
| 2583 | if (gr == GLOB_NOSPACE) |
| 2584 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2585 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2586 | * but we didn't specify it. Paranoia again. */ |
| 2587 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2588 | } |
| 2589 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2590 | char **argv = globdata.gl_pathv; |
| 2591 | while (1) { |
| 2592 | o_addstr_with_NUL(o, *argv); |
| 2593 | n = o_save_ptr_helper(o, n); |
| 2594 | argv++; |
| 2595 | if (!*argv) |
| 2596 | break; |
| 2597 | } |
| 2598 | } |
| 2599 | globfree(&globdata); |
| 2600 | } |
| 2601 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2602 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2603 | /* Performs globbing on last list[], |
| 2604 | * saving each result as a new list[]. |
| 2605 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2606 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2607 | { |
| 2608 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2609 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2610 | 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] | 2611 | if (!o->data) |
| 2612 | return o_save_ptr_helper(o, n); |
| 2613 | pattern = o->data + o_get_last_ptr(o, n); |
| 2614 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2615 | if (!glob_needed(pattern)) { |
| 2616 | /* unbackslash last string in o in place, fix length */ |
| 2617 | o->length = unbackslash(pattern) - o->data; |
| 2618 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2619 | return o_save_ptr_helper(o, n); |
| 2620 | } |
| 2621 | |
| 2622 | copy = xstrdup(pattern); |
| 2623 | /* "forget" pattern in o */ |
| 2624 | o->length = pattern - o->data; |
| 2625 | n = glob_brace(copy, o, n); |
| 2626 | free(copy); |
| 2627 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2628 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2629 | return n; |
| 2630 | } |
| 2631 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2632 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2633 | |
| 2634 | /* Helper */ |
| 2635 | static int glob_needed(const char *s) |
| 2636 | { |
| 2637 | while (*s) { |
| 2638 | if (*s == '\\') { |
| 2639 | if (!s[1]) |
| 2640 | return 0; |
| 2641 | s += 2; |
| 2642 | continue; |
| 2643 | } |
| 2644 | if (*s == '*' || *s == '[' || *s == '?') |
| 2645 | return 1; |
| 2646 | s++; |
| 2647 | } |
| 2648 | return 0; |
| 2649 | } |
| 2650 | /* Performs globbing on last list[], |
| 2651 | * saving each result as a new list[]. |
| 2652 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2653 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2654 | { |
| 2655 | glob_t globdata; |
| 2656 | int gr; |
| 2657 | char *pattern; |
| 2658 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2659 | 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] | 2660 | if (!o->data) |
| 2661 | return o_save_ptr_helper(o, n); |
| 2662 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2663 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2664 | if (!glob_needed(pattern)) { |
| 2665 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2666 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2667 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2668 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2669 | return o_save_ptr_helper(o, n); |
| 2670 | } |
| 2671 | |
| 2672 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2673 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2674 | * If we glob "*.\*" and don't find anything, we need |
| 2675 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2676 | * will return "*.\*"! |
| 2677 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2678 | gr = glob(pattern, 0, NULL, &globdata); |
| 2679 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2680 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2681 | if (gr == GLOB_NOMATCH) { |
| 2682 | globfree(&globdata); |
| 2683 | goto literal; |
| 2684 | } |
| 2685 | if (gr == GLOB_NOSPACE) |
| 2686 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2687 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2688 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2689 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2690 | } |
| 2691 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2692 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2693 | /* "forget" pattern in o */ |
| 2694 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2695 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2696 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2697 | n = o_save_ptr_helper(o, n); |
| 2698 | argv++; |
| 2699 | if (!*argv) |
| 2700 | break; |
| 2701 | } |
| 2702 | } |
| 2703 | globfree(&globdata); |
| 2704 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2705 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2706 | return n; |
| 2707 | } |
| 2708 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2709 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2710 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2711 | /* 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] | 2712 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2713 | static int o_save_ptr(o_string *o, int n) |
| 2714 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2715 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2716 | /* If o->has_empty_slot, list[n] was already globbed |
| 2717 | * (if it was requested back then when it was filled) |
| 2718 | * so don't do that again! */ |
| 2719 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2720 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2721 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2722 | return o_save_ptr_helper(o, n); |
| 2723 | } |
| 2724 | |
| 2725 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2726 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2727 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2728 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2729 | int string_start; |
| 2730 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2731 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2732 | if (DEBUG_EXPAND) |
| 2733 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2734 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2735 | list = (char**)o->data; |
| 2736 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2737 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2738 | while (n) { |
| 2739 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2740 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2741 | } |
| 2742 | return list; |
| 2743 | } |
| 2744 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2745 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2746 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2747 | /* Returns pi->next - next pipe in the list */ |
| 2748 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2749 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2750 | struct pipe *next; |
| 2751 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2752 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2753 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2754 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2755 | struct command *command; |
| 2756 | struct redir_struct *r, *rnext; |
| 2757 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2758 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2759 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2760 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2761 | if (DEBUG_CLEAN) { |
| 2762 | int a; |
| 2763 | char **p; |
| 2764 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2765 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2766 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2767 | } |
| 2768 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2769 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2770 | } |
| 2771 | /* not "else if": on syntax error, we may have both! */ |
| 2772 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2773 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2774 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2775 | free_pipe_list(command->group); |
| 2776 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2777 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2778 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2779 | /* else is crucial here. |
| 2780 | * If group != NULL, child_func is meaningless */ |
| 2781 | #if ENABLE_HUSH_FUNCTIONS |
| 2782 | else if (command->child_func) { |
| 2783 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2784 | command->child_func->parent_cmd = NULL; |
| 2785 | } |
| 2786 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2787 | #if !BB_MMU |
| 2788 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2789 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2790 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2791 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2792 | debug_printf_clean(" redirect %d%s", |
| 2793 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2794 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2795 | if (r->rd_filename) { |
| 2796 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2797 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2798 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2799 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2800 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2801 | rnext = r->next; |
| 2802 | free(r); |
| 2803 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2804 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2805 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2806 | 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] | 2807 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2808 | #if ENABLE_HUSH_JOB |
| 2809 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2810 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2811 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2812 | |
| 2813 | next = pi->next; |
| 2814 | free(pi); |
| 2815 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2816 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2817 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2818 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2819 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2820 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2821 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2822 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2823 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2824 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2825 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2826 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 2830 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2831 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2832 | #ifndef debug_print_tree |
| 2833 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2834 | { |
| 2835 | static const char *const PIPE[] = { |
| 2836 | [PIPE_SEQ] = "SEQ", |
| 2837 | [PIPE_AND] = "AND", |
| 2838 | [PIPE_OR ] = "OR" , |
| 2839 | [PIPE_BG ] = "BG" , |
| 2840 | }; |
| 2841 | static const char *RES[] = { |
| 2842 | [RES_NONE ] = "NONE" , |
| 2843 | # if ENABLE_HUSH_IF |
| 2844 | [RES_IF ] = "IF" , |
| 2845 | [RES_THEN ] = "THEN" , |
| 2846 | [RES_ELIF ] = "ELIF" , |
| 2847 | [RES_ELSE ] = "ELSE" , |
| 2848 | [RES_FI ] = "FI" , |
| 2849 | # endif |
| 2850 | # if ENABLE_HUSH_LOOPS |
| 2851 | [RES_FOR ] = "FOR" , |
| 2852 | [RES_WHILE] = "WHILE", |
| 2853 | [RES_UNTIL] = "UNTIL", |
| 2854 | [RES_DO ] = "DO" , |
| 2855 | [RES_DONE ] = "DONE" , |
| 2856 | # endif |
| 2857 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 2858 | [RES_IN ] = "IN" , |
| 2859 | # endif |
| 2860 | # if ENABLE_HUSH_CASE |
| 2861 | [RES_CASE ] = "CASE" , |
| 2862 | [RES_CASE_IN ] = "CASE_IN" , |
| 2863 | [RES_MATCH] = "MATCH", |
| 2864 | [RES_CASE_BODY] = "CASE_BODY", |
| 2865 | [RES_ESAC ] = "ESAC" , |
| 2866 | # endif |
| 2867 | [RES_XXXX ] = "XXXX" , |
| 2868 | [RES_SNTX ] = "SNTX" , |
| 2869 | }; |
| 2870 | static const char *const CMDTYPE[] = { |
| 2871 | "{}", |
| 2872 | "()", |
| 2873 | "[noglob]", |
| 2874 | # if ENABLE_HUSH_FUNCTIONS |
| 2875 | "func()", |
| 2876 | # endif |
| 2877 | }; |
| 2878 | |
| 2879 | int pin, prn; |
| 2880 | |
| 2881 | pin = 0; |
| 2882 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2883 | 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] | 2884 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 2885 | prn = 0; |
| 2886 | while (prn < pi->num_cmds) { |
| 2887 | struct command *command = &pi->cmds[prn]; |
| 2888 | char **argv = command->argv; |
| 2889 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2890 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2891 | lvl*2, "", prn, |
| 2892 | command->assignment_cnt); |
| 2893 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2894 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2895 | CMDTYPE[command->cmd_type], |
| 2896 | argv |
| 2897 | # if !BB_MMU |
| 2898 | , " group_as_string:", command->group_as_string |
| 2899 | # else |
| 2900 | , "", "" |
| 2901 | # endif |
| 2902 | ); |
| 2903 | debug_print_tree(command->group, lvl+1); |
| 2904 | prn++; |
| 2905 | continue; |
| 2906 | } |
| 2907 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2908 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2909 | argv++; |
| 2910 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2911 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2912 | prn++; |
| 2913 | } |
| 2914 | pi = pi->next; |
| 2915 | pin++; |
| 2916 | } |
| 2917 | } |
| 2918 | #endif /* debug_print_tree */ |
| 2919 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2920 | static struct pipe *new_pipe(void) |
| 2921 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2922 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2923 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2924 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2925 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2926 | return pi; |
| 2927 | } |
| 2928 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2929 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 2930 | * if ctx->command is NULL. |
| 2931 | * No errors possible here. |
| 2932 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2933 | static int done_command(struct parse_context *ctx) |
| 2934 | { |
| 2935 | /* The command is really already in the pipe structure, so |
| 2936 | * advance the pipe counter and make a new, null command. */ |
| 2937 | struct pipe *pi = ctx->pipe; |
| 2938 | struct command *command = ctx->command; |
| 2939 | |
| 2940 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2941 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2942 | 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] | 2943 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2944 | } |
| 2945 | pi->num_cmds++; |
| 2946 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2947 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2948 | } else { |
| 2949 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 2950 | } |
| 2951 | |
| 2952 | /* Only real trickiness here is that the uncommitted |
| 2953 | * command structure is not counted in pi->num_cmds. */ |
| 2954 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2955 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 2956 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2957 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2958 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 2959 | } |
| 2960 | |
| 2961 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 2962 | { |
| 2963 | int not_null; |
| 2964 | |
| 2965 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 2966 | /* Close previous command */ |
| 2967 | not_null = done_command(ctx); |
| 2968 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2969 | #if HAS_KEYWORDS |
| 2970 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 2971 | ctx->ctx_inverted = 0; |
| 2972 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 2973 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2974 | |
| 2975 | /* Without this check, even just <enter> on command line generates |
| 2976 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2977 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2978 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2979 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2980 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2981 | #endif |
| 2982 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2983 | || ctx->ctx_res_w == RES_DONE |
| 2984 | || ctx->ctx_res_w == RES_FOR |
| 2985 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2986 | #endif |
| 2987 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2988 | || ctx->ctx_res_w == RES_ESAC |
| 2989 | #endif |
| 2990 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2991 | struct pipe *new_p; |
| 2992 | debug_printf_parse("done_pipe: adding new pipe: " |
| 2993 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 2994 | not_null, ctx->ctx_res_w); |
| 2995 | new_p = new_pipe(); |
| 2996 | ctx->pipe->next = new_p; |
| 2997 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2998 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2999 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3000 | * This is used to control execution. |
| 3001 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3002 | * cases where variable or value happens to match a keyword): |
| 3003 | */ |
| 3004 | #if ENABLE_HUSH_LOOPS |
| 3005 | if (ctx->ctx_res_w == RES_FOR |
| 3006 | || ctx->ctx_res_w == RES_IN) |
| 3007 | ctx->ctx_res_w = RES_NONE; |
| 3008 | #endif |
| 3009 | #if ENABLE_HUSH_CASE |
| 3010 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3011 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3012 | if (ctx->ctx_res_w == RES_CASE) |
| 3013 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3014 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3015 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3016 | /* Create the memory for command, roughly: |
| 3017 | * ctx->pipe->cmds = new struct command; |
| 3018 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3019 | */ |
| 3020 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3021 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3022 | } |
| 3023 | debug_printf_parse("done_pipe return\n"); |
| 3024 | } |
| 3025 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3026 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3027 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3028 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3029 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3030 | /* Create the memory for command, roughly: |
| 3031 | * ctx->pipe->cmds = new struct command; |
| 3032 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3033 | */ |
| 3034 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3037 | /* If a reserved word is found and processed, parse context is modified |
| 3038 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3039 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3040 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3041 | struct reserved_combo { |
| 3042 | char literal[6]; |
| 3043 | unsigned char res; |
| 3044 | unsigned char assignment_flag; |
| 3045 | int flag; |
| 3046 | }; |
| 3047 | enum { |
| 3048 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3049 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3050 | FLAG_IF = (1 << RES_IF ), |
| 3051 | FLAG_THEN = (1 << RES_THEN ), |
| 3052 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3053 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3054 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3055 | # endif |
| 3056 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3057 | FLAG_FOR = (1 << RES_FOR ), |
| 3058 | FLAG_WHILE = (1 << RES_WHILE), |
| 3059 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3060 | FLAG_DO = (1 << RES_DO ), |
| 3061 | FLAG_DONE = (1 << RES_DONE ), |
| 3062 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3063 | # endif |
| 3064 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3065 | FLAG_MATCH = (1 << RES_MATCH), |
| 3066 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3067 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3068 | FLAG_START = (1 << RES_XXXX ), |
| 3069 | }; |
| 3070 | |
| 3071 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3072 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3073 | /* Mostly a list of accepted follow-up reserved words. |
| 3074 | * FLAG_END means we are done with the sequence, and are ready |
| 3075 | * to turn the compound list into a command. |
| 3076 | * FLAG_START means the word must start a new compound list. |
| 3077 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3078 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3079 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3080 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3081 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3082 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3083 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3084 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3085 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3086 | # endif |
| 3087 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3088 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3089 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3090 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3091 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3092 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3093 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3094 | # endif |
| 3095 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3096 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3097 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3098 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3099 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3100 | const struct reserved_combo *r; |
| 3101 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3102 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3103 | if (strcmp(word->data, r->literal) == 0) |
| 3104 | return r; |
| 3105 | } |
| 3106 | return NULL; |
| 3107 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3108 | /* Return 0: not a keyword, 1: keyword |
| 3109 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3110 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3111 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3112 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3113 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3114 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3115 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3116 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3117 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3118 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3119 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3120 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3121 | r = match_reserved_word(word); |
| 3122 | if (!r) |
| 3123 | return 0; |
| 3124 | |
| 3125 | 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] | 3126 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3127 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3128 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3129 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3130 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3131 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3132 | if (r->flag == 0) { /* '!' */ |
| 3133 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3134 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3135 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3136 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3137 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3138 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3139 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3140 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3141 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3142 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3143 | old = xmalloc(sizeof(*old)); |
| 3144 | debug_printf_parse("push stack %p\n", old); |
| 3145 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3146 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3147 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3148 | } 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] | 3149 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3150 | ctx->ctx_res_w = RES_SNTX; |
| 3151 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3152 | } else { |
| 3153 | /* "{...} fi" is ok. "{...} if" is not |
| 3154 | * Example: |
| 3155 | * if { echo foo; } then { echo bar; } fi */ |
| 3156 | if (ctx->command->group) |
| 3157 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3158 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3159 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3160 | ctx->ctx_res_w = r->res; |
| 3161 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3162 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3163 | 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] | 3164 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3165 | if (ctx->old_flag & FLAG_END) { |
| 3166 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3167 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3168 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3169 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3170 | old = ctx->stack; |
| 3171 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3172 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3173 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3174 | /* At this point, the compound command's string is in |
| 3175 | * ctx->as_string... except for the leading keyword! |
| 3176 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3177 | * ctx->as_string will contain "true; then echo a; fi", |
| 3178 | * with "if " remaining in old->as_string! |
| 3179 | */ |
| 3180 | { |
| 3181 | char *str; |
| 3182 | int len = old->as_string.length; |
| 3183 | /* Concatenate halves */ |
| 3184 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3185 | o_free_unsafe(&ctx->as_string); |
| 3186 | /* Find where leading keyword starts in first half */ |
| 3187 | str = old->as_string.data + len; |
| 3188 | if (str > old->as_string.data) |
| 3189 | str--; /* skip whitespace after keyword */ |
| 3190 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3191 | str--; |
| 3192 | /* Ugh, we're done with this horrid hack */ |
| 3193 | old->command->group_as_string = xstrdup(str); |
| 3194 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3195 | old->command->group_as_string); |
| 3196 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3197 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3198 | *ctx = *old; /* physical copy */ |
| 3199 | free(old); |
| 3200 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3201 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3202 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3203 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3204 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3205 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3206 | * Normal return is 0. Syntax errors return 1. |
| 3207 | * Note: on return, word is reset, but not o_free'd! |
| 3208 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3209 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3210 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3211 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3212 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3213 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3214 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3215 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3216 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3217 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3218 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3219 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3220 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3221 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3222 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3223 | * "2.7 Redirection |
| 3224 | * ...the word that follows the redirection operator |
| 3225 | * shall be subjected to tilde expansion, parameter expansion, |
| 3226 | * command substitution, arithmetic expansion, and quote |
| 3227 | * removal. Pathname expansion shall not be performed |
| 3228 | * on the word by a non-interactive shell; an interactive |
| 3229 | * shell may perform it, but shall do so only when |
| 3230 | * the expansion would result in one word." |
| 3231 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3232 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3233 | /* Cater for >\file case: |
| 3234 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3235 | * Same with heredocs: |
| 3236 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3237 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3238 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3239 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3240 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3241 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3242 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3243 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3244 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3245 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3246 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3247 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3248 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3249 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3250 | if (ctx->ctx_dsemicolon |
| 3251 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3252 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3253 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3254 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3255 | ctx->ctx_dsemicolon = 0; |
| 3256 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3257 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3258 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3259 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3260 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3261 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3262 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3263 | # if ENABLE_HUSH_CASE |
| 3264 | && ctx->ctx_res_w != RES_CASE |
| 3265 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3266 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3267 | int reserved = reserved_word(word, ctx); |
| 3268 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3269 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3270 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3271 | debug_printf_parse("done_word return %d\n", |
| 3272 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3273 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3274 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3275 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3276 | if (strcmp(word->data, "[[") == 0) { |
| 3277 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3278 | } |
| 3279 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3280 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3281 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3282 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3283 | if (command->group) { |
| 3284 | /* "{ echo foo; } echo bar" - bad */ |
| 3285 | syntax_error_at(word->data); |
| 3286 | debug_printf_parse("done_word return 1: syntax error, " |
| 3287 | "groups and arglists don't mix\n"); |
| 3288 | return 1; |
| 3289 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3290 | |
| 3291 | /* If this word wasn't an assignment, next ones definitely |
| 3292 | * can't be assignments. Even if they look like ones. */ |
| 3293 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3294 | && word->o_assignment != WORD_IS_KEYWORD |
| 3295 | ) { |
| 3296 | word->o_assignment = NOT_ASSIGNMENT; |
| 3297 | } else { |
| 3298 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3299 | command->assignment_cnt++; |
| 3300 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3301 | } |
| 3302 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3303 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3304 | } |
| 3305 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3306 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3307 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3308 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3309 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3310 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3311 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3312 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3313 | char *p = word->data; |
| 3314 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3315 | && (p[1] & 0x7f) == '@' |
| 3316 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3317 | ) { |
| 3318 | p += 3; |
| 3319 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3320 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3321 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3322 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3323 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3324 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3325 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3326 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3327 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3328 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3329 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3330 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3331 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3332 | return 1; |
| 3333 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3334 | /* Force FOR to have just one word (variable name) */ |
| 3335 | /* NB: basically, this makes hush see "for v in ..." |
| 3336 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3337 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3338 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3339 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3340 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3341 | #if ENABLE_HUSH_CASE |
| 3342 | /* Force CASE to have just one word */ |
| 3343 | if (ctx->ctx_res_w == RES_CASE) { |
| 3344 | done_pipe(ctx, PIPE_SEQ); |
| 3345 | } |
| 3346 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3347 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3348 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3349 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3350 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3351 | return 0; |
| 3352 | } |
| 3353 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3354 | |
| 3355 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3356 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3357 | * Return: |
| 3358 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3359 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3360 | * REDIRFD_TO_FILE if no & was seen, |
| 3361 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3362 | */ |
| 3363 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3364 | #define parse_redir_right_fd(as_string, input) \ |
| 3365 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3366 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3367 | 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] | 3368 | { |
| 3369 | int ch, d, ok; |
| 3370 | |
| 3371 | ch = i_peek(input); |
| 3372 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3373 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3374 | |
| 3375 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3376 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3377 | ch = i_peek(input); |
| 3378 | if (ch == '-') { |
| 3379 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3380 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3381 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3382 | } |
| 3383 | d = 0; |
| 3384 | ok = 0; |
| 3385 | while (ch != EOF && isdigit(ch)) { |
| 3386 | d = d*10 + (ch-'0'); |
| 3387 | ok = 1; |
| 3388 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3389 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3390 | ch = i_peek(input); |
| 3391 | } |
| 3392 | if (ok) return d; |
| 3393 | |
| 3394 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3395 | |
| 3396 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3397 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3400 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3401 | */ |
| 3402 | static int parse_redirect(struct parse_context *ctx, |
| 3403 | int fd, |
| 3404 | redir_type style, |
| 3405 | struct in_str *input) |
| 3406 | { |
| 3407 | struct command *command = ctx->command; |
| 3408 | struct redir_struct *redir; |
| 3409 | struct redir_struct **redirp; |
| 3410 | int dup_num; |
| 3411 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3412 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3413 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3414 | /* Check for a '>&1' type redirect */ |
| 3415 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3416 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3417 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3418 | } else { |
| 3419 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3420 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3421 | if (dup_num) { /* <<-... */ |
| 3422 | ch = i_getch(input); |
| 3423 | nommu_addchr(&ctx->as_string, ch); |
| 3424 | ch = i_peek(input); |
| 3425 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3426 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3427 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3428 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3429 | int ch = i_peek(input); |
| 3430 | if (ch == '|') { |
| 3431 | /* >|FILE redirect ("clobbering" >). |
| 3432 | * Since we do not support "set -o noclobber" yet, |
| 3433 | * >| and > are the same for now. Just eat |. |
| 3434 | */ |
| 3435 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3436 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | /* Create a new redir_struct and append it to the linked list */ |
| 3441 | redirp = &command->redirects; |
| 3442 | while ((redir = *redirp) != NULL) { |
| 3443 | redirp = &(redir->next); |
| 3444 | } |
| 3445 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3446 | /* redir->next = NULL; */ |
| 3447 | /* redir->rd_filename = NULL; */ |
| 3448 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3449 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3450 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3451 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3452 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3453 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3454 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3455 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3456 | /* Erik had a check here that the file descriptor in question |
| 3457 | * is legit; I postpone that to "run time" |
| 3458 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3459 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3460 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3461 | } else { |
| 3462 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3463 | * end of the next parsed word. */ |
| 3464 | ctx->pending_redirect = redir; |
| 3465 | } |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3469 | /* If a redirect is immediately preceded by a number, that number is |
| 3470 | * supposed to tell which file descriptor to redirect. This routine |
| 3471 | * looks for such preceding numbers. In an ideal world this routine |
| 3472 | * needs to handle all the following classes of redirects... |
| 3473 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3474 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3475 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3476 | * 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] | 3477 | * |
| 3478 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3479 | * "2.7 Redirection |
| 3480 | * ... If n is quoted, the number shall not be recognized as part of |
| 3481 | * the redirection expression. For example: |
| 3482 | * echo \2>a |
| 3483 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3484 | * 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] | 3485 | * |
| 3486 | * A -1 return means no valid number was found, |
| 3487 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3488 | */ |
| 3489 | static int redirect_opt_num(o_string *o) |
| 3490 | { |
| 3491 | int num; |
| 3492 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3493 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3494 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3495 | num = bb_strtou(o->data, NULL, 10); |
| 3496 | if (errno || num < 0) |
| 3497 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3498 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3499 | return num; |
| 3500 | } |
| 3501 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3502 | #if BB_MMU |
| 3503 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3504 | fetch_till_str(input, word, skip_tabs) |
| 3505 | #endif |
| 3506 | static char *fetch_till_str(o_string *as_string, |
| 3507 | struct in_str *input, |
| 3508 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3509 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3510 | { |
| 3511 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3512 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3513 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3514 | int ch; |
| 3515 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3516 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 3517 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3518 | while (1) { |
| 3519 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3520 | if (ch != EOF) |
| 3521 | nommu_addchr(as_string, ch); |
| 3522 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3523 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3524 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3525 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3526 | heredoc.data[past_EOL] = '\0'; |
| 3527 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3528 | return heredoc.data; |
| 3529 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3530 | while (ch == '\n') { |
| 3531 | o_addchr(&heredoc, ch); |
| 3532 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3533 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3534 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3535 | do { |
| 3536 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3537 | if (ch != EOF) |
| 3538 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3539 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3540 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3541 | } |
| 3542 | if (ch == EOF) { |
| 3543 | o_free_unsafe(&heredoc); |
| 3544 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3545 | } |
| 3546 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3547 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3548 | if (prev == '\\' && ch == '\\') |
| 3549 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3550 | prev = 0; /* not \ */ |
| 3551 | else |
| 3552 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3556 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3557 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3558 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3559 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3560 | { |
| 3561 | struct pipe *pi = ctx->list_head; |
| 3562 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3563 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3564 | int i; |
| 3565 | struct command *cmd = pi->cmds; |
| 3566 | |
| 3567 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3568 | pi->num_cmds, |
| 3569 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3570 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3571 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3572 | |
| 3573 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3574 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3575 | while (redir) { |
| 3576 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3577 | char *p; |
| 3578 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3579 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3580 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3581 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3582 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3583 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3584 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3585 | return 1; |
| 3586 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3587 | free(redir->rd_filename); |
| 3588 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3589 | heredoc_cnt--; |
| 3590 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3591 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3592 | } |
| 3593 | cmd++; |
| 3594 | } |
| 3595 | pi = pi->next; |
| 3596 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3597 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3598 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3599 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3600 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3601 | #endif |
| 3602 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3606 | static int run_list(struct pipe *pi); |
| 3607 | #if BB_MMU |
| 3608 | #define parse_stream(pstring, input, end_trigger) \ |
| 3609 | parse_stream(input, end_trigger) |
| 3610 | #endif |
| 3611 | static struct pipe *parse_stream(char **pstring, |
| 3612 | struct in_str *input, |
| 3613 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3614 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3615 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3616 | #if !ENABLE_HUSH_FUNCTIONS |
| 3617 | #define parse_group(dest, ctx, input, ch) \ |
| 3618 | parse_group(ctx, input, ch) |
| 3619 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3620 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3621 | struct in_str *input, int ch) |
| 3622 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3623 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3624 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3625 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3626 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3627 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3628 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3629 | |
| 3630 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3631 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3632 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3633 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3634 | if (done_word(dest, ctx)) |
| 3635 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3636 | if (!command->argv) |
| 3637 | goto skip; /* (... */ |
| 3638 | if (command->argv[1]) { /* word word ... (... */ |
| 3639 | syntax_error_unexpected_ch('('); |
| 3640 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3641 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3642 | /* it is "word(..." or "word (..." */ |
| 3643 | do |
| 3644 | ch = i_getch(input); |
| 3645 | while (ch == ' ' || ch == '\t'); |
| 3646 | if (ch != ')') { |
| 3647 | syntax_error_unexpected_ch(ch); |
| 3648 | return 1; |
| 3649 | } |
| 3650 | nommu_addchr(&ctx->as_string, ch); |
| 3651 | do |
| 3652 | ch = i_getch(input); |
| 3653 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3654 | if (ch != '{') { |
| 3655 | syntax_error_unexpected_ch(ch); |
| 3656 | return 1; |
| 3657 | } |
| 3658 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3659 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3660 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3661 | } |
| 3662 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3663 | |
| 3664 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3665 | if (command->argv /* word [word]{... */ |
| 3666 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3667 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3668 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3669 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3670 | debug_printf_parse("parse_group return 1: " |
| 3671 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3672 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3673 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3674 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3675 | |
| 3676 | #if ENABLE_HUSH_FUNCTIONS |
| 3677 | skip: |
| 3678 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3679 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3680 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3681 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3682 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3683 | } else { |
| 3684 | /* bash does not allow "{echo...", requires whitespace */ |
| 3685 | ch = i_getch(input); |
| 3686 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3687 | syntax_error_unexpected_ch(ch); |
| 3688 | return 1; |
| 3689 | } |
| 3690 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3691 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3692 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3693 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3694 | #if BB_MMU |
| 3695 | # define as_string NULL |
| 3696 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3697 | char *as_string = NULL; |
| 3698 | #endif |
| 3699 | pipe_list = parse_stream(&as_string, input, endch); |
| 3700 | #if !BB_MMU |
| 3701 | if (as_string) |
| 3702 | o_addstr(&ctx->as_string, as_string); |
| 3703 | #endif |
| 3704 | /* empty ()/{} or parse error? */ |
| 3705 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3706 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3707 | if (!BB_MMU) |
| 3708 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3709 | debug_printf_parse("parse_group return 1: " |
| 3710 | "parse_stream returned %p\n", pipe_list); |
| 3711 | return 1; |
| 3712 | } |
| 3713 | command->group = pipe_list; |
| 3714 | #if !BB_MMU |
| 3715 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3716 | command->group_as_string = as_string; |
| 3717 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3718 | command->group_as_string); |
| 3719 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3720 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3721 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3722 | debug_printf_parse("parse_group return 0\n"); |
| 3723 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3724 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3727 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3728 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3729 | 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] | 3730 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3731 | 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] | 3732 | { |
| 3733 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3734 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3735 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3736 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3737 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3738 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3739 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3740 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3741 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3745 | 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] | 3746 | { |
| 3747 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3748 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3749 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3750 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3751 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3752 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3753 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3754 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3755 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3756 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3757 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3758 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3759 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3760 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3761 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 3762 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3763 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3764 | continue; |
| 3765 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3766 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3770 | * \` quoting. |
| 3771 | * "Within the backquoted style of command substitution, backslash |
| 3772 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3773 | * The search for the matching backquote shall be satisfied by the first |
| 3774 | * backquote found without a preceding backslash; during this search, |
| 3775 | * if a non-escaped backquote is encountered within a shell comment, |
| 3776 | * a here-document, an embedded command substitution of the $(command) |
| 3777 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3778 | * or double-quoted string that begins, but does not end, within the |
| 3779 | * "`...`" sequence produces undefined results." |
| 3780 | * Example Output |
| 3781 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3782 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3783 | 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] | 3784 | { |
| 3785 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3786 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3787 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3788 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3789 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3790 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 3791 | ch = i_getch(input); |
| 3792 | if (ch != '`' |
| 3793 | && ch != '$' |
| 3794 | && ch != '\\' |
| 3795 | && (!in_dquote || ch != '"') |
| 3796 | ) { |
| 3797 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3798 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3799 | } |
| 3800 | if (ch == EOF) { |
| 3801 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3802 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3803 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3804 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3808 | * quoting and nested ()s. |
| 3809 | * "With the $(command) style of command substitution, all characters |
| 3810 | * following the open parenthesis to the matching closing parenthesis |
| 3811 | * constitute the command. Any valid shell script can be used for command, |
| 3812 | * except a script consisting solely of redirections which produces |
| 3813 | * unspecified results." |
| 3814 | * Example Output |
| 3815 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3816 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3817 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3818 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3819 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3820 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3821 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 3822 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3823 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3824 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3825 | 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] | 3826 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3827 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3828 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3829 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3830 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3831 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3832 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 3833 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3834 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3835 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3836 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3837 | syntax_error_unterm_ch(end_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 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3840 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3841 | if (!dbl) |
| 3842 | break; |
| 3843 | /* we look for closing )) of $((EXPR)) */ |
| 3844 | if (i_peek(input) == end_ch) { |
| 3845 | i_getch(input); /* eat second ')' */ |
| 3846 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3847 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3848 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3849 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3850 | if (ch == '(' || ch == '{') { |
| 3851 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3852 | if (!add_till_closing_bracket(dest, input, ch)) |
| 3853 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3854 | o_addchr(dest, ch); |
| 3855 | continue; |
| 3856 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3857 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3858 | if (!add_till_single_quote(dest, input)) |
| 3859 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3860 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3861 | continue; |
| 3862 | } |
| 3863 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3864 | if (!add_till_double_quote(dest, input)) |
| 3865 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3866 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3867 | continue; |
| 3868 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3869 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3870 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 3871 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3872 | o_addchr(dest, ch); |
| 3873 | continue; |
| 3874 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3875 | if (ch == '\\') { |
| 3876 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3877 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3878 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3879 | syntax_error_unterm_ch(')'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3880 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3881 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3882 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3883 | continue; |
| 3884 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3885 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3886 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3887 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3888 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3889 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3890 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3891 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3892 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 3893 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3894 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3895 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3896 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3897 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3898 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3899 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3900 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3901 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3902 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3903 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3904 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3905 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3906 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3907 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3908 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3909 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3910 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3911 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3912 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3913 | if (!isalnum(ch) && ch != '_') |
| 3914 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3915 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3916 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3917 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3918 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3919 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3920 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3921 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3922 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3923 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3924 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3925 | o_addchr(dest, ch | quote_mask); |
| 3926 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3927 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3928 | case '$': /* pid */ |
| 3929 | case '!': /* last bg pid */ |
| 3930 | case '?': /* last exit code */ |
| 3931 | case '#': /* number of args */ |
| 3932 | case '*': /* args */ |
| 3933 | case '@': /* args */ |
| 3934 | goto make_one_char_var; |
| 3935 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 3936 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3937 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3938 | ch = i_getch(input); /* eat '{' */ |
| 3939 | nommu_addchr(as_string, ch); |
| 3940 | |
| 3941 | ch = i_getch(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3942 | /* It should be ${?}, or ${#var}, |
| 3943 | * or even ${?+subst} - operator acting on a special variable, |
| 3944 | * or the beginning of variable name. |
| 3945 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3946 | if (ch == EOF |
| 3947 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 3948 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3949 | bad_dollar_syntax: |
| 3950 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3951 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 3952 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3953 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3954 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3955 | ch |= quote_mask; |
| 3956 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3957 | /* 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] | 3958 | * However, this regresses some of our testsuite cases |
| 3959 | * which check invalid constructs like ${%}. |
| 3960 | * Oh well... let's check that the var name part is fine... */ |
| 3961 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3962 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3963 | unsigned pos; |
| 3964 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3965 | o_addchr(dest, ch); |
| 3966 | debug_printf_parse(": '%c'\n", ch); |
| 3967 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3968 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3969 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3970 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3971 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3972 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3973 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3974 | unsigned end_ch; |
| 3975 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3976 | /* handle parameter expansions |
| 3977 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3978 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3979 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3980 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3981 | |
| 3982 | /* Eat everything until closing '}' (or ':') */ |
| 3983 | end_ch = '}'; |
| 3984 | if (ENABLE_HUSH_BASH_COMPAT |
| 3985 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3986 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3987 | ) { |
| 3988 | /* It's ${var:N[:M]} thing */ |
| 3989 | end_ch = '}' * 0x100 + ':'; |
| 3990 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3991 | if (ENABLE_HUSH_BASH_COMPAT |
| 3992 | && ch == '/' |
| 3993 | ) { |
| 3994 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 3995 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 3996 | i_getch(input); |
| 3997 | nommu_addchr(as_string, '/'); |
| 3998 | ch = '\\'; |
| 3999 | } |
| 4000 | end_ch = '}' * 0x100 + '/'; |
| 4001 | } |
| 4002 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4003 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4004 | if (!BB_MMU) |
| 4005 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4006 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4007 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4008 | if (last_ch == 0) /* error? */ |
| 4009 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4010 | #else |
| 4011 | #error Simple code to only allow ${var} is not implemented |
| 4012 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4013 | if (as_string) { |
| 4014 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4015 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4016 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4017 | |
| 4018 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 4019 | /* close the first block: */ |
| 4020 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4021 | /* while parsing N from ${var:N[:M]} |
| 4022 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4023 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4024 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4025 | end_ch = '}'; |
| 4026 | goto again; |
| 4027 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4028 | /* got '}' */ |
| 4029 | if (end_ch == '}' * 0x100 + ':') { |
| 4030 | /* it's ${var:N} - emulate :999999999 */ |
| 4031 | o_addstr(dest, "999999999"); |
| 4032 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4033 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4034 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4035 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4036 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4037 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4038 | break; |
| 4039 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4040 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4041 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4042 | unsigned pos; |
| 4043 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4044 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4045 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4046 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4047 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4048 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4049 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4050 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4051 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4052 | if (!BB_MMU) |
| 4053 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4054 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4055 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4056 | if (as_string) { |
| 4057 | o_addstr(as_string, dest->data + pos); |
| 4058 | o_addchr(as_string, ')'); |
| 4059 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4060 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4061 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4062 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4063 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4064 | # endif |
| 4065 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4066 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4067 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4068 | if (!BB_MMU) |
| 4069 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4070 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4071 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4072 | if (as_string) { |
| 4073 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4074 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4075 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4076 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4077 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4078 | break; |
| 4079 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4080 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4081 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4082 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4083 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4084 | ch = i_peek(input); |
| 4085 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4086 | ch = '_'; |
| 4087 | goto make_var; |
| 4088 | } |
| 4089 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4090 | /* TODO: $_ and $-: */ |
| 4091 | /* $_ Shell or shell script name; or last argument of last command |
| 4092 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4093 | * 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] | 4094 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4095 | default: |
| 4096 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4097 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4098 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4099 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4100 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4103 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4104 | # if ENABLE_HUSH_BASH_COMPAT |
| 4105 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4106 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4107 | # else |
| 4108 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4109 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4110 | encode_string(dest, input, dquote_end) |
| 4111 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4112 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4113 | |
| 4114 | #else /* !MMU */ |
| 4115 | |
| 4116 | # if ENABLE_HUSH_BASH_COMPAT |
| 4117 | /* all parameters are needed, no macro tricks */ |
| 4118 | # else |
| 4119 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4120 | encode_string(as_string, dest, input, dquote_end) |
| 4121 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4122 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4123 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4124 | o_string *dest, |
| 4125 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4126 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4127 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4128 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4129 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4130 | const int process_bkslash = 1; |
| 4131 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4132 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4133 | int next; |
| 4134 | |
| 4135 | again: |
| 4136 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4137 | if (ch != EOF) |
| 4138 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4139 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4140 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4141 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4142 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4143 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4144 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4145 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4146 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4147 | } |
| 4148 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4149 | if (ch != '\n') { |
| 4150 | next = i_peek(input); |
| 4151 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4152 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4153 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4154 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4155 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4156 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4157 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4158 | } |
| 4159 | /* bash: |
| 4160 | * "The backslash retains its special meaning [in "..."] |
| 4161 | * only when followed by one of the following characters: |
| 4162 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4163 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4164 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4165 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4166 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4167 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4168 | ch = i_getch(input); /* eat next */ |
| 4169 | if (ch == '\n') |
| 4170 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4171 | } /* else: ch remains == '\\', and we double it below: */ |
| 4172 | 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] | 4173 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4174 | goto again; |
| 4175 | } |
| 4176 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4177 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4178 | debug_printf_parse("encode_string return 0: " |
| 4179 | "parse_dollar returned 0 (error)\n"); |
| 4180 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4181 | } |
| 4182 | goto again; |
| 4183 | } |
| 4184 | #if ENABLE_HUSH_TICK |
| 4185 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4186 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4187 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4188 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4189 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4190 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4191 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4192 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4193 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4194 | } |
| 4195 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4196 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4197 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4198 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4201 | /* |
| 4202 | * Scan input until EOF or end_trigger char. |
| 4203 | * Return a list of pipes to execute, or NULL on EOF |
| 4204 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4205 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4206 | * reset parsing machinery and start parsing anew, |
| 4207 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4208 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4209 | static struct pipe *parse_stream(char **pstring, |
| 4210 | struct in_str *input, |
| 4211 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4212 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4213 | struct parse_context ctx; |
| 4214 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4215 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4216 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4217 | /* 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] | 4218 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4219 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4220 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4221 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4222 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4223 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4224 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4225 | * Preventing this: */ |
| 4226 | o_addchr(&dest, '\0'); |
| 4227 | dest.length = 0; |
| 4228 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4229 | /* We used to separate words on $IFS here. This was wrong. |
| 4230 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4231 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4232 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4233 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4234 | if (MAYBE_ASSIGNMENT != 0) |
| 4235 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4236 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4237 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4238 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4239 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4240 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4241 | int ch; |
| 4242 | int next; |
| 4243 | int redir_fd; |
| 4244 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4245 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4246 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4247 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4248 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4249 | if (ch == EOF) { |
| 4250 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4251 | |
| 4252 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4253 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4254 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4255 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4256 | /* end_trigger == '}' case errors out earlier, |
| 4257 | * checking only ')' */ |
| 4258 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4259 | syntax_error_unterm_ch('('); |
| 4260 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4261 | } |
| 4262 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4263 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4264 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4265 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4266 | o_free(&dest); |
| 4267 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4268 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4269 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4270 | /* (this makes bare "&" cmd a no-op. |
| 4271 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4272 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4273 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4274 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4275 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4276 | pi = NULL; |
| 4277 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4278 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4279 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4280 | if (pstring) |
| 4281 | *pstring = ctx.as_string.data; |
| 4282 | else |
| 4283 | o_free_unsafe(&ctx.as_string); |
| 4284 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4285 | debug_leave(); |
| 4286 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4287 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4288 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4289 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4290 | |
| 4291 | next = '\0'; |
| 4292 | if (ch != '\n') |
| 4293 | next = i_peek(input); |
| 4294 | |
| 4295 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4296 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4297 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4298 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4299 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4300 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4301 | || (next != ';' /* }; - special */ |
| 4302 | && next != ')' /* }) - special */ |
| 4303 | && next != '&' /* }& and }&& ... - special */ |
| 4304 | && next != '|' /* }|| ... - special */ |
| 4305 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4306 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4307 | ) { |
| 4308 | /* They are not special, skip "{}" */ |
| 4309 | is_special += 2; |
| 4310 | } |
| 4311 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4312 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4313 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4314 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4315 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4316 | o_addQchr(&dest, ch); |
| 4317 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4318 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4319 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4320 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4321 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4322 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4323 | 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] | 4324 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4325 | continue; |
| 4326 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4327 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4328 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4329 | if (done_word(&dest, &ctx)) { |
| 4330 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4331 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4332 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4333 | /* Is this a case when newline is simply ignored? |
| 4334 | * Some examples: |
| 4335 | * "cmd | <newline> cmd ..." |
| 4336 | * "case ... in <newline> word) ..." |
| 4337 | */ |
| 4338 | if (IS_NULL_CMD(ctx.command) |
| 4339 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4340 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4341 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4342 | * Without check #1, interactive shell |
| 4343 | * ignores even bare <newline>, |
| 4344 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4345 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4346 | * ps2> _ <=== wrong, should be ps1 |
| 4347 | * Without check #2, "cmd & <newline>" |
| 4348 | * is similarly mistreated. |
| 4349 | * (BTW, this makes "cmd & cmd" |
| 4350 | * and "cmd && cmd" non-orthogonal. |
| 4351 | * Really, ask yourself, why |
| 4352 | * "cmd && <newline>" doesn't start |
| 4353 | * cmd but waits for more input? |
| 4354 | * No reason...) |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4355 | */ |
| 4356 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4357 | if (pi->num_cmds != 0 /* check #1 */ |
| 4358 | && pi->followup != PIPE_BG /* check #2 */ |
| 4359 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4360 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4361 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4362 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4363 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4364 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4365 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4366 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4367 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4368 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4369 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4370 | heredoc_cnt = 0; |
| 4371 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4372 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4373 | 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] | 4374 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4375 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4376 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4377 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4378 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4379 | |
| 4380 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4381 | * } is an ordinary char in this case, even inside { cmd; } |
| 4382 | * Pathological example: { ""}; } should exec "}" cmd |
| 4383 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4384 | if (ch == '}') { |
| 4385 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 4386 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4387 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4388 | ) { |
| 4389 | goto ordinary_char; |
| 4390 | } |
| 4391 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 4392 | goto skip_end_trigger; |
| 4393 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4396 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4397 | && (ch != ';' || heredoc_cnt == 0) |
| 4398 | #if ENABLE_HUSH_CASE |
| 4399 | && (ch != ')' |
| 4400 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4401 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4402 | ) |
| 4403 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4404 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4405 | if (heredoc_cnt) { |
| 4406 | /* This is technically valid: |
| 4407 | * { cat <<HERE; }; echo Ok |
| 4408 | * heredoc |
| 4409 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4410 | * HERE |
| 4411 | * but we don't support this. |
| 4412 | * We require heredoc to be in enclosing {}/(), |
| 4413 | * if any. |
| 4414 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4415 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4416 | goto parse_error; |
| 4417 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4418 | if (done_word(&dest, &ctx)) { |
| 4419 | goto parse_error; |
| 4420 | } |
| 4421 | done_pipe(&ctx, PIPE_SEQ); |
| 4422 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4423 | 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] | 4424 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4425 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4426 | 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] | 4427 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4428 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4429 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4430 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4431 | if (pstring) |
| 4432 | *pstring = ctx.as_string.data; |
| 4433 | else |
| 4434 | o_free_unsafe(&ctx.as_string); |
| 4435 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4436 | debug_leave(); |
| 4437 | debug_printf_parse("parse_stream return %p: " |
| 4438 | "end_trigger char found\n", |
| 4439 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4440 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4441 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4442 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4443 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4444 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4445 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4446 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4447 | /* Catch <, > before deciding whether this word is |
| 4448 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4449 | switch (ch) { |
| 4450 | case '>': |
| 4451 | redir_fd = redirect_opt_num(&dest); |
| 4452 | if (done_word(&dest, &ctx)) { |
| 4453 | goto parse_error; |
| 4454 | } |
| 4455 | redir_style = REDIRECT_OVERWRITE; |
| 4456 | if (next == '>') { |
| 4457 | redir_style = REDIRECT_APPEND; |
| 4458 | ch = i_getch(input); |
| 4459 | nommu_addchr(&ctx.as_string, ch); |
| 4460 | } |
| 4461 | #if 0 |
| 4462 | else if (next == '(') { |
| 4463 | syntax_error(">(process) not supported"); |
| 4464 | goto parse_error; |
| 4465 | } |
| 4466 | #endif |
| 4467 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4468 | goto parse_error; |
| 4469 | continue; /* back to top of while (1) */ |
| 4470 | case '<': |
| 4471 | redir_fd = redirect_opt_num(&dest); |
| 4472 | if (done_word(&dest, &ctx)) { |
| 4473 | goto parse_error; |
| 4474 | } |
| 4475 | redir_style = REDIRECT_INPUT; |
| 4476 | if (next == '<') { |
| 4477 | redir_style = REDIRECT_HEREDOC; |
| 4478 | heredoc_cnt++; |
| 4479 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4480 | ch = i_getch(input); |
| 4481 | nommu_addchr(&ctx.as_string, ch); |
| 4482 | } else if (next == '>') { |
| 4483 | redir_style = REDIRECT_IO; |
| 4484 | ch = i_getch(input); |
| 4485 | nommu_addchr(&ctx.as_string, ch); |
| 4486 | } |
| 4487 | #if 0 |
| 4488 | else if (next == '(') { |
| 4489 | syntax_error("<(process) not supported"); |
| 4490 | goto parse_error; |
| 4491 | } |
| 4492 | #endif |
| 4493 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4494 | goto parse_error; |
| 4495 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4496 | case '#': |
| 4497 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4498 | /* skip "#comment" */ |
| 4499 | while (1) { |
| 4500 | ch = i_peek(input); |
| 4501 | if (ch == EOF || ch == '\n') |
| 4502 | break; |
| 4503 | i_getch(input); |
| 4504 | /* note: we do not add it to &ctx.as_string */ |
| 4505 | } |
| 4506 | nommu_addchr(&ctx.as_string, '\n'); |
| 4507 | continue; /* back to top of while (1) */ |
| 4508 | } |
| 4509 | break; |
| 4510 | case '\\': |
| 4511 | if (next == '\n') { |
| 4512 | /* It's "\<newline>" */ |
| 4513 | #if !BB_MMU |
| 4514 | /* Remove trailing '\' from ctx.as_string */ |
| 4515 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4516 | #endif |
| 4517 | ch = i_getch(input); /* eat it */ |
| 4518 | continue; /* back to top of while (1) */ |
| 4519 | } |
| 4520 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4524 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4525 | && !ctx.pending_redirect |
| 4526 | ) { |
| 4527 | /* ch is a special char and thus this word |
| 4528 | * cannot be an assignment */ |
| 4529 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4530 | 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] | 4531 | } |
| 4532 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4533 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4534 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4535 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4536 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4537 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4538 | break; |
| 4539 | case '\\': |
| 4540 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4541 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4542 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4543 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4544 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4545 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4546 | o_addchr(&dest, '\\'); |
| 4547 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4548 | o_addchr(&dest, ch); |
| 4549 | nommu_addchr(&ctx.as_string, ch); |
| 4550 | /* Example: echo Hello \2>file |
| 4551 | * we need to know that word 2 is quoted */ |
| 4552 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4553 | break; |
| 4554 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4555 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4556 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4557 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4558 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4559 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4560 | break; |
| 4561 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4562 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4563 | if (next == '\'' && !ctx.pending_redirect) { |
| 4564 | insert_empty_quoted_str_marker: |
| 4565 | nommu_addchr(&ctx.as_string, next); |
| 4566 | i_getch(input); /* eat second ' */ |
| 4567 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4568 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4569 | } else { |
| 4570 | while (1) { |
| 4571 | ch = i_getch(input); |
| 4572 | if (ch == EOF) { |
| 4573 | syntax_error_unterm_ch('\''); |
| 4574 | goto parse_error; |
| 4575 | } |
| 4576 | nommu_addchr(&ctx.as_string, ch); |
| 4577 | if (ch == '\'') |
| 4578 | break; |
| 4579 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4580 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4581 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4582 | break; |
| 4583 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4584 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4585 | if (next == '"' && !ctx.pending_redirect) |
| 4586 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4587 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4588 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4589 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4590 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4591 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4592 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4593 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4594 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4595 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4596 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4597 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4598 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4599 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4600 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 4601 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4602 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4603 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4604 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4605 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4606 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4607 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4608 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4609 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4610 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4611 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4612 | #if ENABLE_HUSH_CASE |
| 4613 | case_semi: |
| 4614 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4615 | if (done_word(&dest, &ctx)) { |
| 4616 | goto parse_error; |
| 4617 | } |
| 4618 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4619 | #if ENABLE_HUSH_CASE |
| 4620 | /* Eat multiple semicolons, detect |
| 4621 | * whether it means something special */ |
| 4622 | while (1) { |
| 4623 | ch = i_peek(input); |
| 4624 | if (ch != ';') |
| 4625 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4626 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4627 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4628 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4629 | ctx.ctx_dsemicolon = 1; |
| 4630 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4631 | break; |
| 4632 | } |
| 4633 | } |
| 4634 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4635 | new_cmd: |
| 4636 | /* We just finished a cmd. New one may start |
| 4637 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4638 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4639 | 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] | 4640 | break; |
| 4641 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4642 | if (done_word(&dest, &ctx)) { |
| 4643 | goto parse_error; |
| 4644 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4645 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4646 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4647 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4648 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4649 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4650 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4651 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4652 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4653 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4654 | if (done_word(&dest, &ctx)) { |
| 4655 | goto parse_error; |
| 4656 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4657 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4658 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4659 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4660 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4661 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4662 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4663 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4664 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4665 | } else { |
| 4666 | /* we could pick up a file descriptor choice here |
| 4667 | * with redirect_opt_num(), but bash doesn't do it. |
| 4668 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4669 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4670 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4671 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4672 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4673 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4674 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4675 | if (ctx.ctx_res_w == RES_MATCH |
| 4676 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4677 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4678 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4679 | ) { |
| 4680 | continue; |
| 4681 | } |
| 4682 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4683 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4684 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4685 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4686 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4687 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4688 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4689 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4690 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4691 | goto case_semi; |
| 4692 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4693 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4694 | /* proper use of this character is caught by end_trigger: |
| 4695 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4696 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4697 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4698 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4699 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4700 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4701 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4702 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4703 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4704 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4705 | parse_error: |
| 4706 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4707 | struct parse_context *pctx; |
| 4708 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4709 | |
| 4710 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4711 | * Sample for finding leaks on syntax error recovery path. |
| 4712 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4713 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4714 | * Samples to catch leaks at execution: |
| 4715 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4716 | * 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] | 4717 | */ |
| 4718 | pctx = &ctx; |
| 4719 | do { |
| 4720 | /* Update pipe/command counts, |
| 4721 | * otherwise freeing may miss some */ |
| 4722 | done_pipe(pctx, PIPE_SEQ); |
| 4723 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4724 | pctx->list_head, pctx); |
| 4725 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4726 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4727 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4728 | #if !BB_MMU |
| 4729 | o_free_unsafe(&pctx->as_string); |
| 4730 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4731 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4732 | if (pctx != &ctx) { |
| 4733 | free(pctx); |
| 4734 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4735 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4736 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4737 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 4738 | o_free(&dest); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4739 | G.last_exitcode = 1; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4740 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4741 | if (pstring) |
| 4742 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4743 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4744 | debug_leave(); |
| 4745 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4746 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4749 | |
| 4750 | /*** Execution routines ***/ |
| 4751 | |
| 4752 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4753 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4754 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4755 | #define expand_string_to_string(str, do_unbackslash) \ |
| 4756 | expand_string_to_string(str) |
| 4757 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4758 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4759 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4760 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4761 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4762 | |
| 4763 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 4764 | * all variable references within and returns a pointer to |
| 4765 | * a list of expanded strings, possibly with larger number |
| 4766 | * of strings. (Think VAR="a b"; echo $VAR). |
| 4767 | * This new list is allocated as a single malloc block. |
| 4768 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4769 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4770 | * Caller can deallocate entire list by single free(list). */ |
| 4771 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4772 | /* A horde of its helpers come first: */ |
| 4773 | |
| 4774 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 4775 | { |
| 4776 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4777 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4778 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4779 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 4780 | if (c == '{' || c == '}') { |
| 4781 | /* { -> \{, } -> \} */ |
| 4782 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4783 | /* And now we want to add { or } and continue: |
| 4784 | * o_addchr(o, c); |
| 4785 | * continue; |
| 4786 | * luckily, just falling throught achieves this. |
| 4787 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4788 | } |
| 4789 | #endif |
| 4790 | o_addchr(o, c); |
| 4791 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4792 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 4793 | o_addchr(o, '\\'); |
| 4794 | if (len) { |
| 4795 | len--; |
| 4796 | o_addchr(o, '\\'); |
| 4797 | o_addchr(o, *str++); |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4803 | /* Store given string, finalizing the word and starting new one whenever |
| 4804 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4805 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 4806 | * Return in *ended_with_ifs: |
| 4807 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 4808 | */ |
| 4809 | 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] | 4810 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4811 | int last_is_ifs = 0; |
| 4812 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4813 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4814 | int word_len; |
| 4815 | |
| 4816 | if (!*str) /* EOL - do not finalize word */ |
| 4817 | break; |
| 4818 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4819 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4820 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4821 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4822 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4823 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4824 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 4825 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 4826 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4827 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4828 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4829 | /*/ Why can't we do it easier? */ |
| 4830 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 4831 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 4832 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4833 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4834 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4835 | if (!*str) /* EOL - do not finalize word */ |
| 4836 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4837 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4838 | |
| 4839 | /* We know str here points to at least one IFS char */ |
| 4840 | last_is_ifs = 1; |
| 4841 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 4842 | if (!*str) /* EOL - do not finalize word */ |
| 4843 | break; |
| 4844 | |
| 4845 | /* Start new word... but not always! */ |
| 4846 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4847 | if (output->has_quoted_part |
| 4848 | /* Case "v=' a'; echo $v": |
| 4849 | * here nothing precedes the space in $v expansion, |
| 4850 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4851 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4852 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4853 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4854 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 4855 | o_addchr(output, '\0'); |
| 4856 | debug_print_list("expand_on_ifs", output, n); |
| 4857 | n = o_save_ptr(output, n); |
| 4858 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4859 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4860 | |
| 4861 | if (ended_with_ifs) |
| 4862 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4863 | debug_print_list("expand_on_ifs[1]", output, n); |
| 4864 | return n; |
| 4865 | } |
| 4866 | |
| 4867 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 4868 | * they are in double quotes, with the exception that they are not :). |
| 4869 | * Just the rules are similar: "expand only $var and `cmd`" |
| 4870 | * |
| 4871 | * Returns malloced string. |
| 4872 | * As an optimization, we return NULL if expansion is not needed. |
| 4873 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4874 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4875 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4876 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 4877 | encode_then_expand_string(str) |
| 4878 | #endif |
| 4879 | 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] | 4880 | { |
| 4881 | char *exp_str; |
| 4882 | struct in_str input; |
| 4883 | o_string dest = NULL_O_STRING; |
| 4884 | |
| 4885 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4886 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4887 | #if ENABLE_HUSH_TICK |
| 4888 | && !strchr(str, '`') |
| 4889 | #endif |
| 4890 | ) { |
| 4891 | return NULL; |
| 4892 | } |
| 4893 | |
| 4894 | /* We need to expand. Example: |
| 4895 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 4896 | */ |
| 4897 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4898 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4899 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4900 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4901 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4902 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 4903 | o_free_unsafe(&dest); |
| 4904 | return exp_str; |
| 4905 | } |
| 4906 | |
| 4907 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4908 | 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] | 4909 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4910 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4911 | arith_t res; |
| 4912 | char *exp_str; |
| 4913 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4914 | math_state.lookupvar = get_local_var_value; |
| 4915 | math_state.setvar = set_local_var_from_halves; |
| 4916 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4917 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4918 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4919 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4920 | if (errmsg_p) |
| 4921 | *errmsg_p = math_state.errmsg; |
| 4922 | if (math_state.errmsg) |
| 4923 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4924 | return res; |
| 4925 | } |
| 4926 | #endif |
| 4927 | |
| 4928 | #if ENABLE_HUSH_BASH_COMPAT |
| 4929 | /* ${var/[/]pattern[/repl]} helpers */ |
| 4930 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 4931 | { |
| 4932 | while (1) { |
| 4933 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 4934 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 4935 | if (end) { |
| 4936 | *size = end - val; |
| 4937 | return val; |
| 4938 | } |
| 4939 | if (*val == '\0') |
| 4940 | return NULL; |
| 4941 | /* Optimization: if "*pat" did not match the start of "string", |
| 4942 | * we know that "tring", "ring" etc will not match too: |
| 4943 | */ |
| 4944 | if (pattern[0] == '*') |
| 4945 | return NULL; |
| 4946 | val++; |
| 4947 | } |
| 4948 | } |
| 4949 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 4950 | { |
| 4951 | char *result = NULL; |
| 4952 | unsigned res_len = 0; |
| 4953 | unsigned repl_len = strlen(repl); |
| 4954 | |
| 4955 | while (1) { |
| 4956 | int size; |
| 4957 | char *s = strstr_pattern(val, pattern, &size); |
| 4958 | if (!s) |
| 4959 | break; |
| 4960 | |
| 4961 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 4962 | memcpy(result + res_len, val, s - val); |
| 4963 | res_len += s - val; |
| 4964 | strcpy(result + res_len, repl); |
| 4965 | res_len += repl_len; |
| 4966 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 4967 | |
| 4968 | val = s + size; |
| 4969 | if (exp_op == '/') |
| 4970 | break; |
| 4971 | } |
| 4972 | if (val[0] && result) { |
| 4973 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 4974 | strcpy(result + res_len, val); |
| 4975 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 4976 | } |
| 4977 | debug_printf_varexp("result:'%s'\n", result); |
| 4978 | return result; |
| 4979 | } |
| 4980 | #endif |
| 4981 | |
| 4982 | /* Helper: |
| 4983 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 4984 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4985 | 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] | 4986 | { |
| 4987 | const char *val = NULL; |
| 4988 | char *to_be_freed = NULL; |
| 4989 | char *p = *pp; |
| 4990 | char *var; |
| 4991 | char first_char; |
| 4992 | char exp_op; |
| 4993 | char exp_save = exp_save; /* for compiler */ |
| 4994 | char *exp_saveptr; /* points to expansion operator */ |
| 4995 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4996 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4997 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4998 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4999 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5000 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5001 | arg0 = arg[0]; |
| 5002 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5003 | exp_op = 0; |
| 5004 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5005 | if (first_char == '#' /* ${#... */ |
| 5006 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 5007 | ) { |
| 5008 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5009 | var++; |
| 5010 | exp_op = 'L'; |
| 5011 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5012 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5013 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5014 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5015 | ) { |
| 5016 | /* ${?:0}, ${#[:]%0} etc */ |
| 5017 | exp_saveptr = var + 1; |
| 5018 | } else { |
| 5019 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5020 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5021 | } |
| 5022 | exp_op = exp_save = *exp_saveptr; |
| 5023 | if (exp_op) { |
| 5024 | exp_word = exp_saveptr + 1; |
| 5025 | if (exp_op == ':') { |
| 5026 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5027 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5028 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5029 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5030 | ) { |
| 5031 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5032 | exp_op = ':'; |
| 5033 | exp_word--; |
| 5034 | } |
| 5035 | } |
| 5036 | *exp_saveptr = '\0'; |
| 5037 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5038 | } |
| 5039 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5040 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5041 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5042 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5043 | int n = xatoi_positive(var); |
| 5044 | if (n < G.global_argc) |
| 5045 | val = G.global_argv[n]; |
| 5046 | /* else val remains NULL: $N with too big N */ |
| 5047 | } else { |
| 5048 | switch (var[0]) { |
| 5049 | case '$': /* pid */ |
| 5050 | val = utoa(G.root_pid); |
| 5051 | break; |
| 5052 | case '!': /* bg pid */ |
| 5053 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5054 | break; |
| 5055 | case '?': /* exitcode */ |
| 5056 | val = utoa(G.last_exitcode); |
| 5057 | break; |
| 5058 | case '#': /* argc */ |
| 5059 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5060 | break; |
| 5061 | default: |
| 5062 | val = get_local_var_value(var); |
| 5063 | } |
| 5064 | } |
| 5065 | |
| 5066 | /* Handle any expansions */ |
| 5067 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5068 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5069 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5070 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5071 | debug_printf_expand("%s\n", val); |
| 5072 | } else if (exp_op) { |
| 5073 | if (exp_op == '%' || exp_op == '#') { |
| 5074 | /* Standard-mandated substring removal ops: |
| 5075 | * ${parameter%word} - remove smallest suffix pattern |
| 5076 | * ${parameter%%word} - remove largest suffix pattern |
| 5077 | * ${parameter#word} - remove smallest prefix pattern |
| 5078 | * ${parameter##word} - remove largest prefix pattern |
| 5079 | * |
| 5080 | * Word is expanded to produce a glob pattern. |
| 5081 | * Then var's value is matched to it and matching part removed. |
| 5082 | */ |
| 5083 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5084 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5085 | char *exp_exp_word; |
| 5086 | char *loc; |
| 5087 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5088 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5089 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5090 | 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] | 5091 | if (exp_exp_word) |
| 5092 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5093 | /* HACK ALERT. We depend here on the fact that |
| 5094 | * G.global_argv and results of utoa and get_local_var_value |
| 5095 | * are actually in writable memory: |
| 5096 | * scan_and_match momentarily stores NULs there. */ |
| 5097 | t = (char*)val; |
| 5098 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5099 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5100 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5101 | free(exp_exp_word); |
| 5102 | if (loc) { /* match was found */ |
| 5103 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5104 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5105 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5106 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5107 | } |
| 5108 | } |
| 5109 | } |
| 5110 | #if ENABLE_HUSH_BASH_COMPAT |
| 5111 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5112 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5113 | * Note that in encoded form it has TWO parts: |
| 5114 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5115 | * and if // is used, it is encoded as \: |
| 5116 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5117 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5118 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5119 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5120 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5121 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5122 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5123 | * by the usual expansion rules: |
| 5124 | * >az; >bz; |
| 5125 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5126 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5127 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5128 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5129 | * (note that a*z _pattern_ is never globbed!) |
| 5130 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5131 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5132 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5133 | if (!pattern) |
| 5134 | pattern = xstrdup(exp_word); |
| 5135 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5136 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5137 | exp_word = p; |
| 5138 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5139 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5140 | 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] | 5141 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5142 | /* HACK ALERT. We depend here on the fact that |
| 5143 | * G.global_argv and results of utoa and get_local_var_value |
| 5144 | * are actually in writable memory: |
| 5145 | * replace_pattern momentarily stores NULs there. */ |
| 5146 | t = (char*)val; |
| 5147 | to_be_freed = replace_pattern(t, |
| 5148 | pattern, |
| 5149 | (repl ? repl : exp_word), |
| 5150 | exp_op); |
| 5151 | if (to_be_freed) /* at least one replace happened */ |
| 5152 | val = to_be_freed; |
| 5153 | free(pattern); |
| 5154 | free(repl); |
| 5155 | } |
| 5156 | } |
| 5157 | #endif |
| 5158 | else if (exp_op == ':') { |
| 5159 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 5160 | /* It's ${var:N[:M]} bashism. |
| 5161 | * Note that in encoded form it has TWO parts: |
| 5162 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5163 | */ |
| 5164 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5165 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5166 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5167 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5168 | if (errmsg) |
| 5169 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5170 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5171 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5172 | exp_word = p; |
| 5173 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5174 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5175 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5176 | if (errmsg) |
| 5177 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5178 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5179 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5180 | if (beg < 0) /* bash compat */ |
| 5181 | beg = 0; |
| 5182 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5183 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5184 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5185 | val = NULL; |
| 5186 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5187 | /* Paranoia. What if user entered 9999999999999 |
| 5188 | * which fits in arith_t but not int? */ |
| 5189 | if (len >= INT_MAX) |
| 5190 | len = INT_MAX; |
| 5191 | val = to_be_freed = xstrndup(val + beg, len); |
| 5192 | } |
| 5193 | debug_printf_varexp("val:'%s'\n", val); |
| 5194 | } else |
| 5195 | #endif |
| 5196 | { |
| 5197 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5198 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5199 | } |
| 5200 | } else { /* one of "-=+?" */ |
| 5201 | /* Standard-mandated substitution ops: |
| 5202 | * ${var?word} - indicate error if unset |
| 5203 | * If var is unset, word (or a message indicating it is unset |
| 5204 | * if word is null) is written to standard error |
| 5205 | * and the shell exits with a non-zero exit status. |
| 5206 | * Otherwise, the value of var is substituted. |
| 5207 | * ${var-word} - use default value |
| 5208 | * If var is unset, word is substituted. |
| 5209 | * ${var=word} - assign and use default value |
| 5210 | * If var is unset, word is assigned to var. |
| 5211 | * In all cases, final value of var is substituted. |
| 5212 | * ${var+word} - use alternative value |
| 5213 | * If var is unset, null is substituted. |
| 5214 | * Otherwise, word is substituted. |
| 5215 | * |
| 5216 | * Word is subjected to tilde expansion, parameter expansion, |
| 5217 | * command substitution, and arithmetic expansion. |
| 5218 | * If word is not needed, it is not expanded. |
| 5219 | * |
| 5220 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5221 | * but also treat null var as if it is unset. |
| 5222 | */ |
| 5223 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5224 | if (exp_op == '+') |
| 5225 | use_word = !use_word; |
| 5226 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5227 | (exp_save == ':') ? "true" : "false", use_word); |
| 5228 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5229 | 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] | 5230 | if (to_be_freed) |
| 5231 | exp_word = to_be_freed; |
| 5232 | if (exp_op == '?') { |
| 5233 | /* mimic bash message */ |
| 5234 | die_if_script("%s: %s", |
| 5235 | var, |
| 5236 | exp_word[0] ? exp_word : "parameter null or not set" |
| 5237 | ); |
| 5238 | //TODO: how interactive bash aborts expansion mid-command? |
| 5239 | } else { |
| 5240 | val = exp_word; |
| 5241 | } |
| 5242 | |
| 5243 | if (exp_op == '=') { |
| 5244 | /* ${var=[word]} or ${var:=[word]} */ |
| 5245 | if (isdigit(var[0]) || var[0] == '#') { |
| 5246 | /* mimic bash message */ |
| 5247 | die_if_script("$%s: cannot assign in this way", var); |
| 5248 | val = NULL; |
| 5249 | } else { |
| 5250 | char *new_var = xasprintf("%s=%s", var, val); |
| 5251 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5252 | } |
| 5253 | } |
| 5254 | } |
| 5255 | } /* one of "-=+?" */ |
| 5256 | |
| 5257 | *exp_saveptr = exp_save; |
| 5258 | } /* if (exp_op) */ |
| 5259 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5260 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5261 | |
| 5262 | *pp = p; |
| 5263 | *to_be_freed_pp = to_be_freed; |
| 5264 | return val; |
| 5265 | } |
| 5266 | |
| 5267 | /* Expand all variable references in given string, adding words to list[] |
| 5268 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5269 | * to be filled). This routine is extremely tricky: has to deal with |
| 5270 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5271 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5272 | 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] | 5273 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5274 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5275 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5276 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5277 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5278 | 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] | 5279 | char *p; |
| 5280 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5281 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5282 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5283 | debug_print_list("expand_vars_to_list", output, n); |
| 5284 | n = o_save_ptr(output, n); |
| 5285 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5286 | |
| 5287 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5288 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5289 | char *to_be_freed = NULL; |
| 5290 | const char *val = NULL; |
| 5291 | #if ENABLE_HUSH_TICK |
| 5292 | o_string subst_result = NULL_O_STRING; |
| 5293 | #endif |
| 5294 | #if ENABLE_SH_MATH_SUPPORT |
| 5295 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5296 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5297 | |
| 5298 | if (ended_in_ifs) { |
| 5299 | o_addchr(output, '\0'); |
| 5300 | n = o_save_ptr(output, n); |
| 5301 | ended_in_ifs = 0; |
| 5302 | } |
| 5303 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5304 | o_addblock(output, arg, p - arg); |
| 5305 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5306 | arg = ++p; |
| 5307 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5308 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5309 | /* Fetch special var name (if it is indeed one of them) |
| 5310 | * and quote bit, force the bit on if singleword expansion - |
| 5311 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5312 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5313 | |
| 5314 | /* Is this variable quoted and thus expansion can't be null? |
| 5315 | * "$@" is special. Even if quoted, it can still |
| 5316 | * expand to nothing (not even an empty string), |
| 5317 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5318 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5319 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5320 | |
| 5321 | switch (first_ch & 0x7f) { |
| 5322 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5323 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5324 | case '@': { |
| 5325 | int i; |
| 5326 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5327 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5328 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5329 | 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] | 5330 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5331 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5332 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5333 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5334 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5335 | /* this argv[] is not empty and not last: |
| 5336 | * put terminating NUL, start new word */ |
| 5337 | o_addchr(output, '\0'); |
| 5338 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5339 | n = o_save_ptr(output, n); |
| 5340 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5341 | } |
| 5342 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5343 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5344 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5345 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5346 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5347 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5348 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5349 | while (1) { |
| 5350 | o_addQstr(output, G.global_argv[i]); |
| 5351 | if (++i >= G.global_argc) |
| 5352 | break; |
| 5353 | o_addchr(output, '\0'); |
| 5354 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5355 | n = o_save_ptr(output, n); |
| 5356 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5357 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5358 | while (1) { |
| 5359 | o_addQstr(output, G.global_argv[i]); |
| 5360 | if (!G.global_argv[++i]) |
| 5361 | break; |
| 5362 | if (G.ifs[0]) |
| 5363 | o_addchr(output, G.ifs[0]); |
| 5364 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5365 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5366 | } |
| 5367 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5368 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5369 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5370 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5371 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5372 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5373 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5374 | break; |
| 5375 | #if ENABLE_HUSH_TICK |
| 5376 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5377 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5378 | arg++; |
| 5379 | /* Can't just stuff it into output o_string, |
| 5380 | * expanded result may need to be globbed |
| 5381 | * and $IFS-splitted */ |
| 5382 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5383 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5384 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5385 | val = subst_result.data; |
| 5386 | goto store_val; |
| 5387 | #endif |
| 5388 | #if ENABLE_SH_MATH_SUPPORT |
| 5389 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5390 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5391 | |
| 5392 | arg++; /* skip '+' */ |
| 5393 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5394 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5395 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5396 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5397 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5398 | val = arith_buf; |
| 5399 | break; |
| 5400 | } |
| 5401 | #endif |
| 5402 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5403 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5404 | IF_HUSH_TICK(store_val:) |
| 5405 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5406 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5407 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5408 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5409 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5410 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5411 | } |
| 5412 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5413 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5414 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5415 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5416 | } |
| 5417 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5418 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5419 | |
| 5420 | if (val && val[0]) { |
| 5421 | o_addQstr(output, val); |
| 5422 | } |
| 5423 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5424 | |
| 5425 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5426 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5427 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5428 | *p = SPECIAL_VAR_SYMBOL; |
| 5429 | |
| 5430 | #if ENABLE_HUSH_TICK |
| 5431 | o_free(&subst_result); |
| 5432 | #endif |
| 5433 | arg = ++p; |
| 5434 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5435 | |
| 5436 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5437 | if (ended_in_ifs) { |
| 5438 | o_addchr(output, '\0'); |
| 5439 | n = o_save_ptr(output, n); |
| 5440 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5441 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5442 | /* this part is literal, and it was already pre-quoted |
| 5443 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5444 | o_addstr_with_NUL(output, arg); |
| 5445 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5446 | } 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] | 5447 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5448 | ) { |
| 5449 | n--; |
| 5450 | /* allow to reuse list[n] later without re-growth */ |
| 5451 | output->has_empty_slot = 1; |
| 5452 | } else { |
| 5453 | o_addchr(output, '\0'); |
| 5454 | } |
| 5455 | |
| 5456 | return n; |
| 5457 | } |
| 5458 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5459 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5460 | { |
| 5461 | int n; |
| 5462 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5463 | o_string output = NULL_O_STRING; |
| 5464 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5465 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5466 | |
| 5467 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5468 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5469 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5470 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5471 | } |
| 5472 | debug_print_list("expand_variables", &output, n); |
| 5473 | |
| 5474 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5475 | list = o_finalize_list(&output, n); |
| 5476 | debug_print_strings("expand_variables[1]", list); |
| 5477 | return list; |
| 5478 | } |
| 5479 | |
| 5480 | static char **expand_strvec_to_strvec(char **argv) |
| 5481 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5482 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | #if ENABLE_HUSH_BASH_COMPAT |
| 5486 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5487 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5488 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5489 | } |
| 5490 | #endif |
| 5491 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5492 | /* Used for expansion of right hand of assignments, |
| 5493 | * $((...)), heredocs, variable espansion parts. |
| 5494 | * |
| 5495 | * NB: should NOT do globbing! |
| 5496 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5497 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5498 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5499 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5500 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5501 | const int do_unbackslash = 1; |
| 5502 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5503 | char *argv[2], **list; |
| 5504 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5505 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5506 | /* This is generally an optimization, but it also |
| 5507 | * handles "", which otherwise trips over !list[0] check below. |
| 5508 | * (is this ever happens that we actually get str="" here?) |
| 5509 | */ |
| 5510 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5511 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5512 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5513 | return xstrdup(str); |
| 5514 | } |
| 5515 | |
| 5516 | argv[0] = (char*)str; |
| 5517 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5518 | list = expand_variables(argv, do_unbackslash |
| 5519 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5520 | : EXP_FLAG_SINGLEWORD |
| 5521 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5522 | if (HUSH_DEBUG) |
| 5523 | if (!list[0] || list[1]) |
| 5524 | bb_error_msg_and_die("BUG in varexp2"); |
| 5525 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5526 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5527 | if (do_unbackslash) |
| 5528 | unbackslash((char*)list); |
| 5529 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5530 | return (char*)list; |
| 5531 | } |
| 5532 | |
| 5533 | /* Used for "eval" builtin */ |
| 5534 | static char* expand_strvec_to_string(char **argv) |
| 5535 | { |
| 5536 | char **list; |
| 5537 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5538 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5539 | /* Convert all NULs to spaces */ |
| 5540 | if (list[0]) { |
| 5541 | int n = 1; |
| 5542 | while (list[n]) { |
| 5543 | if (HUSH_DEBUG) |
| 5544 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5545 | bb_error_msg_and_die("BUG in varexp3"); |
| 5546 | /* bash uses ' ' regardless of $IFS contents */ |
| 5547 | list[n][-1] = ' '; |
| 5548 | n++; |
| 5549 | } |
| 5550 | } |
| 5551 | overlapping_strcpy((char*)list, list[0]); |
| 5552 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5553 | return (char*)list; |
| 5554 | } |
| 5555 | |
| 5556 | static char **expand_assignments(char **argv, int count) |
| 5557 | { |
| 5558 | int i; |
| 5559 | char **p; |
| 5560 | |
| 5561 | G.expanded_assignments = p = NULL; |
| 5562 | /* Expand assignments into one string each */ |
| 5563 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5564 | 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] | 5565 | } |
| 5566 | G.expanded_assignments = NULL; |
| 5567 | return p; |
| 5568 | } |
| 5569 | |
| 5570 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5571 | static void switch_off_special_sigs(unsigned mask) |
| 5572 | { |
| 5573 | unsigned sig = 0; |
| 5574 | while ((mask >>= 1) != 0) { |
| 5575 | sig++; |
| 5576 | if (!(mask & 1)) |
| 5577 | continue; |
| 5578 | if (G.traps) { |
| 5579 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5580 | /* trap is '', has to remain SIG_IGN */ |
| 5581 | continue; |
| 5582 | free(G.traps[sig]); |
| 5583 | G.traps[sig] = NULL; |
| 5584 | } |
| 5585 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5586 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5587 | } |
| 5588 | } |
| 5589 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 5590 | #if BB_MMU |
| 5591 | /* never called */ |
| 5592 | void re_execute_shell(char ***to_free, const char *s, |
| 5593 | char *g_argv0, char **g_argv, |
| 5594 | char **builtin_argv) NORETURN; |
| 5595 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5596 | static void reset_traps_to_defaults(void) |
| 5597 | { |
| 5598 | /* This function is always called in a child shell |
| 5599 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5600 | */ |
| 5601 | unsigned sig; |
| 5602 | unsigned mask; |
| 5603 | |
| 5604 | /* Child shells are not interactive. |
| 5605 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5606 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5607 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5608 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5609 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
| 5610 | if (!G.traps && !mask) |
| 5611 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5612 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5613 | /* Switch off special sigs */ |
| 5614 | switch_off_special_sigs(mask); |
| 5615 | #if ENABLE_HUSH_JOB |
| 5616 | G_fatal_sig_mask = 0; |
| 5617 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 5618 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 5619 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 5620 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5621 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5622 | if (!G.traps) |
| 5623 | return; |
| 5624 | |
| 5625 | /* Reset all sigs to default except ones with empty traps */ |
| 5626 | for (sig = 0; sig < NSIG; sig++) { |
| 5627 | if (!G.traps[sig]) |
| 5628 | continue; /* no trap: nothing to do */ |
| 5629 | if (!G.traps[sig][0]) |
| 5630 | continue; /* empty trap: has to remain SIG_IGN */ |
| 5631 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5632 | free(G.traps[sig]); |
| 5633 | G.traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5634 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5635 | if (sig == 0) |
| 5636 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5637 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5638 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | #else /* !BB_MMU */ |
| 5642 | |
| 5643 | static void re_execute_shell(char ***to_free, const char *s, |
| 5644 | char *g_argv0, char **g_argv, |
| 5645 | char **builtin_argv) NORETURN; |
| 5646 | static void re_execute_shell(char ***to_free, const char *s, |
| 5647 | char *g_argv0, char **g_argv, |
| 5648 | char **builtin_argv) |
| 5649 | { |
| 5650 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5651 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5652 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5653 | char *heredoc_argv[4]; |
| 5654 | struct variable *cur; |
| 5655 | # if ENABLE_HUSH_FUNCTIONS |
| 5656 | struct function *funcp; |
| 5657 | # endif |
| 5658 | char **argv, **pp; |
| 5659 | unsigned cnt; |
| 5660 | unsigned long long empty_trap_mask; |
| 5661 | |
| 5662 | if (!g_argv0) { /* heredoc */ |
| 5663 | argv = heredoc_argv; |
| 5664 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5665 | argv[1] = (char *) "-<"; |
| 5666 | argv[2] = (char *) s; |
| 5667 | argv[3] = NULL; |
| 5668 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5669 | goto do_exec; |
| 5670 | } |
| 5671 | |
| 5672 | cnt = 0; |
| 5673 | pp = builtin_argv; |
| 5674 | if (pp) while (*pp++) |
| 5675 | cnt++; |
| 5676 | |
| 5677 | empty_trap_mask = 0; |
| 5678 | if (G.traps) { |
| 5679 | int sig; |
| 5680 | for (sig = 1; sig < NSIG; sig++) { |
| 5681 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5682 | empty_trap_mask |= 1LL << sig; |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5687 | , (unsigned) G.root_pid |
| 5688 | , (unsigned) G.root_ppid |
| 5689 | , (unsigned) G.last_bg_pid |
| 5690 | , (unsigned) G.last_exitcode |
| 5691 | , cnt |
| 5692 | , empty_trap_mask |
| 5693 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5694 | ); |
| 5695 | # undef NOMMU_HACK_FMT |
| 5696 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5697 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5698 | */ |
| 5699 | cnt += 6; |
| 5700 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5701 | if (!cur->flg_export || cur->flg_read_only) |
| 5702 | cnt += 2; |
| 5703 | } |
| 5704 | # if ENABLE_HUSH_FUNCTIONS |
| 5705 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5706 | cnt += 3; |
| 5707 | # endif |
| 5708 | pp = g_argv; |
| 5709 | while (*pp++) |
| 5710 | cnt++; |
| 5711 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5712 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5713 | *pp++ = param_buf; |
| 5714 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5715 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5716 | continue; |
| 5717 | if (cur->flg_read_only) { |
| 5718 | *pp++ = (char *) "-R"; |
| 5719 | *pp++ = cur->varstr; |
| 5720 | } else if (!cur->flg_export) { |
| 5721 | *pp++ = (char *) "-V"; |
| 5722 | *pp++ = cur->varstr; |
| 5723 | } |
| 5724 | } |
| 5725 | # if ENABLE_HUSH_FUNCTIONS |
| 5726 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 5727 | *pp++ = (char *) "-F"; |
| 5728 | *pp++ = funcp->name; |
| 5729 | *pp++ = funcp->body_as_string; |
| 5730 | } |
| 5731 | # endif |
| 5732 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 5733 | * |
| 5734 | * However, POSIX says that subshells reset signals with traps |
| 5735 | * to SIG_DFL. |
| 5736 | * I tested bash-3.2 and it not only does that with true subshells |
| 5737 | * of the form ( list ), but with any forked children shells. |
| 5738 | * I set trap "echo W" WINCH; and then tried: |
| 5739 | * |
| 5740 | * { echo 1; sleep 20; echo 2; } & |
| 5741 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 5742 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 5743 | * |
| 5744 | * In all these cases sending SIGWINCH to the child shell |
| 5745 | * did not run the trap. If I add trap "echo V" WINCH; |
| 5746 | * _inside_ group (just before echo 1), it works. |
| 5747 | * |
| 5748 | * 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] | 5749 | */ |
| 5750 | *pp++ = (char *) "-c"; |
| 5751 | *pp++ = (char *) s; |
| 5752 | if (builtin_argv) { |
| 5753 | while (*++builtin_argv) |
| 5754 | *pp++ = *builtin_argv; |
| 5755 | *pp++ = (char *) ""; |
| 5756 | } |
| 5757 | *pp++ = g_argv0; |
| 5758 | while (*g_argv) |
| 5759 | *pp++ = *g_argv++; |
| 5760 | /* *pp = NULL; - is already there */ |
| 5761 | pp = environ; |
| 5762 | |
| 5763 | do_exec: |
| 5764 | 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] | 5765 | /* Don't propagate SIG_IGN to the child */ |
| 5766 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 5767 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5768 | execve(bb_busybox_exec_path, argv, pp); |
| 5769 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 5770 | if (argv[0][0] == '/') |
| 5771 | execve(argv[0], argv, pp); |
| 5772 | xfunc_error_retval = 127; |
| 5773 | bb_error_msg_and_die("can't re-execute the shell"); |
| 5774 | } |
| 5775 | #endif /* !BB_MMU */ |
| 5776 | |
| 5777 | |
| 5778 | static int run_and_free_list(struct pipe *pi); |
| 5779 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5780 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5781 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5782 | * end_trigger controls how often we stop parsing |
| 5783 | * NUL: parse all, execute, return |
| 5784 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5785 | */ |
| 5786 | 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] | 5787 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5788 | /* Why we need empty flag? |
| 5789 | * An obscure corner case "false; ``; echo $?": |
| 5790 | * empty command in `` should still set $? to 0. |
| 5791 | * But we can't just set $? to 0 at the start, |
| 5792 | * this breaks "false; echo `echo $?`" case. |
| 5793 | */ |
| 5794 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5795 | while (1) { |
| 5796 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5797 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 5798 | #if ENABLE_HUSH_INTERACTIVE |
| 5799 | if (end_trigger == ';') |
| 5800 | inp->promptmode = 0; /* PS1 */ |
| 5801 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5802 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5803 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 5804 | /* If we are in "big" script |
| 5805 | * (not in `cmd` or something similar)... |
| 5806 | */ |
| 5807 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 5808 | /* Discard cached input (rest of line) */ |
| 5809 | int ch = inp->last_char; |
| 5810 | while (ch != EOF && ch != '\n') { |
| 5811 | //bb_error_msg("Discarded:'%c'", ch); |
| 5812 | ch = i_getch(inp); |
| 5813 | } |
| 5814 | /* Force prompt */ |
| 5815 | inp->p = NULL; |
| 5816 | /* This stream isn't empty */ |
| 5817 | empty = 0; |
| 5818 | continue; |
| 5819 | } |
| 5820 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5821 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5822 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5823 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5824 | debug_print_tree(pipe_list, 0); |
| 5825 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5826 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5827 | empty = 0; |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 5828 | #if ENABLE_HUSH_FUNCTIONS |
| 5829 | if (G.flag_return_in_progress == 1) |
| 5830 | break; |
| 5831 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5832 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5833 | } |
| 5834 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5835 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5836 | { |
| 5837 | struct in_str input; |
| 5838 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5839 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5842 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5843 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5844 | struct in_str input; |
| 5845 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5846 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5849 | #if ENABLE_HUSH_TICK |
| 5850 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 5851 | { |
| 5852 | pid_t pid; |
| 5853 | int channel[2]; |
| 5854 | # if !BB_MMU |
| 5855 | char **to_free = NULL; |
| 5856 | # endif |
| 5857 | |
| 5858 | xpipe(channel); |
| 5859 | pid = BB_MMU ? xfork() : xvfork(); |
| 5860 | if (pid == 0) { /* child */ |
| 5861 | disable_restore_tty_pgrp_on_exit(); |
| 5862 | /* Process substitution is not considered to be usual |
| 5863 | * 'command execution'. |
| 5864 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5865 | */ |
| 5866 | bb_signals(0 |
| 5867 | + (1 << SIGTSTP) |
| 5868 | + (1 << SIGTTIN) |
| 5869 | + (1 << SIGTTOU) |
| 5870 | , SIG_IGN); |
| 5871 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 5872 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5873 | xmove_fd(channel[1], 1); |
| 5874 | /* Prevent it from trying to handle ctrl-z etc */ |
| 5875 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 5876 | /* Awful hack for `trap` or $(trap). |
| 5877 | * |
| 5878 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5879 | * contains an example where "trap" is executed in a subshell: |
| 5880 | * |
| 5881 | * save_traps=$(trap) |
| 5882 | * ... |
| 5883 | * eval "$save_traps" |
| 5884 | * |
| 5885 | * Standard does not say that "trap" in subshell shall print |
| 5886 | * parent shell's traps. It only says that its output |
| 5887 | * must have suitable form, but then, in the above example |
| 5888 | * (which is not supposed to be normative), it implies that. |
| 5889 | * |
| 5890 | * bash (and probably other shell) does implement it |
| 5891 | * (traps are reset to defaults, but "trap" still shows them), |
| 5892 | * but as a result, "trap" logic is hopelessly messed up: |
| 5893 | * |
| 5894 | * # trap |
| 5895 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5896 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5897 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5898 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5899 | * trap -- 'echo Ho' SIGWINCH |
| 5900 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5901 | * trap -- 'echo Ho' SIGWINCH |
| 5902 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5903 | * trap -- 'echo Ho' SIGWINCH |
| 5904 | * |
| 5905 | * The rules when to forget and when to not forget traps |
| 5906 | * get really complex and nonsensical. |
| 5907 | * |
| 5908 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5909 | */ |
| 5910 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 5911 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5912 | && skip_whitespace(s + 4)[0] == '\0' |
| 5913 | ) { |
| 5914 | static const char *const argv[] = { NULL, NULL }; |
| 5915 | builtin_trap((char**)argv); |
| 5916 | exit(0); /* not _exit() - we need to fflush */ |
| 5917 | } |
| 5918 | # if BB_MMU |
| 5919 | reset_traps_to_defaults(); |
| 5920 | parse_and_run_string(s); |
| 5921 | _exit(G.last_exitcode); |
| 5922 | # else |
| 5923 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 5924 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5925 | * huge=`cat BIG` # was blocking here forever |
| 5926 | * echo OK |
| 5927 | */ |
| 5928 | re_execute_shell(&to_free, |
| 5929 | s, |
| 5930 | G.global_argv[0], |
| 5931 | G.global_argv + 1, |
| 5932 | NULL); |
| 5933 | # endif |
| 5934 | } |
| 5935 | |
| 5936 | /* parent */ |
| 5937 | *pid_p = pid; |
| 5938 | # if ENABLE_HUSH_FAST |
| 5939 | G.count_SIGCHLD++; |
| 5940 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 5941 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 5942 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5943 | # endif |
| 5944 | enable_restore_tty_pgrp_on_exit(); |
| 5945 | # if !BB_MMU |
| 5946 | free(to_free); |
| 5947 | # endif |
| 5948 | close(channel[1]); |
| 5949 | close_on_exec_on(channel[0]); |
| 5950 | return xfdopen_for_read(channel[0]); |
| 5951 | } |
| 5952 | |
| 5953 | /* Return code is exit status of the process that is run. */ |
| 5954 | static int process_command_subs(o_string *dest, const char *s) |
| 5955 | { |
| 5956 | FILE *fp; |
| 5957 | struct in_str pipe_str; |
| 5958 | pid_t pid; |
| 5959 | int status, ch, eol_cnt; |
| 5960 | |
| 5961 | fp = generate_stream_from_string(s, &pid); |
| 5962 | |
| 5963 | /* Now send results of command back into original context */ |
| 5964 | setup_file_in_str(&pipe_str, fp); |
| 5965 | eol_cnt = 0; |
| 5966 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 5967 | if (ch == '\n') { |
| 5968 | eol_cnt++; |
| 5969 | continue; |
| 5970 | } |
| 5971 | while (eol_cnt) { |
| 5972 | o_addchr(dest, '\n'); |
| 5973 | eol_cnt--; |
| 5974 | } |
| 5975 | o_addQchr(dest, ch); |
| 5976 | } |
| 5977 | |
| 5978 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
| 5979 | fclose(fp); |
| 5980 | /* We need to extract exitcode. Test case |
| 5981 | * "true; echo `sleep 1; false` $?" |
| 5982 | * should print 1 */ |
| 5983 | safe_waitpid(pid, &status, 0); |
| 5984 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5985 | return WEXITSTATUS(status); |
| 5986 | } |
| 5987 | #endif /* ENABLE_HUSH_TICK */ |
| 5988 | |
| 5989 | |
| 5990 | static void setup_heredoc(struct redir_struct *redir) |
| 5991 | { |
| 5992 | struct fd_pair pair; |
| 5993 | pid_t pid; |
| 5994 | int len, written; |
| 5995 | /* the _body_ of heredoc (misleading field name) */ |
| 5996 | const char *heredoc = redir->rd_filename; |
| 5997 | char *expanded; |
| 5998 | #if !BB_MMU |
| 5999 | char **to_free; |
| 6000 | #endif |
| 6001 | |
| 6002 | expanded = NULL; |
| 6003 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6004 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6005 | if (expanded) |
| 6006 | heredoc = expanded; |
| 6007 | } |
| 6008 | len = strlen(heredoc); |
| 6009 | |
| 6010 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6011 | xpiped_pair(pair); |
| 6012 | xmove_fd(pair.rd, redir->rd_fd); |
| 6013 | |
| 6014 | /* Try writing without forking. Newer kernels have |
| 6015 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6016 | ndelay_on(pair.wr); |
| 6017 | while (1) { |
| 6018 | written = write(pair.wr, heredoc, len); |
| 6019 | if (written <= 0) |
| 6020 | break; |
| 6021 | len -= written; |
| 6022 | if (len == 0) { |
| 6023 | close(pair.wr); |
| 6024 | free(expanded); |
| 6025 | return; |
| 6026 | } |
| 6027 | heredoc += written; |
| 6028 | } |
| 6029 | ndelay_off(pair.wr); |
| 6030 | |
| 6031 | /* Okay, pipe buffer was not big enough */ |
| 6032 | /* Note: we must not create a stray child (bastard? :) |
| 6033 | * for the unsuspecting parent process. Child creates a grandchild |
| 6034 | * and exits before parent execs the process which consumes heredoc |
| 6035 | * (that exec happens after we return from this function) */ |
| 6036 | #if !BB_MMU |
| 6037 | to_free = NULL; |
| 6038 | #endif |
| 6039 | pid = xvfork(); |
| 6040 | if (pid == 0) { |
| 6041 | /* child */ |
| 6042 | disable_restore_tty_pgrp_on_exit(); |
| 6043 | pid = BB_MMU ? xfork() : xvfork(); |
| 6044 | if (pid != 0) |
| 6045 | _exit(0); |
| 6046 | /* grandchild */ |
| 6047 | close(redir->rd_fd); /* read side of the pipe */ |
| 6048 | #if BB_MMU |
| 6049 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6050 | _exit(0); |
| 6051 | #else |
| 6052 | /* Delegate blocking writes to another process */ |
| 6053 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6054 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6055 | #endif |
| 6056 | } |
| 6057 | /* parent */ |
| 6058 | #if ENABLE_HUSH_FAST |
| 6059 | G.count_SIGCHLD++; |
| 6060 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6061 | #endif |
| 6062 | enable_restore_tty_pgrp_on_exit(); |
| 6063 | #if !BB_MMU |
| 6064 | free(to_free); |
| 6065 | #endif |
| 6066 | close(pair.wr); |
| 6067 | free(expanded); |
| 6068 | wait(NULL); /* wait till child has died */ |
| 6069 | } |
| 6070 | |
| 6071 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6072 | * and stderr if they are redirected. */ |
| 6073 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 6074 | { |
| 6075 | int openfd, mode; |
| 6076 | struct redir_struct *redir; |
| 6077 | |
| 6078 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 6079 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
| 6080 | /* rd_fd<<HERE case */ |
| 6081 | if (squirrel && redir->rd_fd < 3 |
| 6082 | && squirrel[redir->rd_fd] < 0 |
| 6083 | ) { |
| 6084 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 6085 | } |
| 6086 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6087 | * of the heredoc */ |
| 6088 | debug_printf_parse("set heredoc '%s'\n", |
| 6089 | redir->rd_filename); |
| 6090 | setup_heredoc(redir); |
| 6091 | continue; |
| 6092 | } |
| 6093 | |
| 6094 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 6095 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
| 6096 | char *p; |
| 6097 | if (redir->rd_filename == NULL) { |
| 6098 | /* Something went wrong in the parse. |
| 6099 | * Pretend it didn't happen */ |
| 6100 | bb_error_msg("bug in redirect parse"); |
| 6101 | continue; |
| 6102 | } |
| 6103 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6104 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6105 | openfd = open_or_warn(p, mode); |
| 6106 | free(p); |
| 6107 | if (openfd < 0) { |
| 6108 | /* this could get lost if stderr has been redirected, but |
| 6109 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 6110 | //what the above comment tries to say? |
| 6111 | return 1; |
| 6112 | } |
| 6113 | } else { |
| 6114 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
| 6115 | openfd = redir->rd_dup; |
| 6116 | } |
| 6117 | |
| 6118 | if (openfd != redir->rd_fd) { |
| 6119 | if (squirrel && redir->rd_fd < 3 |
| 6120 | && squirrel[redir->rd_fd] < 0 |
| 6121 | ) { |
| 6122 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 6123 | } |
| 6124 | if (openfd == REDIRFD_CLOSE) { |
| 6125 | /* "n>-" means "close me" */ |
| 6126 | close(redir->rd_fd); |
| 6127 | } else { |
| 6128 | xdup2(openfd, redir->rd_fd); |
| 6129 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 6130 | close(openfd); |
| 6131 | } |
| 6132 | } |
| 6133 | } |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
| 6137 | static void restore_redirects(int squirrel[]) |
| 6138 | { |
| 6139 | int i, fd; |
| 6140 | for (i = 0; i < 3; i++) { |
| 6141 | fd = squirrel[i]; |
| 6142 | if (fd != -1) { |
| 6143 | /* We simply die on error */ |
| 6144 | xmove_fd(fd, i); |
| 6145 | } |
| 6146 | } |
| 6147 | } |
| 6148 | |
| 6149 | static char *find_in_path(const char *arg) |
| 6150 | { |
| 6151 | char *ret = NULL; |
| 6152 | const char *PATH = get_local_var_value("PATH"); |
| 6153 | |
| 6154 | if (!PATH) |
| 6155 | return NULL; |
| 6156 | |
| 6157 | while (1) { |
| 6158 | const char *end = strchrnul(PATH, ':'); |
| 6159 | int sz = end - PATH; /* must be int! */ |
| 6160 | |
| 6161 | free(ret); |
| 6162 | if (sz != 0) { |
| 6163 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6164 | } else { |
| 6165 | /* We have xxx::yyyy in $PATH, |
| 6166 | * it means "use current dir" */ |
| 6167 | ret = xstrdup(arg); |
| 6168 | } |
| 6169 | if (access(ret, F_OK) == 0) |
| 6170 | break; |
| 6171 | |
| 6172 | if (*end == '\0') { |
| 6173 | free(ret); |
| 6174 | return NULL; |
| 6175 | } |
| 6176 | PATH = end + 1; |
| 6177 | } |
| 6178 | |
| 6179 | return ret; |
| 6180 | } |
| 6181 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6182 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6183 | const struct built_in_command *x, |
| 6184 | const struct built_in_command *end) |
| 6185 | { |
| 6186 | while (x != end) { |
| 6187 | if (strcmp(name, x->b_cmd) != 0) { |
| 6188 | x++; |
| 6189 | continue; |
| 6190 | } |
| 6191 | debug_printf_exec("found builtin '%s'\n", name); |
| 6192 | return x; |
| 6193 | } |
| 6194 | return NULL; |
| 6195 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6196 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6197 | { |
| 6198 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 6199 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6200 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6201 | { |
| 6202 | const struct built_in_command *x = find_builtin1(name); |
| 6203 | if (x) |
| 6204 | return x; |
| 6205 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 6206 | } |
| 6207 | |
| 6208 | #if ENABLE_HUSH_FUNCTIONS |
| 6209 | static struct function **find_function_slot(const char *name) |
| 6210 | { |
| 6211 | struct function **funcpp = &G.top_func; |
| 6212 | while (*funcpp) { |
| 6213 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 6214 | break; |
| 6215 | } |
| 6216 | funcpp = &(*funcpp)->next; |
| 6217 | } |
| 6218 | return funcpp; |
| 6219 | } |
| 6220 | |
| 6221 | static const struct function *find_function(const char *name) |
| 6222 | { |
| 6223 | const struct function *funcp = *find_function_slot(name); |
| 6224 | if (funcp) |
| 6225 | debug_printf_exec("found function '%s'\n", name); |
| 6226 | return funcp; |
| 6227 | } |
| 6228 | |
| 6229 | /* Note: takes ownership on name ptr */ |
| 6230 | static struct function *new_function(char *name) |
| 6231 | { |
| 6232 | struct function **funcpp = find_function_slot(name); |
| 6233 | struct function *funcp = *funcpp; |
| 6234 | |
| 6235 | if (funcp != NULL) { |
| 6236 | struct command *cmd = funcp->parent_cmd; |
| 6237 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 6238 | if (!cmd) { |
| 6239 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 6240 | free(funcp->name); |
| 6241 | /* Note: if !funcp->body, do not free body_as_string! |
| 6242 | * This is a special case of "-F name body" function: |
| 6243 | * body_as_string was not malloced! */ |
| 6244 | if (funcp->body) { |
| 6245 | free_pipe_list(funcp->body); |
| 6246 | # if !BB_MMU |
| 6247 | free(funcp->body_as_string); |
| 6248 | # endif |
| 6249 | } |
| 6250 | } else { |
| 6251 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 6252 | cmd->argv[0] = funcp->name; |
| 6253 | cmd->group = funcp->body; |
| 6254 | # if !BB_MMU |
| 6255 | cmd->group_as_string = funcp->body_as_string; |
| 6256 | # endif |
| 6257 | } |
| 6258 | } else { |
| 6259 | debug_printf_exec("remembering new function '%s'\n", name); |
| 6260 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 6261 | /*funcp->next = NULL;*/ |
| 6262 | } |
| 6263 | |
| 6264 | funcp->name = name; |
| 6265 | return funcp; |
| 6266 | } |
| 6267 | |
| 6268 | static void unset_func(const char *name) |
| 6269 | { |
| 6270 | struct function **funcpp = find_function_slot(name); |
| 6271 | struct function *funcp = *funcpp; |
| 6272 | |
| 6273 | if (funcp != NULL) { |
| 6274 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 6275 | *funcpp = funcp->next; |
| 6276 | /* funcp is unlinked now, deleting it. |
| 6277 | * Note: if !funcp->body, the function was created by |
| 6278 | * "-F name body", do not free ->body_as_string |
| 6279 | * and ->name as they were not malloced. */ |
| 6280 | if (funcp->body) { |
| 6281 | free_pipe_list(funcp->body); |
| 6282 | free(funcp->name); |
| 6283 | # if !BB_MMU |
| 6284 | free(funcp->body_as_string); |
| 6285 | # endif |
| 6286 | } |
| 6287 | free(funcp); |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | # if BB_MMU |
| 6292 | #define exec_function(to_free, funcp, argv) \ |
| 6293 | exec_function(funcp, argv) |
| 6294 | # endif |
| 6295 | static void exec_function(char ***to_free, |
| 6296 | const struct function *funcp, |
| 6297 | char **argv) NORETURN; |
| 6298 | static void exec_function(char ***to_free, |
| 6299 | const struct function *funcp, |
| 6300 | char **argv) |
| 6301 | { |
| 6302 | # if BB_MMU |
| 6303 | int n = 1; |
| 6304 | |
| 6305 | argv[0] = G.global_argv[0]; |
| 6306 | G.global_argv = argv; |
| 6307 | while (*++argv) |
| 6308 | n++; |
| 6309 | G.global_argc = n; |
| 6310 | /* On MMU, funcp->body is always non-NULL */ |
| 6311 | n = run_list(funcp->body); |
| 6312 | fflush_all(); |
| 6313 | _exit(n); |
| 6314 | # else |
| 6315 | re_execute_shell(to_free, |
| 6316 | funcp->body_as_string, |
| 6317 | G.global_argv[0], |
| 6318 | argv + 1, |
| 6319 | NULL); |
| 6320 | # endif |
| 6321 | } |
| 6322 | |
| 6323 | static int run_function(const struct function *funcp, char **argv) |
| 6324 | { |
| 6325 | int rc; |
| 6326 | save_arg_t sv; |
| 6327 | smallint sv_flg; |
| 6328 | |
| 6329 | save_and_replace_G_args(&sv, argv); |
| 6330 | |
| 6331 | /* "we are in function, ok to use return" */ |
| 6332 | sv_flg = G.flag_return_in_progress; |
| 6333 | G.flag_return_in_progress = -1; |
| 6334 | # if ENABLE_HUSH_LOCAL |
| 6335 | G.func_nest_level++; |
| 6336 | # endif |
| 6337 | |
| 6338 | /* On MMU, funcp->body is always non-NULL */ |
| 6339 | # if !BB_MMU |
| 6340 | if (!funcp->body) { |
| 6341 | /* Function defined by -F */ |
| 6342 | parse_and_run_string(funcp->body_as_string); |
| 6343 | rc = G.last_exitcode; |
| 6344 | } else |
| 6345 | # endif |
| 6346 | { |
| 6347 | rc = run_list(funcp->body); |
| 6348 | } |
| 6349 | |
| 6350 | # if ENABLE_HUSH_LOCAL |
| 6351 | { |
| 6352 | struct variable *var; |
| 6353 | struct variable **var_pp; |
| 6354 | |
| 6355 | var_pp = &G.top_var; |
| 6356 | while ((var = *var_pp) != NULL) { |
| 6357 | if (var->func_nest_level < G.func_nest_level) { |
| 6358 | var_pp = &var->next; |
| 6359 | continue; |
| 6360 | } |
| 6361 | /* Unexport */ |
| 6362 | if (var->flg_export) |
| 6363 | bb_unsetenv(var->varstr); |
| 6364 | /* Remove from global list */ |
| 6365 | *var_pp = var->next; |
| 6366 | /* Free */ |
| 6367 | if (!var->max_len) |
| 6368 | free(var->varstr); |
| 6369 | free(var); |
| 6370 | } |
| 6371 | G.func_nest_level--; |
| 6372 | } |
| 6373 | # endif |
| 6374 | G.flag_return_in_progress = sv_flg; |
| 6375 | |
| 6376 | restore_G_args(&sv, argv); |
| 6377 | |
| 6378 | return rc; |
| 6379 | } |
| 6380 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6381 | |
| 6382 | |
| 6383 | #if BB_MMU |
| 6384 | #define exec_builtin(to_free, x, argv) \ |
| 6385 | exec_builtin(x, argv) |
| 6386 | #else |
| 6387 | #define exec_builtin(to_free, x, argv) \ |
| 6388 | exec_builtin(to_free, argv) |
| 6389 | #endif |
| 6390 | static void exec_builtin(char ***to_free, |
| 6391 | const struct built_in_command *x, |
| 6392 | char **argv) NORETURN; |
| 6393 | static void exec_builtin(char ***to_free, |
| 6394 | const struct built_in_command *x, |
| 6395 | char **argv) |
| 6396 | { |
| 6397 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6398 | int rcode; |
| 6399 | fflush_all(); |
| 6400 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6401 | fflush_all(); |
| 6402 | _exit(rcode); |
| 6403 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6404 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6405 | /* On NOMMU, we must never block! |
| 6406 | * Example: { sleep 99 | read line; } & echo Ok |
| 6407 | */ |
| 6408 | re_execute_shell(to_free, |
| 6409 | argv[0], |
| 6410 | G.global_argv[0], |
| 6411 | G.global_argv + 1, |
| 6412 | argv); |
| 6413 | #endif |
| 6414 | } |
| 6415 | |
| 6416 | |
| 6417 | static void execvp_or_die(char **argv) NORETURN; |
| 6418 | static void execvp_or_die(char **argv) |
| 6419 | { |
| 6420 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6421 | /* Don't propagate SIG_IGN to the child */ |
| 6422 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6423 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6424 | execvp(argv[0], argv); |
| 6425 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 6426 | _exit(127); /* bash compat */ |
| 6427 | } |
| 6428 | |
| 6429 | #if ENABLE_HUSH_MODE_X |
| 6430 | static void dump_cmd_in_x_mode(char **argv) |
| 6431 | { |
| 6432 | if (G_x_mode && argv) { |
| 6433 | /* We want to output the line in one write op */ |
| 6434 | char *buf, *p; |
| 6435 | int len; |
| 6436 | int n; |
| 6437 | |
| 6438 | len = 3; |
| 6439 | n = 0; |
| 6440 | while (argv[n]) |
| 6441 | len += strlen(argv[n++]) + 1; |
| 6442 | buf = xmalloc(len); |
| 6443 | buf[0] = '+'; |
| 6444 | p = buf + 1; |
| 6445 | n = 0; |
| 6446 | while (argv[n]) |
| 6447 | p += sprintf(p, " %s", argv[n++]); |
| 6448 | *p++ = '\n'; |
| 6449 | *p = '\0'; |
| 6450 | fputs(buf, stderr); |
| 6451 | free(buf); |
| 6452 | } |
| 6453 | } |
| 6454 | #else |
| 6455 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6456 | #endif |
| 6457 | |
| 6458 | #if BB_MMU |
| 6459 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6460 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6461 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6462 | pseudo_exec(command, argv_expanded) |
| 6463 | #endif |
| 6464 | |
| 6465 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6466 | * Never returns. |
| 6467 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6468 | * The at_exit handlers apparently confuse the calling process, |
| 6469 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
| 6470 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6471 | char **argv, int assignment_cnt, |
| 6472 | char **argv_expanded) NORETURN; |
| 6473 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6474 | char **argv, int assignment_cnt, |
| 6475 | char **argv_expanded) |
| 6476 | { |
| 6477 | char **new_env; |
| 6478 | |
| 6479 | new_env = expand_assignments(argv, assignment_cnt); |
| 6480 | dump_cmd_in_x_mode(new_env); |
| 6481 | |
| 6482 | if (!argv[assignment_cnt]) { |
| 6483 | /* Case when we are here: ... | var=val | ... |
| 6484 | * (note that we do not exit early, i.e., do not optimize out |
| 6485 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6486 | */ |
| 6487 | free_strings(new_env); |
| 6488 | _exit(EXIT_SUCCESS); |
| 6489 | } |
| 6490 | |
| 6491 | #if BB_MMU |
| 6492 | set_vars_and_save_old(new_env); |
| 6493 | free(new_env); /* optional */ |
| 6494 | /* we can also destroy set_vars_and_save_old's return value, |
| 6495 | * to save memory */ |
| 6496 | #else |
| 6497 | nommu_save->new_env = new_env; |
| 6498 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6499 | #endif |
| 6500 | |
| 6501 | if (argv_expanded) { |
| 6502 | argv = argv_expanded; |
| 6503 | } else { |
| 6504 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6505 | #if !BB_MMU |
| 6506 | nommu_save->argv = argv; |
| 6507 | #endif |
| 6508 | } |
| 6509 | dump_cmd_in_x_mode(argv); |
| 6510 | |
| 6511 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6512 | if (strchr(argv[0], '/') != NULL) |
| 6513 | goto skip; |
| 6514 | #endif |
| 6515 | |
| 6516 | /* Check if the command matches any of the builtins. |
| 6517 | * Depending on context, this might be redundant. But it's |
| 6518 | * easier to waste a few CPU cycles than it is to figure out |
| 6519 | * if this is one of those cases. |
| 6520 | */ |
| 6521 | { |
| 6522 | /* On NOMMU, it is more expensive to re-execute shell |
| 6523 | * just in order to run echo or test builtin. |
| 6524 | * It's better to skip it here and run corresponding |
| 6525 | * non-builtin later. */ |
| 6526 | const struct built_in_command *x; |
| 6527 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6528 | if (x) { |
| 6529 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6530 | } |
| 6531 | } |
| 6532 | #if ENABLE_HUSH_FUNCTIONS |
| 6533 | /* Check if the command matches any functions */ |
| 6534 | { |
| 6535 | const struct function *funcp = find_function(argv[0]); |
| 6536 | if (funcp) { |
| 6537 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6538 | } |
| 6539 | } |
| 6540 | #endif |
| 6541 | |
| 6542 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6543 | /* Check if the command matches any busybox applets */ |
| 6544 | { |
| 6545 | int a = find_applet_by_name(argv[0]); |
| 6546 | if (a >= 0) { |
| 6547 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6548 | if (APPLET_IS_NOEXEC(a)) { |
| 6549 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6550 | run_applet_no_and_exit(a, argv); |
| 6551 | } |
| 6552 | # endif |
| 6553 | /* Re-exec ourselves */ |
| 6554 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6555 | /* Don't propagate SIG_IGN to the child */ |
| 6556 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6557 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6558 | execv(bb_busybox_exec_path, argv); |
| 6559 | /* If they called chroot or otherwise made the binary no longer |
| 6560 | * executable, fall through */ |
| 6561 | } |
| 6562 | } |
| 6563 | #endif |
| 6564 | |
| 6565 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6566 | skip: |
| 6567 | #endif |
| 6568 | execvp_or_die(argv); |
| 6569 | } |
| 6570 | |
| 6571 | /* Called after [v]fork() in run_pipe |
| 6572 | */ |
| 6573 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6574 | struct command *command, |
| 6575 | char **argv_expanded) NORETURN; |
| 6576 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6577 | struct command *command, |
| 6578 | char **argv_expanded) |
| 6579 | { |
| 6580 | if (command->argv) { |
| 6581 | pseudo_exec_argv(nommu_save, command->argv, |
| 6582 | command->assignment_cnt, argv_expanded); |
| 6583 | } |
| 6584 | |
| 6585 | if (command->group) { |
| 6586 | /* Cases when we are here: |
| 6587 | * ( list ) |
| 6588 | * { list } & |
| 6589 | * ... | ( list ) | ... |
| 6590 | * ... | { list } | ... |
| 6591 | */ |
| 6592 | #if BB_MMU |
| 6593 | int rcode; |
| 6594 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6595 | reset_traps_to_defaults(); |
| 6596 | rcode = run_list(command->group); |
| 6597 | /* OK to leak memory by not calling free_pipe_list, |
| 6598 | * since this process is about to exit */ |
| 6599 | _exit(rcode); |
| 6600 | #else |
| 6601 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6602 | command->group_as_string, |
| 6603 | G.global_argv[0], |
| 6604 | G.global_argv + 1, |
| 6605 | NULL); |
| 6606 | #endif |
| 6607 | } |
| 6608 | |
| 6609 | /* Case when we are here: ... | >file */ |
| 6610 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6611 | _exit(EXIT_SUCCESS); |
| 6612 | } |
| 6613 | |
| 6614 | #if ENABLE_HUSH_JOB |
| 6615 | static const char *get_cmdtext(struct pipe *pi) |
| 6616 | { |
| 6617 | char **argv; |
| 6618 | char *p; |
| 6619 | int len; |
| 6620 | |
| 6621 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6622 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6623 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6624 | if (pi->cmdtext) |
| 6625 | return pi->cmdtext; |
| 6626 | argv = pi->cmds[0].argv; |
| 6627 | if (!argv || !argv[0]) { |
| 6628 | pi->cmdtext = xzalloc(1); |
| 6629 | return pi->cmdtext; |
| 6630 | } |
| 6631 | |
| 6632 | len = 0; |
| 6633 | do { |
| 6634 | len += strlen(*argv) + 1; |
| 6635 | } while (*++argv); |
| 6636 | p = xmalloc(len); |
| 6637 | pi->cmdtext = p; |
| 6638 | argv = pi->cmds[0].argv; |
| 6639 | do { |
| 6640 | len = strlen(*argv); |
| 6641 | memcpy(p, *argv, len); |
| 6642 | p += len; |
| 6643 | *p++ = ' '; |
| 6644 | } while (*++argv); |
| 6645 | p[-1] = '\0'; |
| 6646 | return pi->cmdtext; |
| 6647 | } |
| 6648 | |
| 6649 | static void insert_bg_job(struct pipe *pi) |
| 6650 | { |
| 6651 | struct pipe *job, **jobp; |
| 6652 | int i; |
| 6653 | |
| 6654 | /* Linear search for the ID of the job to use */ |
| 6655 | pi->jobid = 1; |
| 6656 | for (job = G.job_list; job; job = job->next) |
| 6657 | if (job->jobid >= pi->jobid) |
| 6658 | pi->jobid = job->jobid + 1; |
| 6659 | |
| 6660 | /* Add job to the list of running jobs */ |
| 6661 | jobp = &G.job_list; |
| 6662 | while ((job = *jobp) != NULL) |
| 6663 | jobp = &job->next; |
| 6664 | job = *jobp = xmalloc(sizeof(*job)); |
| 6665 | |
| 6666 | *job = *pi; /* physical copy */ |
| 6667 | job->next = NULL; |
| 6668 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 6669 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 6670 | for (i = 0; i < pi->num_cmds; i++) { |
| 6671 | job->cmds[i].pid = pi->cmds[i].pid; |
| 6672 | /* all other fields are not used and stay zero */ |
| 6673 | } |
| 6674 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 6675 | |
| 6676 | if (G_interactive_fd) |
| 6677 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 6678 | G.last_jobid = job->jobid; |
| 6679 | } |
| 6680 | |
| 6681 | static void remove_bg_job(struct pipe *pi) |
| 6682 | { |
| 6683 | struct pipe *prev_pipe; |
| 6684 | |
| 6685 | if (pi == G.job_list) { |
| 6686 | G.job_list = pi->next; |
| 6687 | } else { |
| 6688 | prev_pipe = G.job_list; |
| 6689 | while (prev_pipe->next != pi) |
| 6690 | prev_pipe = prev_pipe->next; |
| 6691 | prev_pipe->next = pi->next; |
| 6692 | } |
| 6693 | if (G.job_list) |
| 6694 | G.last_jobid = G.job_list->jobid; |
| 6695 | else |
| 6696 | G.last_jobid = 0; |
| 6697 | } |
| 6698 | |
| 6699 | /* Remove a backgrounded job */ |
| 6700 | static void delete_finished_bg_job(struct pipe *pi) |
| 6701 | { |
| 6702 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6703 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6704 | } |
| 6705 | #endif /* JOB */ |
| 6706 | |
| 6707 | /* Check to see if any processes have exited -- if they |
| 6708 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 6709 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6710 | { |
| 6711 | int attributes; |
| 6712 | int status; |
| 6713 | #if ENABLE_HUSH_JOB |
| 6714 | struct pipe *pi; |
| 6715 | #endif |
| 6716 | pid_t childpid; |
| 6717 | int rcode = 0; |
| 6718 | |
| 6719 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 6720 | |
| 6721 | attributes = WUNTRACED; |
| 6722 | if (fg_pipe == NULL) |
| 6723 | attributes |= WNOHANG; |
| 6724 | |
| 6725 | errno = 0; |
| 6726 | #if ENABLE_HUSH_FAST |
| 6727 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 6728 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 6729 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 6730 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 6731 | /* Avoid doing waitpid syscall if possible */ |
| 6732 | if (!G.we_have_children) { |
| 6733 | errno = ECHILD; |
| 6734 | return -1; |
| 6735 | } |
| 6736 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 6737 | /* We have children, but they did not exit |
| 6738 | * or stop yet (we saw no SIGCHLD) */ |
| 6739 | return 0; |
| 6740 | } |
| 6741 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 6742 | } |
| 6743 | #endif |
| 6744 | |
| 6745 | /* Do we do this right? |
| 6746 | * bash-3.00# sleep 20 | false |
| 6747 | * <ctrl-Z pressed> |
| 6748 | * [3]+ Stopped sleep 20 | false |
| 6749 | * bash-3.00# echo $? |
| 6750 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 6751 | * [hush 1.14.0: yes we do it right] |
| 6752 | */ |
| 6753 | wait_more: |
| 6754 | while (1) { |
| 6755 | int i; |
| 6756 | int dead; |
| 6757 | |
| 6758 | #if ENABLE_HUSH_FAST |
| 6759 | i = G.count_SIGCHLD; |
| 6760 | #endif |
| 6761 | childpid = waitpid(-1, &status, attributes); |
| 6762 | if (childpid <= 0) { |
| 6763 | if (childpid && errno != ECHILD) |
| 6764 | bb_perror_msg("waitpid"); |
| 6765 | #if ENABLE_HUSH_FAST |
| 6766 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 6767 | G.we_have_children = (childpid == 0); |
| 6768 | G.handled_SIGCHLD = i; |
| 6769 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6770 | } |
| 6771 | #endif |
| 6772 | break; |
| 6773 | } |
| 6774 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 6775 | |
| 6776 | #if DEBUG_JOBS |
| 6777 | if (WIFSTOPPED(status)) |
| 6778 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 6779 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 6780 | if (WIFSIGNALED(status)) |
| 6781 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 6782 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 6783 | if (WIFEXITED(status)) |
| 6784 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 6785 | childpid, WEXITSTATUS(status)); |
| 6786 | #endif |
| 6787 | /* Were we asked to wait for fg pipe? */ |
| 6788 | if (fg_pipe) { |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6789 | i = fg_pipe->num_cmds; |
| 6790 | while (--i >= 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6791 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 6792 | if (fg_pipe->cmds[i].pid != childpid) |
| 6793 | continue; |
| 6794 | if (dead) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6795 | int ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6796 | fg_pipe->cmds[i].pid = 0; |
| 6797 | fg_pipe->alive_cmds--; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6798 | ex = WEXITSTATUS(status); |
| 6799 | /* bash prints killer signal's name for *last* |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 6800 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6801 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 6802 | */ |
| 6803 | if (WIFSIGNALED(status)) { |
| 6804 | int sig = WTERMSIG(status); |
| 6805 | if (i == fg_pipe->num_cmds-1) |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 6806 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 6807 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 6808 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6809 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 6810 | * Maybe we need to use sig | 128? */ |
| 6811 | ex = sig + 128; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6812 | } |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6813 | fg_pipe->cmds[i].cmd_exitcode = ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6814 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6815 | fg_pipe->stopped_cmds++; |
| 6816 | } |
| 6817 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 6818 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6819 | if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6820 | /* All processes in fg pipe have exited or stopped */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6821 | i = fg_pipe->num_cmds; |
| 6822 | while (--i >= 0) { |
| 6823 | rcode = fg_pipe->cmds[i].cmd_exitcode; |
| 6824 | /* usually last process gives overall exitstatus, |
| 6825 | * but with "set -o pipefail", last *failed* process does */ |
| 6826 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 6827 | break; |
| 6828 | } |
| 6829 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6830 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 6831 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 6832 | * and "killall -STOP cat" */ |
| 6833 | if (G_interactive_fd) { |
| 6834 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6835 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6836 | insert_bg_job(fg_pipe); |
| 6837 | #endif |
| 6838 | return rcode; |
| 6839 | } |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6840 | if (fg_pipe->alive_cmds == 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6841 | return rcode; |
| 6842 | } |
| 6843 | /* There are still running processes in the fg pipe */ |
| 6844 | goto wait_more; /* do waitpid again */ |
| 6845 | } |
| 6846 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 6847 | } |
| 6848 | |
| 6849 | #if ENABLE_HUSH_JOB |
| 6850 | /* We asked to wait for bg or orphaned children */ |
| 6851 | /* No need to remember exitcode in this case */ |
| 6852 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6853 | for (i = 0; i < pi->num_cmds; i++) { |
| 6854 | if (pi->cmds[i].pid == childpid) |
| 6855 | goto found_pi_and_prognum; |
| 6856 | } |
| 6857 | } |
| 6858 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 6859 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 6860 | continue; /* do waitpid again */ |
| 6861 | |
| 6862 | found_pi_and_prognum: |
| 6863 | if (dead) { |
| 6864 | /* child exited */ |
| 6865 | pi->cmds[i].pid = 0; |
| 6866 | pi->alive_cmds--; |
| 6867 | if (!pi->alive_cmds) { |
| 6868 | if (G_interactive_fd) |
| 6869 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 6870 | "Done", pi->cmdtext); |
| 6871 | delete_finished_bg_job(pi); |
| 6872 | } |
| 6873 | } else { |
| 6874 | /* child stopped */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6875 | pi->stopped_cmds++; |
| 6876 | } |
| 6877 | #endif |
| 6878 | } /* while (waitpid succeeds)... */ |
| 6879 | |
| 6880 | return rcode; |
| 6881 | } |
| 6882 | |
| 6883 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6884 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6885 | { |
| 6886 | pid_t p; |
| 6887 | int rcode = checkjobs(fg_pipe); |
| 6888 | if (G_saved_tty_pgrp) { |
| 6889 | /* Job finished, move the shell to the foreground */ |
| 6890 | p = getpgrp(); /* our process group id */ |
| 6891 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 6892 | tcsetpgrp(G_interactive_fd, p); |
| 6893 | } |
| 6894 | return rcode; |
| 6895 | } |
| 6896 | #endif |
| 6897 | |
| 6898 | /* Start all the jobs, but don't wait for anything to finish. |
| 6899 | * See checkjobs(). |
| 6900 | * |
| 6901 | * Return code is normally -1, when the caller has to wait for children |
| 6902 | * to finish to determine the exit status of the pipe. If the pipe |
| 6903 | * is a simple builtin command, however, the action is done by the |
| 6904 | * time run_pipe returns, and the exit code is provided as the |
| 6905 | * return value. |
| 6906 | * |
| 6907 | * Returns -1 only if started some children. IOW: we have to |
| 6908 | * mask out retvals of builtins etc with 0xff! |
| 6909 | * |
| 6910 | * The only case when we do not need to [v]fork is when the pipe |
| 6911 | * is single, non-backgrounded, non-subshell command. Examples: |
| 6912 | * cmd ; ... { list } ; ... |
| 6913 | * cmd && ... { list } && ... |
| 6914 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 6915 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6916 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 6917 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 6918 | * |
| 6919 | * Cases when we must fork: |
| 6920 | * non-single: cmd | cmd |
| 6921 | * backgrounded: cmd & { list } & |
| 6922 | * subshell: ( list ) [&] |
| 6923 | */ |
| 6924 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 6925 | #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] | 6926 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 6927 | #endif |
| 6928 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 6929 | struct variable **old_vars_p, |
| 6930 | struct command *command, |
| 6931 | int squirrel[3], |
| 6932 | char **argv_expanded) |
| 6933 | { |
| 6934 | /* setup_redirects acts on file descriptors, not FILEs. |
| 6935 | * This is perfect for work that comes after exec(). |
| 6936 | * Is it really safe for inline use? Experimentally, |
| 6937 | * things seem to work. */ |
| 6938 | int rcode = setup_redirects(command, squirrel); |
| 6939 | if (rcode == 0) { |
| 6940 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 6941 | *new_env_p = new_env; |
| 6942 | dump_cmd_in_x_mode(new_env); |
| 6943 | dump_cmd_in_x_mode(argv_expanded); |
| 6944 | if (old_vars_p) |
| 6945 | *old_vars_p = set_vars_and_save_old(new_env); |
| 6946 | } |
| 6947 | return rcode; |
| 6948 | } |
| 6949 | static NOINLINE int run_pipe(struct pipe *pi) |
| 6950 | { |
| 6951 | static const char *const null_ptr = NULL; |
| 6952 | |
| 6953 | int cmd_no; |
| 6954 | int next_infd; |
| 6955 | struct command *command; |
| 6956 | char **argv_expanded; |
| 6957 | char **argv; |
| 6958 | /* it is not always needed, but we aim to smaller code */ |
| 6959 | int squirrel[] = { -1, -1, -1 }; |
| 6960 | int rcode; |
| 6961 | |
| 6962 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 6963 | debug_enter(); |
| 6964 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6965 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 6966 | * Result should be 3 lines: q w e, qwe, q w e |
| 6967 | */ |
| 6968 | G.ifs = get_local_var_value("IFS"); |
| 6969 | if (!G.ifs) |
| 6970 | G.ifs = defifs; |
| 6971 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6972 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 6973 | pi->stopped_cmds = 0; |
| 6974 | command = &pi->cmds[0]; |
| 6975 | argv_expanded = NULL; |
| 6976 | |
| 6977 | if (pi->num_cmds != 1 |
| 6978 | || pi->followup == PIPE_BG |
| 6979 | || command->cmd_type == CMD_SUBSHELL |
| 6980 | ) { |
| 6981 | goto must_fork; |
| 6982 | } |
| 6983 | |
| 6984 | pi->alive_cmds = 1; |
| 6985 | |
| 6986 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 6987 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 6988 | |
| 6989 | if (command->group) { |
| 6990 | #if ENABLE_HUSH_FUNCTIONS |
| 6991 | if (command->cmd_type == CMD_FUNCDEF) { |
| 6992 | /* "executing" func () { list } */ |
| 6993 | struct function *funcp; |
| 6994 | |
| 6995 | funcp = new_function(command->argv[0]); |
| 6996 | /* funcp->name is already set to argv[0] */ |
| 6997 | funcp->body = command->group; |
| 6998 | # if !BB_MMU |
| 6999 | funcp->body_as_string = command->group_as_string; |
| 7000 | command->group_as_string = NULL; |
| 7001 | # endif |
| 7002 | command->group = NULL; |
| 7003 | command->argv[0] = NULL; |
| 7004 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 7005 | funcp->parent_cmd = command; |
| 7006 | command->child_func = funcp; |
| 7007 | |
| 7008 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 7009 | debug_leave(); |
| 7010 | return EXIT_SUCCESS; |
| 7011 | } |
| 7012 | #endif |
| 7013 | /* { list } */ |
| 7014 | debug_printf("non-subshell group\n"); |
| 7015 | rcode = 1; /* exitcode if redir failed */ |
| 7016 | if (setup_redirects(command, squirrel) == 0) { |
| 7017 | debug_printf_exec(": run_list\n"); |
| 7018 | rcode = run_list(command->group) & 0xff; |
| 7019 | } |
| 7020 | restore_redirects(squirrel); |
| 7021 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7022 | debug_leave(); |
| 7023 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7024 | return rcode; |
| 7025 | } |
| 7026 | |
| 7027 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 7028 | { |
| 7029 | const struct built_in_command *x; |
| 7030 | #if ENABLE_HUSH_FUNCTIONS |
| 7031 | const struct function *funcp; |
| 7032 | #else |
| 7033 | enum { funcp = 0 }; |
| 7034 | #endif |
| 7035 | char **new_env = NULL; |
| 7036 | struct variable *old_vars = NULL; |
| 7037 | |
| 7038 | if (argv[command->assignment_cnt] == NULL) { |
| 7039 | /* Assignments, but no command */ |
| 7040 | /* Ensure redirects take effect (that is, create files). |
| 7041 | * Try "a=t >file" */ |
| 7042 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 7043 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 7044 | /* Set shell variables */ |
| 7045 | if (new_env) { |
| 7046 | argv = new_env; |
| 7047 | while (*argv) { |
| 7048 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7049 | /* Do we need to flag set_local_var() errors? |
| 7050 | * "assignment to readonly var" and "putenv error" |
| 7051 | */ |
| 7052 | argv++; |
| 7053 | } |
| 7054 | } |
| 7055 | /* Redirect error sets $? to 1. Otherwise, |
| 7056 | * if evaluating assignment value set $?, retain it. |
| 7057 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7058 | if (rcode == 0) |
| 7059 | rcode = G.last_exitcode; |
| 7060 | /* Exit, _skipping_ variable restoring code: */ |
| 7061 | goto clean_up_and_ret0; |
| 7062 | |
| 7063 | #else /* Older, bigger, but more correct code */ |
| 7064 | |
| 7065 | rcode = setup_redirects(command, squirrel); |
| 7066 | restore_redirects(squirrel); |
| 7067 | /* Set shell variables */ |
| 7068 | if (G_x_mode) |
| 7069 | bb_putchar_stderr('+'); |
| 7070 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7071 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7072 | if (G_x_mode) |
| 7073 | fprintf(stderr, " %s", p); |
| 7074 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 7075 | *argv, p); |
| 7076 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7077 | /* Do we need to flag set_local_var() errors? |
| 7078 | * "assignment to readonly var" and "putenv error" |
| 7079 | */ |
| 7080 | argv++; |
| 7081 | } |
| 7082 | if (G_x_mode) |
| 7083 | bb_putchar_stderr('\n'); |
| 7084 | /* Redirect error sets $? to 1. Otherwise, |
| 7085 | * if evaluating assignment value set $?, retain it. |
| 7086 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7087 | if (rcode == 0) |
| 7088 | rcode = G.last_exitcode; |
| 7089 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7090 | debug_leave(); |
| 7091 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7092 | return rcode; |
| 7093 | #endif |
| 7094 | } |
| 7095 | |
| 7096 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7097 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7098 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7099 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7100 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7101 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7102 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7103 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 7104 | } |
| 7105 | |
| 7106 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 7107 | if (!argv_expanded[0]) { |
| 7108 | free(argv_expanded); |
| 7109 | debug_leave(); |
| 7110 | return G.last_exitcode; |
| 7111 | } |
| 7112 | |
| 7113 | x = find_builtin(argv_expanded[0]); |
| 7114 | #if ENABLE_HUSH_FUNCTIONS |
| 7115 | funcp = NULL; |
| 7116 | if (!x) |
| 7117 | funcp = find_function(argv_expanded[0]); |
| 7118 | #endif |
| 7119 | if (x || funcp) { |
| 7120 | if (!funcp) { |
| 7121 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 7122 | debug_printf("exec with redirects only\n"); |
| 7123 | rcode = setup_redirects(command, NULL); |
| 7124 | goto clean_up_and_ret1; |
| 7125 | } |
| 7126 | } |
| 7127 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7128 | if (rcode == 0) { |
| 7129 | if (!funcp) { |
| 7130 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 7131 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7132 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7133 | rcode = x->b_function(argv_expanded) & 0xff; |
| 7134 | fflush_all(); |
| 7135 | } |
| 7136 | #if ENABLE_HUSH_FUNCTIONS |
| 7137 | else { |
| 7138 | # if ENABLE_HUSH_LOCAL |
| 7139 | struct variable **sv; |
| 7140 | sv = G.shadowed_vars_pp; |
| 7141 | G.shadowed_vars_pp = &old_vars; |
| 7142 | # endif |
| 7143 | debug_printf_exec(": function '%s' '%s'...\n", |
| 7144 | funcp->name, argv_expanded[1]); |
| 7145 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 7146 | # if ENABLE_HUSH_LOCAL |
| 7147 | G.shadowed_vars_pp = sv; |
| 7148 | # endif |
| 7149 | } |
| 7150 | #endif |
| 7151 | } |
| 7152 | clean_up_and_ret: |
| 7153 | unset_vars(new_env); |
| 7154 | add_vars(old_vars); |
| 7155 | /* clean_up_and_ret0: */ |
| 7156 | restore_redirects(squirrel); |
| 7157 | clean_up_and_ret1: |
| 7158 | free(argv_expanded); |
| 7159 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7160 | debug_leave(); |
| 7161 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 7162 | return rcode; |
| 7163 | } |
| 7164 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7165 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7166 | int n = find_applet_by_name(argv_expanded[0]); |
| 7167 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 7168 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7169 | if (rcode == 0) { |
| 7170 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 7171 | argv_expanded[0], argv_expanded[1]); |
| 7172 | rcode = run_nofork_applet(n, argv_expanded); |
| 7173 | } |
| 7174 | goto clean_up_and_ret; |
| 7175 | } |
| 7176 | } |
| 7177 | /* It is neither builtin nor applet. We must fork. */ |
| 7178 | } |
| 7179 | |
| 7180 | must_fork: |
| 7181 | /* NB: argv_expanded may already be created, and that |
| 7182 | * might include `cmd` runs! Do not rerun it! We *must* |
| 7183 | * use argv_expanded if it's non-NULL */ |
| 7184 | |
| 7185 | /* Going to fork a child per each pipe member */ |
| 7186 | pi->alive_cmds = 0; |
| 7187 | next_infd = 0; |
| 7188 | |
| 7189 | cmd_no = 0; |
| 7190 | while (cmd_no < pi->num_cmds) { |
| 7191 | struct fd_pair pipefds; |
| 7192 | #if !BB_MMU |
| 7193 | volatile nommu_save_t nommu_save; |
| 7194 | nommu_save.new_env = NULL; |
| 7195 | nommu_save.old_vars = NULL; |
| 7196 | nommu_save.argv = NULL; |
| 7197 | nommu_save.argv_from_re_execing = NULL; |
| 7198 | #endif |
| 7199 | command = &pi->cmds[cmd_no]; |
| 7200 | cmd_no++; |
| 7201 | if (command->argv) { |
| 7202 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 7203 | command->argv[0], command->argv[1]); |
| 7204 | } else { |
| 7205 | debug_printf_exec(": pipe member with no argv\n"); |
| 7206 | } |
| 7207 | |
| 7208 | /* pipes are inserted between pairs of commands */ |
| 7209 | pipefds.rd = 0; |
| 7210 | pipefds.wr = 1; |
| 7211 | if (cmd_no < pi->num_cmds) |
| 7212 | xpiped_pair(pipefds); |
| 7213 | |
| 7214 | command->pid = BB_MMU ? fork() : vfork(); |
| 7215 | if (!command->pid) { /* child */ |
| 7216 | #if ENABLE_HUSH_JOB |
| 7217 | disable_restore_tty_pgrp_on_exit(); |
| 7218 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7219 | |
| 7220 | /* Every child adds itself to new process group |
| 7221 | * with pgid == pid_of_first_child_in_pipe */ |
| 7222 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7223 | pid_t pgrp; |
| 7224 | pgrp = pi->pgrp; |
| 7225 | if (pgrp < 0) /* true for 1st process only */ |
| 7226 | pgrp = getpid(); |
| 7227 | if (setpgid(0, pgrp) == 0 |
| 7228 | && pi->followup != PIPE_BG |
| 7229 | && G_saved_tty_pgrp /* we have ctty */ |
| 7230 | ) { |
| 7231 | /* We do it in *every* child, not just first, |
| 7232 | * to avoid races */ |
| 7233 | tcsetpgrp(G_interactive_fd, pgrp); |
| 7234 | } |
| 7235 | } |
| 7236 | #endif |
| 7237 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 7238 | /* 1st cmd in backgrounded pipe |
| 7239 | * should have its stdin /dev/null'ed */ |
| 7240 | close(0); |
| 7241 | if (open(bb_dev_null, O_RDONLY)) |
| 7242 | xopen("/", O_RDONLY); |
| 7243 | } else { |
| 7244 | xmove_fd(next_infd, 0); |
| 7245 | } |
| 7246 | xmove_fd(pipefds.wr, 1); |
| 7247 | if (pipefds.rd > 1) |
| 7248 | close(pipefds.rd); |
| 7249 | /* Like bash, explicit redirects override pipes, |
| 7250 | * and the pipe fd is available for dup'ing. */ |
| 7251 | if (setup_redirects(command, NULL)) |
| 7252 | _exit(1); |
| 7253 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7254 | /* Stores to nommu_save list of env vars putenv'ed |
| 7255 | * (NOMMU, on MMU we don't need that) */ |
| 7256 | /* cast away volatility... */ |
| 7257 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 7258 | /* pseudo_exec() does not return */ |
| 7259 | } |
| 7260 | |
| 7261 | /* parent or error */ |
| 7262 | #if ENABLE_HUSH_FAST |
| 7263 | G.count_SIGCHLD++; |
| 7264 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7265 | #endif |
| 7266 | enable_restore_tty_pgrp_on_exit(); |
| 7267 | #if !BB_MMU |
| 7268 | /* Clean up after vforked child */ |
| 7269 | free(nommu_save.argv); |
| 7270 | free(nommu_save.argv_from_re_execing); |
| 7271 | unset_vars(nommu_save.new_env); |
| 7272 | add_vars(nommu_save.old_vars); |
| 7273 | #endif |
| 7274 | free(argv_expanded); |
| 7275 | argv_expanded = NULL; |
| 7276 | if (command->pid < 0) { /* [v]fork failed */ |
| 7277 | /* Clearly indicate, was it fork or vfork */ |
| 7278 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 7279 | } else { |
| 7280 | pi->alive_cmds++; |
| 7281 | #if ENABLE_HUSH_JOB |
| 7282 | /* Second and next children need to know pid of first one */ |
| 7283 | if (pi->pgrp < 0) |
| 7284 | pi->pgrp = command->pid; |
| 7285 | #endif |
| 7286 | } |
| 7287 | |
| 7288 | if (cmd_no > 1) |
| 7289 | close(next_infd); |
| 7290 | if (cmd_no < pi->num_cmds) |
| 7291 | close(pipefds.wr); |
| 7292 | /* Pass read (output) pipe end to next iteration */ |
| 7293 | next_infd = pipefds.rd; |
| 7294 | } |
| 7295 | |
| 7296 | if (!pi->alive_cmds) { |
| 7297 | debug_leave(); |
| 7298 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7299 | return 1; |
| 7300 | } |
| 7301 | |
| 7302 | debug_leave(); |
| 7303 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7304 | return -1; |
| 7305 | } |
| 7306 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7307 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7308 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7309 | static int run_list(struct pipe *pi) |
| 7310 | { |
| 7311 | #if ENABLE_HUSH_CASE |
| 7312 | char *case_word = NULL; |
| 7313 | #endif |
| 7314 | #if ENABLE_HUSH_LOOPS |
| 7315 | struct pipe *loop_top = NULL; |
| 7316 | char **for_lcur = NULL; |
| 7317 | char **for_list = NULL; |
| 7318 | #endif |
| 7319 | smallint last_followup; |
| 7320 | smalluint rcode; |
| 7321 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7322 | smalluint cond_code = 0; |
| 7323 | #else |
| 7324 | enum { cond_code = 0 }; |
| 7325 | #endif |
| 7326 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7327 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7328 | smallint last_rword; /* ditto */ |
| 7329 | #endif |
| 7330 | |
| 7331 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7332 | debug_enter(); |
| 7333 | |
| 7334 | #if ENABLE_HUSH_LOOPS |
| 7335 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7336 | { |
| 7337 | struct pipe *cpipe; |
| 7338 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7339 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7340 | continue; |
| 7341 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7342 | if (cpipe->next == NULL) { |
| 7343 | syntax_error("malformed for"); |
| 7344 | debug_leave(); |
| 7345 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7346 | return 1; |
| 7347 | } |
| 7348 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7349 | if (cpipe->next->res_word == RES_DO) |
| 7350 | continue; |
| 7351 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7352 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7353 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7354 | ) { |
| 7355 | syntax_error("malformed for"); |
| 7356 | debug_leave(); |
| 7357 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7358 | return 1; |
| 7359 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7360 | } |
| 7361 | } |
| 7362 | #endif |
| 7363 | |
| 7364 | /* Past this point, all code paths should jump to ret: label |
| 7365 | * in order to return, no direct "return" statements please. |
| 7366 | * This helps to ensure that no memory is leaked. */ |
| 7367 | |
| 7368 | #if ENABLE_HUSH_JOB |
| 7369 | G.run_list_level++; |
| 7370 | #endif |
| 7371 | |
| 7372 | #if HAS_KEYWORDS |
| 7373 | rword = RES_NONE; |
| 7374 | last_rword = RES_XXXX; |
| 7375 | #endif |
| 7376 | last_followup = PIPE_SEQ; |
| 7377 | rcode = G.last_exitcode; |
| 7378 | |
| 7379 | /* Go through list of pipes, (maybe) executing them. */ |
| 7380 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 7381 | if (G.flag_SIGINT) |
| 7382 | break; |
| 7383 | |
| 7384 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7385 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7386 | rword, cond_code, last_rword); |
| 7387 | #if ENABLE_HUSH_LOOPS |
| 7388 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7389 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7390 | ) { |
| 7391 | /* start of a loop: remember where loop starts */ |
| 7392 | loop_top = pi; |
| 7393 | G.depth_of_loop++; |
| 7394 | } |
| 7395 | #endif |
| 7396 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7397 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7398 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7399 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7400 | ) { |
| 7401 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7402 | * and we should not execute CMD */ |
| 7403 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7404 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7405 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7406 | } |
| 7407 | } |
| 7408 | last_followup = pi->followup; |
| 7409 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7410 | #if ENABLE_HUSH_IF |
| 7411 | if (cond_code) { |
| 7412 | if (rword == RES_THEN) { |
| 7413 | /* if false; then ... fi has exitcode 0! */ |
| 7414 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7415 | /* "if <false> THEN cmd": skip cmd */ |
| 7416 | continue; |
| 7417 | } |
| 7418 | } else { |
| 7419 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7420 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7421 | * skip cmd and all following ones */ |
| 7422 | break; |
| 7423 | } |
| 7424 | } |
| 7425 | #endif |
| 7426 | #if ENABLE_HUSH_LOOPS |
| 7427 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7428 | if (!for_lcur) { |
| 7429 | /* first loop through for */ |
| 7430 | |
| 7431 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7432 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7433 | }; /* encoded representation of "$@" */ |
| 7434 | static const char *const encoded_dollar_at_argv[] = { |
| 7435 | encoded_dollar_at, NULL |
| 7436 | }; /* argv list with one element: "$@" */ |
| 7437 | char **vals; |
| 7438 | |
| 7439 | vals = (char**)encoded_dollar_at_argv; |
| 7440 | if (pi->next->res_word == RES_IN) { |
| 7441 | /* if no variable values after "in" we skip "for" */ |
| 7442 | if (!pi->next->cmds[0].argv) { |
| 7443 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7444 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7445 | break; |
| 7446 | } |
| 7447 | vals = pi->next->cmds[0].argv; |
| 7448 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7449 | /* create list of variable values */ |
| 7450 | debug_print_strings("for_list made from", vals); |
| 7451 | for_list = expand_strvec_to_strvec(vals); |
| 7452 | for_lcur = for_list; |
| 7453 | debug_print_strings("for_list", for_list); |
| 7454 | } |
| 7455 | if (!*for_lcur) { |
| 7456 | /* "for" loop is over, clean up */ |
| 7457 | free(for_list); |
| 7458 | for_list = NULL; |
| 7459 | for_lcur = NULL; |
| 7460 | break; |
| 7461 | } |
| 7462 | /* Insert next value from for_lcur */ |
| 7463 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7464 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7465 | continue; |
| 7466 | } |
| 7467 | if (rword == RES_IN) { |
| 7468 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7469 | } |
| 7470 | if (rword == RES_DONE) { |
| 7471 | continue; /* "done" has no cmds too */ |
| 7472 | } |
| 7473 | #endif |
| 7474 | #if ENABLE_HUSH_CASE |
| 7475 | if (rword == RES_CASE) { |
| 7476 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7477 | continue; |
| 7478 | } |
| 7479 | if (rword == RES_MATCH) { |
| 7480 | char **argv; |
| 7481 | |
| 7482 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7483 | break; |
| 7484 | /* all prev words didn't match, does this one match? */ |
| 7485 | argv = pi->cmds->argv; |
| 7486 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7487 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7488 | /* TODO: which FNM_xxx flags to use? */ |
| 7489 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7490 | free(pattern); |
| 7491 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7492 | free(case_word); /* make future "word)" stop */ |
| 7493 | case_word = NULL; |
| 7494 | break; |
| 7495 | } |
| 7496 | argv++; |
| 7497 | } |
| 7498 | continue; |
| 7499 | } |
| 7500 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7501 | if (cond_code != 0) |
| 7502 | continue; /* not matched yet, skip this pipe */ |
| 7503 | } |
| 7504 | #endif |
| 7505 | /* Just pressing <enter> in shell should check for jobs. |
| 7506 | * OTOH, in non-interactive shell this is useless |
| 7507 | * and only leads to extra job checks */ |
| 7508 | if (pi->num_cmds == 0) { |
| 7509 | if (G_interactive_fd) |
| 7510 | goto check_jobs_and_continue; |
| 7511 | continue; |
| 7512 | } |
| 7513 | |
| 7514 | /* After analyzing all keywords and conditions, we decided |
| 7515 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7516 | * after run_pipe to collect any background children, |
| 7517 | * even if list execution is to be stopped. */ |
| 7518 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7519 | { |
| 7520 | int r; |
| 7521 | #if ENABLE_HUSH_LOOPS |
| 7522 | G.flag_break_continue = 0; |
| 7523 | #endif |
| 7524 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7525 | if (r != -1) { |
| 7526 | /* We ran a builtin, function, or group. |
| 7527 | * rcode is already known |
| 7528 | * and we don't need to wait for anything. */ |
| 7529 | G.last_exitcode = rcode; |
| 7530 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7531 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7532 | #if ENABLE_HUSH_LOOPS |
| 7533 | /* Was it "break" or "continue"? */ |
| 7534 | if (G.flag_break_continue) { |
| 7535 | smallint fbc = G.flag_break_continue; |
| 7536 | /* We might fall into outer *loop*, |
| 7537 | * don't want to break it too */ |
| 7538 | if (loop_top) { |
| 7539 | G.depth_break_continue--; |
| 7540 | if (G.depth_break_continue == 0) |
| 7541 | G.flag_break_continue = 0; |
| 7542 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7543 | } /* 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] | 7544 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
| 7545 | checkjobs(NULL); |
| 7546 | break; |
| 7547 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7548 | /* "continue": simulate end of loop */ |
| 7549 | rword = RES_DONE; |
| 7550 | continue; |
| 7551 | } |
| 7552 | #endif |
| 7553 | #if ENABLE_HUSH_FUNCTIONS |
| 7554 | if (G.flag_return_in_progress == 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7555 | checkjobs(NULL); |
| 7556 | break; |
| 7557 | } |
| 7558 | #endif |
| 7559 | } else if (pi->followup == PIPE_BG) { |
| 7560 | /* What does bash do with attempts to background builtins? */ |
| 7561 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7562 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7563 | * I'm NOT treating inner &'s as jobs */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7564 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7565 | #if ENABLE_HUSH_JOB |
| 7566 | if (G.run_list_level == 1) |
| 7567 | insert_bg_job(pi); |
| 7568 | #endif |
| 7569 | /* Last command's pid goes to $! */ |
| 7570 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7571 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7572 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7573 | } else { |
| 7574 | #if ENABLE_HUSH_JOB |
| 7575 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7576 | /* Waits for completion, then fg's main shell */ |
| 7577 | rcode = checkjobs_and_fg_shell(pi); |
| 7578 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7579 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7580 | } else |
| 7581 | #endif |
| 7582 | { /* This one just waits for completion */ |
| 7583 | rcode = checkjobs(pi); |
| 7584 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7585 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7586 | } |
| 7587 | G.last_exitcode = rcode; |
| 7588 | } |
| 7589 | } |
| 7590 | |
| 7591 | /* Analyze how result affects subsequent commands */ |
| 7592 | #if ENABLE_HUSH_IF |
| 7593 | if (rword == RES_IF || rword == RES_ELIF) |
| 7594 | cond_code = rcode; |
| 7595 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7596 | check_jobs_and_continue: |
| 7597 | checkjobs(NULL); |
| 7598 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7599 | #if ENABLE_HUSH_LOOPS |
| 7600 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7601 | if (pi->next |
| 7602 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 7603 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 7604 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7605 | if (rword == RES_WHILE) { |
| 7606 | if (rcode) { |
| 7607 | /* "while false; do...done" - exitcode 0 */ |
| 7608 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7609 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7610 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7611 | } |
| 7612 | } |
| 7613 | if (rword == RES_UNTIL) { |
| 7614 | if (!rcode) { |
| 7615 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7616 | break; |
| 7617 | } |
| 7618 | } |
| 7619 | } |
| 7620 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7621 | } /* for (pi) */ |
| 7622 | |
| 7623 | #if ENABLE_HUSH_JOB |
| 7624 | G.run_list_level--; |
| 7625 | #endif |
| 7626 | #if ENABLE_HUSH_LOOPS |
| 7627 | if (loop_top) |
| 7628 | G.depth_of_loop--; |
| 7629 | free(for_list); |
| 7630 | #endif |
| 7631 | #if ENABLE_HUSH_CASE |
| 7632 | free(case_word); |
| 7633 | #endif |
| 7634 | debug_leave(); |
| 7635 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7636 | return rcode; |
| 7637 | } |
| 7638 | |
| 7639 | /* Select which version we will use */ |
| 7640 | static int run_and_free_list(struct pipe *pi) |
| 7641 | { |
| 7642 | int rcode = 0; |
| 7643 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7644 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7645 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7646 | rcode = run_list(pi); |
| 7647 | } |
| 7648 | /* free_pipe_list has the side effect of clearing memory. |
| 7649 | * In the long run that function can be merged with run_list, |
| 7650 | * but doing that now would hobble the debugging effort. */ |
| 7651 | free_pipe_list(pi); |
| 7652 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 7653 | return rcode; |
| 7654 | } |
| 7655 | |
| 7656 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7657 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7658 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7659 | sighandler_t old_handler; |
| 7660 | unsigned sig = 0; |
| 7661 | while ((mask >>= 1) != 0) { |
| 7662 | sig++; |
| 7663 | if (!(mask & 1)) |
| 7664 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7665 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7666 | /* POSIX allows shell to re-enable SIGCHLD |
| 7667 | * even if it was SIG_IGN on entry. |
| 7668 | * Therefore we skip IGN check for it: |
| 7669 | */ |
| 7670 | if (sig == SIGCHLD) |
| 7671 | continue; |
| 7672 | if (old_handler == SIG_IGN) { |
| 7673 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7674 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7675 | if (!G.traps) |
| 7676 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7677 | free(G.traps[sig]); |
| 7678 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7679 | } |
| 7680 | } |
| 7681 | } |
| 7682 | |
| 7683 | /* Called a few times only (or even once if "sh -c") */ |
| 7684 | static void install_special_sighandlers(void) |
| 7685 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7686 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7687 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7688 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7689 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7690 | if (G_interactive_fd) { |
| 7691 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 7692 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7693 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7694 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7695 | /* Careful, do not re-install handlers we already installed */ |
| 7696 | if (G.special_sig_mask != mask) { |
| 7697 | unsigned diff = mask & ~G.special_sig_mask; |
| 7698 | G.special_sig_mask = mask; |
| 7699 | install_sighandlers(diff); |
| 7700 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7701 | } |
| 7702 | |
| 7703 | #if ENABLE_HUSH_JOB |
| 7704 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7705 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7706 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7707 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7708 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7709 | |
| 7710 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7711 | mask = 0 |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7712 | + (1 << SIGILL ) * HUSH_DEBUG |
| 7713 | + (1 << SIGFPE ) * HUSH_DEBUG |
| 7714 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 7715 | + (1 << SIGSEGV) * HUSH_DEBUG |
| 7716 | + (1 << SIGTRAP) * HUSH_DEBUG |
| 7717 | + (1 << SIGABRT) |
| 7718 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7719 | + (1 << SIGPIPE) |
| 7720 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7721 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7722 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7723 | * we never want to restore pgrp on exit, and this fn is not called |
| 7724 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7725 | /*+ (1 << SIGHUP )*/ |
| 7726 | /*+ (1 << SIGTERM)*/ |
| 7727 | /*+ (1 << SIGINT )*/ |
| 7728 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7729 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 7730 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7731 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7732 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7733 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7734 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7735 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7736 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7737 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7738 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7739 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7740 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7741 | break; |
| 7742 | case 'x': |
| 7743 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 7744 | break; |
| 7745 | case 'o': |
| 7746 | if (!o_opt) { |
| 7747 | /* "set -+o" without parameter. |
| 7748 | * in bash, set -o produces this output: |
| 7749 | * pipefail off |
| 7750 | * and set +o: |
| 7751 | * set +o pipefail |
| 7752 | * We always use the second form. |
| 7753 | */ |
| 7754 | const char *p = o_opt_strings; |
| 7755 | idx = 0; |
| 7756 | while (*p) { |
| 7757 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 7758 | idx++; |
| 7759 | p += strlen(p) + 1; |
| 7760 | } |
| 7761 | break; |
| 7762 | } |
| 7763 | idx = index_in_strings(o_opt_strings, o_opt); |
| 7764 | if (idx >= 0) { |
| 7765 | G.o_opt[idx] = state; |
| 7766 | break; |
| 7767 | } |
| 7768 | default: |
| 7769 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7770 | } |
| 7771 | return EXIT_SUCCESS; |
| 7772 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7773 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7774 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7775 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7776 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7777 | enum { |
| 7778 | OPT_login = (1 << 0), |
| 7779 | }; |
| 7780 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7781 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7782 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7783 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7784 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7785 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7786 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7787 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7788 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7789 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7790 | #if ENABLE_HUSH_FAST |
| 7791 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 7792 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7793 | #if !BB_MMU |
| 7794 | G.argv0_for_re_execing = argv[0]; |
| 7795 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7796 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7797 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 7798 | shell_ver->flg_export = 1; |
| 7799 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 7800 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 7801 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7802 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7803 | /* Create shell local variables from the values |
| 7804 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7805 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7806 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7807 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7808 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7809 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7810 | if (e) while (*e) { |
| 7811 | char *value = strchr(*e, '='); |
| 7812 | if (value) { /* paranoia */ |
| 7813 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7814 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7815 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7816 | cur_var->max_len = strlen(*e); |
| 7817 | cur_var->flg_export = 1; |
| 7818 | } |
| 7819 | e++; |
| 7820 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7821 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 7822 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 7823 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7824 | |
| 7825 | /* Export PWD */ |
| 7826 | set_pwd_var(/*exp:*/ 1); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 7827 | |
| 7828 | #if ENABLE_HUSH_BASH_COMPAT |
| 7829 | /* Set (but not export) HOSTNAME unless already set */ |
| 7830 | if (!get_local_var_value("HOSTNAME")) { |
| 7831 | struct utsname uts; |
| 7832 | uname(&uts); |
| 7833 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 7834 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7835 | /* bash also exports SHLVL and _, |
| 7836 | * and sets (but doesn't export) the following variables: |
| 7837 | * BASH=/bin/bash |
| 7838 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7839 | * BASH_VERSION='3.2.0(1)-release' |
| 7840 | * HOSTTYPE=i386 |
| 7841 | * MACHTYPE=i386-pc-linux-gnu |
| 7842 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7843 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7844 | * EUID=<NNNNN> |
| 7845 | * UID=<NNNNN> |
| 7846 | * GROUPS=() |
| 7847 | * LINES=<NNN> |
| 7848 | * COLUMNS=<NNN> |
| 7849 | * BASH_ARGC=() |
| 7850 | * BASH_ARGV=() |
| 7851 | * BASH_LINENO=() |
| 7852 | * BASH_SOURCE=() |
| 7853 | * DIRSTACK=() |
| 7854 | * PIPESTATUS=([0]="0") |
| 7855 | * HISTFILE=/<xxx>/.bash_history |
| 7856 | * HISTFILESIZE=500 |
| 7857 | * HISTSIZE=500 |
| 7858 | * MAILCHECK=60 |
| 7859 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7860 | * SHELL=/bin/bash |
| 7861 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7862 | * TERM=dumb |
| 7863 | * OPTERR=1 |
| 7864 | * OPTIND=1 |
| 7865 | * IFS=$' \t\n' |
| 7866 | * PS1='\s-\v\$ ' |
| 7867 | * PS2='> ' |
| 7868 | * PS4='+ ' |
| 7869 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 7870 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7871 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7872 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 7873 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7874 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7875 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7876 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7877 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7878 | |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 7879 | die_func = xfunc_has_died; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7880 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7881 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7882 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7883 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7884 | * 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] | 7885 | * in order to intercept (more) signals. |
| 7886 | */ |
| 7887 | |
| 7888 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7889 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7890 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7891 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7892 | while (1) { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7893 | opt = getopt(argc, argv, "+c:xinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7894 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7895 | "<:$:R:V:" |
| 7896 | # if ENABLE_HUSH_FUNCTIONS |
| 7897 | "F:" |
| 7898 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7899 | #endif |
| 7900 | ); |
| 7901 | if (opt <= 0) |
| 7902 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7903 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7904 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7905 | /* Possibilities: |
| 7906 | * sh ... -c 'script' |
| 7907 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7908 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7909 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7910 | * "" needs to be replaced with NULL |
| 7911 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7912 | * Note: the form without ARG0 never happens: |
| 7913 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7914 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7915 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7916 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7917 | G.root_ppid = getppid(); |
| 7918 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7919 | G.global_argv = argv + optind; |
| 7920 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7921 | if (builtin_argc) { |
| 7922 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7923 | const struct built_in_command *x; |
| 7924 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7925 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7926 | x = find_builtin(optarg); |
| 7927 | if (x) { /* paranoia */ |
| 7928 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7929 | G.global_argv += builtin_argc; |
| 7930 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7931 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7932 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7933 | } |
| 7934 | goto final_return; |
| 7935 | } |
| 7936 | if (!G.global_argv[0]) { |
| 7937 | /* -c 'script' (no params): prevent empty $0 */ |
| 7938 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7939 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7940 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7941 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7942 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7943 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7944 | goto final_return; |
| 7945 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7946 | /* Well, we cannot just declare interactiveness, |
| 7947 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7948 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7949 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7950 | case 's': |
| 7951 | /* "-s" means "read from stdin", but this is how we always |
| 7952 | * operate, so simply do nothing here. */ |
| 7953 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7954 | case 'l': |
| 7955 | flags |= OPT_login; |
| 7956 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7957 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7958 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7959 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7960 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7961 | case '$': { |
| 7962 | unsigned long long empty_trap_mask; |
| 7963 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7964 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7965 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7966 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7967 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7968 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7969 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7970 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7971 | optarg++; |
| 7972 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7973 | optarg++; |
| 7974 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7975 | if (empty_trap_mask != 0) { |
| 7976 | int sig; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7977 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7978 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7979 | for (sig = 1; sig < NSIG; sig++) { |
| 7980 | if (empty_trap_mask & (1LL << sig)) { |
| 7981 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7982 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7983 | } |
| 7984 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7985 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7986 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7987 | optarg++; |
| 7988 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7989 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7990 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7991 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7992 | case 'R': |
| 7993 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7994 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7995 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7996 | # if ENABLE_HUSH_FUNCTIONS |
| 7997 | case 'F': { |
| 7998 | struct function *funcp = new_function(optarg); |
| 7999 | /* funcp->name is already set to optarg */ |
| 8000 | /* funcp->body is set to NULL. It's a special case. */ |
| 8001 | funcp->body_as_string = argv[optind]; |
| 8002 | optind++; |
| 8003 | break; |
| 8004 | } |
| 8005 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8006 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8007 | case 'n': |
| 8008 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8009 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8010 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8011 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8012 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8013 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 8014 | " or: sh -c command [args]...\n\n"); |
| 8015 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8016 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8017 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8018 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8019 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8020 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8021 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8022 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 8023 | G.global_argc = argc - (optind - 1); |
| 8024 | G.global_argv = argv + (optind - 1); |
| 8025 | G.global_argv[0] = argv[0]; |
| 8026 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8027 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8028 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8029 | G.root_ppid = getppid(); |
| 8030 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8031 | |
| 8032 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8033 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8034 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8035 | debug_printf("sourcing /etc/profile\n"); |
| 8036 | input = fopen_for_read("/etc/profile"); |
| 8037 | if (input != NULL) { |
| 8038 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8039 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8040 | parse_and_run_file(input); |
| 8041 | fclose(input); |
| 8042 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8043 | /* bash: after sourcing /etc/profile, |
| 8044 | * tries to source (in the given order): |
| 8045 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8046 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8047 | * bash also sources ~/.bash_logout on exit. |
| 8048 | * If called as sh, skips .bash_XXX files. |
| 8049 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8050 | } |
| 8051 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8052 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8053 | FILE *input; |
| 8054 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8055 | * "bash <script>" (which is never interactive (unless -i?)) |
| 8056 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8057 | * If called as sh, does the same but with $ENV. |
| 8058 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8059 | G.global_argc--; |
| 8060 | G.global_argv++; |
| 8061 | debug_printf("running script '%s'\n", G.global_argv[0]); |
| 8062 | input = xfopen_for_read(G.global_argv[0]); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8063 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8064 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8065 | parse_and_run_file(input); |
| 8066 | #if ENABLE_FEATURE_CLEAN_UP |
| 8067 | fclose(input); |
| 8068 | #endif |
| 8069 | goto final_return; |
| 8070 | } |
| 8071 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8072 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8073 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8074 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8075 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8076 | /* A shell is interactive if the '-i' flag was given, |
| 8077 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 8078 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8079 | * no arguments remaining or the -s flag given |
| 8080 | * standard input is a terminal |
| 8081 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8082 | * Refer to Posix.2, the description of the 'sh' utility. |
| 8083 | */ |
| 8084 | #if ENABLE_HUSH_JOB |
| 8085 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8086 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 8087 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 8088 | if (G_saved_tty_pgrp < 0) |
| 8089 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8090 | |
| 8091 | /* try to dup stdin to high fd#, >= 255 */ |
| 8092 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8093 | if (G_interactive_fd < 0) { |
| 8094 | /* try to dup to any fd */ |
| 8095 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8096 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8097 | /* give up */ |
| 8098 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8099 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 8100 | } |
| 8101 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8102 | // TODO: track & disallow any attempts of user |
| 8103 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8104 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8105 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8106 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8107 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8108 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8109 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8110 | /* If we were run as 'hush &', sleep until we are |
| 8111 | * in the foreground (tty pgrp == our pgrp). |
| 8112 | * If we get started under a job aware app (like bash), |
| 8113 | * make sure we are now in charge so we don't fight over |
| 8114 | * who gets the foreground */ |
| 8115 | while (1) { |
| 8116 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8117 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 8118 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8119 | break; |
| 8120 | /* send TTIN to ourself (should stop us) */ |
| 8121 | kill(- shell_pgrp, SIGTTIN); |
| 8122 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8123 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8124 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8125 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8126 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8127 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8128 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8129 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8130 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8131 | /* Put ourselves in our own process group |
| 8132 | * (bash, too, does this only if ctty is available) */ |
| 8133 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 8134 | /* Grab control of the terminal */ |
| 8135 | tcsetpgrp(G_interactive_fd, getpid()); |
| 8136 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 8137 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8138 | |
| 8139 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 8140 | { |
| 8141 | const char *hp = get_local_var_value("HISTFILE"); |
| 8142 | if (!hp) { |
| 8143 | hp = get_local_var_value("HOME"); |
| 8144 | if (hp) |
| 8145 | hp = concat_path_file(hp, ".hush_history"); |
| 8146 | } else { |
| 8147 | hp = xstrdup(hp); |
| 8148 | } |
| 8149 | if (hp) { |
| 8150 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8151 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 8152 | } |
| 8153 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 8154 | hp = get_local_var_value("HISTFILESIZE"); |
| 8155 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 8156 | # endif |
| 8157 | } |
| 8158 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8159 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8160 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8161 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8162 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8163 | /* No job control compiled in, only prompt/line editing */ |
| 8164 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8165 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8166 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8167 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8168 | G_interactive_fd = dup(STDIN_FILENO); |
| 8169 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8170 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8171 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8172 | } |
| 8173 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8174 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8175 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8176 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8177 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8178 | #else |
| 8179 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8180 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8181 | #endif |
| 8182 | /* bash: |
| 8183 | * if interactive but not a login shell, sources ~/.bashrc |
| 8184 | * (--norc turns this off, --rcfile <file> overrides) |
| 8185 | */ |
| 8186 | |
| 8187 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8188 | /* note: ash and hush share this string */ |
| 8189 | printf("\n\n%s %s\n" |
| 8190 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8191 | "\n", |
| 8192 | bb_banner, |
| 8193 | "hush - the humble shell" |
| 8194 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 8195 | } |
| 8196 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8197 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8198 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8199 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8200 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8201 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 8202 | |
| 8203 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8204 | #if ENABLE_MSH |
| 8205 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 8206 | int msh_main(int argc, char **argv) |
| 8207 | { |
| 8208 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 8209 | return hush_main(argc, argv); |
| 8210 | } |
| 8211 | #endif |
| 8212 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8213 | |
| 8214 | /* |
| 8215 | * Built-ins |
| 8216 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8217 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8218 | { |
| 8219 | return 0; |
| 8220 | } |
| 8221 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8222 | 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] | 8223 | { |
| 8224 | int argc = 0; |
| 8225 | while (*argv) { |
| 8226 | argc++; |
| 8227 | argv++; |
| 8228 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8229 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 8230 | } |
| 8231 | |
| 8232 | static int FAST_FUNC builtin_test(char **argv) |
| 8233 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8234 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8235 | } |
| 8236 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8237 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8238 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8239 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8240 | } |
| 8241 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8242 | #if ENABLE_PRINTF |
| 8243 | static int FAST_FUNC builtin_printf(char **argv) |
| 8244 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8245 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8246 | } |
| 8247 | #endif |
| 8248 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8249 | static char **skip_dash_dash(char **argv) |
| 8250 | { |
| 8251 | argv++; |
| 8252 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 8253 | argv++; |
| 8254 | return argv; |
| 8255 | } |
| 8256 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8257 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8258 | { |
| 8259 | int rcode = EXIT_SUCCESS; |
| 8260 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8261 | argv = skip_dash_dash(argv); |
| 8262 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8263 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8264 | /* bash: |
| 8265 | * eval "echo Hi; done" ("done" is syntax error): |
| 8266 | * "echo Hi" will not execute too. |
| 8267 | */ |
| 8268 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8269 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8270 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8271 | } |
| 8272 | return rcode; |
| 8273 | } |
| 8274 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8275 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8276 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8277 | const char *newdir; |
| 8278 | |
| 8279 | argv = skip_dash_dash(argv); |
| 8280 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8281 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8282 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8283 | * bash says "bash: cd: HOME not set" and does nothing |
| 8284 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8285 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 8286 | const char *home = get_local_var_value("HOME"); |
| 8287 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8288 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8289 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8290 | /* Mimic bash message exactly */ |
| 8291 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8292 | return EXIT_FAILURE; |
| 8293 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8294 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 8295 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 8296 | * set it again, but do not export. bash does the same. |
| 8297 | */ |
| 8298 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8299 | return EXIT_SUCCESS; |
| 8300 | } |
| 8301 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8302 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8303 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8304 | argv = skip_dash_dash(argv); |
| 8305 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8306 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8307 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8308 | /* Careful: we can end up here after [v]fork. Do not restore |
| 8309 | * tty pgrp then, only top-level shell process does that */ |
| 8310 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 8311 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 8312 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8313 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8314 | * 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] | 8315 | * and tcsetpgrp, and this is inherently racy. |
| 8316 | */ |
| 8317 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8318 | } |
| 8319 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8320 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8321 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8322 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8323 | |
| 8324 | /* interactive bash: |
| 8325 | * # trap "echo EEE" EXIT |
| 8326 | * # exit |
| 8327 | * exit |
| 8328 | * There are stopped jobs. |
| 8329 | * (if there are _stopped_ jobs, running ones don't count) |
| 8330 | * # exit |
| 8331 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 8332 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8333 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8334 | * 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] | 8335 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8336 | |
| 8337 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8338 | argv = skip_dash_dash(argv); |
| 8339 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8340 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8341 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8342 | xfunc_error_retval = 255; |
| 8343 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8344 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8345 | } |
| 8346 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8347 | static void print_escaped(const char *s) |
| 8348 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8349 | if (*s == '\'') |
| 8350 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8351 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8352 | const char *p = strchrnul(s, '\''); |
| 8353 | /* print 'xxxx', possibly just '' */ |
| 8354 | printf("'%.*s'", (int)(p - s), s); |
| 8355 | if (*p == '\0') |
| 8356 | break; |
| 8357 | s = p; |
| 8358 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8359 | /* s points to '; print "'''...'''" */ |
| 8360 | putchar('"'); |
| 8361 | do putchar('\''); while (*++s == '\''); |
| 8362 | putchar('"'); |
| 8363 | } while (*s); |
| 8364 | } |
| 8365 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8366 | #if !ENABLE_HUSH_LOCAL |
| 8367 | #define helper_export_local(argv, exp, lvl) \ |
| 8368 | helper_export_local(argv, exp) |
| 8369 | #endif |
| 8370 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8371 | { |
| 8372 | do { |
| 8373 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8374 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8375 | |
| 8376 | /* So far we do not check that name is valid (TODO?) */ |
| 8377 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8378 | if (*name_end == '\0') { |
| 8379 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8380 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8381 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8382 | var = vpp ? *vpp : NULL; |
| 8383 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8384 | if (exp == -1) { /* unexporting? */ |
| 8385 | /* export -n NAME (without =VALUE) */ |
| 8386 | if (var) { |
| 8387 | var->flg_export = 0; |
| 8388 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8389 | unsetenv(name); |
| 8390 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8391 | continue; |
| 8392 | } |
| 8393 | if (exp == 1) { /* exporting? */ |
| 8394 | /* export NAME (without =VALUE) */ |
| 8395 | if (var) { |
| 8396 | var->flg_export = 1; |
| 8397 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8398 | putenv(var->varstr); |
| 8399 | continue; |
| 8400 | } |
| 8401 | } |
| 8402 | /* Exporting non-existing variable. |
| 8403 | * bash does not put it in environment, |
| 8404 | * but remembers that it is exported, |
| 8405 | * and does put it in env when it is set later. |
| 8406 | * We just set it to "" and export. */ |
| 8407 | /* Or, it's "local NAME" (without =VALUE). |
| 8408 | * bash sets the value to "". */ |
| 8409 | name = xasprintf("%s=", name); |
| 8410 | } else { |
| 8411 | /* (Un)exporting/making local NAME=VALUE */ |
| 8412 | name = xstrdup(name); |
| 8413 | } |
| 8414 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8415 | } while (*++argv); |
| 8416 | } |
| 8417 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8418 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8419 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8420 | unsigned opt_unexport; |
| 8421 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8422 | #if ENABLE_HUSH_EXPORT_N |
| 8423 | /* "!": do not abort on errors */ |
| 8424 | opt_unexport = getopt32(argv, "!n"); |
| 8425 | if (opt_unexport == (uint32_t)-1) |
| 8426 | return EXIT_FAILURE; |
| 8427 | argv += optind; |
| 8428 | #else |
| 8429 | opt_unexport = 0; |
| 8430 | argv++; |
| 8431 | #endif |
| 8432 | |
| 8433 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8434 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8435 | if (e) { |
| 8436 | while (*e) { |
| 8437 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8438 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8439 | #else |
| 8440 | /* ash emits: export VAR='VAL' |
| 8441 | * bash: declare -x VAR="VAL" |
| 8442 | * we follow ash example */ |
| 8443 | const char *s = *e++; |
| 8444 | const char *p = strchr(s, '='); |
| 8445 | |
| 8446 | if (!p) /* wtf? take next variable */ |
| 8447 | continue; |
| 8448 | /* export var= */ |
| 8449 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8450 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8451 | putchar('\n'); |
| 8452 | #endif |
| 8453 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8454 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8455 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8456 | return EXIT_SUCCESS; |
| 8457 | } |
| 8458 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8459 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8460 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8461 | return EXIT_SUCCESS; |
| 8462 | } |
| 8463 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8464 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8465 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8466 | { |
| 8467 | if (G.func_nest_level == 0) { |
| 8468 | bb_error_msg("%s: not in a function", argv[0]); |
| 8469 | return EXIT_FAILURE; /* bash compat */ |
| 8470 | } |
| 8471 | helper_export_local(argv, 0, G.func_nest_level); |
| 8472 | return EXIT_SUCCESS; |
| 8473 | } |
| 8474 | #endif |
| 8475 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8476 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8477 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8478 | int sig; |
| 8479 | char *new_cmd; |
| 8480 | |
| 8481 | if (!G.traps) |
| 8482 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8483 | |
| 8484 | argv++; |
| 8485 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8486 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8487 | /* No args: print all trapped */ |
| 8488 | for (i = 0; i < NSIG; ++i) { |
| 8489 | if (G.traps[i]) { |
| 8490 | printf("trap -- "); |
| 8491 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 8492 | /* note: bash adds "SIG", but only if invoked |
| 8493 | * as "bash". If called as "sh", or if set -o posix, |
| 8494 | * then it prints short signal names. |
| 8495 | * We are printing short names: */ |
| 8496 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8497 | } |
| 8498 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8499 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8500 | return EXIT_SUCCESS; |
| 8501 | } |
| 8502 | |
| 8503 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8504 | /* If first arg is a number: reset all specified signals */ |
| 8505 | sig = bb_strtou(*argv, NULL, 10); |
| 8506 | if (errno == 0) { |
| 8507 | int ret; |
| 8508 | process_sig_list: |
| 8509 | ret = EXIT_SUCCESS; |
| 8510 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8511 | sighandler_t handler; |
| 8512 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8513 | sig = get_signum(*argv++); |
| 8514 | if (sig < 0 || sig >= NSIG) { |
| 8515 | ret = EXIT_FAILURE; |
| 8516 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8517 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8518 | continue; |
| 8519 | } |
| 8520 | |
| 8521 | free(G.traps[sig]); |
| 8522 | G.traps[sig] = xstrdup(new_cmd); |
| 8523 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8524 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8525 | get_signame(sig), sig, G.traps[sig]); |
| 8526 | |
| 8527 | /* There is no signal for 0 (EXIT) */ |
| 8528 | if (sig == 0) |
| 8529 | continue; |
| 8530 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8531 | if (new_cmd) |
| 8532 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 8533 | else |
| 8534 | /* We are removing trap handler */ |
| 8535 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8536 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8537 | } |
| 8538 | return ret; |
| 8539 | } |
| 8540 | |
| 8541 | if (!argv[1]) { /* no second arg */ |
| 8542 | bb_error_msg("trap: invalid arguments"); |
| 8543 | return EXIT_FAILURE; |
| 8544 | } |
| 8545 | |
| 8546 | /* First arg is "-": reset all specified to default */ |
| 8547 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 8548 | /* Everything else: set arg as signal handler |
| 8549 | * (includes "" case, which ignores signal) */ |
| 8550 | if (argv[0][0] == '-') { |
| 8551 | if (argv[0][1] == '\0') { /* "-" */ |
| 8552 | /* new_cmd remains NULL: "reset these sigs" */ |
| 8553 | goto reset_traps; |
| 8554 | } |
| 8555 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 8556 | argv++; |
| 8557 | } |
| 8558 | /* else: "-something", no special meaning */ |
| 8559 | } |
| 8560 | new_cmd = *argv; |
| 8561 | reset_traps: |
| 8562 | argv++; |
| 8563 | goto process_sig_list; |
| 8564 | } |
| 8565 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8566 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8567 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8568 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8569 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8570 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8571 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8572 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 8573 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8574 | |
| 8575 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8576 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8577 | type = "an alias";*/ |
| 8578 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8579 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8580 | type = "a function"; |
| 8581 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8582 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8583 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8584 | else if ((path = find_in_path(*argv)) != NULL) |
| 8585 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8586 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8587 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8588 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8589 | continue; |
| 8590 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8591 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8592 | printf("%s is %s\n", *argv, type); |
| 8593 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8594 | } |
| 8595 | |
| 8596 | return ret; |
| 8597 | } |
| 8598 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8599 | #if ENABLE_HUSH_JOB |
| 8600 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8601 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8602 | { |
| 8603 | int i, jobnum; |
| 8604 | struct pipe *pi; |
| 8605 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8606 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8607 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8608 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8609 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8610 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8611 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8612 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8613 | goto found; |
| 8614 | } |
| 8615 | } |
| 8616 | bb_error_msg("%s: no current job", argv[0]); |
| 8617 | return EXIT_FAILURE; |
| 8618 | } |
| 8619 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8620 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8621 | return EXIT_FAILURE; |
| 8622 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8623 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8624 | if (pi->jobid == jobnum) { |
| 8625 | goto found; |
| 8626 | } |
| 8627 | } |
| 8628 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8629 | return EXIT_FAILURE; |
| 8630 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8631 | /* TODO: bash prints a string representation |
| 8632 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8633 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8634 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8635 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
| 8638 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8639 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8640 | for (i = 0; i < pi->num_cmds; i++) { |
| 8641 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8642 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8643 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8644 | |
| 8645 | i = kill(- pi->pgrp, SIGCONT); |
| 8646 | if (i < 0) { |
| 8647 | if (errno == ESRCH) { |
| 8648 | delete_finished_bg_job(pi); |
| 8649 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8650 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8651 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8654 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8655 | remove_bg_job(pi); |
| 8656 | return checkjobs_and_fg_shell(pi); |
| 8657 | } |
| 8658 | return EXIT_SUCCESS; |
| 8659 | } |
| 8660 | #endif |
| 8661 | |
| 8662 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8663 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8664 | { |
| 8665 | const struct built_in_command *x; |
| 8666 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8667 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8668 | "Built-in commands:\n" |
| 8669 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8670 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8671 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8672 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8673 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8674 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8675 | return EXIT_SUCCESS; |
| 8676 | } |
| 8677 | #endif |
| 8678 | |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 8679 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 8680 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 8681 | { |
| 8682 | show_history(G.line_input_state); |
| 8683 | return EXIT_SUCCESS; |
| 8684 | } |
| 8685 | #endif |
| 8686 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8687 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8688 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8689 | { |
| 8690 | struct pipe *job; |
| 8691 | const char *status_string; |
| 8692 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8693 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8694 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8695 | status_string = "Stopped"; |
| 8696 | else |
| 8697 | status_string = "Running"; |
| 8698 | |
| 8699 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8700 | } |
| 8701 | return EXIT_SUCCESS; |
| 8702 | } |
| 8703 | #endif |
| 8704 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8705 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8706 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8707 | { |
| 8708 | void *p; |
| 8709 | unsigned long l; |
| 8710 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8711 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8712 | /* Optional. Reduces probability of false positives */ |
| 8713 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8714 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8715 | /* Crude attempt to find where "free memory" starts, |
| 8716 | * sans fragmentation. */ |
| 8717 | p = malloc(240); |
| 8718 | l = (unsigned long)p; |
| 8719 | free(p); |
| 8720 | p = malloc(3400); |
| 8721 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8722 | free(p); |
| 8723 | |
| 8724 | if (!G.memleak_value) |
| 8725 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8726 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8727 | l -= G.memleak_value; |
| 8728 | if ((long)l < 0) |
| 8729 | l = 0; |
| 8730 | l /= 1024; |
| 8731 | if (l > 127) |
| 8732 | l = 127; |
| 8733 | |
| 8734 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8735 | return l; |
| 8736 | } |
| 8737 | #endif |
| 8738 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8739 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8740 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8741 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8742 | return EXIT_SUCCESS; |
| 8743 | } |
| 8744 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8745 | /* Interruptibility of read builtin in bash |
| 8746 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 8747 | * |
| 8748 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 8749 | * |
| 8750 | * SIGINT: |
| 8751 | * - terminates non-interactive shell; |
| 8752 | * - interrupts read in interactive shell; |
| 8753 | * if it has non-empty trap: |
| 8754 | * - executes trap and returns to command prompt in interactive shell; |
| 8755 | * - executes trap and returns to read in non-interactive shell; |
| 8756 | * SIGTERM: |
| 8757 | * - is ignored (does not interrupt) read in interactive shell; |
| 8758 | * - terminates non-interactive shell; |
| 8759 | * if it has non-empty trap: |
| 8760 | * - executes trap and returns to read; |
| 8761 | * SIGHUP: |
| 8762 | * - terminates shell (regardless of interactivity); |
| 8763 | * if it has non-empty trap: |
| 8764 | * - executes trap and returns to read; |
| 8765 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8766 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8767 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8768 | const char *r; |
| 8769 | char *opt_n = NULL; |
| 8770 | char *opt_p = NULL; |
| 8771 | char *opt_t = NULL; |
| 8772 | char *opt_u = NULL; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8773 | const char *ifs; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8774 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8775 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8776 | /* "!": do not abort on errors. |
| 8777 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8778 | */ |
| 8779 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8780 | if (read_flags == (uint32_t)-1) |
| 8781 | return EXIT_FAILURE; |
| 8782 | argv += optind; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8783 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 8784 | |
| 8785 | again: |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8786 | r = shell_builtin_read(set_local_var_from_halves, |
| 8787 | argv, |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8788 | ifs, |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8789 | read_flags, |
| 8790 | opt_n, |
| 8791 | opt_p, |
| 8792 | opt_t, |
| 8793 | opt_u |
| 8794 | ); |
| 8795 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8796 | if ((uintptr_t)r == 1 && errno == EINTR) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8797 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 8798 | if (sig && sig != SIGINT) |
| 8799 | goto again; |
| 8800 | } |
| 8801 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8802 | if ((uintptr_t)r > 1) { |
| 8803 | bb_error_msg("%s", r); |
| 8804 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8805 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8806 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8807 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8808 | } |
| 8809 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8810 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8811 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8812 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8813 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8814 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8815 | * set -- [argument...] |
| 8816 | * set -o |
| 8817 | * set +o |
| 8818 | * Implementations shall support the options in both their hyphen and |
| 8819 | * plus-sign forms. These options can also be specified as options to sh. |
| 8820 | * Examples: |
| 8821 | * Write out all variables and their values: set |
| 8822 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8823 | * Turn on the -x and -v options: set -xv |
| 8824 | * Unset all positional parameters: set -- |
| 8825 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8826 | * Set the positional parameters to the expansion of x, even if x expands |
| 8827 | * with a leading '-' or '+': set -- $x |
| 8828 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8829 | * 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] | 8830 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8831 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8832 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8833 | int n; |
| 8834 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8835 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8836 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8837 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8838 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8839 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8840 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8841 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8842 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8843 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8844 | do { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8845 | if (strcmp(arg, "--") == 0) { |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8846 | ++argv; |
| 8847 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8848 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8849 | if (arg[0] != '+' && arg[0] != '-') |
| 8850 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8851 | for (n = 1; arg[n]; ++n) { |
| 8852 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8853 | goto error; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8854 | if (arg[n] == 'o' && argv[1]) |
| 8855 | argv++; |
| 8856 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8857 | } while ((arg = *++argv) != NULL); |
| 8858 | /* Now argv[0] is 1st argument */ |
| 8859 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8860 | if (arg == NULL) |
| 8861 | return EXIT_SUCCESS; |
| 8862 | set_argv: |
| 8863 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8864 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8865 | g_argv = G.global_argv; |
| 8866 | if (G.global_args_malloced) { |
| 8867 | pp = g_argv; |
| 8868 | while (*++pp) |
| 8869 | free(*pp); |
| 8870 | g_argv[1] = NULL; |
| 8871 | } else { |
| 8872 | G.global_args_malloced = 1; |
| 8873 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8874 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8875 | g_argv = pp; |
| 8876 | } |
| 8877 | /* This realloc's G.global_argv */ |
| 8878 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8879 | |
| 8880 | n = 1; |
| 8881 | while (*++pp) |
| 8882 | n++; |
| 8883 | G.global_argc = n; |
| 8884 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8885 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8886 | |
| 8887 | /* Nothing known, so abort */ |
| 8888 | error: |
| 8889 | bb_error_msg("set: %s: invalid option", arg); |
| 8890 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8891 | } |
| 8892 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8893 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8894 | { |
| 8895 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8896 | argv = skip_dash_dash(argv); |
| 8897 | if (argv[0]) { |
| 8898 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8899 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8900 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8901 | if (G.global_args_malloced) { |
| 8902 | int m = 1; |
| 8903 | while (m <= n) |
| 8904 | free(G.global_argv[m++]); |
| 8905 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8906 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8907 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8908 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8909 | return EXIT_SUCCESS; |
| 8910 | } |
| 8911 | return EXIT_FAILURE; |
| 8912 | } |
| 8913 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8914 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8915 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8916 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8917 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8918 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8919 | #if ENABLE_HUSH_FUNCTIONS |
| 8920 | smallint sv_flg; |
| 8921 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8922 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8923 | argv = skip_dash_dash(argv); |
| 8924 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8925 | if (!filename) { |
| 8926 | /* bash says: "bash: .: filename argument required" */ |
| 8927 | return 2; /* bash compat */ |
| 8928 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8929 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8930 | if (!strchr(filename, '/')) { |
| 8931 | arg_path = find_in_path(filename); |
| 8932 | if (arg_path) |
| 8933 | filename = arg_path; |
| 8934 | } |
| 8935 | input = fopen_or_warn(filename, "r"); |
| 8936 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8937 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8938 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8939 | /* POSIX: non-interactive shell should abort here, |
| 8940 | * not merely fail. So far no one complained :) |
| 8941 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8942 | return EXIT_FAILURE; |
| 8943 | } |
| 8944 | close_on_exec_on(fileno(input)); |
| 8945 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8946 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8947 | sv_flg = G.flag_return_in_progress; |
| 8948 | /* "we are inside sourced file, ok to use return" */ |
| 8949 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8950 | #endif |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8951 | if (argv[1]) |
| 8952 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8953 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8954 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8955 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8956 | |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 8957 | if (argv[1]) |
| 8958 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8959 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8960 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8961 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8962 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8963 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8964 | } |
| 8965 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8966 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8967 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8968 | int rc; |
| 8969 | mode_t mask; |
| 8970 | |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 8971 | rc = 1; |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8972 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8973 | argv = skip_dash_dash(argv); |
| 8974 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8975 | mode_t old_mask = mask; |
| 8976 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 8977 | /* numeric umasks are taken as-is */ |
| 8978 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 8979 | if (!isdigit(argv[0][0])) |
| 8980 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 8981 | mask = bb_parse_mode(argv[0], mask); |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 8982 | if (!isdigit(argv[0][0])) |
| 8983 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 8984 | if ((unsigned)mask > 0777) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8985 | mask = old_mask; |
| 8986 | /* bash messages: |
| 8987 | * bash: umask: 'q': invalid symbolic mode operator |
| 8988 | * bash: umask: 999: octal number out of range |
| 8989 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8990 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 8991 | rc = 0; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8992 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8993 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8994 | /* Mimic bash */ |
| 8995 | printf("%04o\n", (unsigned) mask); |
| 8996 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8997 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8998 | umask(mask); |
| 8999 | |
| 9000 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9001 | } |
| 9002 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9003 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9004 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9005 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9006 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 9007 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9008 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 9009 | /* "!": do not abort on errors */ |
| 9010 | /* "+": stop at 1st non-option */ |
| 9011 | opts = getopt32(argv, "!+vf"); |
| 9012 | if (opts == (unsigned)-1) |
| 9013 | return EXIT_FAILURE; |
| 9014 | if (opts == 3) { |
| 9015 | bb_error_msg("unset: -v and -f are exclusive"); |
| 9016 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9017 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 9018 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9019 | |
| 9020 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9021 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 9022 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9023 | if (unset_local_var(*argv)) { |
| 9024 | /* unset <nonexistent_var> doesn't fail. |
| 9025 | * Error is when one tries to unset RO var. |
| 9026 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9027 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9028 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9029 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9030 | #if ENABLE_HUSH_FUNCTIONS |
| 9031 | else { |
| 9032 | unset_func(*argv); |
| 9033 | } |
| 9034 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9035 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 9036 | } |
| 9037 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9038 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9039 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9040 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9041 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9042 | { |
| 9043 | int ret = EXIT_SUCCESS; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9044 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9045 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9046 | argv = skip_dash_dash(argv); |
| 9047 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9048 | /* Don't care about wait results */ |
| 9049 | /* Note 1: must wait until there are no more children */ |
| 9050 | /* Note 2: must be interruptible */ |
| 9051 | /* Examples: |
| 9052 | * $ sleep 3 & sleep 6 & wait |
| 9053 | * [1] 30934 sleep 3 |
| 9054 | * [2] 30935 sleep 6 |
| 9055 | * [1] Done sleep 3 |
| 9056 | * [2] Done sleep 6 |
| 9057 | * $ sleep 3 & sleep 6 & wait |
| 9058 | * [1] 30936 sleep 3 |
| 9059 | * [2] 30937 sleep 6 |
| 9060 | * [1] Done sleep 3 |
| 9061 | * ^C <-- after ~4 sec from keyboard |
| 9062 | * $ |
| 9063 | */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9064 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9065 | int sig; |
| 9066 | sigset_t oldset, allsigs; |
| 9067 | |
| 9068 | /* waitpid is not interruptible by SA_RESTARTed |
| 9069 | * signals which we use. Thus, this ugly dance: |
| 9070 | */ |
| 9071 | |
| 9072 | /* Make sure possible SIGCHLD is stored in kernel's |
| 9073 | * pending signal mask before we call waitpid. |
| 9074 | * Or else we may race with SIGCHLD, lose it, |
| 9075 | * and get stuck in sigwaitinfo... |
| 9076 | */ |
| 9077 | sigfillset(&allsigs); |
| 9078 | sigprocmask(SIG_SETMASK, &allsigs, &oldset); |
| 9079 | |
| 9080 | if (!sigisemptyset(&G.pending_set)) { |
| 9081 | /* Crap! we raced with some signal! */ |
| 9082 | // sig = 0; |
| 9083 | goto restore; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9084 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9085 | |
| 9086 | checkjobs(NULL); /* waitpid(WNOHANG) inside */ |
| 9087 | if (errno == ECHILD) { |
| 9088 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9089 | break; |
| 9090 | } |
| 9091 | |
| 9092 | /* Wait for SIGCHLD or any other signal */ |
| 9093 | //sig = sigwaitinfo(&allsigs, NULL); |
| 9094 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 9095 | /* Note: sigsuspend invokes signal handler */ |
| 9096 | sigsuspend(&oldset); |
| 9097 | restore: |
| 9098 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9099 | |
| 9100 | /* So, did we get a signal? */ |
| 9101 | //if (sig > 0) |
| 9102 | // raise(sig); /* run handler */ |
| 9103 | sig = check_and_run_traps(); |
| 9104 | if (sig /*&& sig != SIGCHLD - always true */) { |
| 9105 | /* see note 2 */ |
| 9106 | ret = 128 + sig; |
| 9107 | break; |
| 9108 | } |
| 9109 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9110 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9111 | return ret; |
| 9112 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9113 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9114 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9115 | while (*argv) { |
| 9116 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 9117 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9118 | /* mimic bash message */ |
| 9119 | 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] | 9120 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9121 | } |
| 9122 | if (waitpid(pid, &status, 0) == pid) { |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame^] | 9123 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9124 | if (WIFSIGNALED(status)) |
| 9125 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9126 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9127 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9128 | ret = 127; |
| 9129 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9130 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
| 9133 | return ret; |
| 9134 | } |
| 9135 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9136 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 9137 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 9138 | { |
| 9139 | if (argv[1]) { |
| 9140 | def = bb_strtou(argv[1], NULL, 10); |
| 9141 | if (errno || def < def_min || argv[2]) { |
| 9142 | bb_error_msg("%s: bad arguments", argv[0]); |
| 9143 | def = UINT_MAX; |
| 9144 | } |
| 9145 | } |
| 9146 | return def; |
| 9147 | } |
| 9148 | #endif |
| 9149 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9150 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9151 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9152 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9153 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9154 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9155 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 9156 | return EXIT_SUCCESS; /* bash compat */ |
| 9157 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9158 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9159 | |
| 9160 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 9161 | if (depth == UINT_MAX) |
| 9162 | G.flag_break_continue = BC_BREAK; |
| 9163 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9164 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9165 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9166 | return EXIT_SUCCESS; |
| 9167 | } |
| 9168 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9169 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9170 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9171 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 9172 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9173 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9174 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9175 | |
| 9176 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9177 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9178 | { |
| 9179 | int rc; |
| 9180 | |
| 9181 | if (G.flag_return_in_progress != -1) { |
| 9182 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 9183 | return EXIT_FAILURE; /* bash compat */ |
| 9184 | } |
| 9185 | |
| 9186 | G.flag_return_in_progress = 1; |
| 9187 | |
| 9188 | /* bash: |
| 9189 | * out of range: wraps around at 256, does not error out |
| 9190 | * non-numeric param: |
| 9191 | * f() { false; return qwe; }; f; echo $? |
| 9192 | * bash: return: qwe: numeric argument required <== we do this |
| 9193 | * 255 <== we also do this |
| 9194 | */ |
| 9195 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 9196 | return rc; |
| 9197 | } |
| 9198 | #endif |