Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3 | * A prototype Bourne shell grammar parser. |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7 | * |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com> |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10 | * |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 12 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 13 | * Credits: |
| 14 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 15 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 16 | * execution engine, the builtins, and much of the underlying |
| 17 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 19 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 20 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 21 | * Troan, which they placed in the public domain. I don't know |
| 22 | * how much of the Johnson/Troan code has survived the repeated |
| 23 | * rewrites. |
| 24 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 25 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 26 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 27 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 28 | * and many builtins derived from contributions by Erik Andersen. |
| 29 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 30 | * |
| 31 | * There are two big (and related) architecture differences between |
| 32 | * this parser and the lash parser. One is that this version is |
| 33 | * actually designed from the ground up to understand nearly all |
| 34 | * of the Bourne grammar. The second, consequential change is that |
| 35 | * the parser and input reader have been turned inside out. Now, |
| 36 | * the parser is in control, and asks for input as needed. The old |
| 37 | * way had the input reader in control, and it asked for parsing to |
| 38 | * take place as needed. The new way makes it much easier to properly |
| 39 | * handle the recursion implicit in the various substitutions, especially |
| 40 | * across continuation lines. |
| 41 | * |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 42 | * TODOs: |
| 43 | * grep for "TODO" and fix (some of them are easy) |
| 44 | * special variables (done: PWD, PPID, RANDOM) |
| 45 | * tilde expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 46 | * aliases |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * builtins mandated by standards we don't support: |
| 49 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 50 | * make complex ${var%...} constructs support optional |
| 51 | * make here documents optional |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 52 | * make trap, read, ulimit builtins 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 | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 85 | //config:config HUSH |
| 86 | //config: bool "hush" |
| 87 | //config: default y |
| 88 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 89 | //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] | 90 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 91 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 92 | //config: and functions are supported. |
| 93 | //config: |
| 94 | //config: It will compile and work on no-mmu systems. |
| 95 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 96 | //config: It does not handle select, aliases, tilde expansion, |
| 97 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 98 | //config: |
| 99 | //config:config HUSH_BASH_COMPAT |
| 100 | //config: bool "bash-compatible extensions" |
| 101 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 102 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //config: help |
| 104 | //config: Enable bash-compatible extensions. |
| 105 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 106 | //config:config HUSH_BRACE_EXPANSION |
| 107 | //config: bool "Brace expansion" |
| 108 | //config: default y |
| 109 | //config: depends on HUSH_BASH_COMPAT |
| 110 | //config: help |
| 111 | //config: Enable {abc,def} extension. |
| 112 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 113 | //config:config HUSH_HELP |
| 114 | //config: bool "help builtin" |
| 115 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 116 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 117 | //config: help |
| 118 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 119 | //config: |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 120 | //config:config HUSH_PRINTF |
| 121 | //config: bool "printf builtin" |
| 122 | //config: default y |
| 123 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 124 | //config: help |
| 125 | //config: Enable printf builtin in hush. |
| 126 | //config: |
| 127 | //config:config HUSH_KILL |
| 128 | //config: bool "kill builtin (for kill %jobspec)" |
| 129 | //config: default y |
| 130 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 131 | //config: help |
| 132 | //config: Enable kill builtin in hush. |
| 133 | //config: |
| 134 | //config:config HUSH_WAIT |
| 135 | //config: bool "wait builtin" |
| 136 | //config: default y |
| 137 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 138 | //config: help |
| 139 | //config: Enable wait builtin in hush. |
| 140 | //config: |
| 141 | //config:config HUSH_TYPE |
| 142 | //config: bool "type builtin" |
| 143 | //config: default y |
| 144 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 145 | //config: help |
| 146 | //config: Enable type builtin in hush. |
| 147 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 148 | //config:config HUSH_INTERACTIVE |
| 149 | //config: bool "Interactive mode" |
| 150 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 151 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 152 | //config: help |
| 153 | //config: Enable interactive mode (prompt and command editing). |
| 154 | //config: Without this, hush simply reads and executes commands |
| 155 | //config: from stdin just like a shell script from a file. |
| 156 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 157 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 158 | //config:config HUSH_SAVEHISTORY |
| 159 | //config: bool "Save command history to .hush_history" |
| 160 | //config: default y |
| 161 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 162 | //config: help |
| 163 | //config: Enable history saving in hush. |
| 164 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config:config HUSH_JOB |
| 166 | //config: bool "Job control" |
| 167 | //config: default y |
| 168 | //config: depends on HUSH_INTERACTIVE |
| 169 | //config: help |
| 170 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 171 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 172 | //config: "cmd &" still works by simply spawning a process and immediately |
| 173 | //config: prompting for next command (or executing next command in a script), |
| 174 | //config: but no separate process group is formed. |
| 175 | //config: |
| 176 | //config:config HUSH_TICK |
| 177 | //config: bool "Process substitution" |
| 178 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 179 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 180 | //config: help |
| 181 | //config: Enable process substitution `command` and $(command) in hush. |
| 182 | //config: |
| 183 | //config:config HUSH_IF |
| 184 | //config: bool "Support if/then/elif/else/fi" |
| 185 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 186 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 187 | //config: help |
| 188 | //config: Enable if/then/elif/else/fi in hush. |
| 189 | //config: |
| 190 | //config:config HUSH_LOOPS |
| 191 | //config: bool "Support for, while and until loops" |
| 192 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 193 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 194 | //config: help |
| 195 | //config: Enable for, while and until loops in hush. |
| 196 | //config: |
| 197 | //config:config HUSH_CASE |
| 198 | //config: bool "Support case ... esac statement" |
| 199 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 200 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 201 | //config: help |
| 202 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 203 | //config: |
| 204 | //config:config HUSH_FUNCTIONS |
| 205 | //config: bool "Support funcname() { commands; } syntax" |
| 206 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 207 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 208 | //config: help |
| 209 | //config: Enable support for shell functions in hush. +800 bytes. |
| 210 | //config: |
| 211 | //config:config HUSH_LOCAL |
| 212 | //config: bool "Support local builtin" |
| 213 | //config: default y |
| 214 | //config: depends on HUSH_FUNCTIONS |
| 215 | //config: help |
| 216 | //config: Enable support for local variables in functions. |
| 217 | //config: |
| 218 | //config:config HUSH_RANDOM_SUPPORT |
| 219 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 220 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 221 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 222 | //config: help |
| 223 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 224 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 225 | //config: |
| 226 | //config:config HUSH_EXPORT_N |
| 227 | //config: bool "Support 'export -n' option" |
| 228 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 229 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 230 | //config: help |
| 231 | //config: export -n unexports variables. It is a bash extension. |
| 232 | //config: |
| 233 | //config:config HUSH_MODE_X |
| 234 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 235 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 236 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 237 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 238 | //config: This instructs hush to print commands before execution. |
| 239 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 240 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 241 | //config:config MSH |
| 242 | //config: bool "msh (deprecated: aliased to hush)" |
| 243 | //config: default n |
| 244 | //config: select HUSH |
| 245 | //config: help |
| 246 | //config: msh is deprecated and will be removed, please migrate to hush. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 247 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 248 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 249 | //applet:IF_MSH(APPLET_ODDNAME(msh, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
| 250 | //applet:IF_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
| 251 | //applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 252 | |
| 253 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 254 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 255 | //kbuild:lib-$(CONFIG_BASH_IS_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 268 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 269 | || defined(__APPLE__) \ |
| 270 | ) |
| 271 | # include <malloc.h> /* for malloc_trim */ |
| 272 | #endif |
| 273 | #include <glob.h> |
| 274 | /* #include <dmalloc.h> */ |
| 275 | #if ENABLE_HUSH_CASE |
| 276 | # include <fnmatch.h> |
| 277 | #endif |
| 278 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 279 | |
| 280 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 281 | #include "unicode.h" |
| 282 | #include "shell_common.h" |
| 283 | #include "math.h" |
| 284 | #include "match.h" |
| 285 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 286 | # include "random.h" |
| 287 | #else |
| 288 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 289 | #endif |
| 290 | #ifndef F_DUPFD_CLOEXEC |
| 291 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 292 | #endif |
| 293 | #ifndef PIPE_BUF |
| 294 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 295 | #endif |
| 296 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 297 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 298 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 299 | #define LEAK_HUNTING 0 |
| 300 | #define BUILD_AS_NOMMU 0 |
| 301 | /* Enable/disable sanity checks. Ok to enable in production, |
| 302 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 303 | * Keeping 1 for now even in released versions. |
| 304 | */ |
| 305 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 306 | /* Slightly bigger (+200 bytes), but faster hush. |
| 307 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 308 | * which allows us to do fewer waitpid's. |
| 309 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 310 | * and therefore waitpid will return the same result as last time) |
| 311 | */ |
| 312 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 313 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 314 | * So far ${var%...} ops are always enabled: |
| 315 | */ |
| 316 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 317 | |
| 318 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 319 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 320 | # undef BB_MMU |
| 321 | # undef USE_FOR_NOMMU |
| 322 | # undef USE_FOR_MMU |
| 323 | # define BB_MMU 0 |
| 324 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 325 | # define USE_FOR_MMU(...) |
| 326 | #endif |
| 327 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 328 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 329 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 330 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 331 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 332 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 333 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 334 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 335 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 336 | # define IF_FEATURE_SH_STANDALONE(...) |
| 337 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 338 | #endif |
| 339 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 340 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 341 | # undef ENABLE_FEATURE_EDITING |
| 342 | # define ENABLE_FEATURE_EDITING 0 |
| 343 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 344 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 345 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 346 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 347 | #endif |
| 348 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 349 | /* Do we support ANY keywords? */ |
| 350 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 351 | # define HAS_KEYWORDS 1 |
| 352 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 353 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 354 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 355 | # define HAS_KEYWORDS 0 |
| 356 | # define IF_HAS_KEYWORDS(...) |
| 357 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 358 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 360 | /* If you comment out one of these below, it will be #defined later |
| 361 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 362 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 363 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 364 | #define debug_printf_parse(...) do {} while (0) |
| 365 | #define debug_print_tree(a, b) do {} while (0) |
| 366 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 367 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 368 | #define debug_printf_jobs(...) do {} while (0) |
| 369 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 370 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 371 | #define debug_printf_glob(...) do {} while (0) |
| 372 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 373 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 374 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 375 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 376 | #define ERR_PTR ((void*)(long)1) |
| 377 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 378 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 379 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 380 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 381 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 382 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 383 | #if ENABLE_HUSH_BASH_COMPAT |
| 384 | /* Support / and // replace ops */ |
| 385 | /* Note that // is stored as \ in "encoded" string representation */ |
| 386 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 387 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 388 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 389 | #else |
| 390 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 391 | # define VAR_SUBST_OPS "%#:-=+?" |
| 392 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 393 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 394 | |
| 395 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 396 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 397 | struct variable; |
| 398 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 399 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 400 | |
| 401 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 402 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 403 | */ |
| 404 | #if !BB_MMU |
| 405 | typedef struct nommu_save_t { |
| 406 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 407 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 408 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 409 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 410 | } nommu_save_t; |
| 411 | #endif |
| 412 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 413 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 414 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 415 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 416 | RES_IF , |
| 417 | RES_THEN , |
| 418 | RES_ELIF , |
| 419 | RES_ELSE , |
| 420 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 421 | #endif |
| 422 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 423 | RES_FOR , |
| 424 | RES_WHILE , |
| 425 | RES_UNTIL , |
| 426 | RES_DO , |
| 427 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 428 | #endif |
| 429 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 430 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 431 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 432 | #if ENABLE_HUSH_CASE |
| 433 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 434 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 435 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 436 | RES_MATCH , /* "word)" */ |
| 437 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 438 | RES_ESAC , |
| 439 | #endif |
| 440 | RES_XXXX , |
| 441 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 442 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 443 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 444 | typedef struct o_string { |
| 445 | char *data; |
| 446 | int length; /* position where data is appended */ |
| 447 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 448 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 449 | /* At least some part of the string was inside '' or "", |
| 450 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 451 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 452 | smallint has_empty_slot; |
| 453 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 454 | } o_string; |
| 455 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 456 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 457 | EXP_FLAG_GLOB = 0x2, |
| 458 | /* Protect newly added chars against globbing |
| 459 | * by prepending \ to *, ?, [, \ */ |
| 460 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 461 | }; |
| 462 | enum { |
| 463 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 464 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 465 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 466 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 467 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 468 | }; |
| 469 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 470 | #define NULL_O_STRING { NULL } |
| 471 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 472 | #ifndef debug_printf_parse |
| 473 | static const char *const assignment_flag[] = { |
| 474 | "MAYBE_ASSIGNMENT", |
| 475 | "DEFINITELY_ASSIGNMENT", |
| 476 | "NOT_ASSIGNMENT", |
| 477 | "WORD_IS_KEYWORD", |
| 478 | }; |
| 479 | #endif |
| 480 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 481 | typedef struct in_str { |
| 482 | const char *p; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 483 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 484 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 485 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 486 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 487 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 488 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 489 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 490 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 491 | /* The descrip member of this structure is only used to make |
| 492 | * debugging output pretty */ |
| 493 | static const struct { |
| 494 | int mode; |
| 495 | signed char default_fd; |
| 496 | char descrip[3]; |
| 497 | } redir_table[] = { |
| 498 | { O_RDONLY, 0, "<" }, |
| 499 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 500 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 501 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 502 | { O_RDONLY, 0, "<<" }, |
| 503 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 504 | /* { O_RDONLY, 77, "<<" }, */ |
| 505 | }; |
| 506 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 507 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 508 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 509 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 510 | int rd_fd; /* fd to redirect */ |
| 511 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 512 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 513 | smallint rd_type; /* (enum redir_type) */ |
| 514 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 515 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 516 | * bit 0: do we need to trim leading tabs? |
| 517 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 518 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 519 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 520 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 521 | REDIRECT_INPUT = 0, |
| 522 | REDIRECT_OVERWRITE = 1, |
| 523 | REDIRECT_APPEND = 2, |
| 524 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 525 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 526 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 527 | |
| 528 | REDIRFD_CLOSE = -3, |
| 529 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 530 | REDIRFD_TO_FILE = -1, |
| 531 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 532 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 533 | HEREDOC_SKIPTABS = 1, |
| 534 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 535 | } redir_type; |
| 536 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 537 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 538 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 539 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 540 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 541 | smallint cmd_type; /* CMD_xxx */ |
| 542 | #define CMD_NORMAL 0 |
| 543 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 544 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 545 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 546 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 547 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 548 | #if ENABLE_HUSH_FUNCTIONS |
| 549 | # define CMD_FUNCDEF 3 |
| 550 | #endif |
| 551 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 552 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 553 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 554 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 555 | #if !BB_MMU |
| 556 | char *group_as_string; |
| 557 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 558 | #if ENABLE_HUSH_FUNCTIONS |
| 559 | struct function *child_func; |
| 560 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 561 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 562 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 563 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 564 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 565 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 566 | * we put those fields back into cmd->xxx |
| 567 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 568 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 569 | * Without this trick, loop would execute a;b;b;b;... |
| 570 | * instead of correct sequence a;b;a;b;... |
| 571 | * When command is freed, it severs the link |
| 572 | * (sets ->child_func->parent_cmd to NULL). |
| 573 | */ |
| 574 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 575 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 576 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 577 | * and on execution these are substituted with their values. |
| 578 | * Substitution can make _several_ words out of one argv[n]! |
| 579 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 580 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 581 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 582 | struct redir_struct *redirects; /* I/O redirections */ |
| 583 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 584 | /* Is there anything in this command at all? */ |
| 585 | #define IS_NULL_CMD(cmd) \ |
| 586 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 587 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 588 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 589 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 590 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 591 | int alive_cmds; /* number of commands running (not exited) */ |
| 592 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 593 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 594 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 595 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 596 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 597 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 598 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 599 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 600 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 601 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 602 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 603 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 604 | PIPE_SEQ = 0, |
| 605 | PIPE_AND = 1, |
| 606 | PIPE_OR = 2, |
| 607 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 608 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 609 | /* Is there anything in this pipe at all? */ |
| 610 | #define IS_NULL_PIPE(pi) \ |
| 611 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 612 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 613 | /* This holds pointers to the various results of parsing */ |
| 614 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 615 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 616 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 617 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 618 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 619 | /* last command in pipe (being constructed right now) */ |
| 620 | struct command *command; |
| 621 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 622 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 623 | #if !BB_MMU |
| 624 | o_string as_string; |
| 625 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 626 | #if HAS_KEYWORDS |
| 627 | smallint ctx_res_w; |
| 628 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 629 | #if ENABLE_HUSH_CASE |
| 630 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 631 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 632 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 633 | int old_flag; |
| 634 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 635 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 636 | * when we see "if" or "then", we malloc and copy current context, |
| 637 | * and make ->stack point to it. then we parse pipeN. |
| 638 | * when closing "then" / fi" / whatever is found, |
| 639 | * we move list_head into ->stack->command->group, |
| 640 | * copy ->stack into current context, and delete ->stack. |
| 641 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 642 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 643 | struct parse_context *stack; |
| 644 | #endif |
| 645 | }; |
| 646 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 647 | /* On program start, environ points to initial environment. |
| 648 | * putenv adds new pointers into it, unsetenv removes them. |
| 649 | * Neither of these (de)allocates the strings. |
| 650 | * setenv allocates new strings in malloc space and does putenv, |
| 651 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 652 | #define setenv(...) setenv_is_leaky_dont_use() |
| 653 | struct variable { |
| 654 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 655 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 656 | #if ENABLE_HUSH_LOCAL |
| 657 | unsigned func_nest_level; |
| 658 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 659 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 660 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 661 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 662 | }; |
| 663 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 664 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 665 | BC_BREAK = 1, |
| 666 | BC_CONTINUE = 2, |
| 667 | }; |
| 668 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 669 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 670 | struct function { |
| 671 | struct function *next; |
| 672 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 673 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 674 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 675 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 676 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 677 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 678 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 679 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 680 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 681 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 682 | /* set -/+o OPT support. (TODO: make it optional) |
| 683 | * bash supports the following opts: |
| 684 | * allexport off |
| 685 | * braceexpand on |
| 686 | * emacs on |
| 687 | * errexit off |
| 688 | * errtrace off |
| 689 | * functrace off |
| 690 | * hashall on |
| 691 | * histexpand off |
| 692 | * history on |
| 693 | * ignoreeof off |
| 694 | * interactive-comments on |
| 695 | * keyword off |
| 696 | * monitor on |
| 697 | * noclobber off |
| 698 | * noexec off |
| 699 | * noglob off |
| 700 | * nolog off |
| 701 | * notify off |
| 702 | * nounset off |
| 703 | * onecmd off |
| 704 | * physical off |
| 705 | * pipefail off |
| 706 | * posix off |
| 707 | * privileged off |
| 708 | * verbose off |
| 709 | * vi off |
| 710 | * xtrace off |
| 711 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 712 | static const char o_opt_strings[] ALIGN1 = |
| 713 | "pipefail\0" |
| 714 | "noexec\0" |
| 715 | #if ENABLE_HUSH_MODE_X |
| 716 | "xtrace\0" |
| 717 | #endif |
| 718 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 719 | enum { |
| 720 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 721 | OPT_O_NOEXEC, |
| 722 | #if ENABLE_HUSH_MODE_X |
| 723 | OPT_O_XTRACE, |
| 724 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 725 | NUM_OPT_O |
| 726 | }; |
| 727 | |
| 728 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 729 | struct FILE_list { |
| 730 | struct FILE_list *next; |
| 731 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 732 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 733 | }; |
| 734 | |
| 735 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 736 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 737 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 738 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 739 | /* interactive_fd != 0 means we are an interactive shell. |
| 740 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 741 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 742 | * job control still works, but terminal signals |
| 743 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 744 | * process groups can only be created with "cmd &". |
| 745 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 746 | * to give tty to the foreground process group, |
| 747 | * and will take it back when the group is stopped (^Z) |
| 748 | * or killed (^C). |
| 749 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 750 | #if ENABLE_HUSH_INTERACTIVE |
| 751 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 752 | * _AND_ if we decided to act interactively */ |
| 753 | int interactive_fd; |
| 754 | const char *PS1; |
| 755 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 756 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 757 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 758 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 759 | #endif |
| 760 | #if ENABLE_FEATURE_EDITING |
| 761 | line_input_t *line_input_state; |
| 762 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 763 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 764 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 765 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 766 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 767 | random_t random_gen; |
| 768 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 769 | #if ENABLE_HUSH_JOB |
| 770 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 771 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 772 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 773 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 774 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 775 | #else |
| 776 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 777 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 778 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 779 | #if ENABLE_HUSH_MODE_X |
| 780 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 781 | #else |
| 782 | # define G_x_mode 0 |
| 783 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 784 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 785 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 786 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 787 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 788 | #if ENABLE_HUSH_FUNCTIONS |
| 789 | /* 0: outside of a function (or sourced file) |
| 790 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 791 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 792 | */ |
| 793 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 794 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 795 | #else |
| 796 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 797 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 798 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 799 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 800 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 801 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 802 | smalluint global_args_malloced; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 803 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 804 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 805 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 806 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 807 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 808 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 809 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 810 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 811 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 812 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 813 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 814 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 815 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 816 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 817 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 818 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 819 | # if ENABLE_HUSH_LOCAL |
| 820 | struct variable **shadowed_vars_pp; |
| 821 | unsigned func_nest_level; |
| 822 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 823 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 824 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 825 | #if ENABLE_HUSH_FAST |
| 826 | unsigned count_SIGCHLD; |
| 827 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 828 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 829 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 830 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 831 | /* Which signals have non-DFL handler (even with no traps set)? |
| 832 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 833 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 834 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 835 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 836 | * Other than these two times, never modified. |
| 837 | */ |
| 838 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 839 | #if ENABLE_HUSH_JOB |
| 840 | unsigned fatal_sig_mask; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 841 | # define G_fatal_sig_mask G.fatal_sig_mask |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 842 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 843 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 844 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 845 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 846 | sigset_t pending_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 847 | #if HUSH_DEBUG |
| 848 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 849 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 850 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 851 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 852 | #if ENABLE_FEATURE_EDITING |
| 853 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 854 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 855 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 856 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 857 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 858 | * (too many defines). Also, I actually prefer to see when a variable |
| 859 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 860 | #define INIT_G() do { \ |
| 861 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 862 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 863 | sigfillset(&G.sa.sa_mask); \ |
| 864 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 865 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 866 | |
| 867 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 868 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 869 | static int builtin_cd(char **argv) FAST_FUNC; |
| 870 | static int builtin_echo(char **argv) FAST_FUNC; |
| 871 | static int builtin_eval(char **argv) FAST_FUNC; |
| 872 | static int builtin_exec(char **argv) FAST_FUNC; |
| 873 | static int builtin_exit(char **argv) FAST_FUNC; |
| 874 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 875 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 876 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 877 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 878 | #endif |
| 879 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 880 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 881 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 882 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 883 | static int builtin_history(char **argv) FAST_FUNC; |
| 884 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 885 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 886 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 887 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 888 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 889 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 890 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 891 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 892 | static int builtin_printf(char **argv) FAST_FUNC; |
| 893 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 894 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 895 | static int builtin_read(char **argv) FAST_FUNC; |
| 896 | static int builtin_set(char **argv) FAST_FUNC; |
| 897 | static int builtin_shift(char **argv) FAST_FUNC; |
| 898 | static int builtin_source(char **argv) FAST_FUNC; |
| 899 | static int builtin_test(char **argv) FAST_FUNC; |
| 900 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 901 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 902 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 903 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 904 | static int builtin_true(char **argv) FAST_FUNC; |
| 905 | static int builtin_umask(char **argv) FAST_FUNC; |
| 906 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 907 | #if ENABLE_HUSH_KILL |
| 908 | static int builtin_kill(char **argv) FAST_FUNC; |
| 909 | #endif |
| 910 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 911 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 912 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 913 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 914 | static int builtin_break(char **argv) FAST_FUNC; |
| 915 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 916 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 917 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 918 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 919 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 920 | |
| 921 | /* Table of built-in functions. They can be forked or not, depending on |
| 922 | * context: within pipes, they fork. As simple commands, they do not. |
| 923 | * When used in non-forking context, they can change global variables |
| 924 | * in the parent shell process. If forked, of course they cannot. |
| 925 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 926 | * still be set at the end. */ |
| 927 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 928 | const char *b_cmd; |
| 929 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 930 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 931 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 932 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 933 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 934 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 935 | #endif |
| 936 | }; |
| 937 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 938 | static const struct built_in_command bltins1[] = { |
| 939 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 940 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 941 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 942 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 943 | #endif |
| 944 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 945 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 946 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 947 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 948 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 949 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 950 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 951 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 952 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 953 | BLTIN("exit" , builtin_exit , "Exit"), |
| 954 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 955 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 956 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 957 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 958 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 959 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 960 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 961 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 962 | BLTIN("history" , builtin_history , "Show command history"), |
| 963 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 964 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 965 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 966 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 967 | #if ENABLE_HUSH_KILL |
| 968 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 969 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 970 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 971 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 972 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 973 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 974 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 975 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 976 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 977 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 978 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 979 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 980 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 981 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 982 | #if ENABLE_HUSH_BASH_COMPAT |
| 983 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 984 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 985 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 986 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 987 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 988 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 989 | #endif |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 990 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 991 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 992 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 993 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 994 | BLTIN("wait" , builtin_wait , "Wait for process"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 995 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 996 | }; |
| 997 | /* For now, echo and test are unconditionally enabled. |
| 998 | * Maybe make it configurable? */ |
| 999 | static const struct built_in_command bltins2[] = { |
| 1000 | BLTIN("[" , builtin_test , NULL), |
| 1001 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 1002 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1003 | BLTIN("printf" , builtin_printf , NULL), |
| 1004 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1005 | BLTIN("pwd" , builtin_pwd , NULL), |
| 1006 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1007 | }; |
| 1008 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1009 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1010 | /* Debug printouts. |
| 1011 | */ |
| 1012 | #if HUSH_DEBUG |
| 1013 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1014 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1015 | # define debug_enter() (G.debug_indent++) |
| 1016 | # define debug_leave() (G.debug_indent--) |
| 1017 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1018 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1019 | # define debug_enter() ((void)0) |
| 1020 | # define debug_leave() ((void)0) |
| 1021 | #endif |
| 1022 | |
| 1023 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1024 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1025 | #endif |
| 1026 | |
| 1027 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1028 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1029 | #endif |
| 1030 | |
| 1031 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1032 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1033 | #endif |
| 1034 | |
| 1035 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1036 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1037 | #endif |
| 1038 | |
| 1039 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1040 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1041 | # define DEBUG_JOBS 1 |
| 1042 | #else |
| 1043 | # define DEBUG_JOBS 0 |
| 1044 | #endif |
| 1045 | |
| 1046 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1047 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1048 | # define DEBUG_EXPAND 1 |
| 1049 | #else |
| 1050 | # define DEBUG_EXPAND 0 |
| 1051 | #endif |
| 1052 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1053 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1054 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1055 | #endif |
| 1056 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1057 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1058 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1059 | # define DEBUG_GLOB 1 |
| 1060 | #else |
| 1061 | # define DEBUG_GLOB 0 |
| 1062 | #endif |
| 1063 | |
| 1064 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1065 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1066 | #endif |
| 1067 | |
| 1068 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1069 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1070 | #endif |
| 1071 | |
| 1072 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1073 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1074 | # define DEBUG_CLEAN 1 |
| 1075 | #else |
| 1076 | # define DEBUG_CLEAN 0 |
| 1077 | #endif |
| 1078 | |
| 1079 | #if DEBUG_EXPAND |
| 1080 | static void debug_print_strings(const char *prefix, char **vv) |
| 1081 | { |
| 1082 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1083 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1084 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1085 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1086 | } |
| 1087 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1088 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1089 | #endif |
| 1090 | |
| 1091 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1092 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1093 | */ |
| 1094 | #if LEAK_HUNTING |
| 1095 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1096 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1097 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1098 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1099 | return ptr; |
| 1100 | } |
| 1101 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1102 | { |
| 1103 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1104 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1105 | return ptr; |
| 1106 | } |
| 1107 | static char *xxstrdup(int lineno, const char *str) |
| 1108 | { |
| 1109 | char *ptr = xstrdup(str); |
| 1110 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1111 | return ptr; |
| 1112 | } |
| 1113 | static void xxfree(void *ptr) |
| 1114 | { |
| 1115 | fdprintf(2, "free %p\n", ptr); |
| 1116 | free(ptr); |
| 1117 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1118 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1119 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1120 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1121 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1122 | #endif |
| 1123 | |
| 1124 | |
| 1125 | /* Syntax and runtime errors. They always abort scripts. |
| 1126 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1127 | * and return to the prompt. |
| 1128 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1129 | */ |
| 1130 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1131 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1132 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1133 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1134 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1135 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1136 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1137 | #endif |
| 1138 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1139 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1140 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1141 | va_list p; |
| 1142 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1143 | #if HUSH_DEBUG >= 2 |
| 1144 | bb_error_msg("hush.c:%u", lineno); |
| 1145 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1146 | va_start(p, fmt); |
| 1147 | bb_verror_msg(fmt, p, NULL); |
| 1148 | va_end(p); |
| 1149 | if (!G_interactive_fd) |
| 1150 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1151 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1152 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1153 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1154 | { |
| 1155 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1156 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1157 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1158 | bb_error_msg("syntax error"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1161 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1162 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1163 | bb_error_msg("syntax error at '%s'", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1166 | 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] | 1167 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1168 | bb_error_msg("syntax error: unterminated %s", s); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1169 | } |
| 1170 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1171 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1172 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1173 | char msg[2] = { ch, '\0' }; |
| 1174 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1177 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1178 | { |
| 1179 | char msg[2]; |
| 1180 | msg[0] = ch; |
| 1181 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1182 | #if HUSH_DEBUG >= 2 |
| 1183 | bb_error_msg("hush.c:%u", lineno); |
| 1184 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1185 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1188 | #if HUSH_DEBUG < 2 |
| 1189 | # undef die_if_script |
| 1190 | # undef syntax_error |
| 1191 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1192 | # undef syntax_error_unterm_ch |
| 1193 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1194 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1195 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1196 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1197 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1198 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1199 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1200 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1201 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1202 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1203 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1204 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1205 | #if ENABLE_HUSH_INTERACTIVE |
| 1206 | static void cmdedit_update_prompt(void); |
| 1207 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1208 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1209 | #endif |
| 1210 | |
| 1211 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1212 | /* Utility functions |
| 1213 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1214 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1215 | static char *unbackslash(char *src) |
| 1216 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1217 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1218 | while (1) { |
| 1219 | if (*src == '\\') |
| 1220 | src++; |
| 1221 | if ((*dst++ = *src++) == '\0') |
| 1222 | break; |
| 1223 | } |
| 1224 | return dst; |
| 1225 | } |
| 1226 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1227 | 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] | 1228 | { |
| 1229 | int i; |
| 1230 | unsigned count1; |
| 1231 | unsigned count2; |
| 1232 | char **v; |
| 1233 | |
| 1234 | v = strings; |
| 1235 | count1 = 0; |
| 1236 | if (v) { |
| 1237 | while (*v) { |
| 1238 | count1++; |
| 1239 | v++; |
| 1240 | } |
| 1241 | } |
| 1242 | count2 = 0; |
| 1243 | v = add; |
| 1244 | while (*v) { |
| 1245 | count2++; |
| 1246 | v++; |
| 1247 | } |
| 1248 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1249 | v[count1 + count2] = NULL; |
| 1250 | i = count2; |
| 1251 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1252 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1253 | return v; |
| 1254 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1255 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1256 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1257 | { |
| 1258 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1259 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1260 | return ptr; |
| 1261 | } |
| 1262 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1263 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1264 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1265 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1266 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1267 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1268 | { |
| 1269 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1270 | v[0] = add; |
| 1271 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1272 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1273 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1274 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1275 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1276 | { |
| 1277 | char **ptr = add_string_to_strings(strings, add); |
| 1278 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1279 | return ptr; |
| 1280 | } |
| 1281 | #define add_string_to_strings(strings, add) \ |
| 1282 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1283 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1284 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1285 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1286 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1287 | char **v; |
| 1288 | |
| 1289 | if (!strings) |
| 1290 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1291 | v = strings; |
| 1292 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1293 | free(*v); |
| 1294 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1295 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1296 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1299 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1300 | static int xdup_and_close(int fd, int F_DUPFD_maybe_CLOEXEC) |
| 1301 | { |
| 1302 | /* We avoid taking stdio fds. Mimicking ash: use fds above 9 */ |
| 1303 | int newfd = fcntl(fd, F_DUPFD_maybe_CLOEXEC, 10); |
| 1304 | if (newfd < 0) { |
| 1305 | /* fd was not open? */ |
| 1306 | if (errno == EBADF) |
| 1307 | return fd; |
| 1308 | xfunc_die(); |
| 1309 | } |
| 1310 | close(fd); |
| 1311 | return newfd; |
| 1312 | } |
| 1313 | |
| 1314 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1315 | /* Manipulating the list of open FILEs */ |
| 1316 | static FILE *remember_FILE(FILE *fp) |
| 1317 | { |
| 1318 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1319 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1320 | n->next = G.FILE_list; |
| 1321 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1322 | n->fp = fp; |
| 1323 | n->fd = fileno(fp); |
| 1324 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1325 | } |
| 1326 | return fp; |
| 1327 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1328 | static void fclose_and_forget(FILE *fp) |
| 1329 | { |
| 1330 | struct FILE_list **pp = &G.FILE_list; |
| 1331 | while (*pp) { |
| 1332 | struct FILE_list *cur = *pp; |
| 1333 | if (cur->fp == fp) { |
| 1334 | *pp = cur->next; |
| 1335 | free(cur); |
| 1336 | break; |
| 1337 | } |
| 1338 | pp = &cur->next; |
| 1339 | } |
| 1340 | fclose(fp); |
| 1341 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1342 | static int save_FILEs_on_redirect(int fd) |
| 1343 | { |
| 1344 | struct FILE_list *fl = G.FILE_list; |
| 1345 | while (fl) { |
| 1346 | if (fd == fl->fd) { |
| 1347 | /* We use it only on script files, they are all CLOEXEC */ |
| 1348 | fl->fd = xdup_and_close(fd, F_DUPFD_CLOEXEC); |
| 1349 | return 1; |
| 1350 | } |
| 1351 | fl = fl->next; |
| 1352 | } |
| 1353 | return 0; |
| 1354 | } |
| 1355 | static void restore_redirected_FILEs(void) |
| 1356 | { |
| 1357 | struct FILE_list *fl = G.FILE_list; |
| 1358 | while (fl) { |
| 1359 | int should_be = fileno(fl->fp); |
| 1360 | if (fl->fd != should_be) { |
| 1361 | xmove_fd(fl->fd, should_be); |
| 1362 | fl->fd = should_be; |
| 1363 | } |
| 1364 | fl = fl->next; |
| 1365 | } |
| 1366 | } |
| 1367 | #if ENABLE_FEATURE_SH_STANDALONE |
| 1368 | static void close_all_FILE_list(void) |
| 1369 | { |
| 1370 | struct FILE_list *fl = G.FILE_list; |
| 1371 | while (fl) { |
| 1372 | /* fclose would also free FILE object. |
| 1373 | * It is disastrous if we share memory with a vforked parent. |
| 1374 | * I'm not sure we never come here after vfork. |
| 1375 | * Therefore just close fd, nothing more. |
| 1376 | */ |
| 1377 | /*fclose(fl->fp); - unsafe */ |
| 1378 | close(fl->fd); |
| 1379 | fl = fl->next; |
| 1380 | } |
| 1381 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1382 | #endif |
| 1383 | |
| 1384 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1385 | /* Helpers for setting new $n and restoring them back |
| 1386 | */ |
| 1387 | typedef struct save_arg_t { |
| 1388 | char *sv_argv0; |
| 1389 | char **sv_g_argv; |
| 1390 | int sv_g_argc; |
| 1391 | smallint sv_g_malloced; |
| 1392 | } save_arg_t; |
| 1393 | |
| 1394 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1395 | { |
| 1396 | int n; |
| 1397 | |
| 1398 | sv->sv_argv0 = argv[0]; |
| 1399 | sv->sv_g_argv = G.global_argv; |
| 1400 | sv->sv_g_argc = G.global_argc; |
| 1401 | sv->sv_g_malloced = G.global_args_malloced; |
| 1402 | |
| 1403 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1404 | G.global_argv = argv; |
| 1405 | G.global_args_malloced = 0; |
| 1406 | |
| 1407 | n = 1; |
| 1408 | while (*++argv) |
| 1409 | n++; |
| 1410 | G.global_argc = n; |
| 1411 | } |
| 1412 | |
| 1413 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1414 | { |
| 1415 | char **pp; |
| 1416 | |
| 1417 | if (G.global_args_malloced) { |
| 1418 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1419 | pp = G.global_argv; |
| 1420 | while (*++pp) /* note: does not free $0 */ |
| 1421 | free(*pp); |
| 1422 | free(G.global_argv); |
| 1423 | } |
| 1424 | argv[0] = sv->sv_argv0; |
| 1425 | G.global_argv = sv->sv_g_argv; |
| 1426 | G.global_argc = sv->sv_g_argc; |
| 1427 | G.global_args_malloced = sv->sv_g_malloced; |
| 1428 | } |
| 1429 | |
| 1430 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1431 | /* Basic theory of signal handling in shell |
| 1432 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1433 | * This does not describe what hush does, rather, it is current understanding |
| 1434 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1435 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1436 | * |
| 1437 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1438 | * is finished or backgrounded. It is the same in interactive and |
| 1439 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1440 | * 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] | 1441 | * ^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] | 1442 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1443 | * pipe. |
| 1444 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1445 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1446 | * or by SIGINT in interactive shell. |
| 1447 | * |
| 1448 | * Trap handlers will execute even within trap handlers. (right?) |
| 1449 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1450 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1451 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1452 | * |
| 1453 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1454 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1455 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1456 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1457 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1458 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1459 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1460 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1461 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1462 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1463 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1464 | * |
| 1465 | * SIGQUIT: ignore |
| 1466 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1467 | * SIGHUP (interactive): |
| 1468 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1469 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1470 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1471 | * that all pipe members are stopped. Try this in bash: |
| 1472 | * while :; do :; done - ^Z does not background it |
| 1473 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1474 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1475 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1476 | * to interactive shell while shell is waiting for a pipe, |
| 1477 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1478 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1479 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1480 | * Example 2: this does not wait and does not execute ls: |
| 1481 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1482 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1483 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1484 | * Example 4: this does not wait and does not execute ls: |
| 1485 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1486 | * |
| 1487 | * (What happens to signals which are IGN on shell start?) |
| 1488 | * (What happens with signal mask on shell start?) |
| 1489 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1490 | * Old implementation |
| 1491 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1492 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1493 | * We block all signals which we don't want to take action immediately, |
| 1494 | * i.e. we block all signals which need to have special handling as described |
| 1495 | * above, and all signals which have traps set. |
| 1496 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1497 | * and act on them. |
| 1498 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1499 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1500 | * sigset_t blocked_set: current blocked signal set |
| 1501 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1502 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1503 | * 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] | 1504 | * "trap 'cmd' SIGxxx": |
| 1505 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1506 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1507 | * unblock signals with special interactive handling |
| 1508 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1509 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1510 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1511 | * 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] | 1512 | * 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] | 1513 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1514 | * 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] | 1515 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1516 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1517 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1518 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1519 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1520 | * 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] | 1521 | * 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] | 1522 | * |
| 1523 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1524 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1525 | * masked signals are not visible! |
| 1526 | * |
| 1527 | * New implementation |
| 1528 | * ================== |
| 1529 | * We record each signal we are interested in by installing signal handler |
| 1530 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1531 | * We are interested in: signals which need to have special handling |
| 1532 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1533 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1534 | * After each pipe execution, we extract any pending signals |
| 1535 | * and act on them. |
| 1536 | * |
| 1537 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1538 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1539 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1540 | * sigset_t pending_set: set of sigs we received. |
| 1541 | * |
| 1542 | * "trap - SIGxxx": |
| 1543 | * if sig is in special_sig_mask, set handler back to: |
| 1544 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1545 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1546 | * else: set handler back to SIG_DFL |
| 1547 | * "trap 'cmd' SIGxxx": |
| 1548 | * set handler to record_pending_signo. |
| 1549 | * "trap '' SIGxxx": |
| 1550 | * set handler to SIG_IGN. |
| 1551 | * after [v]fork, if we plan to be a shell: |
| 1552 | * set signals with special interactive handling to SIG_DFL |
| 1553 | * (because child shell is not interactive), |
| 1554 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1555 | * after [v]fork, if we plan to exec: |
| 1556 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1557 | * |
| 1558 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1559 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1560 | * |
| 1561 | * Note (compat): |
| 1562 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1563 | * are set to the default actions". bash interprets it so that traps which |
| 1564 | * 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] | 1565 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1566 | enum { |
| 1567 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1568 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1569 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1570 | | (1 << SIGHUP) |
| 1571 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1572 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1573 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1574 | | (1 << SIGTTIN) |
| 1575 | | (1 << SIGTTOU) |
| 1576 | | (1 << SIGTSTP) |
| 1577 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1578 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1579 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1580 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1581 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1582 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1583 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1584 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1585 | if (sig == SIGCHLD) { |
| 1586 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1587 | //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] | 1588 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1589 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1590 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1591 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1592 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1593 | { |
| 1594 | struct sigaction old_sa; |
| 1595 | |
| 1596 | /* We could use signal() to install handlers... almost: |
| 1597 | * except that we need to mask ALL signals while handlers run. |
| 1598 | * I saw signal nesting in strace, race window isn't small. |
| 1599 | * SA_RESTART is also needed, but in Linux, signal() |
| 1600 | * sets SA_RESTART too. |
| 1601 | */ |
| 1602 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1603 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1604 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1605 | G.sa.sa_handler = handler; |
| 1606 | sigaction(sig, &G.sa, &old_sa); |
| 1607 | return old_sa.sa_handler; |
| 1608 | } |
| 1609 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1610 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1611 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1612 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1613 | static void restore_ttypgrp_and__exit(void) |
| 1614 | { |
| 1615 | /* xfunc has failed! die die die */ |
| 1616 | /* no EXIT traps, this is an escape hatch! */ |
| 1617 | G.exiting = 1; |
| 1618 | hush_exit(xfunc_error_retval); |
| 1619 | } |
| 1620 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1621 | #if ENABLE_HUSH_JOB |
| 1622 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1623 | /* Needed only on some libc: |
| 1624 | * It was observed that on exit(), fgetc'ed buffered data |
| 1625 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1626 | * With the net effect that even after fork(), not vfork(), |
| 1627 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1628 | * noexec_applet_here |
| 1629 | * echo END_OF_SCRIPT |
| 1630 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1631 | * This makes "echo END_OF_SCRIPT" executed twice. |
| 1632 | * Similar problems can be seen with die_if_script() -> xfunc_die() |
| 1633 | * and in `cmd` handling. |
| 1634 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1635 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1636 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1637 | static void fflush_and__exit(void) |
| 1638 | { |
| 1639 | fflush_all(); |
| 1640 | _exit(xfunc_error_retval); |
| 1641 | } |
| 1642 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1643 | /* 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] | 1644 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1645 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1646 | # 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] | 1647 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1648 | /* Restores tty foreground process group, and exits. |
| 1649 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1650 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1651 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1652 | * We also call it if xfunc is exiting. |
| 1653 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1654 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1655 | static void sigexit(int sig) |
| 1656 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1657 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1658 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1659 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1660 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1661 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1662 | */ |
| 1663 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1664 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1665 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* Not a signal, just exit */ |
| 1668 | if (sig <= 0) |
| 1669 | _exit(- sig); |
| 1670 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1671 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1672 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1673 | #else |
| 1674 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1675 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1676 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1677 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1678 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1679 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1680 | static sighandler_t pick_sighandler(unsigned sig) |
| 1681 | { |
| 1682 | sighandler_t handler = SIG_DFL; |
| 1683 | if (sig < sizeof(unsigned)*8) { |
| 1684 | unsigned sigmask = (1 << sig); |
| 1685 | |
| 1686 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1687 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1688 | if (G_fatal_sig_mask & sigmask) |
| 1689 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1690 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1691 | #endif |
| 1692 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1693 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1694 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1695 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1696 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1697 | * in an endless loop when we try to do some |
| 1698 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1699 | */ |
| 1700 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1701 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1702 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1703 | } |
| 1704 | return handler; |
| 1705 | } |
| 1706 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1707 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1708 | static void hush_exit(int exitcode) |
| 1709 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1710 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1711 | save_history(G.line_input_state); |
| 1712 | #endif |
| 1713 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1714 | fflush_all(); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1715 | 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] | 1716 | char *argv[3]; |
| 1717 | /* argv[0] is unused */ |
| 1718 | argv[1] = G.traps[0]; |
| 1719 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1720 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1721 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1722 | * "trap" will still show it, if executed |
| 1723 | * in the handler */ |
| 1724 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1725 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1726 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1727 | #if ENABLE_FEATURE_CLEAN_UP |
| 1728 | { |
| 1729 | struct variable *cur_var; |
| 1730 | if (G.cwd != bb_msg_unknown) |
| 1731 | free((char*)G.cwd); |
| 1732 | cur_var = G.top_var; |
| 1733 | while (cur_var) { |
| 1734 | struct variable *tmp = cur_var; |
| 1735 | if (!cur_var->max_len) |
| 1736 | free(cur_var->varstr); |
| 1737 | cur_var = cur_var->next; |
| 1738 | free(tmp); |
| 1739 | } |
| 1740 | } |
| 1741 | #endif |
| 1742 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1743 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1744 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1745 | sigexit(- (exitcode & 0xff)); |
| 1746 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1747 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1748 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1751 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1752 | //TODO: return a mask of ALL handled sigs? |
| 1753 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1754 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1755 | int last_sig = 0; |
| 1756 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1757 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1758 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1759 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1760 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1761 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1762 | sig = 0; |
| 1763 | do { |
| 1764 | sig++; |
| 1765 | if (sigismember(&G.pending_set, sig)) { |
| 1766 | sigdelset(&G.pending_set, sig); |
| 1767 | goto got_sig; |
| 1768 | } |
| 1769 | } while (sig < NSIG); |
| 1770 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1771 | got_sig: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1772 | if (G.traps && G.traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1773 | 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] | 1774 | if (G.traps[sig][0]) { |
| 1775 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1776 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1777 | char *argv[3]; |
| 1778 | /* argv[0] is unused */ |
| 1779 | argv[1] = G.traps[sig]; |
| 1780 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1781 | save_rcode = G.last_exitcode; |
| 1782 | builtin_eval(argv); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 1783 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1784 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1785 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1786 | } /* else: "" trap, ignoring signal */ |
| 1787 | continue; |
| 1788 | } |
| 1789 | /* not a trap: special action */ |
| 1790 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1791 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1792 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1793 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1794 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | #if ENABLE_HUSH_JOB |
| 1797 | case SIGHUP: { |
| 1798 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1799 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1800 | /* bash is observed to signal whole process groups, |
| 1801 | * not individual processes */ |
| 1802 | for (job = G.job_list; job; job = job->next) { |
| 1803 | if (job->pgrp <= 0) |
| 1804 | continue; |
| 1805 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1806 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1807 | kill(- job->pgrp, SIGCONT); |
| 1808 | } |
| 1809 | sigexit(SIGHUP); |
| 1810 | } |
| 1811 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1812 | #if ENABLE_HUSH_FAST |
| 1813 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1814 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1815 | G.count_SIGCHLD++; |
| 1816 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1817 | /* Note: |
| 1818 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1819 | * This simplifies wait builtin a bit. |
| 1820 | */ |
| 1821 | break; |
| 1822 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1823 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1824 | 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] | 1825 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1826 | /* Note: |
| 1827 | * We dont do 'last_sig = sig' here -> NOT returning this sig. |
| 1828 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1829 | * in interactive shell, because TERM is ignored. |
| 1830 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | } |
| 1834 | return last_sig; |
| 1835 | } |
| 1836 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1837 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1838 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1839 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1840 | if (force || G.cwd == NULL) { |
| 1841 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1842 | * we must not try to free(bb_msg_unknown) */ |
| 1843 | if (G.cwd == bb_msg_unknown) |
| 1844 | G.cwd = NULL; |
| 1845 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1846 | if (!G.cwd) |
| 1847 | G.cwd = bb_msg_unknown; |
| 1848 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1849 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1852 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1853 | /* |
| 1854 | * Shell and environment variable support |
| 1855 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1856 | 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] | 1857 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1858 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1859 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1860 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1861 | pp = &G.top_var; |
| 1862 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1863 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1864 | return pp; |
| 1865 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1866 | } |
| 1867 | return NULL; |
| 1868 | } |
| 1869 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1870 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1871 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1872 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1873 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1874 | |
| 1875 | if (G.expanded_assignments) { |
| 1876 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1877 | while (*cpp) { |
| 1878 | char *cp = *cpp; |
| 1879 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1880 | return cp + len + 1; |
| 1881 | cpp++; |
| 1882 | } |
| 1883 | } |
| 1884 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1885 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1886 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1887 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1888 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1889 | if (strcmp(name, "PPID") == 0) |
| 1890 | return utoa(G.root_ppid); |
| 1891 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1892 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1893 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1894 | return utoa(next_random(&G.random_gen)); |
| 1895 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1896 | return NULL; |
| 1897 | } |
| 1898 | |
| 1899 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1900 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1901 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1902 | * 0: do not change export flag |
| 1903 | * (if creating new variable, flag will be 0) |
| 1904 | * 1: set export flag and putenv the variable |
| 1905 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1906 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1907 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1908 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1909 | /* all params are used */ |
| 1910 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1911 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1912 | set_local_var(str, flg_export, local_lvl) |
| 1913 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1914 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1915 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1916 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1917 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1918 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1919 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1920 | 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] | 1921 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1922 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1923 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1924 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1925 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1926 | int name_len; |
| 1927 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1928 | eq_sign = strchr(str, '='); |
| 1929 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1930 | free(str); |
| 1931 | return -1; |
| 1932 | } |
| 1933 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1934 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1935 | var_pp = &G.top_var; |
| 1936 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1937 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1938 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1939 | continue; |
| 1940 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1941 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1942 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1943 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1944 | #if !BB_MMU |
| 1945 | if (!flg_read_only) |
| 1946 | #endif |
| 1947 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1948 | free(str); |
| 1949 | return -1; |
| 1950 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1951 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1952 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1953 | *eq_sign = '\0'; |
| 1954 | unsetenv(str); |
| 1955 | *eq_sign = '='; |
| 1956 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1957 | #if ENABLE_HUSH_LOCAL |
| 1958 | if (cur->func_nest_level < local_lvl) { |
| 1959 | /* New variable is declared as local, |
| 1960 | * and existing one is global, or local |
| 1961 | * from enclosing function. |
| 1962 | * Remove and save old one: */ |
| 1963 | *var_pp = cur->next; |
| 1964 | cur->next = *G.shadowed_vars_pp; |
| 1965 | *G.shadowed_vars_pp = cur; |
| 1966 | /* bash 3.2.33(1) and exported vars: |
| 1967 | * # export z=z |
| 1968 | * # f() { local z=a; env | grep ^z; } |
| 1969 | * # f |
| 1970 | * z=a |
| 1971 | * # env | grep ^z |
| 1972 | * z=z |
| 1973 | */ |
| 1974 | if (cur->flg_export) |
| 1975 | flg_export = 1; |
| 1976 | break; |
| 1977 | } |
| 1978 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1979 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1980 | free_and_exp: |
| 1981 | free(str); |
| 1982 | goto exp; |
| 1983 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1984 | if (cur->max_len != 0) { |
| 1985 | if (cur->max_len >= strlen(str)) { |
| 1986 | /* This one is from startup env, reuse space */ |
| 1987 | strcpy(cur->varstr, str); |
| 1988 | goto free_and_exp; |
| 1989 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1990 | /* Can't reuse */ |
| 1991 | cur->max_len = 0; |
| 1992 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1993 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 1994 | /* max_len == 0 signifies "malloced" var, which we can |
| 1995 | * (and have to) free. But we can't free(cur->varstr) here: |
| 1996 | * if cur->flg_export is 1, it is in the environment. |
| 1997 | * We should either unsetenv+free, or wait until putenv, |
| 1998 | * then putenv(new)+free(old). |
| 1999 | */ |
| 2000 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2001 | goto set_str_and_exp; |
| 2002 | } |
| 2003 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2004 | /* Not found - create new variable struct */ |
| 2005 | cur = xzalloc(sizeof(*cur)); |
| 2006 | #if ENABLE_HUSH_LOCAL |
| 2007 | cur->func_nest_level = local_lvl; |
| 2008 | #endif |
| 2009 | cur->next = *var_pp; |
| 2010 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2011 | |
| 2012 | set_str_and_exp: |
| 2013 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2014 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 2015 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 2016 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2017 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2018 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2019 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2020 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2021 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2022 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2023 | if (flg_export == -1) { |
| 2024 | cur->flg_export = 0; |
| 2025 | /* unsetenv was already done */ |
| 2026 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2027 | int i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2028 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2029 | i = putenv(cur->varstr); |
| 2030 | /* only now we can free old exported malloced string */ |
| 2031 | free(free_me); |
| 2032 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2033 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2034 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2035 | free(free_me); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2036 | return 0; |
| 2037 | } |
| 2038 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2039 | /* Used at startup and after each cd */ |
| 2040 | static void set_pwd_var(int exp) |
| 2041 | { |
| 2042 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 2043 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 2044 | } |
| 2045 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2046 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2047 | { |
| 2048 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2049 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2052 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2053 | var_pp = &G.top_var; |
| 2054 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2055 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 2056 | if (cur->flg_read_only) { |
| 2057 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2058 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2059 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2060 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2061 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2062 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2063 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2064 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2065 | if (!cur->max_len) |
| 2066 | free(cur->varstr); |
| 2067 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2068 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2069 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2070 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2071 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2072 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2075 | static int unset_local_var(const char *name) |
| 2076 | { |
| 2077 | return unset_local_var_len(name, strlen(name)); |
| 2078 | } |
| 2079 | |
| 2080 | static void unset_vars(char **strings) |
| 2081 | { |
| 2082 | char **v; |
| 2083 | |
| 2084 | if (!strings) |
| 2085 | return; |
| 2086 | v = strings; |
| 2087 | while (*v) { |
| 2088 | const char *eq = strchrnul(*v, '='); |
| 2089 | unset_local_var_len(*v, (int)(eq - *v)); |
| 2090 | v++; |
| 2091 | } |
| 2092 | free(strings); |
| 2093 | } |
| 2094 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2095 | 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] | 2096 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2097 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2098 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2099 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2100 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2101 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2102 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2103 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2104 | */ |
| 2105 | static void add_vars(struct variable *var) |
| 2106 | { |
| 2107 | struct variable *next; |
| 2108 | |
| 2109 | while (var) { |
| 2110 | next = var->next; |
| 2111 | var->next = G.top_var; |
| 2112 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2113 | if (var->flg_export) { |
| 2114 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2115 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2116 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2117 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2118 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2119 | var = next; |
| 2120 | } |
| 2121 | } |
| 2122 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2123 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2124 | { |
| 2125 | char **s; |
| 2126 | struct variable *old = NULL; |
| 2127 | |
| 2128 | if (!strings) |
| 2129 | return old; |
| 2130 | s = strings; |
| 2131 | while (*s) { |
| 2132 | struct variable *var_p; |
| 2133 | struct variable **var_pp; |
| 2134 | char *eq; |
| 2135 | |
| 2136 | eq = strchr(*s, '='); |
| 2137 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2138 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2139 | if (var_pp) { |
| 2140 | /* Remove variable from global linked list */ |
| 2141 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2142 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2143 | *var_pp = var_p->next; |
| 2144 | /* Add it to returned list */ |
| 2145 | var_p->next = old; |
| 2146 | old = var_p; |
| 2147 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2148 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2149 | } |
| 2150 | s++; |
| 2151 | } |
| 2152 | return old; |
| 2153 | } |
| 2154 | |
| 2155 | |
| 2156 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2157 | * Unicode helper |
| 2158 | */ |
| 2159 | static void reinit_unicode_for_hush(void) |
| 2160 | { |
| 2161 | /* Unicode support should be activated even if LANG is set |
| 2162 | * _during_ shell execution, not only if it was set when |
| 2163 | * shell was started. Therefore, re-check LANG every time: |
| 2164 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2165 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2166 | || ENABLE_UNICODE_USING_LOCALE |
| 2167 | ) { |
| 2168 | const char *s = get_local_var_value("LC_ALL"); |
| 2169 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2170 | if (!s) s = get_local_var_value("LANG"); |
| 2171 | reinit_unicode(s); |
| 2172 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2173 | } |
| 2174 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2175 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2176 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2177 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2178 | |
| 2179 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2180 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2181 | * echo $P\ |
| 2182 | * \ |
| 2183 | * AT\ |
| 2184 | * H\ |
| 2185 | * \ |
| 2186 | * It excercises a lot of corner cases. |
| 2187 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2188 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2189 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2190 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2191 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2192 | if (G.PS1 == NULL) |
| 2193 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2194 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2195 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2196 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2197 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2198 | if (G.PS2 == NULL) |
| 2199 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2200 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2201 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2202 | { |
| 2203 | const char *prompt_str; |
| 2204 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2205 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2206 | /* Set up the prompt */ |
| 2207 | if (promptmode == 0) { /* PS1 */ |
| 2208 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2209 | /* bash uses $PWD value, even if it is set by user. |
| 2210 | * It uses current dir only if PWD is unset. |
| 2211 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2212 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2213 | prompt_str = G.PS1; |
| 2214 | } else |
| 2215 | prompt_str = G.PS2; |
| 2216 | } else |
| 2217 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2218 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2219 | return prompt_str; |
| 2220 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2221 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2222 | { |
| 2223 | int r; |
| 2224 | const char *prompt_str; |
| 2225 | |
| 2226 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2227 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2228 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2229 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2230 | if (G.flag_SIGINT) { |
| 2231 | /* There was ^C'ed, make it look prettier: */ |
| 2232 | bb_putchar('\n'); |
| 2233 | G.flag_SIGINT = 0; |
| 2234 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2235 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2236 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2237 | r = read_line_input(G.line_input_state, prompt_str, |
| 2238 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, |
| 2239 | /*timeout*/ -1 |
| 2240 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2241 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
| 2242 | if (r == 0) { |
| 2243 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2244 | raise(SIGINT); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2245 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2246 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2247 | if (r != 0 && !G.flag_SIGINT) |
| 2248 | break; |
| 2249 | /* ^C or SIGINT: repeat */ |
| 2250 | G.last_exitcode = 128 + SIGINT; |
| 2251 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2252 | if (r < 0) { |
| 2253 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2254 | i->p = NULL; |
| 2255 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2256 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2257 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2258 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2259 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2260 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2261 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2262 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2263 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2264 | /* Why check_and_run_traps here? Try this interactively: |
| 2265 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2266 | * $ <[enter], repeatedly...> |
| 2267 | * Without check_and_run_traps, handler never runs. |
| 2268 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2269 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2270 | fputs(prompt_str, stdout); |
| 2271 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2272 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2273 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2274 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2275 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2276 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2277 | * it generates SIGINT as usual. |
| 2278 | */ |
| 2279 | check_and_run_traps(); |
| 2280 | if (G.flag_SIGINT) |
| 2281 | G.last_exitcode = 128 + SIGINT; |
| 2282 | if (r != '\0') |
| 2283 | break; |
| 2284 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2285 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2286 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2287 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2288 | /* This is the magic location that prints prompts |
| 2289 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2290 | static int fgetc_interactive(struct in_str *i) |
| 2291 | { |
| 2292 | int ch; |
| 2293 | /* If it's interactive stdin, get new line. */ |
| 2294 | if (G_interactive_fd && i->file == stdin) { |
| 2295 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2296 | ch = get_user_input(i); |
| 2297 | i->promptmode = 1; /* PS2 */ |
| 2298 | } else { |
| 2299 | /* Not stdin: script file, sourced file, etc */ |
| 2300 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2301 | } |
| 2302 | return ch; |
| 2303 | } |
| 2304 | #else |
| 2305 | static inline int fgetc_interactive(struct in_str *i) |
| 2306 | { |
| 2307 | int ch; |
| 2308 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2309 | return ch; |
| 2310 | } |
| 2311 | #endif /* INTERACTIVE */ |
| 2312 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2313 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2314 | { |
| 2315 | int ch; |
| 2316 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2317 | if (!i->file) { |
| 2318 | /* string-based in_str */ |
| 2319 | ch = (unsigned char)*i->p; |
| 2320 | if (ch != '\0') { |
| 2321 | i->p++; |
| 2322 | i->last_char = ch; |
| 2323 | return ch; |
| 2324 | } |
| 2325 | return EOF; |
| 2326 | } |
| 2327 | |
| 2328 | /* FILE-based in_str */ |
| 2329 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2330 | #if ENABLE_FEATURE_EDITING |
| 2331 | /* This can be stdin, check line editing char[] buffer */ |
| 2332 | if (i->p && *i->p != '\0') { |
| 2333 | ch = (unsigned char)*i->p++; |
| 2334 | goto out; |
| 2335 | } |
| 2336 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2337 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2338 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2339 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2340 | int ch2 = i->peek_buf[1]; |
| 2341 | i->peek_buf[0] = ch2; |
| 2342 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2343 | goto out; |
| 2344 | i->peek_buf[1] = 0; |
| 2345 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2346 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2347 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2348 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2349 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2350 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2351 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2352 | return ch; |
| 2353 | } |
| 2354 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2355 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2356 | { |
| 2357 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2358 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2359 | if (!i->file) { |
| 2360 | /* string-based in_str */ |
| 2361 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2362 | return (unsigned char)*i->p; |
| 2363 | } |
| 2364 | |
| 2365 | /* FILE-based in_str */ |
| 2366 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2367 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2368 | /* This can be stdin, check line editing char[] buffer */ |
| 2369 | if (i->p && *i->p != '\0') |
| 2370 | return (unsigned char)*i->p; |
| 2371 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2372 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2373 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2374 | if (ch != 0) |
| 2375 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2376 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2377 | /* Need to get a new char */ |
| 2378 | ch = fgetc_interactive(i); |
| 2379 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2380 | |
| 2381 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2382 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2383 | if (i->p) { |
| 2384 | i->p -= 1; |
| 2385 | return ch; |
| 2386 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2387 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2388 | i->peek_buf[0] = ch; |
| 2389 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2390 | return ch; |
| 2391 | } |
| 2392 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2393 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2394 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2395 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2396 | */ |
| 2397 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2398 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2399 | int ch; |
| 2400 | |
| 2401 | /* There are two cases when i->p[] buffer exists. |
| 2402 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2403 | * (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] | 2404 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2405 | * the peek2 result is in i->p[1]. |
| 2406 | */ |
| 2407 | if (i->p) |
| 2408 | return (unsigned char)i->p[1]; |
| 2409 | |
| 2410 | /* Now we know it is a file-based in_str. */ |
| 2411 | |
| 2412 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2413 | /* Is there 2nd char? */ |
| 2414 | ch = i->peek_buf[1]; |
| 2415 | if (ch == 0) { |
| 2416 | /* We did not read it yet, get it now */ |
| 2417 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2418 | i->peek_buf[1] = ch; |
| 2419 | } |
| 2420 | |
| 2421 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2422 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2425 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2426 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2427 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2428 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2429 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2430 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2434 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2435 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2436 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2437 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2438 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2442 | /* |
| 2443 | * o_string support |
| 2444 | */ |
| 2445 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2446 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2447 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2448 | { |
| 2449 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2450 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2451 | if (o->data) |
| 2452 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2455 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2456 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2457 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2458 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2461 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2462 | { |
| 2463 | free(o->data); |
| 2464 | } |
| 2465 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2466 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2467 | { |
| 2468 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2469 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2470 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2471 | } |
| 2472 | } |
| 2473 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2474 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2475 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2476 | 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] | 2477 | if (o->length < o->maxlen) { |
| 2478 | /* likely. avoid o_grow_by() call */ |
| 2479 | add: |
| 2480 | o->data[o->length] = ch; |
| 2481 | o->length++; |
| 2482 | o->data[o->length] = '\0'; |
| 2483 | return; |
| 2484 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2485 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2486 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2489 | #if 0 |
| 2490 | /* Valid only if we know o_string is not empty */ |
| 2491 | static void o_delchr(o_string *o) |
| 2492 | { |
| 2493 | o->length--; |
| 2494 | o->data[o->length] = '\0'; |
| 2495 | } |
| 2496 | #endif |
| 2497 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2498 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2499 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2500 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2501 | memcpy(&o->data[o->length], str, len); |
| 2502 | o->length += len; |
| 2503 | o->data[o->length] = '\0'; |
| 2504 | } |
| 2505 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2506 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2507 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2508 | o_addblock(o, str, strlen(str)); |
| 2509 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2510 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2511 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2512 | static void nommu_addchr(o_string *o, int ch) |
| 2513 | { |
| 2514 | if (o) |
| 2515 | o_addchr(o, ch); |
| 2516 | } |
| 2517 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2518 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2519 | #endif |
| 2520 | |
| 2521 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2522 | { |
| 2523 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2526 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2527 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2528 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2529 | * Apparently, on unquoted $v bash still does globbing |
| 2530 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2531 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2532 | * quoting mechanisms on $v expansion side: one protects |
| 2533 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2534 | * We have only second one. |
| 2535 | */ |
| 2536 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2537 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2538 | # define MAYBE_BRACES "{}" |
| 2539 | #else |
| 2540 | # define MAYBE_BRACES "" |
| 2541 | #endif |
| 2542 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2543 | /* My analysis of quoting semantics tells me that state information |
| 2544 | * is associated with a destination, not a source. |
| 2545 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2546 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2547 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2548 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2549 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2550 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2551 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2552 | o_grow_by(o, sz); |
| 2553 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2554 | o->data[o->length] = '\\'; |
| 2555 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2556 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2557 | o->data[o->length] = ch; |
| 2558 | o->length++; |
| 2559 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2562 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2563 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2564 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2565 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2566 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2567 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2568 | sz++; |
| 2569 | o->data[o->length] = '\\'; |
| 2570 | o->length++; |
| 2571 | } |
| 2572 | o_grow_by(o, sz); |
| 2573 | o->data[o->length] = ch; |
| 2574 | o->length++; |
| 2575 | o->data[o->length] = '\0'; |
| 2576 | } |
| 2577 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2578 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2579 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2580 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2581 | char ch; |
| 2582 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2583 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2584 | if (ordinary_cnt > len) /* paranoia */ |
| 2585 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2586 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2587 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2588 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2589 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2590 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2591 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2592 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2593 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2594 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2595 | sz++; |
| 2596 | o->data[o->length] = '\\'; |
| 2597 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2598 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2599 | o_grow_by(o, sz); |
| 2600 | o->data[o->length] = ch; |
| 2601 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2602 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2603 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2606 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2607 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2608 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2609 | o_addblock(o, str, len); |
| 2610 | return; |
| 2611 | } |
| 2612 | o_addqblock(o, str, len); |
| 2613 | } |
| 2614 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2615 | static void o_addQstr(o_string *o, const char *str) |
| 2616 | { |
| 2617 | o_addQblock(o, str, strlen(str)); |
| 2618 | } |
| 2619 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2620 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2621 | * 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] | 2622 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2623 | * list[i] contains an INDEX (int!) into this string data. |
| 2624 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2625 | * but list[i]'s need not be modified. |
| 2626 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2627 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2628 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2629 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2630 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2631 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2632 | { |
| 2633 | char **list = (char**)o->data; |
| 2634 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2635 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2636 | |
| 2637 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2638 | 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] | 2639 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2640 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2641 | o->has_quoted_part, |
| 2642 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2643 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2644 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2645 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2646 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2647 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2648 | i++; |
| 2649 | } |
| 2650 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2651 | 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] | 2652 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2653 | 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] | 2654 | } |
| 2655 | } |
| 2656 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2657 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2658 | #endif |
| 2659 | |
| 2660 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2661 | * in list[n] so that it points past last stored byte so far. |
| 2662 | * It returns n+1. */ |
| 2663 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2664 | { |
| 2665 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2666 | int string_start; |
| 2667 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2668 | |
| 2669 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2670 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2671 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2672 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2673 | 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] | 2674 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2675 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2676 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2677 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2678 | memmove(list + n + 0x10, list + n, string_len); |
| 2679 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2680 | } else { |
| 2681 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2682 | n, string_len, string_start); |
| 2683 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2684 | } else { |
| 2685 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2686 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2687 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2688 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2689 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2690 | o->has_empty_slot = 0; |
| 2691 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2692 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2693 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2694 | return n + 1; |
| 2695 | } |
| 2696 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2697 | /* "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] | 2698 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2699 | { |
| 2700 | char **list = (char**)o->data; |
| 2701 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2702 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2703 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2706 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2707 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2708 | * first, it processes even {a} (no commas), second, |
| 2709 | * 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] | 2710 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2711 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2712 | |
| 2713 | /* Helper */ |
| 2714 | static int glob_needed(const char *s) |
| 2715 | { |
| 2716 | while (*s) { |
| 2717 | if (*s == '\\') { |
| 2718 | if (!s[1]) |
| 2719 | return 0; |
| 2720 | s += 2; |
| 2721 | continue; |
| 2722 | } |
| 2723 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2724 | return 1; |
| 2725 | s++; |
| 2726 | } |
| 2727 | return 0; |
| 2728 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2729 | /* Return pointer to next closing brace or to comma */ |
| 2730 | static const char *next_brace_sub(const char *cp) |
| 2731 | { |
| 2732 | unsigned depth = 0; |
| 2733 | cp++; |
| 2734 | while (*cp != '\0') { |
| 2735 | if (*cp == '\\') { |
| 2736 | if (*++cp == '\0') |
| 2737 | break; |
| 2738 | cp++; |
| 2739 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2740 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2741 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2742 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2743 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2744 | depth++; |
| 2745 | } |
| 2746 | |
| 2747 | return *cp != '\0' ? cp : NULL; |
| 2748 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2749 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2750 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2751 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2752 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2753 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2754 | const char *next; |
| 2755 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2756 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2757 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2758 | |
| 2759 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2760 | |
| 2761 | begin = pattern; |
| 2762 | while (1) { |
| 2763 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2764 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2765 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2766 | /* Find the first sub-pattern and at the same time |
| 2767 | * find the rest after the closing brace */ |
| 2768 | next = next_brace_sub(begin); |
| 2769 | if (next == NULL) { |
| 2770 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2771 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2772 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2773 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2774 | /* "{abc}" with no commas - illegal |
| 2775 | * brace expr, disregard and skip it */ |
| 2776 | begin = next + 1; |
| 2777 | continue; |
| 2778 | } |
| 2779 | break; |
| 2780 | } |
| 2781 | if (*begin == '\\' && begin[1] != '\0') |
| 2782 | begin++; |
| 2783 | begin++; |
| 2784 | } |
| 2785 | debug_printf_glob("begin:%s\n", begin); |
| 2786 | debug_printf_glob("next:%s\n", next); |
| 2787 | |
| 2788 | /* Now find the end of the whole brace expression */ |
| 2789 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2790 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2791 | rest = next_brace_sub(rest); |
| 2792 | if (rest == NULL) { |
| 2793 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2794 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2795 | } |
| 2796 | debug_printf_glob("rest:%s\n", rest); |
| 2797 | } |
| 2798 | rest_len = strlen(++rest) + 1; |
| 2799 | |
| 2800 | /* We are sure the brace expression is well-formed */ |
| 2801 | |
| 2802 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2803 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2804 | |
| 2805 | /* We have a brace expression. BEGIN points to the opening {, |
| 2806 | * NEXT points past the terminator of the first element, and REST |
| 2807 | * points past the final }. We will accumulate result names from |
| 2808 | * recursive runs for each brace alternative in the buffer using |
| 2809 | * GLOB_APPEND. */ |
| 2810 | |
| 2811 | p = begin + 1; |
| 2812 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2813 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2814 | memcpy( |
| 2815 | mempcpy( |
| 2816 | mempcpy(new_pattern_buf, |
| 2817 | /* We know the prefix for all sub-patterns */ |
| 2818 | pattern, begin - pattern), |
| 2819 | p, next - p), |
| 2820 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2821 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2822 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2823 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2824 | */ |
| 2825 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2826 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2827 | /* We saw the last entry */ |
| 2828 | break; |
| 2829 | } |
| 2830 | p = next + 1; |
| 2831 | next = next_brace_sub(next); |
| 2832 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2833 | free(new_pattern_buf); |
| 2834 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2835 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2836 | simple_glob: |
| 2837 | { |
| 2838 | int gr; |
| 2839 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2840 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2841 | memset(&globdata, 0, sizeof(globdata)); |
| 2842 | gr = glob(pattern, 0, NULL, &globdata); |
| 2843 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2844 | if (gr != 0) { |
| 2845 | if (gr == GLOB_NOMATCH) { |
| 2846 | globfree(&globdata); |
| 2847 | /* NB: garbles parameter */ |
| 2848 | unbackslash(pattern); |
| 2849 | o_addstr_with_NUL(o, pattern); |
| 2850 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2851 | return o_save_ptr_helper(o, n); |
| 2852 | } |
| 2853 | if (gr == GLOB_NOSPACE) |
| 2854 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2855 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2856 | * but we didn't specify it. Paranoia again. */ |
| 2857 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2858 | } |
| 2859 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2860 | char **argv = globdata.gl_pathv; |
| 2861 | while (1) { |
| 2862 | o_addstr_with_NUL(o, *argv); |
| 2863 | n = o_save_ptr_helper(o, n); |
| 2864 | argv++; |
| 2865 | if (!*argv) |
| 2866 | break; |
| 2867 | } |
| 2868 | } |
| 2869 | globfree(&globdata); |
| 2870 | } |
| 2871 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2872 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2873 | /* Performs globbing on last list[], |
| 2874 | * saving each result as a new list[]. |
| 2875 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2876 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2877 | { |
| 2878 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2879 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2880 | 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] | 2881 | if (!o->data) |
| 2882 | return o_save_ptr_helper(o, n); |
| 2883 | pattern = o->data + o_get_last_ptr(o, n); |
| 2884 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2885 | if (!glob_needed(pattern)) { |
| 2886 | /* unbackslash last string in o in place, fix length */ |
| 2887 | o->length = unbackslash(pattern) - o->data; |
| 2888 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2889 | return o_save_ptr_helper(o, n); |
| 2890 | } |
| 2891 | |
| 2892 | copy = xstrdup(pattern); |
| 2893 | /* "forget" pattern in o */ |
| 2894 | o->length = pattern - o->data; |
| 2895 | n = glob_brace(copy, o, n); |
| 2896 | free(copy); |
| 2897 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2898 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2899 | return n; |
| 2900 | } |
| 2901 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2902 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2903 | |
| 2904 | /* Helper */ |
| 2905 | static int glob_needed(const char *s) |
| 2906 | { |
| 2907 | while (*s) { |
| 2908 | if (*s == '\\') { |
| 2909 | if (!s[1]) |
| 2910 | return 0; |
| 2911 | s += 2; |
| 2912 | continue; |
| 2913 | } |
| 2914 | if (*s == '*' || *s == '[' || *s == '?') |
| 2915 | return 1; |
| 2916 | s++; |
| 2917 | } |
| 2918 | return 0; |
| 2919 | } |
| 2920 | /* Performs globbing on last list[], |
| 2921 | * saving each result as a new list[]. |
| 2922 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2923 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2924 | { |
| 2925 | glob_t globdata; |
| 2926 | int gr; |
| 2927 | char *pattern; |
| 2928 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2929 | 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] | 2930 | if (!o->data) |
| 2931 | return o_save_ptr_helper(o, n); |
| 2932 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2933 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2934 | if (!glob_needed(pattern)) { |
| 2935 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2936 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2937 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2938 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2939 | return o_save_ptr_helper(o, n); |
| 2940 | } |
| 2941 | |
| 2942 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2943 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2944 | * If we glob "*.\*" and don't find anything, we need |
| 2945 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2946 | * will return "*.\*"! |
| 2947 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2948 | gr = glob(pattern, 0, NULL, &globdata); |
| 2949 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2950 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2951 | if (gr == GLOB_NOMATCH) { |
| 2952 | globfree(&globdata); |
| 2953 | goto literal; |
| 2954 | } |
| 2955 | if (gr == GLOB_NOSPACE) |
| 2956 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2957 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2958 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2959 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2960 | } |
| 2961 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2962 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2963 | /* "forget" pattern in o */ |
| 2964 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2965 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2966 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2967 | n = o_save_ptr_helper(o, n); |
| 2968 | argv++; |
| 2969 | if (!*argv) |
| 2970 | break; |
| 2971 | } |
| 2972 | } |
| 2973 | globfree(&globdata); |
| 2974 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2975 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2976 | return n; |
| 2977 | } |
| 2978 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2979 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2980 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2981 | /* 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] | 2982 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2983 | static int o_save_ptr(o_string *o, int n) |
| 2984 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2985 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2986 | /* If o->has_empty_slot, list[n] was already globbed |
| 2987 | * (if it was requested back then when it was filled) |
| 2988 | * so don't do that again! */ |
| 2989 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2990 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2991 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2992 | return o_save_ptr_helper(o, n); |
| 2993 | } |
| 2994 | |
| 2995 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2996 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2997 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2998 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2999 | int string_start; |
| 3000 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3001 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3002 | if (DEBUG_EXPAND) |
| 3003 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3004 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3005 | list = (char**)o->data; |
| 3006 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3007 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3008 | while (n) { |
| 3009 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3010 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3011 | } |
| 3012 | return list; |
| 3013 | } |
| 3014 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3015 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3016 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3017 | /* Returns pi->next - next pipe in the list */ |
| 3018 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3019 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3020 | struct pipe *next; |
| 3021 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3022 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3023 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3024 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3025 | struct command *command; |
| 3026 | struct redir_struct *r, *rnext; |
| 3027 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3028 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3029 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3030 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3031 | if (DEBUG_CLEAN) { |
| 3032 | int a; |
| 3033 | char **p; |
| 3034 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3035 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3036 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3037 | } |
| 3038 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3039 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3040 | } |
| 3041 | /* not "else if": on syntax error, we may have both! */ |
| 3042 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3043 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3044 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3045 | free_pipe_list(command->group); |
| 3046 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3047 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3048 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3049 | /* else is crucial here. |
| 3050 | * If group != NULL, child_func is meaningless */ |
| 3051 | #if ENABLE_HUSH_FUNCTIONS |
| 3052 | else if (command->child_func) { |
| 3053 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3054 | command->child_func->parent_cmd = NULL; |
| 3055 | } |
| 3056 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3057 | #if !BB_MMU |
| 3058 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3059 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3060 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3061 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3062 | debug_printf_clean(" redirect %d%s", |
| 3063 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3064 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3065 | if (r->rd_filename) { |
| 3066 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3067 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3068 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3069 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3070 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3071 | rnext = r->next; |
| 3072 | free(r); |
| 3073 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3074 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3075 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3076 | 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] | 3077 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3078 | #if ENABLE_HUSH_JOB |
| 3079 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3080 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3081 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3082 | |
| 3083 | next = pi->next; |
| 3084 | free(pi); |
| 3085 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3086 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3087 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3088 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3089 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3090 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3091 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3092 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3093 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3094 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3095 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3096 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3100 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3101 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3102 | #ifndef debug_print_tree |
| 3103 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3104 | { |
| 3105 | static const char *const PIPE[] = { |
| 3106 | [PIPE_SEQ] = "SEQ", |
| 3107 | [PIPE_AND] = "AND", |
| 3108 | [PIPE_OR ] = "OR" , |
| 3109 | [PIPE_BG ] = "BG" , |
| 3110 | }; |
| 3111 | static const char *RES[] = { |
| 3112 | [RES_NONE ] = "NONE" , |
| 3113 | # if ENABLE_HUSH_IF |
| 3114 | [RES_IF ] = "IF" , |
| 3115 | [RES_THEN ] = "THEN" , |
| 3116 | [RES_ELIF ] = "ELIF" , |
| 3117 | [RES_ELSE ] = "ELSE" , |
| 3118 | [RES_FI ] = "FI" , |
| 3119 | # endif |
| 3120 | # if ENABLE_HUSH_LOOPS |
| 3121 | [RES_FOR ] = "FOR" , |
| 3122 | [RES_WHILE] = "WHILE", |
| 3123 | [RES_UNTIL] = "UNTIL", |
| 3124 | [RES_DO ] = "DO" , |
| 3125 | [RES_DONE ] = "DONE" , |
| 3126 | # endif |
| 3127 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3128 | [RES_IN ] = "IN" , |
| 3129 | # endif |
| 3130 | # if ENABLE_HUSH_CASE |
| 3131 | [RES_CASE ] = "CASE" , |
| 3132 | [RES_CASE_IN ] = "CASE_IN" , |
| 3133 | [RES_MATCH] = "MATCH", |
| 3134 | [RES_CASE_BODY] = "CASE_BODY", |
| 3135 | [RES_ESAC ] = "ESAC" , |
| 3136 | # endif |
| 3137 | [RES_XXXX ] = "XXXX" , |
| 3138 | [RES_SNTX ] = "SNTX" , |
| 3139 | }; |
| 3140 | static const char *const CMDTYPE[] = { |
| 3141 | "{}", |
| 3142 | "()", |
| 3143 | "[noglob]", |
| 3144 | # if ENABLE_HUSH_FUNCTIONS |
| 3145 | "func()", |
| 3146 | # endif |
| 3147 | }; |
| 3148 | |
| 3149 | int pin, prn; |
| 3150 | |
| 3151 | pin = 0; |
| 3152 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3153 | 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] | 3154 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 3155 | prn = 0; |
| 3156 | while (prn < pi->num_cmds) { |
| 3157 | struct command *command = &pi->cmds[prn]; |
| 3158 | char **argv = command->argv; |
| 3159 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3160 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3161 | lvl*2, "", prn, |
| 3162 | command->assignment_cnt); |
| 3163 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3164 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3165 | CMDTYPE[command->cmd_type], |
| 3166 | argv |
| 3167 | # if !BB_MMU |
| 3168 | , " group_as_string:", command->group_as_string |
| 3169 | # else |
| 3170 | , "", "" |
| 3171 | # endif |
| 3172 | ); |
| 3173 | debug_print_tree(command->group, lvl+1); |
| 3174 | prn++; |
| 3175 | continue; |
| 3176 | } |
| 3177 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3178 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3179 | argv++; |
| 3180 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3181 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3182 | prn++; |
| 3183 | } |
| 3184 | pi = pi->next; |
| 3185 | pin++; |
| 3186 | } |
| 3187 | } |
| 3188 | #endif /* debug_print_tree */ |
| 3189 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3190 | static struct pipe *new_pipe(void) |
| 3191 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3192 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3193 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3194 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3195 | return pi; |
| 3196 | } |
| 3197 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3198 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3199 | * if ctx->command is NULL. |
| 3200 | * No errors possible here. |
| 3201 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3202 | static int done_command(struct parse_context *ctx) |
| 3203 | { |
| 3204 | /* The command is really already in the pipe structure, so |
| 3205 | * advance the pipe counter and make a new, null command. */ |
| 3206 | struct pipe *pi = ctx->pipe; |
| 3207 | struct command *command = ctx->command; |
| 3208 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3209 | #if 0 /* Instead we emit error message at run time */ |
| 3210 | if (ctx->pending_redirect) { |
| 3211 | /* For example, "cmd >" (no filename to redirect to) */ |
| 3212 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3213 | ctx->pending_redirect = NULL; |
| 3214 | } |
| 3215 | #endif |
| 3216 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3217 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3218 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3219 | 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] | 3220 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3221 | } |
| 3222 | pi->num_cmds++; |
| 3223 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3224 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3225 | } else { |
| 3226 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3227 | } |
| 3228 | |
| 3229 | /* Only real trickiness here is that the uncommitted |
| 3230 | * command structure is not counted in pi->num_cmds. */ |
| 3231 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3232 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3233 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3234 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3235 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3236 | } |
| 3237 | |
| 3238 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3239 | { |
| 3240 | int not_null; |
| 3241 | |
| 3242 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3243 | /* Close previous command */ |
| 3244 | not_null = done_command(ctx); |
| 3245 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3246 | #if HAS_KEYWORDS |
| 3247 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3248 | ctx->ctx_inverted = 0; |
| 3249 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3250 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* Without this check, even just <enter> on command line generates |
| 3253 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3254 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3255 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3256 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3257 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3258 | #endif |
| 3259 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3260 | || ctx->ctx_res_w == RES_DONE |
| 3261 | || ctx->ctx_res_w == RES_FOR |
| 3262 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3263 | #endif |
| 3264 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3265 | || ctx->ctx_res_w == RES_ESAC |
| 3266 | #endif |
| 3267 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3268 | struct pipe *new_p; |
| 3269 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3270 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3271 | not_null, ctx->ctx_res_w); |
| 3272 | new_p = new_pipe(); |
| 3273 | ctx->pipe->next = new_p; |
| 3274 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3275 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3276 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3277 | * This is used to control execution. |
| 3278 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3279 | * cases where variable or value happens to match a keyword): |
| 3280 | */ |
| 3281 | #if ENABLE_HUSH_LOOPS |
| 3282 | if (ctx->ctx_res_w == RES_FOR |
| 3283 | || ctx->ctx_res_w == RES_IN) |
| 3284 | ctx->ctx_res_w = RES_NONE; |
| 3285 | #endif |
| 3286 | #if ENABLE_HUSH_CASE |
| 3287 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3288 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3289 | if (ctx->ctx_res_w == RES_CASE) |
| 3290 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3291 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3292 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3293 | /* Create the memory for command, roughly: |
| 3294 | * ctx->pipe->cmds = new struct command; |
| 3295 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3296 | */ |
| 3297 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3298 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3299 | } |
| 3300 | debug_printf_parse("done_pipe return\n"); |
| 3301 | } |
| 3302 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3303 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3304 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3305 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3306 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3307 | /* Create the memory for command, roughly: |
| 3308 | * ctx->pipe->cmds = new struct command; |
| 3309 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3310 | */ |
| 3311 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3314 | /* If a reserved word is found and processed, parse context is modified |
| 3315 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3316 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3317 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3318 | struct reserved_combo { |
| 3319 | char literal[6]; |
| 3320 | unsigned char res; |
| 3321 | unsigned char assignment_flag; |
| 3322 | int flag; |
| 3323 | }; |
| 3324 | enum { |
| 3325 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3326 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3327 | FLAG_IF = (1 << RES_IF ), |
| 3328 | FLAG_THEN = (1 << RES_THEN ), |
| 3329 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3330 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3331 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3332 | # endif |
| 3333 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3334 | FLAG_FOR = (1 << RES_FOR ), |
| 3335 | FLAG_WHILE = (1 << RES_WHILE), |
| 3336 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3337 | FLAG_DO = (1 << RES_DO ), |
| 3338 | FLAG_DONE = (1 << RES_DONE ), |
| 3339 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3340 | # endif |
| 3341 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3342 | FLAG_MATCH = (1 << RES_MATCH), |
| 3343 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3344 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3345 | FLAG_START = (1 << RES_XXXX ), |
| 3346 | }; |
| 3347 | |
| 3348 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3349 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3350 | /* Mostly a list of accepted follow-up reserved words. |
| 3351 | * FLAG_END means we are done with the sequence, and are ready |
| 3352 | * to turn the compound list into a command. |
| 3353 | * FLAG_START means the word must start a new compound list. |
| 3354 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3355 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3356 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3357 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3358 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3359 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3360 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3361 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3362 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3363 | # endif |
| 3364 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3365 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3366 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3367 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3368 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3369 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3370 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3371 | # endif |
| 3372 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3373 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3374 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3375 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3376 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3377 | const struct reserved_combo *r; |
| 3378 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3379 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3380 | if (strcmp(word->data, r->literal) == 0) |
| 3381 | return r; |
| 3382 | } |
| 3383 | return NULL; |
| 3384 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3385 | /* Return 0: not a keyword, 1: keyword |
| 3386 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3387 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3388 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3389 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3390 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3391 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3392 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3393 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3394 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3395 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3396 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3397 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3398 | r = match_reserved_word(word); |
| 3399 | if (!r) |
| 3400 | return 0; |
| 3401 | |
| 3402 | 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] | 3403 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3404 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3405 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3406 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3407 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3408 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3409 | if (r->flag == 0) { /* '!' */ |
| 3410 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3411 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3412 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3413 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3414 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3415 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3416 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3417 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3418 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3419 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3420 | old = xmalloc(sizeof(*old)); |
| 3421 | debug_printf_parse("push stack %p\n", old); |
| 3422 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3423 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3424 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3425 | } 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] | 3426 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3427 | ctx->ctx_res_w = RES_SNTX; |
| 3428 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3429 | } else { |
| 3430 | /* "{...} fi" is ok. "{...} if" is not |
| 3431 | * Example: |
| 3432 | * if { echo foo; } then { echo bar; } fi */ |
| 3433 | if (ctx->command->group) |
| 3434 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3435 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3436 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3437 | ctx->ctx_res_w = r->res; |
| 3438 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3439 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3440 | 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] | 3441 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3442 | if (ctx->old_flag & FLAG_END) { |
| 3443 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3444 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3445 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3446 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3447 | old = ctx->stack; |
| 3448 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3449 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3450 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3451 | /* At this point, the compound command's string is in |
| 3452 | * ctx->as_string... except for the leading keyword! |
| 3453 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3454 | * ctx->as_string will contain "true; then echo a; fi", |
| 3455 | * with "if " remaining in old->as_string! |
| 3456 | */ |
| 3457 | { |
| 3458 | char *str; |
| 3459 | int len = old->as_string.length; |
| 3460 | /* Concatenate halves */ |
| 3461 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3462 | o_free_unsafe(&ctx->as_string); |
| 3463 | /* Find where leading keyword starts in first half */ |
| 3464 | str = old->as_string.data + len; |
| 3465 | if (str > old->as_string.data) |
| 3466 | str--; /* skip whitespace after keyword */ |
| 3467 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3468 | str--; |
| 3469 | /* Ugh, we're done with this horrid hack */ |
| 3470 | old->command->group_as_string = xstrdup(str); |
| 3471 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3472 | old->command->group_as_string); |
| 3473 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3474 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3475 | *ctx = *old; /* physical copy */ |
| 3476 | free(old); |
| 3477 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3478 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3479 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3480 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3481 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3482 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3483 | * Normal return is 0. Syntax errors return 1. |
| 3484 | * Note: on return, word is reset, but not o_free'd! |
| 3485 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3486 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3487 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3488 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3489 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3490 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3491 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3492 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3493 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3494 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3495 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3496 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3497 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3498 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3499 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3500 | * "2.7 Redirection |
| 3501 | * ...the word that follows the redirection operator |
| 3502 | * shall be subjected to tilde expansion, parameter expansion, |
| 3503 | * command substitution, arithmetic expansion, and quote |
| 3504 | * removal. Pathname expansion shall not be performed |
| 3505 | * on the word by a non-interactive shell; an interactive |
| 3506 | * shell may perform it, but shall do so only when |
| 3507 | * the expansion would result in one word." |
| 3508 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3509 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3510 | /* Cater for >\file case: |
| 3511 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3512 | * Same with heredocs: |
| 3513 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3514 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3515 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3516 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3517 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3518 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3519 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3520 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3521 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3522 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3523 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3524 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3525 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3526 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3527 | if (ctx->ctx_dsemicolon |
| 3528 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3529 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3530 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3531 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3532 | ctx->ctx_dsemicolon = 0; |
| 3533 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3534 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3535 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3536 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3537 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3538 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3539 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3540 | # if ENABLE_HUSH_CASE |
| 3541 | && ctx->ctx_res_w != RES_CASE |
| 3542 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3543 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3544 | int reserved = reserved_word(word, ctx); |
| 3545 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3546 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3547 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3548 | debug_printf_parse("done_word return %d\n", |
| 3549 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3550 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3551 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3552 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3553 | if (strcmp(word->data, "[[") == 0) { |
| 3554 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3555 | } |
| 3556 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3557 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3558 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3559 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3560 | if (command->group) { |
| 3561 | /* "{ echo foo; } echo bar" - bad */ |
| 3562 | syntax_error_at(word->data); |
| 3563 | debug_printf_parse("done_word return 1: syntax error, " |
| 3564 | "groups and arglists don't mix\n"); |
| 3565 | return 1; |
| 3566 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3567 | |
| 3568 | /* If this word wasn't an assignment, next ones definitely |
| 3569 | * can't be assignments. Even if they look like ones. */ |
| 3570 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3571 | && word->o_assignment != WORD_IS_KEYWORD |
| 3572 | ) { |
| 3573 | word->o_assignment = NOT_ASSIGNMENT; |
| 3574 | } else { |
| 3575 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3576 | command->assignment_cnt++; |
| 3577 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3578 | } |
| 3579 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3580 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3581 | } |
| 3582 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3583 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3584 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3585 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3586 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3587 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3588 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3589 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3590 | char *p = word->data; |
| 3591 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3592 | && (p[1] & 0x7f) == '@' |
| 3593 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3594 | ) { |
| 3595 | p += 3; |
| 3596 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3597 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3598 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3599 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3600 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3601 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3602 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3603 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3604 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3605 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3606 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3607 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3608 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3609 | return 1; |
| 3610 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3611 | /* Force FOR to have just one word (variable name) */ |
| 3612 | /* NB: basically, this makes hush see "for v in ..." |
| 3613 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3614 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3615 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3616 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3617 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3618 | #if ENABLE_HUSH_CASE |
| 3619 | /* Force CASE to have just one word */ |
| 3620 | if (ctx->ctx_res_w == RES_CASE) { |
| 3621 | done_pipe(ctx, PIPE_SEQ); |
| 3622 | } |
| 3623 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3624 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3625 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3626 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3627 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3628 | return 0; |
| 3629 | } |
| 3630 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3633 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3634 | * Return: |
| 3635 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3636 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3637 | * REDIRFD_TO_FILE if no & was seen, |
| 3638 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3639 | */ |
| 3640 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3641 | #define parse_redir_right_fd(as_string, input) \ |
| 3642 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3643 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3644 | 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] | 3645 | { |
| 3646 | int ch, d, ok; |
| 3647 | |
| 3648 | ch = i_peek(input); |
| 3649 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3650 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3651 | |
| 3652 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3653 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3654 | ch = i_peek(input); |
| 3655 | if (ch == '-') { |
| 3656 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3657 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3658 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3659 | } |
| 3660 | d = 0; |
| 3661 | ok = 0; |
| 3662 | while (ch != EOF && isdigit(ch)) { |
| 3663 | d = d*10 + (ch-'0'); |
| 3664 | ok = 1; |
| 3665 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3666 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3667 | ch = i_peek(input); |
| 3668 | } |
| 3669 | if (ok) return d; |
| 3670 | |
| 3671 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3672 | |
| 3673 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3674 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3677 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3678 | */ |
| 3679 | static int parse_redirect(struct parse_context *ctx, |
| 3680 | int fd, |
| 3681 | redir_type style, |
| 3682 | struct in_str *input) |
| 3683 | { |
| 3684 | struct command *command = ctx->command; |
| 3685 | struct redir_struct *redir; |
| 3686 | struct redir_struct **redirp; |
| 3687 | int dup_num; |
| 3688 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3689 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3690 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3691 | /* Check for a '>&1' type redirect */ |
| 3692 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3693 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3694 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3695 | } else { |
| 3696 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3697 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3698 | if (dup_num) { /* <<-... */ |
| 3699 | ch = i_getch(input); |
| 3700 | nommu_addchr(&ctx->as_string, ch); |
| 3701 | ch = i_peek(input); |
| 3702 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3703 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3704 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3705 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3706 | int ch = i_peek(input); |
| 3707 | if (ch == '|') { |
| 3708 | /* >|FILE redirect ("clobbering" >). |
| 3709 | * Since we do not support "set -o noclobber" yet, |
| 3710 | * >| and > are the same for now. Just eat |. |
| 3711 | */ |
| 3712 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3713 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | /* Create a new redir_struct and append it to the linked list */ |
| 3718 | redirp = &command->redirects; |
| 3719 | while ((redir = *redirp) != NULL) { |
| 3720 | redirp = &(redir->next); |
| 3721 | } |
| 3722 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3723 | /* redir->next = NULL; */ |
| 3724 | /* redir->rd_filename = NULL; */ |
| 3725 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3726 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3727 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3728 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3729 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3730 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3731 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3732 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3733 | /* Erik had a check here that the file descriptor in question |
| 3734 | * is legit; I postpone that to "run time" |
| 3735 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3736 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3737 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3738 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3739 | #if 0 /* Instead we emit error message at run time */ |
| 3740 | if (ctx->pending_redirect) { |
| 3741 | /* For example, "cmd > <file" */ |
| 3742 | die_if_script("syntax error: %s", "invalid redirect"); |
| 3743 | } |
| 3744 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3745 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3746 | * end of the next parsed word. */ |
| 3747 | ctx->pending_redirect = redir; |
| 3748 | } |
| 3749 | return 0; |
| 3750 | } |
| 3751 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3752 | /* If a redirect is immediately preceded by a number, that number is |
| 3753 | * supposed to tell which file descriptor to redirect. This routine |
| 3754 | * looks for such preceding numbers. In an ideal world this routine |
| 3755 | * needs to handle all the following classes of redirects... |
| 3756 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3757 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3758 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3759 | * 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] | 3760 | * |
| 3761 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3762 | * "2.7 Redirection |
| 3763 | * ... If n is quoted, the number shall not be recognized as part of |
| 3764 | * the redirection expression. For example: |
| 3765 | * echo \2>a |
| 3766 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3767 | * 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] | 3768 | * |
| 3769 | * A -1 return means no valid number was found, |
| 3770 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3771 | */ |
| 3772 | static int redirect_opt_num(o_string *o) |
| 3773 | { |
| 3774 | int num; |
| 3775 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3776 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3777 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3778 | num = bb_strtou(o->data, NULL, 10); |
| 3779 | if (errno || num < 0) |
| 3780 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3781 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3782 | return num; |
| 3783 | } |
| 3784 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3785 | #if BB_MMU |
| 3786 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3787 | fetch_till_str(input, word, skip_tabs) |
| 3788 | #endif |
| 3789 | static char *fetch_till_str(o_string *as_string, |
| 3790 | struct in_str *input, |
| 3791 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3792 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3793 | { |
| 3794 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3795 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3796 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3797 | int ch; |
| 3798 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3799 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 3800 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3801 | while (1) { |
| 3802 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3803 | if (ch != EOF) |
| 3804 | nommu_addchr(as_string, ch); |
| 3805 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3806 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3807 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3808 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3809 | heredoc.data[past_EOL] = '\0'; |
| 3810 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3811 | return heredoc.data; |
| 3812 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3813 | while (ch == '\n') { |
| 3814 | o_addchr(&heredoc, ch); |
| 3815 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3816 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3817 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3818 | do { |
| 3819 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3820 | if (ch != EOF) |
| 3821 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3822 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3823 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3824 | } |
| 3825 | if (ch == EOF) { |
| 3826 | o_free_unsafe(&heredoc); |
| 3827 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3828 | } |
| 3829 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3830 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3831 | if (prev == '\\' && ch == '\\') |
| 3832 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3833 | prev = 0; /* not \ */ |
| 3834 | else |
| 3835 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3836 | } |
| 3837 | } |
| 3838 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3839 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3840 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3841 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3842 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3843 | { |
| 3844 | struct pipe *pi = ctx->list_head; |
| 3845 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3846 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3847 | int i; |
| 3848 | struct command *cmd = pi->cmds; |
| 3849 | |
| 3850 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3851 | pi->num_cmds, |
| 3852 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3853 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3854 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3855 | |
| 3856 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3857 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3858 | while (redir) { |
| 3859 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3860 | char *p; |
| 3861 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3862 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3863 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3864 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3865 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3866 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3867 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3868 | return 1; |
| 3869 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3870 | free(redir->rd_filename); |
| 3871 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3872 | heredoc_cnt--; |
| 3873 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3874 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3875 | } |
| 3876 | cmd++; |
| 3877 | } |
| 3878 | pi = pi->next; |
| 3879 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3880 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3881 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3882 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3883 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3884 | #endif |
| 3885 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3889 | static int run_list(struct pipe *pi); |
| 3890 | #if BB_MMU |
| 3891 | #define parse_stream(pstring, input, end_trigger) \ |
| 3892 | parse_stream(input, end_trigger) |
| 3893 | #endif |
| 3894 | static struct pipe *parse_stream(char **pstring, |
| 3895 | struct in_str *input, |
| 3896 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3897 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3898 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3899 | #if !ENABLE_HUSH_FUNCTIONS |
| 3900 | #define parse_group(dest, ctx, input, ch) \ |
| 3901 | parse_group(ctx, input, ch) |
| 3902 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3903 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3904 | struct in_str *input, int ch) |
| 3905 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3906 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3907 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3908 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3909 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3910 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3911 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3912 | |
| 3913 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3914 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3915 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3916 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3917 | if (done_word(dest, ctx)) |
| 3918 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3919 | if (!command->argv) |
| 3920 | goto skip; /* (... */ |
| 3921 | if (command->argv[1]) { /* word word ... (... */ |
| 3922 | syntax_error_unexpected_ch('('); |
| 3923 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3924 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3925 | /* it is "word(..." or "word (..." */ |
| 3926 | do |
| 3927 | ch = i_getch(input); |
| 3928 | while (ch == ' ' || ch == '\t'); |
| 3929 | if (ch != ')') { |
| 3930 | syntax_error_unexpected_ch(ch); |
| 3931 | return 1; |
| 3932 | } |
| 3933 | nommu_addchr(&ctx->as_string, ch); |
| 3934 | do |
| 3935 | ch = i_getch(input); |
| 3936 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3937 | if (ch != '{') { |
| 3938 | syntax_error_unexpected_ch(ch); |
| 3939 | return 1; |
| 3940 | } |
| 3941 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3942 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3943 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3944 | } |
| 3945 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3946 | |
| 3947 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3948 | if (command->argv /* word [word]{... */ |
| 3949 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3950 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3951 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3952 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3953 | debug_printf_parse("parse_group return 1: " |
| 3954 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3955 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3956 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3957 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3958 | |
| 3959 | #if ENABLE_HUSH_FUNCTIONS |
| 3960 | skip: |
| 3961 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3962 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3963 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3964 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3965 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3966 | } else { |
| 3967 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 3968 | ch = i_peek(input); |
| 3969 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 3970 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 3971 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3972 | syntax_error_unexpected_ch(ch); |
| 3973 | return 1; |
| 3974 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 3975 | if (ch != '(') { |
| 3976 | ch = i_getch(input); |
| 3977 | nommu_addchr(&ctx->as_string, ch); |
| 3978 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3979 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3980 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3981 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3982 | #if BB_MMU |
| 3983 | # define as_string NULL |
| 3984 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3985 | char *as_string = NULL; |
| 3986 | #endif |
| 3987 | pipe_list = parse_stream(&as_string, input, endch); |
| 3988 | #if !BB_MMU |
| 3989 | if (as_string) |
| 3990 | o_addstr(&ctx->as_string, as_string); |
| 3991 | #endif |
| 3992 | /* empty ()/{} or parse error? */ |
| 3993 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3994 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3995 | if (!BB_MMU) |
| 3996 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3997 | debug_printf_parse("parse_group return 1: " |
| 3998 | "parse_stream returned %p\n", pipe_list); |
| 3999 | return 1; |
| 4000 | } |
| 4001 | command->group = pipe_list; |
| 4002 | #if !BB_MMU |
| 4003 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4004 | command->group_as_string = as_string; |
| 4005 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4006 | command->group_as_string); |
| 4007 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4008 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4009 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4010 | debug_printf_parse("parse_group return 0\n"); |
| 4011 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4012 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4015 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 4016 | { |
| 4017 | for (;;) { |
| 4018 | int ch, ch2; |
| 4019 | |
| 4020 | ch = i_getch(input); |
| 4021 | if (ch != '\\') |
| 4022 | return ch; |
| 4023 | ch2 = i_peek(input); |
| 4024 | if (ch2 != '\n') |
| 4025 | return ch; |
| 4026 | /* backslash+newline, skip it */ |
| 4027 | i_getch(input); |
| 4028 | } |
| 4029 | } |
| 4030 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4031 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 4032 | { |
| 4033 | for (;;) { |
| 4034 | int ch, ch2; |
| 4035 | |
| 4036 | ch = i_peek(input); |
| 4037 | if (ch != '\\') |
| 4038 | return ch; |
| 4039 | ch2 = i_peek2(input); |
| 4040 | if (ch2 != '\n') |
| 4041 | return ch; |
| 4042 | /* backslash+newline, skip it */ |
| 4043 | i_getch(input); |
| 4044 | i_getch(input); |
| 4045 | } |
| 4046 | } |
| 4047 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4048 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4049 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4050 | 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] | 4051 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4052 | 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] | 4053 | { |
| 4054 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4055 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4056 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4057 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4058 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4059 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4060 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4061 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4062 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4066 | 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] | 4067 | { |
| 4068 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4069 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4070 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4071 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4072 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4073 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4074 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4075 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4076 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4077 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4078 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4079 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4080 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4081 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4082 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4083 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4084 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4085 | continue; |
| 4086 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4087 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4088 | } |
| 4089 | } |
| 4090 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4091 | * \` quoting. |
| 4092 | * "Within the backquoted style of command substitution, backslash |
| 4093 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4094 | * The search for the matching backquote shall be satisfied by the first |
| 4095 | * backquote found without a preceding backslash; during this search, |
| 4096 | * if a non-escaped backquote is encountered within a shell comment, |
| 4097 | * a here-document, an embedded command substitution of the $(command) |
| 4098 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4099 | * or double-quoted string that begins, but does not end, within the |
| 4100 | * "`...`" sequence produces undefined results." |
| 4101 | * Example Output |
| 4102 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4103 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4104 | 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] | 4105 | { |
| 4106 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4107 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4108 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4109 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4110 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4111 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4112 | ch = i_getch(input); |
| 4113 | if (ch != '`' |
| 4114 | && ch != '$' |
| 4115 | && ch != '\\' |
| 4116 | && (!in_dquote || ch != '"') |
| 4117 | ) { |
| 4118 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4119 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4120 | } |
| 4121 | if (ch == EOF) { |
| 4122 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4123 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4124 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4125 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4129 | * quoting and nested ()s. |
| 4130 | * "With the $(command) style of command substitution, all characters |
| 4131 | * following the open parenthesis to the matching closing parenthesis |
| 4132 | * constitute the command. Any valid shell script can be used for command, |
| 4133 | * except a script consisting solely of redirections which produces |
| 4134 | * unspecified results." |
| 4135 | * Example Output |
| 4136 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4137 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4138 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4139 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4140 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4141 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4142 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4143 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4144 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4145 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4146 | 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] | 4147 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4148 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4149 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4150 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4151 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4152 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4153 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4154 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4155 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4156 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4157 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4158 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4159 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4160 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4161 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4162 | if (!dbl) |
| 4163 | break; |
| 4164 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4165 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4166 | i_getch(input); /* eat second ')' */ |
| 4167 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4168 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4169 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4170 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4171 | if (ch == '(' || ch == '{') { |
| 4172 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4173 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4174 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4175 | o_addchr(dest, ch); |
| 4176 | continue; |
| 4177 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4178 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4179 | if (!add_till_single_quote(dest, input)) |
| 4180 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4181 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4182 | continue; |
| 4183 | } |
| 4184 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4185 | if (!add_till_double_quote(dest, input)) |
| 4186 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4187 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4188 | continue; |
| 4189 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4190 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4191 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4192 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4193 | o_addchr(dest, ch); |
| 4194 | continue; |
| 4195 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4196 | if (ch == '\\') { |
| 4197 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4198 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4199 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4200 | syntax_error_unterm_ch(')'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4201 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4202 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4203 | #if 0 |
| 4204 | if (ch == '\n') { |
| 4205 | /* "backslash+newline", ignore both */ |
| 4206 | o_delchr(dest); /* undo insertion of '\' */ |
| 4207 | continue; |
| 4208 | } |
| 4209 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4210 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4211 | continue; |
| 4212 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4213 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4214 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4215 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4216 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4217 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4218 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4219 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4220 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4221 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4222 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4223 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4224 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4225 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4226 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4227 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4228 | 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] | 4229 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4230 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4231 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4232 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4233 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 4234 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4235 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4236 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4237 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4238 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4239 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4240 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4241 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4242 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4243 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4244 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4245 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4246 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4247 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4248 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4249 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4250 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4251 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4252 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4253 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4254 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4255 | o_addchr(dest, ch | quote_mask); |
| 4256 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4257 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4258 | case '$': /* pid */ |
| 4259 | case '!': /* last bg pid */ |
| 4260 | case '?': /* last exit code */ |
| 4261 | case '#': /* number of args */ |
| 4262 | case '*': /* args */ |
| 4263 | case '@': /* args */ |
| 4264 | goto make_one_char_var; |
| 4265 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4266 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4267 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4268 | ch = i_getch(input); /* eat '{' */ |
| 4269 | nommu_addchr(as_string, ch); |
| 4270 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4271 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4272 | /* It should be ${?}, or ${#var}, |
| 4273 | * or even ${?+subst} - operator acting on a special variable, |
| 4274 | * or the beginning of variable name. |
| 4275 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4276 | if (ch == EOF |
| 4277 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4278 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4279 | bad_dollar_syntax: |
| 4280 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4281 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4282 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4283 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4284 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4285 | ch |= quote_mask; |
| 4286 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4287 | /* 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] | 4288 | * However, this regresses some of our testsuite cases |
| 4289 | * which check invalid constructs like ${%}. |
| 4290 | * Oh well... let's check that the var name part is fine... */ |
| 4291 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4292 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4293 | unsigned pos; |
| 4294 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4295 | o_addchr(dest, ch); |
| 4296 | debug_printf_parse(": '%c'\n", ch); |
| 4297 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4298 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4299 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4300 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4301 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4302 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4303 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4304 | unsigned end_ch; |
| 4305 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4306 | /* handle parameter expansions |
| 4307 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4308 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4309 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4310 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4311 | |
| 4312 | /* Eat everything until closing '}' (or ':') */ |
| 4313 | end_ch = '}'; |
| 4314 | if (ENABLE_HUSH_BASH_COMPAT |
| 4315 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4316 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4317 | ) { |
| 4318 | /* It's ${var:N[:M]} thing */ |
| 4319 | end_ch = '}' * 0x100 + ':'; |
| 4320 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4321 | if (ENABLE_HUSH_BASH_COMPAT |
| 4322 | && ch == '/' |
| 4323 | ) { |
| 4324 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4325 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4326 | i_getch(input); |
| 4327 | nommu_addchr(as_string, '/'); |
| 4328 | ch = '\\'; |
| 4329 | } |
| 4330 | end_ch = '}' * 0x100 + '/'; |
| 4331 | } |
| 4332 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4333 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4334 | if (!BB_MMU) |
| 4335 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4336 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4337 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4338 | if (last_ch == 0) /* error? */ |
| 4339 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4340 | #else |
| 4341 | #error Simple code to only allow ${var} is not implemented |
| 4342 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4343 | if (as_string) { |
| 4344 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4345 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4346 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4347 | |
| 4348 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 4349 | /* close the first block: */ |
| 4350 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4351 | /* while parsing N from ${var:N[:M]} |
| 4352 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4353 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4354 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4355 | end_ch = '}'; |
| 4356 | goto again; |
| 4357 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4358 | /* got '}' */ |
| 4359 | if (end_ch == '}' * 0x100 + ':') { |
| 4360 | /* it's ${var:N} - emulate :999999999 */ |
| 4361 | o_addstr(dest, "999999999"); |
| 4362 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4363 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4364 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4365 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4366 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4367 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4368 | break; |
| 4369 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4370 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4371 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4372 | unsigned pos; |
| 4373 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4374 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4375 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4376 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4377 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4378 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4379 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4380 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4381 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4382 | if (!BB_MMU) |
| 4383 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4384 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4385 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4386 | if (as_string) { |
| 4387 | o_addstr(as_string, dest->data + pos); |
| 4388 | o_addchr(as_string, ')'); |
| 4389 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4390 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4391 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4392 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4393 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4394 | # endif |
| 4395 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4396 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4397 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4398 | if (!BB_MMU) |
| 4399 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4400 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4401 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4402 | if (as_string) { |
| 4403 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4404 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4405 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4406 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4407 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4410 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4411 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4412 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4413 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4414 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4415 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4416 | ch = '_'; |
| 4417 | goto make_var; |
| 4418 | } |
| 4419 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4420 | /* TODO: $_ and $-: */ |
| 4421 | /* $_ Shell or shell script name; or last argument of last command |
| 4422 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4423 | * 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] | 4424 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4425 | default: |
| 4426 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4427 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4428 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4429 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4430 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4433 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4434 | # if ENABLE_HUSH_BASH_COMPAT |
| 4435 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4436 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4437 | # else |
| 4438 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4439 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4440 | encode_string(dest, input, dquote_end) |
| 4441 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4442 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4443 | |
| 4444 | #else /* !MMU */ |
| 4445 | |
| 4446 | # if ENABLE_HUSH_BASH_COMPAT |
| 4447 | /* all parameters are needed, no macro tricks */ |
| 4448 | # else |
| 4449 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4450 | encode_string(as_string, dest, input, dquote_end) |
| 4451 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4452 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4453 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4454 | o_string *dest, |
| 4455 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4456 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4457 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4458 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4459 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4460 | const int process_bkslash = 1; |
| 4461 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4462 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4463 | int next; |
| 4464 | |
| 4465 | again: |
| 4466 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4467 | if (ch != EOF) |
| 4468 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4469 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4470 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4471 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4472 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4473 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4474 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4475 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4476 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4477 | } |
| 4478 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4479 | if (ch != '\n') { |
| 4480 | next = i_peek(input); |
| 4481 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4482 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4483 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4484 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4485 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4486 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4487 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4488 | } |
| 4489 | /* bash: |
| 4490 | * "The backslash retains its special meaning [in "..."] |
| 4491 | * only when followed by one of the following characters: |
| 4492 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4493 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4494 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4495 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4496 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4497 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4498 | ch = i_getch(input); /* eat next */ |
| 4499 | if (ch == '\n') |
| 4500 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4501 | } /* else: ch remains == '\\', and we double it below: */ |
| 4502 | 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] | 4503 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4504 | goto again; |
| 4505 | } |
| 4506 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4507 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4508 | debug_printf_parse("encode_string return 0: " |
| 4509 | "parse_dollar returned 0 (error)\n"); |
| 4510 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4511 | } |
| 4512 | goto again; |
| 4513 | } |
| 4514 | #if ENABLE_HUSH_TICK |
| 4515 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4516 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4517 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4518 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4519 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4520 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4521 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4522 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4523 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4524 | } |
| 4525 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4526 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4527 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4528 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4531 | /* |
| 4532 | * Scan input until EOF or end_trigger char. |
| 4533 | * Return a list of pipes to execute, or NULL on EOF |
| 4534 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4535 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4536 | * reset parsing machinery and start parsing anew, |
| 4537 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4538 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4539 | static struct pipe *parse_stream(char **pstring, |
| 4540 | struct in_str *input, |
| 4541 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4542 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4543 | struct parse_context ctx; |
| 4544 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4545 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4546 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4547 | /* 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] | 4548 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4549 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4550 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4551 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4552 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4553 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4554 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4555 | * Preventing this: */ |
| 4556 | o_addchr(&dest, '\0'); |
| 4557 | dest.length = 0; |
| 4558 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4559 | /* We used to separate words on $IFS here. This was wrong. |
| 4560 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4561 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4562 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4563 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4564 | if (MAYBE_ASSIGNMENT != 0) |
| 4565 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4566 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4567 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4568 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4569 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4570 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4571 | int ch; |
| 4572 | int next; |
| 4573 | int redir_fd; |
| 4574 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4575 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4576 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4577 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4578 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4579 | if (ch == EOF) { |
| 4580 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4581 | |
| 4582 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4583 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4584 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4585 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4586 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4587 | syntax_error_unterm_ch('('); |
| 4588 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4589 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 4590 | if (end_trigger == '}') { |
| 4591 | syntax_error_unterm_ch('{'); |
| 4592 | goto parse_error; |
| 4593 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4594 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4595 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4596 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4597 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4598 | o_free(&dest); |
| 4599 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4600 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4601 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4602 | /* (this makes bare "&" cmd a no-op. |
| 4603 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4604 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4605 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4606 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4607 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4608 | pi = NULL; |
| 4609 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4610 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4611 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4612 | if (pstring) |
| 4613 | *pstring = ctx.as_string.data; |
| 4614 | else |
| 4615 | o_free_unsafe(&ctx.as_string); |
| 4616 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4617 | debug_leave(); |
| 4618 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4619 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4620 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4621 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4622 | |
| 4623 | next = '\0'; |
| 4624 | if (ch != '\n') |
| 4625 | next = i_peek(input); |
| 4626 | |
| 4627 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4628 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4629 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4630 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4631 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4632 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4633 | || (next != ';' /* }; - special */ |
| 4634 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4635 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4636 | && next != '&' /* }& and }&& ... - special */ |
| 4637 | && next != '|' /* }|| ... - special */ |
| 4638 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4639 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4640 | ) { |
| 4641 | /* They are not special, skip "{}" */ |
| 4642 | is_special += 2; |
| 4643 | } |
| 4644 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4645 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4646 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4647 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4648 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4649 | o_addQchr(&dest, ch); |
| 4650 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4651 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4652 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4653 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4654 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4655 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4656 | 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] | 4657 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4658 | continue; |
| 4659 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4660 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4661 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4662 | if (done_word(&dest, &ctx)) { |
| 4663 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4664 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4665 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4666 | /* Is this a case when newline is simply ignored? |
| 4667 | * Some examples: |
| 4668 | * "cmd | <newline> cmd ..." |
| 4669 | * "case ... in <newline> word) ..." |
| 4670 | */ |
| 4671 | if (IS_NULL_CMD(ctx.command) |
| 4672 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4673 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4674 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4675 | * Without check #1, interactive shell |
| 4676 | * ignores even bare <newline>, |
| 4677 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4678 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4679 | * ps2> _ <=== wrong, should be ps1 |
| 4680 | * Without check #2, "cmd & <newline>" |
| 4681 | * is similarly mistreated. |
| 4682 | * (BTW, this makes "cmd & cmd" |
| 4683 | * and "cmd && cmd" non-orthogonal. |
| 4684 | * Really, ask yourself, why |
| 4685 | * "cmd && <newline>" doesn't start |
| 4686 | * cmd but waits for more input? |
| 4687 | * No reason...) |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4688 | */ |
| 4689 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4690 | if (pi->num_cmds != 0 /* check #1 */ |
| 4691 | && pi->followup != PIPE_BG /* check #2 */ |
| 4692 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4693 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4694 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4695 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4696 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4697 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4698 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4699 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4700 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4701 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4702 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4703 | heredoc_cnt = 0; |
| 4704 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4705 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4706 | 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] | 4707 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4708 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4709 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4710 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4711 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4712 | |
| 4713 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4714 | * } is an ordinary char in this case, even inside { cmd; } |
| 4715 | * Pathological example: { ""}; } should exec "}" cmd |
| 4716 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4717 | if (ch == '}') { |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4718 | if (dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4719 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4720 | ) { |
| 4721 | goto ordinary_char; |
| 4722 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4723 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 4724 | /* Generally, there should be semicolon: "cmd; }" |
| 4725 | * However, bash allows to omit it if "cmd" is |
| 4726 | * a group. Examples: |
| 4727 | * { { echo 1; } } |
| 4728 | * {(echo 1)} |
| 4729 | * { echo 0 >&2 | { echo 1; } } |
| 4730 | * { while false; do :; done } |
| 4731 | * { case a in b) ;; esac } |
| 4732 | */ |
| 4733 | if (ctx.command->group) |
| 4734 | goto term_group; |
| 4735 | goto ordinary_char; |
| 4736 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4737 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4738 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4739 | goto skip_end_trigger; |
| 4740 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4741 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4742 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4743 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4744 | && (ch != ';' || heredoc_cnt == 0) |
| 4745 | #if ENABLE_HUSH_CASE |
| 4746 | && (ch != ')' |
| 4747 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4748 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4749 | ) |
| 4750 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4751 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4752 | if (heredoc_cnt) { |
| 4753 | /* This is technically valid: |
| 4754 | * { cat <<HERE; }; echo Ok |
| 4755 | * heredoc |
| 4756 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4757 | * HERE |
| 4758 | * but we don't support this. |
| 4759 | * We require heredoc to be in enclosing {}/(), |
| 4760 | * if any. |
| 4761 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4762 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4763 | goto parse_error; |
| 4764 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4765 | if (done_word(&dest, &ctx)) { |
| 4766 | goto parse_error; |
| 4767 | } |
| 4768 | done_pipe(&ctx, PIPE_SEQ); |
| 4769 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4770 | 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] | 4771 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4772 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4773 | 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] | 4774 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4775 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4776 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4777 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4778 | if (pstring) |
| 4779 | *pstring = ctx.as_string.data; |
| 4780 | else |
| 4781 | o_free_unsafe(&ctx.as_string); |
| 4782 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4783 | debug_leave(); |
| 4784 | debug_printf_parse("parse_stream return %p: " |
| 4785 | "end_trigger char found\n", |
| 4786 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4787 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4788 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4789 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4790 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4791 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4792 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4793 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4794 | /* Catch <, > before deciding whether this word is |
| 4795 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4796 | switch (ch) { |
| 4797 | case '>': |
| 4798 | redir_fd = redirect_opt_num(&dest); |
| 4799 | if (done_word(&dest, &ctx)) { |
| 4800 | goto parse_error; |
| 4801 | } |
| 4802 | redir_style = REDIRECT_OVERWRITE; |
| 4803 | if (next == '>') { |
| 4804 | redir_style = REDIRECT_APPEND; |
| 4805 | ch = i_getch(input); |
| 4806 | nommu_addchr(&ctx.as_string, ch); |
| 4807 | } |
| 4808 | #if 0 |
| 4809 | else if (next == '(') { |
| 4810 | syntax_error(">(process) not supported"); |
| 4811 | goto parse_error; |
| 4812 | } |
| 4813 | #endif |
| 4814 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4815 | goto parse_error; |
| 4816 | continue; /* back to top of while (1) */ |
| 4817 | case '<': |
| 4818 | redir_fd = redirect_opt_num(&dest); |
| 4819 | if (done_word(&dest, &ctx)) { |
| 4820 | goto parse_error; |
| 4821 | } |
| 4822 | redir_style = REDIRECT_INPUT; |
| 4823 | if (next == '<') { |
| 4824 | redir_style = REDIRECT_HEREDOC; |
| 4825 | heredoc_cnt++; |
| 4826 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4827 | ch = i_getch(input); |
| 4828 | nommu_addchr(&ctx.as_string, ch); |
| 4829 | } else if (next == '>') { |
| 4830 | redir_style = REDIRECT_IO; |
| 4831 | ch = i_getch(input); |
| 4832 | nommu_addchr(&ctx.as_string, ch); |
| 4833 | } |
| 4834 | #if 0 |
| 4835 | else if (next == '(') { |
| 4836 | syntax_error("<(process) not supported"); |
| 4837 | goto parse_error; |
| 4838 | } |
| 4839 | #endif |
| 4840 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4841 | goto parse_error; |
| 4842 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4843 | case '#': |
| 4844 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4845 | /* skip "#comment" */ |
| 4846 | while (1) { |
| 4847 | ch = i_peek(input); |
| 4848 | if (ch == EOF || ch == '\n') |
| 4849 | break; |
| 4850 | i_getch(input); |
| 4851 | /* note: we do not add it to &ctx.as_string */ |
| 4852 | } |
| 4853 | nommu_addchr(&ctx.as_string, '\n'); |
| 4854 | continue; /* back to top of while (1) */ |
| 4855 | } |
| 4856 | break; |
| 4857 | case '\\': |
| 4858 | if (next == '\n') { |
| 4859 | /* It's "\<newline>" */ |
| 4860 | #if !BB_MMU |
| 4861 | /* Remove trailing '\' from ctx.as_string */ |
| 4862 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4863 | #endif |
| 4864 | ch = i_getch(input); /* eat it */ |
| 4865 | continue; /* back to top of while (1) */ |
| 4866 | } |
| 4867 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4871 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4872 | && !ctx.pending_redirect |
| 4873 | ) { |
| 4874 | /* ch is a special char and thus this word |
| 4875 | * cannot be an assignment */ |
| 4876 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4877 | 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] | 4878 | } |
| 4879 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4880 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4881 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4882 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4883 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4884 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | case '\\': |
| 4887 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4888 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4889 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4890 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4891 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4892 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4893 | o_addchr(&dest, '\\'); |
| 4894 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4895 | o_addchr(&dest, ch); |
| 4896 | nommu_addchr(&ctx.as_string, ch); |
| 4897 | /* Example: echo Hello \2>file |
| 4898 | * we need to know that word 2 is quoted */ |
| 4899 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4900 | break; |
| 4901 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4902 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4903 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4904 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4905 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4906 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4907 | break; |
| 4908 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4909 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4910 | if (next == '\'' && !ctx.pending_redirect) { |
| 4911 | insert_empty_quoted_str_marker: |
| 4912 | nommu_addchr(&ctx.as_string, next); |
| 4913 | i_getch(input); /* eat second ' */ |
| 4914 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4915 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4916 | } else { |
| 4917 | while (1) { |
| 4918 | ch = i_getch(input); |
| 4919 | if (ch == EOF) { |
| 4920 | syntax_error_unterm_ch('\''); |
| 4921 | goto parse_error; |
| 4922 | } |
| 4923 | nommu_addchr(&ctx.as_string, ch); |
| 4924 | if (ch == '\'') |
| 4925 | break; |
| 4926 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4927 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4928 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4929 | break; |
| 4930 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4931 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 4932 | if (next == '"' && !ctx.pending_redirect) |
| 4933 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4934 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4935 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4936 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4937 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4938 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4939 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4940 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4941 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4942 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4943 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4944 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4945 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 4946 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4947 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 4948 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4949 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4950 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4951 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4952 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4953 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4954 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4955 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4956 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4957 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4958 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4959 | #if ENABLE_HUSH_CASE |
| 4960 | case_semi: |
| 4961 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4962 | if (done_word(&dest, &ctx)) { |
| 4963 | goto parse_error; |
| 4964 | } |
| 4965 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4966 | #if ENABLE_HUSH_CASE |
| 4967 | /* Eat multiple semicolons, detect |
| 4968 | * whether it means something special */ |
| 4969 | while (1) { |
| 4970 | ch = i_peek(input); |
| 4971 | if (ch != ';') |
| 4972 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4973 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4974 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4975 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4976 | ctx.ctx_dsemicolon = 1; |
| 4977 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4978 | break; |
| 4979 | } |
| 4980 | } |
| 4981 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4982 | new_cmd: |
| 4983 | /* We just finished a cmd. New one may start |
| 4984 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4985 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4986 | 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] | 4987 | break; |
| 4988 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4989 | if (done_word(&dest, &ctx)) { |
| 4990 | goto parse_error; |
| 4991 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4992 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4993 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4994 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4995 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4996 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4997 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4998 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4999 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5000 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5001 | if (done_word(&dest, &ctx)) { |
| 5002 | goto parse_error; |
| 5003 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5004 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5005 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5006 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5007 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5008 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5009 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5010 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5011 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5012 | } else { |
| 5013 | /* we could pick up a file descriptor choice here |
| 5014 | * with redirect_opt_num(), but bash doesn't do it. |
| 5015 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5016 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5017 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5018 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5019 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5020 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5021 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5022 | if (ctx.ctx_res_w == RES_MATCH |
| 5023 | && ctx.command->argv == NULL /* not (word|(... */ |
| 5024 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5025 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5026 | ) { |
| 5027 | continue; |
| 5028 | } |
| 5029 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5030 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5031 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 5032 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5033 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5034 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5035 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5036 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5037 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5038 | goto case_semi; |
| 5039 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5040 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5041 | /* proper use of this character is caught by end_trigger: |
| 5042 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5043 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5044 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5045 | G.last_exitcode = 2; |
| 5046 | goto parse_error1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5047 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5048 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5049 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5050 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5051 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5052 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5053 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5054 | G.last_exitcode = 1; |
| 5055 | parse_error1: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5056 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5057 | struct parse_context *pctx; |
| 5058 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5059 | |
| 5060 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5061 | * Sample for finding leaks on syntax error recovery path. |
| 5062 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5063 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5064 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5065 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5066 | * 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] | 5067 | */ |
| 5068 | pctx = &ctx; |
| 5069 | do { |
| 5070 | /* Update pipe/command counts, |
| 5071 | * otherwise freeing may miss some */ |
| 5072 | done_pipe(pctx, PIPE_SEQ); |
| 5073 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5074 | pctx->list_head, pctx); |
| 5075 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5076 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5077 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5078 | #if !BB_MMU |
| 5079 | o_free_unsafe(&pctx->as_string); |
| 5080 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5081 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5082 | if (pctx != &ctx) { |
| 5083 | free(pctx); |
| 5084 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5085 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5086 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5087 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 5088 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5089 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5090 | if (pstring) |
| 5091 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5092 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5093 | debug_leave(); |
| 5094 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5095 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5098 | |
| 5099 | /*** Execution routines ***/ |
| 5100 | |
| 5101 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5102 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5103 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5104 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5105 | expand_string_to_string(str) |
| 5106 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5107 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5108 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5109 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5110 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5111 | |
| 5112 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5113 | * all variable references within and returns a pointer to |
| 5114 | * a list of expanded strings, possibly with larger number |
| 5115 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5116 | * This new list is allocated as a single malloc block. |
| 5117 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5118 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5119 | * Caller can deallocate entire list by single free(list). */ |
| 5120 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5121 | /* A horde of its helpers come first: */ |
| 5122 | |
| 5123 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5124 | { |
| 5125 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5126 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5127 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5128 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5129 | if (c == '{' || c == '}') { |
| 5130 | /* { -> \{, } -> \} */ |
| 5131 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5132 | /* And now we want to add { or } and continue: |
| 5133 | * o_addchr(o, c); |
| 5134 | * continue; |
| 5135 | * luckily, just falling throught achieves this. |
| 5136 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5137 | } |
| 5138 | #endif |
| 5139 | o_addchr(o, c); |
| 5140 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5141 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5142 | o_addchr(o, '\\'); |
| 5143 | if (len) { |
| 5144 | len--; |
| 5145 | o_addchr(o, '\\'); |
| 5146 | o_addchr(o, *str++); |
| 5147 | } |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5152 | /* Store given string, finalizing the word and starting new one whenever |
| 5153 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5154 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5155 | * Return in *ended_with_ifs: |
| 5156 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5157 | */ |
| 5158 | 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] | 5159 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5160 | int last_is_ifs = 0; |
| 5161 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5162 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5163 | int word_len; |
| 5164 | |
| 5165 | if (!*str) /* EOL - do not finalize word */ |
| 5166 | break; |
| 5167 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5168 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5169 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5170 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5171 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5172 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5173 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5174 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5175 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5176 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5177 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5178 | /*/ Why can't we do it easier? */ |
| 5179 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5180 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5181 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5182 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5183 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5184 | if (!*str) /* EOL - do not finalize word */ |
| 5185 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5186 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5187 | |
| 5188 | /* We know str here points to at least one IFS char */ |
| 5189 | last_is_ifs = 1; |
| 5190 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5191 | if (!*str) /* EOL - do not finalize word */ |
| 5192 | break; |
| 5193 | |
| 5194 | /* Start new word... but not always! */ |
| 5195 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5196 | if (output->has_quoted_part |
| 5197 | /* Case "v=' a'; echo $v": |
| 5198 | * here nothing precedes the space in $v expansion, |
| 5199 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5200 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5201 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5202 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5203 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5204 | o_addchr(output, '\0'); |
| 5205 | debug_print_list("expand_on_ifs", output, n); |
| 5206 | n = o_save_ptr(output, n); |
| 5207 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5208 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5209 | |
| 5210 | if (ended_with_ifs) |
| 5211 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5212 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5213 | return n; |
| 5214 | } |
| 5215 | |
| 5216 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5217 | * they are in double quotes, with the exception that they are not :). |
| 5218 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5219 | * |
| 5220 | * Returns malloced string. |
| 5221 | * As an optimization, we return NULL if expansion is not needed. |
| 5222 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5223 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5224 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5225 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5226 | encode_then_expand_string(str) |
| 5227 | #endif |
| 5228 | 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] | 5229 | { |
| 5230 | char *exp_str; |
| 5231 | struct in_str input; |
| 5232 | o_string dest = NULL_O_STRING; |
| 5233 | |
| 5234 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5235 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5236 | #if ENABLE_HUSH_TICK |
| 5237 | && !strchr(str, '`') |
| 5238 | #endif |
| 5239 | ) { |
| 5240 | return NULL; |
| 5241 | } |
| 5242 | |
| 5243 | /* We need to expand. Example: |
| 5244 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5245 | */ |
| 5246 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5247 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5248 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5249 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5250 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5251 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5252 | o_free_unsafe(&dest); |
| 5253 | return exp_str; |
| 5254 | } |
| 5255 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5256 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5257 | 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] | 5258 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5259 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5260 | arith_t res; |
| 5261 | char *exp_str; |
| 5262 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5263 | math_state.lookupvar = get_local_var_value; |
| 5264 | math_state.setvar = set_local_var_from_halves; |
| 5265 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5266 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5267 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5268 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5269 | if (errmsg_p) |
| 5270 | *errmsg_p = math_state.errmsg; |
| 5271 | if (math_state.errmsg) |
| 5272 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5273 | return res; |
| 5274 | } |
| 5275 | #endif |
| 5276 | |
| 5277 | #if ENABLE_HUSH_BASH_COMPAT |
| 5278 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5279 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5280 | { |
| 5281 | while (1) { |
| 5282 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5283 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5284 | if (end) { |
| 5285 | *size = end - val; |
| 5286 | return val; |
| 5287 | } |
| 5288 | if (*val == '\0') |
| 5289 | return NULL; |
| 5290 | /* Optimization: if "*pat" did not match the start of "string", |
| 5291 | * we know that "tring", "ring" etc will not match too: |
| 5292 | */ |
| 5293 | if (pattern[0] == '*') |
| 5294 | return NULL; |
| 5295 | val++; |
| 5296 | } |
| 5297 | } |
| 5298 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5299 | { |
| 5300 | char *result = NULL; |
| 5301 | unsigned res_len = 0; |
| 5302 | unsigned repl_len = strlen(repl); |
| 5303 | |
| 5304 | while (1) { |
| 5305 | int size; |
| 5306 | char *s = strstr_pattern(val, pattern, &size); |
| 5307 | if (!s) |
| 5308 | break; |
| 5309 | |
| 5310 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 5311 | memcpy(result + res_len, val, s - val); |
| 5312 | res_len += s - val; |
| 5313 | strcpy(result + res_len, repl); |
| 5314 | res_len += repl_len; |
| 5315 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5316 | |
| 5317 | val = s + size; |
| 5318 | if (exp_op == '/') |
| 5319 | break; |
| 5320 | } |
| 5321 | if (val[0] && result) { |
| 5322 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5323 | strcpy(result + res_len, val); |
| 5324 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5325 | } |
| 5326 | debug_printf_varexp("result:'%s'\n", result); |
| 5327 | return result; |
| 5328 | } |
| 5329 | #endif |
| 5330 | |
| 5331 | /* Helper: |
| 5332 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5333 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5334 | 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] | 5335 | { |
| 5336 | const char *val = NULL; |
| 5337 | char *to_be_freed = NULL; |
| 5338 | char *p = *pp; |
| 5339 | char *var; |
| 5340 | char first_char; |
| 5341 | char exp_op; |
| 5342 | char exp_save = exp_save; /* for compiler */ |
| 5343 | char *exp_saveptr; /* points to expansion operator */ |
| 5344 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5345 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5346 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5347 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5348 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5349 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5350 | arg0 = arg[0]; |
| 5351 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5352 | exp_op = 0; |
| 5353 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5354 | if (first_char == '#' /* ${#... */ |
| 5355 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 5356 | ) { |
| 5357 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5358 | var++; |
| 5359 | exp_op = 'L'; |
| 5360 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5361 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5362 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5363 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5364 | ) { |
| 5365 | /* ${?:0}, ${#[:]%0} etc */ |
| 5366 | exp_saveptr = var + 1; |
| 5367 | } else { |
| 5368 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5369 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5370 | } |
| 5371 | exp_op = exp_save = *exp_saveptr; |
| 5372 | if (exp_op) { |
| 5373 | exp_word = exp_saveptr + 1; |
| 5374 | if (exp_op == ':') { |
| 5375 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5376 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5377 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5378 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5379 | ) { |
| 5380 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5381 | exp_op = ':'; |
| 5382 | exp_word--; |
| 5383 | } |
| 5384 | } |
| 5385 | *exp_saveptr = '\0'; |
| 5386 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5387 | } |
| 5388 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5389 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5390 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5391 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5392 | int n = xatoi_positive(var); |
| 5393 | if (n < G.global_argc) |
| 5394 | val = G.global_argv[n]; |
| 5395 | /* else val remains NULL: $N with too big N */ |
| 5396 | } else { |
| 5397 | switch (var[0]) { |
| 5398 | case '$': /* pid */ |
| 5399 | val = utoa(G.root_pid); |
| 5400 | break; |
| 5401 | case '!': /* bg pid */ |
| 5402 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5403 | break; |
| 5404 | case '?': /* exitcode */ |
| 5405 | val = utoa(G.last_exitcode); |
| 5406 | break; |
| 5407 | case '#': /* argc */ |
| 5408 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5409 | break; |
| 5410 | default: |
| 5411 | val = get_local_var_value(var); |
| 5412 | } |
| 5413 | } |
| 5414 | |
| 5415 | /* Handle any expansions */ |
| 5416 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5417 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5418 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5419 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5420 | debug_printf_expand("%s\n", val); |
| 5421 | } else if (exp_op) { |
| 5422 | if (exp_op == '%' || exp_op == '#') { |
| 5423 | /* Standard-mandated substring removal ops: |
| 5424 | * ${parameter%word} - remove smallest suffix pattern |
| 5425 | * ${parameter%%word} - remove largest suffix pattern |
| 5426 | * ${parameter#word} - remove smallest prefix pattern |
| 5427 | * ${parameter##word} - remove largest prefix pattern |
| 5428 | * |
| 5429 | * Word is expanded to produce a glob pattern. |
| 5430 | * Then var's value is matched to it and matching part removed. |
| 5431 | */ |
| 5432 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5433 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5434 | char *exp_exp_word; |
| 5435 | char *loc; |
| 5436 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5437 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5438 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5439 | 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] | 5440 | if (exp_exp_word) |
| 5441 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5442 | /* HACK ALERT. We depend here on the fact that |
| 5443 | * G.global_argv and results of utoa and get_local_var_value |
| 5444 | * are actually in writable memory: |
| 5445 | * scan_and_match momentarily stores NULs there. */ |
| 5446 | t = (char*)val; |
| 5447 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5448 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5449 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5450 | free(exp_exp_word); |
| 5451 | if (loc) { /* match was found */ |
| 5452 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5453 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5454 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5455 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | #if ENABLE_HUSH_BASH_COMPAT |
| 5460 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5461 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5462 | * Note that in encoded form it has TWO parts: |
| 5463 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5464 | * and if // is used, it is encoded as \: |
| 5465 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5466 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5467 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5468 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5469 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5470 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5471 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5472 | * by the usual expansion rules: |
| 5473 | * >az; >bz; |
| 5474 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5475 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5476 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5477 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5478 | * (note that a*z _pattern_ is never globbed!) |
| 5479 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5480 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5481 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5482 | if (!pattern) |
| 5483 | pattern = xstrdup(exp_word); |
| 5484 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5485 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5486 | exp_word = p; |
| 5487 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5488 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5489 | 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] | 5490 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5491 | /* HACK ALERT. We depend here on the fact that |
| 5492 | * G.global_argv and results of utoa and get_local_var_value |
| 5493 | * are actually in writable memory: |
| 5494 | * replace_pattern momentarily stores NULs there. */ |
| 5495 | t = (char*)val; |
| 5496 | to_be_freed = replace_pattern(t, |
| 5497 | pattern, |
| 5498 | (repl ? repl : exp_word), |
| 5499 | exp_op); |
| 5500 | if (to_be_freed) /* at least one replace happened */ |
| 5501 | val = to_be_freed; |
| 5502 | free(pattern); |
| 5503 | free(repl); |
| 5504 | } |
| 5505 | } |
| 5506 | #endif |
| 5507 | else if (exp_op == ':') { |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5508 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5509 | /* It's ${var:N[:M]} bashism. |
| 5510 | * Note that in encoded form it has TWO parts: |
| 5511 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5512 | */ |
| 5513 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5514 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5515 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5516 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5517 | if (errmsg) |
| 5518 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5519 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5520 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5521 | exp_word = p; |
| 5522 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5523 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5524 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5525 | if (errmsg) |
| 5526 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5527 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5528 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5529 | if (beg < 0) /* bash compat */ |
| 5530 | beg = 0; |
| 5531 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5532 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5533 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5534 | val = NULL; |
| 5535 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5536 | /* Paranoia. What if user entered 9999999999999 |
| 5537 | * which fits in arith_t but not int? */ |
| 5538 | if (len >= INT_MAX) |
| 5539 | len = INT_MAX; |
| 5540 | val = to_be_freed = xstrndup(val + beg, len); |
| 5541 | } |
| 5542 | debug_printf_varexp("val:'%s'\n", val); |
| 5543 | } else |
| 5544 | #endif |
| 5545 | { |
| 5546 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 5547 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5548 | } |
| 5549 | } else { /* one of "-=+?" */ |
| 5550 | /* Standard-mandated substitution ops: |
| 5551 | * ${var?word} - indicate error if unset |
| 5552 | * If var is unset, word (or a message indicating it is unset |
| 5553 | * if word is null) is written to standard error |
| 5554 | * and the shell exits with a non-zero exit status. |
| 5555 | * Otherwise, the value of var is substituted. |
| 5556 | * ${var-word} - use default value |
| 5557 | * If var is unset, word is substituted. |
| 5558 | * ${var=word} - assign and use default value |
| 5559 | * If var is unset, word is assigned to var. |
| 5560 | * In all cases, final value of var is substituted. |
| 5561 | * ${var+word} - use alternative value |
| 5562 | * If var is unset, null is substituted. |
| 5563 | * Otherwise, word is substituted. |
| 5564 | * |
| 5565 | * Word is subjected to tilde expansion, parameter expansion, |
| 5566 | * command substitution, and arithmetic expansion. |
| 5567 | * If word is not needed, it is not expanded. |
| 5568 | * |
| 5569 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5570 | * but also treat null var as if it is unset. |
| 5571 | */ |
| 5572 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5573 | if (exp_op == '+') |
| 5574 | use_word = !use_word; |
| 5575 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5576 | (exp_save == ':') ? "true" : "false", use_word); |
| 5577 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5578 | 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] | 5579 | if (to_be_freed) |
| 5580 | exp_word = to_be_freed; |
| 5581 | if (exp_op == '?') { |
| 5582 | /* mimic bash message */ |
| 5583 | die_if_script("%s: %s", |
| 5584 | var, |
| 5585 | exp_word[0] ? exp_word : "parameter null or not set" |
| 5586 | ); |
| 5587 | //TODO: how interactive bash aborts expansion mid-command? |
| 5588 | } else { |
| 5589 | val = exp_word; |
| 5590 | } |
| 5591 | |
| 5592 | if (exp_op == '=') { |
| 5593 | /* ${var=[word]} or ${var:=[word]} */ |
| 5594 | if (isdigit(var[0]) || var[0] == '#') { |
| 5595 | /* mimic bash message */ |
| 5596 | die_if_script("$%s: cannot assign in this way", var); |
| 5597 | val = NULL; |
| 5598 | } else { |
| 5599 | char *new_var = xasprintf("%s=%s", var, val); |
| 5600 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 5601 | } |
| 5602 | } |
| 5603 | } |
| 5604 | } /* one of "-=+?" */ |
| 5605 | |
| 5606 | *exp_saveptr = exp_save; |
| 5607 | } /* if (exp_op) */ |
| 5608 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5609 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5610 | |
| 5611 | *pp = p; |
| 5612 | *to_be_freed_pp = to_be_freed; |
| 5613 | return val; |
| 5614 | } |
| 5615 | |
| 5616 | /* Expand all variable references in given string, adding words to list[] |
| 5617 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5618 | * to be filled). This routine is extremely tricky: has to deal with |
| 5619 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5620 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5621 | 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] | 5622 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5623 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5624 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5625 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5626 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5627 | 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] | 5628 | char *p; |
| 5629 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5630 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5631 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5632 | debug_print_list("expand_vars_to_list", output, n); |
| 5633 | n = o_save_ptr(output, n); |
| 5634 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5635 | |
| 5636 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5637 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5638 | char *to_be_freed = NULL; |
| 5639 | const char *val = NULL; |
| 5640 | #if ENABLE_HUSH_TICK |
| 5641 | o_string subst_result = NULL_O_STRING; |
| 5642 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5643 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5644 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5645 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5646 | |
| 5647 | if (ended_in_ifs) { |
| 5648 | o_addchr(output, '\0'); |
| 5649 | n = o_save_ptr(output, n); |
| 5650 | ended_in_ifs = 0; |
| 5651 | } |
| 5652 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5653 | o_addblock(output, arg, p - arg); |
| 5654 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5655 | arg = ++p; |
| 5656 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5657 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5658 | /* Fetch special var name (if it is indeed one of them) |
| 5659 | * and quote bit, force the bit on if singleword expansion - |
| 5660 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5661 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5662 | |
| 5663 | /* Is this variable quoted and thus expansion can't be null? |
| 5664 | * "$@" is special. Even if quoted, it can still |
| 5665 | * expand to nothing (not even an empty string), |
| 5666 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5667 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5668 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5669 | |
| 5670 | switch (first_ch & 0x7f) { |
| 5671 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5672 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5673 | case '@': { |
| 5674 | int i; |
| 5675 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5676 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5677 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5678 | 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] | 5679 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5680 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5681 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5682 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5683 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5684 | /* this argv[] is not empty and not last: |
| 5685 | * put terminating NUL, start new word */ |
| 5686 | o_addchr(output, '\0'); |
| 5687 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5688 | n = o_save_ptr(output, n); |
| 5689 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5690 | } |
| 5691 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5692 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5693 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5694 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5695 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5696 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5697 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5698 | while (1) { |
| 5699 | o_addQstr(output, G.global_argv[i]); |
| 5700 | if (++i >= G.global_argc) |
| 5701 | break; |
| 5702 | o_addchr(output, '\0'); |
| 5703 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5704 | n = o_save_ptr(output, n); |
| 5705 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5706 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5707 | while (1) { |
| 5708 | o_addQstr(output, G.global_argv[i]); |
| 5709 | if (!G.global_argv[++i]) |
| 5710 | break; |
| 5711 | if (G.ifs[0]) |
| 5712 | o_addchr(output, G.ifs[0]); |
| 5713 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5714 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5715 | } |
| 5716 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5717 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5718 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5719 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5720 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5721 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5722 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5723 | break; |
| 5724 | #if ENABLE_HUSH_TICK |
| 5725 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5726 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5727 | arg++; |
| 5728 | /* Can't just stuff it into output o_string, |
| 5729 | * expanded result may need to be globbed |
| 5730 | * and $IFS-splitted */ |
| 5731 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5732 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5733 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5734 | val = subst_result.data; |
| 5735 | goto store_val; |
| 5736 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5737 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5738 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5739 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5740 | |
| 5741 | arg++; /* skip '+' */ |
| 5742 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5743 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5744 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5745 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5746 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5747 | val = arith_buf; |
| 5748 | break; |
| 5749 | } |
| 5750 | #endif |
| 5751 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5752 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5753 | IF_HUSH_TICK(store_val:) |
| 5754 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5755 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5756 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5757 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5758 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5759 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5760 | } |
| 5761 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5762 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5763 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5764 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5765 | } |
| 5766 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5767 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5768 | |
| 5769 | if (val && val[0]) { |
| 5770 | o_addQstr(output, val); |
| 5771 | } |
| 5772 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5773 | |
| 5774 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5775 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5776 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5777 | *p = SPECIAL_VAR_SYMBOL; |
| 5778 | |
| 5779 | #if ENABLE_HUSH_TICK |
| 5780 | o_free(&subst_result); |
| 5781 | #endif |
| 5782 | arg = ++p; |
| 5783 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5784 | |
| 5785 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5786 | if (ended_in_ifs) { |
| 5787 | o_addchr(output, '\0'); |
| 5788 | n = o_save_ptr(output, n); |
| 5789 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5790 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5791 | /* this part is literal, and it was already pre-quoted |
| 5792 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5793 | o_addstr_with_NUL(output, arg); |
| 5794 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5795 | } 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] | 5796 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5797 | ) { |
| 5798 | n--; |
| 5799 | /* allow to reuse list[n] later without re-growth */ |
| 5800 | output->has_empty_slot = 1; |
| 5801 | } else { |
| 5802 | o_addchr(output, '\0'); |
| 5803 | } |
| 5804 | |
| 5805 | return n; |
| 5806 | } |
| 5807 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5808 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5809 | { |
| 5810 | int n; |
| 5811 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5812 | o_string output = NULL_O_STRING; |
| 5813 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5814 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5815 | |
| 5816 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5817 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5818 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5819 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5820 | } |
| 5821 | debug_print_list("expand_variables", &output, n); |
| 5822 | |
| 5823 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5824 | list = o_finalize_list(&output, n); |
| 5825 | debug_print_strings("expand_variables[1]", list); |
| 5826 | return list; |
| 5827 | } |
| 5828 | |
| 5829 | static char **expand_strvec_to_strvec(char **argv) |
| 5830 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5831 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | #if ENABLE_HUSH_BASH_COMPAT |
| 5835 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5836 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5837 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5838 | } |
| 5839 | #endif |
| 5840 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5841 | /* Used for expansion of right hand of assignments, |
| 5842 | * $((...)), heredocs, variable espansion parts. |
| 5843 | * |
| 5844 | * NB: should NOT do globbing! |
| 5845 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5846 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5847 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5848 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5849 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5850 | const int do_unbackslash = 1; |
| 5851 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5852 | char *argv[2], **list; |
| 5853 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5854 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5855 | /* This is generally an optimization, but it also |
| 5856 | * handles "", which otherwise trips over !list[0] check below. |
| 5857 | * (is this ever happens that we actually get str="" here?) |
| 5858 | */ |
| 5859 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5860 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5861 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5862 | return xstrdup(str); |
| 5863 | } |
| 5864 | |
| 5865 | argv[0] = (char*)str; |
| 5866 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5867 | list = expand_variables(argv, do_unbackslash |
| 5868 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5869 | : EXP_FLAG_SINGLEWORD |
| 5870 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5871 | if (HUSH_DEBUG) |
| 5872 | if (!list[0] || list[1]) |
| 5873 | bb_error_msg_and_die("BUG in varexp2"); |
| 5874 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5875 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5876 | if (do_unbackslash) |
| 5877 | unbackslash((char*)list); |
| 5878 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5879 | return (char*)list; |
| 5880 | } |
| 5881 | |
| 5882 | /* Used for "eval" builtin */ |
| 5883 | static char* expand_strvec_to_string(char **argv) |
| 5884 | { |
| 5885 | char **list; |
| 5886 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5887 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5888 | /* Convert all NULs to spaces */ |
| 5889 | if (list[0]) { |
| 5890 | int n = 1; |
| 5891 | while (list[n]) { |
| 5892 | if (HUSH_DEBUG) |
| 5893 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5894 | bb_error_msg_and_die("BUG in varexp3"); |
| 5895 | /* bash uses ' ' regardless of $IFS contents */ |
| 5896 | list[n][-1] = ' '; |
| 5897 | n++; |
| 5898 | } |
| 5899 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 5900 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5901 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5902 | return (char*)list; |
| 5903 | } |
| 5904 | |
| 5905 | static char **expand_assignments(char **argv, int count) |
| 5906 | { |
| 5907 | int i; |
| 5908 | char **p; |
| 5909 | |
| 5910 | G.expanded_assignments = p = NULL; |
| 5911 | /* Expand assignments into one string each */ |
| 5912 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5913 | 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] | 5914 | } |
| 5915 | G.expanded_assignments = NULL; |
| 5916 | return p; |
| 5917 | } |
| 5918 | |
| 5919 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5920 | static void switch_off_special_sigs(unsigned mask) |
| 5921 | { |
| 5922 | unsigned sig = 0; |
| 5923 | while ((mask >>= 1) != 0) { |
| 5924 | sig++; |
| 5925 | if (!(mask & 1)) |
| 5926 | continue; |
| 5927 | if (G.traps) { |
| 5928 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5929 | /* trap is '', has to remain SIG_IGN */ |
| 5930 | continue; |
| 5931 | free(G.traps[sig]); |
| 5932 | G.traps[sig] = NULL; |
| 5933 | } |
| 5934 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5935 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5936 | } |
| 5937 | } |
| 5938 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 5939 | #if BB_MMU |
| 5940 | /* never called */ |
| 5941 | void re_execute_shell(char ***to_free, const char *s, |
| 5942 | char *g_argv0, char **g_argv, |
| 5943 | char **builtin_argv) NORETURN; |
| 5944 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5945 | static void reset_traps_to_defaults(void) |
| 5946 | { |
| 5947 | /* This function is always called in a child shell |
| 5948 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5949 | */ |
| 5950 | unsigned sig; |
| 5951 | unsigned mask; |
| 5952 | |
| 5953 | /* Child shells are not interactive. |
| 5954 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5955 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5956 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5957 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5958 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
| 5959 | if (!G.traps && !mask) |
| 5960 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5961 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5962 | /* Switch off special sigs */ |
| 5963 | switch_off_special_sigs(mask); |
| 5964 | #if ENABLE_HUSH_JOB |
| 5965 | G_fatal_sig_mask = 0; |
| 5966 | #endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 5967 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 5968 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 5969 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5970 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5971 | if (!G.traps) |
| 5972 | return; |
| 5973 | |
| 5974 | /* Reset all sigs to default except ones with empty traps */ |
| 5975 | for (sig = 0; sig < NSIG; sig++) { |
| 5976 | if (!G.traps[sig]) |
| 5977 | continue; /* no trap: nothing to do */ |
| 5978 | if (!G.traps[sig][0]) |
| 5979 | continue; /* empty trap: has to remain SIG_IGN */ |
| 5980 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5981 | free(G.traps[sig]); |
| 5982 | G.traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 5983 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5984 | if (sig == 0) |
| 5985 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 5986 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5987 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | #else /* !BB_MMU */ |
| 5991 | |
| 5992 | static void re_execute_shell(char ***to_free, const char *s, |
| 5993 | char *g_argv0, char **g_argv, |
| 5994 | char **builtin_argv) NORETURN; |
| 5995 | static void re_execute_shell(char ***to_free, const char *s, |
| 5996 | char *g_argv0, char **g_argv, |
| 5997 | char **builtin_argv) |
| 5998 | { |
| 5999 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6000 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6001 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6002 | char *heredoc_argv[4]; |
| 6003 | struct variable *cur; |
| 6004 | # if ENABLE_HUSH_FUNCTIONS |
| 6005 | struct function *funcp; |
| 6006 | # endif |
| 6007 | char **argv, **pp; |
| 6008 | unsigned cnt; |
| 6009 | unsigned long long empty_trap_mask; |
| 6010 | |
| 6011 | if (!g_argv0) { /* heredoc */ |
| 6012 | argv = heredoc_argv; |
| 6013 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6014 | argv[1] = (char *) "-<"; |
| 6015 | argv[2] = (char *) s; |
| 6016 | argv[3] = NULL; |
| 6017 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6018 | goto do_exec; |
| 6019 | } |
| 6020 | |
| 6021 | cnt = 0; |
| 6022 | pp = builtin_argv; |
| 6023 | if (pp) while (*pp++) |
| 6024 | cnt++; |
| 6025 | |
| 6026 | empty_trap_mask = 0; |
| 6027 | if (G.traps) { |
| 6028 | int sig; |
| 6029 | for (sig = 1; sig < NSIG; sig++) { |
| 6030 | if (G.traps[sig] && !G.traps[sig][0]) |
| 6031 | empty_trap_mask |= 1LL << sig; |
| 6032 | } |
| 6033 | } |
| 6034 | |
| 6035 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6036 | , (unsigned) G.root_pid |
| 6037 | , (unsigned) G.root_ppid |
| 6038 | , (unsigned) G.last_bg_pid |
| 6039 | , (unsigned) G.last_exitcode |
| 6040 | , cnt |
| 6041 | , empty_trap_mask |
| 6042 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6043 | ); |
| 6044 | # undef NOMMU_HACK_FMT |
| 6045 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6046 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6047 | */ |
| 6048 | cnt += 6; |
| 6049 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6050 | if (!cur->flg_export || cur->flg_read_only) |
| 6051 | cnt += 2; |
| 6052 | } |
| 6053 | # if ENABLE_HUSH_FUNCTIONS |
| 6054 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6055 | cnt += 3; |
| 6056 | # endif |
| 6057 | pp = g_argv; |
| 6058 | while (*pp++) |
| 6059 | cnt++; |
| 6060 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6061 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6062 | *pp++ = param_buf; |
| 6063 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6064 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6065 | continue; |
| 6066 | if (cur->flg_read_only) { |
| 6067 | *pp++ = (char *) "-R"; |
| 6068 | *pp++ = cur->varstr; |
| 6069 | } else if (!cur->flg_export) { |
| 6070 | *pp++ = (char *) "-V"; |
| 6071 | *pp++ = cur->varstr; |
| 6072 | } |
| 6073 | } |
| 6074 | # if ENABLE_HUSH_FUNCTIONS |
| 6075 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6076 | *pp++ = (char *) "-F"; |
| 6077 | *pp++ = funcp->name; |
| 6078 | *pp++ = funcp->body_as_string; |
| 6079 | } |
| 6080 | # endif |
| 6081 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6082 | * |
| 6083 | * However, POSIX says that subshells reset signals with traps |
| 6084 | * to SIG_DFL. |
| 6085 | * I tested bash-3.2 and it not only does that with true subshells |
| 6086 | * of the form ( list ), but with any forked children shells. |
| 6087 | * I set trap "echo W" WINCH; and then tried: |
| 6088 | * |
| 6089 | * { echo 1; sleep 20; echo 2; } & |
| 6090 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6091 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6092 | * |
| 6093 | * In all these cases sending SIGWINCH to the child shell |
| 6094 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6095 | * _inside_ group (just before echo 1), it works. |
| 6096 | * |
| 6097 | * 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] | 6098 | */ |
| 6099 | *pp++ = (char *) "-c"; |
| 6100 | *pp++ = (char *) s; |
| 6101 | if (builtin_argv) { |
| 6102 | while (*++builtin_argv) |
| 6103 | *pp++ = *builtin_argv; |
| 6104 | *pp++ = (char *) ""; |
| 6105 | } |
| 6106 | *pp++ = g_argv0; |
| 6107 | while (*g_argv) |
| 6108 | *pp++ = *g_argv++; |
| 6109 | /* *pp = NULL; - is already there */ |
| 6110 | pp = environ; |
| 6111 | |
| 6112 | do_exec: |
| 6113 | 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] | 6114 | /* Don't propagate SIG_IGN to the child */ |
| 6115 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6116 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6117 | execve(bb_busybox_exec_path, argv, pp); |
| 6118 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6119 | if (argv[0][0] == '/') |
| 6120 | execve(argv[0], argv, pp); |
| 6121 | xfunc_error_retval = 127; |
| 6122 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6123 | } |
| 6124 | #endif /* !BB_MMU */ |
| 6125 | |
| 6126 | |
| 6127 | static int run_and_free_list(struct pipe *pi); |
| 6128 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6129 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6130 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6131 | * end_trigger controls how often we stop parsing |
| 6132 | * NUL: parse all, execute, return |
| 6133 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6134 | */ |
| 6135 | 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] | 6136 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6137 | /* Why we need empty flag? |
| 6138 | * An obscure corner case "false; ``; echo $?": |
| 6139 | * empty command in `` should still set $? to 0. |
| 6140 | * But we can't just set $? to 0 at the start, |
| 6141 | * this breaks "false; echo `echo $?`" case. |
| 6142 | */ |
| 6143 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6144 | while (1) { |
| 6145 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6146 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6147 | #if ENABLE_HUSH_INTERACTIVE |
| 6148 | if (end_trigger == ';') |
| 6149 | inp->promptmode = 0; /* PS1 */ |
| 6150 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6151 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6152 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6153 | /* If we are in "big" script |
| 6154 | * (not in `cmd` or something similar)... |
| 6155 | */ |
| 6156 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6157 | /* Discard cached input (rest of line) */ |
| 6158 | int ch = inp->last_char; |
| 6159 | while (ch != EOF && ch != '\n') { |
| 6160 | //bb_error_msg("Discarded:'%c'", ch); |
| 6161 | ch = i_getch(inp); |
| 6162 | } |
| 6163 | /* Force prompt */ |
| 6164 | inp->p = NULL; |
| 6165 | /* This stream isn't empty */ |
| 6166 | empty = 0; |
| 6167 | continue; |
| 6168 | } |
| 6169 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6170 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6171 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6172 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6173 | debug_print_tree(pipe_list, 0); |
| 6174 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6175 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6176 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6177 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6178 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6179 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6182 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6183 | { |
| 6184 | struct in_str input; |
| 6185 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6186 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6187 | } |
| 6188 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6189 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6190 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6191 | struct in_str input; |
| 6192 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6193 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6196 | #if ENABLE_HUSH_TICK |
| 6197 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6198 | { |
| 6199 | pid_t pid; |
| 6200 | int channel[2]; |
| 6201 | # if !BB_MMU |
| 6202 | char **to_free = NULL; |
| 6203 | # endif |
| 6204 | |
| 6205 | xpipe(channel); |
| 6206 | pid = BB_MMU ? xfork() : xvfork(); |
| 6207 | if (pid == 0) { /* child */ |
| 6208 | disable_restore_tty_pgrp_on_exit(); |
| 6209 | /* Process substitution is not considered to be usual |
| 6210 | * 'command execution'. |
| 6211 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6212 | */ |
| 6213 | bb_signals(0 |
| 6214 | + (1 << SIGTSTP) |
| 6215 | + (1 << SIGTTIN) |
| 6216 | + (1 << SIGTTOU) |
| 6217 | , SIG_IGN); |
| 6218 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6219 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6220 | xmove_fd(channel[1], 1); |
| 6221 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6222 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 6223 | /* Awful hack for `trap` or $(trap). |
| 6224 | * |
| 6225 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6226 | * contains an example where "trap" is executed in a subshell: |
| 6227 | * |
| 6228 | * save_traps=$(trap) |
| 6229 | * ... |
| 6230 | * eval "$save_traps" |
| 6231 | * |
| 6232 | * Standard does not say that "trap" in subshell shall print |
| 6233 | * parent shell's traps. It only says that its output |
| 6234 | * must have suitable form, but then, in the above example |
| 6235 | * (which is not supposed to be normative), it implies that. |
| 6236 | * |
| 6237 | * bash (and probably other shell) does implement it |
| 6238 | * (traps are reset to defaults, but "trap" still shows them), |
| 6239 | * but as a result, "trap" logic is hopelessly messed up: |
| 6240 | * |
| 6241 | * # trap |
| 6242 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6243 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6244 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6245 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6246 | * trap -- 'echo Ho' SIGWINCH |
| 6247 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6248 | * trap -- 'echo Ho' SIGWINCH |
| 6249 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6250 | * trap -- 'echo Ho' SIGWINCH |
| 6251 | * |
| 6252 | * The rules when to forget and when to not forget traps |
| 6253 | * get really complex and nonsensical. |
| 6254 | * |
| 6255 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6256 | */ |
| 6257 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6258 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6259 | && skip_whitespace(s + 4)[0] == '\0' |
| 6260 | ) { |
| 6261 | static const char *const argv[] = { NULL, NULL }; |
| 6262 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6263 | fflush_all(); /* important */ |
| 6264 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6265 | } |
| 6266 | # if BB_MMU |
| 6267 | reset_traps_to_defaults(); |
| 6268 | parse_and_run_string(s); |
| 6269 | _exit(G.last_exitcode); |
| 6270 | # else |
| 6271 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6272 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6273 | * huge=`cat BIG` # was blocking here forever |
| 6274 | * echo OK |
| 6275 | */ |
| 6276 | re_execute_shell(&to_free, |
| 6277 | s, |
| 6278 | G.global_argv[0], |
| 6279 | G.global_argv + 1, |
| 6280 | NULL); |
| 6281 | # endif |
| 6282 | } |
| 6283 | |
| 6284 | /* parent */ |
| 6285 | *pid_p = pid; |
| 6286 | # if ENABLE_HUSH_FAST |
| 6287 | G.count_SIGCHLD++; |
| 6288 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6289 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6290 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6291 | # endif |
| 6292 | enable_restore_tty_pgrp_on_exit(); |
| 6293 | # if !BB_MMU |
| 6294 | free(to_free); |
| 6295 | # endif |
| 6296 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6297 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6298 | } |
| 6299 | |
| 6300 | /* Return code is exit status of the process that is run. */ |
| 6301 | static int process_command_subs(o_string *dest, const char *s) |
| 6302 | { |
| 6303 | FILE *fp; |
| 6304 | struct in_str pipe_str; |
| 6305 | pid_t pid; |
| 6306 | int status, ch, eol_cnt; |
| 6307 | |
| 6308 | fp = generate_stream_from_string(s, &pid); |
| 6309 | |
| 6310 | /* Now send results of command back into original context */ |
| 6311 | setup_file_in_str(&pipe_str, fp); |
| 6312 | eol_cnt = 0; |
| 6313 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 6314 | if (ch == '\n') { |
| 6315 | eol_cnt++; |
| 6316 | continue; |
| 6317 | } |
| 6318 | while (eol_cnt) { |
| 6319 | o_addchr(dest, '\n'); |
| 6320 | eol_cnt--; |
| 6321 | } |
| 6322 | o_addQchr(dest, ch); |
| 6323 | } |
| 6324 | |
| 6325 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6326 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6327 | /* We need to extract exitcode. Test case |
| 6328 | * "true; echo `sleep 1; false` $?" |
| 6329 | * should print 1 */ |
| 6330 | safe_waitpid(pid, &status, 0); |
| 6331 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6332 | return WEXITSTATUS(status); |
| 6333 | } |
| 6334 | #endif /* ENABLE_HUSH_TICK */ |
| 6335 | |
| 6336 | |
| 6337 | static void setup_heredoc(struct redir_struct *redir) |
| 6338 | { |
| 6339 | struct fd_pair pair; |
| 6340 | pid_t pid; |
| 6341 | int len, written; |
| 6342 | /* the _body_ of heredoc (misleading field name) */ |
| 6343 | const char *heredoc = redir->rd_filename; |
| 6344 | char *expanded; |
| 6345 | #if !BB_MMU |
| 6346 | char **to_free; |
| 6347 | #endif |
| 6348 | |
| 6349 | expanded = NULL; |
| 6350 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6351 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6352 | if (expanded) |
| 6353 | heredoc = expanded; |
| 6354 | } |
| 6355 | len = strlen(heredoc); |
| 6356 | |
| 6357 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6358 | xpiped_pair(pair); |
| 6359 | xmove_fd(pair.rd, redir->rd_fd); |
| 6360 | |
| 6361 | /* Try writing without forking. Newer kernels have |
| 6362 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6363 | ndelay_on(pair.wr); |
| 6364 | while (1) { |
| 6365 | written = write(pair.wr, heredoc, len); |
| 6366 | if (written <= 0) |
| 6367 | break; |
| 6368 | len -= written; |
| 6369 | if (len == 0) { |
| 6370 | close(pair.wr); |
| 6371 | free(expanded); |
| 6372 | return; |
| 6373 | } |
| 6374 | heredoc += written; |
| 6375 | } |
| 6376 | ndelay_off(pair.wr); |
| 6377 | |
| 6378 | /* Okay, pipe buffer was not big enough */ |
| 6379 | /* Note: we must not create a stray child (bastard? :) |
| 6380 | * for the unsuspecting parent process. Child creates a grandchild |
| 6381 | * and exits before parent execs the process which consumes heredoc |
| 6382 | * (that exec happens after we return from this function) */ |
| 6383 | #if !BB_MMU |
| 6384 | to_free = NULL; |
| 6385 | #endif |
| 6386 | pid = xvfork(); |
| 6387 | if (pid == 0) { |
| 6388 | /* child */ |
| 6389 | disable_restore_tty_pgrp_on_exit(); |
| 6390 | pid = BB_MMU ? xfork() : xvfork(); |
| 6391 | if (pid != 0) |
| 6392 | _exit(0); |
| 6393 | /* grandchild */ |
| 6394 | close(redir->rd_fd); /* read side of the pipe */ |
| 6395 | #if BB_MMU |
| 6396 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6397 | _exit(0); |
| 6398 | #else |
| 6399 | /* Delegate blocking writes to another process */ |
| 6400 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6401 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6402 | #endif |
| 6403 | } |
| 6404 | /* parent */ |
| 6405 | #if ENABLE_HUSH_FAST |
| 6406 | G.count_SIGCHLD++; |
| 6407 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6408 | #endif |
| 6409 | enable_restore_tty_pgrp_on_exit(); |
| 6410 | #if !BB_MMU |
| 6411 | free(to_free); |
| 6412 | #endif |
| 6413 | close(pair.wr); |
| 6414 | free(expanded); |
| 6415 | wait(NULL); /* wait till child has died */ |
| 6416 | } |
| 6417 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6418 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 6419 | * Move all conflicting internally used fds, |
| 6420 | * and remember them so that we can restore them later. |
| 6421 | */ |
| 6422 | static int save_fds_on_redirect(int fd, int squirrel[3]) |
| 6423 | { |
| 6424 | if (squirrel) { |
| 6425 | /* Handle redirects of fds 0,1,2 */ |
| 6426 | |
| 6427 | /* If we collide with an already moved stdio fd... */ |
| 6428 | if (fd == squirrel[0]) { |
| 6429 | squirrel[0] = xdup_and_close(squirrel[0], F_DUPFD); |
| 6430 | return 1; |
| 6431 | } |
| 6432 | if (fd == squirrel[1]) { |
| 6433 | squirrel[1] = xdup_and_close(squirrel[1], F_DUPFD); |
| 6434 | return 1; |
| 6435 | } |
| 6436 | if (fd == squirrel[2]) { |
| 6437 | squirrel[2] = xdup_and_close(squirrel[2], F_DUPFD); |
| 6438 | return 1; |
| 6439 | } |
| 6440 | /* If we are about to redirect stdio fd, and did not yet move it... */ |
| 6441 | if (fd <= 2 && squirrel[fd] < 0) { |
| 6442 | /* We avoid taking stdio fds */ |
| 6443 | squirrel[fd] = fcntl(fd, F_DUPFD, 10); |
| 6444 | if (squirrel[fd] < 0 && errno != EBADF) |
| 6445 | xfunc_die(); |
| 6446 | return 0; /* "we did not close fd" */ |
| 6447 | } |
| 6448 | } |
| 6449 | |
| 6450 | #if ENABLE_HUSH_INTERACTIVE |
| 6451 | if (fd != 0 && fd == G.interactive_fd) { |
| 6452 | G.interactive_fd = xdup_and_close(G.interactive_fd, F_DUPFD_CLOEXEC); |
| 6453 | return 1; |
| 6454 | } |
| 6455 | #endif |
| 6456 | |
| 6457 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 6458 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 6459 | * we aren't going to use them anymore, ok to trash. |
| 6460 | * (2) "exec 3>FILE". Bummer. We can save FILEs' fds, |
| 6461 | * but how are we doing to use them? |
| 6462 | * "fileno(fd) = new_fd" can't be done. |
| 6463 | */ |
| 6464 | if (!squirrel) |
| 6465 | return 0; |
| 6466 | |
| 6467 | return save_FILEs_on_redirect(fd); |
| 6468 | } |
| 6469 | |
| 6470 | static void restore_redirects(int squirrel[3]) |
| 6471 | { |
| 6472 | int i, fd; |
| 6473 | for (i = 0; i <= 2; i++) { |
| 6474 | fd = squirrel[i]; |
| 6475 | if (fd != -1) { |
| 6476 | /* We simply die on error */ |
| 6477 | xmove_fd(fd, i); |
| 6478 | } |
| 6479 | } |
| 6480 | |
| 6481 | /* Moved G.interactive_fd stays on new fd, not doing anything for it */ |
| 6482 | |
| 6483 | restore_redirected_FILEs(); |
| 6484 | } |
| 6485 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6486 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6487 | * and stderr if they are redirected. */ |
| 6488 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 6489 | { |
| 6490 | int openfd, mode; |
| 6491 | struct redir_struct *redir; |
| 6492 | |
| 6493 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 6494 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6495 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6496 | save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6497 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6498 | * of the heredoc */ |
| 6499 | debug_printf_parse("set heredoc '%s'\n", |
| 6500 | redir->rd_filename); |
| 6501 | setup_heredoc(redir); |
| 6502 | continue; |
| 6503 | } |
| 6504 | |
| 6505 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6506 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6507 | char *p; |
| 6508 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 6509 | /* |
| 6510 | * Examples: |
| 6511 | * "cmd >" (no filename) |
| 6512 | * "cmd > <file" (2nd redirect starts too early) |
| 6513 | */ |
| 6514 | die_if_script("syntax error: %s", "invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6515 | continue; |
| 6516 | } |
| 6517 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6518 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6519 | openfd = open_or_warn(p, mode); |
| 6520 | free(p); |
| 6521 | if (openfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6522 | /* Error message from open_or_warn can be lost |
| 6523 | * if stderr has been redirected, but bash |
| 6524 | * and ash both lose it as well |
| 6525 | * (though zsh doesn't!) |
| 6526 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6527 | return 1; |
| 6528 | } |
| 6529 | } else { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6530 | /* "rd_fd<*>rd_dup" or "rd_fd<*>-" cases */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6531 | openfd = redir->rd_dup; |
| 6532 | } |
| 6533 | |
| 6534 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6535 | int closed = save_fds_on_redirect(redir->rd_fd, squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6536 | if (openfd == REDIRFD_CLOSE) { |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6537 | /* "rd_fd >&-" means "close me" */ |
| 6538 | if (!closed) { |
| 6539 | /* ^^^ optimization: saving may already |
| 6540 | * have closed it. If not... */ |
| 6541 | close(redir->rd_fd); |
| 6542 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6543 | } else { |
| 6544 | xdup2(openfd, redir->rd_fd); |
| 6545 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6546 | /* "rd_fd > FILE" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6547 | close(openfd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6548 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6549 | } |
| 6550 | } |
| 6551 | } |
| 6552 | return 0; |
| 6553 | } |
| 6554 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6555 | static char *find_in_path(const char *arg) |
| 6556 | { |
| 6557 | char *ret = NULL; |
| 6558 | const char *PATH = get_local_var_value("PATH"); |
| 6559 | |
| 6560 | if (!PATH) |
| 6561 | return NULL; |
| 6562 | |
| 6563 | while (1) { |
| 6564 | const char *end = strchrnul(PATH, ':'); |
| 6565 | int sz = end - PATH; /* must be int! */ |
| 6566 | |
| 6567 | free(ret); |
| 6568 | if (sz != 0) { |
| 6569 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6570 | } else { |
| 6571 | /* We have xxx::yyyy in $PATH, |
| 6572 | * it means "use current dir" */ |
| 6573 | ret = xstrdup(arg); |
| 6574 | } |
| 6575 | if (access(ret, F_OK) == 0) |
| 6576 | break; |
| 6577 | |
| 6578 | if (*end == '\0') { |
| 6579 | free(ret); |
| 6580 | return NULL; |
| 6581 | } |
| 6582 | PATH = end + 1; |
| 6583 | } |
| 6584 | |
| 6585 | return ret; |
| 6586 | } |
| 6587 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6588 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6589 | const struct built_in_command *x, |
| 6590 | const struct built_in_command *end) |
| 6591 | { |
| 6592 | while (x != end) { |
| 6593 | if (strcmp(name, x->b_cmd) != 0) { |
| 6594 | x++; |
| 6595 | continue; |
| 6596 | } |
| 6597 | debug_printf_exec("found builtin '%s'\n", name); |
| 6598 | return x; |
| 6599 | } |
| 6600 | return NULL; |
| 6601 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6602 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6603 | { |
| 6604 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 6605 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6606 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6607 | { |
| 6608 | const struct built_in_command *x = find_builtin1(name); |
| 6609 | if (x) |
| 6610 | return x; |
| 6611 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 6612 | } |
| 6613 | |
| 6614 | #if ENABLE_HUSH_FUNCTIONS |
| 6615 | static struct function **find_function_slot(const char *name) |
| 6616 | { |
| 6617 | struct function **funcpp = &G.top_func; |
| 6618 | while (*funcpp) { |
| 6619 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 6620 | break; |
| 6621 | } |
| 6622 | funcpp = &(*funcpp)->next; |
| 6623 | } |
| 6624 | return funcpp; |
| 6625 | } |
| 6626 | |
| 6627 | static const struct function *find_function(const char *name) |
| 6628 | { |
| 6629 | const struct function *funcp = *find_function_slot(name); |
| 6630 | if (funcp) |
| 6631 | debug_printf_exec("found function '%s'\n", name); |
| 6632 | return funcp; |
| 6633 | } |
| 6634 | |
| 6635 | /* Note: takes ownership on name ptr */ |
| 6636 | static struct function *new_function(char *name) |
| 6637 | { |
| 6638 | struct function **funcpp = find_function_slot(name); |
| 6639 | struct function *funcp = *funcpp; |
| 6640 | |
| 6641 | if (funcp != NULL) { |
| 6642 | struct command *cmd = funcp->parent_cmd; |
| 6643 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 6644 | if (!cmd) { |
| 6645 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 6646 | free(funcp->name); |
| 6647 | /* Note: if !funcp->body, do not free body_as_string! |
| 6648 | * This is a special case of "-F name body" function: |
| 6649 | * body_as_string was not malloced! */ |
| 6650 | if (funcp->body) { |
| 6651 | free_pipe_list(funcp->body); |
| 6652 | # if !BB_MMU |
| 6653 | free(funcp->body_as_string); |
| 6654 | # endif |
| 6655 | } |
| 6656 | } else { |
| 6657 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 6658 | cmd->argv[0] = funcp->name; |
| 6659 | cmd->group = funcp->body; |
| 6660 | # if !BB_MMU |
| 6661 | cmd->group_as_string = funcp->body_as_string; |
| 6662 | # endif |
| 6663 | } |
| 6664 | } else { |
| 6665 | debug_printf_exec("remembering new function '%s'\n", name); |
| 6666 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 6667 | /*funcp->next = NULL;*/ |
| 6668 | } |
| 6669 | |
| 6670 | funcp->name = name; |
| 6671 | return funcp; |
| 6672 | } |
| 6673 | |
| 6674 | static void unset_func(const char *name) |
| 6675 | { |
| 6676 | struct function **funcpp = find_function_slot(name); |
| 6677 | struct function *funcp = *funcpp; |
| 6678 | |
| 6679 | if (funcp != NULL) { |
| 6680 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 6681 | *funcpp = funcp->next; |
| 6682 | /* funcp is unlinked now, deleting it. |
| 6683 | * Note: if !funcp->body, the function was created by |
| 6684 | * "-F name body", do not free ->body_as_string |
| 6685 | * and ->name as they were not malloced. */ |
| 6686 | if (funcp->body) { |
| 6687 | free_pipe_list(funcp->body); |
| 6688 | free(funcp->name); |
| 6689 | # if !BB_MMU |
| 6690 | free(funcp->body_as_string); |
| 6691 | # endif |
| 6692 | } |
| 6693 | free(funcp); |
| 6694 | } |
| 6695 | } |
| 6696 | |
| 6697 | # if BB_MMU |
| 6698 | #define exec_function(to_free, funcp, argv) \ |
| 6699 | exec_function(funcp, argv) |
| 6700 | # endif |
| 6701 | static void exec_function(char ***to_free, |
| 6702 | const struct function *funcp, |
| 6703 | char **argv) NORETURN; |
| 6704 | static void exec_function(char ***to_free, |
| 6705 | const struct function *funcp, |
| 6706 | char **argv) |
| 6707 | { |
| 6708 | # if BB_MMU |
| 6709 | int n = 1; |
| 6710 | |
| 6711 | argv[0] = G.global_argv[0]; |
| 6712 | G.global_argv = argv; |
| 6713 | while (*++argv) |
| 6714 | n++; |
| 6715 | G.global_argc = n; |
| 6716 | /* On MMU, funcp->body is always non-NULL */ |
| 6717 | n = run_list(funcp->body); |
| 6718 | fflush_all(); |
| 6719 | _exit(n); |
| 6720 | # else |
| 6721 | re_execute_shell(to_free, |
| 6722 | funcp->body_as_string, |
| 6723 | G.global_argv[0], |
| 6724 | argv + 1, |
| 6725 | NULL); |
| 6726 | # endif |
| 6727 | } |
| 6728 | |
| 6729 | static int run_function(const struct function *funcp, char **argv) |
| 6730 | { |
| 6731 | int rc; |
| 6732 | save_arg_t sv; |
| 6733 | smallint sv_flg; |
| 6734 | |
| 6735 | save_and_replace_G_args(&sv, argv); |
| 6736 | |
| 6737 | /* "we are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6738 | sv_flg = G_flag_return_in_progress; |
| 6739 | G_flag_return_in_progress = -1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6740 | # if ENABLE_HUSH_LOCAL |
| 6741 | G.func_nest_level++; |
| 6742 | # endif |
| 6743 | |
| 6744 | /* On MMU, funcp->body is always non-NULL */ |
| 6745 | # if !BB_MMU |
| 6746 | if (!funcp->body) { |
| 6747 | /* Function defined by -F */ |
| 6748 | parse_and_run_string(funcp->body_as_string); |
| 6749 | rc = G.last_exitcode; |
| 6750 | } else |
| 6751 | # endif |
| 6752 | { |
| 6753 | rc = run_list(funcp->body); |
| 6754 | } |
| 6755 | |
| 6756 | # if ENABLE_HUSH_LOCAL |
| 6757 | { |
| 6758 | struct variable *var; |
| 6759 | struct variable **var_pp; |
| 6760 | |
| 6761 | var_pp = &G.top_var; |
| 6762 | while ((var = *var_pp) != NULL) { |
| 6763 | if (var->func_nest_level < G.func_nest_level) { |
| 6764 | var_pp = &var->next; |
| 6765 | continue; |
| 6766 | } |
| 6767 | /* Unexport */ |
| 6768 | if (var->flg_export) |
| 6769 | bb_unsetenv(var->varstr); |
| 6770 | /* Remove from global list */ |
| 6771 | *var_pp = var->next; |
| 6772 | /* Free */ |
| 6773 | if (!var->max_len) |
| 6774 | free(var->varstr); |
| 6775 | free(var); |
| 6776 | } |
| 6777 | G.func_nest_level--; |
| 6778 | } |
| 6779 | # endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6780 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6781 | |
| 6782 | restore_G_args(&sv, argv); |
| 6783 | |
| 6784 | return rc; |
| 6785 | } |
| 6786 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6787 | |
| 6788 | |
| 6789 | #if BB_MMU |
| 6790 | #define exec_builtin(to_free, x, argv) \ |
| 6791 | exec_builtin(x, argv) |
| 6792 | #else |
| 6793 | #define exec_builtin(to_free, x, argv) \ |
| 6794 | exec_builtin(to_free, argv) |
| 6795 | #endif |
| 6796 | static void exec_builtin(char ***to_free, |
| 6797 | const struct built_in_command *x, |
| 6798 | char **argv) NORETURN; |
| 6799 | static void exec_builtin(char ***to_free, |
| 6800 | const struct built_in_command *x, |
| 6801 | char **argv) |
| 6802 | { |
| 6803 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6804 | int rcode; |
| 6805 | fflush_all(); |
| 6806 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6807 | fflush_all(); |
| 6808 | _exit(rcode); |
| 6809 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 6810 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6811 | /* On NOMMU, we must never block! |
| 6812 | * Example: { sleep 99 | read line; } & echo Ok |
| 6813 | */ |
| 6814 | re_execute_shell(to_free, |
| 6815 | argv[0], |
| 6816 | G.global_argv[0], |
| 6817 | G.global_argv + 1, |
| 6818 | argv); |
| 6819 | #endif |
| 6820 | } |
| 6821 | |
| 6822 | |
| 6823 | static void execvp_or_die(char **argv) NORETURN; |
| 6824 | static void execvp_or_die(char **argv) |
| 6825 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6826 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6827 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6828 | /* Don't propagate SIG_IGN to the child */ |
| 6829 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6830 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6831 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6832 | e = 2; |
| 6833 | if (errno == EACCES) e = 126; |
| 6834 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6835 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 6836 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | #if ENABLE_HUSH_MODE_X |
| 6840 | static void dump_cmd_in_x_mode(char **argv) |
| 6841 | { |
| 6842 | if (G_x_mode && argv) { |
| 6843 | /* We want to output the line in one write op */ |
| 6844 | char *buf, *p; |
| 6845 | int len; |
| 6846 | int n; |
| 6847 | |
| 6848 | len = 3; |
| 6849 | n = 0; |
| 6850 | while (argv[n]) |
| 6851 | len += strlen(argv[n++]) + 1; |
| 6852 | buf = xmalloc(len); |
| 6853 | buf[0] = '+'; |
| 6854 | p = buf + 1; |
| 6855 | n = 0; |
| 6856 | while (argv[n]) |
| 6857 | p += sprintf(p, " %s", argv[n++]); |
| 6858 | *p++ = '\n'; |
| 6859 | *p = '\0'; |
| 6860 | fputs(buf, stderr); |
| 6861 | free(buf); |
| 6862 | } |
| 6863 | } |
| 6864 | #else |
| 6865 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6866 | #endif |
| 6867 | |
| 6868 | #if BB_MMU |
| 6869 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6870 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6871 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6872 | pseudo_exec(command, argv_expanded) |
| 6873 | #endif |
| 6874 | |
| 6875 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6876 | * Never returns. |
| 6877 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6878 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6879 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 6880 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6881 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6882 | char **argv, int assignment_cnt, |
| 6883 | char **argv_expanded) NORETURN; |
| 6884 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6885 | char **argv, int assignment_cnt, |
| 6886 | char **argv_expanded) |
| 6887 | { |
| 6888 | char **new_env; |
| 6889 | |
| 6890 | new_env = expand_assignments(argv, assignment_cnt); |
| 6891 | dump_cmd_in_x_mode(new_env); |
| 6892 | |
| 6893 | if (!argv[assignment_cnt]) { |
| 6894 | /* Case when we are here: ... | var=val | ... |
| 6895 | * (note that we do not exit early, i.e., do not optimize out |
| 6896 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6897 | */ |
| 6898 | free_strings(new_env); |
| 6899 | _exit(EXIT_SUCCESS); |
| 6900 | } |
| 6901 | |
| 6902 | #if BB_MMU |
| 6903 | set_vars_and_save_old(new_env); |
| 6904 | free(new_env); /* optional */ |
| 6905 | /* we can also destroy set_vars_and_save_old's return value, |
| 6906 | * to save memory */ |
| 6907 | #else |
| 6908 | nommu_save->new_env = new_env; |
| 6909 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6910 | #endif |
| 6911 | |
| 6912 | if (argv_expanded) { |
| 6913 | argv = argv_expanded; |
| 6914 | } else { |
| 6915 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6916 | #if !BB_MMU |
| 6917 | nommu_save->argv = argv; |
| 6918 | #endif |
| 6919 | } |
| 6920 | dump_cmd_in_x_mode(argv); |
| 6921 | |
| 6922 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6923 | if (strchr(argv[0], '/') != NULL) |
| 6924 | goto skip; |
| 6925 | #endif |
| 6926 | |
| 6927 | /* Check if the command matches any of the builtins. |
| 6928 | * Depending on context, this might be redundant. But it's |
| 6929 | * easier to waste a few CPU cycles than it is to figure out |
| 6930 | * if this is one of those cases. |
| 6931 | */ |
| 6932 | { |
| 6933 | /* On NOMMU, it is more expensive to re-execute shell |
| 6934 | * just in order to run echo or test builtin. |
| 6935 | * It's better to skip it here and run corresponding |
| 6936 | * non-builtin later. */ |
| 6937 | const struct built_in_command *x; |
| 6938 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6939 | if (x) { |
| 6940 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6941 | } |
| 6942 | } |
| 6943 | #if ENABLE_HUSH_FUNCTIONS |
| 6944 | /* Check if the command matches any functions */ |
| 6945 | { |
| 6946 | const struct function *funcp = find_function(argv[0]); |
| 6947 | if (funcp) { |
| 6948 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6949 | } |
| 6950 | } |
| 6951 | #endif |
| 6952 | |
| 6953 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6954 | /* Check if the command matches any busybox applets */ |
| 6955 | { |
| 6956 | int a = find_applet_by_name(argv[0]); |
| 6957 | if (a >= 0) { |
| 6958 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6959 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6960 | /* Do not leak open fds from opened script files etc */ |
| 6961 | close_all_FILE_list(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6962 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6963 | run_applet_no_and_exit(a, argv); |
| 6964 | } |
| 6965 | # endif |
| 6966 | /* Re-exec ourselves */ |
| 6967 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 6968 | /* Don't propagate SIG_IGN to the child */ |
| 6969 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6970 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6971 | execv(bb_busybox_exec_path, argv); |
| 6972 | /* If they called chroot or otherwise made the binary no longer |
| 6973 | * executable, fall through */ |
| 6974 | } |
| 6975 | } |
| 6976 | #endif |
| 6977 | |
| 6978 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6979 | skip: |
| 6980 | #endif |
| 6981 | execvp_or_die(argv); |
| 6982 | } |
| 6983 | |
| 6984 | /* Called after [v]fork() in run_pipe |
| 6985 | */ |
| 6986 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6987 | struct command *command, |
| 6988 | char **argv_expanded) NORETURN; |
| 6989 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6990 | struct command *command, |
| 6991 | char **argv_expanded) |
| 6992 | { |
| 6993 | if (command->argv) { |
| 6994 | pseudo_exec_argv(nommu_save, command->argv, |
| 6995 | command->assignment_cnt, argv_expanded); |
| 6996 | } |
| 6997 | |
| 6998 | if (command->group) { |
| 6999 | /* Cases when we are here: |
| 7000 | * ( list ) |
| 7001 | * { list } & |
| 7002 | * ... | ( list ) | ... |
| 7003 | * ... | { list } | ... |
| 7004 | */ |
| 7005 | #if BB_MMU |
| 7006 | int rcode; |
| 7007 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7008 | reset_traps_to_defaults(); |
| 7009 | rcode = run_list(command->group); |
| 7010 | /* OK to leak memory by not calling free_pipe_list, |
| 7011 | * since this process is about to exit */ |
| 7012 | _exit(rcode); |
| 7013 | #else |
| 7014 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7015 | command->group_as_string, |
| 7016 | G.global_argv[0], |
| 7017 | G.global_argv + 1, |
| 7018 | NULL); |
| 7019 | #endif |
| 7020 | } |
| 7021 | |
| 7022 | /* Case when we are here: ... | >file */ |
| 7023 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7024 | _exit(EXIT_SUCCESS); |
| 7025 | } |
| 7026 | |
| 7027 | #if ENABLE_HUSH_JOB |
| 7028 | static const char *get_cmdtext(struct pipe *pi) |
| 7029 | { |
| 7030 | char **argv; |
| 7031 | char *p; |
| 7032 | int len; |
| 7033 | |
| 7034 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7035 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7036 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7037 | if (pi->cmdtext) |
| 7038 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7039 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7040 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7041 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7042 | pi->cmdtext = xzalloc(1); |
| 7043 | return pi->cmdtext; |
| 7044 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7045 | len = 0; |
| 7046 | do { |
| 7047 | len += strlen(*argv) + 1; |
| 7048 | } while (*++argv); |
| 7049 | p = xmalloc(len); |
| 7050 | pi->cmdtext = p; |
| 7051 | argv = pi->cmds[0].argv; |
| 7052 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7053 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7054 | *p++ = ' '; |
| 7055 | } while (*++argv); |
| 7056 | p[-1] = '\0'; |
| 7057 | return pi->cmdtext; |
| 7058 | } |
| 7059 | |
| 7060 | static void insert_bg_job(struct pipe *pi) |
| 7061 | { |
| 7062 | struct pipe *job, **jobp; |
| 7063 | int i; |
| 7064 | |
| 7065 | /* Linear search for the ID of the job to use */ |
| 7066 | pi->jobid = 1; |
| 7067 | for (job = G.job_list; job; job = job->next) |
| 7068 | if (job->jobid >= pi->jobid) |
| 7069 | pi->jobid = job->jobid + 1; |
| 7070 | |
| 7071 | /* Add job to the list of running jobs */ |
| 7072 | jobp = &G.job_list; |
| 7073 | while ((job = *jobp) != NULL) |
| 7074 | jobp = &job->next; |
| 7075 | job = *jobp = xmalloc(sizeof(*job)); |
| 7076 | |
| 7077 | *job = *pi; /* physical copy */ |
| 7078 | job->next = NULL; |
| 7079 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7080 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7081 | for (i = 0; i < pi->num_cmds; i++) { |
| 7082 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7083 | /* all other fields are not used and stay zero */ |
| 7084 | } |
| 7085 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7086 | |
| 7087 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7088 | printf("[%u] %u %s\n", job->jobid, (unsigned)job->cmds[0].pid, job->cmdtext); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7089 | G.last_jobid = job->jobid; |
| 7090 | } |
| 7091 | |
| 7092 | static void remove_bg_job(struct pipe *pi) |
| 7093 | { |
| 7094 | struct pipe *prev_pipe; |
| 7095 | |
| 7096 | if (pi == G.job_list) { |
| 7097 | G.job_list = pi->next; |
| 7098 | } else { |
| 7099 | prev_pipe = G.job_list; |
| 7100 | while (prev_pipe->next != pi) |
| 7101 | prev_pipe = prev_pipe->next; |
| 7102 | prev_pipe->next = pi->next; |
| 7103 | } |
| 7104 | if (G.job_list) |
| 7105 | G.last_jobid = G.job_list->jobid; |
| 7106 | else |
| 7107 | G.last_jobid = 0; |
| 7108 | } |
| 7109 | |
| 7110 | /* Remove a backgrounded job */ |
| 7111 | static void delete_finished_bg_job(struct pipe *pi) |
| 7112 | { |
| 7113 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7114 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7115 | } |
| 7116 | #endif /* JOB */ |
| 7117 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7118 | static int job_exited_or_stopped(struct pipe *pi) |
| 7119 | { |
| 7120 | int rcode, i; |
| 7121 | |
| 7122 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7123 | return -1; |
| 7124 | |
| 7125 | /* All processes in fg pipe have exited or stopped */ |
| 7126 | rcode = 0; |
| 7127 | i = pi->num_cmds; |
| 7128 | while (--i >= 0) { |
| 7129 | rcode = pi->cmds[i].cmd_exitcode; |
| 7130 | /* usually last process gives overall exitstatus, |
| 7131 | * but with "set -o pipefail", last *failed* process does */ |
| 7132 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7133 | break; |
| 7134 | } |
| 7135 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7136 | return rcode; |
| 7137 | } |
| 7138 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7139 | 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] | 7140 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7141 | #if ENABLE_HUSH_JOB |
| 7142 | struct pipe *pi; |
| 7143 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7144 | int i, dead; |
| 7145 | |
| 7146 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7147 | |
| 7148 | #if DEBUG_JOBS |
| 7149 | if (WIFSTOPPED(status)) |
| 7150 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7151 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7152 | if (WIFSIGNALED(status)) |
| 7153 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7154 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7155 | if (WIFEXITED(status)) |
| 7156 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7157 | childpid, WEXITSTATUS(status)); |
| 7158 | #endif |
| 7159 | /* Were we asked to wait for a fg pipe? */ |
| 7160 | if (fg_pipe) { |
| 7161 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7162 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7163 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7164 | int rcode; |
| 7165 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7166 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7167 | if (fg_pipe->cmds[i].pid != childpid) |
| 7168 | continue; |
| 7169 | if (dead) { |
| 7170 | int ex; |
| 7171 | fg_pipe->cmds[i].pid = 0; |
| 7172 | fg_pipe->alive_cmds--; |
| 7173 | ex = WEXITSTATUS(status); |
| 7174 | /* bash prints killer signal's name for *last* |
| 7175 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 7176 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7177 | */ |
| 7178 | if (WIFSIGNALED(status)) { |
| 7179 | int sig = WTERMSIG(status); |
| 7180 | if (i == fg_pipe->num_cmds-1) |
| 7181 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 7182 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 7183 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 7184 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7185 | * Maybe we need to use sig | 128? */ |
| 7186 | ex = sig + 128; |
| 7187 | } |
| 7188 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 7189 | } else { |
| 7190 | fg_pipe->stopped_cmds++; |
| 7191 | } |
| 7192 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 7193 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7194 | rcode = job_exited_or_stopped(fg_pipe); |
| 7195 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7196 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 7197 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 7198 | * and "killall -STOP cat" */ |
| 7199 | if (G_interactive_fd) { |
| 7200 | #if ENABLE_HUSH_JOB |
| 7201 | if (fg_pipe->alive_cmds != 0) |
| 7202 | insert_bg_job(fg_pipe); |
| 7203 | #endif |
| 7204 | return rcode; |
| 7205 | } |
| 7206 | if (fg_pipe->alive_cmds == 0) |
| 7207 | return rcode; |
| 7208 | } |
| 7209 | /* There are still running processes in the fg_pipe */ |
| 7210 | return -1; |
| 7211 | } |
| 7212 | /* It wasnt in fg_pipe, look for process in bg pipes */ |
| 7213 | } |
| 7214 | |
| 7215 | #if ENABLE_HUSH_JOB |
| 7216 | /* We were asked to wait for bg or orphaned children */ |
| 7217 | /* No need to remember exitcode in this case */ |
| 7218 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7219 | for (i = 0; i < pi->num_cmds; i++) { |
| 7220 | if (pi->cmds[i].pid == childpid) |
| 7221 | goto found_pi_and_prognum; |
| 7222 | } |
| 7223 | } |
| 7224 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 7225 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 7226 | return -1; /* this wasn't a process from fg_pipe */ |
| 7227 | |
| 7228 | found_pi_and_prognum: |
| 7229 | if (dead) { |
| 7230 | /* child exited */ |
| 7231 | pi->cmds[i].pid = 0; |
| 7232 | pi->cmds[i].cmd_exitcode = WEXITSTATUS(status); |
| 7233 | if (WIFSIGNALED(status)) |
| 7234 | pi->cmds[i].cmd_exitcode = 128 + WTERMSIG(status); |
| 7235 | pi->alive_cmds--; |
| 7236 | if (!pi->alive_cmds) { |
| 7237 | if (G_interactive_fd) |
| 7238 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 7239 | "Done", pi->cmdtext); |
| 7240 | delete_finished_bg_job(pi); |
| 7241 | } |
| 7242 | } else { |
| 7243 | /* child stopped */ |
| 7244 | pi->stopped_cmds++; |
| 7245 | } |
| 7246 | #endif |
| 7247 | return -1; /* this wasn't a process from fg_pipe */ |
| 7248 | } |
| 7249 | |
| 7250 | /* Check to see if any processes have exited -- if they have, |
| 7251 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7252 | * |
| 7253 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 7254 | * Return its exitcode or zero if stopped. |
| 7255 | * |
| 7256 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 7257 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 7258 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7259 | * or 0 if no children changed status. |
| 7260 | * |
| 7261 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 7262 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7263 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7264 | */ |
| 7265 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 7266 | { |
| 7267 | int attributes; |
| 7268 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7269 | int rcode = 0; |
| 7270 | |
| 7271 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 7272 | |
| 7273 | attributes = WUNTRACED; |
| 7274 | if (fg_pipe == NULL) |
| 7275 | attributes |= WNOHANG; |
| 7276 | |
| 7277 | errno = 0; |
| 7278 | #if ENABLE_HUSH_FAST |
| 7279 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 7280 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 7281 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 7282 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 7283 | /* Avoid doing waitpid syscall if possible */ |
| 7284 | if (!G.we_have_children) { |
| 7285 | errno = ECHILD; |
| 7286 | return -1; |
| 7287 | } |
| 7288 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 7289 | /* We have children, but they did not exit |
| 7290 | * or stop yet (we saw no SIGCHLD) */ |
| 7291 | return 0; |
| 7292 | } |
| 7293 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 7294 | } |
| 7295 | #endif |
| 7296 | |
| 7297 | /* Do we do this right? |
| 7298 | * bash-3.00# sleep 20 | false |
| 7299 | * <ctrl-Z pressed> |
| 7300 | * [3]+ Stopped sleep 20 | false |
| 7301 | * bash-3.00# echo $? |
| 7302 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 7303 | * [hush 1.14.0: yes we do it right] |
| 7304 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7305 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7306 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7307 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7308 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7309 | i = G.count_SIGCHLD; |
| 7310 | #endif |
| 7311 | childpid = waitpid(-1, &status, attributes); |
| 7312 | if (childpid <= 0) { |
| 7313 | if (childpid && errno != ECHILD) |
| 7314 | bb_perror_msg("waitpid"); |
| 7315 | #if ENABLE_HUSH_FAST |
| 7316 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 7317 | G.we_have_children = (childpid == 0); |
| 7318 | G.handled_SIGCHLD = i; |
| 7319 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7320 | } |
| 7321 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7322 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 7323 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7324 | break; |
| 7325 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7326 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 7327 | if (rcode >= 0) { |
| 7328 | /* fg_pipe exited or stopped */ |
| 7329 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7330 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7331 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7332 | 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] | 7333 | rcode = WEXITSTATUS(status); |
| 7334 | if (WIFSIGNALED(status)) |
| 7335 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7336 | if (WIFSTOPPED(status)) |
| 7337 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 7338 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7339 | rcode++; |
| 7340 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7341 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7342 | /* This wasn't one of our processes, or */ |
| 7343 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7344 | } /* while (waitpid succeeds)... */ |
| 7345 | |
| 7346 | return rcode; |
| 7347 | } |
| 7348 | |
| 7349 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7350 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7351 | { |
| 7352 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7353 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7354 | if (G_saved_tty_pgrp) { |
| 7355 | /* Job finished, move the shell to the foreground */ |
| 7356 | p = getpgrp(); /* our process group id */ |
| 7357 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 7358 | tcsetpgrp(G_interactive_fd, p); |
| 7359 | } |
| 7360 | return rcode; |
| 7361 | } |
| 7362 | #endif |
| 7363 | |
| 7364 | /* Start all the jobs, but don't wait for anything to finish. |
| 7365 | * See checkjobs(). |
| 7366 | * |
| 7367 | * Return code is normally -1, when the caller has to wait for children |
| 7368 | * to finish to determine the exit status of the pipe. If the pipe |
| 7369 | * is a simple builtin command, however, the action is done by the |
| 7370 | * time run_pipe returns, and the exit code is provided as the |
| 7371 | * return value. |
| 7372 | * |
| 7373 | * Returns -1 only if started some children. IOW: we have to |
| 7374 | * mask out retvals of builtins etc with 0xff! |
| 7375 | * |
| 7376 | * The only case when we do not need to [v]fork is when the pipe |
| 7377 | * is single, non-backgrounded, non-subshell command. Examples: |
| 7378 | * cmd ; ... { list } ; ... |
| 7379 | * cmd && ... { list } && ... |
| 7380 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7381 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7382 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 7383 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 7384 | * |
| 7385 | * Cases when we must fork: |
| 7386 | * non-single: cmd | cmd |
| 7387 | * backgrounded: cmd & { list } & |
| 7388 | * subshell: ( list ) [&] |
| 7389 | */ |
| 7390 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 7391 | #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] | 7392 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 7393 | #endif |
| 7394 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 7395 | struct variable **old_vars_p, |
| 7396 | struct command *command, |
| 7397 | int squirrel[3], |
| 7398 | char **argv_expanded) |
| 7399 | { |
| 7400 | /* setup_redirects acts on file descriptors, not FILEs. |
| 7401 | * This is perfect for work that comes after exec(). |
| 7402 | * Is it really safe for inline use? Experimentally, |
| 7403 | * things seem to work. */ |
| 7404 | int rcode = setup_redirects(command, squirrel); |
| 7405 | if (rcode == 0) { |
| 7406 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 7407 | *new_env_p = new_env; |
| 7408 | dump_cmd_in_x_mode(new_env); |
| 7409 | dump_cmd_in_x_mode(argv_expanded); |
| 7410 | if (old_vars_p) |
| 7411 | *old_vars_p = set_vars_and_save_old(new_env); |
| 7412 | } |
| 7413 | return rcode; |
| 7414 | } |
| 7415 | static NOINLINE int run_pipe(struct pipe *pi) |
| 7416 | { |
| 7417 | static const char *const null_ptr = NULL; |
| 7418 | |
| 7419 | int cmd_no; |
| 7420 | int next_infd; |
| 7421 | struct command *command; |
| 7422 | char **argv_expanded; |
| 7423 | char **argv; |
| 7424 | /* it is not always needed, but we aim to smaller code */ |
| 7425 | int squirrel[] = { -1, -1, -1 }; |
| 7426 | int rcode; |
| 7427 | |
| 7428 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 7429 | debug_enter(); |
| 7430 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7431 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 7432 | * Result should be 3 lines: q w e, qwe, q w e |
| 7433 | */ |
| 7434 | G.ifs = get_local_var_value("IFS"); |
| 7435 | if (!G.ifs) |
| 7436 | G.ifs = defifs; |
| 7437 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7438 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 7439 | pi->stopped_cmds = 0; |
| 7440 | command = &pi->cmds[0]; |
| 7441 | argv_expanded = NULL; |
| 7442 | |
| 7443 | if (pi->num_cmds != 1 |
| 7444 | || pi->followup == PIPE_BG |
| 7445 | || command->cmd_type == CMD_SUBSHELL |
| 7446 | ) { |
| 7447 | goto must_fork; |
| 7448 | } |
| 7449 | |
| 7450 | pi->alive_cmds = 1; |
| 7451 | |
| 7452 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 7453 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 7454 | |
| 7455 | if (command->group) { |
| 7456 | #if ENABLE_HUSH_FUNCTIONS |
| 7457 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7458 | /* "executing" func () { list } */ |
| 7459 | struct function *funcp; |
| 7460 | |
| 7461 | funcp = new_function(command->argv[0]); |
| 7462 | /* funcp->name is already set to argv[0] */ |
| 7463 | funcp->body = command->group; |
| 7464 | # if !BB_MMU |
| 7465 | funcp->body_as_string = command->group_as_string; |
| 7466 | command->group_as_string = NULL; |
| 7467 | # endif |
| 7468 | command->group = NULL; |
| 7469 | command->argv[0] = NULL; |
| 7470 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 7471 | funcp->parent_cmd = command; |
| 7472 | command->child_func = funcp; |
| 7473 | |
| 7474 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 7475 | debug_leave(); |
| 7476 | return EXIT_SUCCESS; |
| 7477 | } |
| 7478 | #endif |
| 7479 | /* { list } */ |
| 7480 | debug_printf("non-subshell group\n"); |
| 7481 | rcode = 1; /* exitcode if redir failed */ |
| 7482 | if (setup_redirects(command, squirrel) == 0) { |
| 7483 | debug_printf_exec(": run_list\n"); |
| 7484 | rcode = run_list(command->group) & 0xff; |
| 7485 | } |
| 7486 | restore_redirects(squirrel); |
| 7487 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7488 | debug_leave(); |
| 7489 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7490 | return rcode; |
| 7491 | } |
| 7492 | |
| 7493 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 7494 | { |
| 7495 | const struct built_in_command *x; |
| 7496 | #if ENABLE_HUSH_FUNCTIONS |
| 7497 | const struct function *funcp; |
| 7498 | #else |
| 7499 | enum { funcp = 0 }; |
| 7500 | #endif |
| 7501 | char **new_env = NULL; |
| 7502 | struct variable *old_vars = NULL; |
| 7503 | |
| 7504 | if (argv[command->assignment_cnt] == NULL) { |
| 7505 | /* Assignments, but no command */ |
| 7506 | /* Ensure redirects take effect (that is, create files). |
| 7507 | * Try "a=t >file" */ |
| 7508 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 7509 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 7510 | /* Set shell variables */ |
| 7511 | if (new_env) { |
| 7512 | argv = new_env; |
| 7513 | while (*argv) { |
| 7514 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7515 | /* Do we need to flag set_local_var() errors? |
| 7516 | * "assignment to readonly var" and "putenv error" |
| 7517 | */ |
| 7518 | argv++; |
| 7519 | } |
| 7520 | } |
| 7521 | /* Redirect error sets $? to 1. Otherwise, |
| 7522 | * if evaluating assignment value set $?, retain it. |
| 7523 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7524 | if (rcode == 0) |
| 7525 | rcode = G.last_exitcode; |
| 7526 | /* Exit, _skipping_ variable restoring code: */ |
| 7527 | goto clean_up_and_ret0; |
| 7528 | |
| 7529 | #else /* Older, bigger, but more correct code */ |
| 7530 | |
| 7531 | rcode = setup_redirects(command, squirrel); |
| 7532 | restore_redirects(squirrel); |
| 7533 | /* Set shell variables */ |
| 7534 | if (G_x_mode) |
| 7535 | bb_putchar_stderr('+'); |
| 7536 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7537 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7538 | if (G_x_mode) |
| 7539 | fprintf(stderr, " %s", p); |
| 7540 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 7541 | *argv, p); |
| 7542 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7543 | /* Do we need to flag set_local_var() errors? |
| 7544 | * "assignment to readonly var" and "putenv error" |
| 7545 | */ |
| 7546 | argv++; |
| 7547 | } |
| 7548 | if (G_x_mode) |
| 7549 | bb_putchar_stderr('\n'); |
| 7550 | /* Redirect error sets $? to 1. Otherwise, |
| 7551 | * if evaluating assignment value set $?, retain it. |
| 7552 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7553 | if (rcode == 0) |
| 7554 | rcode = G.last_exitcode; |
| 7555 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7556 | debug_leave(); |
| 7557 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7558 | return rcode; |
| 7559 | #endif |
| 7560 | } |
| 7561 | |
| 7562 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7563 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7564 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7565 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7566 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7567 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7568 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7569 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 7570 | } |
| 7571 | |
| 7572 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 7573 | if (!argv_expanded[0]) { |
| 7574 | free(argv_expanded); |
| 7575 | debug_leave(); |
| 7576 | return G.last_exitcode; |
| 7577 | } |
| 7578 | |
| 7579 | x = find_builtin(argv_expanded[0]); |
| 7580 | #if ENABLE_HUSH_FUNCTIONS |
| 7581 | funcp = NULL; |
| 7582 | if (!x) |
| 7583 | funcp = find_function(argv_expanded[0]); |
| 7584 | #endif |
| 7585 | if (x || funcp) { |
| 7586 | if (!funcp) { |
| 7587 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 7588 | debug_printf("exec with redirects only\n"); |
| 7589 | rcode = setup_redirects(command, NULL); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7590 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7591 | goto clean_up_and_ret1; |
| 7592 | } |
| 7593 | } |
| 7594 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7595 | if (rcode == 0) { |
| 7596 | if (!funcp) { |
| 7597 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 7598 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7599 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7600 | rcode = x->b_function(argv_expanded) & 0xff; |
| 7601 | fflush_all(); |
| 7602 | } |
| 7603 | #if ENABLE_HUSH_FUNCTIONS |
| 7604 | else { |
| 7605 | # if ENABLE_HUSH_LOCAL |
| 7606 | struct variable **sv; |
| 7607 | sv = G.shadowed_vars_pp; |
| 7608 | G.shadowed_vars_pp = &old_vars; |
| 7609 | # endif |
| 7610 | debug_printf_exec(": function '%s' '%s'...\n", |
| 7611 | funcp->name, argv_expanded[1]); |
| 7612 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 7613 | # if ENABLE_HUSH_LOCAL |
| 7614 | G.shadowed_vars_pp = sv; |
| 7615 | # endif |
| 7616 | } |
| 7617 | #endif |
| 7618 | } |
| 7619 | clean_up_and_ret: |
| 7620 | unset_vars(new_env); |
| 7621 | add_vars(old_vars); |
| 7622 | /* clean_up_and_ret0: */ |
| 7623 | restore_redirects(squirrel); |
| 7624 | clean_up_and_ret1: |
| 7625 | free(argv_expanded); |
| 7626 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7627 | debug_leave(); |
| 7628 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 7629 | return rcode; |
| 7630 | } |
| 7631 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7632 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7633 | int n = find_applet_by_name(argv_expanded[0]); |
| 7634 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 7635 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 7636 | if (rcode == 0) { |
| 7637 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 7638 | argv_expanded[0], argv_expanded[1]); |
| 7639 | rcode = run_nofork_applet(n, argv_expanded); |
| 7640 | } |
| 7641 | goto clean_up_and_ret; |
| 7642 | } |
| 7643 | } |
| 7644 | /* It is neither builtin nor applet. We must fork. */ |
| 7645 | } |
| 7646 | |
| 7647 | must_fork: |
| 7648 | /* NB: argv_expanded may already be created, and that |
| 7649 | * might include `cmd` runs! Do not rerun it! We *must* |
| 7650 | * use argv_expanded if it's non-NULL */ |
| 7651 | |
| 7652 | /* Going to fork a child per each pipe member */ |
| 7653 | pi->alive_cmds = 0; |
| 7654 | next_infd = 0; |
| 7655 | |
| 7656 | cmd_no = 0; |
| 7657 | while (cmd_no < pi->num_cmds) { |
| 7658 | struct fd_pair pipefds; |
| 7659 | #if !BB_MMU |
| 7660 | volatile nommu_save_t nommu_save; |
| 7661 | nommu_save.new_env = NULL; |
| 7662 | nommu_save.old_vars = NULL; |
| 7663 | nommu_save.argv = NULL; |
| 7664 | nommu_save.argv_from_re_execing = NULL; |
| 7665 | #endif |
| 7666 | command = &pi->cmds[cmd_no]; |
| 7667 | cmd_no++; |
| 7668 | if (command->argv) { |
| 7669 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 7670 | command->argv[0], command->argv[1]); |
| 7671 | } else { |
| 7672 | debug_printf_exec(": pipe member with no argv\n"); |
| 7673 | } |
| 7674 | |
| 7675 | /* pipes are inserted between pairs of commands */ |
| 7676 | pipefds.rd = 0; |
| 7677 | pipefds.wr = 1; |
| 7678 | if (cmd_no < pi->num_cmds) |
| 7679 | xpiped_pair(pipefds); |
| 7680 | |
| 7681 | command->pid = BB_MMU ? fork() : vfork(); |
| 7682 | if (!command->pid) { /* child */ |
| 7683 | #if ENABLE_HUSH_JOB |
| 7684 | disable_restore_tty_pgrp_on_exit(); |
| 7685 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7686 | |
| 7687 | /* Every child adds itself to new process group |
| 7688 | * with pgid == pid_of_first_child_in_pipe */ |
| 7689 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7690 | pid_t pgrp; |
| 7691 | pgrp = pi->pgrp; |
| 7692 | if (pgrp < 0) /* true for 1st process only */ |
| 7693 | pgrp = getpid(); |
| 7694 | if (setpgid(0, pgrp) == 0 |
| 7695 | && pi->followup != PIPE_BG |
| 7696 | && G_saved_tty_pgrp /* we have ctty */ |
| 7697 | ) { |
| 7698 | /* We do it in *every* child, not just first, |
| 7699 | * to avoid races */ |
| 7700 | tcsetpgrp(G_interactive_fd, pgrp); |
| 7701 | } |
| 7702 | } |
| 7703 | #endif |
| 7704 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 7705 | /* 1st cmd in backgrounded pipe |
| 7706 | * should have its stdin /dev/null'ed */ |
| 7707 | close(0); |
| 7708 | if (open(bb_dev_null, O_RDONLY)) |
| 7709 | xopen("/", O_RDONLY); |
| 7710 | } else { |
| 7711 | xmove_fd(next_infd, 0); |
| 7712 | } |
| 7713 | xmove_fd(pipefds.wr, 1); |
| 7714 | if (pipefds.rd > 1) |
| 7715 | close(pipefds.rd); |
| 7716 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7717 | * and the pipe fd (fd#1) is available for dup'ing: |
| 7718 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 7719 | * of cmd1 goes into pipe. |
| 7720 | */ |
| 7721 | if (setup_redirects(command, NULL)) { |
| 7722 | /* Happens when redir file can't be opened: |
| 7723 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 7724 | * FOO |
| 7725 | * hush: can't open '/qwe/rty': No such file or directory |
| 7726 | * BAZ |
| 7727 | * (echo BAR is not executed, it hits _exit(1) below) |
| 7728 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7729 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7730 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7731 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7732 | /* Stores to nommu_save list of env vars putenv'ed |
| 7733 | * (NOMMU, on MMU we don't need that) */ |
| 7734 | /* cast away volatility... */ |
| 7735 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 7736 | /* pseudo_exec() does not return */ |
| 7737 | } |
| 7738 | |
| 7739 | /* parent or error */ |
| 7740 | #if ENABLE_HUSH_FAST |
| 7741 | G.count_SIGCHLD++; |
| 7742 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7743 | #endif |
| 7744 | enable_restore_tty_pgrp_on_exit(); |
| 7745 | #if !BB_MMU |
| 7746 | /* Clean up after vforked child */ |
| 7747 | free(nommu_save.argv); |
| 7748 | free(nommu_save.argv_from_re_execing); |
| 7749 | unset_vars(nommu_save.new_env); |
| 7750 | add_vars(nommu_save.old_vars); |
| 7751 | #endif |
| 7752 | free(argv_expanded); |
| 7753 | argv_expanded = NULL; |
| 7754 | if (command->pid < 0) { /* [v]fork failed */ |
| 7755 | /* Clearly indicate, was it fork or vfork */ |
| 7756 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 7757 | } else { |
| 7758 | pi->alive_cmds++; |
| 7759 | #if ENABLE_HUSH_JOB |
| 7760 | /* Second and next children need to know pid of first one */ |
| 7761 | if (pi->pgrp < 0) |
| 7762 | pi->pgrp = command->pid; |
| 7763 | #endif |
| 7764 | } |
| 7765 | |
| 7766 | if (cmd_no > 1) |
| 7767 | close(next_infd); |
| 7768 | if (cmd_no < pi->num_cmds) |
| 7769 | close(pipefds.wr); |
| 7770 | /* Pass read (output) pipe end to next iteration */ |
| 7771 | next_infd = pipefds.rd; |
| 7772 | } |
| 7773 | |
| 7774 | if (!pi->alive_cmds) { |
| 7775 | debug_leave(); |
| 7776 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 7777 | return 1; |
| 7778 | } |
| 7779 | |
| 7780 | debug_leave(); |
| 7781 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 7782 | return -1; |
| 7783 | } |
| 7784 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7785 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 7786 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 7787 | static int run_list(struct pipe *pi) |
| 7788 | { |
| 7789 | #if ENABLE_HUSH_CASE |
| 7790 | char *case_word = NULL; |
| 7791 | #endif |
| 7792 | #if ENABLE_HUSH_LOOPS |
| 7793 | struct pipe *loop_top = NULL; |
| 7794 | char **for_lcur = NULL; |
| 7795 | char **for_list = NULL; |
| 7796 | #endif |
| 7797 | smallint last_followup; |
| 7798 | smalluint rcode; |
| 7799 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7800 | smalluint cond_code = 0; |
| 7801 | #else |
| 7802 | enum { cond_code = 0 }; |
| 7803 | #endif |
| 7804 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7805 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7806 | smallint last_rword; /* ditto */ |
| 7807 | #endif |
| 7808 | |
| 7809 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7810 | debug_enter(); |
| 7811 | |
| 7812 | #if ENABLE_HUSH_LOOPS |
| 7813 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 7814 | { |
| 7815 | struct pipe *cpipe; |
| 7816 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7817 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7818 | continue; |
| 7819 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7820 | if (cpipe->next == NULL) { |
| 7821 | syntax_error("malformed for"); |
| 7822 | debug_leave(); |
| 7823 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7824 | return 1; |
| 7825 | } |
| 7826 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7827 | if (cpipe->next->res_word == RES_DO) |
| 7828 | continue; |
| 7829 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7830 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7831 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7832 | ) { |
| 7833 | syntax_error("malformed for"); |
| 7834 | debug_leave(); |
| 7835 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7836 | return 1; |
| 7837 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7838 | } |
| 7839 | } |
| 7840 | #endif |
| 7841 | |
| 7842 | /* Past this point, all code paths should jump to ret: label |
| 7843 | * in order to return, no direct "return" statements please. |
| 7844 | * This helps to ensure that no memory is leaked. */ |
| 7845 | |
| 7846 | #if ENABLE_HUSH_JOB |
| 7847 | G.run_list_level++; |
| 7848 | #endif |
| 7849 | |
| 7850 | #if HAS_KEYWORDS |
| 7851 | rword = RES_NONE; |
| 7852 | last_rword = RES_XXXX; |
| 7853 | #endif |
| 7854 | last_followup = PIPE_SEQ; |
| 7855 | rcode = G.last_exitcode; |
| 7856 | |
| 7857 | /* Go through list of pipes, (maybe) executing them. */ |
| 7858 | 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] | 7859 | int r; |
| 7860 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7861 | if (G.flag_SIGINT) |
| 7862 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7863 | if (G_flag_return_in_progress == 1) |
| 7864 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7865 | |
| 7866 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7867 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7868 | rword, cond_code, last_rword); |
| 7869 | #if ENABLE_HUSH_LOOPS |
| 7870 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7871 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7872 | ) { |
| 7873 | /* start of a loop: remember where loop starts */ |
| 7874 | loop_top = pi; |
| 7875 | G.depth_of_loop++; |
| 7876 | } |
| 7877 | #endif |
| 7878 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7879 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7880 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7881 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7882 | ) { |
| 7883 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7884 | * and we should not execute CMD */ |
| 7885 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7886 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 7887 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7888 | } |
| 7889 | } |
| 7890 | last_followup = pi->followup; |
| 7891 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7892 | #if ENABLE_HUSH_IF |
| 7893 | if (cond_code) { |
| 7894 | if (rword == RES_THEN) { |
| 7895 | /* if false; then ... fi has exitcode 0! */ |
| 7896 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7897 | /* "if <false> THEN cmd": skip cmd */ |
| 7898 | continue; |
| 7899 | } |
| 7900 | } else { |
| 7901 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7902 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7903 | * skip cmd and all following ones */ |
| 7904 | break; |
| 7905 | } |
| 7906 | } |
| 7907 | #endif |
| 7908 | #if ENABLE_HUSH_LOOPS |
| 7909 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7910 | if (!for_lcur) { |
| 7911 | /* first loop through for */ |
| 7912 | |
| 7913 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7914 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7915 | }; /* encoded representation of "$@" */ |
| 7916 | static const char *const encoded_dollar_at_argv[] = { |
| 7917 | encoded_dollar_at, NULL |
| 7918 | }; /* argv list with one element: "$@" */ |
| 7919 | char **vals; |
| 7920 | |
| 7921 | vals = (char**)encoded_dollar_at_argv; |
| 7922 | if (pi->next->res_word == RES_IN) { |
| 7923 | /* if no variable values after "in" we skip "for" */ |
| 7924 | if (!pi->next->cmds[0].argv) { |
| 7925 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7926 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7927 | break; |
| 7928 | } |
| 7929 | vals = pi->next->cmds[0].argv; |
| 7930 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7931 | /* create list of variable values */ |
| 7932 | debug_print_strings("for_list made from", vals); |
| 7933 | for_list = expand_strvec_to_strvec(vals); |
| 7934 | for_lcur = for_list; |
| 7935 | debug_print_strings("for_list", for_list); |
| 7936 | } |
| 7937 | if (!*for_lcur) { |
| 7938 | /* "for" loop is over, clean up */ |
| 7939 | free(for_list); |
| 7940 | for_list = NULL; |
| 7941 | for_lcur = NULL; |
| 7942 | break; |
| 7943 | } |
| 7944 | /* Insert next value from for_lcur */ |
| 7945 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7946 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7947 | continue; |
| 7948 | } |
| 7949 | if (rword == RES_IN) { |
| 7950 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7951 | } |
| 7952 | if (rword == RES_DONE) { |
| 7953 | continue; /* "done" has no cmds too */ |
| 7954 | } |
| 7955 | #endif |
| 7956 | #if ENABLE_HUSH_CASE |
| 7957 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7958 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7959 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7960 | continue; |
| 7961 | } |
| 7962 | if (rword == RES_MATCH) { |
| 7963 | char **argv; |
| 7964 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7965 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7966 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7967 | break; |
| 7968 | /* all prev words didn't match, does this one match? */ |
| 7969 | argv = pi->cmds->argv; |
| 7970 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7971 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7972 | /* TODO: which FNM_xxx flags to use? */ |
| 7973 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7974 | free(pattern); |
| 7975 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7976 | free(case_word); |
| 7977 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7978 | break; |
| 7979 | } |
| 7980 | argv++; |
| 7981 | } |
| 7982 | continue; |
| 7983 | } |
| 7984 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7985 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7986 | if (cond_code != 0) |
| 7987 | continue; /* not matched yet, skip this pipe */ |
| 7988 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 7989 | if (rword == RES_ESAC) { |
| 7990 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 7991 | if (case_word) { |
| 7992 | /* "case" did not match anything: still set $? (to 0) */ |
| 7993 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7994 | } |
| 7995 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7996 | #endif |
| 7997 | /* Just pressing <enter> in shell should check for jobs. |
| 7998 | * OTOH, in non-interactive shell this is useless |
| 7999 | * and only leads to extra job checks */ |
| 8000 | if (pi->num_cmds == 0) { |
| 8001 | if (G_interactive_fd) |
| 8002 | goto check_jobs_and_continue; |
| 8003 | continue; |
| 8004 | } |
| 8005 | |
| 8006 | /* After analyzing all keywords and conditions, we decided |
| 8007 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8008 | * after run_pipe to collect any background children, |
| 8009 | * even if list execution is to be stopped. */ |
| 8010 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8011 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8012 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8013 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8014 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8015 | if (r != -1) { |
| 8016 | /* We ran a builtin, function, or group. |
| 8017 | * rcode is already known |
| 8018 | * and we don't need to wait for anything. */ |
| 8019 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8020 | G.last_exitcode = rcode; |
| 8021 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8022 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8023 | /* Was it "break" or "continue"? */ |
| 8024 | if (G.flag_break_continue) { |
| 8025 | smallint fbc = G.flag_break_continue; |
| 8026 | /* We might fall into outer *loop*, |
| 8027 | * don't want to break it too */ |
| 8028 | if (loop_top) { |
| 8029 | G.depth_break_continue--; |
| 8030 | if (G.depth_break_continue == 0) |
| 8031 | G.flag_break_continue = 0; |
| 8032 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 8033 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 8034 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8035 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8036 | break; |
| 8037 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8038 | /* "continue": simulate end of loop */ |
| 8039 | rword = RES_DONE; |
| 8040 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8041 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8042 | #endif |
| 8043 | if (G_flag_return_in_progress == 1) { |
| 8044 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 8045 | break; |
| 8046 | } |
| 8047 | } else if (pi->followup == PIPE_BG) { |
| 8048 | /* What does bash do with attempts to background builtins? */ |
| 8049 | /* even bash 3.2 doesn't do that well with nested bg: |
| 8050 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 8051 | * I'm NOT treating inner &'s as jobs */ |
| 8052 | #if ENABLE_HUSH_JOB |
| 8053 | if (G.run_list_level == 1) |
| 8054 | insert_bg_job(pi); |
| 8055 | #endif |
| 8056 | /* Last command's pid goes to $! */ |
| 8057 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 8058 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 8059 | /* 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] | 8060 | rcode = EXIT_SUCCESS; |
| 8061 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8062 | } else { |
| 8063 | #if ENABLE_HUSH_JOB |
| 8064 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8065 | /* Waits for completion, then fg's main shell */ |
| 8066 | rcode = checkjobs_and_fg_shell(pi); |
| 8067 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8068 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8069 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8070 | #endif |
| 8071 | /* This one just waits for completion */ |
| 8072 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8073 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8074 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8075 | G.last_exitcode = rcode; |
| 8076 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8077 | } |
| 8078 | |
| 8079 | /* Analyze how result affects subsequent commands */ |
| 8080 | #if ENABLE_HUSH_IF |
| 8081 | if (rword == RES_IF || rword == RES_ELIF) |
| 8082 | cond_code = rcode; |
| 8083 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8084 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8085 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8086 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8087 | #if ENABLE_HUSH_LOOPS |
| 8088 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8089 | if (pi->next |
| 8090 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 8091 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8092 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8093 | if (rword == RES_WHILE) { |
| 8094 | if (rcode) { |
| 8095 | /* "while false; do...done" - exitcode 0 */ |
| 8096 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8097 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8098 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8099 | } |
| 8100 | } |
| 8101 | if (rword == RES_UNTIL) { |
| 8102 | if (!rcode) { |
| 8103 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8104 | break; |
| 8105 | } |
| 8106 | } |
| 8107 | } |
| 8108 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8109 | } /* for (pi) */ |
| 8110 | |
| 8111 | #if ENABLE_HUSH_JOB |
| 8112 | G.run_list_level--; |
| 8113 | #endif |
| 8114 | #if ENABLE_HUSH_LOOPS |
| 8115 | if (loop_top) |
| 8116 | G.depth_of_loop--; |
| 8117 | free(for_list); |
| 8118 | #endif |
| 8119 | #if ENABLE_HUSH_CASE |
| 8120 | free(case_word); |
| 8121 | #endif |
| 8122 | debug_leave(); |
| 8123 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 8124 | return rcode; |
| 8125 | } |
| 8126 | |
| 8127 | /* Select which version we will use */ |
| 8128 | static int run_and_free_list(struct pipe *pi) |
| 8129 | { |
| 8130 | int rcode = 0; |
| 8131 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8132 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8133 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 8134 | rcode = run_list(pi); |
| 8135 | } |
| 8136 | /* free_pipe_list has the side effect of clearing memory. |
| 8137 | * In the long run that function can be merged with run_list, |
| 8138 | * but doing that now would hobble the debugging effort. */ |
| 8139 | free_pipe_list(pi); |
| 8140 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 8141 | return rcode; |
| 8142 | } |
| 8143 | |
| 8144 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8145 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 8146 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8147 | sighandler_t old_handler; |
| 8148 | unsigned sig = 0; |
| 8149 | while ((mask >>= 1) != 0) { |
| 8150 | sig++; |
| 8151 | if (!(mask & 1)) |
| 8152 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8153 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8154 | /* POSIX allows shell to re-enable SIGCHLD |
| 8155 | * even if it was SIG_IGN on entry. |
| 8156 | * Therefore we skip IGN check for it: |
| 8157 | */ |
| 8158 | if (sig == SIGCHLD) |
| 8159 | continue; |
| 8160 | if (old_handler == SIG_IGN) { |
| 8161 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8162 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8163 | if (!G.traps) |
| 8164 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8165 | free(G.traps[sig]); |
| 8166 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 8167 | } |
| 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | /* Called a few times only (or even once if "sh -c") */ |
| 8172 | static void install_special_sighandlers(void) |
| 8173 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8174 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8175 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8176 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8177 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8178 | if (G_interactive_fd) { |
| 8179 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 8180 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8181 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8182 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8183 | /* Careful, do not re-install handlers we already installed */ |
| 8184 | if (G.special_sig_mask != mask) { |
| 8185 | unsigned diff = mask & ~G.special_sig_mask; |
| 8186 | G.special_sig_mask = mask; |
| 8187 | install_sighandlers(diff); |
| 8188 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8189 | } |
| 8190 | |
| 8191 | #if ENABLE_HUSH_JOB |
| 8192 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8193 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8194 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8195 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8196 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8197 | |
| 8198 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8199 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8200 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 8201 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8202 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 8203 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8204 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8205 | + (1 << SIGABRT) |
| 8206 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 8207 | + (1 << SIGPIPE) |
| 8208 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8209 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8210 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8211 | * we never want to restore pgrp on exit, and this fn is not called |
| 8212 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8213 | /*+ (1 << SIGHUP )*/ |
| 8214 | /*+ (1 << SIGTERM)*/ |
| 8215 | /*+ (1 << SIGINT )*/ |
| 8216 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8217 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8218 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8219 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8220 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 8221 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 8222 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8223 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8224 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8225 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8226 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8227 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8228 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8229 | break; |
| 8230 | case 'x': |
| 8231 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 8232 | break; |
| 8233 | case 'o': |
| 8234 | if (!o_opt) { |
| 8235 | /* "set -+o" without parameter. |
| 8236 | * in bash, set -o produces this output: |
| 8237 | * pipefail off |
| 8238 | * and set +o: |
| 8239 | * set +o pipefail |
| 8240 | * We always use the second form. |
| 8241 | */ |
| 8242 | const char *p = o_opt_strings; |
| 8243 | idx = 0; |
| 8244 | while (*p) { |
| 8245 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 8246 | idx++; |
| 8247 | p += strlen(p) + 1; |
| 8248 | } |
| 8249 | break; |
| 8250 | } |
| 8251 | idx = index_in_strings(o_opt_strings, o_opt); |
| 8252 | if (idx >= 0) { |
| 8253 | G.o_opt[idx] = state; |
| 8254 | break; |
| 8255 | } |
| 8256 | default: |
| 8257 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8258 | } |
| 8259 | return EXIT_SUCCESS; |
| 8260 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8261 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 8262 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 8263 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8264 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8265 | enum { |
| 8266 | OPT_login = (1 << 0), |
| 8267 | }; |
| 8268 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8269 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8270 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8271 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8272 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8273 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 8274 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 8275 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8276 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8277 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8278 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8279 | #if ENABLE_HUSH_FAST |
| 8280 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 8281 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8282 | #if !BB_MMU |
| 8283 | G.argv0_for_re_execing = argv[0]; |
| 8284 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8285 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8286 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 8287 | shell_ver->flg_export = 1; |
| 8288 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 8289 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 8290 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8291 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8292 | /* Create shell local variables from the values |
| 8293 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 8294 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8295 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8296 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8297 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8298 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8299 | if (e) while (*e) { |
| 8300 | char *value = strchr(*e, '='); |
| 8301 | if (value) { /* paranoia */ |
| 8302 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 8303 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 8304 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8305 | cur_var->max_len = strlen(*e); |
| 8306 | cur_var->flg_export = 1; |
| 8307 | } |
| 8308 | e++; |
| 8309 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8310 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8311 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 8312 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8313 | |
| 8314 | /* Export PWD */ |
| 8315 | set_pwd_var(/*exp:*/ 1); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8316 | |
| 8317 | #if ENABLE_HUSH_BASH_COMPAT |
| 8318 | /* Set (but not export) HOSTNAME unless already set */ |
| 8319 | if (!get_local_var_value("HOSTNAME")) { |
| 8320 | struct utsname uts; |
| 8321 | uname(&uts); |
| 8322 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 8323 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8324 | /* bash also exports SHLVL and _, |
| 8325 | * and sets (but doesn't export) the following variables: |
| 8326 | * BASH=/bin/bash |
| 8327 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 8328 | * BASH_VERSION='3.2.0(1)-release' |
| 8329 | * HOSTTYPE=i386 |
| 8330 | * MACHTYPE=i386-pc-linux-gnu |
| 8331 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8332 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8333 | * EUID=<NNNNN> |
| 8334 | * UID=<NNNNN> |
| 8335 | * GROUPS=() |
| 8336 | * LINES=<NNN> |
| 8337 | * COLUMNS=<NNN> |
| 8338 | * BASH_ARGC=() |
| 8339 | * BASH_ARGV=() |
| 8340 | * BASH_LINENO=() |
| 8341 | * BASH_SOURCE=() |
| 8342 | * DIRSTACK=() |
| 8343 | * PIPESTATUS=([0]="0") |
| 8344 | * HISTFILE=/<xxx>/.bash_history |
| 8345 | * HISTFILESIZE=500 |
| 8346 | * HISTSIZE=500 |
| 8347 | * MAILCHECK=60 |
| 8348 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 8349 | * SHELL=/bin/bash |
| 8350 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 8351 | * TERM=dumb |
| 8352 | * OPTERR=1 |
| 8353 | * OPTIND=1 |
| 8354 | * IFS=$' \t\n' |
| 8355 | * PS1='\s-\v\$ ' |
| 8356 | * PS2='> ' |
| 8357 | * PS4='+ ' |
| 8358 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8359 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8360 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 8361 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 8362 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 8363 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 8364 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 8365 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 8366 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 8367 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8368 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 8369 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8370 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8371 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8372 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8373 | * 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] | 8374 | * in order to intercept (more) signals. |
| 8375 | */ |
| 8376 | |
| 8377 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8378 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8379 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8380 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8381 | while (1) { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8382 | opt = getopt(argc, argv, "+c:xinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8383 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8384 | "<:$:R:V:" |
| 8385 | # if ENABLE_HUSH_FUNCTIONS |
| 8386 | "F:" |
| 8387 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8388 | #endif |
| 8389 | ); |
| 8390 | if (opt <= 0) |
| 8391 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8392 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8393 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8394 | /* Possibilities: |
| 8395 | * sh ... -c 'script' |
| 8396 | * sh ... -c 'script' ARG0 [ARG1...] |
| 8397 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8398 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8399 | * "" needs to be replaced with NULL |
| 8400 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8401 | * Note: the form without ARG0 never happens: |
| 8402 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8403 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8404 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8405 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8406 | G.root_ppid = getppid(); |
| 8407 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8408 | G.global_argv = argv + optind; |
| 8409 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8410 | if (builtin_argc) { |
| 8411 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 8412 | const struct built_in_command *x; |
| 8413 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8414 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8415 | x = find_builtin(optarg); |
| 8416 | if (x) { /* paranoia */ |
| 8417 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 8418 | G.global_argv += builtin_argc; |
| 8419 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8420 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8421 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8422 | } |
| 8423 | goto final_return; |
| 8424 | } |
| 8425 | if (!G.global_argv[0]) { |
| 8426 | /* -c 'script' (no params): prevent empty $0 */ |
| 8427 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 8428 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 8429 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8430 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8431 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8432 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8433 | goto final_return; |
| 8434 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 8435 | /* Well, we cannot just declare interactiveness, |
| 8436 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8437 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8438 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8439 | case 's': |
| 8440 | /* "-s" means "read from stdin", but this is how we always |
| 8441 | * operate, so simply do nothing here. */ |
| 8442 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8443 | case 'l': |
| 8444 | flags |= OPT_login; |
| 8445 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8446 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8447 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 8448 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8449 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8450 | case '$': { |
| 8451 | unsigned long long empty_trap_mask; |
| 8452 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8453 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 8454 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8455 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 8456 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8457 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 8458 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8459 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8460 | optarg++; |
| 8461 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8462 | optarg++; |
| 8463 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 8464 | if (empty_trap_mask != 0) { |
| 8465 | int sig; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8466 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8467 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8468 | for (sig = 1; sig < NSIG; sig++) { |
| 8469 | if (empty_trap_mask & (1LL << sig)) { |
| 8470 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8471 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8472 | } |
| 8473 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8474 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8475 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8476 | optarg++; |
| 8477 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8478 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8479 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8480 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8481 | case 'R': |
| 8482 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8483 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8484 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8485 | # if ENABLE_HUSH_FUNCTIONS |
| 8486 | case 'F': { |
| 8487 | struct function *funcp = new_function(optarg); |
| 8488 | /* funcp->name is already set to optarg */ |
| 8489 | /* funcp->body is set to NULL. It's a special case. */ |
| 8490 | funcp->body_as_string = argv[optind]; |
| 8491 | optind++; |
| 8492 | break; |
| 8493 | } |
| 8494 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8495 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8496 | case 'n': |
| 8497 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8498 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8499 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8500 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8501 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8502 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 8503 | " or: sh -c command [args]...\n\n"); |
| 8504 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8505 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8506 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 8507 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8508 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8509 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8510 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8511 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 8512 | G.global_argc = argc - (optind - 1); |
| 8513 | G.global_argv = argv + (optind - 1); |
| 8514 | G.global_argv[0] = argv[0]; |
| 8515 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8516 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8517 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8518 | G.root_ppid = getppid(); |
| 8519 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8520 | |
| 8521 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8522 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8523 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8524 | debug_printf("sourcing /etc/profile\n"); |
| 8525 | input = fopen_for_read("/etc/profile"); |
| 8526 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8527 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8528 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8529 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8530 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8531 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8532 | /* bash: after sourcing /etc/profile, |
| 8533 | * tries to source (in the given order): |
| 8534 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8535 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8536 | * bash also sources ~/.bash_logout on exit. |
| 8537 | * If called as sh, skips .bash_XXX files. |
| 8538 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8539 | } |
| 8540 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8541 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8542 | FILE *input; |
| 8543 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8544 | * "bash <script>" (which is never interactive (unless -i?)) |
| 8545 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8546 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 8547 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8548 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8549 | G.global_argc--; |
| 8550 | G.global_argv++; |
| 8551 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8552 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8553 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 8554 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8555 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8556 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8557 | parse_and_run_file(input); |
| 8558 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 8559 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8560 | #endif |
| 8561 | goto final_return; |
| 8562 | } |
| 8563 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8564 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8565 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8566 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8567 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 8568 | /* A shell is interactive if the '-i' flag was given, |
| 8569 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 8570 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8571 | * no arguments remaining or the -s flag given |
| 8572 | * standard input is a terminal |
| 8573 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8574 | * Refer to Posix.2, the description of the 'sh' utility. |
| 8575 | */ |
| 8576 | #if ENABLE_HUSH_JOB |
| 8577 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8578 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 8579 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 8580 | if (G_saved_tty_pgrp < 0) |
| 8581 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8582 | |
| 8583 | /* try to dup stdin to high fd#, >= 255 */ |
| 8584 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8585 | if (G_interactive_fd < 0) { |
| 8586 | /* try to dup to any fd */ |
| 8587 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8588 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8589 | /* give up */ |
| 8590 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8591 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 8592 | } |
| 8593 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8594 | // TODO: track & disallow any attempts of user |
| 8595 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8596 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8597 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8598 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8599 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8600 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8601 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8602 | /* If we were run as 'hush &', sleep until we are |
| 8603 | * in the foreground (tty pgrp == our pgrp). |
| 8604 | * If we get started under a job aware app (like bash), |
| 8605 | * make sure we are now in charge so we don't fight over |
| 8606 | * who gets the foreground */ |
| 8607 | while (1) { |
| 8608 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8609 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 8610 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8611 | break; |
| 8612 | /* send TTIN to ourself (should stop us) */ |
| 8613 | kill(- shell_pgrp, SIGTTIN); |
| 8614 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8615 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8616 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8617 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8618 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8619 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8620 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8621 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8622 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8623 | /* Put ourselves in our own process group |
| 8624 | * (bash, too, does this only if ctty is available) */ |
| 8625 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 8626 | /* Grab control of the terminal */ |
| 8627 | tcsetpgrp(G_interactive_fd, getpid()); |
| 8628 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 8629 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8630 | |
| 8631 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 8632 | { |
| 8633 | const char *hp = get_local_var_value("HISTFILE"); |
| 8634 | if (!hp) { |
| 8635 | hp = get_local_var_value("HOME"); |
| 8636 | if (hp) |
| 8637 | hp = concat_path_file(hp, ".hush_history"); |
| 8638 | } else { |
| 8639 | hp = xstrdup(hp); |
| 8640 | } |
| 8641 | if (hp) { |
| 8642 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 8643 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 8644 | } |
| 8645 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 8646 | hp = get_local_var_value("HISTFILESIZE"); |
| 8647 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 8648 | # endif |
| 8649 | } |
| 8650 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8651 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8652 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8653 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8654 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8655 | /* No job control compiled in, only prompt/line editing */ |
| 8656 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8657 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 8658 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8659 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8660 | G_interactive_fd = dup(STDIN_FILENO); |
| 8661 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8662 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8663 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 8664 | } |
| 8665 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8666 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8667 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8668 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8669 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8670 | #else |
| 8671 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8672 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8673 | #endif |
| 8674 | /* bash: |
| 8675 | * if interactive but not a login shell, sources ~/.bashrc |
| 8676 | * (--norc turns this off, --rcfile <file> overrides) |
| 8677 | */ |
| 8678 | |
| 8679 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8680 | /* note: ash and hush share this string */ |
| 8681 | printf("\n\n%s %s\n" |
| 8682 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8683 | "\n", |
| 8684 | bb_banner, |
| 8685 | "hush - the humble shell" |
| 8686 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 8687 | } |
| 8688 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8689 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8690 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8691 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8692 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8693 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 8694 | |
| 8695 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8696 | #if ENABLE_MSH |
| 8697 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 8698 | int msh_main(int argc, char **argv) |
| 8699 | { |
Denys Vlasenko | ed6ff5e | 2016-09-30 12:28:37 +0200 | [diff] [blame] | 8700 | bb_error_msg("msh is deprecated, please use hush instead"); |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 8701 | return hush_main(argc, argv); |
| 8702 | } |
| 8703 | #endif |
| 8704 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8705 | |
| 8706 | /* |
| 8707 | * Built-ins |
| 8708 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8709 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8710 | { |
| 8711 | return 0; |
| 8712 | } |
| 8713 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8714 | 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] | 8715 | { |
| 8716 | int argc = 0; |
| 8717 | while (*argv) { |
| 8718 | argc++; |
| 8719 | argv++; |
| 8720 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 8721 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 8722 | } |
| 8723 | |
| 8724 | static int FAST_FUNC builtin_test(char **argv) |
| 8725 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8726 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8727 | } |
| 8728 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8729 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8730 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8731 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8732 | } |
| 8733 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 8734 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8735 | static int FAST_FUNC builtin_printf(char **argv) |
| 8736 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8737 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 8738 | } |
| 8739 | #endif |
| 8740 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8741 | static char **skip_dash_dash(char **argv) |
| 8742 | { |
| 8743 | argv++; |
| 8744 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 8745 | argv++; |
| 8746 | return argv; |
| 8747 | } |
| 8748 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8749 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8750 | { |
| 8751 | int rcode = EXIT_SUCCESS; |
| 8752 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8753 | argv = skip_dash_dash(argv); |
| 8754 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8755 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8756 | /* bash: |
| 8757 | * eval "echo Hi; done" ("done" is syntax error): |
| 8758 | * "echo Hi" will not execute too. |
| 8759 | */ |
| 8760 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8761 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8762 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8763 | } |
| 8764 | return rcode; |
| 8765 | } |
| 8766 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8767 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8768 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8769 | const char *newdir; |
| 8770 | |
| 8771 | argv = skip_dash_dash(argv); |
| 8772 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8773 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8774 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8775 | * bash says "bash: cd: HOME not set" and does nothing |
| 8776 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8777 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 8778 | const char *home = get_local_var_value("HOME"); |
| 8779 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 8780 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8781 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8782 | /* Mimic bash message exactly */ |
| 8783 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8784 | return EXIT_FAILURE; |
| 8785 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8786 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 8787 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 8788 | * set it again, but do not export. bash does the same. |
| 8789 | */ |
| 8790 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8791 | return EXIT_SUCCESS; |
| 8792 | } |
| 8793 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8794 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8795 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8796 | argv = skip_dash_dash(argv); |
| 8797 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8798 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8799 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 8800 | /* Careful: we can end up here after [v]fork. Do not restore |
| 8801 | * tty pgrp then, only top-level shell process does that */ |
| 8802 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 8803 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 8804 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 8805 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8806 | * 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] | 8807 | * and tcsetpgrp, and this is inherently racy. |
| 8808 | */ |
| 8809 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8812 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8813 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 8814 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8815 | |
| 8816 | /* interactive bash: |
| 8817 | * # trap "echo EEE" EXIT |
| 8818 | * # exit |
| 8819 | * exit |
| 8820 | * There are stopped jobs. |
| 8821 | * (if there are _stopped_ jobs, running ones don't count) |
| 8822 | * # exit |
| 8823 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 8824 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8825 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8826 | * 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] | 8827 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8828 | |
| 8829 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8830 | argv = skip_dash_dash(argv); |
| 8831 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8832 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8833 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8834 | xfunc_error_retval = 255; |
| 8835 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8836 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8837 | } |
| 8838 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8839 | static void print_escaped(const char *s) |
| 8840 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8841 | if (*s == '\'') |
| 8842 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8843 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8844 | const char *p = strchrnul(s, '\''); |
| 8845 | /* print 'xxxx', possibly just '' */ |
| 8846 | printf("'%.*s'", (int)(p - s), s); |
| 8847 | if (*p == '\0') |
| 8848 | break; |
| 8849 | s = p; |
| 8850 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8851 | /* s points to '; print "'''...'''" */ |
| 8852 | putchar('"'); |
| 8853 | do putchar('\''); while (*++s == '\''); |
| 8854 | putchar('"'); |
| 8855 | } while (*s); |
| 8856 | } |
| 8857 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8858 | #if !ENABLE_HUSH_LOCAL |
| 8859 | #define helper_export_local(argv, exp, lvl) \ |
| 8860 | helper_export_local(argv, exp) |
| 8861 | #endif |
| 8862 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8863 | { |
| 8864 | do { |
| 8865 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8866 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8867 | |
| 8868 | /* So far we do not check that name is valid (TODO?) */ |
| 8869 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8870 | if (*name_end == '\0') { |
| 8871 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8872 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8873 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8874 | var = vpp ? *vpp : NULL; |
| 8875 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8876 | if (exp == -1) { /* unexporting? */ |
| 8877 | /* export -n NAME (without =VALUE) */ |
| 8878 | if (var) { |
| 8879 | var->flg_export = 0; |
| 8880 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8881 | unsetenv(name); |
| 8882 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8883 | continue; |
| 8884 | } |
| 8885 | if (exp == 1) { /* exporting? */ |
| 8886 | /* export NAME (without =VALUE) */ |
| 8887 | if (var) { |
| 8888 | var->flg_export = 1; |
| 8889 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8890 | putenv(var->varstr); |
| 8891 | continue; |
| 8892 | } |
| 8893 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8894 | #if ENABLE_HUSH_LOCAL |
| 8895 | if (exp == 0 /* local? */ |
| 8896 | && var && var->func_nest_level == lvl |
| 8897 | ) { |
| 8898 | /* "local x=abc; ...; local x" - ignore second local decl */ |
Denys Vlasenko | 80729a4 | 2016-10-02 22:33:15 +0200 | [diff] [blame] | 8899 | continue; |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8900 | } |
| 8901 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8902 | /* Exporting non-existing variable. |
| 8903 | * bash does not put it in environment, |
| 8904 | * but remembers that it is exported, |
| 8905 | * and does put it in env when it is set later. |
| 8906 | * We just set it to "" and export. */ |
| 8907 | /* Or, it's "local NAME" (without =VALUE). |
| 8908 | * bash sets the value to "". */ |
| 8909 | name = xasprintf("%s=", name); |
| 8910 | } else { |
| 8911 | /* (Un)exporting/making local NAME=VALUE */ |
| 8912 | name = xstrdup(name); |
| 8913 | } |
| 8914 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8915 | } while (*++argv); |
| 8916 | } |
| 8917 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8918 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8919 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8920 | unsigned opt_unexport; |
| 8921 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8922 | #if ENABLE_HUSH_EXPORT_N |
| 8923 | /* "!": do not abort on errors */ |
| 8924 | opt_unexport = getopt32(argv, "!n"); |
| 8925 | if (opt_unexport == (uint32_t)-1) |
| 8926 | return EXIT_FAILURE; |
| 8927 | argv += optind; |
| 8928 | #else |
| 8929 | opt_unexport = 0; |
| 8930 | argv++; |
| 8931 | #endif |
| 8932 | |
| 8933 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8934 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8935 | if (e) { |
| 8936 | while (*e) { |
| 8937 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8938 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8939 | #else |
| 8940 | /* ash emits: export VAR='VAL' |
| 8941 | * bash: declare -x VAR="VAL" |
| 8942 | * we follow ash example */ |
| 8943 | const char *s = *e++; |
| 8944 | const char *p = strchr(s, '='); |
| 8945 | |
| 8946 | if (!p) /* wtf? take next variable */ |
| 8947 | continue; |
| 8948 | /* export var= */ |
| 8949 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8950 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8951 | putchar('\n'); |
| 8952 | #endif |
| 8953 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8954 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8955 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8956 | return EXIT_SUCCESS; |
| 8957 | } |
| 8958 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8959 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8960 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8961 | return EXIT_SUCCESS; |
| 8962 | } |
| 8963 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8964 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8965 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8966 | { |
| 8967 | if (G.func_nest_level == 0) { |
| 8968 | bb_error_msg("%s: not in a function", argv[0]); |
| 8969 | return EXIT_FAILURE; /* bash compat */ |
| 8970 | } |
| 8971 | helper_export_local(argv, 0, G.func_nest_level); |
| 8972 | return EXIT_SUCCESS; |
| 8973 | } |
| 8974 | #endif |
| 8975 | |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 8976 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 8977 | static int FAST_FUNC builtin_unset(char **argv) |
| 8978 | { |
| 8979 | int ret; |
| 8980 | unsigned opts; |
| 8981 | |
| 8982 | /* "!": do not abort on errors */ |
| 8983 | /* "+": stop at 1st non-option */ |
| 8984 | opts = getopt32(argv, "!+vf"); |
| 8985 | if (opts == (unsigned)-1) |
| 8986 | return EXIT_FAILURE; |
| 8987 | if (opts == 3) { |
| 8988 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8989 | return EXIT_FAILURE; |
| 8990 | } |
| 8991 | argv += optind; |
| 8992 | |
| 8993 | ret = EXIT_SUCCESS; |
| 8994 | while (*argv) { |
| 8995 | if (!(opts & 2)) { /* not -f */ |
| 8996 | if (unset_local_var(*argv)) { |
| 8997 | /* unset <nonexistent_var> doesn't fail. |
| 8998 | * Error is when one tries to unset RO var. |
| 8999 | * Message was printed by unset_local_var. */ |
| 9000 | ret = EXIT_FAILURE; |
| 9001 | } |
| 9002 | } |
| 9003 | #if ENABLE_HUSH_FUNCTIONS |
| 9004 | else { |
| 9005 | unset_func(*argv); |
| 9006 | } |
| 9007 | #endif |
| 9008 | argv++; |
| 9009 | } |
| 9010 | return ret; |
| 9011 | } |
| 9012 | |
| 9013 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 9014 | * built-in 'set' handler |
| 9015 | * SUSv3 says: |
| 9016 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 9017 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 9018 | * set -- [argument...] |
| 9019 | * set -o |
| 9020 | * set +o |
| 9021 | * Implementations shall support the options in both their hyphen and |
| 9022 | * plus-sign forms. These options can also be specified as options to sh. |
| 9023 | * Examples: |
| 9024 | * Write out all variables and their values: set |
| 9025 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 9026 | * Turn on the -x and -v options: set -xv |
| 9027 | * Unset all positional parameters: set -- |
| 9028 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 9029 | * Set the positional parameters to the expansion of x, even if x expands |
| 9030 | * with a leading '-' or '+': set -- $x |
| 9031 | * |
| 9032 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 9033 | */ |
| 9034 | static int FAST_FUNC builtin_set(char **argv) |
| 9035 | { |
| 9036 | int n; |
| 9037 | char **pp, **g_argv; |
| 9038 | char *arg = *++argv; |
| 9039 | |
| 9040 | if (arg == NULL) { |
| 9041 | struct variable *e; |
| 9042 | for (e = G.top_var; e; e = e->next) |
| 9043 | puts(e->varstr); |
| 9044 | return EXIT_SUCCESS; |
| 9045 | } |
| 9046 | |
| 9047 | do { |
| 9048 | if (strcmp(arg, "--") == 0) { |
| 9049 | ++argv; |
| 9050 | goto set_argv; |
| 9051 | } |
| 9052 | if (arg[0] != '+' && arg[0] != '-') |
| 9053 | break; |
| 9054 | for (n = 1; arg[n]; ++n) { |
| 9055 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 9056 | goto error; |
| 9057 | if (arg[n] == 'o' && argv[1]) |
| 9058 | argv++; |
| 9059 | } |
| 9060 | } while ((arg = *++argv) != NULL); |
| 9061 | /* Now argv[0] is 1st argument */ |
| 9062 | |
| 9063 | if (arg == NULL) |
| 9064 | return EXIT_SUCCESS; |
| 9065 | set_argv: |
| 9066 | |
| 9067 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 9068 | g_argv = G.global_argv; |
| 9069 | if (G.global_args_malloced) { |
| 9070 | pp = g_argv; |
| 9071 | while (*++pp) |
| 9072 | free(*pp); |
| 9073 | g_argv[1] = NULL; |
| 9074 | } else { |
| 9075 | G.global_args_malloced = 1; |
| 9076 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 9077 | pp[0] = g_argv[0]; /* retain $0 */ |
| 9078 | g_argv = pp; |
| 9079 | } |
| 9080 | /* This realloc's G.global_argv */ |
| 9081 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 9082 | |
| 9083 | n = 1; |
| 9084 | while (*++pp) |
| 9085 | n++; |
| 9086 | G.global_argc = n; |
| 9087 | |
| 9088 | return EXIT_SUCCESS; |
| 9089 | |
| 9090 | /* Nothing known, so abort */ |
| 9091 | error: |
| 9092 | bb_error_msg("set: %s: invalid option", arg); |
| 9093 | return EXIT_FAILURE; |
| 9094 | } |
| 9095 | |
| 9096 | static int FAST_FUNC builtin_shift(char **argv) |
| 9097 | { |
| 9098 | int n = 1; |
| 9099 | argv = skip_dash_dash(argv); |
| 9100 | if (argv[0]) { |
| 9101 | n = atoi(argv[0]); |
| 9102 | } |
| 9103 | if (n >= 0 && n < G.global_argc) { |
| 9104 | if (G.global_args_malloced) { |
| 9105 | int m = 1; |
| 9106 | while (m <= n) |
| 9107 | free(G.global_argv[m++]); |
| 9108 | } |
| 9109 | G.global_argc -= n; |
| 9110 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 9111 | G.global_argc * sizeof(G.global_argv[0])); |
| 9112 | return EXIT_SUCCESS; |
| 9113 | } |
| 9114 | return EXIT_FAILURE; |
| 9115 | } |
| 9116 | |
| 9117 | /* Interruptibility of read builtin in bash |
| 9118 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9119 | * |
| 9120 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9121 | * |
| 9122 | * SIGINT: |
| 9123 | * - terminates non-interactive shell; |
| 9124 | * - interrupts read in interactive shell; |
| 9125 | * if it has non-empty trap: |
| 9126 | * - executes trap and returns to command prompt in interactive shell; |
| 9127 | * - executes trap and returns to read in non-interactive shell; |
| 9128 | * SIGTERM: |
| 9129 | * - is ignored (does not interrupt) read in interactive shell; |
| 9130 | * - terminates non-interactive shell; |
| 9131 | * if it has non-empty trap: |
| 9132 | * - executes trap and returns to read; |
| 9133 | * SIGHUP: |
| 9134 | * - terminates shell (regardless of interactivity); |
| 9135 | * if it has non-empty trap: |
| 9136 | * - executes trap and returns to read; |
| 9137 | */ |
| 9138 | static int FAST_FUNC builtin_read(char **argv) |
| 9139 | { |
| 9140 | const char *r; |
| 9141 | char *opt_n = NULL; |
| 9142 | char *opt_p = NULL; |
| 9143 | char *opt_t = NULL; |
| 9144 | char *opt_u = NULL; |
| 9145 | const char *ifs; |
| 9146 | int read_flags; |
| 9147 | |
| 9148 | /* "!": do not abort on errors. |
| 9149 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9150 | */ |
| 9151 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 9152 | if (read_flags == (uint32_t)-1) |
| 9153 | return EXIT_FAILURE; |
| 9154 | argv += optind; |
| 9155 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9156 | |
| 9157 | again: |
| 9158 | r = shell_builtin_read(set_local_var_from_halves, |
| 9159 | argv, |
| 9160 | ifs, |
| 9161 | read_flags, |
| 9162 | opt_n, |
| 9163 | opt_p, |
| 9164 | opt_t, |
| 9165 | opt_u |
| 9166 | ); |
| 9167 | |
| 9168 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9169 | unsigned sig = check_and_run_traps(); |
| 9170 | if (sig && sig != SIGINT) |
| 9171 | goto again; |
| 9172 | } |
| 9173 | |
| 9174 | if ((uintptr_t)r > 1) { |
| 9175 | bb_error_msg("%s", r); |
| 9176 | r = (char*)(uintptr_t)1; |
| 9177 | } |
| 9178 | |
| 9179 | return (uintptr_t)r; |
| 9180 | } |
| 9181 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9182 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9183 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9184 | int sig; |
| 9185 | char *new_cmd; |
| 9186 | |
| 9187 | if (!G.traps) |
| 9188 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 9189 | |
| 9190 | argv++; |
| 9191 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9192 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9193 | /* No args: print all trapped */ |
| 9194 | for (i = 0; i < NSIG; ++i) { |
| 9195 | if (G.traps[i]) { |
| 9196 | printf("trap -- "); |
| 9197 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 9198 | /* note: bash adds "SIG", but only if invoked |
| 9199 | * as "bash". If called as "sh", or if set -o posix, |
| 9200 | * then it prints short signal names. |
| 9201 | * We are printing short names: */ |
| 9202 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9203 | } |
| 9204 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9205 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9206 | return EXIT_SUCCESS; |
| 9207 | } |
| 9208 | |
| 9209 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9210 | /* If first arg is a number: reset all specified signals */ |
| 9211 | sig = bb_strtou(*argv, NULL, 10); |
| 9212 | if (errno == 0) { |
| 9213 | int ret; |
| 9214 | process_sig_list: |
| 9215 | ret = EXIT_SUCCESS; |
| 9216 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9217 | sighandler_t handler; |
| 9218 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9219 | sig = get_signum(*argv++); |
| 9220 | if (sig < 0 || sig >= NSIG) { |
| 9221 | ret = EXIT_FAILURE; |
| 9222 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 9223 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9224 | continue; |
| 9225 | } |
| 9226 | |
| 9227 | free(G.traps[sig]); |
| 9228 | G.traps[sig] = xstrdup(new_cmd); |
| 9229 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9230 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9231 | get_signame(sig), sig, G.traps[sig]); |
| 9232 | |
| 9233 | /* There is no signal for 0 (EXIT) */ |
| 9234 | if (sig == 0) |
| 9235 | continue; |
| 9236 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9237 | if (new_cmd) |
| 9238 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 9239 | else |
| 9240 | /* We are removing trap handler */ |
| 9241 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9242 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9243 | } |
| 9244 | return ret; |
| 9245 | } |
| 9246 | |
| 9247 | if (!argv[1]) { /* no second arg */ |
| 9248 | bb_error_msg("trap: invalid arguments"); |
| 9249 | return EXIT_FAILURE; |
| 9250 | } |
| 9251 | |
| 9252 | /* First arg is "-": reset all specified to default */ |
| 9253 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 9254 | /* Everything else: set arg as signal handler |
| 9255 | * (includes "" case, which ignores signal) */ |
| 9256 | if (argv[0][0] == '-') { |
| 9257 | if (argv[0][1] == '\0') { /* "-" */ |
| 9258 | /* new_cmd remains NULL: "reset these sigs" */ |
| 9259 | goto reset_traps; |
| 9260 | } |
| 9261 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 9262 | argv++; |
| 9263 | } |
| 9264 | /* else: "-something", no special meaning */ |
| 9265 | } |
| 9266 | new_cmd = *argv; |
| 9267 | reset_traps: |
| 9268 | argv++; |
| 9269 | goto process_sig_list; |
| 9270 | } |
| 9271 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 9272 | #if ENABLE_HUSH_TYPE |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9273 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9274 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9275 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9276 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9277 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9278 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9279 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 9280 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9281 | |
| 9282 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9283 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9284 | type = "an alias";*/ |
| 9285 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9286 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9287 | type = "a function"; |
| 9288 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9289 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9290 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9291 | else if ((path = find_in_path(*argv)) != NULL) |
| 9292 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9293 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 9294 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9295 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9296 | continue; |
| 9297 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9298 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 9299 | printf("%s is %s\n", *argv, type); |
| 9300 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9301 | } |
| 9302 | |
| 9303 | return ret; |
| 9304 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 9305 | #endif |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 9306 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9307 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9308 | static struct pipe *parse_jobspec(const char *str) |
| 9309 | { |
| 9310 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 9311 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9312 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 9313 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 9314 | if (str[0] != '%' |
| 9315 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 9316 | ) { |
| 9317 | bb_error_msg("bad argument '%s'", str); |
| 9318 | return NULL; |
| 9319 | } |
| 9320 | /* It is "%%", "%+" or "%" - current job */ |
| 9321 | jobnum = G.last_jobid; |
| 9322 | if (jobnum == 0) { |
| 9323 | bb_error_msg("no current job"); |
| 9324 | return NULL; |
| 9325 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9326 | } |
| 9327 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9328 | if (pi->jobid == jobnum) { |
| 9329 | return pi; |
| 9330 | } |
| 9331 | } |
| 9332 | bb_error_msg("%d: no such job", jobnum); |
| 9333 | return NULL; |
| 9334 | } |
| 9335 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9336 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9337 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9338 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9339 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9340 | struct pipe *pi; |
| 9341 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9342 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9343 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9344 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9345 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 9346 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9347 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9348 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9349 | goto found; |
| 9350 | } |
| 9351 | } |
| 9352 | bb_error_msg("%s: no current job", argv[0]); |
| 9353 | return EXIT_FAILURE; |
| 9354 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 9355 | |
| 9356 | pi = parse_jobspec(argv[1]); |
| 9357 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9358 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9359 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 9360 | /* TODO: bash prints a string representation |
| 9361 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9362 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9363 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9364 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
| 9367 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9368 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 9369 | for (i = 0; i < pi->num_cmds; i++) { |
| 9370 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9371 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9372 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9373 | |
| 9374 | i = kill(- pi->pgrp, SIGCONT); |
| 9375 | if (i < 0) { |
| 9376 | if (errno == ESRCH) { |
| 9377 | delete_finished_bg_job(pi); |
| 9378 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9379 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9380 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9381 | } |
| 9382 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9383 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9384 | remove_bg_job(pi); |
| 9385 | return checkjobs_and_fg_shell(pi); |
| 9386 | } |
| 9387 | return EXIT_SUCCESS; |
| 9388 | } |
| 9389 | #endif |
| 9390 | |
| 9391 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9392 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9393 | { |
| 9394 | const struct built_in_command *x; |
| 9395 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9396 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 9397 | "Built-in commands:\n" |
| 9398 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9399 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9400 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9401 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9402 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9403 | return EXIT_SUCCESS; |
| 9404 | } |
| 9405 | #endif |
| 9406 | |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 9407 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9408 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9409 | { |
| 9410 | show_history(G.line_input_state); |
| 9411 | return EXIT_SUCCESS; |
| 9412 | } |
| 9413 | #endif |
| 9414 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9415 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9416 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9417 | { |
| 9418 | struct pipe *job; |
| 9419 | const char *status_string; |
| 9420 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9421 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9422 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 9423 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9424 | status_string = "Stopped"; |
| 9425 | else |
| 9426 | status_string = "Running"; |
| 9427 | |
| 9428 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 9429 | } |
| 9430 | return EXIT_SUCCESS; |
| 9431 | } |
| 9432 | #endif |
| 9433 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9434 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9435 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9436 | { |
| 9437 | void *p; |
| 9438 | unsigned long l; |
| 9439 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9440 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 9441 | /* Optional. Reduces probability of false positives */ |
| 9442 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9443 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9444 | /* Crude attempt to find where "free memory" starts, |
| 9445 | * sans fragmentation. */ |
| 9446 | p = malloc(240); |
| 9447 | l = (unsigned long)p; |
| 9448 | free(p); |
| 9449 | p = malloc(3400); |
| 9450 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 9451 | free(p); |
| 9452 | |
Denys Vlasenko | 7f0ebbc | 2016-10-03 17:42:53 +0200 | [diff] [blame] | 9453 | |
| 9454 | # if 0 /* debug */ |
| 9455 | { |
| 9456 | struct mallinfo mi = mallinfo(); |
| 9457 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 9458 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 9459 | } |
| 9460 | # endif |
| 9461 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9462 | if (!G.memleak_value) |
| 9463 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 9464 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 9465 | l -= G.memleak_value; |
| 9466 | if ((long)l < 0) |
| 9467 | l = 0; |
| 9468 | l /= 1024; |
| 9469 | if (l > 127) |
| 9470 | l = 127; |
| 9471 | |
| 9472 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 9473 | return l; |
| 9474 | } |
| 9475 | #endif |
| 9476 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9477 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9478 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9479 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9480 | return EXIT_SUCCESS; |
| 9481 | } |
| 9482 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9483 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9484 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9485 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9486 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9487 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9488 | #if ENABLE_HUSH_FUNCTIONS |
| 9489 | smallint sv_flg; |
| 9490 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9491 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9492 | argv = skip_dash_dash(argv); |
| 9493 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9494 | if (!filename) { |
| 9495 | /* bash says: "bash: .: filename argument required" */ |
| 9496 | return 2; /* bash compat */ |
| 9497 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9498 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9499 | if (!strchr(filename, '/')) { |
| 9500 | arg_path = find_in_path(filename); |
| 9501 | if (arg_path) |
| 9502 | filename = arg_path; |
| 9503 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9504 | input = remember_FILE(fopen_or_warn(filename, "r")); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 9505 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9506 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9507 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9508 | /* POSIX: non-interactive shell should abort here, |
| 9509 | * not merely fail. So far no one complained :) |
| 9510 | */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9511 | return EXIT_FAILURE; |
| 9512 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9513 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9514 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9515 | sv_flg = G_flag_return_in_progress; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9516 | /* "we are inside sourced file, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9517 | G_flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9518 | #endif |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9519 | if (argv[1]) |
| 9520 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9521 | |
Denys Vlasenko | 992e0ff | 2016-09-29 01:27:09 +0200 | [diff] [blame] | 9522 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 9523 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9524 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9525 | fclose_and_forget(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 9526 | |
Denys Vlasenko | 88b532d | 2013-03-17 14:11:04 +0100 | [diff] [blame] | 9527 | if (argv[1]) |
| 9528 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9529 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9530 | G_flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 9531 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9532 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9533 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9534 | } |
| 9535 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9536 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9537 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9538 | int rc; |
| 9539 | mode_t mask; |
| 9540 | |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9541 | rc = 1; |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9542 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9543 | argv = skip_dash_dash(argv); |
| 9544 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9545 | mode_t old_mask = mask; |
| 9546 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9547 | /* numeric umasks are taken as-is */ |
| 9548 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9549 | if (!isdigit(argv[0][0])) |
| 9550 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9551 | mask = bb_parse_mode(argv[0], mask); |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 9552 | if (!isdigit(argv[0][0])) |
| 9553 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9554 | if ((unsigned)mask > 0777) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9555 | mask = old_mask; |
| 9556 | /* bash messages: |
| 9557 | * bash: umask: 'q': invalid symbolic mode operator |
| 9558 | * bash: umask: 999: octal number out of range |
| 9559 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 9560 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 9561 | rc = 0; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9562 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9563 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9564 | /* Mimic bash */ |
| 9565 | printf("%04o\n", (unsigned) mask); |
| 9566 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9567 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 9568 | umask(mask); |
| 9569 | |
| 9570 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 9573 | #if ENABLE_HUSH_KILL |
| 9574 | static int FAST_FUNC builtin_kill(char **argv) |
| 9575 | { |
| 9576 | int ret = 0; |
| 9577 | |
| 9578 | argv = skip_dash_dash(argv); |
| 9579 | if (argv[0] && strcmp(argv[0], "-l") != 0) { |
| 9580 | int i = 0; |
| 9581 | |
| 9582 | do { |
| 9583 | struct pipe *pi; |
| 9584 | char *dst; |
| 9585 | int j, n; |
| 9586 | |
| 9587 | if (argv[i][0] != '%') |
| 9588 | continue; |
| 9589 | /* |
| 9590 | * "kill %N" - job kill |
| 9591 | * Converting to pgrp / pid kill |
| 9592 | */ |
| 9593 | pi = parse_jobspec(argv[i]); |
| 9594 | if (!pi) { |
| 9595 | /* Eat bad jobspec */ |
| 9596 | j = i; |
| 9597 | do { |
| 9598 | j++; |
| 9599 | argv[j - 1] = argv[j]; |
| 9600 | } while (argv[j]); |
| 9601 | ret = 1; |
| 9602 | i--; |
| 9603 | continue; |
| 9604 | } |
| 9605 | /* |
| 9606 | * In jobs started under job control, we signal |
| 9607 | * entire process group by kill -PGRP_ID. |
| 9608 | * This happens, f.e., in interactive shell. |
| 9609 | * |
| 9610 | * Otherwise, we signal each child via |
| 9611 | * kill PID1 PID2 PID3. |
| 9612 | * Testcases: |
| 9613 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 9614 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 9615 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 9616 | */ |
| 9617 | n = pi->num_cmds; |
| 9618 | if (ENABLE_HUSH_JOB && G_interactive_fd) |
| 9619 | n = 1; |
| 9620 | dst = alloca(n * sizeof(int)*4); |
| 9621 | argv[i] = dst; |
| 9622 | #if ENABLE_HUSH_JOB |
| 9623 | if (G_interactive_fd) |
| 9624 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
| 9625 | else |
| 9626 | #endif |
| 9627 | for (j = 0; j < n; j++) { |
| 9628 | struct command *cmd = &pi->cmds[j]; |
| 9629 | /* Skip exited members of the job */ |
| 9630 | if (cmd->pid == 0) |
| 9631 | continue; |
| 9632 | /* |
| 9633 | * kill_main has matching code to expect |
| 9634 | * leading space. Needed to not confuse |
| 9635 | * negative pids with "kill -SIGNAL_NO" syntax |
| 9636 | */ |
| 9637 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 9638 | } |
| 9639 | *dst = '\0'; |
| 9640 | } while (argv[++i]); |
| 9641 | } |
| 9642 | |
| 9643 | if (argv[0] || ret == 0) { |
| 9644 | argv--; |
| 9645 | argv[0] = (char*)"kill"; /* why? think about "kill -- PID" */ |
| 9646 | /* kill_main also handles "killall" etc, so it does look at argv[0]! */ |
| 9647 | ret = run_applet_main(argv, kill_main); |
| 9648 | } |
| 9649 | return ret; |
| 9650 | } |
| 9651 | #endif |
| 9652 | |
| 9653 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9654 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9655 | #if !ENABLE_HUSH_JOB |
| 9656 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 9657 | #endif |
| 9658 | 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] | 9659 | { |
| 9660 | int ret = 0; |
| 9661 | for (;;) { |
| 9662 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9663 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9664 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9665 | if (!sigisemptyset(&G.pending_set)) |
| 9666 | goto check_sig; |
| 9667 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9668 | /* waitpid is not interruptible by SA_RESTARTed |
| 9669 | * signals which we use. Thus, this ugly dance: |
| 9670 | */ |
| 9671 | |
| 9672 | /* Make sure possible SIGCHLD is stored in kernel's |
| 9673 | * pending signal mask before we call waitpid. |
| 9674 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9675 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9676 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9677 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 9678 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9679 | |
| 9680 | if (!sigisemptyset(&G.pending_set)) { |
| 9681 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9682 | goto restore; |
| 9683 | } |
| 9684 | |
| 9685 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9686 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9687 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9688 | debug_printf_exec("checkjobs:%d\n", ret); |
| 9689 | #if ENABLE_HUSH_JOB |
| 9690 | if (waitfor_pipe) { |
| 9691 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 9692 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 9693 | if (rcode >= 0) { |
| 9694 | ret = rcode; |
| 9695 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9696 | break; |
| 9697 | } |
| 9698 | } |
| 9699 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9700 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 9701 | /* if ret == 0, no children changed state */ |
| 9702 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9703 | if (errno == ECHILD || ret) { |
| 9704 | ret--; |
| 9705 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9706 | ret = 0; |
| 9707 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 9708 | break; |
| 9709 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9710 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9711 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 9712 | /* Note: sigsuspend invokes signal handler */ |
| 9713 | sigsuspend(&oldset); |
| 9714 | restore: |
| 9715 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9716 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9717 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9718 | sig = check_and_run_traps(); |
| 9719 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9720 | ret = 128 + sig; |
| 9721 | break; |
| 9722 | } |
| 9723 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 9724 | } |
| 9725 | return ret; |
| 9726 | } |
| 9727 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9728 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9729 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9730 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9731 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9732 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9733 | argv = skip_dash_dash(argv); |
| 9734 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 9735 | /* Don't care about wait results */ |
| 9736 | /* Note 1: must wait until there are no more children */ |
| 9737 | /* Note 2: must be interruptible */ |
| 9738 | /* Examples: |
| 9739 | * $ sleep 3 & sleep 6 & wait |
| 9740 | * [1] 30934 sleep 3 |
| 9741 | * [2] 30935 sleep 6 |
| 9742 | * [1] Done sleep 3 |
| 9743 | * [2] Done sleep 6 |
| 9744 | * $ sleep 3 & sleep 6 & wait |
| 9745 | * [1] 30936 sleep 3 |
| 9746 | * [2] 30937 sleep 6 |
| 9747 | * [1] Done sleep 3 |
| 9748 | * ^C <-- after ~4 sec from keyboard |
| 9749 | * $ |
| 9750 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9751 | 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] | 9752 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9753 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9754 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9755 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9756 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9757 | #if ENABLE_HUSH_JOB |
| 9758 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9759 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 9760 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9761 | wait_pipe = parse_jobspec(*argv); |
| 9762 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9763 | ret = job_exited_or_stopped(wait_pipe); |
| 9764 | if (ret < 0) |
| 9765 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9766 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 9767 | /* else: parse_jobspec() already emitted error msg */ |
| 9768 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9769 | } |
| 9770 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9771 | /* mimic bash message */ |
| 9772 | 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] | 9773 | ret = EXIT_FAILURE; |
| 9774 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9775 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9776 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9777 | /* Do we have such child? */ |
| 9778 | ret = waitpid(pid, &status, WNOHANG); |
| 9779 | if (ret < 0) { |
| 9780 | /* No */ |
| 9781 | if (errno == ECHILD) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9782 | if (G.last_bg_pid > 0 && pid == G.last_bg_pid) { |
| 9783 | /* "wait $!" but last bg task has already exited. Try: |
| 9784 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 9785 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 9786 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9787 | */ |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 9788 | /* ret = G.last_bg_pid_exitstatus - FIXME */ |
| 9789 | } else { |
| 9790 | /* Example: "wait 1". mimic bash message */ |
| 9791 | 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] | 9792 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9793 | } else { |
| 9794 | /* ??? */ |
| 9795 | bb_perror_msg("wait %s", *argv); |
| 9796 | } |
| 9797 | ret = 127; |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9798 | continue; /* bash checks all argv[] */ |
| 9799 | } |
| 9800 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9801 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9802 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9803 | } else { |
| 9804 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 9805 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 9806 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9807 | if (WIFSIGNALED(status)) |
| 9808 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9809 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 9810 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9811 | |
| 9812 | return ret; |
| 9813 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame^] | 9814 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 9815 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9816 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 9817 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 9818 | { |
| 9819 | if (argv[1]) { |
| 9820 | def = bb_strtou(argv[1], NULL, 10); |
| 9821 | if (errno || def < def_min || argv[2]) { |
| 9822 | bb_error_msg("%s: bad arguments", argv[0]); |
| 9823 | def = UINT_MAX; |
| 9824 | } |
| 9825 | } |
| 9826 | return def; |
| 9827 | } |
| 9828 | #endif |
| 9829 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9830 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9831 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9832 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9833 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9834 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9835 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9836 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 9837 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 9838 | return EXIT_SUCCESS; /* bash compat */ |
| 9839 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 9840 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9841 | |
| 9842 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 9843 | if (depth == UINT_MAX) |
| 9844 | G.flag_break_continue = BC_BREAK; |
| 9845 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9846 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9847 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9848 | return EXIT_SUCCESS; |
| 9849 | } |
| 9850 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9851 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9852 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 9853 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 9854 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 9855 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 9856 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9857 | |
| 9858 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9859 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9860 | { |
| 9861 | int rc; |
| 9862 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9863 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9864 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 9865 | return EXIT_FAILURE; /* bash compat */ |
| 9866 | } |
| 9867 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9868 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 9869 | |
| 9870 | /* bash: |
| 9871 | * out of range: wraps around at 256, does not error out |
| 9872 | * non-numeric param: |
| 9873 | * f() { false; return qwe; }; f; echo $? |
| 9874 | * bash: return: qwe: numeric argument required <== we do this |
| 9875 | * 255 <== we also do this |
| 9876 | */ |
| 9877 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 9878 | return rc; |
| 9879 | } |
| 9880 | #endif |