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 | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 47 | * kill %jobspec |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 48 | * follow IFS rules more precisely, including update semantics |
| 49 | * builtins mandated by standards we don't support: |
| 50 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 51 | * make complex ${var%...} constructs support optional |
| 52 | * make here documents optional |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 53 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 54 | * Bash compat TODO: |
| 55 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 56 | * reserved words: function select |
| 57 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 58 | * process substitution: <(list) and >(list) |
| 59 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 60 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 61 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 62 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 63 | * 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] | 64 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 65 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 66 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 67 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 68 | * |
| 69 | * Won't do: |
| 70 | * In bash, export builtin is special, its arguments are assignments |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 71 | * and therefore expansion of them should be "one-word" expansion: |
| 72 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 73 | * compare with: |
| 74 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 75 | * ls: cannot access i=a: No such file or directory |
| 76 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 77 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 78 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 79 | * is not quoted: |
| 80 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 81 | * aaa bbb |
| 82 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 83 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 84 | */ |
Denys Vlasenko | 8da415e | 2010-12-05 01:30:14 +0100 | [diff] [blame] | 85 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 86 | || defined(__APPLE__) \ |
| 87 | ) |
| 88 | # include <malloc.h> /* for malloc_trim */ |
| 89 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 90 | #include <glob.h> |
| 91 | /* #include <dmalloc.h> */ |
| 92 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 93 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 94 | #endif |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 95 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 96 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 97 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 98 | #include "unicode.h" |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 99 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 100 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 101 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 102 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 103 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 104 | #else |
| 105 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 106 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 107 | #ifndef F_DUPFD_CLOEXEC |
| 108 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 109 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 110 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 111 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 112 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 113 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 114 | //config:config HUSH |
| 115 | //config: bool "hush" |
| 116 | //config: default y |
| 117 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 118 | //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] | 119 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 120 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 121 | //config: and functions are supported. |
| 122 | //config: |
| 123 | //config: It will compile and work on no-mmu systems. |
| 124 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 125 | //config: It does not handle select, aliases, tilde expansion, |
| 126 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 127 | //config: |
| 128 | //config:config HUSH_BASH_COMPAT |
| 129 | //config: bool "bash-compatible extensions" |
| 130 | //config: default y |
| 131 | //config: depends on HUSH |
| 132 | //config: help |
| 133 | //config: Enable bash-compatible extensions. |
| 134 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 135 | //config:config HUSH_BRACE_EXPANSION |
| 136 | //config: bool "Brace expansion" |
| 137 | //config: default y |
| 138 | //config: depends on HUSH_BASH_COMPAT |
| 139 | //config: help |
| 140 | //config: Enable {abc,def} extension. |
| 141 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config:config HUSH_HELP |
| 143 | //config: bool "help builtin" |
| 144 | //config: default y |
| 145 | //config: depends on HUSH |
| 146 | //config: help |
| 147 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 148 | //config: |
| 149 | //config:config HUSH_INTERACTIVE |
| 150 | //config: bool "Interactive mode" |
| 151 | //config: default y |
| 152 | //config: depends on HUSH |
| 153 | //config: help |
| 154 | //config: Enable interactive mode (prompt and command editing). |
| 155 | //config: Without this, hush simply reads and executes commands |
| 156 | //config: from stdin just like a shell script from a file. |
| 157 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 158 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 159 | //config:config HUSH_SAVEHISTORY |
| 160 | //config: bool "Save command history to .hush_history" |
| 161 | //config: default y |
| 162 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 163 | //config: help |
| 164 | //config: Enable history saving in hush. |
| 165 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 166 | //config:config HUSH_JOB |
| 167 | //config: bool "Job control" |
| 168 | //config: default y |
| 169 | //config: depends on HUSH_INTERACTIVE |
| 170 | //config: help |
| 171 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 172 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 173 | //config: "cmd &" still works by simply spawning a process and immediately |
| 174 | //config: prompting for next command (or executing next command in a script), |
| 175 | //config: but no separate process group is formed. |
| 176 | //config: |
| 177 | //config:config HUSH_TICK |
| 178 | //config: bool "Process substitution" |
| 179 | //config: default y |
| 180 | //config: depends on HUSH |
| 181 | //config: help |
| 182 | //config: Enable process substitution `command` and $(command) in hush. |
| 183 | //config: |
| 184 | //config:config HUSH_IF |
| 185 | //config: bool "Support if/then/elif/else/fi" |
| 186 | //config: default y |
| 187 | //config: depends on HUSH |
| 188 | //config: help |
| 189 | //config: Enable if/then/elif/else/fi in hush. |
| 190 | //config: |
| 191 | //config:config HUSH_LOOPS |
| 192 | //config: bool "Support for, while and until loops" |
| 193 | //config: default y |
| 194 | //config: depends on HUSH |
| 195 | //config: help |
| 196 | //config: Enable for, while and until loops in hush. |
| 197 | //config: |
| 198 | //config:config HUSH_CASE |
| 199 | //config: bool "Support case ... esac statement" |
| 200 | //config: default y |
| 201 | //config: depends on HUSH |
| 202 | //config: help |
| 203 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 204 | //config: |
| 205 | //config:config HUSH_FUNCTIONS |
| 206 | //config: bool "Support funcname() { commands; } syntax" |
| 207 | //config: default y |
| 208 | //config: depends on HUSH |
| 209 | //config: help |
| 210 | //config: Enable support for shell functions in hush. +800 bytes. |
| 211 | //config: |
| 212 | //config:config HUSH_LOCAL |
| 213 | //config: bool "Support local builtin" |
| 214 | //config: default y |
| 215 | //config: depends on HUSH_FUNCTIONS |
| 216 | //config: help |
| 217 | //config: Enable support for local variables in functions. |
| 218 | //config: |
| 219 | //config:config HUSH_RANDOM_SUPPORT |
| 220 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 221 | //config: default y |
| 222 | //config: depends on HUSH |
| 223 | //config: help |
| 224 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 225 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 226 | //config: |
| 227 | //config:config HUSH_EXPORT_N |
| 228 | //config: bool "Support 'export -n' option" |
| 229 | //config: default y |
| 230 | //config: depends on HUSH |
| 231 | //config: help |
| 232 | //config: export -n unexports variables. It is a bash extension. |
| 233 | //config: |
| 234 | //config:config HUSH_MODE_X |
| 235 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 236 | //config: default y |
| 237 | //config: depends on HUSH |
| 238 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 239 | //config: This instructs hush to print commands before execution. |
| 240 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 241 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 242 | //config:config MSH |
| 243 | //config: bool "msh (deprecated: aliased to hush)" |
| 244 | //config: default n |
| 245 | //config: select HUSH |
| 246 | //config: help |
| 247 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 248 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 249 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 250 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
| 251 | //applet:IF_MSH(APPLET(msh, BB_DIR_BIN, BB_SUID_DROP)) |
| 252 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 253 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 254 | |
| 255 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 256 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 257 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 258 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 259 | * but currently do nothing, therefore aren't shown in help. |
| 260 | * NOMMU-specific options are not meant to be used by users, |
| 261 | * therefore we don't show them either. |
| 262 | */ |
| 263 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 264 | //usage: "[-nxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 265 | //usage:#define hush_full_usage "\n\n" |
| 266 | //usage: "Unix shell interpreter" |
| 267 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 268 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 269 | //usage:#define msh_full_usage hush_full_usage |
| 270 | |
| 271 | //usage:#if ENABLE_FEATURE_SH_IS_HUSH |
| 272 | //usage:# define sh_trivial_usage hush_trivial_usage |
| 273 | //usage:# define sh_full_usage hush_full_usage |
| 274 | //usage:#endif |
| 275 | //usage:#if ENABLE_FEATURE_BASH_IS_HUSH |
| 276 | //usage:# define bash_trivial_usage hush_trivial_usage |
| 277 | //usage:# define bash_full_usage hush_full_usage |
| 278 | //usage:#endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 279 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 280 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 281 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 282 | #define LEAK_HUNTING 0 |
| 283 | #define BUILD_AS_NOMMU 0 |
| 284 | /* Enable/disable sanity checks. Ok to enable in production, |
| 285 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 286 | * Keeping 1 for now even in released versions. |
| 287 | */ |
| 288 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 289 | /* Slightly bigger (+200 bytes), but faster hush. |
| 290 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 291 | * which allows us to do fewer waitpid's. |
| 292 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 293 | * and therefore waitpid will return the same result as last time) |
| 294 | */ |
| 295 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 296 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 297 | * So far ${var%...} ops are always enabled: |
| 298 | */ |
| 299 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 300 | |
| 301 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 302 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 303 | # undef BB_MMU |
| 304 | # undef USE_FOR_NOMMU |
| 305 | # undef USE_FOR_MMU |
| 306 | # define BB_MMU 0 |
| 307 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 308 | # define USE_FOR_MMU(...) |
| 309 | #endif |
| 310 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 311 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 312 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 313 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 314 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 315 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 316 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 317 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 318 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 319 | # define IF_FEATURE_SH_STANDALONE(...) |
| 320 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 321 | #endif |
| 322 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 323 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 324 | # undef ENABLE_FEATURE_EDITING |
| 325 | # define ENABLE_FEATURE_EDITING 0 |
| 326 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 327 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 328 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 329 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 330 | #endif |
| 331 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 332 | /* Do we support ANY keywords? */ |
| 333 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 334 | # define HAS_KEYWORDS 1 |
| 335 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 336 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 337 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 338 | # define HAS_KEYWORDS 0 |
| 339 | # define IF_HAS_KEYWORDS(...) |
| 340 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 341 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 343 | /* If you comment out one of these below, it will be #defined later |
| 344 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 345 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 346 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 347 | #define debug_printf_parse(...) do {} while (0) |
| 348 | #define debug_print_tree(a, b) do {} while (0) |
| 349 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 350 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 351 | #define debug_printf_jobs(...) do {} while (0) |
| 352 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 353 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 354 | #define debug_printf_glob(...) do {} while (0) |
| 355 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 356 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 357 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 358 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 359 | #define ERR_PTR ((void*)(long)1) |
| 360 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 361 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 362 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 363 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 364 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 365 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 366 | #if ENABLE_HUSH_BASH_COMPAT |
| 367 | /* Support / and // replace ops */ |
| 368 | /* Note that // is stored as \ in "encoded" string representation */ |
| 369 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 370 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 371 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 372 | #else |
| 373 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 374 | # define VAR_SUBST_OPS "%#:-=+?" |
| 375 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 376 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 377 | |
| 378 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 379 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 380 | struct variable; |
| 381 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 382 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 383 | |
| 384 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 385 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 386 | */ |
| 387 | #if !BB_MMU |
| 388 | typedef struct nommu_save_t { |
| 389 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 390 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 391 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 392 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 393 | } nommu_save_t; |
| 394 | #endif |
| 395 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 396 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 397 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 398 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 399 | RES_IF , |
| 400 | RES_THEN , |
| 401 | RES_ELIF , |
| 402 | RES_ELSE , |
| 403 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 404 | #endif |
| 405 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 406 | RES_FOR , |
| 407 | RES_WHILE , |
| 408 | RES_UNTIL , |
| 409 | RES_DO , |
| 410 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 411 | #endif |
| 412 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 413 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 414 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 415 | #if ENABLE_HUSH_CASE |
| 416 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 417 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 418 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 419 | RES_MATCH , /* "word)" */ |
| 420 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 421 | RES_ESAC , |
| 422 | #endif |
| 423 | RES_XXXX , |
| 424 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 425 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 426 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 427 | typedef struct o_string { |
| 428 | char *data; |
| 429 | int length; /* position where data is appended */ |
| 430 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 431 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 432 | /* At least some part of the string was inside '' or "", |
| 433 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 434 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 435 | smallint has_empty_slot; |
| 436 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 437 | } o_string; |
| 438 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 439 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 440 | EXP_FLAG_GLOB = 0x2, |
| 441 | /* Protect newly added chars against globbing |
| 442 | * by prepending \ to *, ?, [, \ */ |
| 443 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 444 | }; |
| 445 | enum { |
| 446 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 447 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 448 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 449 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 450 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 451 | }; |
| 452 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 453 | #define NULL_O_STRING { NULL } |
| 454 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 455 | #ifndef debug_printf_parse |
| 456 | static const char *const assignment_flag[] = { |
| 457 | "MAYBE_ASSIGNMENT", |
| 458 | "DEFINITELY_ASSIGNMENT", |
| 459 | "NOT_ASSIGNMENT", |
| 460 | "WORD_IS_KEYWORD", |
| 461 | }; |
| 462 | #endif |
| 463 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 464 | typedef struct in_str { |
| 465 | const char *p; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 466 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 467 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 468 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 469 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 470 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 471 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 472 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 473 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 474 | /* The descrip member of this structure is only used to make |
| 475 | * debugging output pretty */ |
| 476 | static const struct { |
| 477 | int mode; |
| 478 | signed char default_fd; |
| 479 | char descrip[3]; |
| 480 | } redir_table[] = { |
| 481 | { O_RDONLY, 0, "<" }, |
| 482 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 483 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 484 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 485 | { O_RDONLY, 0, "<<" }, |
| 486 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 487 | /* { O_RDONLY, 77, "<<" }, */ |
| 488 | }; |
| 489 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 490 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 491 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 492 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 493 | int rd_fd; /* fd to redirect */ |
| 494 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 495 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 496 | smallint rd_type; /* (enum redir_type) */ |
| 497 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 498 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 499 | * bit 0: do we need to trim leading tabs? |
| 500 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 501 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 502 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 503 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 504 | REDIRECT_INPUT = 0, |
| 505 | REDIRECT_OVERWRITE = 1, |
| 506 | REDIRECT_APPEND = 2, |
| 507 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 508 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 509 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 510 | |
| 511 | REDIRFD_CLOSE = -3, |
| 512 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 513 | REDIRFD_TO_FILE = -1, |
| 514 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 515 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 516 | HEREDOC_SKIPTABS = 1, |
| 517 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 518 | } redir_type; |
| 519 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 520 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 521 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 522 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 523 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 524 | smallint cmd_type; /* CMD_xxx */ |
| 525 | #define CMD_NORMAL 0 |
| 526 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 527 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 528 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 529 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 530 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 531 | #if ENABLE_HUSH_FUNCTIONS |
| 532 | # define CMD_FUNCDEF 3 |
| 533 | #endif |
| 534 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 535 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 536 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 537 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 538 | #if !BB_MMU |
| 539 | char *group_as_string; |
| 540 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 541 | #if ENABLE_HUSH_FUNCTIONS |
| 542 | struct function *child_func; |
| 543 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 544 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 545 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 546 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 547 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 548 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 549 | * we put those fields back into cmd->xxx |
| 550 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 551 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 552 | * Without this trick, loop would execute a;b;b;b;... |
| 553 | * instead of correct sequence a;b;a;b;... |
| 554 | * When command is freed, it severs the link |
| 555 | * (sets ->child_func->parent_cmd to NULL). |
| 556 | */ |
| 557 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 558 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 559 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 560 | * and on execution these are substituted with their values. |
| 561 | * Substitution can make _several_ words out of one argv[n]! |
| 562 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 563 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 564 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 565 | struct redir_struct *redirects; /* I/O redirections */ |
| 566 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 567 | /* Is there anything in this command at all? */ |
| 568 | #define IS_NULL_CMD(cmd) \ |
| 569 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 570 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 571 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 572 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 573 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 574 | int alive_cmds; /* number of commands running (not exited) */ |
| 575 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 576 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 577 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 578 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 579 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 580 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 581 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 582 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 583 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 584 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 585 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 586 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 587 | PIPE_SEQ = 0, |
| 588 | PIPE_AND = 1, |
| 589 | PIPE_OR = 2, |
| 590 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 591 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 592 | /* Is there anything in this pipe at all? */ |
| 593 | #define IS_NULL_PIPE(pi) \ |
| 594 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 595 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 596 | /* This holds pointers to the various results of parsing */ |
| 597 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 598 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 599 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 600 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 601 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 602 | /* last command in pipe (being constructed right now) */ |
| 603 | struct command *command; |
| 604 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 605 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 606 | #if !BB_MMU |
| 607 | o_string as_string; |
| 608 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 609 | #if HAS_KEYWORDS |
| 610 | smallint ctx_res_w; |
| 611 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 612 | #if ENABLE_HUSH_CASE |
| 613 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 614 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 615 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 616 | int old_flag; |
| 617 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 618 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 619 | * when we see "if" or "then", we malloc and copy current context, |
| 620 | * and make ->stack point to it. then we parse pipeN. |
| 621 | * when closing "then" / fi" / whatever is found, |
| 622 | * we move list_head into ->stack->command->group, |
| 623 | * copy ->stack into current context, and delete ->stack. |
| 624 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 625 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 626 | struct parse_context *stack; |
| 627 | #endif |
| 628 | }; |
| 629 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 630 | /* On program start, environ points to initial environment. |
| 631 | * putenv adds new pointers into it, unsetenv removes them. |
| 632 | * Neither of these (de)allocates the strings. |
| 633 | * setenv allocates new strings in malloc space and does putenv, |
| 634 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 635 | #define setenv(...) setenv_is_leaky_dont_use() |
| 636 | struct variable { |
| 637 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 638 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 639 | #if ENABLE_HUSH_LOCAL |
| 640 | unsigned func_nest_level; |
| 641 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 642 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 643 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 644 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 645 | }; |
| 646 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 647 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 648 | BC_BREAK = 1, |
| 649 | BC_CONTINUE = 2, |
| 650 | }; |
| 651 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 652 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 653 | struct function { |
| 654 | struct function *next; |
| 655 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 656 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 657 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 658 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 659 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 660 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 661 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 662 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 663 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 664 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 665 | /* set -/+o OPT support. (TODO: make it optional) |
| 666 | * bash supports the following opts: |
| 667 | * allexport off |
| 668 | * braceexpand on |
| 669 | * emacs on |
| 670 | * errexit off |
| 671 | * errtrace off |
| 672 | * functrace off |
| 673 | * hashall on |
| 674 | * histexpand off |
| 675 | * history on |
| 676 | * ignoreeof off |
| 677 | * interactive-comments on |
| 678 | * keyword off |
| 679 | * monitor on |
| 680 | * noclobber off |
| 681 | * noexec off |
| 682 | * noglob off |
| 683 | * nolog off |
| 684 | * notify off |
| 685 | * nounset off |
| 686 | * onecmd off |
| 687 | * physical off |
| 688 | * pipefail off |
| 689 | * posix off |
| 690 | * privileged off |
| 691 | * verbose off |
| 692 | * vi off |
| 693 | * xtrace off |
| 694 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 695 | static const char o_opt_strings[] ALIGN1 = |
| 696 | "pipefail\0" |
| 697 | "noexec\0" |
| 698 | #if ENABLE_HUSH_MODE_X |
| 699 | "xtrace\0" |
| 700 | #endif |
| 701 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 702 | enum { |
| 703 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 704 | OPT_O_NOEXEC, |
| 705 | #if ENABLE_HUSH_MODE_X |
| 706 | OPT_O_XTRACE, |
| 707 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 708 | NUM_OPT_O |
| 709 | }; |
| 710 | |
| 711 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 712 | struct FILE_list { |
| 713 | struct FILE_list *next; |
| 714 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 715 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 716 | }; |
| 717 | |
| 718 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 719 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 720 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 721 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 722 | /* interactive_fd != 0 means we are an interactive shell. |
| 723 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 724 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 725 | * job control still works, but terminal signals |
| 726 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 727 | * process groups can only be created with "cmd &". |
| 728 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 729 | * to give tty to the foreground process group, |
| 730 | * and will take it back when the group is stopped (^Z) |
| 731 | * or killed (^C). |
| 732 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 733 | #if ENABLE_HUSH_INTERACTIVE |
| 734 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 735 | * _AND_ if we decided to act interactively */ |
| 736 | int interactive_fd; |
| 737 | const char *PS1; |
| 738 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 739 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 740 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 741 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 742 | #endif |
| 743 | #if ENABLE_FEATURE_EDITING |
| 744 | line_input_t *line_input_state; |
| 745 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 746 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 747 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 748 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 749 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 750 | random_t random_gen; |
| 751 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 752 | #if ENABLE_HUSH_JOB |
| 753 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 754 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 755 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 756 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 757 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 758 | #else |
| 759 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 760 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 761 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 762 | #if ENABLE_HUSH_MODE_X |
| 763 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 764 | #else |
| 765 | # define G_x_mode 0 |
| 766 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 767 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 768 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 769 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 770 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 771 | #if ENABLE_HUSH_FUNCTIONS |
| 772 | /* 0: outside of a function (or sourced file) |
| 773 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 774 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 775 | */ |
| 776 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 777 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 778 | #else |
| 779 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 780 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 781 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 782 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 783 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 784 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 785 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 786 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 787 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 788 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 789 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 790 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 791 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 792 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 793 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 794 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 795 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 796 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 797 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 798 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 799 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 800 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 801 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 802 | # if ENABLE_HUSH_LOCAL |
| 803 | struct variable **shadowed_vars_pp; |
| 804 | unsigned func_nest_level; |
| 805 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 806 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 807 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 808 | #if ENABLE_HUSH_FAST |
| 809 | unsigned count_SIGCHLD; |
| 810 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 811 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 812 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 813 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 814 | /* Which signals have non-DFL handler (even with no traps set)? |
| 815 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 816 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 817 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 818 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 819 | * Other than these two times, never modified. |
| 820 | */ |
| 821 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 822 | #if ENABLE_HUSH_JOB |
| 823 | unsigned fatal_sig_mask; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 824 | # define G_fatal_sig_mask G.fatal_sig_mask |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 825 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 826 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 827 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 828 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 829 | sigset_t pending_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 830 | #if HUSH_DEBUG |
| 831 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 832 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 833 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 834 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 835 | #if ENABLE_FEATURE_EDITING |
| 836 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 837 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 838 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 839 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 840 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 841 | * (too many defines). Also, I actually prefer to see when a variable |
| 842 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 843 | #define INIT_G() do { \ |
| 844 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 845 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 846 | sigfillset(&G.sa.sa_mask); \ |
| 847 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 848 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 849 | |
| 850 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 851 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 852 | static int builtin_cd(char **argv) FAST_FUNC; |
| 853 | static int builtin_echo(char **argv) FAST_FUNC; |
| 854 | static int builtin_eval(char **argv) FAST_FUNC; |
| 855 | static int builtin_exec(char **argv) FAST_FUNC; |
| 856 | static int builtin_exit(char **argv) FAST_FUNC; |
| 857 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 858 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 859 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 860 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 861 | #endif |
| 862 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 863 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 864 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 865 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 866 | static int builtin_history(char **argv) FAST_FUNC; |
| 867 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 868 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 869 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 870 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 871 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 872 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 873 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 874 | #if ENABLE_PRINTF |
| 875 | static int builtin_printf(char **argv) FAST_FUNC; |
| 876 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 877 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 878 | static int builtin_read(char **argv) FAST_FUNC; |
| 879 | static int builtin_set(char **argv) FAST_FUNC; |
| 880 | static int builtin_shift(char **argv) FAST_FUNC; |
| 881 | static int builtin_source(char **argv) FAST_FUNC; |
| 882 | static int builtin_test(char **argv) FAST_FUNC; |
| 883 | static int builtin_trap(char **argv) FAST_FUNC; |
| 884 | static int builtin_type(char **argv) FAST_FUNC; |
| 885 | static int builtin_true(char **argv) FAST_FUNC; |
| 886 | static int builtin_umask(char **argv) FAST_FUNC; |
| 887 | static int builtin_unset(char **argv) FAST_FUNC; |
| 888 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 889 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 890 | static int builtin_break(char **argv) FAST_FUNC; |
| 891 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 892 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 893 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 894 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 895 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 896 | |
| 897 | /* Table of built-in functions. They can be forked or not, depending on |
| 898 | * context: within pipes, they fork. As simple commands, they do not. |
| 899 | * When used in non-forking context, they can change global variables |
| 900 | * in the parent shell process. If forked, of course they cannot. |
| 901 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 902 | * still be set at the end. */ |
| 903 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 904 | const char *b_cmd; |
| 905 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 906 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 907 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 908 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 909 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 910 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 911 | #endif |
| 912 | }; |
| 913 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 914 | static const struct built_in_command bltins1[] = { |
| 915 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 916 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 917 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 918 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 919 | #endif |
| 920 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 921 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 922 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 923 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 924 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 925 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 926 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 927 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 928 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 929 | BLTIN("exit" , builtin_exit , "Exit"), |
| 930 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 931 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 932 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 933 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 934 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 935 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 936 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 937 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 938 | BLTIN("history" , builtin_history , "Show command history"), |
| 939 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 940 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 941 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 942 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 943 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 944 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 945 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 946 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 947 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 948 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 949 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 950 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 951 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 952 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 953 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 954 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 955 | #if ENABLE_HUSH_BASH_COMPAT |
| 956 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 957 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 958 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 959 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 960 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 961 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 962 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 963 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 964 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 965 | }; |
| 966 | /* For now, echo and test are unconditionally enabled. |
| 967 | * Maybe make it configurable? */ |
| 968 | static const struct built_in_command bltins2[] = { |
| 969 | BLTIN("[" , builtin_test , NULL), |
| 970 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 971 | #if ENABLE_PRINTF |
| 972 | BLTIN("printf" , builtin_printf , NULL), |
| 973 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 974 | BLTIN("pwd" , builtin_pwd , NULL), |
| 975 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 976 | }; |
| 977 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 978 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 979 | /* Debug printouts. |
| 980 | */ |
| 981 | #if HUSH_DEBUG |
| 982 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 983 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 984 | # define debug_enter() (G.debug_indent++) |
| 985 | # define debug_leave() (G.debug_indent--) |
| 986 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 987 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 988 | # define debug_enter() ((void)0) |
| 989 | # define debug_leave() ((void)0) |
| 990 | #endif |
| 991 | |
| 992 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 993 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 994 | #endif |
| 995 | |
| 996 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 997 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 998 | #endif |
| 999 | |
| 1000 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1001 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1002 | #endif |
| 1003 | |
| 1004 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1005 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1006 | #endif |
| 1007 | |
| 1008 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1009 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1010 | # define DEBUG_JOBS 1 |
| 1011 | #else |
| 1012 | # define DEBUG_JOBS 0 |
| 1013 | #endif |
| 1014 | |
| 1015 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1016 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1017 | # define DEBUG_EXPAND 1 |
| 1018 | #else |
| 1019 | # define DEBUG_EXPAND 0 |
| 1020 | #endif |
| 1021 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1022 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1023 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1024 | #endif |
| 1025 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1026 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1027 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1028 | # define DEBUG_GLOB 1 |
| 1029 | #else |
| 1030 | # define DEBUG_GLOB 0 |
| 1031 | #endif |
| 1032 | |
| 1033 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1034 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1035 | #endif |
| 1036 | |
| 1037 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1038 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1039 | #endif |
| 1040 | |
| 1041 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1042 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1043 | # define DEBUG_CLEAN 1 |
| 1044 | #else |
| 1045 | # define DEBUG_CLEAN 0 |
| 1046 | #endif |
| 1047 | |
| 1048 | #if DEBUG_EXPAND |
| 1049 | static void debug_print_strings(const char *prefix, char **vv) |
| 1050 | { |
| 1051 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1052 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1053 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1054 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1055 | } |
| 1056 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1057 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1058 | #endif |
| 1059 | |
| 1060 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1061 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1062 | */ |
| 1063 | #if LEAK_HUNTING |
| 1064 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1065 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1066 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1067 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1068 | return ptr; |
| 1069 | } |
| 1070 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1071 | { |
| 1072 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1073 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1074 | return ptr; |
| 1075 | } |
| 1076 | static char *xxstrdup(int lineno, const char *str) |
| 1077 | { |
| 1078 | char *ptr = xstrdup(str); |
| 1079 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1080 | return ptr; |
| 1081 | } |
| 1082 | static void xxfree(void *ptr) |
| 1083 | { |
| 1084 | fdprintf(2, "free %p\n", ptr); |
| 1085 | free(ptr); |
| 1086 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1087 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1088 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1089 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1090 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1091 | #endif |
| 1092 | |
| 1093 | |
| 1094 | /* Syntax and runtime errors. They always abort scripts. |
| 1095 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1096 | * and return to the prompt. |
| 1097 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1098 | */ |
| 1099 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1100 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1101 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1102 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1103 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1104 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1105 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1106 | #endif |
| 1107 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1108 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1109 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1110 | va_list p; |
| 1111 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1112 | #if HUSH_DEBUG >= 2 |
| 1113 | bb_error_msg("hush.c:%u", lineno); |
| 1114 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1115 | va_start(p, fmt); |
| 1116 | bb_verror_msg(fmt, p, NULL); |
| 1117 | va_end(p); |
| 1118 | if (!G_interactive_fd) |
| 1119 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1120 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1121 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1122 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1123 | { |
| 1124 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1125 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1126 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1127 | bb_error_msg("syntax error"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1130 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1131 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1132 | bb_error_msg("syntax error at '%s'", 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_unterm_str(unsigned lineno UNUSED_PARAM, const char *s) |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1136 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1137 | bb_error_msg("syntax error: unterminated %s", s); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1138 | } |
| 1139 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1140 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1141 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1142 | char msg[2] = { ch, '\0' }; |
| 1143 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1146 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1147 | { |
| 1148 | char msg[2]; |
| 1149 | msg[0] = ch; |
| 1150 | msg[1] = '\0'; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1151 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1154 | #if HUSH_DEBUG < 2 |
| 1155 | # undef die_if_script |
| 1156 | # undef syntax_error |
| 1157 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1158 | # undef syntax_error_unterm_ch |
| 1159 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1160 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1161 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1162 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1163 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1164 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1165 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1166 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1167 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1168 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1169 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1170 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1171 | #if ENABLE_HUSH_INTERACTIVE |
| 1172 | static void cmdedit_update_prompt(void); |
| 1173 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1174 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1175 | #endif |
| 1176 | |
| 1177 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1178 | /* Utility functions |
| 1179 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1180 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1181 | static char *unbackslash(char *src) |
| 1182 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1183 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1184 | while (1) { |
| 1185 | if (*src == '\\') |
| 1186 | src++; |
| 1187 | if ((*dst++ = *src++) == '\0') |
| 1188 | break; |
| 1189 | } |
| 1190 | return dst; |
| 1191 | } |
| 1192 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1193 | 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] | 1194 | { |
| 1195 | int i; |
| 1196 | unsigned count1; |
| 1197 | unsigned count2; |
| 1198 | char **v; |
| 1199 | |
| 1200 | v = strings; |
| 1201 | count1 = 0; |
| 1202 | if (v) { |
| 1203 | while (*v) { |
| 1204 | count1++; |
| 1205 | v++; |
| 1206 | } |
| 1207 | } |
| 1208 | count2 = 0; |
| 1209 | v = add; |
| 1210 | while (*v) { |
| 1211 | count2++; |
| 1212 | v++; |
| 1213 | } |
| 1214 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1215 | v[count1 + count2] = NULL; |
| 1216 | i = count2; |
| 1217 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1218 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1219 | return v; |
| 1220 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1221 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1222 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1223 | { |
| 1224 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1225 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1226 | return ptr; |
| 1227 | } |
| 1228 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1229 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1230 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1231 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1232 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1233 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1234 | { |
| 1235 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1236 | v[0] = add; |
| 1237 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1238 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1239 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1240 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1241 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1242 | { |
| 1243 | char **ptr = add_string_to_strings(strings, add); |
| 1244 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1245 | return ptr; |
| 1246 | } |
| 1247 | #define add_string_to_strings(strings, add) \ |
| 1248 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1249 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1250 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1251 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1252 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1253 | char **v; |
| 1254 | |
| 1255 | if (!strings) |
| 1256 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1257 | v = strings; |
| 1258 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1259 | free(*v); |
| 1260 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1261 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1262 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1265 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1266 | static int xdup_and_close(int fd, int F_DUPFD_maybe_CLOEXEC) |
| 1267 | { |
| 1268 | /* We avoid taking stdio fds. Mimicking ash: use fds above 9 */ |
| 1269 | int newfd = fcntl(fd, F_DUPFD_maybe_CLOEXEC, 10); |
| 1270 | if (newfd < 0) { |
| 1271 | /* fd was not open? */ |
| 1272 | if (errno == EBADF) |
| 1273 | return fd; |
| 1274 | xfunc_die(); |
| 1275 | } |
| 1276 | close(fd); |
| 1277 | return newfd; |
| 1278 | } |
| 1279 | |
| 1280 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1281 | /* Manipulating the list of open FILEs */ |
| 1282 | static FILE *remember_FILE(FILE *fp) |
| 1283 | { |
| 1284 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1285 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1286 | n->next = G.FILE_list; |
| 1287 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1288 | n->fp = fp; |
| 1289 | n->fd = fileno(fp); |
| 1290 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1291 | } |
| 1292 | return fp; |
| 1293 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1294 | static void fclose_and_forget(FILE *fp) |
| 1295 | { |
| 1296 | struct FILE_list **pp = &G.FILE_list; |
| 1297 | while (*pp) { |
| 1298 | struct FILE_list *cur = *pp; |
| 1299 | if (cur->fp == fp) { |
| 1300 | *pp = cur->next; |
| 1301 | free(cur); |
| 1302 | break; |
| 1303 | } |
| 1304 | pp = &cur->next; |
| 1305 | } |
| 1306 | fclose(fp); |
| 1307 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1308 | static int save_FILEs_on_redirect(int fd) |
| 1309 | { |
| 1310 | struct FILE_list *fl = G.FILE_list; |
| 1311 | while (fl) { |
| 1312 | if (fd == fl->fd) { |
| 1313 | /* We use it only on script files, they are all CLOEXEC */ |
| 1314 | fl->fd = xdup_and_close(fd, F_DUPFD_CLOEXEC); |
| 1315 | return 1; |
| 1316 | } |
| 1317 | fl = fl->next; |
| 1318 | } |
| 1319 | return 0; |
| 1320 | } |
| 1321 | static void restore_redirected_FILEs(void) |
| 1322 | { |
| 1323 | struct FILE_list *fl = G.FILE_list; |
| 1324 | while (fl) { |
| 1325 | int should_be = fileno(fl->fp); |
| 1326 | if (fl->fd != should_be) { |
| 1327 | xmove_fd(fl->fd, should_be); |
| 1328 | fl->fd = should_be; |
| 1329 | } |
| 1330 | fl = fl->next; |
| 1331 | } |
| 1332 | } |
| 1333 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1334 | static void close_all_FILE_list(void) |
| 1335 | { |
| 1336 | struct FILE_list *fl = G.FILE_list; |
| 1337 | while (fl) { |
| 1338 | /* fclose would also free FILE object. |
| 1339 | * It is disastrous if we share memory with a vforked parent. |
| 1340 | * I'm not sure we never come here after vfork. |
| 1341 | * Therefore just close fd, nothing more. |
| 1342 | */ |
| 1343 | /*fclose(fl->fp); - unsafe */ |
| 1344 | close(fl->fd); |
| 1345 | fl = fl->next; |
| 1346 | } |
| 1347 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1348 | #endif |
| 1349 | |
| 1350 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1351 | /* Helpers for setting new $n and restoring them back |
| 1352 | */ |
| 1353 | typedef struct save_arg_t { |
| 1354 | char *sv_argv0; |
| 1355 | char **sv_g_argv; |
| 1356 | int sv_g_argc; |
| 1357 | smallint sv_g_malloced; |
| 1358 | } save_arg_t; |
| 1359 | |
| 1360 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1361 | { |
| 1362 | int n; |
| 1363 | |
| 1364 | sv->sv_argv0 = argv[0]; |
| 1365 | sv->sv_g_argv = G.global_argv; |
| 1366 | sv->sv_g_argc = G.global_argc; |
| 1367 | sv->sv_g_malloced = G.global_args_malloced; |
| 1368 | |
| 1369 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1370 | G.global_argv = argv; |
| 1371 | G.global_args_malloced = 0; |
| 1372 | |
| 1373 | n = 1; |
| 1374 | while (*++argv) |
| 1375 | n++; |
| 1376 | G.global_argc = n; |
| 1377 | } |
| 1378 | |
| 1379 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1380 | { |
| 1381 | char **pp; |
| 1382 | |
| 1383 | if (G.global_args_malloced) { |
| 1384 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1385 | pp = G.global_argv; |
| 1386 | while (*++pp) /* note: does not free $0 */ |
| 1387 | free(*pp); |
| 1388 | free(G.global_argv); |
| 1389 | } |
| 1390 | argv[0] = sv->sv_argv0; |
| 1391 | G.global_argv = sv->sv_g_argv; |
| 1392 | G.global_argc = sv->sv_g_argc; |
| 1393 | G.global_args_malloced = sv->sv_g_malloced; |
| 1394 | } |
| 1395 | |
| 1396 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1397 | /* Basic theory of signal handling in shell |
| 1398 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1399 | * This does not describe what hush does, rather, it is current understanding |
| 1400 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1401 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1402 | * |
| 1403 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1404 | * is finished or backgrounded. It is the same in interactive and |
| 1405 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1406 | * 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] | 1407 | * ^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] | 1408 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1409 | * pipe. |
| 1410 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1411 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1412 | * or by SIGINT in interactive shell. |
| 1413 | * |
| 1414 | * Trap handlers will execute even within trap handlers. (right?) |
| 1415 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1416 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1417 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1418 | * |
| 1419 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1420 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1421 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1422 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1423 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1424 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1425 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1426 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1427 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1428 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1429 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1430 | * |
| 1431 | * SIGQUIT: ignore |
| 1432 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1433 | * SIGHUP (interactive): |
| 1434 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1435 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1436 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1437 | * that all pipe members are stopped. Try this in bash: |
| 1438 | * while :; do :; done - ^Z does not background it |
| 1439 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1440 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1441 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1442 | * to interactive shell while shell is waiting for a pipe, |
| 1443 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1444 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1445 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1446 | * Example 2: this does not wait and does not execute ls: |
| 1447 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1448 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1449 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1450 | * Example 4: this does not wait and does not execute ls: |
| 1451 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1452 | * |
| 1453 | * (What happens to signals which are IGN on shell start?) |
| 1454 | * (What happens with signal mask on shell start?) |
| 1455 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1456 | * Old implementation |
| 1457 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1458 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1459 | * We block all signals which we don't want to take action immediately, |
| 1460 | * i.e. we block all signals which need to have special handling as described |
| 1461 | * above, and all signals which have traps set. |
| 1462 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1463 | * and act on them. |
| 1464 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1465 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1466 | * sigset_t blocked_set: current blocked signal set |
| 1467 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1468 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1469 | * 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] | 1470 | * "trap 'cmd' SIGxxx": |
| 1471 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1472 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1473 | * unblock signals with special interactive handling |
| 1474 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1475 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1476 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1477 | * 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] | 1478 | * 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] | 1479 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1480 | * 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] | 1481 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1482 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1483 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1484 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1485 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1486 | * 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] | 1487 | * 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] | 1488 | * |
| 1489 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1490 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1491 | * masked signals are not visible! |
| 1492 | * |
| 1493 | * New implementation |
| 1494 | * ================== |
| 1495 | * We record each signal we are interested in by installing signal handler |
| 1496 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1497 | * We are interested in: signals which need to have special handling |
| 1498 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1499 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1500 | * After each pipe execution, we extract any pending signals |
| 1501 | * and act on them. |
| 1502 | * |
| 1503 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1504 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1505 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1506 | * sigset_t pending_set: set of sigs we received. |
| 1507 | * |
| 1508 | * "trap - SIGxxx": |
| 1509 | * if sig is in special_sig_mask, set handler back to: |
| 1510 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1511 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1512 | * else: set handler back to SIG_DFL |
| 1513 | * "trap 'cmd' SIGxxx": |
| 1514 | * set handler to record_pending_signo. |
| 1515 | * "trap '' SIGxxx": |
| 1516 | * set handler to SIG_IGN. |
| 1517 | * after [v]fork, if we plan to be a shell: |
| 1518 | * set signals with special interactive handling to SIG_DFL |
| 1519 | * (because child shell is not interactive), |
| 1520 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1521 | * after [v]fork, if we plan to exec: |
| 1522 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1523 | * |
| 1524 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1525 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1526 | * |
| 1527 | * Note (compat): |
| 1528 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1529 | * are set to the default actions". bash interprets it so that traps which |
| 1530 | * 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] | 1531 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1532 | enum { |
| 1533 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1534 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1535 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1536 | | (1 << SIGHUP) |
| 1537 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1538 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1539 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1540 | | (1 << SIGTTIN) |
| 1541 | | (1 << SIGTTOU) |
| 1542 | | (1 << SIGTSTP) |
| 1543 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1544 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1545 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1546 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1547 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1548 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1549 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1550 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1551 | if (sig == SIGCHLD) { |
| 1552 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1553 | //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] | 1554 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1555 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1556 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1557 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1558 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1559 | { |
| 1560 | struct sigaction old_sa; |
| 1561 | |
| 1562 | /* We could use signal() to install handlers... almost: |
| 1563 | * except that we need to mask ALL signals while handlers run. |
| 1564 | * I saw signal nesting in strace, race window isn't small. |
| 1565 | * SA_RESTART is also needed, but in Linux, signal() |
| 1566 | * sets SA_RESTART too. |
| 1567 | */ |
| 1568 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1569 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1570 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1571 | G.sa.sa_handler = handler; |
| 1572 | sigaction(sig, &G.sa, &old_sa); |
| 1573 | return old_sa.sa_handler; |
| 1574 | } |
| 1575 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1576 | static void hush_exit(int exitcode) NORETURN; |
| 1577 | static void fflush_and__exit(void) NORETURN; |
| 1578 | static void restore_ttypgrp_and__exit(void) NORETURN; |
| 1579 | |
| 1580 | static void restore_ttypgrp_and__exit(void) |
| 1581 | { |
| 1582 | /* xfunc has failed! die die die */ |
| 1583 | /* no EXIT traps, this is an escape hatch! */ |
| 1584 | G.exiting = 1; |
| 1585 | hush_exit(xfunc_error_retval); |
| 1586 | } |
| 1587 | |
| 1588 | /* Needed only on some libc: |
| 1589 | * It was observed that on exit(), fgetc'ed buffered data |
| 1590 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1591 | * With the net effect that even after fork(), not vfork(), |
| 1592 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1593 | * noexec_applet_here |
| 1594 | * echo END_OF_SCRIPT |
| 1595 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1596 | * This makes "echo END_OF_SCRIPT" executed twice. |
| 1597 | * Similar problems can be seen with die_if_script() -> xfunc_die() |
| 1598 | * and in `cmd` handling. |
| 1599 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1600 | */ |
| 1601 | static void fflush_and__exit(void) |
| 1602 | { |
| 1603 | fflush_all(); |
| 1604 | _exit(xfunc_error_retval); |
| 1605 | } |
| 1606 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1607 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1608 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1609 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1610 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1611 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1612 | # define enable_restore_tty_pgrp_on_exit() (die_func = restore_ttypgrp_and__exit) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1613 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1614 | /* Restores tty foreground process group, and exits. |
| 1615 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1616 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1617 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1618 | * We also call it if xfunc is exiting. |
| 1619 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1620 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1621 | static void sigexit(int sig) |
| 1622 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1623 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1624 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1625 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1626 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1627 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1628 | */ |
| 1629 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1630 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1631 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1632 | |
| 1633 | /* Not a signal, just exit */ |
| 1634 | if (sig <= 0) |
| 1635 | _exit(- sig); |
| 1636 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1637 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1638 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1639 | #else |
| 1640 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1641 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1642 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1643 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1644 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1645 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1646 | static sighandler_t pick_sighandler(unsigned sig) |
| 1647 | { |
| 1648 | sighandler_t handler = SIG_DFL; |
| 1649 | if (sig < sizeof(unsigned)*8) { |
| 1650 | unsigned sigmask = (1 << sig); |
| 1651 | |
| 1652 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1653 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1654 | if (G_fatal_sig_mask & sigmask) |
| 1655 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1656 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1657 | #endif |
| 1658 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1659 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1660 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1661 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1662 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1663 | * in an endless loop when we try to do some |
| 1664 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1665 | */ |
| 1666 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1667 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1668 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1669 | } |
| 1670 | return handler; |
| 1671 | } |
| 1672 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1673 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1674 | static void hush_exit(int exitcode) |
| 1675 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1676 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1677 | save_history(G.line_input_state); |
| 1678 | #endif |
| 1679 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1680 | fflush_all(); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1681 | 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] | 1682 | char *argv[3]; |
| 1683 | /* argv[0] is unused */ |
| 1684 | argv[1] = G.traps[0]; |
| 1685 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1686 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1687 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1688 | * "trap" will still show it, if executed |
| 1689 | * in the handler */ |
| 1690 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1691 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1692 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1693 | #if ENABLE_FEATURE_CLEAN_UP |
| 1694 | { |
| 1695 | struct variable *cur_var; |
| 1696 | if (G.cwd != bb_msg_unknown) |
| 1697 | free((char*)G.cwd); |
| 1698 | cur_var = G.top_var; |
| 1699 | while (cur_var) { |
| 1700 | struct variable *tmp = cur_var; |
| 1701 | if (!cur_var->max_len) |
| 1702 | free(cur_var->varstr); |
| 1703 | cur_var = cur_var->next; |
| 1704 | free(tmp); |
| 1705 | } |
| 1706 | } |
| 1707 | #endif |
| 1708 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1709 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1710 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1711 | sigexit(- (exitcode & 0xff)); |
| 1712 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1713 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1714 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1717 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1718 | //TODO: return a mask of ALL handled sigs? |
| 1719 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1720 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1721 | int last_sig = 0; |
| 1722 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1723 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1724 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1725 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1726 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1727 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1728 | sig = 0; |
| 1729 | do { |
| 1730 | sig++; |
| 1731 | if (sigismember(&G.pending_set, sig)) { |
| 1732 | sigdelset(&G.pending_set, sig); |
| 1733 | goto got_sig; |
| 1734 | } |
| 1735 | } while (sig < NSIG); |
| 1736 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1737 | got_sig: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1738 | if (G.traps && G.traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1739 | debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1740 | if (G.traps[sig][0]) { |
| 1741 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1742 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1743 | char *argv[3]; |
| 1744 | /* argv[0] is unused */ |
| 1745 | argv[1] = G.traps[sig]; |
| 1746 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1747 | save_rcode = G.last_exitcode; |
| 1748 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1749 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1750 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1751 | } /* else: "" trap, ignoring signal */ |
| 1752 | continue; |
| 1753 | } |
| 1754 | /* not a trap: special action */ |
| 1755 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1756 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1757 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1758 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1759 | bb_putchar('\n'); |
| 1760 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1761 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1762 | break; |
| 1763 | #if ENABLE_HUSH_JOB |
| 1764 | case SIGHUP: { |
| 1765 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1766 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1767 | /* bash is observed to signal whole process groups, |
| 1768 | * not individual processes */ |
| 1769 | for (job = G.job_list; job; job = job->next) { |
| 1770 | if (job->pgrp <= 0) |
| 1771 | continue; |
| 1772 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1773 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1774 | kill(- job->pgrp, SIGCONT); |
| 1775 | } |
| 1776 | sigexit(SIGHUP); |
| 1777 | } |
| 1778 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1779 | #if ENABLE_HUSH_FAST |
| 1780 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1781 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1782 | G.count_SIGCHLD++; |
| 1783 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1784 | /* Note: |
| 1785 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1786 | * This simplifies wait builtin a bit. |
| 1787 | */ |
| 1788 | break; |
| 1789 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1790 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1791 | debug_printf_exec("%s: sig:%d default handling is to ignore\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1792 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1793 | /* Note: |
| 1794 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1795 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1796 | * in interactive shell, because TERM is ignored. |
| 1797 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1798 | break; |
| 1799 | } |
| 1800 | } |
| 1801 | return last_sig; |
| 1802 | } |
| 1803 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1804 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1805 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1806 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1807 | if (force || G.cwd == NULL) { |
| 1808 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1809 | * we must not try to free(bb_msg_unknown) */ |
| 1810 | if (G.cwd == bb_msg_unknown) |
| 1811 | G.cwd = NULL; |
| 1812 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1813 | if (!G.cwd) |
| 1814 | G.cwd = bb_msg_unknown; |
| 1815 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1816 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1819 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1820 | /* |
| 1821 | * Shell and environment variable support |
| 1822 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1823 | 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] | 1824 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1825 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1826 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1827 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1828 | pp = &G.top_var; |
| 1829 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1830 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1831 | return pp; |
| 1832 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1833 | } |
| 1834 | return NULL; |
| 1835 | } |
| 1836 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1837 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1838 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1839 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1840 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1841 | |
| 1842 | if (G.expanded_assignments) { |
| 1843 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1844 | while (*cpp) { |
| 1845 | char *cp = *cpp; |
| 1846 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1847 | return cp + len + 1; |
| 1848 | cpp++; |
| 1849 | } |
| 1850 | } |
| 1851 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1852 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1853 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1854 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1855 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1856 | if (strcmp(name, "PPID") == 0) |
| 1857 | return utoa(G.root_ppid); |
| 1858 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1859 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1860 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1861 | return utoa(next_random(&G.random_gen)); |
| 1862 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1867 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1868 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1869 | * 0: do not change export flag |
| 1870 | * (if creating new variable, flag will be 0) |
| 1871 | * 1: set export flag and putenv the variable |
| 1872 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1873 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1874 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1875 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1876 | /* all params are used */ |
| 1877 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1878 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1879 | set_local_var(str, flg_export, local_lvl) |
| 1880 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1881 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1882 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1883 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1884 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1885 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1886 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1887 | 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] | 1888 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1889 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1890 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1891 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1892 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1893 | int name_len; |
| 1894 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1895 | eq_sign = strchr(str, '='); |
| 1896 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1897 | free(str); |
| 1898 | return -1; |
| 1899 | } |
| 1900 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1901 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1902 | var_pp = &G.top_var; |
| 1903 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1904 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1905 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1906 | continue; |
| 1907 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1908 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1909 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1910 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1911 | #if !BB_MMU |
| 1912 | if (!flg_read_only) |
| 1913 | #endif |
| 1914 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1915 | free(str); |
| 1916 | return -1; |
| 1917 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1918 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1919 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1920 | *eq_sign = '\0'; |
| 1921 | unsetenv(str); |
| 1922 | *eq_sign = '='; |
| 1923 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1924 | #if ENABLE_HUSH_LOCAL |
| 1925 | if (cur->func_nest_level < local_lvl) { |
| 1926 | /* New variable is declared as local, |
| 1927 | * and existing one is global, or local |
| 1928 | * from enclosing function. |
| 1929 | * Remove and save old one: */ |
| 1930 | *var_pp = cur->next; |
| 1931 | cur->next = *G.shadowed_vars_pp; |
| 1932 | *G.shadowed_vars_pp = cur; |
| 1933 | /* bash 3.2.33(1) and exported vars: |
| 1934 | * # export z=z |
| 1935 | * # f() { local z=a; env | grep ^z; } |
| 1936 | * # f |
| 1937 | * z=a |
| 1938 | * # env | grep ^z |
| 1939 | * z=z |
| 1940 | */ |
| 1941 | if (cur->flg_export) |
| 1942 | flg_export = 1; |
| 1943 | break; |
| 1944 | } |
| 1945 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1946 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1947 | free_and_exp: |
| 1948 | free(str); |
| 1949 | goto exp; |
| 1950 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1951 | if (cur->max_len != 0) { |
| 1952 | if (cur->max_len >= strlen(str)) { |
| 1953 | /* This one is from startup env, reuse space */ |
| 1954 | strcpy(cur->varstr, str); |
| 1955 | goto free_and_exp; |
| 1956 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1957 | /* Can't reuse */ |
| 1958 | cur->max_len = 0; |
| 1959 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1960 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1961 | /* max_len == 0 signifies "malloced" var, which we can |
| 1962 | * (and have to) free. But we can't free(cur->varstr) here: |
| 1963 | * if cur->flg_export is 1, it is in the environment. |
| 1964 | * We should either unsetenv+free, or wait until putenv, |
| 1965 | * then putenv(new)+free(old). |
| 1966 | */ |
| 1967 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1968 | goto set_str_and_exp; |
| 1969 | } |
| 1970 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1971 | /* Not found - create new variable struct */ |
| 1972 | cur = xzalloc(sizeof(*cur)); |
| 1973 | #if ENABLE_HUSH_LOCAL |
| 1974 | cur->func_nest_level = local_lvl; |
| 1975 | #endif |
| 1976 | cur->next = *var_pp; |
| 1977 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1978 | |
| 1979 | set_str_and_exp: |
| 1980 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1981 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1982 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1983 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1984 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1985 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1986 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1987 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1988 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1989 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1990 | if (flg_export == -1) { |
| 1991 | cur->flg_export = 0; |
| 1992 | /* unsetenv was already done */ |
| 1993 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1994 | int i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1995 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1996 | i = putenv(cur->varstr); |
| 1997 | /* only now we can free old exported malloced string */ |
| 1998 | free(free_me); |
| 1999 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2000 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2001 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2002 | free(free_me); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2003 | return 0; |
| 2004 | } |
| 2005 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2006 | /* Used at startup and after each cd */ |
| 2007 | static void set_pwd_var(int exp) |
| 2008 | { |
| 2009 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 2010 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 2011 | } |
| 2012 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2013 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2014 | { |
| 2015 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2016 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2019 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2020 | var_pp = &G.top_var; |
| 2021 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2022 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 2023 | if (cur->flg_read_only) { |
| 2024 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2025 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2026 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2027 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2028 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2029 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2030 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2031 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2032 | if (!cur->max_len) |
| 2033 | free(cur->varstr); |
| 2034 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2035 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2036 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2037 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2038 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2039 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2042 | static int unset_local_var(const char *name) |
| 2043 | { |
| 2044 | return unset_local_var_len(name, strlen(name)); |
| 2045 | } |
| 2046 | |
| 2047 | static void unset_vars(char **strings) |
| 2048 | { |
| 2049 | char **v; |
| 2050 | |
| 2051 | if (!strings) |
| 2052 | return; |
| 2053 | v = strings; |
| 2054 | while (*v) { |
| 2055 | const char *eq = strchrnul(*v, '='); |
| 2056 | unset_local_var_len(*v, (int)(eq - *v)); |
| 2057 | v++; |
| 2058 | } |
| 2059 | free(strings); |
| 2060 | } |
| 2061 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2062 | 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] | 2063 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2064 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2065 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2066 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2067 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2068 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2069 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2070 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2071 | */ |
| 2072 | static void add_vars(struct variable *var) |
| 2073 | { |
| 2074 | struct variable *next; |
| 2075 | |
| 2076 | while (var) { |
| 2077 | next = var->next; |
| 2078 | var->next = G.top_var; |
| 2079 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2080 | if (var->flg_export) { |
| 2081 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2082 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2083 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2084 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2085 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2086 | var = next; |
| 2087 | } |
| 2088 | } |
| 2089 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2090 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2091 | { |
| 2092 | char **s; |
| 2093 | struct variable *old = NULL; |
| 2094 | |
| 2095 | if (!strings) |
| 2096 | return old; |
| 2097 | s = strings; |
| 2098 | while (*s) { |
| 2099 | struct variable *var_p; |
| 2100 | struct variable **var_pp; |
| 2101 | char *eq; |
| 2102 | |
| 2103 | eq = strchr(*s, '='); |
| 2104 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2105 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2106 | if (var_pp) { |
| 2107 | /* Remove variable from global linked list */ |
| 2108 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2109 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2110 | *var_pp = var_p->next; |
| 2111 | /* Add it to returned list */ |
| 2112 | var_p->next = old; |
| 2113 | old = var_p; |
| 2114 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2115 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2116 | } |
| 2117 | s++; |
| 2118 | } |
| 2119 | return old; |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2124 | * Unicode helper |
| 2125 | */ |
| 2126 | static void reinit_unicode_for_hush(void) |
| 2127 | { |
| 2128 | /* Unicode support should be activated even if LANG is set |
| 2129 | * _during_ shell execution, not only if it was set when |
| 2130 | * shell was started. Therefore, re-check LANG every time: |
| 2131 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2132 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2133 | || ENABLE_UNICODE_USING_LOCALE |
| 2134 | ) { |
| 2135 | const char *s = get_local_var_value("LC_ALL"); |
| 2136 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2137 | if (!s) s = get_local_var_value("LANG"); |
| 2138 | reinit_unicode(s); |
| 2139 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2142 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2143 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2144 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2145 | |
| 2146 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2147 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2148 | * echo $P\ |
| 2149 | * \ |
| 2150 | * AT\ |
| 2151 | * H\ |
| 2152 | * \ |
| 2153 | * It excercises a lot of corner cases. |
| 2154 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2155 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2156 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2157 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2158 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2159 | if (G.PS1 == NULL) |
| 2160 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2161 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2162 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2163 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2164 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2165 | if (G.PS2 == NULL) |
| 2166 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2167 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2168 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2169 | { |
| 2170 | const char *prompt_str; |
| 2171 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2172 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2173 | /* Set up the prompt */ |
| 2174 | if (promptmode == 0) { /* PS1 */ |
| 2175 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2176 | /* bash uses $PWD value, even if it is set by user. |
| 2177 | * It uses current dir only if PWD is unset. |
| 2178 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2179 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2180 | prompt_str = G.PS1; |
| 2181 | } else |
| 2182 | prompt_str = G.PS2; |
| 2183 | } else |
| 2184 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2185 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2186 | return prompt_str; |
| 2187 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2188 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2189 | { |
| 2190 | int r; |
| 2191 | const char *prompt_str; |
| 2192 | |
| 2193 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2194 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2195 | do { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2196 | reinit_unicode_for_hush(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2197 | G.flag_SIGINT = 0; |
| 2198 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 2199 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2200 | r = read_line_input(G.line_input_state, prompt_str, |
| 2201 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, |
| 2202 | /*timeout*/ -1 |
| 2203 | ); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2204 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2205 | check_and_run_traps(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2206 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2207 | if (r < 0) { |
| 2208 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2209 | i->p = NULL; |
| 2210 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2211 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2212 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2213 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2214 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2215 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2216 | do { |
| 2217 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2218 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2219 | /* Why check_and_run_traps here? Try this interactively: |
| 2220 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2221 | * $ <[enter], repeatedly...> |
| 2222 | * Without check_and_run_traps, handler never runs. |
| 2223 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2224 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2225 | fputs(prompt_str, stdout); |
| 2226 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2227 | fflush_all(); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2228 | r = fgetc(i->file); |
| 2229 | } while (G.flag_SIGINT || r == '\0'); |
| 2230 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2231 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2232 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2233 | /* This is the magic location that prints prompts |
| 2234 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2235 | static int fgetc_interactive(struct in_str *i) |
| 2236 | { |
| 2237 | int ch; |
| 2238 | /* If it's interactive stdin, get new line. */ |
| 2239 | if (G_interactive_fd && i->file == stdin) { |
| 2240 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2241 | ch = get_user_input(i); |
| 2242 | i->promptmode = 1; /* PS2 */ |
| 2243 | } else { |
| 2244 | /* Not stdin: script file, sourced file, etc */ |
| 2245 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2246 | } |
| 2247 | return ch; |
| 2248 | } |
| 2249 | #else |
| 2250 | static inline int fgetc_interactive(struct in_str *i) |
| 2251 | { |
| 2252 | int ch; |
| 2253 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2254 | return ch; |
| 2255 | } |
| 2256 | #endif /* INTERACTIVE */ |
| 2257 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame^] | 2258 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2259 | { |
| 2260 | int ch; |
| 2261 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame^] | 2262 | if (!i->file) { |
| 2263 | /* string-based in_str */ |
| 2264 | ch = (unsigned char)*i->p; |
| 2265 | if (ch != '\0') { |
| 2266 | i->p++; |
| 2267 | i->last_char = ch; |
| 2268 | return ch; |
| 2269 | } |
| 2270 | return EOF; |
| 2271 | } |
| 2272 | |
| 2273 | /* FILE-based in_str */ |
| 2274 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2275 | #if ENABLE_FEATURE_EDITING |
| 2276 | /* This can be stdin, check line editing char[] buffer */ |
| 2277 | if (i->p && *i->p != '\0') { |
| 2278 | ch = (unsigned char)*i->p++; |
| 2279 | goto out; |
| 2280 | } |
| 2281 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2282 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2283 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2284 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2285 | int ch2 = i->peek_buf[1]; |
| 2286 | i->peek_buf[0] = ch2; |
| 2287 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2288 | goto out; |
| 2289 | i->peek_buf[1] = 0; |
| 2290 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2291 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2292 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2293 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2294 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2295 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2296 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2297 | return ch; |
| 2298 | } |
| 2299 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame^] | 2300 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2301 | { |
| 2302 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2303 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame^] | 2304 | if (!i->file) { |
| 2305 | /* string-based in_str */ |
| 2306 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2307 | return (unsigned char)*i->p; |
| 2308 | } |
| 2309 | |
| 2310 | /* FILE-based in_str */ |
| 2311 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2312 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2313 | /* This can be stdin, check line editing char[] buffer */ |
| 2314 | if (i->p && *i->p != '\0') |
| 2315 | return (unsigned char)*i->p; |
| 2316 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2317 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2318 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2319 | if (ch != 0) |
| 2320 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2321 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2322 | /* Need to get a new char */ |
| 2323 | ch = fgetc_interactive(i); |
| 2324 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2325 | |
| 2326 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2327 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2328 | if (i->p) { |
| 2329 | i->p -= 1; |
| 2330 | return ch; |
| 2331 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2332 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2333 | i->peek_buf[0] = ch; |
| 2334 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2335 | return ch; |
| 2336 | } |
| 2337 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2338 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2339 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2340 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2341 | */ |
| 2342 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2343 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2344 | int ch; |
| 2345 | |
| 2346 | /* There are two cases when i->p[] buffer exists. |
| 2347 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2348 | * (2) It's a file, and we have a saved line editing buffer. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2349 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2350 | * the peek2 result is in i->p[1]. |
| 2351 | */ |
| 2352 | if (i->p) |
| 2353 | return (unsigned char)i->p[1]; |
| 2354 | |
| 2355 | /* Now we know it is a file-based in_str. */ |
| 2356 | |
| 2357 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2358 | /* Is there 2nd char? */ |
| 2359 | ch = i->peek_buf[1]; |
| 2360 | if (ch == 0) { |
| 2361 | /* We did not read it yet, get it now */ |
| 2362 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2363 | i->peek_buf[1] = ch; |
| 2364 | } |
| 2365 | |
| 2366 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2367 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2370 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2371 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2372 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2373 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2374 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2375 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2379 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2380 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2381 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame^] | 2382 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2383 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2387 | /* |
| 2388 | * o_string support |
| 2389 | */ |
| 2390 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2391 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2392 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2393 | { |
| 2394 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2395 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2396 | if (o->data) |
| 2397 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2400 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2401 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2402 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2403 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2406 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2407 | { |
| 2408 | free(o->data); |
| 2409 | } |
| 2410 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2411 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2412 | { |
| 2413 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2414 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2415 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2416 | } |
| 2417 | } |
| 2418 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2419 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2420 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2421 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2422 | if (o->length < o->maxlen) { |
| 2423 | /* likely. avoid o_grow_by() call */ |
| 2424 | add: |
| 2425 | o->data[o->length] = ch; |
| 2426 | o->length++; |
| 2427 | o->data[o->length] = '\0'; |
| 2428 | return; |
| 2429 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2430 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2431 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2434 | #if 0 |
| 2435 | /* Valid only if we know o_string is not empty */ |
| 2436 | static void o_delchr(o_string *o) |
| 2437 | { |
| 2438 | o->length--; |
| 2439 | o->data[o->length] = '\0'; |
| 2440 | } |
| 2441 | #endif |
| 2442 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2443 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2444 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2445 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2446 | memcpy(&o->data[o->length], str, len); |
| 2447 | o->length += len; |
| 2448 | o->data[o->length] = '\0'; |
| 2449 | } |
| 2450 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2451 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2452 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2453 | o_addblock(o, str, strlen(str)); |
| 2454 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2455 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2456 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2457 | static void nommu_addchr(o_string *o, int ch) |
| 2458 | { |
| 2459 | if (o) |
| 2460 | o_addchr(o, ch); |
| 2461 | } |
| 2462 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2463 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2464 | #endif |
| 2465 | |
| 2466 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2467 | { |
| 2468 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2471 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2472 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2473 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2474 | * Apparently, on unquoted $v bash still does globbing |
| 2475 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2476 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2477 | * quoting mechanisms on $v expansion side: one protects |
| 2478 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2479 | * We have only second one. |
| 2480 | */ |
| 2481 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2482 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2483 | # define MAYBE_BRACES "{}" |
| 2484 | #else |
| 2485 | # define MAYBE_BRACES "" |
| 2486 | #endif |
| 2487 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2488 | /* My analysis of quoting semantics tells me that state information |
| 2489 | * is associated with a destination, not a source. |
| 2490 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2491 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2492 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2493 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2494 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2495 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2496 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2497 | o_grow_by(o, sz); |
| 2498 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2499 | o->data[o->length] = '\\'; |
| 2500 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2501 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2502 | o->data[o->length] = ch; |
| 2503 | o->length++; |
| 2504 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2507 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2508 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2509 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2510 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2511 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2512 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2513 | sz++; |
| 2514 | o->data[o->length] = '\\'; |
| 2515 | o->length++; |
| 2516 | } |
| 2517 | o_grow_by(o, sz); |
| 2518 | o->data[o->length] = ch; |
| 2519 | o->length++; |
| 2520 | o->data[o->length] = '\0'; |
| 2521 | } |
| 2522 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2523 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2524 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2525 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2526 | char ch; |
| 2527 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2528 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2529 | if (ordinary_cnt > len) /* paranoia */ |
| 2530 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2531 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2532 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2533 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2534 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2535 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2536 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2537 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2538 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2539 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2540 | sz++; |
| 2541 | o->data[o->length] = '\\'; |
| 2542 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2543 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2544 | o_grow_by(o, sz); |
| 2545 | o->data[o->length] = ch; |
| 2546 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2547 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2548 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2551 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2552 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2553 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2554 | o_addblock(o, str, len); |
| 2555 | return; |
| 2556 | } |
| 2557 | o_addqblock(o, str, len); |
| 2558 | } |
| 2559 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2560 | static void o_addQstr(o_string *o, const char *str) |
| 2561 | { |
| 2562 | o_addQblock(o, str, strlen(str)); |
| 2563 | } |
| 2564 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2565 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2566 | * 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] | 2567 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2568 | * list[i] contains an INDEX (int!) into this string data. |
| 2569 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2570 | * but list[i]'s need not be modified. |
| 2571 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2572 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2573 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2574 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2575 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2576 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2577 | { |
| 2578 | char **list = (char**)o->data; |
| 2579 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2580 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2581 | |
| 2582 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2583 | 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] | 2584 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2585 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2586 | o->has_quoted_part, |
| 2587 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2588 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2589 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2590 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2591 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2592 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2593 | i++; |
| 2594 | } |
| 2595 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2596 | 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] | 2597 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2598 | 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] | 2599 | } |
| 2600 | } |
| 2601 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2602 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2603 | #endif |
| 2604 | |
| 2605 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2606 | * in list[n] so that it points past last stored byte so far. |
| 2607 | * It returns n+1. */ |
| 2608 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2609 | { |
| 2610 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2611 | int string_start; |
| 2612 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2613 | |
| 2614 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2615 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2616 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2617 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2618 | 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] | 2619 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2620 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2621 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2622 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2623 | memmove(list + n + 0x10, list + n, string_len); |
| 2624 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2625 | } else { |
| 2626 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2627 | n, string_len, string_start); |
| 2628 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2629 | } else { |
| 2630 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2631 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2632 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2633 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2634 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2635 | o->has_empty_slot = 0; |
| 2636 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2637 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2638 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2639 | return n + 1; |
| 2640 | } |
| 2641 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2642 | /* "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] | 2643 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2644 | { |
| 2645 | char **list = (char**)o->data; |
| 2646 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2647 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2648 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2651 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2652 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2653 | * first, it processes even {a} (no commas), second, |
| 2654 | * 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] | 2655 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2656 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2657 | |
| 2658 | /* Helper */ |
| 2659 | static int glob_needed(const char *s) |
| 2660 | { |
| 2661 | while (*s) { |
| 2662 | if (*s == '\\') { |
| 2663 | if (!s[1]) |
| 2664 | return 0; |
| 2665 | s += 2; |
| 2666 | continue; |
| 2667 | } |
| 2668 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2669 | return 1; |
| 2670 | s++; |
| 2671 | } |
| 2672 | return 0; |
| 2673 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2674 | /* Return pointer to next closing brace or to comma */ |
| 2675 | static const char *next_brace_sub(const char *cp) |
| 2676 | { |
| 2677 | unsigned depth = 0; |
| 2678 | cp++; |
| 2679 | while (*cp != '\0') { |
| 2680 | if (*cp == '\\') { |
| 2681 | if (*++cp == '\0') |
| 2682 | break; |
| 2683 | cp++; |
| 2684 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2685 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2686 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2687 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2688 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2689 | depth++; |
| 2690 | } |
| 2691 | |
| 2692 | return *cp != '\0' ? cp : NULL; |
| 2693 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2694 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2695 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2696 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2697 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2698 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2699 | const char *next; |
| 2700 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2701 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2702 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2703 | |
| 2704 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2705 | |
| 2706 | begin = pattern; |
| 2707 | while (1) { |
| 2708 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2709 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2710 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2711 | /* Find the first sub-pattern and at the same time |
| 2712 | * find the rest after the closing brace */ |
| 2713 | next = next_brace_sub(begin); |
| 2714 | if (next == NULL) { |
| 2715 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2716 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2717 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2718 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2719 | /* "{abc}" with no commas - illegal |
| 2720 | * brace expr, disregard and skip it */ |
| 2721 | begin = next + 1; |
| 2722 | continue; |
| 2723 | } |
| 2724 | break; |
| 2725 | } |
| 2726 | if (*begin == '\\' && begin[1] != '\0') |
| 2727 | begin++; |
| 2728 | begin++; |
| 2729 | } |
| 2730 | debug_printf_glob("begin:%s\n", begin); |
| 2731 | debug_printf_glob("next:%s\n", next); |
| 2732 | |
| 2733 | /* Now find the end of the whole brace expression */ |
| 2734 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2735 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2736 | rest = next_brace_sub(rest); |
| 2737 | if (rest == NULL) { |
| 2738 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2739 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2740 | } |
| 2741 | debug_printf_glob("rest:%s\n", rest); |
| 2742 | } |
| 2743 | rest_len = strlen(++rest) + 1; |
| 2744 | |
| 2745 | /* We are sure the brace expression is well-formed */ |
| 2746 | |
| 2747 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2748 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* We have a brace expression. BEGIN points to the opening {, |
| 2751 | * NEXT points past the terminator of the first element, and REST |
| 2752 | * points past the final }. We will accumulate result names from |
| 2753 | * recursive runs for each brace alternative in the buffer using |
| 2754 | * GLOB_APPEND. */ |
| 2755 | |
| 2756 | p = begin + 1; |
| 2757 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2758 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2759 | memcpy( |
| 2760 | mempcpy( |
| 2761 | mempcpy(new_pattern_buf, |
| 2762 | /* We know the prefix for all sub-patterns */ |
| 2763 | pattern, begin - pattern), |
| 2764 | p, next - p), |
| 2765 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2766 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2767 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2768 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2769 | */ |
| 2770 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2771 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2772 | /* We saw the last entry */ |
| 2773 | break; |
| 2774 | } |
| 2775 | p = next + 1; |
| 2776 | next = next_brace_sub(next); |
| 2777 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2778 | free(new_pattern_buf); |
| 2779 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2780 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2781 | simple_glob: |
| 2782 | { |
| 2783 | int gr; |
| 2784 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2785 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2786 | memset(&globdata, 0, sizeof(globdata)); |
| 2787 | gr = glob(pattern, 0, NULL, &globdata); |
| 2788 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2789 | if (gr != 0) { |
| 2790 | if (gr == GLOB_NOMATCH) { |
| 2791 | globfree(&globdata); |
| 2792 | /* NB: garbles parameter */ |
| 2793 | unbackslash(pattern); |
| 2794 | o_addstr_with_NUL(o, pattern); |
| 2795 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2796 | return o_save_ptr_helper(o, n); |
| 2797 | } |
| 2798 | if (gr == GLOB_NOSPACE) |
| 2799 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2800 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2801 | * but we didn't specify it. Paranoia again. */ |
| 2802 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2803 | } |
| 2804 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2805 | char **argv = globdata.gl_pathv; |
| 2806 | while (1) { |
| 2807 | o_addstr_with_NUL(o, *argv); |
| 2808 | n = o_save_ptr_helper(o, n); |
| 2809 | argv++; |
| 2810 | if (!*argv) |
| 2811 | break; |
| 2812 | } |
| 2813 | } |
| 2814 | globfree(&globdata); |
| 2815 | } |
| 2816 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2817 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2818 | /* Performs globbing on last list[], |
| 2819 | * saving each result as a new list[]. |
| 2820 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2821 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2822 | { |
| 2823 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2824 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2825 | 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] | 2826 | if (!o->data) |
| 2827 | return o_save_ptr_helper(o, n); |
| 2828 | pattern = o->data + o_get_last_ptr(o, n); |
| 2829 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2830 | if (!glob_needed(pattern)) { |
| 2831 | /* unbackslash last string in o in place, fix length */ |
| 2832 | o->length = unbackslash(pattern) - o->data; |
| 2833 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2834 | return o_save_ptr_helper(o, n); |
| 2835 | } |
| 2836 | |
| 2837 | copy = xstrdup(pattern); |
| 2838 | /* "forget" pattern in o */ |
| 2839 | o->length = pattern - o->data; |
| 2840 | n = glob_brace(copy, o, n); |
| 2841 | free(copy); |
| 2842 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2843 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2844 | return n; |
| 2845 | } |
| 2846 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2847 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2848 | |
| 2849 | /* Helper */ |
| 2850 | static int glob_needed(const char *s) |
| 2851 | { |
| 2852 | while (*s) { |
| 2853 | if (*s == '\\') { |
| 2854 | if (!s[1]) |
| 2855 | return 0; |
| 2856 | s += 2; |
| 2857 | continue; |
| 2858 | } |
| 2859 | if (*s == '*' || *s == '[' || *s == '?') |
| 2860 | return 1; |
| 2861 | s++; |
| 2862 | } |
| 2863 | return 0; |
| 2864 | } |
| 2865 | /* Performs globbing on last list[], |
| 2866 | * saving each result as a new list[]. |
| 2867 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2868 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2869 | { |
| 2870 | glob_t globdata; |
| 2871 | int gr; |
| 2872 | char *pattern; |
| 2873 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2874 | 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] | 2875 | if (!o->data) |
| 2876 | return o_save_ptr_helper(o, n); |
| 2877 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2878 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2879 | if (!glob_needed(pattern)) { |
| 2880 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2881 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2882 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2883 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2884 | return o_save_ptr_helper(o, n); |
| 2885 | } |
| 2886 | |
| 2887 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2888 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2889 | * If we glob "*.\*" and don't find anything, we need |
| 2890 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2891 | * will return "*.\*"! |
| 2892 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2893 | gr = glob(pattern, 0, NULL, &globdata); |
| 2894 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2895 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2896 | if (gr == GLOB_NOMATCH) { |
| 2897 | globfree(&globdata); |
| 2898 | goto literal; |
| 2899 | } |
| 2900 | if (gr == GLOB_NOSPACE) |
| 2901 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2902 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2903 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2904 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2905 | } |
| 2906 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2907 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2908 | /* "forget" pattern in o */ |
| 2909 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2910 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2911 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2912 | n = o_save_ptr_helper(o, n); |
| 2913 | argv++; |
| 2914 | if (!*argv) |
| 2915 | break; |
| 2916 | } |
| 2917 | } |
| 2918 | globfree(&globdata); |
| 2919 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2920 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2921 | return n; |
| 2922 | } |
| 2923 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2924 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2925 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2926 | /* 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] | 2927 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2928 | static int o_save_ptr(o_string *o, int n) |
| 2929 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2930 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2931 | /* If o->has_empty_slot, list[n] was already globbed |
| 2932 | * (if it was requested back then when it was filled) |
| 2933 | * so don't do that again! */ |
| 2934 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2935 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2936 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2937 | return o_save_ptr_helper(o, n); |
| 2938 | } |
| 2939 | |
| 2940 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2941 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2942 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2943 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2944 | int string_start; |
| 2945 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2946 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2947 | if (DEBUG_EXPAND) |
| 2948 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2949 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2950 | list = (char**)o->data; |
| 2951 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2952 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2953 | while (n) { |
| 2954 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2955 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2956 | } |
| 2957 | return list; |
| 2958 | } |
| 2959 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2960 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2961 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2962 | /* Returns pi->next - next pipe in the list */ |
| 2963 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2964 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2965 | struct pipe *next; |
| 2966 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2967 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2968 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2969 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2970 | struct command *command; |
| 2971 | struct redir_struct *r, *rnext; |
| 2972 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2973 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2974 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2975 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2976 | if (DEBUG_CLEAN) { |
| 2977 | int a; |
| 2978 | char **p; |
| 2979 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2980 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2981 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2982 | } |
| 2983 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2984 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2985 | } |
| 2986 | /* not "else if": on syntax error, we may have both! */ |
| 2987 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2988 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2989 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2990 | free_pipe_list(command->group); |
| 2991 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2992 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2993 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2994 | /* else is crucial here. |
| 2995 | * If group != NULL, child_func is meaningless */ |
| 2996 | #if ENABLE_HUSH_FUNCTIONS |
| 2997 | else if (command->child_func) { |
| 2998 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2999 | command->child_func->parent_cmd = NULL; |
| 3000 | } |
| 3001 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3002 | #if !BB_MMU |
| 3003 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3004 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3005 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3006 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3007 | debug_printf_clean(" redirect %d%s", |
| 3008 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3009 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3010 | if (r->rd_filename) { |
| 3011 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3012 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3013 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3014 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3015 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3016 | rnext = r->next; |
| 3017 | free(r); |
| 3018 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3019 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3020 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3021 | 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] | 3022 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3023 | #if ENABLE_HUSH_JOB |
| 3024 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3025 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3026 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3027 | |
| 3028 | next = pi->next; |
| 3029 | free(pi); |
| 3030 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3031 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3032 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3033 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3034 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3035 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3036 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3037 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3038 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3039 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3040 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3041 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3045 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3046 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3047 | #ifndef debug_print_tree |
| 3048 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3049 | { |
| 3050 | static const char *const PIPE[] = { |
| 3051 | [PIPE_SEQ] = "SEQ", |
| 3052 | [PIPE_AND] = "AND", |
| 3053 | [PIPE_OR ] = "OR" , |
| 3054 | [PIPE_BG ] = "BG" , |
| 3055 | }; |
| 3056 | static const char *RES[] = { |
| 3057 | [RES_NONE ] = "NONE" , |
| 3058 | # if ENABLE_HUSH_IF |
| 3059 | [RES_IF ] = "IF" , |
| 3060 | [RES_THEN ] = "THEN" , |
| 3061 | [RES_ELIF ] = "ELIF" , |
| 3062 | [RES_ELSE ] = "ELSE" , |
| 3063 | [RES_FI ] = "FI" , |
| 3064 | # endif |
| 3065 | # if ENABLE_HUSH_LOOPS |
| 3066 | [RES_FOR ] = "FOR" , |
| 3067 | [RES_WHILE] = "WHILE", |
| 3068 | [RES_UNTIL] = "UNTIL", |
| 3069 | [RES_DO ] = "DO" , |
| 3070 | [RES_DONE ] = "DONE" , |
| 3071 | # endif |
| 3072 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3073 | [RES_IN ] = "IN" , |
| 3074 | # endif |
| 3075 | # if ENABLE_HUSH_CASE |
| 3076 | [RES_CASE ] = "CASE" , |
| 3077 | [RES_CASE_IN ] = "CASE_IN" , |
| 3078 | [RES_MATCH] = "MATCH", |
| 3079 | [RES_CASE_BODY] = "CASE_BODY", |
| 3080 | [RES_ESAC ] = "ESAC" , |
| 3081 | # endif |
| 3082 | [RES_XXXX ] = "XXXX" , |
| 3083 | [RES_SNTX ] = "SNTX" , |
| 3084 | }; |
| 3085 | static const char *const CMDTYPE[] = { |
| 3086 | "{}", |
| 3087 | "()", |
| 3088 | "[noglob]", |
| 3089 | # if ENABLE_HUSH_FUNCTIONS |
| 3090 | "func()", |
| 3091 | # endif |
| 3092 | }; |
| 3093 | |
| 3094 | int pin, prn; |
| 3095 | |
| 3096 | pin = 0; |
| 3097 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3098 | 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] | 3099 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 3100 | prn = 0; |
| 3101 | while (prn < pi->num_cmds) { |
| 3102 | struct command *command = &pi->cmds[prn]; |
| 3103 | char **argv = command->argv; |
| 3104 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3105 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3106 | lvl*2, "", prn, |
| 3107 | command->assignment_cnt); |
| 3108 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3109 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3110 | CMDTYPE[command->cmd_type], |
| 3111 | argv |
| 3112 | # if !BB_MMU |
| 3113 | , " group_as_string:", command->group_as_string |
| 3114 | # else |
| 3115 | , "", "" |
| 3116 | # endif |
| 3117 | ); |
| 3118 | debug_print_tree(command->group, lvl+1); |
| 3119 | prn++; |
| 3120 | continue; |
| 3121 | } |
| 3122 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3123 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3124 | argv++; |
| 3125 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3126 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3127 | prn++; |
| 3128 | } |
| 3129 | pi = pi->next; |
| 3130 | pin++; |
| 3131 | } |
| 3132 | } |
| 3133 | #endif /* debug_print_tree */ |
| 3134 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3135 | static struct pipe *new_pipe(void) |
| 3136 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3137 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3138 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3139 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3140 | return pi; |
| 3141 | } |
| 3142 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3143 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3144 | * if ctx->command is NULL. |
| 3145 | * No errors possible here. |
| 3146 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3147 | static int done_command(struct parse_context *ctx) |
| 3148 | { |
| 3149 | /* The command is really already in the pipe structure, so |
| 3150 | * advance the pipe counter and make a new, null command. */ |
| 3151 | struct pipe *pi = ctx->pipe; |
| 3152 | struct command *command = ctx->command; |
| 3153 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3154 | #if 0 /* Instead we emit error message at run time */ |
| 3155 | if (ctx->pending_redirect) { |
| 3156 | /* For example, "cmd >" (no filename to redirect to) */ |
| 3157 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3158 | ctx->pending_redirect = NULL; |
| 3159 | } |
| 3160 | #endif |
| 3161 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3162 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3163 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3164 | 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] | 3165 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3166 | } |
| 3167 | pi->num_cmds++; |
| 3168 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3169 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3170 | } else { |
| 3171 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3172 | } |
| 3173 | |
| 3174 | /* Only real trickiness here is that the uncommitted |
| 3175 | * command structure is not counted in pi->num_cmds. */ |
| 3176 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3177 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3178 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3179 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3180 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3181 | } |
| 3182 | |
| 3183 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3184 | { |
| 3185 | int not_null; |
| 3186 | |
| 3187 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3188 | /* Close previous command */ |
| 3189 | not_null = done_command(ctx); |
| 3190 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3191 | #if HAS_KEYWORDS |
| 3192 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3193 | ctx->ctx_inverted = 0; |
| 3194 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3195 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3196 | |
| 3197 | /* Without this check, even just <enter> on command line generates |
| 3198 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3199 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3200 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3201 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3202 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3203 | #endif |
| 3204 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3205 | || ctx->ctx_res_w == RES_DONE |
| 3206 | || ctx->ctx_res_w == RES_FOR |
| 3207 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3208 | #endif |
| 3209 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3210 | || ctx->ctx_res_w == RES_ESAC |
| 3211 | #endif |
| 3212 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3213 | struct pipe *new_p; |
| 3214 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3215 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3216 | not_null, ctx->ctx_res_w); |
| 3217 | new_p = new_pipe(); |
| 3218 | ctx->pipe->next = new_p; |
| 3219 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3220 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3221 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3222 | * This is used to control execution. |
| 3223 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3224 | * cases where variable or value happens to match a keyword): |
| 3225 | */ |
| 3226 | #if ENABLE_HUSH_LOOPS |
| 3227 | if (ctx->ctx_res_w == RES_FOR |
| 3228 | || ctx->ctx_res_w == RES_IN) |
| 3229 | ctx->ctx_res_w = RES_NONE; |
| 3230 | #endif |
| 3231 | #if ENABLE_HUSH_CASE |
| 3232 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3233 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3234 | if (ctx->ctx_res_w == RES_CASE) |
| 3235 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3236 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3237 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3238 | /* Create the memory for command, roughly: |
| 3239 | * ctx->pipe->cmds = new struct command; |
| 3240 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3241 | */ |
| 3242 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3243 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3244 | } |
| 3245 | debug_printf_parse("done_pipe return\n"); |
| 3246 | } |
| 3247 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3248 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3249 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3250 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3251 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3252 | /* Create the memory for command, roughly: |
| 3253 | * ctx->pipe->cmds = new struct command; |
| 3254 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3255 | */ |
| 3256 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3259 | /* If a reserved word is found and processed, parse context is modified |
| 3260 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3261 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3262 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3263 | struct reserved_combo { |
| 3264 | char literal[6]; |
| 3265 | unsigned char res; |
| 3266 | unsigned char assignment_flag; |
| 3267 | int flag; |
| 3268 | }; |
| 3269 | enum { |
| 3270 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3271 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3272 | FLAG_IF = (1 << RES_IF ), |
| 3273 | FLAG_THEN = (1 << RES_THEN ), |
| 3274 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3275 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3276 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3277 | # endif |
| 3278 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3279 | FLAG_FOR = (1 << RES_FOR ), |
| 3280 | FLAG_WHILE = (1 << RES_WHILE), |
| 3281 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3282 | FLAG_DO = (1 << RES_DO ), |
| 3283 | FLAG_DONE = (1 << RES_DONE ), |
| 3284 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3285 | # endif |
| 3286 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3287 | FLAG_MATCH = (1 << RES_MATCH), |
| 3288 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3289 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3290 | FLAG_START = (1 << RES_XXXX ), |
| 3291 | }; |
| 3292 | |
| 3293 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3294 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3295 | /* Mostly a list of accepted follow-up reserved words. |
| 3296 | * FLAG_END means we are done with the sequence, and are ready |
| 3297 | * to turn the compound list into a command. |
| 3298 | * FLAG_START means the word must start a new compound list. |
| 3299 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3300 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3301 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3302 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3303 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3304 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3305 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3306 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3307 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3308 | # endif |
| 3309 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3310 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3311 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3312 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3313 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3314 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3315 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3316 | # endif |
| 3317 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3318 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3319 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3320 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3321 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3322 | const struct reserved_combo *r; |
| 3323 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3324 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3325 | if (strcmp(word->data, r->literal) == 0) |
| 3326 | return r; |
| 3327 | } |
| 3328 | return NULL; |
| 3329 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3330 | /* Return 0: not a keyword, 1: keyword |
| 3331 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3332 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3333 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3334 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3335 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3336 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3337 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3338 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3339 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3340 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3341 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3342 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3343 | r = match_reserved_word(word); |
| 3344 | if (!r) |
| 3345 | return 0; |
| 3346 | |
| 3347 | 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] | 3348 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3349 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3350 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3351 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3352 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3353 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3354 | if (r->flag == 0) { /* '!' */ |
| 3355 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3356 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3357 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3358 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3359 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3360 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3361 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3362 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3363 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3364 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3365 | old = xmalloc(sizeof(*old)); |
| 3366 | debug_printf_parse("push stack %p\n", old); |
| 3367 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3368 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3369 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3370 | } 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] | 3371 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3372 | ctx->ctx_res_w = RES_SNTX; |
| 3373 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3374 | } else { |
| 3375 | /* "{...} fi" is ok. "{...} if" is not |
| 3376 | * Example: |
| 3377 | * if { echo foo; } then { echo bar; } fi */ |
| 3378 | if (ctx->command->group) |
| 3379 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3380 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3381 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3382 | ctx->ctx_res_w = r->res; |
| 3383 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3384 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3385 | 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] | 3386 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3387 | if (ctx->old_flag & FLAG_END) { |
| 3388 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3389 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3390 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3391 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3392 | old = ctx->stack; |
| 3393 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3394 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3395 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3396 | /* At this point, the compound command's string is in |
| 3397 | * ctx->as_string... except for the leading keyword! |
| 3398 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3399 | * ctx->as_string will contain "true; then echo a; fi", |
| 3400 | * with "if " remaining in old->as_string! |
| 3401 | */ |
| 3402 | { |
| 3403 | char *str; |
| 3404 | int len = old->as_string.length; |
| 3405 | /* Concatenate halves */ |
| 3406 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3407 | o_free_unsafe(&ctx->as_string); |
| 3408 | /* Find where leading keyword starts in first half */ |
| 3409 | str = old->as_string.data + len; |
| 3410 | if (str > old->as_string.data) |
| 3411 | str--; /* skip whitespace after keyword */ |
| 3412 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3413 | str--; |
| 3414 | /* Ugh, we're done with this horrid hack */ |
| 3415 | old->command->group_as_string = xstrdup(str); |
| 3416 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3417 | old->command->group_as_string); |
| 3418 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3419 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3420 | *ctx = *old; /* physical copy */ |
| 3421 | free(old); |
| 3422 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3423 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3424 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3425 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3426 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3427 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3428 | * Normal return is 0. Syntax errors return 1. |
| 3429 | * Note: on return, word is reset, but not o_free'd! |
| 3430 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3431 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3432 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3433 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3434 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3435 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3436 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3437 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3438 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3439 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3440 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3441 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3442 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3443 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3444 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3445 | * "2.7 Redirection |
| 3446 | * ...the word that follows the redirection operator |
| 3447 | * shall be subjected to tilde expansion, parameter expansion, |
| 3448 | * command substitution, arithmetic expansion, and quote |
| 3449 | * removal. Pathname expansion shall not be performed |
| 3450 | * on the word by a non-interactive shell; an interactive |
| 3451 | * shell may perform it, but shall do so only when |
| 3452 | * the expansion would result in one word." |
| 3453 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3454 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3455 | /* Cater for >\file case: |
| 3456 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3457 | * Same with heredocs: |
| 3458 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3459 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3460 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3461 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3462 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3463 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3464 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3465 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3466 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3467 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3468 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3469 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3470 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3471 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3472 | if (ctx->ctx_dsemicolon |
| 3473 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3474 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3475 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3476 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3477 | ctx->ctx_dsemicolon = 0; |
| 3478 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3479 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3480 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3481 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3482 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3483 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3484 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3485 | # if ENABLE_HUSH_CASE |
| 3486 | && ctx->ctx_res_w != RES_CASE |
| 3487 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3488 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3489 | int reserved = reserved_word(word, ctx); |
| 3490 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3491 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3492 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3493 | debug_printf_parse("done_word return %d\n", |
| 3494 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3495 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3496 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3497 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3498 | if (strcmp(word->data, "[[") == 0) { |
| 3499 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3500 | } |
| 3501 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3502 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3503 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3504 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3505 | if (command->group) { |
| 3506 | /* "{ echo foo; } echo bar" - bad */ |
| 3507 | syntax_error_at(word->data); |
| 3508 | debug_printf_parse("done_word return 1: syntax error, " |
| 3509 | "groups and arglists don't mix\n"); |
| 3510 | return 1; |
| 3511 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3512 | |
| 3513 | /* If this word wasn't an assignment, next ones definitely |
| 3514 | * can't be assignments. Even if they look like ones. */ |
| 3515 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3516 | && word->o_assignment != WORD_IS_KEYWORD |
| 3517 | ) { |
| 3518 | word->o_assignment = NOT_ASSIGNMENT; |
| 3519 | } else { |
| 3520 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3521 | command->assignment_cnt++; |
| 3522 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3523 | } |
| 3524 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3525 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3526 | } |
| 3527 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3528 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3529 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3530 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3531 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3532 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3533 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3534 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3535 | char *p = word->data; |
| 3536 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3537 | && (p[1] & 0x7f) == '@' |
| 3538 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3539 | ) { |
| 3540 | p += 3; |
| 3541 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3542 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3543 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3544 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3545 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3546 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3547 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3548 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3549 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3550 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3551 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3552 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3553 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3554 | return 1; |
| 3555 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3556 | /* Force FOR to have just one word (variable name) */ |
| 3557 | /* NB: basically, this makes hush see "for v in ..." |
| 3558 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3559 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3560 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3561 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3562 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3563 | #if ENABLE_HUSH_CASE |
| 3564 | /* Force CASE to have just one word */ |
| 3565 | if (ctx->ctx_res_w == RES_CASE) { |
| 3566 | done_pipe(ctx, PIPE_SEQ); |
| 3567 | } |
| 3568 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3569 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3570 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3571 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3572 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3573 | return 0; |
| 3574 | } |
| 3575 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3576 | |
| 3577 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3578 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3579 | * Return: |
| 3580 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3581 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3582 | * REDIRFD_TO_FILE if no & was seen, |
| 3583 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3584 | */ |
| 3585 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3586 | #define parse_redir_right_fd(as_string, input) \ |
| 3587 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3588 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3589 | 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] | 3590 | { |
| 3591 | int ch, d, ok; |
| 3592 | |
| 3593 | ch = i_peek(input); |
| 3594 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3595 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3596 | |
| 3597 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3598 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3599 | ch = i_peek(input); |
| 3600 | if (ch == '-') { |
| 3601 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3602 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3603 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3604 | } |
| 3605 | d = 0; |
| 3606 | ok = 0; |
| 3607 | while (ch != EOF && isdigit(ch)) { |
| 3608 | d = d*10 + (ch-'0'); |
| 3609 | ok = 1; |
| 3610 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3611 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3612 | ch = i_peek(input); |
| 3613 | } |
| 3614 | if (ok) return d; |
| 3615 | |
| 3616 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3617 | |
| 3618 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3619 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3622 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3623 | */ |
| 3624 | static int parse_redirect(struct parse_context *ctx, |
| 3625 | int fd, |
| 3626 | redir_type style, |
| 3627 | struct in_str *input) |
| 3628 | { |
| 3629 | struct command *command = ctx->command; |
| 3630 | struct redir_struct *redir; |
| 3631 | struct redir_struct **redirp; |
| 3632 | int dup_num; |
| 3633 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3634 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3635 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3636 | /* Check for a '>&1' type redirect */ |
| 3637 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3638 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3639 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3640 | } else { |
| 3641 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3642 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3643 | if (dup_num) { /* <<-... */ |
| 3644 | ch = i_getch(input); |
| 3645 | nommu_addchr(&ctx->as_string, ch); |
| 3646 | ch = i_peek(input); |
| 3647 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3648 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3649 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3650 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3651 | int ch = i_peek(input); |
| 3652 | if (ch == '|') { |
| 3653 | /* >|FILE redirect ("clobbering" >). |
| 3654 | * Since we do not support "set -o noclobber" yet, |
| 3655 | * >| and > are the same for now. Just eat |. |
| 3656 | */ |
| 3657 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3658 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | /* Create a new redir_struct and append it to the linked list */ |
| 3663 | redirp = &command->redirects; |
| 3664 | while ((redir = *redirp) != NULL) { |
| 3665 | redirp = &(redir->next); |
| 3666 | } |
| 3667 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3668 | /* redir->next = NULL; */ |
| 3669 | /* redir->rd_filename = NULL; */ |
| 3670 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3671 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3672 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3673 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3674 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3675 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3676 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3677 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3678 | /* Erik had a check here that the file descriptor in question |
| 3679 | * is legit; I postpone that to "run time" |
| 3680 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3681 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3682 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3683 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3684 | #if 0 /* Instead we emit error message at run time */ |
| 3685 | if (ctx->pending_redirect) { |
| 3686 | /* For example, "cmd > <file" */ |
| 3687 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3688 | } |
| 3689 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3690 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3691 | * end of the next parsed word. */ |
| 3692 | ctx->pending_redirect = redir; |
| 3693 | } |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3697 | /* If a redirect is immediately preceded by a number, that number is |
| 3698 | * supposed to tell which file descriptor to redirect. This routine |
| 3699 | * looks for such preceding numbers. In an ideal world this routine |
| 3700 | * needs to handle all the following classes of redirects... |
| 3701 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3702 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3703 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3704 | * 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] | 3705 | * |
| 3706 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3707 | * "2.7 Redirection |
| 3708 | * ... If n is quoted, the number shall not be recognized as part of |
| 3709 | * the redirection expression. For example: |
| 3710 | * echo \2>a |
| 3711 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3712 | * 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] | 3713 | * |
| 3714 | * A -1 return means no valid number was found, |
| 3715 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3716 | */ |
| 3717 | static int redirect_opt_num(o_string *o) |
| 3718 | { |
| 3719 | int num; |
| 3720 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3721 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3722 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3723 | num = bb_strtou(o->data, NULL, 10); |
| 3724 | if (errno || num < 0) |
| 3725 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3726 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | return num; |
| 3728 | } |
| 3729 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3730 | #if BB_MMU |
| 3731 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3732 | fetch_till_str(input, word, skip_tabs) |
| 3733 | #endif |
| 3734 | static char *fetch_till_str(o_string *as_string, |
| 3735 | struct in_str *input, |
| 3736 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3737 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3738 | { |
| 3739 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3740 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3741 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3742 | int ch; |
| 3743 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3744 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 3745 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3746 | while (1) { |
| 3747 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3748 | if (ch != EOF) |
| 3749 | nommu_addchr(as_string, ch); |
| 3750 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3751 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3752 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3753 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3754 | heredoc.data[past_EOL] = '\0'; |
| 3755 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3756 | return heredoc.data; |
| 3757 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3758 | while (ch == '\n') { |
| 3759 | o_addchr(&heredoc, ch); |
| 3760 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3761 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3762 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3763 | do { |
| 3764 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3765 | if (ch != EOF) |
| 3766 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3767 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3768 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3769 | } |
| 3770 | if (ch == EOF) { |
| 3771 | o_free_unsafe(&heredoc); |
| 3772 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3773 | } |
| 3774 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3775 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3776 | if (prev == '\\' && ch == '\\') |
| 3777 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3778 | prev = 0; /* not \ */ |
| 3779 | else |
| 3780 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3784 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3785 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3786 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3787 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3788 | { |
| 3789 | struct pipe *pi = ctx->list_head; |
| 3790 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3791 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3792 | int i; |
| 3793 | struct command *cmd = pi->cmds; |
| 3794 | |
| 3795 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3796 | pi->num_cmds, |
| 3797 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3798 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3799 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3800 | |
| 3801 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3802 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3803 | while (redir) { |
| 3804 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3805 | char *p; |
| 3806 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3807 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3808 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3809 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3810 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3811 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3812 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3813 | return 1; |
| 3814 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3815 | free(redir->rd_filename); |
| 3816 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3817 | heredoc_cnt--; |
| 3818 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3819 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3820 | } |
| 3821 | cmd++; |
| 3822 | } |
| 3823 | pi = pi->next; |
| 3824 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3825 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3826 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3827 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3828 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3829 | #endif |
| 3830 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3834 | static int run_list(struct pipe *pi); |
| 3835 | #if BB_MMU |
| 3836 | #define parse_stream(pstring, input, end_trigger) \ |
| 3837 | parse_stream(input, end_trigger) |
| 3838 | #endif |
| 3839 | static struct pipe *parse_stream(char **pstring, |
| 3840 | struct in_str *input, |
| 3841 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3842 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3843 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3844 | #if !ENABLE_HUSH_FUNCTIONS |
| 3845 | #define parse_group(dest, ctx, input, ch) \ |
| 3846 | parse_group(ctx, input, ch) |
| 3847 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3848 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3849 | struct in_str *input, int ch) |
| 3850 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3851 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3852 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3853 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3854 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3855 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3856 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3857 | |
| 3858 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3859 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3860 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3861 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3862 | if (done_word(dest, ctx)) |
| 3863 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3864 | if (!command->argv) |
| 3865 | goto skip; /* (... */ |
| 3866 | if (command->argv[1]) { /* word word ... (... */ |
| 3867 | syntax_error_unexpected_ch('('); |
| 3868 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3869 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3870 | /* it is "word(..." or "word (..." */ |
| 3871 | do |
| 3872 | ch = i_getch(input); |
| 3873 | while (ch == ' ' || ch == '\t'); |
| 3874 | if (ch != ')') { |
| 3875 | syntax_error_unexpected_ch(ch); |
| 3876 | return 1; |
| 3877 | } |
| 3878 | nommu_addchr(&ctx->as_string, ch); |
| 3879 | do |
| 3880 | ch = i_getch(input); |
| 3881 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3882 | if (ch != '{') { |
| 3883 | syntax_error_unexpected_ch(ch); |
| 3884 | return 1; |
| 3885 | } |
| 3886 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3887 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3888 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3889 | } |
| 3890 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3891 | |
| 3892 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3893 | if (command->argv /* word [word]{... */ |
| 3894 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3895 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3896 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3897 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3898 | debug_printf_parse("parse_group return 1: " |
| 3899 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3900 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3901 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3902 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3903 | |
| 3904 | #if ENABLE_HUSH_FUNCTIONS |
| 3905 | skip: |
| 3906 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3907 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3908 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3909 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3910 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3911 | } else { |
| 3912 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 3913 | ch = i_peek(input); |
| 3914 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 3915 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 3916 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3917 | syntax_error_unexpected_ch(ch); |
| 3918 | return 1; |
| 3919 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 3920 | if (ch != '(') { |
| 3921 | ch = i_getch(input); |
| 3922 | nommu_addchr(&ctx->as_string, ch); |
| 3923 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3924 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3925 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3926 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3927 | #if BB_MMU |
| 3928 | # define as_string NULL |
| 3929 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3930 | char *as_string = NULL; |
| 3931 | #endif |
| 3932 | pipe_list = parse_stream(&as_string, input, endch); |
| 3933 | #if !BB_MMU |
| 3934 | if (as_string) |
| 3935 | o_addstr(&ctx->as_string, as_string); |
| 3936 | #endif |
| 3937 | /* empty ()/{} or parse error? */ |
| 3938 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3939 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3940 | if (!BB_MMU) |
| 3941 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3942 | debug_printf_parse("parse_group return 1: " |
| 3943 | "parse_stream returned %p\n", pipe_list); |
| 3944 | return 1; |
| 3945 | } |
| 3946 | command->group = pipe_list; |
| 3947 | #if !BB_MMU |
| 3948 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3949 | command->group_as_string = as_string; |
| 3950 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3951 | command->group_as_string); |
| 3952 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3953 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3954 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3955 | debug_printf_parse("parse_group return 0\n"); |
| 3956 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3957 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 3960 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 3961 | { |
| 3962 | for (;;) { |
| 3963 | int ch, ch2; |
| 3964 | |
| 3965 | ch = i_getch(input); |
| 3966 | if (ch != '\\') |
| 3967 | return ch; |
| 3968 | ch2 = i_peek(input); |
| 3969 | if (ch2 != '\n') |
| 3970 | return ch; |
| 3971 | /* backslash+newline, skip it */ |
| 3972 | i_getch(input); |
| 3973 | } |
| 3974 | } |
| 3975 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 3976 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 3977 | { |
| 3978 | for (;;) { |
| 3979 | int ch, ch2; |
| 3980 | |
| 3981 | ch = i_peek(input); |
| 3982 | if (ch != '\\') |
| 3983 | return ch; |
| 3984 | ch2 = i_peek2(input); |
| 3985 | if (ch2 != '\n') |
| 3986 | return ch; |
| 3987 | /* backslash+newline, skip it */ |
| 3988 | i_getch(input); |
| 3989 | i_getch(input); |
| 3990 | } |
| 3991 | } |
| 3992 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3993 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3994 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3995 | 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] | 3996 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 3997 | 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] | 3998 | { |
| 3999 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4000 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4001 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4002 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4003 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4004 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4005 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4006 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4007 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4008 | } |
| 4009 | } |
| 4010 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4011 | 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] | 4012 | { |
| 4013 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4014 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4015 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4016 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4017 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4018 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4019 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4020 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4021 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4022 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4023 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4024 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4025 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4026 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4027 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4028 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4029 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4030 | continue; |
| 4031 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4032 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4036 | * \` quoting. |
| 4037 | * "Within the backquoted style of command substitution, backslash |
| 4038 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4039 | * The search for the matching backquote shall be satisfied by the first |
| 4040 | * backquote found without a preceding backslash; during this search, |
| 4041 | * if a non-escaped backquote is encountered within a shell comment, |
| 4042 | * a here-document, an embedded command substitution of the $(command) |
| 4043 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4044 | * or double-quoted string that begins, but does not end, within the |
| 4045 | * "`...`" sequence produces undefined results." |
| 4046 | * Example Output |
| 4047 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4048 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4049 | 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] | 4050 | { |
| 4051 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4052 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4053 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4054 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4055 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4056 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4057 | ch = i_getch(input); |
| 4058 | if (ch != '`' |
| 4059 | && ch != '$' |
| 4060 | && ch != '\\' |
| 4061 | && (!in_dquote || ch != '"') |
| 4062 | ) { |
| 4063 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4064 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4065 | } |
| 4066 | if (ch == EOF) { |
| 4067 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4068 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4069 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4070 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4071 | } |
| 4072 | } |
| 4073 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4074 | * quoting and nested ()s. |
| 4075 | * "With the $(command) style of command substitution, all characters |
| 4076 | * following the open parenthesis to the matching closing parenthesis |
| 4077 | * constitute the command. Any valid shell script can be used for command, |
| 4078 | * except a script consisting solely of redirections which produces |
| 4079 | * unspecified results." |
| 4080 | * Example Output |
| 4081 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4082 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4083 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4084 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4085 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4086 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4087 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4088 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4089 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4090 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4091 | 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] | 4092 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4093 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4094 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4095 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4096 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4097 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4098 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4099 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4100 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4101 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4102 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4103 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4104 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4105 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4106 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4107 | if (!dbl) |
| 4108 | break; |
| 4109 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4110 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4111 | i_getch(input); /* eat second ')' */ |
| 4112 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4113 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4114 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4115 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4116 | if (ch == '(' || ch == '{') { |
| 4117 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4118 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4119 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4120 | o_addchr(dest, ch); |
| 4121 | continue; |
| 4122 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4123 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4124 | if (!add_till_single_quote(dest, input)) |
| 4125 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4126 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4127 | continue; |
| 4128 | } |
| 4129 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4130 | if (!add_till_double_quote(dest, input)) |
| 4131 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4132 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4133 | continue; |
| 4134 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4135 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4136 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4137 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4138 | o_addchr(dest, ch); |
| 4139 | continue; |
| 4140 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4141 | if (ch == '\\') { |
| 4142 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4143 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +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; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4147 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4148 | #if 0 |
| 4149 | if (ch == '\n') { |
| 4150 | /* "backslash+newline", ignore both */ |
| 4151 | o_delchr(dest); /* undo insertion of '\' */ |
| 4152 | continue; |
| 4153 | } |
| 4154 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4155 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4156 | continue; |
| 4157 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4158 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4159 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4160 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4161 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4162 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4163 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4164 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4165 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4166 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4167 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4168 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4169 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4170 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4171 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4172 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4173 | int ch = i_peek_and_eat_bkslash_nl(input); /* first character after the $ */ |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4174 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4175 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4176 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4177 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4178 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 4179 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4180 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4181 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4182 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4183 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4184 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4185 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4186 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4187 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4188 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4189 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4190 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4191 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4192 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4193 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4194 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4195 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4196 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4197 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4198 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4199 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4200 | o_addchr(dest, ch | quote_mask); |
| 4201 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4202 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4203 | case '$': /* pid */ |
| 4204 | case '!': /* last bg pid */ |
| 4205 | case '?': /* last exit code */ |
| 4206 | case '#': /* number of args */ |
| 4207 | case '*': /* args */ |
| 4208 | case '@': /* args */ |
| 4209 | goto make_one_char_var; |
| 4210 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4211 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4212 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4213 | ch = i_getch(input); /* eat '{' */ |
| 4214 | nommu_addchr(as_string, ch); |
| 4215 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4216 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4217 | /* It should be ${?}, or ${#var}, |
| 4218 | * or even ${?+subst} - operator acting on a special variable, |
| 4219 | * or the beginning of variable name. |
| 4220 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4221 | if (ch == EOF |
| 4222 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4223 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4224 | bad_dollar_syntax: |
| 4225 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4226 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4227 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4228 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4229 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4230 | ch |= quote_mask; |
| 4231 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4232 | /* 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] | 4233 | * However, this regresses some of our testsuite cases |
| 4234 | * which check invalid constructs like ${%}. |
| 4235 | * Oh well... let's check that the var name part is fine... */ |
| 4236 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4237 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4238 | unsigned pos; |
| 4239 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4240 | o_addchr(dest, ch); |
| 4241 | debug_printf_parse(": '%c'\n", ch); |
| 4242 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4243 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4244 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4245 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4246 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4247 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4248 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4249 | unsigned end_ch; |
| 4250 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4251 | /* handle parameter expansions |
| 4252 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4253 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4254 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4255 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4256 | |
| 4257 | /* Eat everything until closing '}' (or ':') */ |
| 4258 | end_ch = '}'; |
| 4259 | if (ENABLE_HUSH_BASH_COMPAT |
| 4260 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4261 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4262 | ) { |
| 4263 | /* It's ${var:N[:M]} thing */ |
| 4264 | end_ch = '}' * 0x100 + ':'; |
| 4265 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4266 | if (ENABLE_HUSH_BASH_COMPAT |
| 4267 | && ch == '/' |
| 4268 | ) { |
| 4269 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4270 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4271 | i_getch(input); |
| 4272 | nommu_addchr(as_string, '/'); |
| 4273 | ch = '\\'; |
| 4274 | } |
| 4275 | end_ch = '}' * 0x100 + '/'; |
| 4276 | } |
| 4277 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4278 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4279 | if (!BB_MMU) |
| 4280 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4281 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4282 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4283 | if (last_ch == 0) /* error? */ |
| 4284 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4285 | #else |
| 4286 | #error Simple code to only allow ${var} is not implemented |
| 4287 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4288 | if (as_string) { |
| 4289 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4290 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4291 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4292 | |
| 4293 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 4294 | /* close the first block: */ |
| 4295 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4296 | /* while parsing N from ${var:N[:M]} |
| 4297 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4298 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4299 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4300 | end_ch = '}'; |
| 4301 | goto again; |
| 4302 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4303 | /* got '}' */ |
| 4304 | if (end_ch == '}' * 0x100 + ':') { |
| 4305 | /* it's ${var:N} - emulate :999999999 */ |
| 4306 | o_addstr(dest, "999999999"); |
| 4307 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4308 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4309 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4310 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4311 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4312 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4313 | break; |
| 4314 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4315 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4316 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4317 | unsigned pos; |
| 4318 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4319 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4320 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4321 | # if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4322 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4323 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4324 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4325 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4326 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4327 | if (!BB_MMU) |
| 4328 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4329 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4330 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4331 | if (as_string) { |
| 4332 | o_addstr(as_string, dest->data + pos); |
| 4333 | o_addchr(as_string, ')'); |
| 4334 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4335 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4336 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4337 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4338 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4339 | # endif |
| 4340 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4341 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4342 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4343 | if (!BB_MMU) |
| 4344 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4345 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4346 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4347 | if (as_string) { |
| 4348 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4349 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4350 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4351 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4352 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4353 | break; |
| 4354 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4355 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4356 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4357 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4358 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4359 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4360 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4361 | ch = '_'; |
| 4362 | goto make_var; |
| 4363 | } |
| 4364 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4365 | /* TODO: $_ and $-: */ |
| 4366 | /* $_ Shell or shell script name; or last argument of last command |
| 4367 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4368 | * 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] | 4369 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4370 | default: |
| 4371 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4372 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4373 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4374 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4375 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4378 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4379 | # if ENABLE_HUSH_BASH_COMPAT |
| 4380 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4381 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4382 | # else |
| 4383 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4384 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4385 | encode_string(dest, input, dquote_end) |
| 4386 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4387 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4388 | |
| 4389 | #else /* !MMU */ |
| 4390 | |
| 4391 | # if ENABLE_HUSH_BASH_COMPAT |
| 4392 | /* all parameters are needed, no macro tricks */ |
| 4393 | # else |
| 4394 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4395 | encode_string(as_string, dest, input, dquote_end) |
| 4396 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4397 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4398 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4399 | o_string *dest, |
| 4400 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4401 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4402 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4403 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4404 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4405 | const int process_bkslash = 1; |
| 4406 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4407 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4408 | int next; |
| 4409 | |
| 4410 | again: |
| 4411 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4412 | if (ch != EOF) |
| 4413 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4414 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4415 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4416 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4417 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4418 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4419 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4420 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4421 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4422 | } |
| 4423 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4424 | if (ch != '\n') { |
| 4425 | next = i_peek(input); |
| 4426 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4427 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4428 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4429 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4430 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4431 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4432 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4433 | } |
| 4434 | /* bash: |
| 4435 | * "The backslash retains its special meaning [in "..."] |
| 4436 | * only when followed by one of the following characters: |
| 4437 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4438 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4439 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4440 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4441 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4442 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4443 | ch = i_getch(input); /* eat next */ |
| 4444 | if (ch == '\n') |
| 4445 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4446 | } /* else: ch remains == '\\', and we double it below: */ |
| 4447 | 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] | 4448 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4449 | goto again; |
| 4450 | } |
| 4451 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4452 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4453 | debug_printf_parse("encode_string return 0: " |
| 4454 | "parse_dollar returned 0 (error)\n"); |
| 4455 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4456 | } |
| 4457 | goto again; |
| 4458 | } |
| 4459 | #if ENABLE_HUSH_TICK |
| 4460 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4461 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4462 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4463 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4464 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4465 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4466 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4467 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4468 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4469 | } |
| 4470 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4471 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4472 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4473 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4476 | /* |
| 4477 | * Scan input until EOF or end_trigger char. |
| 4478 | * Return a list of pipes to execute, or NULL on EOF |
| 4479 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4480 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4481 | * reset parsing machinery and start parsing anew, |
| 4482 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4483 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4484 | static struct pipe *parse_stream(char **pstring, |
| 4485 | struct in_str *input, |
| 4486 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4487 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4488 | struct parse_context ctx; |
| 4489 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4490 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4491 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4492 | /* 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] | 4493 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4494 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4495 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4496 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4497 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4498 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4499 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4500 | * Preventing this: */ |
| 4501 | o_addchr(&dest, '\0'); |
| 4502 | dest.length = 0; |
| 4503 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4504 | /* We used to separate words on $IFS here. This was wrong. |
| 4505 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4506 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4507 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4508 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4509 | if (MAYBE_ASSIGNMENT != 0) |
| 4510 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4511 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4512 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4513 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4514 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4515 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4516 | int ch; |
| 4517 | int next; |
| 4518 | int redir_fd; |
| 4519 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4520 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4521 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4522 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4523 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4524 | if (ch == EOF) { |
| 4525 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4526 | |
| 4527 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4528 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4529 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4530 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4531 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4532 | syntax_error_unterm_ch('('); |
| 4533 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4534 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 4535 | if (end_trigger == '}') { |
| 4536 | syntax_error_unterm_ch('{'); |
| 4537 | goto parse_error; |
| 4538 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4539 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4540 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4541 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4542 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4543 | o_free(&dest); |
| 4544 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4545 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4546 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4547 | /* (this makes bare "&" cmd a no-op. |
| 4548 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4549 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4550 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4551 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4552 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4553 | pi = NULL; |
| 4554 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4555 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4556 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4557 | if (pstring) |
| 4558 | *pstring = ctx.as_string.data; |
| 4559 | else |
| 4560 | o_free_unsafe(&ctx.as_string); |
| 4561 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4562 | debug_leave(); |
| 4563 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4564 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4565 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4566 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4567 | |
| 4568 | next = '\0'; |
| 4569 | if (ch != '\n') |
| 4570 | next = i_peek(input); |
| 4571 | |
| 4572 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4573 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4574 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4575 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4576 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4577 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4578 | || (next != ';' /* }; - special */ |
| 4579 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4580 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4581 | && next != '&' /* }& and }&& ... - special */ |
| 4582 | && next != '|' /* }|| ... - special */ |
| 4583 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4584 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4585 | ) { |
| 4586 | /* They are not special, skip "{}" */ |
| 4587 | is_special += 2; |
| 4588 | } |
| 4589 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4590 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4591 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4592 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4593 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4594 | o_addQchr(&dest, ch); |
| 4595 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4596 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4597 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4598 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4599 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4600 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4601 | 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] | 4602 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4603 | continue; |
| 4604 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4605 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4606 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4607 | if (done_word(&dest, &ctx)) { |
| 4608 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4609 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4610 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4611 | /* Is this a case when newline is simply ignored? |
| 4612 | * Some examples: |
| 4613 | * "cmd | <newline> cmd ..." |
| 4614 | * "case ... in <newline> word) ..." |
| 4615 | */ |
| 4616 | if (IS_NULL_CMD(ctx.command) |
| 4617 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4618 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4619 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4620 | * Without check #1, interactive shell |
| 4621 | * ignores even bare <newline>, |
| 4622 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4623 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4624 | * ps2> _ <=== wrong, should be ps1 |
| 4625 | * Without check #2, "cmd & <newline>" |
| 4626 | * is similarly mistreated. |
| 4627 | * (BTW, this makes "cmd & cmd" |
| 4628 | * and "cmd && cmd" non-orthogonal. |
| 4629 | * Really, ask yourself, why |
| 4630 | * "cmd && <newline>" doesn't start |
| 4631 | * cmd but waits for more input? |
| 4632 | * No reason...) |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4633 | */ |
| 4634 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4635 | if (pi->num_cmds != 0 /* check #1 */ |
| 4636 | && pi->followup != PIPE_BG /* check #2 */ |
| 4637 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4638 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4639 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4640 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4641 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4642 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4643 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4644 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4645 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4646 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4647 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4648 | heredoc_cnt = 0; |
| 4649 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4650 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4651 | 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] | 4652 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4653 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4654 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4655 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4656 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4657 | |
| 4658 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4659 | * } is an ordinary char in this case, even inside { cmd; } |
| 4660 | * Pathological example: { ""}; } should exec "}" cmd |
| 4661 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4662 | if (ch == '}') { |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4663 | if (dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4664 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4665 | ) { |
| 4666 | goto ordinary_char; |
| 4667 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4668 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 4669 | /* Generally, there should be semicolon: "cmd; }" |
| 4670 | * However, bash allows to omit it if "cmd" is |
| 4671 | * a group. Examples: |
| 4672 | * { { echo 1; } } |
| 4673 | * {(echo 1)} |
| 4674 | * { echo 0 >&2 | { echo 1; } } |
| 4675 | * { while false; do :; done } |
| 4676 | * { case a in b) ;; esac } |
| 4677 | */ |
| 4678 | if (ctx.command->group) |
| 4679 | goto term_group; |
| 4680 | goto ordinary_char; |
| 4681 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4682 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4683 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4684 | goto skip_end_trigger; |
| 4685 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4686 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4687 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4688 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4689 | && (ch != ';' || heredoc_cnt == 0) |
| 4690 | #if ENABLE_HUSH_CASE |
| 4691 | && (ch != ')' |
| 4692 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4693 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4694 | ) |
| 4695 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4696 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4697 | if (heredoc_cnt) { |
| 4698 | /* This is technically valid: |
| 4699 | * { cat <<HERE; }; echo Ok |
| 4700 | * heredoc |
| 4701 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4702 | * HERE |
| 4703 | * but we don't support this. |
| 4704 | * We require heredoc to be in enclosing {}/(), |
| 4705 | * if any. |
| 4706 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4707 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4708 | goto parse_error; |
| 4709 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4710 | if (done_word(&dest, &ctx)) { |
| 4711 | goto parse_error; |
| 4712 | } |
| 4713 | done_pipe(&ctx, PIPE_SEQ); |
| 4714 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4715 | 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] | 4716 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4717 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4718 | 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] | 4719 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4720 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4721 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4722 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4723 | if (pstring) |
| 4724 | *pstring = ctx.as_string.data; |
| 4725 | else |
| 4726 | o_free_unsafe(&ctx.as_string); |
| 4727 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4728 | debug_leave(); |
| 4729 | debug_printf_parse("parse_stream return %p: " |
| 4730 | "end_trigger char found\n", |
| 4731 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4732 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4733 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4734 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4735 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4736 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4737 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4738 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4739 | /* Catch <, > before deciding whether this word is |
| 4740 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4741 | switch (ch) { |
| 4742 | case '>': |
| 4743 | redir_fd = redirect_opt_num(&dest); |
| 4744 | if (done_word(&dest, &ctx)) { |
| 4745 | goto parse_error; |
| 4746 | } |
| 4747 | redir_style = REDIRECT_OVERWRITE; |
| 4748 | if (next == '>') { |
| 4749 | redir_style = REDIRECT_APPEND; |
| 4750 | ch = i_getch(input); |
| 4751 | nommu_addchr(&ctx.as_string, ch); |
| 4752 | } |
| 4753 | #if 0 |
| 4754 | else if (next == '(') { |
| 4755 | syntax_error(">(process) not supported"); |
| 4756 | goto parse_error; |
| 4757 | } |
| 4758 | #endif |
| 4759 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4760 | goto parse_error; |
| 4761 | continue; /* back to top of while (1) */ |
| 4762 | case '<': |
| 4763 | redir_fd = redirect_opt_num(&dest); |
| 4764 | if (done_word(&dest, &ctx)) { |
| 4765 | goto parse_error; |
| 4766 | } |
| 4767 | redir_style = REDIRECT_INPUT; |
| 4768 | if (next == '<') { |
| 4769 | redir_style = REDIRECT_HEREDOC; |
| 4770 | heredoc_cnt++; |
| 4771 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4772 | ch = i_getch(input); |
| 4773 | nommu_addchr(&ctx.as_string, ch); |
| 4774 | } else if (next == '>') { |
| 4775 | redir_style = REDIRECT_IO; |
| 4776 | ch = i_getch(input); |
| 4777 | nommu_addchr(&ctx.as_string, ch); |
| 4778 | } |
| 4779 | #if 0 |
| 4780 | else if (next == '(') { |
| 4781 | syntax_error("<(process) not supported"); |
| 4782 | goto parse_error; |
| 4783 | } |
| 4784 | #endif |
| 4785 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4786 | goto parse_error; |
| 4787 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4788 | case '#': |
| 4789 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4790 | /* skip "#comment" */ |
| 4791 | while (1) { |
| 4792 | ch = i_peek(input); |
| 4793 | if (ch == EOF || ch == '\n') |
| 4794 | break; |
| 4795 | i_getch(input); |
| 4796 | /* note: we do not add it to &ctx.as_string */ |
| 4797 | } |
| 4798 | nommu_addchr(&ctx.as_string, '\n'); |
| 4799 | continue; /* back to top of while (1) */ |
| 4800 | } |
| 4801 | break; |
| 4802 | case '\\': |
| 4803 | if (next == '\n') { |
| 4804 | /* It's "\<newline>" */ |
| 4805 | #if !BB_MMU |
| 4806 | /* Remove trailing '\' from ctx.as_string */ |
| 4807 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4808 | #endif |
| 4809 | ch = i_getch(input); /* eat it */ |
| 4810 | continue; /* back to top of while (1) */ |
| 4811 | } |
| 4812 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4813 | } |
| 4814 | |
| 4815 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4816 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4817 | && !ctx.pending_redirect |
| 4818 | ) { |
| 4819 | /* ch is a special char and thus this word |
| 4820 | * cannot be an assignment */ |
| 4821 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4822 | 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] | 4823 | } |
| 4824 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4825 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4826 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4827 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4828 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4829 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4830 | break; |
| 4831 | case '\\': |
| 4832 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4833 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4834 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4835 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4836 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4837 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4838 | o_addchr(&dest, '\\'); |
| 4839 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4840 | o_addchr(&dest, ch); |
| 4841 | nommu_addchr(&ctx.as_string, ch); |
| 4842 | /* Example: echo Hello \2>file |
| 4843 | * we need to know that word 2 is quoted */ |
| 4844 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4845 | break; |
| 4846 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4847 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4848 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4849 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4850 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4851 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4852 | break; |
| 4853 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4854 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4855 | if (next == '\'' && !ctx.pending_redirect) { |
| 4856 | insert_empty_quoted_str_marker: |
| 4857 | nommu_addchr(&ctx.as_string, next); |
| 4858 | i_getch(input); /* eat second ' */ |
| 4859 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4860 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4861 | } else { |
| 4862 | while (1) { |
| 4863 | ch = i_getch(input); |
| 4864 | if (ch == EOF) { |
| 4865 | syntax_error_unterm_ch('\''); |
| 4866 | goto parse_error; |
| 4867 | } |
| 4868 | nommu_addchr(&ctx.as_string, ch); |
| 4869 | if (ch == '\'') |
| 4870 | break; |
| 4871 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4872 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4873 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4874 | break; |
| 4875 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4876 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4877 | if (next == '"' && !ctx.pending_redirect) |
| 4878 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4879 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4880 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4881 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4882 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4883 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4885 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4886 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4887 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4888 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4889 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4890 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4891 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4892 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 4893 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4894 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4895 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4896 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4897 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4898 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4899 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4900 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4901 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4902 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4903 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4904 | #if ENABLE_HUSH_CASE |
| 4905 | case_semi: |
| 4906 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4907 | if (done_word(&dest, &ctx)) { |
| 4908 | goto parse_error; |
| 4909 | } |
| 4910 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4911 | #if ENABLE_HUSH_CASE |
| 4912 | /* Eat multiple semicolons, detect |
| 4913 | * whether it means something special */ |
| 4914 | while (1) { |
| 4915 | ch = i_peek(input); |
| 4916 | if (ch != ';') |
| 4917 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4918 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4919 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4920 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4921 | ctx.ctx_dsemicolon = 1; |
| 4922 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4923 | break; |
| 4924 | } |
| 4925 | } |
| 4926 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4927 | new_cmd: |
| 4928 | /* We just finished a cmd. New one may start |
| 4929 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4930 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4931 | 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] | 4932 | break; |
| 4933 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4934 | if (done_word(&dest, &ctx)) { |
| 4935 | goto parse_error; |
| 4936 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4937 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4938 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4939 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4940 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4941 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4942 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4943 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4944 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4945 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4946 | if (done_word(&dest, &ctx)) { |
| 4947 | goto parse_error; |
| 4948 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4949 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4950 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4951 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4952 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4953 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4954 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4955 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4956 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4957 | } else { |
| 4958 | /* we could pick up a file descriptor choice here |
| 4959 | * with redirect_opt_num(), but bash doesn't do it. |
| 4960 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4961 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4962 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4963 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4964 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4965 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4966 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4967 | if (ctx.ctx_res_w == RES_MATCH |
| 4968 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4969 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4970 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4971 | ) { |
| 4972 | continue; |
| 4973 | } |
| 4974 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4975 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4976 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4977 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4978 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4979 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4980 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4981 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4982 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4983 | goto case_semi; |
| 4984 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4985 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4986 | /* proper use of this character is caught by end_trigger: |
| 4987 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4988 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4989 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4990 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4991 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4992 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4993 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4994 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4995 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4996 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4997 | parse_error: |
| 4998 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4999 | struct parse_context *pctx; |
| 5000 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5001 | |
| 5002 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5003 | * Sample for finding leaks on syntax error recovery path. |
| 5004 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5005 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5006 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5007 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5008 | * 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] | 5009 | */ |
| 5010 | pctx = &ctx; |
| 5011 | do { |
| 5012 | /* Update pipe/command counts, |
| 5013 | * otherwise freeing may miss some */ |
| 5014 | done_pipe(pctx, PIPE_SEQ); |
| 5015 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5016 | pctx->list_head, pctx); |
| 5017 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5018 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5019 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5020 | #if !BB_MMU |
| 5021 | o_free_unsafe(&pctx->as_string); |
| 5022 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5023 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5024 | if (pctx != &ctx) { |
| 5025 | free(pctx); |
| 5026 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5027 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5028 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5029 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 5030 | o_free(&dest); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5031 | G.last_exitcode = 1; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5032 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5033 | if (pstring) |
| 5034 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5035 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5036 | debug_leave(); |
| 5037 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5038 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5039 | } |
| 5040 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5041 | |
| 5042 | /*** Execution routines ***/ |
| 5043 | |
| 5044 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5045 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5046 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5047 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5048 | expand_string_to_string(str) |
| 5049 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5050 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5051 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5052 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5053 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5054 | |
| 5055 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5056 | * all variable references within and returns a pointer to |
| 5057 | * a list of expanded strings, possibly with larger number |
| 5058 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5059 | * This new list is allocated as a single malloc block. |
| 5060 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5061 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5062 | * Caller can deallocate entire list by single free(list). */ |
| 5063 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5064 | /* A horde of its helpers come first: */ |
| 5065 | |
| 5066 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5067 | { |
| 5068 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5069 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5070 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5071 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5072 | if (c == '{' || c == '}') { |
| 5073 | /* { -> \{, } -> \} */ |
| 5074 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5075 | /* And now we want to add { or } and continue: |
| 5076 | * o_addchr(o, c); |
| 5077 | * continue; |
| 5078 | * luckily, just falling throught achieves this. |
| 5079 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5080 | } |
| 5081 | #endif |
| 5082 | o_addchr(o, c); |
| 5083 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5084 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5085 | o_addchr(o, '\\'); |
| 5086 | if (len) { |
| 5087 | len--; |
| 5088 | o_addchr(o, '\\'); |
| 5089 | o_addchr(o, *str++); |
| 5090 | } |
| 5091 | } |
| 5092 | } |
| 5093 | } |
| 5094 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5095 | /* Store given string, finalizing the word and starting new one whenever |
| 5096 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5097 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5098 | * Return in *ended_with_ifs: |
| 5099 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5100 | */ |
| 5101 | 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] | 5102 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5103 | int last_is_ifs = 0; |
| 5104 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5105 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5106 | int word_len; |
| 5107 | |
| 5108 | if (!*str) /* EOL - do not finalize word */ |
| 5109 | break; |
| 5110 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5111 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5112 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5113 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5114 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5115 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5116 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5117 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5118 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5119 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5120 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5121 | /*/ Why can't we do it easier? */ |
| 5122 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5123 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5124 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5125 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5126 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5127 | if (!*str) /* EOL - do not finalize word */ |
| 5128 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5129 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5130 | |
| 5131 | /* We know str here points to at least one IFS char */ |
| 5132 | last_is_ifs = 1; |
| 5133 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5134 | if (!*str) /* EOL - do not finalize word */ |
| 5135 | break; |
| 5136 | |
| 5137 | /* Start new word... but not always! */ |
| 5138 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5139 | if (output->has_quoted_part |
| 5140 | /* Case "v=' a'; echo $v": |
| 5141 | * here nothing precedes the space in $v expansion, |
| 5142 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5143 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5144 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5145 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5146 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5147 | o_addchr(output, '\0'); |
| 5148 | debug_print_list("expand_on_ifs", output, n); |
| 5149 | n = o_save_ptr(output, n); |
| 5150 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5151 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5152 | |
| 5153 | if (ended_with_ifs) |
| 5154 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5155 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5156 | return n; |
| 5157 | } |
| 5158 | |
| 5159 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5160 | * they are in double quotes, with the exception that they are not :). |
| 5161 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5162 | * |
| 5163 | * Returns malloced string. |
| 5164 | * As an optimization, we return NULL if expansion is not needed. |
| 5165 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5166 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5167 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5168 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5169 | encode_then_expand_string(str) |
| 5170 | #endif |
| 5171 | 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] | 5172 | { |
| 5173 | char *exp_str; |
| 5174 | struct in_str input; |
| 5175 | o_string dest = NULL_O_STRING; |
| 5176 | |
| 5177 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5178 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5179 | #if ENABLE_HUSH_TICK |
| 5180 | && !strchr(str, '`') |
| 5181 | #endif |
| 5182 | ) { |
| 5183 | return NULL; |
| 5184 | } |
| 5185 | |
| 5186 | /* We need to expand. Example: |
| 5187 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5188 | */ |
| 5189 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5190 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5191 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5192 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5193 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5194 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5195 | o_free_unsafe(&dest); |
| 5196 | return exp_str; |
| 5197 | } |
| 5198 | |
| 5199 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5200 | 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] | 5201 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5202 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5203 | arith_t res; |
| 5204 | char *exp_str; |
| 5205 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5206 | math_state.lookupvar = get_local_var_value; |
| 5207 | math_state.setvar = set_local_var_from_halves; |
| 5208 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5209 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5210 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5211 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5212 | if (errmsg_p) |
| 5213 | *errmsg_p = math_state.errmsg; |
| 5214 | if (math_state.errmsg) |
| 5215 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5216 | return res; |
| 5217 | } |
| 5218 | #endif |
| 5219 | |
| 5220 | #if ENABLE_HUSH_BASH_COMPAT |
| 5221 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5222 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5223 | { |
| 5224 | while (1) { |
| 5225 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5226 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5227 | if (end) { |
| 5228 | *size = end - val; |
| 5229 | return val; |
| 5230 | } |
| 5231 | if (*val == '\0') |
| 5232 | return NULL; |
| 5233 | /* Optimization: if "*pat" did not match the start of "string", |
| 5234 | * we know that "tring", "ring" etc will not match too: |
| 5235 | */ |
| 5236 | if (pattern[0] == '*') |
| 5237 | return NULL; |
| 5238 | val++; |
| 5239 | } |
| 5240 | } |
| 5241 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5242 | { |
| 5243 | char *result = NULL; |
| 5244 | unsigned res_len = 0; |
| 5245 | unsigned repl_len = strlen(repl); |
| 5246 | |
| 5247 | while (1) { |
| 5248 | int size; |
| 5249 | char *s = strstr_pattern(val, pattern, &size); |
| 5250 | if (!s) |
| 5251 | break; |
| 5252 | |
| 5253 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 5254 | memcpy(result + res_len, val, s - val); |
| 5255 | res_len += s - val; |
| 5256 | strcpy(result + res_len, repl); |
| 5257 | res_len += repl_len; |
| 5258 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5259 | |
| 5260 | val = s + size; |
| 5261 | if (exp_op == '/') |
| 5262 | break; |
| 5263 | } |
| 5264 | if (val[0] && result) { |
| 5265 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5266 | strcpy(result + res_len, val); |
| 5267 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5268 | } |
| 5269 | debug_printf_varexp("result:'%s'\n", result); |
| 5270 | return result; |
| 5271 | } |
| 5272 | #endif |
| 5273 | |
| 5274 | /* Helper: |
| 5275 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5276 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5277 | 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] | 5278 | { |
| 5279 | const char *val = NULL; |
| 5280 | char *to_be_freed = NULL; |
| 5281 | char *p = *pp; |
| 5282 | char *var; |
| 5283 | char first_char; |
| 5284 | char exp_op; |
| 5285 | char exp_save = exp_save; /* for compiler */ |
| 5286 | char *exp_saveptr; /* points to expansion operator */ |
| 5287 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5288 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5289 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5290 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5291 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5292 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5293 | arg0 = arg[0]; |
| 5294 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5295 | exp_op = 0; |
| 5296 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5297 | if (first_char == '#' /* ${#... */ |
| 5298 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 5299 | ) { |
| 5300 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5301 | var++; |
| 5302 | exp_op = 'L'; |
| 5303 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5304 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5305 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5306 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5307 | ) { |
| 5308 | /* ${?:0}, ${#[:]%0} etc */ |
| 5309 | exp_saveptr = var + 1; |
| 5310 | } else { |
| 5311 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5312 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5313 | } |
| 5314 | exp_op = exp_save = *exp_saveptr; |
| 5315 | if (exp_op) { |
| 5316 | exp_word = exp_saveptr + 1; |
| 5317 | if (exp_op == ':') { |
| 5318 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5319 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5320 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5321 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5322 | ) { |
| 5323 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5324 | exp_op = ':'; |
| 5325 | exp_word--; |
| 5326 | } |
| 5327 | } |
| 5328 | *exp_saveptr = '\0'; |
| 5329 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5330 | } |
| 5331 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5332 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5333 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5334 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5335 | int n = xatoi_positive(var); |
| 5336 | if (n < G.global_argc) |
| 5337 | val = G.global_argv[n]; |
| 5338 | /* else val remains NULL: $N with too big N */ |
| 5339 | } else { |
| 5340 | switch (var[0]) { |
| 5341 | case '$': /* pid */ |
| 5342 | val = utoa(G.root_pid); |
| 5343 | break; |
| 5344 | case '!': /* bg pid */ |
| 5345 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5346 | break; |
| 5347 | case '?': /* exitcode */ |
| 5348 | val = utoa(G.last_exitcode); |
| 5349 | break; |
| 5350 | case '#': /* argc */ |
| 5351 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5352 | break; |
| 5353 | default: |
| 5354 | val = get_local_var_value(var); |
| 5355 | } |
| 5356 | } |
| 5357 | |
| 5358 | /* Handle any expansions */ |
| 5359 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5360 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5361 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5362 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5363 | debug_printf_expand("%s\n", val); |
| 5364 | } else if (exp_op) { |
| 5365 | if (exp_op == '%' || exp_op == '#') { |
| 5366 | /* Standard-mandated substring removal ops: |
| 5367 | * ${parameter%word} - remove smallest suffix pattern |
| 5368 | * ${parameter%%word} - remove largest suffix pattern |
| 5369 | * ${parameter#word} - remove smallest prefix pattern |
| 5370 | * ${parameter##word} - remove largest prefix pattern |
| 5371 | * |
| 5372 | * Word is expanded to produce a glob pattern. |
| 5373 | * Then var's value is matched to it and matching part removed. |
| 5374 | */ |
| 5375 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5376 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5377 | char *exp_exp_word; |
| 5378 | char *loc; |
| 5379 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5380 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5381 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5382 | 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] | 5383 | if (exp_exp_word) |
| 5384 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5385 | /* HACK ALERT. We depend here on the fact that |
| 5386 | * G.global_argv and results of utoa and get_local_var_value |
| 5387 | * are actually in writable memory: |
| 5388 | * scan_and_match momentarily stores NULs there. */ |
| 5389 | t = (char*)val; |
| 5390 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5391 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5392 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5393 | free(exp_exp_word); |
| 5394 | if (loc) { /* match was found */ |
| 5395 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5396 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5397 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5398 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | } |
| 5402 | #if ENABLE_HUSH_BASH_COMPAT |
| 5403 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5404 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5405 | * Note that in encoded form it has TWO parts: |
| 5406 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5407 | * and if // is used, it is encoded as \: |
| 5408 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5409 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5410 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5411 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5412 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5413 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5414 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5415 | * by the usual expansion rules: |
| 5416 | * >az; >bz; |
| 5417 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5418 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5419 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5420 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5421 | * (note that a*z _pattern_ is never globbed!) |
| 5422 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5423 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5424 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5425 | if (!pattern) |
| 5426 | pattern = xstrdup(exp_word); |
| 5427 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5428 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5429 | exp_word = p; |
| 5430 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5431 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5432 | 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] | 5433 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5434 | /* HACK ALERT. We depend here on the fact that |
| 5435 | * G.global_argv and results of utoa and get_local_var_value |
| 5436 | * are actually in writable memory: |
| 5437 | * replace_pattern momentarily stores NULs there. */ |
| 5438 | t = (char*)val; |
| 5439 | to_be_freed = replace_pattern(t, |
| 5440 | pattern, |
| 5441 | (repl ? repl : exp_word), |
| 5442 | exp_op); |
| 5443 | if (to_be_freed) /* at least one replace happened */ |
| 5444 | val = to_be_freed; |
| 5445 | free(pattern); |
| 5446 | free(repl); |
| 5447 | } |
| 5448 | } |
| 5449 | #endif |
| 5450 | else if (exp_op == ':') { |
| 5451 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 5452 | /* It's ${var:N[:M]} bashism. |
| 5453 | * Note that in encoded form it has TWO parts: |
| 5454 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5455 | */ |
| 5456 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5457 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5458 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5459 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5460 | if (errmsg) |
| 5461 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5462 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5463 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5464 | exp_word = p; |
| 5465 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5466 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5467 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5468 | if (errmsg) |
| 5469 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5470 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5471 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5472 | if (beg < 0) /* bash compat */ |
| 5473 | beg = 0; |
| 5474 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5475 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5476 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5477 | val = NULL; |
| 5478 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5479 | /* Paranoia. What if user entered 9999999999999 |
| 5480 | * which fits in arith_t but not int? */ |
| 5481 | if (len >= INT_MAX) |
| 5482 | len = INT_MAX; |
| 5483 | val = to_be_freed = xstrndup(val + beg, len); |
| 5484 | } |
| 5485 | debug_printf_varexp("val:'%s'\n", val); |
| 5486 | } else |
| 5487 | #endif |
| 5488 | { |
| 5489 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5490 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5491 | } |
| 5492 | } else { /* one of "-=+?" */ |
| 5493 | /* Standard-mandated substitution ops: |
| 5494 | * ${var?word} - indicate error if unset |
| 5495 | * If var is unset, word (or a message indicating it is unset |
| 5496 | * if word is null) is written to standard error |
| 5497 | * and the shell exits with a non-zero exit status. |
| 5498 | * Otherwise, the value of var is substituted. |
| 5499 | * ${var-word} - use default value |
| 5500 | * If var is unset, word is substituted. |
| 5501 | * ${var=word} - assign and use default value |
| 5502 | * If var is unset, word is assigned to var. |
| 5503 | * In all cases, final value of var is substituted. |
| 5504 | * ${var+word} - use alternative value |
| 5505 | * If var is unset, null is substituted. |
| 5506 | * Otherwise, word is substituted. |
| 5507 | * |
| 5508 | * Word is subjected to tilde expansion, parameter expansion, |
| 5509 | * command substitution, and arithmetic expansion. |
| 5510 | * If word is not needed, it is not expanded. |
| 5511 | * |
| 5512 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5513 | * but also treat null var as if it is unset. |
| 5514 | */ |
| 5515 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5516 | if (exp_op == '+') |
| 5517 | use_word = !use_word; |
| 5518 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5519 | (exp_save == ':') ? "true" : "false", use_word); |
| 5520 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5521 | 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] | 5522 | if (to_be_freed) |
| 5523 | exp_word = to_be_freed; |
| 5524 | if (exp_op == '?') { |
| 5525 | /* mimic bash message */ |
| 5526 | die_if_script("%s: %s", |
| 5527 | var, |
| 5528 | exp_word[0] ? exp_word : "parameter null or not set" |
| 5529 | ); |
| 5530 | //TODO: how interactive bash aborts expansion mid-command? |
| 5531 | } else { |
| 5532 | val = exp_word; |
| 5533 | } |
| 5534 | |
| 5535 | if (exp_op == '=') { |
| 5536 | /* ${var=[word]} or ${var:=[word]} */ |
| 5537 | if (isdigit(var[0]) || var[0] == '#') { |
| 5538 | /* mimic bash message */ |
| 5539 | die_if_script("$%s: cannot assign in this way", var); |
| 5540 | val = NULL; |
| 5541 | } else { |
| 5542 | char *new_var = xasprintf("%s=%s", var, val); |
| 5543 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5544 | } |
| 5545 | } |
| 5546 | } |
| 5547 | } /* one of "-=+?" */ |
| 5548 | |
| 5549 | *exp_saveptr = exp_save; |
| 5550 | } /* if (exp_op) */ |
| 5551 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5552 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5553 | |
| 5554 | *pp = p; |
| 5555 | *to_be_freed_pp = to_be_freed; |
| 5556 | return val; |
| 5557 | } |
| 5558 | |
| 5559 | /* Expand all variable references in given string, adding words to list[] |
| 5560 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5561 | * to be filled). This routine is extremely tricky: has to deal with |
| 5562 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5563 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5564 | 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] | 5565 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5566 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5567 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5568 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5569 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5570 | 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] | 5571 | char *p; |
| 5572 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5573 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5574 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5575 | debug_print_list("expand_vars_to_list", output, n); |
| 5576 | n = o_save_ptr(output, n); |
| 5577 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5578 | |
| 5579 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5580 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5581 | char *to_be_freed = NULL; |
| 5582 | const char *val = NULL; |
| 5583 | #if ENABLE_HUSH_TICK |
| 5584 | o_string subst_result = NULL_O_STRING; |
| 5585 | #endif |
| 5586 | #if ENABLE_SH_MATH_SUPPORT |
| 5587 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5588 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5589 | |
| 5590 | if (ended_in_ifs) { |
| 5591 | o_addchr(output, '\0'); |
| 5592 | n = o_save_ptr(output, n); |
| 5593 | ended_in_ifs = 0; |
| 5594 | } |
| 5595 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5596 | o_addblock(output, arg, p - arg); |
| 5597 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5598 | arg = ++p; |
| 5599 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5600 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5601 | /* Fetch special var name (if it is indeed one of them) |
| 5602 | * and quote bit, force the bit on if singleword expansion - |
| 5603 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5604 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5605 | |
| 5606 | /* Is this variable quoted and thus expansion can't be null? |
| 5607 | * "$@" is special. Even if quoted, it can still |
| 5608 | * expand to nothing (not even an empty string), |
| 5609 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5610 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5611 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5612 | |
| 5613 | switch (first_ch & 0x7f) { |
| 5614 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5615 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5616 | case '@': { |
| 5617 | int i; |
| 5618 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5619 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5620 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5621 | 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] | 5622 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5623 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5624 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5625 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5626 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5627 | /* this argv[] is not empty and not last: |
| 5628 | * put terminating NUL, start new word */ |
| 5629 | o_addchr(output, '\0'); |
| 5630 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5631 | n = o_save_ptr(output, n); |
| 5632 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5633 | } |
| 5634 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5635 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5636 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5637 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5638 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5639 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5640 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5641 | while (1) { |
| 5642 | o_addQstr(output, G.global_argv[i]); |
| 5643 | if (++i >= G.global_argc) |
| 5644 | break; |
| 5645 | o_addchr(output, '\0'); |
| 5646 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5647 | n = o_save_ptr(output, n); |
| 5648 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5649 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5650 | while (1) { |
| 5651 | o_addQstr(output, G.global_argv[i]); |
| 5652 | if (!G.global_argv[++i]) |
| 5653 | break; |
| 5654 | if (G.ifs[0]) |
| 5655 | o_addchr(output, G.ifs[0]); |
| 5656 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5657 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5658 | } |
| 5659 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5660 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5661 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5662 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5663 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5664 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5665 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5666 | break; |
| 5667 | #if ENABLE_HUSH_TICK |
| 5668 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5669 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5670 | arg++; |
| 5671 | /* Can't just stuff it into output o_string, |
| 5672 | * expanded result may need to be globbed |
| 5673 | * and $IFS-splitted */ |
| 5674 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5675 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5676 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5677 | val = subst_result.data; |
| 5678 | goto store_val; |
| 5679 | #endif |
| 5680 | #if ENABLE_SH_MATH_SUPPORT |
| 5681 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5682 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5683 | |
| 5684 | arg++; /* skip '+' */ |
| 5685 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5686 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5687 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5688 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5689 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5690 | val = arith_buf; |
| 5691 | break; |
| 5692 | } |
| 5693 | #endif |
| 5694 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5695 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5696 | IF_HUSH_TICK(store_val:) |
| 5697 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5698 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5699 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5700 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5701 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5702 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5703 | } |
| 5704 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5705 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5706 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5707 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5708 | } |
| 5709 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5710 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5711 | |
| 5712 | if (val && val[0]) { |
| 5713 | o_addQstr(output, val); |
| 5714 | } |
| 5715 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5716 | |
| 5717 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5718 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5719 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5720 | *p = SPECIAL_VAR_SYMBOL; |
| 5721 | |
| 5722 | #if ENABLE_HUSH_TICK |
| 5723 | o_free(&subst_result); |
| 5724 | #endif |
| 5725 | arg = ++p; |
| 5726 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5727 | |
| 5728 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5729 | if (ended_in_ifs) { |
| 5730 | o_addchr(output, '\0'); |
| 5731 | n = o_save_ptr(output, n); |
| 5732 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5733 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5734 | /* this part is literal, and it was already pre-quoted |
| 5735 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5736 | o_addstr_with_NUL(output, arg); |
| 5737 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5738 | } 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] | 5739 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5740 | ) { |
| 5741 | n--; |
| 5742 | /* allow to reuse list[n] later without re-growth */ |
| 5743 | output->has_empty_slot = 1; |
| 5744 | } else { |
| 5745 | o_addchr(output, '\0'); |
| 5746 | } |
| 5747 | |
| 5748 | return n; |
| 5749 | } |
| 5750 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5751 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5752 | { |
| 5753 | int n; |
| 5754 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5755 | o_string output = NULL_O_STRING; |
| 5756 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5757 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5758 | |
| 5759 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5760 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5761 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5762 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5763 | } |
| 5764 | debug_print_list("expand_variables", &output, n); |
| 5765 | |
| 5766 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5767 | list = o_finalize_list(&output, n); |
| 5768 | debug_print_strings("expand_variables[1]", list); |
| 5769 | return list; |
| 5770 | } |
| 5771 | |
| 5772 | static char **expand_strvec_to_strvec(char **argv) |
| 5773 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5774 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | #if ENABLE_HUSH_BASH_COMPAT |
| 5778 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5779 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5780 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5781 | } |
| 5782 | #endif |
| 5783 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5784 | /* Used for expansion of right hand of assignments, |
| 5785 | * $((...)), heredocs, variable espansion parts. |
| 5786 | * |
| 5787 | * NB: should NOT do globbing! |
| 5788 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5789 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5790 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5791 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5792 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5793 | const int do_unbackslash = 1; |
| 5794 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5795 | char *argv[2], **list; |
| 5796 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5797 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5798 | /* This is generally an optimization, but it also |
| 5799 | * handles "", which otherwise trips over !list[0] check below. |
| 5800 | * (is this ever happens that we actually get str="" here?) |
| 5801 | */ |
| 5802 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5803 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5804 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5805 | return xstrdup(str); |
| 5806 | } |
| 5807 | |
| 5808 | argv[0] = (char*)str; |
| 5809 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5810 | list = expand_variables(argv, do_unbackslash |
| 5811 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5812 | : EXP_FLAG_SINGLEWORD |
| 5813 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5814 | if (HUSH_DEBUG) |
| 5815 | if (!list[0] || list[1]) |
| 5816 | bb_error_msg_and_die("BUG in varexp2"); |
| 5817 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5818 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5819 | if (do_unbackslash) |
| 5820 | unbackslash((char*)list); |
| 5821 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5822 | return (char*)list; |
| 5823 | } |
| 5824 | |
| 5825 | /* Used for "eval" builtin */ |
| 5826 | static char* expand_strvec_to_string(char **argv) |
| 5827 | { |
| 5828 | char **list; |
| 5829 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5830 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5831 | /* Convert all NULs to spaces */ |
| 5832 | if (list[0]) { |
| 5833 | int n = 1; |
| 5834 | while (list[n]) { |
| 5835 | if (HUSH_DEBUG) |
| 5836 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5837 | bb_error_msg_and_die("BUG in varexp3"); |
| 5838 | /* bash uses ' ' regardless of $IFS contents */ |
| 5839 | list[n][-1] = ' '; |
| 5840 | n++; |
| 5841 | } |
| 5842 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 5843 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5844 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5845 | return (char*)list; |
| 5846 | } |
| 5847 | |
| 5848 | static char **expand_assignments(char **argv, int count) |
| 5849 | { |
| 5850 | int i; |
| 5851 | char **p; |
| 5852 | |
| 5853 | G.expanded_assignments = p = NULL; |
| 5854 | /* Expand assignments into one string each */ |
| 5855 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5856 | 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] | 5857 | } |
| 5858 | G.expanded_assignments = NULL; |
| 5859 | return p; |
| 5860 | } |
| 5861 | |
| 5862 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5863 | static void switch_off_special_sigs(unsigned mask) |
| 5864 | { |
| 5865 | unsigned sig = 0; |
| 5866 | while ((mask >>= 1) != 0) { |
| 5867 | sig++; |
| 5868 | if (!(mask & 1)) |
| 5869 | continue; |
| 5870 | if (G.traps) { |
| 5871 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5872 | /* trap is '', has to remain SIG_IGN */ |
| 5873 | continue; |
| 5874 | free(G.traps[sig]); |
| 5875 | G.traps[sig] = NULL; |
| 5876 | } |
| 5877 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5878 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5879 | } |
| 5880 | } |
| 5881 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 5882 | #if BB_MMU |
| 5883 | /* never called */ |
| 5884 | void re_execute_shell(char ***to_free, const char *s, |
| 5885 | char *g_argv0, char **g_argv, |
| 5886 | char **builtin_argv) NORETURN; |
| 5887 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5888 | static void reset_traps_to_defaults(void) |
| 5889 | { |
| 5890 | /* This function is always called in a child shell |
| 5891 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5892 | */ |
| 5893 | unsigned sig; |
| 5894 | unsigned mask; |
| 5895 | |
| 5896 | /* Child shells are not interactive. |
| 5897 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5898 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5899 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5900 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5901 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
| 5902 | if (!G.traps && !mask) |
| 5903 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5904 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5905 | /* Switch off special sigs */ |
| 5906 | switch_off_special_sigs(mask); |
| 5907 | #if ENABLE_HUSH_JOB |
| 5908 | G_fatal_sig_mask = 0; |
| 5909 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 5910 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 5911 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 5912 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5913 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5914 | if (!G.traps) |
| 5915 | return; |
| 5916 | |
| 5917 | /* Reset all sigs to default except ones with empty traps */ |
| 5918 | for (sig = 0; sig < NSIG; sig++) { |
| 5919 | if (!G.traps[sig]) |
| 5920 | continue; /* no trap: nothing to do */ |
| 5921 | if (!G.traps[sig][0]) |
| 5922 | continue; /* empty trap: has to remain SIG_IGN */ |
| 5923 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5924 | free(G.traps[sig]); |
| 5925 | G.traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5926 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5927 | if (sig == 0) |
| 5928 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5929 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5930 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | #else /* !BB_MMU */ |
| 5934 | |
| 5935 | static void re_execute_shell(char ***to_free, const char *s, |
| 5936 | char *g_argv0, char **g_argv, |
| 5937 | char **builtin_argv) NORETURN; |
| 5938 | static void re_execute_shell(char ***to_free, const char *s, |
| 5939 | char *g_argv0, char **g_argv, |
| 5940 | char **builtin_argv) |
| 5941 | { |
| 5942 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5943 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5944 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5945 | char *heredoc_argv[4]; |
| 5946 | struct variable *cur; |
| 5947 | # if ENABLE_HUSH_FUNCTIONS |
| 5948 | struct function *funcp; |
| 5949 | # endif |
| 5950 | char **argv, **pp; |
| 5951 | unsigned cnt; |
| 5952 | unsigned long long empty_trap_mask; |
| 5953 | |
| 5954 | if (!g_argv0) { /* heredoc */ |
| 5955 | argv = heredoc_argv; |
| 5956 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5957 | argv[1] = (char *) "-<"; |
| 5958 | argv[2] = (char *) s; |
| 5959 | argv[3] = NULL; |
| 5960 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5961 | goto do_exec; |
| 5962 | } |
| 5963 | |
| 5964 | cnt = 0; |
| 5965 | pp = builtin_argv; |
| 5966 | if (pp) while (*pp++) |
| 5967 | cnt++; |
| 5968 | |
| 5969 | empty_trap_mask = 0; |
| 5970 | if (G.traps) { |
| 5971 | int sig; |
| 5972 | for (sig = 1; sig < NSIG; sig++) { |
| 5973 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5974 | empty_trap_mask |= 1LL << sig; |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5979 | , (unsigned) G.root_pid |
| 5980 | , (unsigned) G.root_ppid |
| 5981 | , (unsigned) G.last_bg_pid |
| 5982 | , (unsigned) G.last_exitcode |
| 5983 | , cnt |
| 5984 | , empty_trap_mask |
| 5985 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5986 | ); |
| 5987 | # undef NOMMU_HACK_FMT |
| 5988 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5989 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5990 | */ |
| 5991 | cnt += 6; |
| 5992 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5993 | if (!cur->flg_export || cur->flg_read_only) |
| 5994 | cnt += 2; |
| 5995 | } |
| 5996 | # if ENABLE_HUSH_FUNCTIONS |
| 5997 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5998 | cnt += 3; |
| 5999 | # endif |
| 6000 | pp = g_argv; |
| 6001 | while (*pp++) |
| 6002 | cnt++; |
| 6003 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6004 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6005 | *pp++ = param_buf; |
| 6006 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6007 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6008 | continue; |
| 6009 | if (cur->flg_read_only) { |
| 6010 | *pp++ = (char *) "-R"; |
| 6011 | *pp++ = cur->varstr; |
| 6012 | } else if (!cur->flg_export) { |
| 6013 | *pp++ = (char *) "-V"; |
| 6014 | *pp++ = cur->varstr; |
| 6015 | } |
| 6016 | } |
| 6017 | # if ENABLE_HUSH_FUNCTIONS |
| 6018 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6019 | *pp++ = (char *) "-F"; |
| 6020 | *pp++ = funcp->name; |
| 6021 | *pp++ = funcp->body_as_string; |
| 6022 | } |
| 6023 | # endif |
| 6024 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6025 | * |
| 6026 | * However, POSIX says that subshells reset signals with traps |
| 6027 | * to SIG_DFL. |
| 6028 | * I tested bash-3.2 and it not only does that with true subshells |
| 6029 | * of the form ( list ), but with any forked children shells. |
| 6030 | * I set trap "echo W" WINCH; and then tried: |
| 6031 | * |
| 6032 | * { echo 1; sleep 20; echo 2; } & |
| 6033 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6034 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6035 | * |
| 6036 | * In all these cases sending SIGWINCH to the child shell |
| 6037 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6038 | * _inside_ group (just before echo 1), it works. |
| 6039 | * |
| 6040 | * 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] | 6041 | */ |
| 6042 | *pp++ = (char *) "-c"; |
| 6043 | *pp++ = (char *) s; |
| 6044 | if (builtin_argv) { |
| 6045 | while (*++builtin_argv) |
| 6046 | *pp++ = *builtin_argv; |
| 6047 | *pp++ = (char *) ""; |
| 6048 | } |
| 6049 | *pp++ = g_argv0; |
| 6050 | while (*g_argv) |
| 6051 | *pp++ = *g_argv++; |
| 6052 | /* *pp = NULL; - is already there */ |
| 6053 | pp = environ; |
| 6054 | |
| 6055 | do_exec: |
| 6056 | 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] | 6057 | /* Don't propagate SIG_IGN to the child */ |
| 6058 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6059 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6060 | execve(bb_busybox_exec_path, argv, pp); |
| 6061 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6062 | if (argv[0][0] == '/') |
| 6063 | execve(argv[0], argv, pp); |
| 6064 | xfunc_error_retval = 127; |
| 6065 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6066 | } |
| 6067 | #endif /* !BB_MMU */ |
| 6068 | |
| 6069 | |
| 6070 | static int run_and_free_list(struct pipe *pi); |
| 6071 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6072 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6073 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6074 | * end_trigger controls how often we stop parsing |
| 6075 | * NUL: parse all, execute, return |
| 6076 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6077 | */ |
| 6078 | 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] | 6079 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6080 | /* Why we need empty flag? |
| 6081 | * An obscure corner case "false; ``; echo $?": |
| 6082 | * empty command in `` should still set $? to 0. |
| 6083 | * But we can't just set $? to 0 at the start, |
| 6084 | * this breaks "false; echo `echo $?`" case. |
| 6085 | */ |
| 6086 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6087 | while (1) { |
| 6088 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6089 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6090 | #if ENABLE_HUSH_INTERACTIVE |
| 6091 | if (end_trigger == ';') |
| 6092 | inp->promptmode = 0; /* PS1 */ |
| 6093 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6094 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6095 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6096 | /* If we are in "big" script |
| 6097 | * (not in `cmd` or something similar)... |
| 6098 | */ |
| 6099 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6100 | /* Discard cached input (rest of line) */ |
| 6101 | int ch = inp->last_char; |
| 6102 | while (ch != EOF && ch != '\n') { |
| 6103 | //bb_error_msg("Discarded:'%c'", ch); |
| 6104 | ch = i_getch(inp); |
| 6105 | } |
| 6106 | /* Force prompt */ |
| 6107 | inp->p = NULL; |
| 6108 | /* This stream isn't empty */ |
| 6109 | empty = 0; |
| 6110 | continue; |
| 6111 | } |
| 6112 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6113 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6114 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6115 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6116 | debug_print_tree(pipe_list, 0); |
| 6117 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6118 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6119 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6120 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6121 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6122 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6123 | } |
| 6124 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6125 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6126 | { |
| 6127 | struct in_str input; |
| 6128 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6129 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6130 | } |
| 6131 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6132 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6133 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6134 | struct in_str input; |
| 6135 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6136 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6137 | } |
| 6138 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6139 | #if ENABLE_HUSH_TICK |
| 6140 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6141 | { |
| 6142 | pid_t pid; |
| 6143 | int channel[2]; |
| 6144 | # if !BB_MMU |
| 6145 | char **to_free = NULL; |
| 6146 | # endif |
| 6147 | |
| 6148 | xpipe(channel); |
| 6149 | pid = BB_MMU ? xfork() : xvfork(); |
| 6150 | if (pid == 0) { /* child */ |
| 6151 | disable_restore_tty_pgrp_on_exit(); |
| 6152 | /* Process substitution is not considered to be usual |
| 6153 | * 'command execution'. |
| 6154 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6155 | */ |
| 6156 | bb_signals(0 |
| 6157 | + (1 << SIGTSTP) |
| 6158 | + (1 << SIGTTIN) |
| 6159 | + (1 << SIGTTOU) |
| 6160 | , SIG_IGN); |
| 6161 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6162 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6163 | xmove_fd(channel[1], 1); |
| 6164 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6165 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 6166 | /* Awful hack for `trap` or $(trap). |
| 6167 | * |
| 6168 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6169 | * contains an example where "trap" is executed in a subshell: |
| 6170 | * |
| 6171 | * save_traps=$(trap) |
| 6172 | * ... |
| 6173 | * eval "$save_traps" |
| 6174 | * |
| 6175 | * Standard does not say that "trap" in subshell shall print |
| 6176 | * parent shell's traps. It only says that its output |
| 6177 | * must have suitable form, but then, in the above example |
| 6178 | * (which is not supposed to be normative), it implies that. |
| 6179 | * |
| 6180 | * bash (and probably other shell) does implement it |
| 6181 | * (traps are reset to defaults, but "trap" still shows them), |
| 6182 | * but as a result, "trap" logic is hopelessly messed up: |
| 6183 | * |
| 6184 | * # trap |
| 6185 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6186 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6187 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6188 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6189 | * trap -- 'echo Ho' SIGWINCH |
| 6190 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6191 | * trap -- 'echo Ho' SIGWINCH |
| 6192 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6193 | * trap -- 'echo Ho' SIGWINCH |
| 6194 | * |
| 6195 | * The rules when to forget and when to not forget traps |
| 6196 | * get really complex and nonsensical. |
| 6197 | * |
| 6198 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6199 | */ |
| 6200 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6201 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6202 | && skip_whitespace(s + 4)[0] == '\0' |
| 6203 | ) { |
| 6204 | static const char *const argv[] = { NULL, NULL }; |
| 6205 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6206 | fflush_all(); /* important */ |
| 6207 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6208 | } |
| 6209 | # if BB_MMU |
| 6210 | reset_traps_to_defaults(); |
| 6211 | parse_and_run_string(s); |
| 6212 | _exit(G.last_exitcode); |
| 6213 | # else |
| 6214 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6215 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6216 | * huge=`cat BIG` # was blocking here forever |
| 6217 | * echo OK |
| 6218 | */ |
| 6219 | re_execute_shell(&to_free, |
| 6220 | s, |
| 6221 | G.global_argv[0], |
| 6222 | G.global_argv + 1, |
| 6223 | NULL); |
| 6224 | # endif |
| 6225 | } |
| 6226 | |
| 6227 | /* parent */ |
| 6228 | *pid_p = pid; |
| 6229 | # if ENABLE_HUSH_FAST |
| 6230 | G.count_SIGCHLD++; |
| 6231 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6232 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6233 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6234 | # endif |
| 6235 | enable_restore_tty_pgrp_on_exit(); |
| 6236 | # if !BB_MMU |
| 6237 | free(to_free); |
| 6238 | # endif |
| 6239 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6240 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | /* Return code is exit status of the process that is run. */ |
| 6244 | static int process_command_subs(o_string *dest, const char *s) |
| 6245 | { |
| 6246 | FILE *fp; |
| 6247 | struct in_str pipe_str; |
| 6248 | pid_t pid; |
| 6249 | int status, ch, eol_cnt; |
| 6250 | |
| 6251 | fp = generate_stream_from_string(s, &pid); |
| 6252 | |
| 6253 | /* Now send results of command back into original context */ |
| 6254 | setup_file_in_str(&pipe_str, fp); |
| 6255 | eol_cnt = 0; |
| 6256 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 6257 | if (ch == '\n') { |
| 6258 | eol_cnt++; |
| 6259 | continue; |
| 6260 | } |
| 6261 | while (eol_cnt) { |
| 6262 | o_addchr(dest, '\n'); |
| 6263 | eol_cnt--; |
| 6264 | } |
| 6265 | o_addQchr(dest, ch); |
| 6266 | } |
| 6267 | |
| 6268 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6269 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6270 | /* We need to extract exitcode. Test case |
| 6271 | * "true; echo `sleep 1; false` $?" |
| 6272 | * should print 1 */ |
| 6273 | safe_waitpid(pid, &status, 0); |
| 6274 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6275 | return WEXITSTATUS(status); |
| 6276 | } |
| 6277 | #endif /* ENABLE_HUSH_TICK */ |
| 6278 | |
| 6279 | |
| 6280 | static void setup_heredoc(struct redir_struct *redir) |
| 6281 | { |
| 6282 | struct fd_pair pair; |
| 6283 | pid_t pid; |
| 6284 | int len, written; |
| 6285 | /* the _body_ of heredoc (misleading field name) */ |
| 6286 | const char *heredoc = redir->rd_filename; |
| 6287 | char *expanded; |
| 6288 | #if !BB_MMU |
| 6289 | char **to_free; |
| 6290 | #endif |
| 6291 | |
| 6292 | expanded = NULL; |
| 6293 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6294 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6295 | if (expanded) |
| 6296 | heredoc = expanded; |
| 6297 | } |
| 6298 | len = strlen(heredoc); |
| 6299 | |
| 6300 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6301 | xpiped_pair(pair); |
| 6302 | xmove_fd(pair.rd, redir->rd_fd); |
| 6303 | |
| 6304 | /* Try writing without forking. Newer kernels have |
| 6305 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6306 | ndelay_on(pair.wr); |
| 6307 | while (1) { |
| 6308 | written = write(pair.wr, heredoc, len); |
| 6309 | if (written <= 0) |
| 6310 | break; |
| 6311 | len -= written; |
| 6312 | if (len == 0) { |
| 6313 | close(pair.wr); |
| 6314 | free(expanded); |
| 6315 | return; |
| 6316 | } |
| 6317 | heredoc += written; |
| 6318 | } |
| 6319 | ndelay_off(pair.wr); |
| 6320 | |
| 6321 | /* Okay, pipe buffer was not big enough */ |
| 6322 | /* Note: we must not create a stray child (bastard? :) |
| 6323 | * for the unsuspecting parent process. Child creates a grandchild |
| 6324 | * and exits before parent execs the process which consumes heredoc |
| 6325 | * (that exec happens after we return from this function) */ |
| 6326 | #if !BB_MMU |
| 6327 | to_free = NULL; |
| 6328 | #endif |
| 6329 | pid = xvfork(); |
| 6330 | if (pid == 0) { |
| 6331 | /* child */ |
| 6332 | disable_restore_tty_pgrp_on_exit(); |
| 6333 | pid = BB_MMU ? xfork() : xvfork(); |
| 6334 | if (pid != 0) |
| 6335 | _exit(0); |
| 6336 | /* grandchild */ |
| 6337 | close(redir->rd_fd); /* read side of the pipe */ |
| 6338 | #if BB_MMU |
| 6339 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6340 | _exit(0); |
| 6341 | #else |
| 6342 | /* Delegate blocking writes to another process */ |
| 6343 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6344 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6345 | #endif |
| 6346 | } |
| 6347 | /* parent */ |
| 6348 | #if ENABLE_HUSH_FAST |
| 6349 | G.count_SIGCHLD++; |
| 6350 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6351 | #endif |
| 6352 | enable_restore_tty_pgrp_on_exit(); |
| 6353 | #if !BB_MMU |
| 6354 | free(to_free); |
| 6355 | #endif |
| 6356 | close(pair.wr); |
| 6357 | free(expanded); |
| 6358 | wait(NULL); /* wait till child has died */ |
| 6359 | } |
| 6360 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6361 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 6362 | * Move all conflicting internally used fds, |
| 6363 | * and remember them so that we can restore them later. |
| 6364 | */ |
| 6365 | static int save_fds_on_redirect(int fd, int squirrel[3]) |
| 6366 | { |
| 6367 | if (squirrel) { |
| 6368 | /* Handle redirects of fds 0,1,2 */ |
| 6369 | |
| 6370 | /* If we collide with an already moved stdio fd... */ |
| 6371 | if (fd == squirrel[0]) { |
| 6372 | squirrel[0] = xdup_and_close(squirrel[0], F_DUPFD); |
| 6373 | return 1; |
| 6374 | } |
| 6375 | if (fd == squirrel[1]) { |
| 6376 | squirrel[1] = xdup_and_close(squirrel[1], F_DUPFD); |
| 6377 | return 1; |
| 6378 | } |
| 6379 | if (fd == squirrel[2]) { |
| 6380 | squirrel[2] = xdup_and_close(squirrel[2], F_DUPFD); |
| 6381 | return 1; |
| 6382 | } |
| 6383 | /* If we are about to redirect stdio fd, and did not yet move it... */ |
| 6384 | if (fd <= 2 && squirrel[fd] < 0) { |
| 6385 | /* We avoid taking stdio fds */ |
| 6386 | squirrel[fd] = fcntl(fd, F_DUPFD, 10); |
| 6387 | if (squirrel[fd] < 0 && errno != EBADF) |
| 6388 | xfunc_die(); |
| 6389 | return 0; /* "we did not close fd" */ |
| 6390 | } |
| 6391 | } |
| 6392 | |
| 6393 | #if ENABLE_HUSH_INTERACTIVE |
| 6394 | if (fd != 0 && fd == G.interactive_fd) { |
| 6395 | G.interactive_fd = xdup_and_close(G.interactive_fd, F_DUPFD_CLOEXEC); |
| 6396 | return 1; |
| 6397 | } |
| 6398 | #endif |
| 6399 | |
| 6400 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 6401 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 6402 | * we aren't going to use them anymore, ok to trash. |
| 6403 | * (2) "exec 3>FILE". Bummer. We can save FILEs' fds, |
| 6404 | * but how are we doing to use them? |
| 6405 | * "fileno(fd) = new_fd" can't be done. |
| 6406 | */ |
| 6407 | if (!squirrel) |
| 6408 | return 0; |
| 6409 | |
| 6410 | return save_FILEs_on_redirect(fd); |
| 6411 | } |
| 6412 | |
| 6413 | static void restore_redirects(int squirrel[3]) |
| 6414 | { |
| 6415 | int i, fd; |
| 6416 | for (i = 0; i <= 2; i++) { |
| 6417 | fd = squirrel[i]; |
| 6418 | if (fd != -1) { |
| 6419 | /* We simply die on error */ |
| 6420 | xmove_fd(fd, i); |
| 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | /* Moved G.interactive_fd stays on new fd, not doing anything for it */ |
| 6425 | |
| 6426 | restore_redirected_FILEs(); |
| 6427 | } |
| 6428 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6429 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6430 | * and stderr if they are redirected. */ |
| 6431 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 6432 | { |
| 6433 | int openfd, mode; |
| 6434 | struct redir_struct *redir; |
| 6435 | |
| 6436 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 6437 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6438 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6439 | save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6440 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6441 | * of the heredoc */ |
| 6442 | debug_printf_parse("set heredoc '%s'\n", |
| 6443 | redir->rd_filename); |
| 6444 | setup_heredoc(redir); |
| 6445 | continue; |
| 6446 | } |
| 6447 | |
| 6448 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6449 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6450 | char *p; |
| 6451 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 6452 | /* |
| 6453 | * Examples: |
| 6454 | * "cmd >" (no filename) |
| 6455 | * "cmd > <file" (2nd redirect starts too early) |
| 6456 | */ |
| 6457 | die_if_script("syntax error: %s", "invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6458 | continue; |
| 6459 | } |
| 6460 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6461 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6462 | openfd = open_or_warn(p, mode); |
| 6463 | free(p); |
| 6464 | if (openfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6465 | /* Error message from open_or_warn can be lost |
| 6466 | * if stderr has been redirected, but bash |
| 6467 | * and ash both lose it as well |
| 6468 | * (though zsh doesn't!) |
| 6469 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6470 | return 1; |
| 6471 | } |
| 6472 | } else { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6473 | /* "rd_fd<*>rd_dup" or "rd_fd<*>-" cases */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6474 | openfd = redir->rd_dup; |
| 6475 | } |
| 6476 | |
| 6477 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6478 | int closed = save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6479 | if (openfd == REDIRFD_CLOSE) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6480 | /* "rd_fd >&-" means "close me" */ |
| 6481 | if (!closed) { |
| 6482 | /* ^^^ optimization: saving may already |
| 6483 | * have closed it. If not... */ |
| 6484 | close(redir->rd_fd); |
| 6485 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6486 | } else { |
| 6487 | xdup2(openfd, redir->rd_fd); |
| 6488 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6489 | /* "rd_fd > FILE" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6490 | close(openfd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6491 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6492 | } |
| 6493 | } |
| 6494 | } |
| 6495 | return 0; |
| 6496 | } |
| 6497 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6498 | static char *find_in_path(const char *arg) |
| 6499 | { |
| 6500 | char *ret = NULL; |
| 6501 | const char *PATH = get_local_var_value("PATH"); |
| 6502 | |
| 6503 | if (!PATH) |
| 6504 | return NULL; |
| 6505 | |
| 6506 | while (1) { |
| 6507 | const char *end = strchrnul(PATH, ':'); |
| 6508 | int sz = end - PATH; /* must be int! */ |
| 6509 | |
| 6510 | free(ret); |
| 6511 | if (sz != 0) { |
| 6512 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6513 | } else { |
| 6514 | /* We have xxx::yyyy in $PATH, |
| 6515 | * it means "use current dir" */ |
| 6516 | ret = xstrdup(arg); |
| 6517 | } |
| 6518 | if (access(ret, F_OK) == 0) |
| 6519 | break; |
| 6520 | |
| 6521 | if (*end == '\0') { |
| 6522 | free(ret); |
| 6523 | return NULL; |
| 6524 | } |
| 6525 | PATH = end + 1; |
| 6526 | } |
| 6527 | |
| 6528 | return ret; |
| 6529 | } |
| 6530 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6531 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6532 | const struct built_in_command *x, |
| 6533 | const struct built_in_command *end) |
| 6534 | { |
| 6535 | while (x != end) { |
| 6536 | if (strcmp(name, x->b_cmd) != 0) { |
| 6537 | x++; |
| 6538 | continue; |
| 6539 | } |
| 6540 | debug_printf_exec("found builtin '%s'\n", name); |
| 6541 | return x; |
| 6542 | } |
| 6543 | return NULL; |
| 6544 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6545 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6546 | { |
| 6547 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 6548 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6549 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6550 | { |
| 6551 | const struct built_in_command *x = find_builtin1(name); |
| 6552 | if (x) |
| 6553 | return x; |
| 6554 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 6555 | } |
| 6556 | |
| 6557 | #if ENABLE_HUSH_FUNCTIONS |
| 6558 | static struct function **find_function_slot(const char *name) |
| 6559 | { |
| 6560 | struct function **funcpp = &G.top_func; |
| 6561 | while (*funcpp) { |
| 6562 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 6563 | break; |
| 6564 | } |
| 6565 | funcpp = &(*funcpp)->next; |
| 6566 | } |
| 6567 | return funcpp; |
| 6568 | } |
| 6569 | |
| 6570 | static const struct function *find_function(const char *name) |
| 6571 | { |
| 6572 | const struct function *funcp = *find_function_slot(name); |
| 6573 | if (funcp) |
| 6574 | debug_printf_exec("found function '%s'\n", name); |
| 6575 | return funcp; |
| 6576 | } |
| 6577 | |
| 6578 | /* Note: takes ownership on name ptr */ |
| 6579 | static struct function *new_function(char *name) |
| 6580 | { |
| 6581 | struct function **funcpp = find_function_slot(name); |
| 6582 | struct function *funcp = *funcpp; |
| 6583 | |
| 6584 | if (funcp != NULL) { |
| 6585 | struct command *cmd = funcp->parent_cmd; |
| 6586 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 6587 | if (!cmd) { |
| 6588 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 6589 | free(funcp->name); |
| 6590 | /* Note: if !funcp->body, do not free body_as_string! |
| 6591 | * This is a special case of "-F name body" function: |
| 6592 | * body_as_string was not malloced! */ |
| 6593 | if (funcp->body) { |
| 6594 | free_pipe_list(funcp->body); |
| 6595 | # if !BB_MMU |
| 6596 | free(funcp->body_as_string); |
| 6597 | # endif |
| 6598 | } |
| 6599 | } else { |
| 6600 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 6601 | cmd->argv[0] = funcp->name; |
| 6602 | cmd->group = funcp->body; |
| 6603 | # if !BB_MMU |
| 6604 | cmd->group_as_string = funcp->body_as_string; |
| 6605 | # endif |
| 6606 | } |
| 6607 | } else { |
| 6608 | debug_printf_exec("remembering new function '%s'\n", name); |
| 6609 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 6610 | /*funcp->next = NULL;*/ |
| 6611 | } |
| 6612 | |
| 6613 | funcp->name = name; |
| 6614 | return funcp; |
| 6615 | } |
| 6616 | |
| 6617 | static void unset_func(const char *name) |
| 6618 | { |
| 6619 | struct function **funcpp = find_function_slot(name); |
| 6620 | struct function *funcp = *funcpp; |
| 6621 | |
| 6622 | if (funcp != NULL) { |
| 6623 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 6624 | *funcpp = funcp->next; |
| 6625 | /* funcp is unlinked now, deleting it. |
| 6626 | * Note: if !funcp->body, the function was created by |
| 6627 | * "-F name body", do not free ->body_as_string |
| 6628 | * and ->name as they were not malloced. */ |
| 6629 | if (funcp->body) { |
| 6630 | free_pipe_list(funcp->body); |
| 6631 | free(funcp->name); |
| 6632 | # if !BB_MMU |
| 6633 | free(funcp->body_as_string); |
| 6634 | # endif |
| 6635 | } |
| 6636 | free(funcp); |
| 6637 | } |
| 6638 | } |
| 6639 | |
| 6640 | # if BB_MMU |
| 6641 | #define exec_function(to_free, funcp, argv) \ |
| 6642 | exec_function(funcp, argv) |
| 6643 | # endif |
| 6644 | static void exec_function(char ***to_free, |
| 6645 | const struct function *funcp, |
| 6646 | char **argv) NORETURN; |
| 6647 | static void exec_function(char ***to_free, |
| 6648 | const struct function *funcp, |
| 6649 | char **argv) |
| 6650 | { |
| 6651 | # if BB_MMU |
| 6652 | int n = 1; |
| 6653 | |
| 6654 | argv[0] = G.global_argv[0]; |
| 6655 | G.global_argv = argv; |
| 6656 | while (*++argv) |
| 6657 | n++; |
| 6658 | G.global_argc = n; |
| 6659 | /* On MMU, funcp->body is always non-NULL */ |
| 6660 | n = run_list(funcp->body); |
| 6661 | fflush_all(); |
| 6662 | _exit(n); |
| 6663 | # else |
| 6664 | re_execute_shell(to_free, |
| 6665 | funcp->body_as_string, |
| 6666 | G.global_argv[0], |
| 6667 | argv + 1, |
| 6668 | NULL); |
| 6669 | # endif |
| 6670 | } |
| 6671 | |
| 6672 | static int run_function(const struct function *funcp, char **argv) |
| 6673 | { |
| 6674 | int rc; |
| 6675 | save_arg_t sv; |
| 6676 | smallint sv_flg; |
| 6677 | |
| 6678 | save_and_replace_G_args(&sv, argv); |
| 6679 | |
| 6680 | /* "we are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6681 | sv_flg = G_flag_return_in_progress; |
| 6682 | G_flag_return_in_progress = -1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6683 | # if ENABLE_HUSH_LOCAL |
| 6684 | G.func_nest_level++; |
| 6685 | # endif |
| 6686 | |
| 6687 | /* On MMU, funcp->body is always non-NULL */ |
| 6688 | # if !BB_MMU |
| 6689 | if (!funcp->body) { |
| 6690 | /* Function defined by -F */ |
| 6691 | parse_and_run_string(funcp->body_as_string); |
| 6692 | rc = G.last_exitcode; |
| 6693 | } else |
| 6694 | # endif |
| 6695 | { |
| 6696 | rc = run_list(funcp->body); |
| 6697 | } |
| 6698 | |
| 6699 | # if ENABLE_HUSH_LOCAL |
| 6700 | { |
| 6701 | struct variable *var; |
| 6702 | struct variable **var_pp; |
| 6703 | |
| 6704 | var_pp = &G.top_var; |
| 6705 | while ((var = *var_pp) != NULL) { |
| 6706 | if (var->func_nest_level < G.func_nest_level) { |
| 6707 | var_pp = &var->next; |
| 6708 | continue; |
| 6709 | } |
| 6710 | /* Unexport */ |
| 6711 | if (var->flg_export) |
| 6712 | bb_unsetenv(var->varstr); |
| 6713 | /* Remove from global list */ |
| 6714 | *var_pp = var->next; |
| 6715 | /* Free */ |
| 6716 | if (!var->max_len) |
| 6717 | free(var->varstr); |
| 6718 | free(var); |
| 6719 | } |
| 6720 | G.func_nest_level--; |
| 6721 | } |
| 6722 | # endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6723 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6724 | |
| 6725 | restore_G_args(&sv, argv); |
| 6726 | |
| 6727 | return rc; |
| 6728 | } |
| 6729 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6730 | |
| 6731 | |
| 6732 | #if BB_MMU |
| 6733 | #define exec_builtin(to_free, x, argv) \ |
| 6734 | exec_builtin(x, argv) |
| 6735 | #else |
| 6736 | #define exec_builtin(to_free, x, argv) \ |
| 6737 | exec_builtin(to_free, argv) |
| 6738 | #endif |
| 6739 | static void exec_builtin(char ***to_free, |
| 6740 | const struct built_in_command *x, |
| 6741 | char **argv) NORETURN; |
| 6742 | static void exec_builtin(char ***to_free, |
| 6743 | const struct built_in_command *x, |
| 6744 | char **argv) |
| 6745 | { |
| 6746 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6747 | int rcode; |
| 6748 | fflush_all(); |
| 6749 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6750 | fflush_all(); |
| 6751 | _exit(rcode); |
| 6752 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6753 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6754 | /* On NOMMU, we must never block! |
| 6755 | * Example: { sleep 99 | read line; } & echo Ok |
| 6756 | */ |
| 6757 | re_execute_shell(to_free, |
| 6758 | argv[0], |
| 6759 | G.global_argv[0], |
| 6760 | G.global_argv + 1, |
| 6761 | argv); |
| 6762 | #endif |
| 6763 | } |
| 6764 | |
| 6765 | |
| 6766 | static void execvp_or_die(char **argv) NORETURN; |
| 6767 | static void execvp_or_die(char **argv) |
| 6768 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6769 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6770 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6771 | /* Don't propagate SIG_IGN to the child */ |
| 6772 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6773 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6774 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6775 | e = 2; |
| 6776 | if (errno == EACCES) e = 126; |
| 6777 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6778 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6779 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | #if ENABLE_HUSH_MODE_X |
| 6783 | static void dump_cmd_in_x_mode(char **argv) |
| 6784 | { |
| 6785 | if (G_x_mode && argv) { |
| 6786 | /* We want to output the line in one write op */ |
| 6787 | char *buf, *p; |
| 6788 | int len; |
| 6789 | int n; |
| 6790 | |
| 6791 | len = 3; |
| 6792 | n = 0; |
| 6793 | while (argv[n]) |
| 6794 | len += strlen(argv[n++]) + 1; |
| 6795 | buf = xmalloc(len); |
| 6796 | buf[0] = '+'; |
| 6797 | p = buf + 1; |
| 6798 | n = 0; |
| 6799 | while (argv[n]) |
| 6800 | p += sprintf(p, " %s", argv[n++]); |
| 6801 | *p++ = '\n'; |
| 6802 | *p = '\0'; |
| 6803 | fputs(buf, stderr); |
| 6804 | free(buf); |
| 6805 | } |
| 6806 | } |
| 6807 | #else |
| 6808 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6809 | #endif |
| 6810 | |
| 6811 | #if BB_MMU |
| 6812 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6813 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6814 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6815 | pseudo_exec(command, argv_expanded) |
| 6816 | #endif |
| 6817 | |
| 6818 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6819 | * Never returns. |
| 6820 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6821 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6822 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6823 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6824 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6825 | char **argv, int assignment_cnt, |
| 6826 | char **argv_expanded) NORETURN; |
| 6827 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6828 | char **argv, int assignment_cnt, |
| 6829 | char **argv_expanded) |
| 6830 | { |
| 6831 | char **new_env; |
| 6832 | |
| 6833 | new_env = expand_assignments(argv, assignment_cnt); |
| 6834 | dump_cmd_in_x_mode(new_env); |
| 6835 | |
| 6836 | if (!argv[assignment_cnt]) { |
| 6837 | /* Case when we are here: ... | var=val | ... |
| 6838 | * (note that we do not exit early, i.e., do not optimize out |
| 6839 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6840 | */ |
| 6841 | free_strings(new_env); |
| 6842 | _exit(EXIT_SUCCESS); |
| 6843 | } |
| 6844 | |
| 6845 | #if BB_MMU |
| 6846 | set_vars_and_save_old(new_env); |
| 6847 | free(new_env); /* optional */ |
| 6848 | /* we can also destroy set_vars_and_save_old's return value, |
| 6849 | * to save memory */ |
| 6850 | #else |
| 6851 | nommu_save->new_env = new_env; |
| 6852 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6853 | #endif |
| 6854 | |
| 6855 | if (argv_expanded) { |
| 6856 | argv = argv_expanded; |
| 6857 | } else { |
| 6858 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6859 | #if !BB_MMU |
| 6860 | nommu_save->argv = argv; |
| 6861 | #endif |
| 6862 | } |
| 6863 | dump_cmd_in_x_mode(argv); |
| 6864 | |
| 6865 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6866 | if (strchr(argv[0], '/') != NULL) |
| 6867 | goto skip; |
| 6868 | #endif |
| 6869 | |
| 6870 | /* Check if the command matches any of the builtins. |
| 6871 | * Depending on context, this might be redundant. But it's |
| 6872 | * easier to waste a few CPU cycles than it is to figure out |
| 6873 | * if this is one of those cases. |
| 6874 | */ |
| 6875 | { |
| 6876 | /* On NOMMU, it is more expensive to re-execute shell |
| 6877 | * just in order to run echo or test builtin. |
| 6878 | * It's better to skip it here and run corresponding |
| 6879 | * non-builtin later. */ |
| 6880 | const struct built_in_command *x; |
| 6881 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6882 | if (x) { |
| 6883 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6884 | } |
| 6885 | } |
| 6886 | #if ENABLE_HUSH_FUNCTIONS |
| 6887 | /* Check if the command matches any functions */ |
| 6888 | { |
| 6889 | const struct function *funcp = find_function(argv[0]); |
| 6890 | if (funcp) { |
| 6891 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6892 | } |
| 6893 | } |
| 6894 | #endif |
| 6895 | |
| 6896 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6897 | /* Check if the command matches any busybox applets */ |
| 6898 | { |
| 6899 | int a = find_applet_by_name(argv[0]); |
| 6900 | if (a >= 0) { |
| 6901 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6902 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6903 | /* Do not leak open fds from opened script files etc */ |
| 6904 | close_all_FILE_list(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6905 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6906 | run_applet_no_and_exit(a, argv); |
| 6907 | } |
| 6908 | # endif |
| 6909 | /* Re-exec ourselves */ |
| 6910 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6911 | /* Don't propagate SIG_IGN to the child */ |
| 6912 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6913 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6914 | execv(bb_busybox_exec_path, argv); |
| 6915 | /* If they called chroot or otherwise made the binary no longer |
| 6916 | * executable, fall through */ |
| 6917 | } |
| 6918 | } |
| 6919 | #endif |
| 6920 | |
| 6921 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6922 | skip: |
| 6923 | #endif |
| 6924 | execvp_or_die(argv); |
| 6925 | } |
| 6926 | |
| 6927 | /* Called after [v]fork() in run_pipe |
| 6928 | */ |
| 6929 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6930 | struct command *command, |
| 6931 | char **argv_expanded) NORETURN; |
| 6932 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6933 | struct command *command, |
| 6934 | char **argv_expanded) |
| 6935 | { |
| 6936 | if (command->argv) { |
| 6937 | pseudo_exec_argv(nommu_save, command->argv, |
| 6938 | command->assignment_cnt, argv_expanded); |
| 6939 | } |
| 6940 | |
| 6941 | if (command->group) { |
| 6942 | /* Cases when we are here: |
| 6943 | * ( list ) |
| 6944 | * { list } & |
| 6945 | * ... | ( list ) | ... |
| 6946 | * ... | { list } | ... |
| 6947 | */ |
| 6948 | #if BB_MMU |
| 6949 | int rcode; |
| 6950 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6951 | reset_traps_to_defaults(); |
| 6952 | rcode = run_list(command->group); |
| 6953 | /* OK to leak memory by not calling free_pipe_list, |
| 6954 | * since this process is about to exit */ |
| 6955 | _exit(rcode); |
| 6956 | #else |
| 6957 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6958 | command->group_as_string, |
| 6959 | G.global_argv[0], |
| 6960 | G.global_argv + 1, |
| 6961 | NULL); |
| 6962 | #endif |
| 6963 | } |
| 6964 | |
| 6965 | /* Case when we are here: ... | >file */ |
| 6966 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6967 | _exit(EXIT_SUCCESS); |
| 6968 | } |
| 6969 | |
| 6970 | #if ENABLE_HUSH_JOB |
| 6971 | static const char *get_cmdtext(struct pipe *pi) |
| 6972 | { |
| 6973 | char **argv; |
| 6974 | char *p; |
| 6975 | int len; |
| 6976 | |
| 6977 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6978 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6979 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6980 | if (pi->cmdtext) |
| 6981 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 6982 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6983 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 6984 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6985 | pi->cmdtext = xzalloc(1); |
| 6986 | return pi->cmdtext; |
| 6987 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6988 | len = 0; |
| 6989 | do { |
| 6990 | len += strlen(*argv) + 1; |
| 6991 | } while (*++argv); |
| 6992 | p = xmalloc(len); |
| 6993 | pi->cmdtext = p; |
| 6994 | argv = pi->cmds[0].argv; |
| 6995 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 6996 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6997 | *p++ = ' '; |
| 6998 | } while (*++argv); |
| 6999 | p[-1] = '\0'; |
| 7000 | return pi->cmdtext; |
| 7001 | } |
| 7002 | |
| 7003 | static void insert_bg_job(struct pipe *pi) |
| 7004 | { |
| 7005 | struct pipe *job, **jobp; |
| 7006 | int i; |
| 7007 | |
| 7008 | /* Linear search for the ID of the job to use */ |
| 7009 | pi->jobid = 1; |
| 7010 | for (job = G.job_list; job; job = job->next) |
| 7011 | if (job->jobid >= pi->jobid) |
| 7012 | pi->jobid = job->jobid + 1; |
| 7013 | |
| 7014 | /* Add job to the list of running jobs */ |
| 7015 | jobp = &G.job_list; |
| 7016 | while ((job = *jobp) != NULL) |
| 7017 | jobp = &job->next; |
| 7018 | job = *jobp = xmalloc(sizeof(*job)); |
| 7019 | |
| 7020 | *job = *pi; /* physical copy */ |
| 7021 | job->next = NULL; |
| 7022 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7023 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7024 | for (i = 0; i < pi->num_cmds; i++) { |
| 7025 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7026 | /* all other fields are not used and stay zero */ |
| 7027 | } |
| 7028 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7029 | |
| 7030 | if (G_interactive_fd) |
| 7031 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 7032 | G.last_jobid = job->jobid; |
| 7033 | } |
| 7034 | |
| 7035 | static void remove_bg_job(struct pipe *pi) |
| 7036 | { |
| 7037 | struct pipe *prev_pipe; |
| 7038 | |
| 7039 | if (pi == G.job_list) { |
| 7040 | G.job_list = pi->next; |
| 7041 | } else { |
| 7042 | prev_pipe = G.job_list; |
| 7043 | while (prev_pipe->next != pi) |
| 7044 | prev_pipe = prev_pipe->next; |
| 7045 | prev_pipe->next = pi->next; |
| 7046 | } |
| 7047 | if (G.job_list) |
| 7048 | G.last_jobid = G.job_list->jobid; |
| 7049 | else |
| 7050 | G.last_jobid = 0; |
| 7051 | } |
| 7052 | |
| 7053 | /* Remove a backgrounded job */ |
| 7054 | static void delete_finished_bg_job(struct pipe *pi) |
| 7055 | { |
| 7056 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7057 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7058 | } |
| 7059 | #endif /* JOB */ |
| 7060 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7061 | static int job_exited_or_stopped(struct pipe *pi) |
| 7062 | { |
| 7063 | int rcode, i; |
| 7064 | |
| 7065 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7066 | return -1; |
| 7067 | |
| 7068 | /* All processes in fg pipe have exited or stopped */ |
| 7069 | rcode = 0; |
| 7070 | i = pi->num_cmds; |
| 7071 | while (--i >= 0) { |
| 7072 | rcode = pi->cmds[i].cmd_exitcode; |
| 7073 | /* usually last process gives overall exitstatus, |
| 7074 | * but with "set -o pipefail", last *failed* process does */ |
| 7075 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7076 | break; |
| 7077 | } |
| 7078 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7079 | return rcode; |
| 7080 | } |
| 7081 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7082 | static int process_wait_result(struct pipe *fg_pipe, pid_t childpid, int status) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7083 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7084 | #if ENABLE_HUSH_JOB |
| 7085 | struct pipe *pi; |
| 7086 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7087 | int i, dead; |
| 7088 | |
| 7089 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7090 | |
| 7091 | #if DEBUG_JOBS |
| 7092 | if (WIFSTOPPED(status)) |
| 7093 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7094 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7095 | if (WIFSIGNALED(status)) |
| 7096 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7097 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7098 | if (WIFEXITED(status)) |
| 7099 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7100 | childpid, WEXITSTATUS(status)); |
| 7101 | #endif |
| 7102 | /* Were we asked to wait for a fg pipe? */ |
| 7103 | if (fg_pipe) { |
| 7104 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7105 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7106 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7107 | int rcode; |
| 7108 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7109 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7110 | if (fg_pipe->cmds[i].pid != childpid) |
| 7111 | continue; |
| 7112 | if (dead) { |
| 7113 | int ex; |
| 7114 | fg_pipe->cmds[i].pid = 0; |
| 7115 | fg_pipe->alive_cmds--; |
| 7116 | ex = WEXITSTATUS(status); |
| 7117 | /* bash prints killer signal's name for *last* |
| 7118 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 7119 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7120 | */ |
| 7121 | if (WIFSIGNALED(status)) { |
| 7122 | int sig = WTERMSIG(status); |
| 7123 | if (i == fg_pipe->num_cmds-1) |
| 7124 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 7125 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 7126 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 7127 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7128 | * Maybe we need to use sig | 128? */ |
| 7129 | ex = sig + 128; |
| 7130 | } |
| 7131 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 7132 | } else { |
| 7133 | fg_pipe->stopped_cmds++; |
| 7134 | } |
| 7135 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 7136 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7137 | rcode = job_exited_or_stopped(fg_pipe); |
| 7138 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7139 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 7140 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 7141 | * and "killall -STOP cat" */ |
| 7142 | if (G_interactive_fd) { |
| 7143 | #if ENABLE_HUSH_JOB |
| 7144 | if (fg_pipe->alive_cmds != 0) |
| 7145 | insert_bg_job(fg_pipe); |
| 7146 | #endif |
| 7147 | return rcode; |
| 7148 | } |
| 7149 | if (fg_pipe->alive_cmds == 0) |
| 7150 | return rcode; |
| 7151 | } |
| 7152 | /* There are still running processes in the fg_pipe */ |
| 7153 | return -1; |
| 7154 | } |
| 7155 | /* It wasnt in fg_pipe, look for process in bg pipes */ |
| 7156 | } |
| 7157 | |
| 7158 | #if ENABLE_HUSH_JOB |
| 7159 | /* We were asked to wait for bg or orphaned children */ |
| 7160 | /* No need to remember exitcode in this case */ |
| 7161 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7162 | for (i = 0; i < pi->num_cmds; i++) { |
| 7163 | if (pi->cmds[i].pid == childpid) |
| 7164 | goto found_pi_and_prognum; |
| 7165 | } |
| 7166 | } |
| 7167 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 7168 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 7169 | return -1; /* this wasn't a process from fg_pipe */ |
| 7170 | |
| 7171 | found_pi_and_prognum: |
| 7172 | if (dead) { |
| 7173 | /* child exited */ |
| 7174 | pi->cmds[i].pid = 0; |
| 7175 | pi->cmds[i].cmd_exitcode = WEXITSTATUS(status); |
| 7176 | if (WIFSIGNALED(status)) |
| 7177 | pi->cmds[i].cmd_exitcode = 128 + WTERMSIG(status); |
| 7178 | pi->alive_cmds--; |
| 7179 | if (!pi->alive_cmds) { |
| 7180 | if (G_interactive_fd) |
| 7181 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 7182 | "Done", pi->cmdtext); |
| 7183 | delete_finished_bg_job(pi); |
| 7184 | } |
| 7185 | } else { |
| 7186 | /* child stopped */ |
| 7187 | pi->stopped_cmds++; |
| 7188 | } |
| 7189 | #endif |
| 7190 | return -1; /* this wasn't a process from fg_pipe */ |
| 7191 | } |
| 7192 | |
| 7193 | /* Check to see if any processes have exited -- if they have, |
| 7194 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7195 | * |
| 7196 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 7197 | * Return its exitcode or zero if stopped. |
| 7198 | * |
| 7199 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 7200 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 7201 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7202 | * or 0 if no children changed status. |
| 7203 | * |
| 7204 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 7205 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7206 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7207 | */ |
| 7208 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 7209 | { |
| 7210 | int attributes; |
| 7211 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7212 | int rcode = 0; |
| 7213 | |
| 7214 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 7215 | |
| 7216 | attributes = WUNTRACED; |
| 7217 | if (fg_pipe == NULL) |
| 7218 | attributes |= WNOHANG; |
| 7219 | |
| 7220 | errno = 0; |
| 7221 | #if ENABLE_HUSH_FAST |
| 7222 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 7223 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 7224 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 7225 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 7226 | /* Avoid doing waitpid syscall if possible */ |
| 7227 | if (!G.we_have_children) { |
| 7228 | errno = ECHILD; |
| 7229 | return -1; |
| 7230 | } |
| 7231 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 7232 | /* We have children, but they did not exit |
| 7233 | * or stop yet (we saw no SIGCHLD) */ |
| 7234 | return 0; |
| 7235 | } |
| 7236 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 7237 | } |
| 7238 | #endif |
| 7239 | |
| 7240 | /* Do we do this right? |
| 7241 | * bash-3.00# sleep 20 | false |
| 7242 | * <ctrl-Z pressed> |
| 7243 | * [3]+ Stopped sleep 20 | false |
| 7244 | * bash-3.00# echo $? |
| 7245 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 7246 | * [hush 1.14.0: yes we do it right] |
| 7247 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7248 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7249 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7250 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7251 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7252 | i = G.count_SIGCHLD; |
| 7253 | #endif |
| 7254 | childpid = waitpid(-1, &status, attributes); |
| 7255 | if (childpid <= 0) { |
| 7256 | if (childpid && errno != ECHILD) |
| 7257 | bb_perror_msg("waitpid"); |
| 7258 | #if ENABLE_HUSH_FAST |
| 7259 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 7260 | G.we_have_children = (childpid == 0); |
| 7261 | G.handled_SIGCHLD = i; |
| 7262 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7263 | } |
| 7264 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7265 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 7266 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7267 | break; |
| 7268 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7269 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 7270 | if (rcode >= 0) { |
| 7271 | /* fg_pipe exited or stopped */ |
| 7272 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7273 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7274 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7275 | debug_printf_exec("childpid==waitfor_pid:%d status:0x%08x\n", childpid, status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7276 | rcode = WEXITSTATUS(status); |
| 7277 | if (WIFSIGNALED(status)) |
| 7278 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7279 | if (WIFSTOPPED(status)) |
| 7280 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 7281 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7282 | rcode++; |
| 7283 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7284 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7285 | /* This wasn't one of our processes, or */ |
| 7286 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7287 | } /* while (waitpid succeeds)... */ |
| 7288 | |
| 7289 | return rcode; |
| 7290 | } |
| 7291 | |
| 7292 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7293 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7294 | { |
| 7295 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7296 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7297 | if (G_saved_tty_pgrp) { |
| 7298 | /* Job finished, move the shell to the foreground */ |
| 7299 | p = getpgrp(); /* our process group id */ |
| 7300 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 7301 | tcsetpgrp(G_interactive_fd, p); |
| 7302 | } |
| 7303 | return rcode; |
| 7304 | } |
| 7305 | #endif |
| 7306 | |
| 7307 | /* Start all the jobs, but don't wait for anything to finish. |
| 7308 | * See checkjobs(). |
| 7309 | * |
| 7310 | * Return code is normally -1, when the caller has to wait for children |
| 7311 | * to finish to determine the exit status of the pipe. If the pipe |
| 7312 | * is a simple builtin command, however, the action is done by the |
| 7313 | * time run_pipe returns, and the exit code is provided as the |
| 7314 | * return value. |
| 7315 | * |
| 7316 | * Returns -1 only if started some children. IOW: we have to |
| 7317 | * mask out retvals of builtins etc with 0xff! |
| 7318 | * |
| 7319 | * The only case when we do not need to [v]fork is when the pipe |
| 7320 | * is single, non-backgrounded, non-subshell command. Examples: |
| 7321 | * cmd ; ... { list } ; ... |
| 7322 | * cmd && ... { list } && ... |
| 7323 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7324 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7325 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 7326 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 7327 | * |
| 7328 | * Cases when we must fork: |
| 7329 | * non-single: cmd | cmd |
| 7330 | * backgrounded: cmd & { list } & |
| 7331 | * subshell: ( list ) [&] |
| 7332 | */ |
| 7333 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 7334 | #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] | 7335 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 7336 | #endif |
| 7337 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 7338 | struct variable **old_vars_p, |
| 7339 | struct command *command, |
| 7340 | int squirrel[3], |
| 7341 | char **argv_expanded) |
| 7342 | { |
| 7343 | /* setup_redirects acts on file descriptors, not FILEs. |
| 7344 | * This is perfect for work that comes after exec(). |
| 7345 | * Is it really safe for inline use? Experimentally, |
| 7346 | * things seem to work. */ |
| 7347 | int rcode = setup_redirects(command, squirrel); |
| 7348 | if (rcode == 0) { |
| 7349 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 7350 | *new_env_p = new_env; |
| 7351 | dump_cmd_in_x_mode(new_env); |
| 7352 | dump_cmd_in_x_mode(argv_expanded); |
| 7353 | if (old_vars_p) |
| 7354 | *old_vars_p = set_vars_and_save_old(new_env); |
| 7355 | } |
| 7356 | return rcode; |
| 7357 | } |
| 7358 | static NOINLINE int run_pipe(struct pipe *pi) |
| 7359 | { |
| 7360 | static const char *const null_ptr = NULL; |
| 7361 | |
| 7362 | int cmd_no; |
| 7363 | int next_infd; |
| 7364 | struct command *command; |
| 7365 | char **argv_expanded; |
| 7366 | char **argv; |
| 7367 | /* it is not always needed, but we aim to smaller code */ |
| 7368 | int squirrel[] = { -1, -1, -1 }; |
| 7369 | int rcode; |
| 7370 | |
| 7371 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 7372 | debug_enter(); |
| 7373 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7374 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 7375 | * Result should be 3 lines: q w e, qwe, q w e |
| 7376 | */ |
| 7377 | G.ifs = get_local_var_value("IFS"); |
| 7378 | if (!G.ifs) |
| 7379 | G.ifs = defifs; |
| 7380 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7381 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 7382 | pi->stopped_cmds = 0; |
| 7383 | command = &pi->cmds[0]; |
| 7384 | argv_expanded = NULL; |
| 7385 | |
| 7386 | if (pi->num_cmds != 1 |
| 7387 | || pi->followup == PIPE_BG |
| 7388 | || command->cmd_type == CMD_SUBSHELL |
| 7389 | ) { |
| 7390 | goto must_fork; |
| 7391 | } |
| 7392 | |
| 7393 | pi->alive_cmds = 1; |
| 7394 | |
| 7395 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 7396 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 7397 | |
| 7398 | if (command->group) { |
| 7399 | #if ENABLE_HUSH_FUNCTIONS |
| 7400 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7401 | /* "executing" func () { list } */ |
| 7402 | struct function *funcp; |
| 7403 | |
| 7404 | funcp = new_function(command->argv[0]); |
| 7405 | /* funcp->name is already set to argv[0] */ |
| 7406 | funcp->body = command->group; |
| 7407 | # if !BB_MMU |
| 7408 | funcp->body_as_string = command->group_as_string; |
| 7409 | command->group_as_string = NULL; |
| 7410 | # endif |
| 7411 | command->group = NULL; |
| 7412 | command->argv[0] = NULL; |
| 7413 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 7414 | funcp->parent_cmd = command; |
| 7415 | command->child_func = funcp; |
| 7416 | |
| 7417 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 7418 | debug_leave(); |
| 7419 | return EXIT_SUCCESS; |
| 7420 | } |
| 7421 | #endif |
| 7422 | /* { list } */ |
| 7423 | debug_printf("non-subshell group\n"); |
| 7424 | rcode = 1; /* exitcode if redir failed */ |
| 7425 | if (setup_redirects(command, squirrel) == 0) { |
| 7426 | debug_printf_exec(": run_list\n"); |
| 7427 | rcode = run_list(command->group) & 0xff; |
| 7428 | } |
| 7429 | restore_redirects(squirrel); |
| 7430 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7431 | debug_leave(); |
| 7432 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7433 | return rcode; |
| 7434 | } |
| 7435 | |
| 7436 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 7437 | { |
| 7438 | const struct built_in_command *x; |
| 7439 | #if ENABLE_HUSH_FUNCTIONS |
| 7440 | const struct function *funcp; |
| 7441 | #else |
| 7442 | enum { funcp = 0 }; |
| 7443 | #endif |
| 7444 | char **new_env = NULL; |
| 7445 | struct variable *old_vars = NULL; |
| 7446 | |
| 7447 | if (argv[command->assignment_cnt] == NULL) { |
| 7448 | /* Assignments, but no command */ |
| 7449 | /* Ensure redirects take effect (that is, create files). |
| 7450 | * Try "a=t >file" */ |
| 7451 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 7452 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 7453 | /* Set shell variables */ |
| 7454 | if (new_env) { |
| 7455 | argv = new_env; |
| 7456 | while (*argv) { |
| 7457 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7458 | /* Do we need to flag set_local_var() errors? |
| 7459 | * "assignment to readonly var" and "putenv error" |
| 7460 | */ |
| 7461 | argv++; |
| 7462 | } |
| 7463 | } |
| 7464 | /* Redirect error sets $? to 1. Otherwise, |
| 7465 | * if evaluating assignment value set $?, retain it. |
| 7466 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7467 | if (rcode == 0) |
| 7468 | rcode = G.last_exitcode; |
| 7469 | /* Exit, _skipping_ variable restoring code: */ |
| 7470 | goto clean_up_and_ret0; |
| 7471 | |
| 7472 | #else /* Older, bigger, but more correct code */ |
| 7473 | |
| 7474 | rcode = setup_redirects(command, squirrel); |
| 7475 | restore_redirects(squirrel); |
| 7476 | /* Set shell variables */ |
| 7477 | if (G_x_mode) |
| 7478 | bb_putchar_stderr('+'); |
| 7479 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7480 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7481 | if (G_x_mode) |
| 7482 | fprintf(stderr, " %s", p); |
| 7483 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 7484 | *argv, p); |
| 7485 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7486 | /* Do we need to flag set_local_var() errors? |
| 7487 | * "assignment to readonly var" and "putenv error" |
| 7488 | */ |
| 7489 | argv++; |
| 7490 | } |
| 7491 | if (G_x_mode) |
| 7492 | bb_putchar_stderr('\n'); |
| 7493 | /* Redirect error sets $? to 1. Otherwise, |
| 7494 | * if evaluating assignment value set $?, retain it. |
| 7495 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7496 | if (rcode == 0) |
| 7497 | rcode = G.last_exitcode; |
| 7498 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7499 | debug_leave(); |
| 7500 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7501 | return rcode; |
| 7502 | #endif |
| 7503 | } |
| 7504 | |
| 7505 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7506 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7507 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7508 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7509 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7510 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7511 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7512 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 7513 | } |
| 7514 | |
| 7515 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 7516 | if (!argv_expanded[0]) { |
| 7517 | free(argv_expanded); |
| 7518 | debug_leave(); |
| 7519 | return G.last_exitcode; |
| 7520 | } |
| 7521 | |
| 7522 | x = find_builtin(argv_expanded[0]); |
| 7523 | #if ENABLE_HUSH_FUNCTIONS |
| 7524 | funcp = NULL; |
| 7525 | if (!x) |
| 7526 | funcp = find_function(argv_expanded[0]); |
| 7527 | #endif |
| 7528 | if (x || funcp) { |
| 7529 | if (!funcp) { |
| 7530 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 7531 | debug_printf("exec with redirects only\n"); |
| 7532 | rcode = setup_redirects(command, NULL); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7533 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7534 | goto clean_up_and_ret1; |
| 7535 | } |
| 7536 | } |
| 7537 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7538 | if (rcode == 0) { |
| 7539 | if (!funcp) { |
| 7540 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 7541 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7542 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7543 | rcode = x->b_function(argv_expanded) & 0xff; |
| 7544 | fflush_all(); |
| 7545 | } |
| 7546 | #if ENABLE_HUSH_FUNCTIONS |
| 7547 | else { |
| 7548 | # if ENABLE_HUSH_LOCAL |
| 7549 | struct variable **sv; |
| 7550 | sv = G.shadowed_vars_pp; |
| 7551 | G.shadowed_vars_pp = &old_vars; |
| 7552 | # endif |
| 7553 | debug_printf_exec(": function '%s' '%s'...\n", |
| 7554 | funcp->name, argv_expanded[1]); |
| 7555 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 7556 | # if ENABLE_HUSH_LOCAL |
| 7557 | G.shadowed_vars_pp = sv; |
| 7558 | # endif |
| 7559 | } |
| 7560 | #endif |
| 7561 | } |
| 7562 | clean_up_and_ret: |
| 7563 | unset_vars(new_env); |
| 7564 | add_vars(old_vars); |
| 7565 | /* clean_up_and_ret0: */ |
| 7566 | restore_redirects(squirrel); |
| 7567 | clean_up_and_ret1: |
| 7568 | free(argv_expanded); |
| 7569 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7570 | debug_leave(); |
| 7571 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 7572 | return rcode; |
| 7573 | } |
| 7574 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7575 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7576 | int n = find_applet_by_name(argv_expanded[0]); |
| 7577 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 7578 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7579 | if (rcode == 0) { |
| 7580 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 7581 | argv_expanded[0], argv_expanded[1]); |
| 7582 | rcode = run_nofork_applet(n, argv_expanded); |
| 7583 | } |
| 7584 | goto clean_up_and_ret; |
| 7585 | } |
| 7586 | } |
| 7587 | /* It is neither builtin nor applet. We must fork. */ |
| 7588 | } |
| 7589 | |
| 7590 | must_fork: |
| 7591 | /* NB: argv_expanded may already be created, and that |
| 7592 | * might include `cmd` runs! Do not rerun it! We *must* |
| 7593 | * use argv_expanded if it's non-NULL */ |
| 7594 | |
| 7595 | /* Going to fork a child per each pipe member */ |
| 7596 | pi->alive_cmds = 0; |
| 7597 | next_infd = 0; |
| 7598 | |
| 7599 | cmd_no = 0; |
| 7600 | while (cmd_no < pi->num_cmds) { |
| 7601 | struct fd_pair pipefds; |
| 7602 | #if !BB_MMU |
| 7603 | volatile nommu_save_t nommu_save; |
| 7604 | nommu_save.new_env = NULL; |
| 7605 | nommu_save.old_vars = NULL; |
| 7606 | nommu_save.argv = NULL; |
| 7607 | nommu_save.argv_from_re_execing = NULL; |
| 7608 | #endif |
| 7609 | command = &pi->cmds[cmd_no]; |
| 7610 | cmd_no++; |
| 7611 | if (command->argv) { |
| 7612 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 7613 | command->argv[0], command->argv[1]); |
| 7614 | } else { |
| 7615 | debug_printf_exec(": pipe member with no argv\n"); |
| 7616 | } |
| 7617 | |
| 7618 | /* pipes are inserted between pairs of commands */ |
| 7619 | pipefds.rd = 0; |
| 7620 | pipefds.wr = 1; |
| 7621 | if (cmd_no < pi->num_cmds) |
| 7622 | xpiped_pair(pipefds); |
| 7623 | |
| 7624 | command->pid = BB_MMU ? fork() : vfork(); |
| 7625 | if (!command->pid) { /* child */ |
| 7626 | #if ENABLE_HUSH_JOB |
| 7627 | disable_restore_tty_pgrp_on_exit(); |
| 7628 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7629 | |
| 7630 | /* Every child adds itself to new process group |
| 7631 | * with pgid == pid_of_first_child_in_pipe */ |
| 7632 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7633 | pid_t pgrp; |
| 7634 | pgrp = pi->pgrp; |
| 7635 | if (pgrp < 0) /* true for 1st process only */ |
| 7636 | pgrp = getpid(); |
| 7637 | if (setpgid(0, pgrp) == 0 |
| 7638 | && pi->followup != PIPE_BG |
| 7639 | && G_saved_tty_pgrp /* we have ctty */ |
| 7640 | ) { |
| 7641 | /* We do it in *every* child, not just first, |
| 7642 | * to avoid races */ |
| 7643 | tcsetpgrp(G_interactive_fd, pgrp); |
| 7644 | } |
| 7645 | } |
| 7646 | #endif |
| 7647 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 7648 | /* 1st cmd in backgrounded pipe |
| 7649 | * should have its stdin /dev/null'ed */ |
| 7650 | close(0); |
| 7651 | if (open(bb_dev_null, O_RDONLY)) |
| 7652 | xopen("/", O_RDONLY); |
| 7653 | } else { |
| 7654 | xmove_fd(next_infd, 0); |
| 7655 | } |
| 7656 | xmove_fd(pipefds.wr, 1); |
| 7657 | if (pipefds.rd > 1) |
| 7658 | close(pipefds.rd); |
| 7659 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7660 | * and the pipe fd (fd#1) is available for dup'ing: |
| 7661 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 7662 | * of cmd1 goes into pipe. |
| 7663 | */ |
| 7664 | if (setup_redirects(command, NULL)) { |
| 7665 | /* Happens when redir file can't be opened: |
| 7666 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 7667 | * FOO |
| 7668 | * hush: can't open '/qwe/rty': No such file or directory |
| 7669 | * BAZ |
| 7670 | * (echo BAR is not executed, it hits _exit(1) below) |
| 7671 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7672 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7673 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7674 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7675 | /* Stores to nommu_save list of env vars putenv'ed |
| 7676 | * (NOMMU, on MMU we don't need that) */ |
| 7677 | /* cast away volatility... */ |
| 7678 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 7679 | /* pseudo_exec() does not return */ |
| 7680 | } |
| 7681 | |
| 7682 | /* parent or error */ |
| 7683 | #if ENABLE_HUSH_FAST |
| 7684 | G.count_SIGCHLD++; |
| 7685 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7686 | #endif |
| 7687 | enable_restore_tty_pgrp_on_exit(); |
| 7688 | #if !BB_MMU |
| 7689 | /* Clean up after vforked child */ |
| 7690 | free(nommu_save.argv); |
| 7691 | free(nommu_save.argv_from_re_execing); |
| 7692 | unset_vars(nommu_save.new_env); |
| 7693 | add_vars(nommu_save.old_vars); |
| 7694 | #endif |
| 7695 | free(argv_expanded); |
| 7696 | argv_expanded = NULL; |
| 7697 | if (command->pid < 0) { /* [v]fork failed */ |
| 7698 | /* Clearly indicate, was it fork or vfork */ |
| 7699 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 7700 | } else { |
| 7701 | pi->alive_cmds++; |
| 7702 | #if ENABLE_HUSH_JOB |
| 7703 | /* Second and next children need to know pid of first one */ |
| 7704 | if (pi->pgrp < 0) |
| 7705 | pi->pgrp = command->pid; |
| 7706 | #endif |
| 7707 | } |
| 7708 | |
| 7709 | if (cmd_no > 1) |
| 7710 | close(next_infd); |
| 7711 | if (cmd_no < pi->num_cmds) |
| 7712 | close(pipefds.wr); |
| 7713 | /* Pass read (output) pipe end to next iteration */ |
| 7714 | next_infd = pipefds.rd; |
| 7715 | } |
| 7716 | |
| 7717 | if (!pi->alive_cmds) { |
| 7718 | debug_leave(); |
| 7719 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7720 | return 1; |
| 7721 | } |
| 7722 | |
| 7723 | debug_leave(); |
| 7724 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7725 | return -1; |
| 7726 | } |
| 7727 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7728 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7729 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7730 | static int run_list(struct pipe *pi) |
| 7731 | { |
| 7732 | #if ENABLE_HUSH_CASE |
| 7733 | char *case_word = NULL; |
| 7734 | #endif |
| 7735 | #if ENABLE_HUSH_LOOPS |
| 7736 | struct pipe *loop_top = NULL; |
| 7737 | char **for_lcur = NULL; |
| 7738 | char **for_list = NULL; |
| 7739 | #endif |
| 7740 | smallint last_followup; |
| 7741 | smalluint rcode; |
| 7742 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7743 | smalluint cond_code = 0; |
| 7744 | #else |
| 7745 | enum { cond_code = 0 }; |
| 7746 | #endif |
| 7747 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7748 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7749 | smallint last_rword; /* ditto */ |
| 7750 | #endif |
| 7751 | |
| 7752 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7753 | debug_enter(); |
| 7754 | |
| 7755 | #if ENABLE_HUSH_LOOPS |
| 7756 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7757 | { |
| 7758 | struct pipe *cpipe; |
| 7759 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7760 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7761 | continue; |
| 7762 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7763 | if (cpipe->next == NULL) { |
| 7764 | syntax_error("malformed for"); |
| 7765 | debug_leave(); |
| 7766 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7767 | return 1; |
| 7768 | } |
| 7769 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7770 | if (cpipe->next->res_word == RES_DO) |
| 7771 | continue; |
| 7772 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7773 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7774 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7775 | ) { |
| 7776 | syntax_error("malformed for"); |
| 7777 | debug_leave(); |
| 7778 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7779 | return 1; |
| 7780 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7781 | } |
| 7782 | } |
| 7783 | #endif |
| 7784 | |
| 7785 | /* Past this point, all code paths should jump to ret: label |
| 7786 | * in order to return, no direct "return" statements please. |
| 7787 | * This helps to ensure that no memory is leaked. */ |
| 7788 | |
| 7789 | #if ENABLE_HUSH_JOB |
| 7790 | G.run_list_level++; |
| 7791 | #endif |
| 7792 | |
| 7793 | #if HAS_KEYWORDS |
| 7794 | rword = RES_NONE; |
| 7795 | last_rword = RES_XXXX; |
| 7796 | #endif |
| 7797 | last_followup = PIPE_SEQ; |
| 7798 | rcode = G.last_exitcode; |
| 7799 | |
| 7800 | /* Go through list of pipes, (maybe) executing them. */ |
| 7801 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7802 | int r; |
| 7803 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7804 | if (G.flag_SIGINT) |
| 7805 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7806 | if (G_flag_return_in_progress == 1) |
| 7807 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7808 | |
| 7809 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7810 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7811 | rword, cond_code, last_rword); |
| 7812 | #if ENABLE_HUSH_LOOPS |
| 7813 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7814 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7815 | ) { |
| 7816 | /* start of a loop: remember where loop starts */ |
| 7817 | loop_top = pi; |
| 7818 | G.depth_of_loop++; |
| 7819 | } |
| 7820 | #endif |
| 7821 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7822 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7823 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7824 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7825 | ) { |
| 7826 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7827 | * and we should not execute CMD */ |
| 7828 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7829 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7830 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7831 | } |
| 7832 | } |
| 7833 | last_followup = pi->followup; |
| 7834 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7835 | #if ENABLE_HUSH_IF |
| 7836 | if (cond_code) { |
| 7837 | if (rword == RES_THEN) { |
| 7838 | /* if false; then ... fi has exitcode 0! */ |
| 7839 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7840 | /* "if <false> THEN cmd": skip cmd */ |
| 7841 | continue; |
| 7842 | } |
| 7843 | } else { |
| 7844 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7845 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7846 | * skip cmd and all following ones */ |
| 7847 | break; |
| 7848 | } |
| 7849 | } |
| 7850 | #endif |
| 7851 | #if ENABLE_HUSH_LOOPS |
| 7852 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7853 | if (!for_lcur) { |
| 7854 | /* first loop through for */ |
| 7855 | |
| 7856 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7857 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7858 | }; /* encoded representation of "$@" */ |
| 7859 | static const char *const encoded_dollar_at_argv[] = { |
| 7860 | encoded_dollar_at, NULL |
| 7861 | }; /* argv list with one element: "$@" */ |
| 7862 | char **vals; |
| 7863 | |
| 7864 | vals = (char**)encoded_dollar_at_argv; |
| 7865 | if (pi->next->res_word == RES_IN) { |
| 7866 | /* if no variable values after "in" we skip "for" */ |
| 7867 | if (!pi->next->cmds[0].argv) { |
| 7868 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7869 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7870 | break; |
| 7871 | } |
| 7872 | vals = pi->next->cmds[0].argv; |
| 7873 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7874 | /* create list of variable values */ |
| 7875 | debug_print_strings("for_list made from", vals); |
| 7876 | for_list = expand_strvec_to_strvec(vals); |
| 7877 | for_lcur = for_list; |
| 7878 | debug_print_strings("for_list", for_list); |
| 7879 | } |
| 7880 | if (!*for_lcur) { |
| 7881 | /* "for" loop is over, clean up */ |
| 7882 | free(for_list); |
| 7883 | for_list = NULL; |
| 7884 | for_lcur = NULL; |
| 7885 | break; |
| 7886 | } |
| 7887 | /* Insert next value from for_lcur */ |
| 7888 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7889 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7890 | continue; |
| 7891 | } |
| 7892 | if (rword == RES_IN) { |
| 7893 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7894 | } |
| 7895 | if (rword == RES_DONE) { |
| 7896 | continue; /* "done" has no cmds too */ |
| 7897 | } |
| 7898 | #endif |
| 7899 | #if ENABLE_HUSH_CASE |
| 7900 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7901 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7902 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7903 | continue; |
| 7904 | } |
| 7905 | if (rword == RES_MATCH) { |
| 7906 | char **argv; |
| 7907 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7908 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7909 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7910 | break; |
| 7911 | /* all prev words didn't match, does this one match? */ |
| 7912 | argv = pi->cmds->argv; |
| 7913 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7914 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7915 | /* TODO: which FNM_xxx flags to use? */ |
| 7916 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7917 | free(pattern); |
| 7918 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7919 | free(case_word); |
| 7920 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7921 | break; |
| 7922 | } |
| 7923 | argv++; |
| 7924 | } |
| 7925 | continue; |
| 7926 | } |
| 7927 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7928 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7929 | if (cond_code != 0) |
| 7930 | continue; /* not matched yet, skip this pipe */ |
| 7931 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7932 | if (rword == RES_ESAC) { |
| 7933 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 7934 | if (case_word) { |
| 7935 | /* "case" did not match anything: still set $? (to 0) */ |
| 7936 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7937 | } |
| 7938 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7939 | #endif |
| 7940 | /* Just pressing <enter> in shell should check for jobs. |
| 7941 | * OTOH, in non-interactive shell this is useless |
| 7942 | * and only leads to extra job checks */ |
| 7943 | if (pi->num_cmds == 0) { |
| 7944 | if (G_interactive_fd) |
| 7945 | goto check_jobs_and_continue; |
| 7946 | continue; |
| 7947 | } |
| 7948 | |
| 7949 | /* After analyzing all keywords and conditions, we decided |
| 7950 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7951 | * after run_pipe to collect any background children, |
| 7952 | * even if list execution is to be stopped. */ |
| 7953 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7954 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7955 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7956 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7957 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 7958 | if (r != -1) { |
| 7959 | /* We ran a builtin, function, or group. |
| 7960 | * rcode is already known |
| 7961 | * and we don't need to wait for anything. */ |
| 7962 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 7963 | G.last_exitcode = rcode; |
| 7964 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7965 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7966 | /* Was it "break" or "continue"? */ |
| 7967 | if (G.flag_break_continue) { |
| 7968 | smallint fbc = G.flag_break_continue; |
| 7969 | /* We might fall into outer *loop*, |
| 7970 | * don't want to break it too */ |
| 7971 | if (loop_top) { |
| 7972 | G.depth_break_continue--; |
| 7973 | if (G.depth_break_continue == 0) |
| 7974 | G.flag_break_continue = 0; |
| 7975 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7976 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 7977 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7978 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7979 | break; |
| 7980 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7981 | /* "continue": simulate end of loop */ |
| 7982 | rword = RES_DONE; |
| 7983 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7984 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 7985 | #endif |
| 7986 | if (G_flag_return_in_progress == 1) { |
| 7987 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 7988 | break; |
| 7989 | } |
| 7990 | } else if (pi->followup == PIPE_BG) { |
| 7991 | /* What does bash do with attempts to background builtins? */ |
| 7992 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7993 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7994 | * I'm NOT treating inner &'s as jobs */ |
| 7995 | #if ENABLE_HUSH_JOB |
| 7996 | if (G.run_list_level == 1) |
| 7997 | insert_bg_job(pi); |
| 7998 | #endif |
| 7999 | /* Last command's pid goes to $! */ |
| 8000 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 8001 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 8002 | /* Check pi->pi_inverted? "! sleep 1 & echo $?": bash says 1. dash and ash says 0 */ |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8003 | rcode = EXIT_SUCCESS; |
| 8004 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8005 | } else { |
| 8006 | #if ENABLE_HUSH_JOB |
| 8007 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8008 | /* Waits for completion, then fg's main shell */ |
| 8009 | rcode = checkjobs_and_fg_shell(pi); |
| 8010 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8011 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8012 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8013 | #endif |
| 8014 | /* This one just waits for completion */ |
| 8015 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8016 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8017 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8018 | G.last_exitcode = rcode; |
| 8019 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8020 | } |
| 8021 | |
| 8022 | /* Analyze how result affects subsequent commands */ |
| 8023 | #if ENABLE_HUSH_IF |
| 8024 | if (rword == RES_IF || rword == RES_ELIF) |
| 8025 | cond_code = rcode; |
| 8026 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8027 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8028 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8029 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8030 | #if ENABLE_HUSH_LOOPS |
| 8031 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8032 | if (pi->next |
| 8033 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 8034 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8035 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8036 | if (rword == RES_WHILE) { |
| 8037 | if (rcode) { |
| 8038 | /* "while false; do...done" - exitcode 0 */ |
| 8039 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8040 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8041 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8042 | } |
| 8043 | } |
| 8044 | if (rword == RES_UNTIL) { |
| 8045 | if (!rcode) { |
| 8046 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8047 | break; |
| 8048 | } |
| 8049 | } |
| 8050 | } |
| 8051 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8052 | } /* for (pi) */ |
| 8053 | |
| 8054 | #if ENABLE_HUSH_JOB |
| 8055 | G.run_list_level--; |
| 8056 | #endif |
| 8057 | #if ENABLE_HUSH_LOOPS |
| 8058 | if (loop_top) |
| 8059 | G.depth_of_loop--; |
| 8060 | free(for_list); |
| 8061 | #endif |
| 8062 | #if ENABLE_HUSH_CASE |
| 8063 | free(case_word); |
| 8064 | #endif |
| 8065 | debug_leave(); |
| 8066 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 8067 | return rcode; |
| 8068 | } |
| 8069 | |
| 8070 | /* Select which version we will use */ |
| 8071 | static int run_and_free_list(struct pipe *pi) |
| 8072 | { |
| 8073 | int rcode = 0; |
| 8074 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8075 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8076 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 8077 | rcode = run_list(pi); |
| 8078 | } |
| 8079 | /* free_pipe_list has the side effect of clearing memory. |
| 8080 | * In the long run that function can be merged with run_list, |
| 8081 | * but doing that now would hobble the debugging effort. */ |
| 8082 | free_pipe_list(pi); |
| 8083 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 8084 | return rcode; |
| 8085 | } |
| 8086 | |
| 8087 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8088 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 8089 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8090 | sighandler_t old_handler; |
| 8091 | unsigned sig = 0; |
| 8092 | while ((mask >>= 1) != 0) { |
| 8093 | sig++; |
| 8094 | if (!(mask & 1)) |
| 8095 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8096 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8097 | /* POSIX allows shell to re-enable SIGCHLD |
| 8098 | * even if it was SIG_IGN on entry. |
| 8099 | * Therefore we skip IGN check for it: |
| 8100 | */ |
| 8101 | if (sig == SIGCHLD) |
| 8102 | continue; |
| 8103 | if (old_handler == SIG_IGN) { |
| 8104 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8105 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8106 | if (!G.traps) |
| 8107 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8108 | free(G.traps[sig]); |
| 8109 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 8110 | } |
| 8111 | } |
| 8112 | } |
| 8113 | |
| 8114 | /* Called a few times only (or even once if "sh -c") */ |
| 8115 | static void install_special_sighandlers(void) |
| 8116 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8117 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8118 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8119 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8120 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8121 | if (G_interactive_fd) { |
| 8122 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 8123 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8124 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8125 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8126 | /* Careful, do not re-install handlers we already installed */ |
| 8127 | if (G.special_sig_mask != mask) { |
| 8128 | unsigned diff = mask & ~G.special_sig_mask; |
| 8129 | G.special_sig_mask = mask; |
| 8130 | install_sighandlers(diff); |
| 8131 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | #if ENABLE_HUSH_JOB |
| 8135 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8136 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8137 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8138 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8139 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8140 | |
| 8141 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8142 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8143 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 8144 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8145 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 8146 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8147 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8148 | + (1 << SIGABRT) |
| 8149 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 8150 | + (1 << SIGPIPE) |
| 8151 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8152 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8153 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8154 | * we never want to restore pgrp on exit, and this fn is not called |
| 8155 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8156 | /*+ (1 << SIGHUP )*/ |
| 8157 | /*+ (1 << SIGTERM)*/ |
| 8158 | /*+ (1 << SIGINT )*/ |
| 8159 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8160 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8161 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8162 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8163 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 8164 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 8165 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8166 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8167 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8168 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8169 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8170 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8171 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8172 | break; |
| 8173 | case 'x': |
| 8174 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 8175 | break; |
| 8176 | case 'o': |
| 8177 | if (!o_opt) { |
| 8178 | /* "set -+o" without parameter. |
| 8179 | * in bash, set -o produces this output: |
| 8180 | * pipefail off |
| 8181 | * and set +o: |
| 8182 | * set +o pipefail |
| 8183 | * We always use the second form. |
| 8184 | */ |
| 8185 | const char *p = o_opt_strings; |
| 8186 | idx = 0; |
| 8187 | while (*p) { |
| 8188 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 8189 | idx++; |
| 8190 | p += strlen(p) + 1; |
| 8191 | } |
| 8192 | break; |
| 8193 | } |
| 8194 | idx = index_in_strings(o_opt_strings, o_opt); |
| 8195 | if (idx >= 0) { |
| 8196 | G.o_opt[idx] = state; |
| 8197 | break; |
| 8198 | } |
| 8199 | default: |
| 8200 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8201 | } |
| 8202 | return EXIT_SUCCESS; |
| 8203 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8204 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 8205 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 8206 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8207 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8208 | enum { |
| 8209 | OPT_login = (1 << 0), |
| 8210 | }; |
| 8211 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8212 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8213 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8214 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8215 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8216 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 8217 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 8218 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8219 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8220 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8221 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8222 | #if ENABLE_HUSH_FAST |
| 8223 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 8224 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8225 | #if !BB_MMU |
| 8226 | G.argv0_for_re_execing = argv[0]; |
| 8227 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8228 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8229 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 8230 | shell_ver->flg_export = 1; |
| 8231 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 8232 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 8233 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8234 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8235 | /* Create shell local variables from the values |
| 8236 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 8237 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8238 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8239 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8240 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8241 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8242 | if (e) while (*e) { |
| 8243 | char *value = strchr(*e, '='); |
| 8244 | if (value) { /* paranoia */ |
| 8245 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 8246 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 8247 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8248 | cur_var->max_len = strlen(*e); |
| 8249 | cur_var->flg_export = 1; |
| 8250 | } |
| 8251 | e++; |
| 8252 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8253 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8254 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 8255 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8256 | |
| 8257 | /* Export PWD */ |
| 8258 | set_pwd_var(/*exp:*/ 1); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8259 | |
| 8260 | #if ENABLE_HUSH_BASH_COMPAT |
| 8261 | /* Set (but not export) HOSTNAME unless already set */ |
| 8262 | if (!get_local_var_value("HOSTNAME")) { |
| 8263 | struct utsname uts; |
| 8264 | uname(&uts); |
| 8265 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 8266 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8267 | /* bash also exports SHLVL and _, |
| 8268 | * and sets (but doesn't export) the following variables: |
| 8269 | * BASH=/bin/bash |
| 8270 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 8271 | * BASH_VERSION='3.2.0(1)-release' |
| 8272 | * HOSTTYPE=i386 |
| 8273 | * MACHTYPE=i386-pc-linux-gnu |
| 8274 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8275 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8276 | * EUID=<NNNNN> |
| 8277 | * UID=<NNNNN> |
| 8278 | * GROUPS=() |
| 8279 | * LINES=<NNN> |
| 8280 | * COLUMNS=<NNN> |
| 8281 | * BASH_ARGC=() |
| 8282 | * BASH_ARGV=() |
| 8283 | * BASH_LINENO=() |
| 8284 | * BASH_SOURCE=() |
| 8285 | * DIRSTACK=() |
| 8286 | * PIPESTATUS=([0]="0") |
| 8287 | * HISTFILE=/<xxx>/.bash_history |
| 8288 | * HISTFILESIZE=500 |
| 8289 | * HISTSIZE=500 |
| 8290 | * MAILCHECK=60 |
| 8291 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 8292 | * SHELL=/bin/bash |
| 8293 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 8294 | * TERM=dumb |
| 8295 | * OPTERR=1 |
| 8296 | * OPTIND=1 |
| 8297 | * IFS=$' \t\n' |
| 8298 | * PS1='\s-\v\$ ' |
| 8299 | * PS2='> ' |
| 8300 | * PS4='+ ' |
| 8301 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8302 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8303 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 8304 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 8305 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 8306 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 8307 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 8308 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 8309 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 8310 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8311 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 8312 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8313 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8314 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8315 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8316 | * 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] | 8317 | * in order to intercept (more) signals. |
| 8318 | */ |
| 8319 | |
| 8320 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8321 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8322 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8323 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8324 | while (1) { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8325 | opt = getopt(argc, argv, "+c:xinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8326 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8327 | "<:$:R:V:" |
| 8328 | # if ENABLE_HUSH_FUNCTIONS |
| 8329 | "F:" |
| 8330 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8331 | #endif |
| 8332 | ); |
| 8333 | if (opt <= 0) |
| 8334 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8335 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8336 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8337 | /* Possibilities: |
| 8338 | * sh ... -c 'script' |
| 8339 | * sh ... -c 'script' ARG0 [ARG1...] |
| 8340 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8341 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8342 | * "" needs to be replaced with NULL |
| 8343 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8344 | * Note: the form without ARG0 never happens: |
| 8345 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8346 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8347 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8348 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8349 | G.root_ppid = getppid(); |
| 8350 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8351 | G.global_argv = argv + optind; |
| 8352 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8353 | if (builtin_argc) { |
| 8354 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 8355 | const struct built_in_command *x; |
| 8356 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8357 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8358 | x = find_builtin(optarg); |
| 8359 | if (x) { /* paranoia */ |
| 8360 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 8361 | G.global_argv += builtin_argc; |
| 8362 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8363 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8364 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8365 | } |
| 8366 | goto final_return; |
| 8367 | } |
| 8368 | if (!G.global_argv[0]) { |
| 8369 | /* -c 'script' (no params): prevent empty $0 */ |
| 8370 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 8371 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 8372 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8373 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8374 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8375 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8376 | goto final_return; |
| 8377 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 8378 | /* Well, we cannot just declare interactiveness, |
| 8379 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8380 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8381 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8382 | case 's': |
| 8383 | /* "-s" means "read from stdin", but this is how we always |
| 8384 | * operate, so simply do nothing here. */ |
| 8385 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8386 | case 'l': |
| 8387 | flags |= OPT_login; |
| 8388 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8389 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8390 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 8391 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8392 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8393 | case '$': { |
| 8394 | unsigned long long empty_trap_mask; |
| 8395 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8396 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 8397 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8398 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 8399 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8400 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 8401 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8402 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8403 | optarg++; |
| 8404 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8405 | optarg++; |
| 8406 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 8407 | if (empty_trap_mask != 0) { |
| 8408 | int sig; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8409 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8410 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8411 | for (sig = 1; sig < NSIG; sig++) { |
| 8412 | if (empty_trap_mask & (1LL << sig)) { |
| 8413 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8414 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8415 | } |
| 8416 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8417 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8418 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8419 | optarg++; |
| 8420 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8421 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8422 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8423 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8424 | case 'R': |
| 8425 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8426 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8427 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8428 | # if ENABLE_HUSH_FUNCTIONS |
| 8429 | case 'F': { |
| 8430 | struct function *funcp = new_function(optarg); |
| 8431 | /* funcp->name is already set to optarg */ |
| 8432 | /* funcp->body is set to NULL. It's a special case. */ |
| 8433 | funcp->body_as_string = argv[optind]; |
| 8434 | optind++; |
| 8435 | break; |
| 8436 | } |
| 8437 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8438 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8439 | case 'n': |
| 8440 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8441 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8442 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8443 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8444 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8445 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 8446 | " or: sh -c command [args]...\n\n"); |
| 8447 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8448 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8449 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8450 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8451 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8452 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8453 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8454 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 8455 | G.global_argc = argc - (optind - 1); |
| 8456 | G.global_argv = argv + (optind - 1); |
| 8457 | G.global_argv[0] = argv[0]; |
| 8458 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8459 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8460 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8461 | G.root_ppid = getppid(); |
| 8462 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8463 | |
| 8464 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8465 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8466 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8467 | debug_printf("sourcing /etc/profile\n"); |
| 8468 | input = fopen_for_read("/etc/profile"); |
| 8469 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8470 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8471 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8472 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8473 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8474 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8475 | /* bash: after sourcing /etc/profile, |
| 8476 | * tries to source (in the given order): |
| 8477 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8478 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8479 | * bash also sources ~/.bash_logout on exit. |
| 8480 | * If called as sh, skips .bash_XXX files. |
| 8481 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8482 | } |
| 8483 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8484 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8485 | FILE *input; |
| 8486 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8487 | * "bash <script>" (which is never interactive (unless -i?)) |
| 8488 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8489 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 8490 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8491 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8492 | G.global_argc--; |
| 8493 | G.global_argv++; |
| 8494 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8495 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8496 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8497 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8498 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8499 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8500 | parse_and_run_file(input); |
| 8501 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8502 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8503 | #endif |
| 8504 | goto final_return; |
| 8505 | } |
| 8506 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8507 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8508 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8509 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8510 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8511 | /* A shell is interactive if the '-i' flag was given, |
| 8512 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 8513 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8514 | * no arguments remaining or the -s flag given |
| 8515 | * standard input is a terminal |
| 8516 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8517 | * Refer to Posix.2, the description of the 'sh' utility. |
| 8518 | */ |
| 8519 | #if ENABLE_HUSH_JOB |
| 8520 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8521 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 8522 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 8523 | if (G_saved_tty_pgrp < 0) |
| 8524 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8525 | |
| 8526 | /* try to dup stdin to high fd#, >= 255 */ |
| 8527 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8528 | if (G_interactive_fd < 0) { |
| 8529 | /* try to dup to any fd */ |
| 8530 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8531 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8532 | /* give up */ |
| 8533 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8534 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 8535 | } |
| 8536 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8537 | // TODO: track & disallow any attempts of user |
| 8538 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8539 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8540 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8541 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8542 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8543 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8544 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8545 | /* If we were run as 'hush &', sleep until we are |
| 8546 | * in the foreground (tty pgrp == our pgrp). |
| 8547 | * If we get started under a job aware app (like bash), |
| 8548 | * make sure we are now in charge so we don't fight over |
| 8549 | * who gets the foreground */ |
| 8550 | while (1) { |
| 8551 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8552 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 8553 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8554 | break; |
| 8555 | /* send TTIN to ourself (should stop us) */ |
| 8556 | kill(- shell_pgrp, SIGTTIN); |
| 8557 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8558 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8559 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8560 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8561 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8562 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8563 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8564 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8565 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8566 | /* Put ourselves in our own process group |
| 8567 | * (bash, too, does this only if ctty is available) */ |
| 8568 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 8569 | /* Grab control of the terminal */ |
| 8570 | tcsetpgrp(G_interactive_fd, getpid()); |
| 8571 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 8572 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8573 | |
| 8574 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 8575 | { |
| 8576 | const char *hp = get_local_var_value("HISTFILE"); |
| 8577 | if (!hp) { |
| 8578 | hp = get_local_var_value("HOME"); |
| 8579 | if (hp) |
| 8580 | hp = concat_path_file(hp, ".hush_history"); |
| 8581 | } else { |
| 8582 | hp = xstrdup(hp); |
| 8583 | } |
| 8584 | if (hp) { |
| 8585 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8586 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 8587 | } |
| 8588 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 8589 | hp = get_local_var_value("HISTFILESIZE"); |
| 8590 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 8591 | # endif |
| 8592 | } |
| 8593 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8594 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8595 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8596 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8597 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8598 | /* No job control compiled in, only prompt/line editing */ |
| 8599 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8600 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8601 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8602 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8603 | G_interactive_fd = dup(STDIN_FILENO); |
| 8604 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8605 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8606 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8607 | } |
| 8608 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8609 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8610 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8611 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8612 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8613 | #else |
| 8614 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8615 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8616 | #endif |
| 8617 | /* bash: |
| 8618 | * if interactive but not a login shell, sources ~/.bashrc |
| 8619 | * (--norc turns this off, --rcfile <file> overrides) |
| 8620 | */ |
| 8621 | |
| 8622 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8623 | /* note: ash and hush share this string */ |
| 8624 | printf("\n\n%s %s\n" |
| 8625 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8626 | "\n", |
| 8627 | bb_banner, |
| 8628 | "hush - the humble shell" |
| 8629 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8632 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8633 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8634 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8635 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8636 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 8637 | |
| 8638 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8639 | #if ENABLE_MSH |
| 8640 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 8641 | int msh_main(int argc, char **argv) |
| 8642 | { |
Denys Vlasenko | ed6ff5e | 2016-09-30 12:28:37 +0200 | [diff] [blame] | 8643 | bb_error_msg("msh is deprecated, please use hush instead"); |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8644 | return hush_main(argc, argv); |
| 8645 | } |
| 8646 | #endif |
| 8647 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8648 | |
| 8649 | /* |
| 8650 | * Built-ins |
| 8651 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8652 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8653 | { |
| 8654 | return 0; |
| 8655 | } |
| 8656 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8657 | 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] | 8658 | { |
| 8659 | int argc = 0; |
| 8660 | while (*argv) { |
| 8661 | argc++; |
| 8662 | argv++; |
| 8663 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8664 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | static int FAST_FUNC builtin_test(char **argv) |
| 8668 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8669 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8670 | } |
| 8671 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8672 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8673 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8674 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8675 | } |
| 8676 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8677 | #if ENABLE_PRINTF |
| 8678 | static int FAST_FUNC builtin_printf(char **argv) |
| 8679 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8680 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8681 | } |
| 8682 | #endif |
| 8683 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8684 | static char **skip_dash_dash(char **argv) |
| 8685 | { |
| 8686 | argv++; |
| 8687 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 8688 | argv++; |
| 8689 | return argv; |
| 8690 | } |
| 8691 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8692 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8693 | { |
| 8694 | int rcode = EXIT_SUCCESS; |
| 8695 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8696 | argv = skip_dash_dash(argv); |
| 8697 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8698 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8699 | /* bash: |
| 8700 | * eval "echo Hi; done" ("done" is syntax error): |
| 8701 | * "echo Hi" will not execute too. |
| 8702 | */ |
| 8703 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8704 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8705 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8706 | } |
| 8707 | return rcode; |
| 8708 | } |
| 8709 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8710 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8711 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8712 | const char *newdir; |
| 8713 | |
| 8714 | argv = skip_dash_dash(argv); |
| 8715 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8716 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8717 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8718 | * bash says "bash: cd: HOME not set" and does nothing |
| 8719 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8720 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 8721 | const char *home = get_local_var_value("HOME"); |
| 8722 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8723 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8724 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8725 | /* Mimic bash message exactly */ |
| 8726 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8727 | return EXIT_FAILURE; |
| 8728 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8729 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 8730 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 8731 | * set it again, but do not export. bash does the same. |
| 8732 | */ |
| 8733 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8734 | return EXIT_SUCCESS; |
| 8735 | } |
| 8736 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8737 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8738 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8739 | argv = skip_dash_dash(argv); |
| 8740 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8741 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8742 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8743 | /* Careful: we can end up here after [v]fork. Do not restore |
| 8744 | * tty pgrp then, only top-level shell process does that */ |
| 8745 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 8746 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 8747 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8748 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8749 | * 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] | 8750 | * and tcsetpgrp, and this is inherently racy. |
| 8751 | */ |
| 8752 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8753 | } |
| 8754 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8755 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8756 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8757 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8758 | |
| 8759 | /* interactive bash: |
| 8760 | * # trap "echo EEE" EXIT |
| 8761 | * # exit |
| 8762 | * exit |
| 8763 | * There are stopped jobs. |
| 8764 | * (if there are _stopped_ jobs, running ones don't count) |
| 8765 | * # exit |
| 8766 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 8767 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8768 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8769 | * 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] | 8770 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8771 | |
| 8772 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8773 | argv = skip_dash_dash(argv); |
| 8774 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8775 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8776 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8777 | xfunc_error_retval = 255; |
| 8778 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8779 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8780 | } |
| 8781 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8782 | static void print_escaped(const char *s) |
| 8783 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8784 | if (*s == '\'') |
| 8785 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8786 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8787 | const char *p = strchrnul(s, '\''); |
| 8788 | /* print 'xxxx', possibly just '' */ |
| 8789 | printf("'%.*s'", (int)(p - s), s); |
| 8790 | if (*p == '\0') |
| 8791 | break; |
| 8792 | s = p; |
| 8793 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8794 | /* s points to '; print "'''...'''" */ |
| 8795 | putchar('"'); |
| 8796 | do putchar('\''); while (*++s == '\''); |
| 8797 | putchar('"'); |
| 8798 | } while (*s); |
| 8799 | } |
| 8800 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8801 | #if !ENABLE_HUSH_LOCAL |
| 8802 | #define helper_export_local(argv, exp, lvl) \ |
| 8803 | helper_export_local(argv, exp) |
| 8804 | #endif |
| 8805 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8806 | { |
| 8807 | do { |
| 8808 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8809 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8810 | |
| 8811 | /* So far we do not check that name is valid (TODO?) */ |
| 8812 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8813 | if (*name_end == '\0') { |
| 8814 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8815 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8816 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8817 | var = vpp ? *vpp : NULL; |
| 8818 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8819 | if (exp == -1) { /* unexporting? */ |
| 8820 | /* export -n NAME (without =VALUE) */ |
| 8821 | if (var) { |
| 8822 | var->flg_export = 0; |
| 8823 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8824 | unsetenv(name); |
| 8825 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8826 | continue; |
| 8827 | } |
| 8828 | if (exp == 1) { /* exporting? */ |
| 8829 | /* export NAME (without =VALUE) */ |
| 8830 | if (var) { |
| 8831 | var->flg_export = 1; |
| 8832 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8833 | putenv(var->varstr); |
| 8834 | continue; |
| 8835 | } |
| 8836 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8837 | #if ENABLE_HUSH_LOCAL |
| 8838 | if (exp == 0 /* local? */ |
| 8839 | && var && var->func_nest_level == lvl |
| 8840 | ) { |
| 8841 | /* "local x=abc; ...; local x" - ignore second local decl */ |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8842 | continue; |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8843 | } |
| 8844 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8845 | /* Exporting non-existing variable. |
| 8846 | * bash does not put it in environment, |
| 8847 | * but remembers that it is exported, |
| 8848 | * and does put it in env when it is set later. |
| 8849 | * We just set it to "" and export. */ |
| 8850 | /* Or, it's "local NAME" (without =VALUE). |
| 8851 | * bash sets the value to "". */ |
| 8852 | name = xasprintf("%s=", name); |
| 8853 | } else { |
| 8854 | /* (Un)exporting/making local NAME=VALUE */ |
| 8855 | name = xstrdup(name); |
| 8856 | } |
| 8857 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8858 | } while (*++argv); |
| 8859 | } |
| 8860 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8861 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8862 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8863 | unsigned opt_unexport; |
| 8864 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8865 | #if ENABLE_HUSH_EXPORT_N |
| 8866 | /* "!": do not abort on errors */ |
| 8867 | opt_unexport = getopt32(argv, "!n"); |
| 8868 | if (opt_unexport == (uint32_t)-1) |
| 8869 | return EXIT_FAILURE; |
| 8870 | argv += optind; |
| 8871 | #else |
| 8872 | opt_unexport = 0; |
| 8873 | argv++; |
| 8874 | #endif |
| 8875 | |
| 8876 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8877 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8878 | if (e) { |
| 8879 | while (*e) { |
| 8880 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8881 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8882 | #else |
| 8883 | /* ash emits: export VAR='VAL' |
| 8884 | * bash: declare -x VAR="VAL" |
| 8885 | * we follow ash example */ |
| 8886 | const char *s = *e++; |
| 8887 | const char *p = strchr(s, '='); |
| 8888 | |
| 8889 | if (!p) /* wtf? take next variable */ |
| 8890 | continue; |
| 8891 | /* export var= */ |
| 8892 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8893 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8894 | putchar('\n'); |
| 8895 | #endif |
| 8896 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8897 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8898 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8899 | return EXIT_SUCCESS; |
| 8900 | } |
| 8901 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8902 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8903 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8904 | return EXIT_SUCCESS; |
| 8905 | } |
| 8906 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8907 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8908 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8909 | { |
| 8910 | if (G.func_nest_level == 0) { |
| 8911 | bb_error_msg("%s: not in a function", argv[0]); |
| 8912 | return EXIT_FAILURE; /* bash compat */ |
| 8913 | } |
| 8914 | helper_export_local(argv, 0, G.func_nest_level); |
| 8915 | return EXIT_SUCCESS; |
| 8916 | } |
| 8917 | #endif |
| 8918 | |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8919 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 8920 | static int FAST_FUNC builtin_unset(char **argv) |
| 8921 | { |
| 8922 | int ret; |
| 8923 | unsigned opts; |
| 8924 | |
| 8925 | /* "!": do not abort on errors */ |
| 8926 | /* "+": stop at 1st non-option */ |
| 8927 | opts = getopt32(argv, "!+vf"); |
| 8928 | if (opts == (unsigned)-1) |
| 8929 | return EXIT_FAILURE; |
| 8930 | if (opts == 3) { |
| 8931 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8932 | return EXIT_FAILURE; |
| 8933 | } |
| 8934 | argv += optind; |
| 8935 | |
| 8936 | ret = EXIT_SUCCESS; |
| 8937 | while (*argv) { |
| 8938 | if (!(opts & 2)) { /* not -f */ |
| 8939 | if (unset_local_var(*argv)) { |
| 8940 | /* unset <nonexistent_var> doesn't fail. |
| 8941 | * Error is when one tries to unset RO var. |
| 8942 | * Message was printed by unset_local_var. */ |
| 8943 | ret = EXIT_FAILURE; |
| 8944 | } |
| 8945 | } |
| 8946 | #if ENABLE_HUSH_FUNCTIONS |
| 8947 | else { |
| 8948 | unset_func(*argv); |
| 8949 | } |
| 8950 | #endif |
| 8951 | argv++; |
| 8952 | } |
| 8953 | return ret; |
| 8954 | } |
| 8955 | |
| 8956 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8957 | * built-in 'set' handler |
| 8958 | * SUSv3 says: |
| 8959 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8960 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 8961 | * set -- [argument...] |
| 8962 | * set -o |
| 8963 | * set +o |
| 8964 | * Implementations shall support the options in both their hyphen and |
| 8965 | * plus-sign forms. These options can also be specified as options to sh. |
| 8966 | * Examples: |
| 8967 | * Write out all variables and their values: set |
| 8968 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8969 | * Turn on the -x and -v options: set -xv |
| 8970 | * Unset all positional parameters: set -- |
| 8971 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8972 | * Set the positional parameters to the expansion of x, even if x expands |
| 8973 | * with a leading '-' or '+': set -- $x |
| 8974 | * |
| 8975 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 8976 | */ |
| 8977 | static int FAST_FUNC builtin_set(char **argv) |
| 8978 | { |
| 8979 | int n; |
| 8980 | char **pp, **g_argv; |
| 8981 | char *arg = *++argv; |
| 8982 | |
| 8983 | if (arg == NULL) { |
| 8984 | struct variable *e; |
| 8985 | for (e = G.top_var; e; e = e->next) |
| 8986 | puts(e->varstr); |
| 8987 | return EXIT_SUCCESS; |
| 8988 | } |
| 8989 | |
| 8990 | do { |
| 8991 | if (strcmp(arg, "--") == 0) { |
| 8992 | ++argv; |
| 8993 | goto set_argv; |
| 8994 | } |
| 8995 | if (arg[0] != '+' && arg[0] != '-') |
| 8996 | break; |
| 8997 | for (n = 1; arg[n]; ++n) { |
| 8998 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 8999 | goto error; |
| 9000 | if (arg[n] == 'o' && argv[1]) |
| 9001 | argv++; |
| 9002 | } |
| 9003 | } while ((arg = *++argv) != NULL); |
| 9004 | /* Now argv[0] is 1st argument */ |
| 9005 | |
| 9006 | if (arg == NULL) |
| 9007 | return EXIT_SUCCESS; |
| 9008 | set_argv: |
| 9009 | |
| 9010 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 9011 | g_argv = G.global_argv; |
| 9012 | if (G.global_args_malloced) { |
| 9013 | pp = g_argv; |
| 9014 | while (*++pp) |
| 9015 | free(*pp); |
| 9016 | g_argv[1] = NULL; |
| 9017 | } else { |
| 9018 | G.global_args_malloced = 1; |
| 9019 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 9020 | pp[0] = g_argv[0]; /* retain $0 */ |
| 9021 | g_argv = pp; |
| 9022 | } |
| 9023 | /* This realloc's G.global_argv */ |
| 9024 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 9025 | |
| 9026 | n = 1; |
| 9027 | while (*++pp) |
| 9028 | n++; |
| 9029 | G.global_argc = n; |
| 9030 | |
| 9031 | return EXIT_SUCCESS; |
| 9032 | |
| 9033 | /* Nothing known, so abort */ |
| 9034 | error: |
| 9035 | bb_error_msg("set: %s: invalid option", arg); |
| 9036 | return EXIT_FAILURE; |
| 9037 | } |
| 9038 | |
| 9039 | static int FAST_FUNC builtin_shift(char **argv) |
| 9040 | { |
| 9041 | int n = 1; |
| 9042 | argv = skip_dash_dash(argv); |
| 9043 | if (argv[0]) { |
| 9044 | n = atoi(argv[0]); |
| 9045 | } |
| 9046 | if (n >= 0 && n < G.global_argc) { |
| 9047 | if (G.global_args_malloced) { |
| 9048 | int m = 1; |
| 9049 | while (m <= n) |
| 9050 | free(G.global_argv[m++]); |
| 9051 | } |
| 9052 | G.global_argc -= n; |
| 9053 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 9054 | G.global_argc * sizeof(G.global_argv[0])); |
| 9055 | return EXIT_SUCCESS; |
| 9056 | } |
| 9057 | return EXIT_FAILURE; |
| 9058 | } |
| 9059 | |
| 9060 | /* Interruptibility of read builtin in bash |
| 9061 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9062 | * |
| 9063 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9064 | * |
| 9065 | * SIGINT: |
| 9066 | * - terminates non-interactive shell; |
| 9067 | * - interrupts read in interactive shell; |
| 9068 | * if it has non-empty trap: |
| 9069 | * - executes trap and returns to command prompt in interactive shell; |
| 9070 | * - executes trap and returns to read in non-interactive shell; |
| 9071 | * SIGTERM: |
| 9072 | * - is ignored (does not interrupt) read in interactive shell; |
| 9073 | * - terminates non-interactive shell; |
| 9074 | * if it has non-empty trap: |
| 9075 | * - executes trap and returns to read; |
| 9076 | * SIGHUP: |
| 9077 | * - terminates shell (regardless of interactivity); |
| 9078 | * if it has non-empty trap: |
| 9079 | * - executes trap and returns to read; |
| 9080 | */ |
| 9081 | static int FAST_FUNC builtin_read(char **argv) |
| 9082 | { |
| 9083 | const char *r; |
| 9084 | char *opt_n = NULL; |
| 9085 | char *opt_p = NULL; |
| 9086 | char *opt_t = NULL; |
| 9087 | char *opt_u = NULL; |
| 9088 | const char *ifs; |
| 9089 | int read_flags; |
| 9090 | |
| 9091 | /* "!": do not abort on errors. |
| 9092 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9093 | */ |
| 9094 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 9095 | if (read_flags == (uint32_t)-1) |
| 9096 | return EXIT_FAILURE; |
| 9097 | argv += optind; |
| 9098 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9099 | |
| 9100 | again: |
| 9101 | r = shell_builtin_read(set_local_var_from_halves, |
| 9102 | argv, |
| 9103 | ifs, |
| 9104 | read_flags, |
| 9105 | opt_n, |
| 9106 | opt_p, |
| 9107 | opt_t, |
| 9108 | opt_u |
| 9109 | ); |
| 9110 | |
| 9111 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9112 | unsigned sig = check_and_run_traps(); |
| 9113 | if (sig && sig != SIGINT) |
| 9114 | goto again; |
| 9115 | } |
| 9116 | |
| 9117 | if ((uintptr_t)r > 1) { |
| 9118 | bb_error_msg("%s", r); |
| 9119 | r = (char*)(uintptr_t)1; |
| 9120 | } |
| 9121 | |
| 9122 | return (uintptr_t)r; |
| 9123 | } |
| 9124 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9125 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9126 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9127 | int sig; |
| 9128 | char *new_cmd; |
| 9129 | |
| 9130 | if (!G.traps) |
| 9131 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 9132 | |
| 9133 | argv++; |
| 9134 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9135 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9136 | /* No args: print all trapped */ |
| 9137 | for (i = 0; i < NSIG; ++i) { |
| 9138 | if (G.traps[i]) { |
| 9139 | printf("trap -- "); |
| 9140 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 9141 | /* note: bash adds "SIG", but only if invoked |
| 9142 | * as "bash". If called as "sh", or if set -o posix, |
| 9143 | * then it prints short signal names. |
| 9144 | * We are printing short names: */ |
| 9145 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9146 | } |
| 9147 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9148 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9149 | return EXIT_SUCCESS; |
| 9150 | } |
| 9151 | |
| 9152 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9153 | /* If first arg is a number: reset all specified signals */ |
| 9154 | sig = bb_strtou(*argv, NULL, 10); |
| 9155 | if (errno == 0) { |
| 9156 | int ret; |
| 9157 | process_sig_list: |
| 9158 | ret = EXIT_SUCCESS; |
| 9159 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9160 | sighandler_t handler; |
| 9161 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9162 | sig = get_signum(*argv++); |
| 9163 | if (sig < 0 || sig >= NSIG) { |
| 9164 | ret = EXIT_FAILURE; |
| 9165 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9166 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9167 | continue; |
| 9168 | } |
| 9169 | |
| 9170 | free(G.traps[sig]); |
| 9171 | G.traps[sig] = xstrdup(new_cmd); |
| 9172 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9173 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9174 | get_signame(sig), sig, G.traps[sig]); |
| 9175 | |
| 9176 | /* There is no signal for 0 (EXIT) */ |
| 9177 | if (sig == 0) |
| 9178 | continue; |
| 9179 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9180 | if (new_cmd) |
| 9181 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 9182 | else |
| 9183 | /* We are removing trap handler */ |
| 9184 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9185 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9186 | } |
| 9187 | return ret; |
| 9188 | } |
| 9189 | |
| 9190 | if (!argv[1]) { /* no second arg */ |
| 9191 | bb_error_msg("trap: invalid arguments"); |
| 9192 | return EXIT_FAILURE; |
| 9193 | } |
| 9194 | |
| 9195 | /* First arg is "-": reset all specified to default */ |
| 9196 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 9197 | /* Everything else: set arg as signal handler |
| 9198 | * (includes "" case, which ignores signal) */ |
| 9199 | if (argv[0][0] == '-') { |
| 9200 | if (argv[0][1] == '\0') { /* "-" */ |
| 9201 | /* new_cmd remains NULL: "reset these sigs" */ |
| 9202 | goto reset_traps; |
| 9203 | } |
| 9204 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 9205 | argv++; |
| 9206 | } |
| 9207 | /* else: "-something", no special meaning */ |
| 9208 | } |
| 9209 | new_cmd = *argv; |
| 9210 | reset_traps: |
| 9211 | argv++; |
| 9212 | goto process_sig_list; |
| 9213 | } |
| 9214 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9215 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9216 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9217 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9218 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9219 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9220 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9221 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 9222 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9223 | |
| 9224 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9225 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9226 | type = "an alias";*/ |
| 9227 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9228 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9229 | type = "a function"; |
| 9230 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9231 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9232 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9233 | else if ((path = find_in_path(*argv)) != NULL) |
| 9234 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9235 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9236 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9237 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9238 | continue; |
| 9239 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9240 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9241 | printf("%s is %s\n", *argv, type); |
| 9242 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9243 | } |
| 9244 | |
| 9245 | return ret; |
| 9246 | } |
| 9247 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9248 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9249 | static struct pipe *parse_jobspec(const char *str) |
| 9250 | { |
| 9251 | struct pipe *pi; |
| 9252 | int jobnum; |
| 9253 | |
| 9254 | if (sscanf(str, "%%%d", &jobnum) != 1) { |
| 9255 | bb_error_msg("bad argument '%s'", str); |
| 9256 | return NULL; |
| 9257 | } |
| 9258 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9259 | if (pi->jobid == jobnum) { |
| 9260 | return pi; |
| 9261 | } |
| 9262 | } |
| 9263 | bb_error_msg("%d: no such job", jobnum); |
| 9264 | return NULL; |
| 9265 | } |
| 9266 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9267 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9268 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9269 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9270 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9271 | struct pipe *pi; |
| 9272 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9273 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9274 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9275 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9276 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 9277 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9278 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9279 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9280 | goto found; |
| 9281 | } |
| 9282 | } |
| 9283 | bb_error_msg("%s: no current job", argv[0]); |
| 9284 | return EXIT_FAILURE; |
| 9285 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9286 | |
| 9287 | pi = parse_jobspec(argv[1]); |
| 9288 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9289 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9290 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 9291 | /* TODO: bash prints a string representation |
| 9292 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9293 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9294 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9295 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9299 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 9300 | for (i = 0; i < pi->num_cmds; i++) { |
| 9301 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9302 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9303 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9304 | |
| 9305 | i = kill(- pi->pgrp, SIGCONT); |
| 9306 | if (i < 0) { |
| 9307 | if (errno == ESRCH) { |
| 9308 | delete_finished_bg_job(pi); |
| 9309 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9310 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9311 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9312 | } |
| 9313 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9314 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9315 | remove_bg_job(pi); |
| 9316 | return checkjobs_and_fg_shell(pi); |
| 9317 | } |
| 9318 | return EXIT_SUCCESS; |
| 9319 | } |
| 9320 | #endif |
| 9321 | |
| 9322 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9323 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9324 | { |
| 9325 | const struct built_in_command *x; |
| 9326 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9327 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9328 | "Built-in commands:\n" |
| 9329 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9330 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9331 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9332 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9333 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9334 | return EXIT_SUCCESS; |
| 9335 | } |
| 9336 | #endif |
| 9337 | |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 9338 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9339 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9340 | { |
| 9341 | show_history(G.line_input_state); |
| 9342 | return EXIT_SUCCESS; |
| 9343 | } |
| 9344 | #endif |
| 9345 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9346 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9347 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9348 | { |
| 9349 | struct pipe *job; |
| 9350 | const char *status_string; |
| 9351 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9352 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9353 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9354 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9355 | status_string = "Stopped"; |
| 9356 | else |
| 9357 | status_string = "Running"; |
| 9358 | |
| 9359 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 9360 | } |
| 9361 | return EXIT_SUCCESS; |
| 9362 | } |
| 9363 | #endif |
| 9364 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9365 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9366 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9367 | { |
| 9368 | void *p; |
| 9369 | unsigned long l; |
| 9370 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9371 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 9372 | /* Optional. Reduces probability of false positives */ |
| 9373 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9374 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9375 | /* Crude attempt to find where "free memory" starts, |
| 9376 | * sans fragmentation. */ |
| 9377 | p = malloc(240); |
| 9378 | l = (unsigned long)p; |
| 9379 | free(p); |
| 9380 | p = malloc(3400); |
| 9381 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 9382 | free(p); |
| 9383 | |
Denys Vlasenko | 7f0ebbc | 2016-10-03 17:42:53 +0200 | [diff] [blame] | 9384 | |
| 9385 | # if 0 /* debug */ |
| 9386 | { |
| 9387 | struct mallinfo mi = mallinfo(); |
| 9388 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 9389 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 9390 | } |
| 9391 | # endif |
| 9392 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9393 | if (!G.memleak_value) |
| 9394 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 9395 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9396 | l -= G.memleak_value; |
| 9397 | if ((long)l < 0) |
| 9398 | l = 0; |
| 9399 | l /= 1024; |
| 9400 | if (l > 127) |
| 9401 | l = 127; |
| 9402 | |
| 9403 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 9404 | return l; |
| 9405 | } |
| 9406 | #endif |
| 9407 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9408 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9409 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9410 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9411 | return EXIT_SUCCESS; |
| 9412 | } |
| 9413 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9414 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9415 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9416 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9417 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9418 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9419 | #if ENABLE_HUSH_FUNCTIONS |
| 9420 | smallint sv_flg; |
| 9421 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9422 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9423 | argv = skip_dash_dash(argv); |
| 9424 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9425 | if (!filename) { |
| 9426 | /* bash says: "bash: .: filename argument required" */ |
| 9427 | return 2; /* bash compat */ |
| 9428 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9429 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9430 | if (!strchr(filename, '/')) { |
| 9431 | arg_path = find_in_path(filename); |
| 9432 | if (arg_path) |
| 9433 | filename = arg_path; |
| 9434 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9435 | input = remember_FILE(fopen_or_warn(filename, "r")); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9436 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9437 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9438 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9439 | /* POSIX: non-interactive shell should abort here, |
| 9440 | * not merely fail. So far no one complained :) |
| 9441 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9442 | return EXIT_FAILURE; |
| 9443 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9444 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9445 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9446 | sv_flg = G_flag_return_in_progress; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9447 | /* "we are inside sourced file, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9448 | G_flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9449 | #endif |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9450 | if (argv[1]) |
| 9451 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9452 | |
Denys Vlasenko | 992e0ff | 2016-09-29 01:27:09 +0200 | [diff] [blame] | 9453 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 9454 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9455 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9456 | fclose_and_forget(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9457 | |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9458 | if (argv[1]) |
| 9459 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9460 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9461 | G_flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9462 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9463 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9464 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9465 | } |
| 9466 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9467 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9468 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9469 | int rc; |
| 9470 | mode_t mask; |
| 9471 | |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9472 | rc = 1; |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9473 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9474 | argv = skip_dash_dash(argv); |
| 9475 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9476 | mode_t old_mask = mask; |
| 9477 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9478 | /* numeric umasks are taken as-is */ |
| 9479 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9480 | if (!isdigit(argv[0][0])) |
| 9481 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9482 | mask = bb_parse_mode(argv[0], mask); |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9483 | if (!isdigit(argv[0][0])) |
| 9484 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9485 | if ((unsigned)mask > 0777) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9486 | mask = old_mask; |
| 9487 | /* bash messages: |
| 9488 | * bash: umask: 'q': invalid symbolic mode operator |
| 9489 | * bash: umask: 999: octal number out of range |
| 9490 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 9491 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9492 | rc = 0; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9493 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9494 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9495 | /* Mimic bash */ |
| 9496 | printf("%04o\n", (unsigned) mask); |
| 9497 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9498 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9499 | umask(mask); |
| 9500 | |
| 9501 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9502 | } |
| 9503 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9504 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9505 | #if !ENABLE_HUSH_JOB |
| 9506 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 9507 | #endif |
| 9508 | static int wait_for_child_or_signal(struct pipe *waitfor_pipe, pid_t waitfor_pid) |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9509 | { |
| 9510 | int ret = 0; |
| 9511 | for (;;) { |
| 9512 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9513 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9514 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9515 | if (!sigisemptyset(&G.pending_set)) |
| 9516 | goto check_sig; |
| 9517 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9518 | /* waitpid is not interruptible by SA_RESTARTed |
| 9519 | * signals which we use. Thus, this ugly dance: |
| 9520 | */ |
| 9521 | |
| 9522 | /* Make sure possible SIGCHLD is stored in kernel's |
| 9523 | * pending signal mask before we call waitpid. |
| 9524 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9525 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9526 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9527 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 9528 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9529 | |
| 9530 | if (!sigisemptyset(&G.pending_set)) { |
| 9531 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9532 | goto restore; |
| 9533 | } |
| 9534 | |
| 9535 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9536 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9537 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9538 | debug_printf_exec("checkjobs:%d\n", ret); |
| 9539 | #if ENABLE_HUSH_JOB |
| 9540 | if (waitfor_pipe) { |
| 9541 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 9542 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 9543 | if (rcode >= 0) { |
| 9544 | ret = rcode; |
| 9545 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9546 | break; |
| 9547 | } |
| 9548 | } |
| 9549 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9550 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 9551 | /* if ret == 0, no children changed state */ |
| 9552 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9553 | if (errno == ECHILD || ret) { |
| 9554 | ret--; |
| 9555 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9556 | ret = 0; |
| 9557 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9558 | break; |
| 9559 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9560 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9561 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 9562 | /* Note: sigsuspend invokes signal handler */ |
| 9563 | sigsuspend(&oldset); |
| 9564 | restore: |
| 9565 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9566 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9567 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9568 | sig = check_and_run_traps(); |
| 9569 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9570 | ret = 128 + sig; |
| 9571 | break; |
| 9572 | } |
| 9573 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 9574 | } |
| 9575 | return ret; |
| 9576 | } |
| 9577 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9578 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9579 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9580 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9581 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9582 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9583 | argv = skip_dash_dash(argv); |
| 9584 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9585 | /* Don't care about wait results */ |
| 9586 | /* Note 1: must wait until there are no more children */ |
| 9587 | /* Note 2: must be interruptible */ |
| 9588 | /* Examples: |
| 9589 | * $ sleep 3 & sleep 6 & wait |
| 9590 | * [1] 30934 sleep 3 |
| 9591 | * [2] 30935 sleep 6 |
| 9592 | * [1] Done sleep 3 |
| 9593 | * [2] Done sleep 6 |
| 9594 | * $ sleep 3 & sleep 6 & wait |
| 9595 | * [1] 30936 sleep 3 |
| 9596 | * [2] 30937 sleep 6 |
| 9597 | * [1] Done sleep 3 |
| 9598 | * ^C <-- after ~4 sec from keyboard |
| 9599 | * $ |
| 9600 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9601 | return wait_for_child_or_signal(NULL, 0 /*(no job and no pid to wait for)*/); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9602 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9603 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9604 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9605 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9606 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9607 | #if ENABLE_HUSH_JOB |
| 9608 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9609 | struct pipe *wait_pipe; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9610 | wait_pipe = parse_jobspec(*argv); |
| 9611 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9612 | ret = job_exited_or_stopped(wait_pipe); |
| 9613 | if (ret < 0) |
| 9614 | ret = wait_for_child_or_signal(wait_pipe, 0); |
| 9615 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9616 | } |
| 9617 | } |
| 9618 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9619 | /* mimic bash message */ |
| 9620 | bb_error_msg("wait: '%s': not a pid or valid job spec", *argv); |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9621 | ret = EXIT_FAILURE; |
| 9622 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9623 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9624 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9625 | /* Do we have such child? */ |
| 9626 | ret = waitpid(pid, &status, WNOHANG); |
| 9627 | if (ret < 0) { |
| 9628 | /* No */ |
| 9629 | if (errno == ECHILD) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9630 | if (G.last_bg_pid > 0 && pid == G.last_bg_pid) { |
| 9631 | /* "wait $!" but last bg task has already exited. Try: |
| 9632 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 9633 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 9634 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9635 | */ |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 9636 | /* ret = G.last_bg_pid_exitstatus - FIXME */ |
| 9637 | } else { |
| 9638 | /* Example: "wait 1". mimic bash message */ |
| 9639 | bb_error_msg("wait: pid %d is not a child of this shell", (int)pid); |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9640 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9641 | } else { |
| 9642 | /* ??? */ |
| 9643 | bb_perror_msg("wait %s", *argv); |
| 9644 | } |
| 9645 | ret = 127; |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9646 | continue; /* bash checks all argv[] */ |
| 9647 | } |
| 9648 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9649 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9650 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9651 | } else { |
| 9652 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9653 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 9654 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9655 | if (WIFSIGNALED(status)) |
| 9656 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9657 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9658 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9659 | |
| 9660 | return ret; |
| 9661 | } |
| 9662 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9663 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 9664 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 9665 | { |
| 9666 | if (argv[1]) { |
| 9667 | def = bb_strtou(argv[1], NULL, 10); |
| 9668 | if (errno || def < def_min || argv[2]) { |
| 9669 | bb_error_msg("%s: bad arguments", argv[0]); |
| 9670 | def = UINT_MAX; |
| 9671 | } |
| 9672 | } |
| 9673 | return def; |
| 9674 | } |
| 9675 | #endif |
| 9676 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9677 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9678 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9679 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9680 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9681 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9682 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9683 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 9684 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 9685 | return EXIT_SUCCESS; /* bash compat */ |
| 9686 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9687 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9688 | |
| 9689 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 9690 | if (depth == UINT_MAX) |
| 9691 | G.flag_break_continue = BC_BREAK; |
| 9692 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9693 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9694 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9695 | return EXIT_SUCCESS; |
| 9696 | } |
| 9697 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9698 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9699 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9700 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 9701 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9702 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9703 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9704 | |
| 9705 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9706 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9707 | { |
| 9708 | int rc; |
| 9709 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9710 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9711 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 9712 | return EXIT_FAILURE; /* bash compat */ |
| 9713 | } |
| 9714 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9715 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9716 | |
| 9717 | /* bash: |
| 9718 | * out of range: wraps around at 256, does not error out |
| 9719 | * non-numeric param: |
| 9720 | * f() { false; return qwe; }; f; echo $? |
| 9721 | * bash: return: qwe: numeric argument required <== we do this |
| 9722 | * 255 <== we also do this |
| 9723 | */ |
| 9724 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 9725 | return rc; |
| 9726 | } |
| 9727 | #endif |