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) |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 44 | * make complex ${var%...} constructs support optional |
| 45 | * make here documents optional |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 46 | * special variables (done: PWD, PPID, RANDOM) |
| 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * tilde expansion |
| 49 | * aliases |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 50 | * "command" missing features: |
| 51 | * command -p CMD: run CMD using default $PATH |
| 52 | * (can use this to override standalone shell as well?) |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 53 | * command BLTIN: disables special-ness (e.g. errors do not abort) |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 54 | * command -V CMD1 CMD2 CMD3 (multiple args) (not in standard) |
| 55 | * builtins mandated by standards we don't support: |
| 56 | * [un]alias, fc: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 57 | * fc -l[nr] [BEG] [END]: list range of commands in history |
| 58 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands |
| 59 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 60 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 61 | * Bash compat TODO: |
| 62 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 63 | * reserved words: function select |
| 64 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 65 | * process substitution: <(list) and >(list) |
| 66 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 67 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 68 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 69 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 70 | * 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] | 71 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 72 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 73 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 74 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 75 | * indirect expansion: ${!VAR} |
| 76 | * substring op on @: ${@:n:m} |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 77 | * |
| 78 | * Won't do: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 79 | * Some builtins mandated by standards: |
| 80 | * newgrp [GRP]: not a builtin in bash but a suid binary |
| 81 | * which spawns a new shell with new group ID |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 82 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 83 | //config:config HUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 84 | //config: bool "hush (64 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 85 | //config: default y |
| 86 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 87 | //config: hush is a small shell. It handles the normal flow control |
| 88 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 89 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 90 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 91 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 92 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 93 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 94 | //config: It does not handle select, aliases, tilde expansion, |
| 95 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 96 | //config: |
| 97 | //config:config HUSH_BASH_COMPAT |
| 98 | //config: bool "bash-compatible extensions" |
| 99 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 100 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 101 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 102 | //config:config HUSH_BRACE_EXPANSION |
| 103 | //config: bool "Brace expansion" |
| 104 | //config: default y |
| 105 | //config: depends on HUSH_BASH_COMPAT |
| 106 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 107 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 108 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 109 | //config:config HUSH_LINENO_VAR |
| 110 | //config: bool "$LINENO variable" |
| 111 | //config: default y |
| 112 | //config: depends on HUSH_BASH_COMPAT |
| 113 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 114 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 115 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 116 | //config: default n # do not encourage non-standard behavior |
| 117 | //config: depends on HUSH_BASH_COMPAT |
| 118 | //config: help |
| 119 | //config: This is not compliant with standards. Avoid if possible. |
| 120 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 121 | //config:config HUSH_INTERACTIVE |
| 122 | //config: bool "Interactive mode" |
| 123 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 124 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 125 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 126 | //config: Enable interactive mode (prompt and command editing). |
| 127 | //config: Without this, hush simply reads and executes commands |
| 128 | //config: from stdin just like a shell script from a file. |
| 129 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 130 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 131 | //config:config HUSH_SAVEHISTORY |
| 132 | //config: bool "Save command history to .hush_history" |
| 133 | //config: default y |
| 134 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 135 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 136 | //config:config HUSH_JOB |
| 137 | //config: bool "Job control" |
| 138 | //config: default y |
| 139 | //config: depends on HUSH_INTERACTIVE |
| 140 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 141 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 142 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 143 | //config: "cmd &" still works by simply spawning a process and immediately |
| 144 | //config: prompting for next command (or executing next command in a script), |
| 145 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 146 | //config: |
| 147 | //config:config HUSH_TICK |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 148 | //config: bool "Support process substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 149 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 150 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 151 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 152 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 153 | //config: |
| 154 | //config:config HUSH_IF |
| 155 | //config: bool "Support if/then/elif/else/fi" |
| 156 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 157 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 158 | //config: |
| 159 | //config:config HUSH_LOOPS |
| 160 | //config: bool "Support for, while and until loops" |
| 161 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 162 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 163 | //config: |
| 164 | //config:config HUSH_CASE |
| 165 | //config: bool "Support case ... esac statement" |
| 166 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 167 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 168 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 169 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 170 | //config: |
| 171 | //config:config HUSH_FUNCTIONS |
| 172 | //config: bool "Support funcname() { commands; } syntax" |
| 173 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 174 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 175 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 176 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 177 | //config: |
| 178 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 179 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 180 | //config: default y |
| 181 | //config: depends on HUSH_FUNCTIONS |
| 182 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 183 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 184 | //config: |
| 185 | //config:config HUSH_RANDOM_SUPPORT |
| 186 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 187 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 188 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 190 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 191 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 192 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 193 | //config:config HUSH_MODE_X |
| 194 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 195 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 196 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 197 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 198 | //config: This instructs hush to print commands before execution. |
| 199 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 200 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 201 | //config:config HUSH_ECHO |
| 202 | //config: bool "echo builtin" |
| 203 | //config: default y |
| 204 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 205 | //config: |
| 206 | //config:config HUSH_PRINTF |
| 207 | //config: bool "printf builtin" |
| 208 | //config: default y |
| 209 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 210 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 211 | //config:config HUSH_TEST |
| 212 | //config: bool "test builtin" |
| 213 | //config: default y |
| 214 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 215 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 216 | //config:config HUSH_HELP |
| 217 | //config: bool "help builtin" |
| 218 | //config: default y |
| 219 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 220 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 221 | //config:config HUSH_EXPORT |
| 222 | //config: bool "export builtin" |
| 223 | //config: default y |
| 224 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 225 | //config: |
| 226 | //config:config HUSH_EXPORT_N |
| 227 | //config: bool "Support 'export -n' option" |
| 228 | //config: default y |
| 229 | //config: depends on HUSH_EXPORT |
| 230 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 231 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 232 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 233 | //config:config HUSH_READONLY |
| 234 | //config: bool "readonly builtin" |
| 235 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 236 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 237 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 238 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 239 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 240 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 241 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 242 | //config: default y |
| 243 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 244 | //config: |
| 245 | //config:config HUSH_WAIT |
| 246 | //config: bool "wait builtin" |
| 247 | //config: default y |
| 248 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 249 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 250 | //config:config HUSH_COMMAND |
| 251 | //config: bool "command builtin" |
| 252 | //config: default y |
| 253 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 254 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 255 | //config:config HUSH_TRAP |
| 256 | //config: bool "trap builtin" |
| 257 | //config: default y |
| 258 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 259 | //config: |
| 260 | //config:config HUSH_TYPE |
| 261 | //config: bool "type builtin" |
| 262 | //config: default y |
| 263 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 264 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 265 | //config:config HUSH_TIMES |
| 266 | //config: bool "times builtin" |
| 267 | //config: default y |
| 268 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 269 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 270 | //config:config HUSH_READ |
| 271 | //config: bool "read builtin" |
| 272 | //config: default y |
| 273 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 274 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 275 | //config:config HUSH_SET |
| 276 | //config: bool "set builtin" |
| 277 | //config: default y |
| 278 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 279 | //config: |
| 280 | //config:config HUSH_UNSET |
| 281 | //config: bool "unset builtin" |
| 282 | //config: default y |
| 283 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 284 | //config: |
| 285 | //config:config HUSH_ULIMIT |
| 286 | //config: bool "ulimit builtin" |
| 287 | //config: default y |
| 288 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 289 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 290 | //config:config HUSH_UMASK |
| 291 | //config: bool "umask builtin" |
| 292 | //config: default y |
| 293 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 294 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 295 | //config:config HUSH_GETOPTS |
| 296 | //config: bool "getopts builtin" |
| 297 | //config: default y |
| 298 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 299 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 300 | //config:config HUSH_MEMLEAK |
| 301 | //config: bool "memleak builtin (debugging)" |
| 302 | //config: default n |
| 303 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 304 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 305 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 306 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 307 | //applet:IF_SH_IS_HUSH( APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 308 | //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] | 309 | |
| 310 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 311 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 312 | //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] | 313 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 314 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 315 | /* -i (interactive) is also accepted, |
| 316 | * but does nothing, therefore not shown in help. |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 317 | * NOMMU-specific options are not meant to be used by users, |
| 318 | * therefore we don't show them either. |
| 319 | */ |
| 320 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 321 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 322 | //usage:#define hush_full_usage "\n\n" |
| 323 | //usage: "Unix shell interpreter" |
| 324 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 325 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 326 | || defined(__APPLE__) \ |
| 327 | ) |
| 328 | # include <malloc.h> /* for malloc_trim */ |
| 329 | #endif |
| 330 | #include <glob.h> |
| 331 | /* #include <dmalloc.h> */ |
| 332 | #if ENABLE_HUSH_CASE |
| 333 | # include <fnmatch.h> |
| 334 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 335 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 336 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 337 | |
| 338 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 339 | #include "unicode.h" |
| 340 | #include "shell_common.h" |
| 341 | #include "math.h" |
| 342 | #include "match.h" |
| 343 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 344 | # include "random.h" |
| 345 | #else |
| 346 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 347 | #endif |
| 348 | #ifndef F_DUPFD_CLOEXEC |
| 349 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 350 | #endif |
| 351 | #ifndef PIPE_BUF |
| 352 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 353 | #endif |
| 354 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 355 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 356 | /* So far, all bash compat is controlled by one config option */ |
| 357 | /* Separate defines document which part of code implements what */ |
| 358 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 359 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 360 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 361 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 362 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 363 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 364 | |
| 365 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 366 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 367 | #define LEAK_HUNTING 0 |
| 368 | #define BUILD_AS_NOMMU 0 |
| 369 | /* Enable/disable sanity checks. Ok to enable in production, |
| 370 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 371 | * Keeping 1 for now even in released versions. |
| 372 | */ |
| 373 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 374 | /* Slightly bigger (+200 bytes), but faster hush. |
| 375 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 376 | * which allows us to do fewer waitpid's. |
| 377 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 378 | * and therefore waitpid will return the same result as last time) |
| 379 | */ |
| 380 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 381 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 382 | * So far ${var%...} ops are always enabled: |
| 383 | */ |
| 384 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 385 | |
| 386 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 387 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 388 | # undef BB_MMU |
| 389 | # undef USE_FOR_NOMMU |
| 390 | # undef USE_FOR_MMU |
| 391 | # define BB_MMU 0 |
| 392 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 393 | # define USE_FOR_MMU(...) |
| 394 | #endif |
| 395 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 396 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 397 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 398 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 399 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 400 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 401 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 402 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 403 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 404 | # define IF_FEATURE_SH_STANDALONE(...) |
| 405 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 406 | #endif |
| 407 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 408 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 409 | # undef ENABLE_FEATURE_EDITING |
| 410 | # define ENABLE_FEATURE_EDITING 0 |
| 411 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 412 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 413 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 414 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 415 | #endif |
| 416 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 417 | /* Do we support ANY keywords? */ |
| 418 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 419 | # define HAS_KEYWORDS 1 |
| 420 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 421 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 422 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 423 | # define HAS_KEYWORDS 0 |
| 424 | # define IF_HAS_KEYWORDS(...) |
| 425 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 426 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 427 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 428 | /* If you comment out one of these below, it will be #defined later |
| 429 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 430 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 431 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 432 | #define debug_printf_parse(...) do {} while (0) |
| 433 | #define debug_print_tree(a, b) do {} while (0) |
| 434 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 435 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 436 | #define debug_printf_jobs(...) do {} while (0) |
| 437 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 438 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 439 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 440 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 441 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 442 | #define debug_printf_subst(...) do {} while (0) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 443 | #define debug_printf_prompt(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 446 | #define ERR_PTR ((void*)(long)1) |
| 447 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 448 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 449 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 450 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 451 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 452 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 453 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 454 | /* Support / and // replace ops */ |
| 455 | /* Note that // is stored as \ in "encoded" string representation */ |
| 456 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 457 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 458 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 459 | #else |
| 460 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 461 | # define VAR_SUBST_OPS "%#:-=+?" |
| 462 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 463 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 464 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 465 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 466 | #define SPECIAL_VAR_SYMBOL 3 |
| 467 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 468 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 469 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 470 | struct variable; |
| 471 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 472 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 473 | |
| 474 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 475 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 476 | */ |
| 477 | #if !BB_MMU |
| 478 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 479 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 480 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 481 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 482 | } nommu_save_t; |
| 483 | #endif |
| 484 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 485 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 486 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 487 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 488 | RES_IF , |
| 489 | RES_THEN , |
| 490 | RES_ELIF , |
| 491 | RES_ELSE , |
| 492 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 493 | #endif |
| 494 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 495 | RES_FOR , |
| 496 | RES_WHILE , |
| 497 | RES_UNTIL , |
| 498 | RES_DO , |
| 499 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 500 | #endif |
| 501 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 502 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 503 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 504 | #if ENABLE_HUSH_CASE |
| 505 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 506 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 507 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 508 | RES_MATCH , /* "word)" */ |
| 509 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 510 | RES_ESAC , |
| 511 | #endif |
| 512 | RES_XXXX , |
| 513 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 514 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 515 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 516 | typedef struct o_string { |
| 517 | char *data; |
| 518 | int length; /* position where data is appended */ |
| 519 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 520 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 521 | /* At least some part of the string was inside '' or "", |
| 522 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 523 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 524 | smallint has_empty_slot; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 525 | } o_string; |
| 526 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 527 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 528 | EXP_FLAG_GLOB = 0x2, |
| 529 | /* Protect newly added chars against globbing |
| 530 | * by prepending \ to *, ?, [, \ */ |
| 531 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 532 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 533 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 534 | #define NULL_O_STRING { NULL } |
| 535 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 536 | #ifndef debug_printf_parse |
| 537 | static const char *const assignment_flag[] = { |
| 538 | "MAYBE_ASSIGNMENT", |
| 539 | "DEFINITELY_ASSIGNMENT", |
| 540 | "NOT_ASSIGNMENT", |
| 541 | "WORD_IS_KEYWORD", |
| 542 | }; |
| 543 | #endif |
| 544 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 545 | typedef struct in_str { |
| 546 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 547 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 548 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 549 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 550 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 551 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 552 | /* The descrip member of this structure is only used to make |
| 553 | * debugging output pretty */ |
| 554 | static const struct { |
| 555 | int mode; |
| 556 | signed char default_fd; |
| 557 | char descrip[3]; |
| 558 | } redir_table[] = { |
| 559 | { O_RDONLY, 0, "<" }, |
| 560 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 561 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 562 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 563 | { O_RDONLY, 0, "<<" }, |
| 564 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 565 | /* { O_RDONLY, 77, "<<" }, */ |
| 566 | }; |
| 567 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 568 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 569 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 570 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 571 | int rd_fd; /* fd to redirect */ |
| 572 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 573 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 574 | smallint rd_type; /* (enum redir_type) */ |
| 575 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 576 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 577 | * bit 0: do we need to trim leading tabs? |
| 578 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 579 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 580 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 581 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 582 | REDIRECT_INPUT = 0, |
| 583 | REDIRECT_OVERWRITE = 1, |
| 584 | REDIRECT_APPEND = 2, |
| 585 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 586 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 587 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 588 | |
| 589 | REDIRFD_CLOSE = -3, |
| 590 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 591 | REDIRFD_TO_FILE = -1, |
| 592 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 593 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 594 | HEREDOC_SKIPTABS = 1, |
| 595 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 596 | } redir_type; |
| 597 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 599 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 600 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 601 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 602 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 603 | unsigned lineno; |
| 604 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 605 | smallint cmd_type; /* CMD_xxx */ |
| 606 | #define CMD_NORMAL 0 |
| 607 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 608 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 609 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 610 | * "export v=t*" |
| 611 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 612 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 613 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 614 | #if ENABLE_HUSH_FUNCTIONS |
| 615 | # define CMD_FUNCDEF 3 |
| 616 | #endif |
| 617 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 618 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 619 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 620 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 621 | #if !BB_MMU |
| 622 | char *group_as_string; |
| 623 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 624 | #if ENABLE_HUSH_FUNCTIONS |
| 625 | struct function *child_func; |
| 626 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 627 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 628 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 629 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 630 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 631 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 632 | * we put those fields back into cmd->xxx |
| 633 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 634 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 635 | * Without this trick, loop would execute a;b;b;b;... |
| 636 | * instead of correct sequence a;b;a;b;... |
| 637 | * When command is freed, it severs the link |
| 638 | * (sets ->child_func->parent_cmd to NULL). |
| 639 | */ |
| 640 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 641 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 642 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 643 | * and on execution these are substituted with their values. |
| 644 | * Substitution can make _several_ words out of one argv[n]! |
| 645 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 646 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 647 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 648 | struct redir_struct *redirects; /* I/O redirections */ |
| 649 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 650 | /* Is there anything in this command at all? */ |
| 651 | #define IS_NULL_CMD(cmd) \ |
| 652 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 653 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 654 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 655 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 656 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 657 | int alive_cmds; /* number of commands running (not exited) */ |
| 658 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 659 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 660 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 661 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 662 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 663 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 664 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 665 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 666 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 667 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 668 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 669 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 670 | PIPE_SEQ = 0, |
| 671 | PIPE_AND = 1, |
| 672 | PIPE_OR = 2, |
| 673 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 674 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 675 | /* Is there anything in this pipe at all? */ |
| 676 | #define IS_NULL_PIPE(pi) \ |
| 677 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 678 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 679 | /* This holds pointers to the various results of parsing */ |
| 680 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 681 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 682 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 683 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 684 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 685 | /* last command in pipe (being constructed right now) */ |
| 686 | struct command *command; |
| 687 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 688 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 689 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 690 | #if !BB_MMU |
| 691 | o_string as_string; |
| 692 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 693 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 694 | #if HAS_KEYWORDS |
| 695 | smallint ctx_res_w; |
| 696 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 697 | #if ENABLE_HUSH_CASE |
| 698 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 699 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 700 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 701 | int old_flag; |
| 702 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 703 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 704 | * when we see "if" or "then", we malloc and copy current context, |
| 705 | * and make ->stack point to it. then we parse pipeN. |
| 706 | * when closing "then" / fi" / whatever is found, |
| 707 | * we move list_head into ->stack->command->group, |
| 708 | * copy ->stack into current context, and delete ->stack. |
| 709 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 710 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 711 | struct parse_context *stack; |
| 712 | #endif |
| 713 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 714 | enum { |
| 715 | MAYBE_ASSIGNMENT = 0, |
| 716 | DEFINITELY_ASSIGNMENT = 1, |
| 717 | NOT_ASSIGNMENT = 2, |
| 718 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 719 | WORD_IS_KEYWORD = 3, |
| 720 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 721 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 722 | /* On program start, environ points to initial environment. |
| 723 | * putenv adds new pointers into it, unsetenv removes them. |
| 724 | * Neither of these (de)allocates the strings. |
| 725 | * setenv allocates new strings in malloc space and does putenv, |
| 726 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 727 | #define setenv(...) setenv_is_leaky_dont_use() |
| 728 | struct variable { |
| 729 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 730 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 731 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 732 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 733 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 734 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 735 | }; |
| 736 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 737 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 738 | BC_BREAK = 1, |
| 739 | BC_CONTINUE = 2, |
| 740 | }; |
| 741 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 742 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 743 | struct function { |
| 744 | struct function *next; |
| 745 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 746 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 747 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 748 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 749 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 750 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 751 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 752 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 753 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 754 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 755 | /* set -/+o OPT support. (TODO: make it optional) |
| 756 | * bash supports the following opts: |
| 757 | * allexport off |
| 758 | * braceexpand on |
| 759 | * emacs on |
| 760 | * errexit off |
| 761 | * errtrace off |
| 762 | * functrace off |
| 763 | * hashall on |
| 764 | * histexpand off |
| 765 | * history on |
| 766 | * ignoreeof off |
| 767 | * interactive-comments on |
| 768 | * keyword off |
| 769 | * monitor on |
| 770 | * noclobber off |
| 771 | * noexec off |
| 772 | * noglob off |
| 773 | * nolog off |
| 774 | * notify off |
| 775 | * nounset off |
| 776 | * onecmd off |
| 777 | * physical off |
| 778 | * pipefail off |
| 779 | * posix off |
| 780 | * privileged off |
| 781 | * verbose off |
| 782 | * vi off |
| 783 | * xtrace off |
| 784 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 785 | static const char o_opt_strings[] ALIGN1 = |
| 786 | "pipefail\0" |
| 787 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 788 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 789 | #if ENABLE_HUSH_MODE_X |
| 790 | "xtrace\0" |
| 791 | #endif |
| 792 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 793 | enum { |
| 794 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 795 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 796 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 797 | #if ENABLE_HUSH_MODE_X |
| 798 | OPT_O_XTRACE, |
| 799 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 800 | NUM_OPT_O |
| 801 | }; |
| 802 | |
| 803 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 804 | struct FILE_list { |
| 805 | struct FILE_list *next; |
| 806 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 807 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 808 | }; |
| 809 | |
| 810 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 811 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 812 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 813 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 814 | /* interactive_fd != 0 means we are an interactive shell. |
| 815 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 816 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 817 | * job control still works, but terminal signals |
| 818 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 819 | * process groups can only be created with "cmd &". |
| 820 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 821 | * to give tty to the foreground process group, |
| 822 | * and will take it back when the group is stopped (^Z) |
| 823 | * or killed (^C). |
| 824 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 825 | #if ENABLE_HUSH_INTERACTIVE |
| 826 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 827 | * _AND_ if we decided to act interactively */ |
| 828 | int interactive_fd; |
| 829 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 830 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 831 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 832 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 833 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 834 | #endif |
| 835 | #if ENABLE_FEATURE_EDITING |
| 836 | line_input_t *line_input_state; |
| 837 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 838 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 839 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 840 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 841 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 842 | random_t random_gen; |
| 843 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 844 | #if ENABLE_HUSH_JOB |
| 845 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 846 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 847 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 848 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 849 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 850 | #else |
| 851 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 852 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 853 | /* How deeply are we in context where "set -e" is ignored */ |
| 854 | int errexit_depth; |
| 855 | /* "set -e" rules (do we follow them correctly?): |
| 856 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 857 | * Shell does not exit if failed command is part of condition in |
| 858 | * if/while, part of && or || list except the last command, any command |
| 859 | * in a pipe but the last, or if the command's return value is being |
| 860 | * inverted with !. If a compound command other than a subshell returns a |
| 861 | * non-zero status because a command failed while -e was being ignored, the |
| 862 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 863 | * exits [ERR is a bashism]. |
| 864 | * |
| 865 | * If a compound command or function executes in a context where -e is |
| 866 | * ignored, none of the commands executed within are affected by the -e |
| 867 | * setting. If a compound command or function sets -e while executing in a |
| 868 | * context where -e is ignored, that setting does not have any effect until |
| 869 | * the compound command or the command containing the function call completes. |
| 870 | */ |
| 871 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 872 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 873 | #if ENABLE_HUSH_MODE_X |
| 874 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 875 | #else |
| 876 | # define G_x_mode 0 |
| 877 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 878 | #if ENABLE_HUSH_INTERACTIVE |
| 879 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 880 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 881 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 882 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 883 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 884 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 885 | #if ENABLE_HUSH_FUNCTIONS |
| 886 | /* 0: outside of a function (or sourced file) |
| 887 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 888 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 889 | */ |
| 890 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 891 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 892 | #else |
| 893 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 894 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 895 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 896 | /* These support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 897 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 898 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 899 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 900 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 901 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 902 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 903 | # define G_global_args_malloced (G.global_args_malloced) |
| 904 | #else |
| 905 | # define G_global_args_malloced 0 |
| 906 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 907 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 908 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 909 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 910 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 911 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 912 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 913 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 914 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 915 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 916 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 917 | #if ENABLE_HUSH_GETOPTS |
| 918 | unsigned getopt_count; |
| 919 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 920 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 921 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 922 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 923 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 924 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 925 | unsigned var_nest_level; |
| 926 | #if ENABLE_HUSH_FUNCTIONS |
| 927 | # if ENABLE_HUSH_LOCAL |
| 928 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 929 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 930 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 931 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 932 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 933 | #if ENABLE_HUSH_FAST |
| 934 | unsigned count_SIGCHLD; |
| 935 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 936 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 937 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 938 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 939 | unsigned lineno; |
| 940 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 941 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 942 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 943 | /* Which signals have non-DFL handler (even with no traps set)? |
| 944 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 945 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 946 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 947 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 948 | * Other than these two times, never modified. |
| 949 | */ |
| 950 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 951 | #if ENABLE_HUSH_JOB |
| 952 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 953 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 954 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 955 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 956 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 957 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 958 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 959 | # define G_traps G.traps |
| 960 | #else |
| 961 | # define G_traps ((char**)NULL) |
| 962 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 963 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 964 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 965 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 966 | #endif |
| 967 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 968 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 969 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 970 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 971 | #if ENABLE_FEATURE_EDITING |
| 972 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 973 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 974 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 975 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 976 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 977 | * (too many defines). Also, I actually prefer to see when a variable |
| 978 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 979 | #define INIT_G() do { \ |
| 980 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 981 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 982 | sigfillset(&G.sa.sa_mask); \ |
| 983 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 984 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 985 | |
| 986 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 987 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 988 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 989 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 990 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 991 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 992 | static int builtin_eval(char **argv) FAST_FUNC; |
| 993 | static int builtin_exec(char **argv) FAST_FUNC; |
| 994 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 995 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 996 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 997 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 998 | #if ENABLE_HUSH_READONLY |
| 999 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1000 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1001 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1002 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1003 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1004 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1005 | #if ENABLE_HUSH_GETOPTS |
| 1006 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1007 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1008 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1009 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1010 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1011 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1012 | static int builtin_history(char **argv) FAST_FUNC; |
| 1013 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1014 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1015 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1016 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1017 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1018 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1019 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1020 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1021 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1022 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1023 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1024 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1025 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1026 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1027 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1028 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1029 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1030 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1031 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1032 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1033 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1034 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1035 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1036 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1037 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1038 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1039 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1040 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1041 | #if ENABLE_HUSH_TIMES |
| 1042 | static int builtin_times(char **argv) FAST_FUNC; |
| 1043 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1044 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1045 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1046 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1047 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1048 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1049 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1050 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1051 | #if ENABLE_HUSH_KILL |
| 1052 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1053 | #endif |
| 1054 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1055 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1056 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1057 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1058 | static int builtin_break(char **argv) FAST_FUNC; |
| 1059 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1060 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1061 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1062 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1063 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* Table of built-in functions. They can be forked or not, depending on |
| 1066 | * context: within pipes, they fork. As simple commands, they do not. |
| 1067 | * When used in non-forking context, they can change global variables |
| 1068 | * in the parent shell process. If forked, of course they cannot. |
| 1069 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1070 | * still be set at the end. */ |
| 1071 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1072 | const char *b_cmd; |
| 1073 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1074 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1075 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1076 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1077 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1078 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1079 | #endif |
| 1080 | }; |
| 1081 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1082 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1083 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1084 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1085 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1086 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1087 | #endif |
| 1088 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1089 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1090 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1091 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1092 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1093 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1094 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1095 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1096 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1097 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1098 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1099 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1100 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1101 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1102 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1103 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1104 | #if ENABLE_HUSH_GETOPTS |
| 1105 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1106 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1107 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1108 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1109 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1110 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1111 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1112 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1113 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1114 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1115 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1116 | #if ENABLE_HUSH_KILL |
| 1117 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1118 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1119 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1120 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1121 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1122 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1123 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1124 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1125 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1126 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1127 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1128 | #if ENABLE_HUSH_READONLY |
| 1129 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1130 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1131 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1132 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1133 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1134 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1135 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1136 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1137 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1138 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1139 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1140 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1141 | #if ENABLE_HUSH_TIMES |
| 1142 | BLTIN("times" , builtin_times , NULL), |
| 1143 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1144 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1145 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1146 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1147 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1148 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1149 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1150 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1151 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1152 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1153 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1154 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1155 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1156 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1157 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1158 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1159 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1160 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1161 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1162 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1163 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1164 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1165 | * (it's cheaper to run an external program in this case): |
| 1166 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1167 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1168 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1169 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1170 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1171 | #if BASH_TEST2 |
| 1172 | BLTIN("[[" , builtin_test , NULL), |
| 1173 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1174 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1175 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1176 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1177 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1178 | BLTIN("printf" , builtin_printf , NULL), |
| 1179 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1180 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1181 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1182 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1183 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1184 | }; |
| 1185 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1186 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1187 | /* Debug printouts. |
| 1188 | */ |
| 1189 | #if HUSH_DEBUG |
| 1190 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1191 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1192 | # define debug_enter() (G.debug_indent++) |
| 1193 | # define debug_leave() (G.debug_indent--) |
| 1194 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1195 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1196 | # define debug_enter() ((void)0) |
| 1197 | # define debug_leave() ((void)0) |
| 1198 | #endif |
| 1199 | |
| 1200 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1201 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1202 | #endif |
| 1203 | |
| 1204 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1205 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1206 | #endif |
| 1207 | |
| 1208 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1209 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1210 | #endif |
| 1211 | |
| 1212 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1213 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1214 | #endif |
| 1215 | |
| 1216 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1217 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1218 | # define DEBUG_JOBS 1 |
| 1219 | #else |
| 1220 | # define DEBUG_JOBS 0 |
| 1221 | #endif |
| 1222 | |
| 1223 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1224 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1225 | # define DEBUG_EXPAND 1 |
| 1226 | #else |
| 1227 | # define DEBUG_EXPAND 0 |
| 1228 | #endif |
| 1229 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1230 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1231 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1232 | #endif |
| 1233 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1234 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1235 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1236 | # define DEBUG_GLOB 1 |
| 1237 | #else |
| 1238 | # define DEBUG_GLOB 0 |
| 1239 | #endif |
| 1240 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1241 | #ifndef debug_printf_redir |
| 1242 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1243 | #endif |
| 1244 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1245 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1246 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1247 | #endif |
| 1248 | |
| 1249 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1250 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1251 | #endif |
| 1252 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1253 | #ifndef debug_printf_prompt |
| 1254 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1255 | #endif |
| 1256 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1257 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1258 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1259 | # define DEBUG_CLEAN 1 |
| 1260 | #else |
| 1261 | # define DEBUG_CLEAN 0 |
| 1262 | #endif |
| 1263 | |
| 1264 | #if DEBUG_EXPAND |
| 1265 | static void debug_print_strings(const char *prefix, char **vv) |
| 1266 | { |
| 1267 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1268 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1269 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1270 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1271 | } |
| 1272 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1273 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1274 | #endif |
| 1275 | |
| 1276 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1277 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1278 | */ |
| 1279 | #if LEAK_HUNTING |
| 1280 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1281 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1282 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1283 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1284 | return ptr; |
| 1285 | } |
| 1286 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1287 | { |
| 1288 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1289 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1290 | return ptr; |
| 1291 | } |
| 1292 | static char *xxstrdup(int lineno, const char *str) |
| 1293 | { |
| 1294 | char *ptr = xstrdup(str); |
| 1295 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1296 | return ptr; |
| 1297 | } |
| 1298 | static void xxfree(void *ptr) |
| 1299 | { |
| 1300 | fdprintf(2, "free %p\n", ptr); |
| 1301 | free(ptr); |
| 1302 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1303 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1304 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1305 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1306 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1307 | #endif |
| 1308 | |
| 1309 | |
| 1310 | /* Syntax and runtime errors. They always abort scripts. |
| 1311 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1312 | * and return to the prompt. |
| 1313 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1314 | */ |
| 1315 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1316 | # define msg_and_die_if_script(lineno, ...) msg_and_die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1317 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1318 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1319 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1320 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1321 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1324 | static void die_if_script(void) |
| 1325 | { |
| 1326 | if (!G_interactive_fd) { |
| 1327 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1328 | xfunc_error_retval = G.last_exitcode; |
| 1329 | xfunc_die(); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1334 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1335 | va_list p; |
| 1336 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1337 | #if HUSH_DEBUG >= 2 |
| 1338 | bb_error_msg("hush.c:%u", lineno); |
| 1339 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1340 | va_start(p, fmt); |
| 1341 | bb_verror_msg(fmt, p, NULL); |
| 1342 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1343 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1344 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1345 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1346 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1347 | { |
| 1348 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1349 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1350 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1351 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1352 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1355 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1356 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1357 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1358 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1361 | 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] | 1362 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1363 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1364 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1365 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1366 | } |
| 1367 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1368 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1369 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1370 | char msg[2] = { ch, '\0' }; |
| 1371 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1374 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1375 | { |
| 1376 | char msg[2]; |
| 1377 | msg[0] = ch; |
| 1378 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1379 | #if HUSH_DEBUG >= 2 |
| 1380 | bb_error_msg("hush.c:%u", lineno); |
| 1381 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1382 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1383 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1386 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1387 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1388 | # undef syntax_error |
| 1389 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1390 | # undef syntax_error_unterm_ch |
| 1391 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1392 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1393 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1394 | # define msg_and_die_if_script(...) msg_and_die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1395 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1396 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1397 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1398 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1399 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1400 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1401 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1402 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1403 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1404 | static void cmdedit_update_prompt(void); |
| 1405 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1406 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1407 | #endif |
| 1408 | |
| 1409 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1410 | /* Utility functions |
| 1411 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1412 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1413 | static char *unbackslash(char *src) |
| 1414 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1415 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1416 | while (1) { |
| 1417 | if (*src == '\\') |
| 1418 | src++; |
| 1419 | if ((*dst++ = *src++) == '\0') |
| 1420 | break; |
| 1421 | } |
| 1422 | return dst; |
| 1423 | } |
| 1424 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1425 | 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] | 1426 | { |
| 1427 | int i; |
| 1428 | unsigned count1; |
| 1429 | unsigned count2; |
| 1430 | char **v; |
| 1431 | |
| 1432 | v = strings; |
| 1433 | count1 = 0; |
| 1434 | if (v) { |
| 1435 | while (*v) { |
| 1436 | count1++; |
| 1437 | v++; |
| 1438 | } |
| 1439 | } |
| 1440 | count2 = 0; |
| 1441 | v = add; |
| 1442 | while (*v) { |
| 1443 | count2++; |
| 1444 | v++; |
| 1445 | } |
| 1446 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1447 | v[count1 + count2] = NULL; |
| 1448 | i = count2; |
| 1449 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1450 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1451 | return v; |
| 1452 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1453 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1454 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1455 | { |
| 1456 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1457 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1458 | return ptr; |
| 1459 | } |
| 1460 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1461 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1462 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1463 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1464 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1465 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1466 | { |
| 1467 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1468 | v[0] = add; |
| 1469 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1470 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1471 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1472 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1473 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1474 | { |
| 1475 | char **ptr = add_string_to_strings(strings, add); |
| 1476 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1477 | return ptr; |
| 1478 | } |
| 1479 | #define add_string_to_strings(strings, add) \ |
| 1480 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1481 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1482 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1483 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1484 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1485 | char **v; |
| 1486 | |
| 1487 | if (!strings) |
| 1488 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1489 | v = strings; |
| 1490 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1491 | free(*v); |
| 1492 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1493 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1494 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1497 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1498 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1499 | int newfd; |
| 1500 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1501 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1502 | if (newfd >= 0) { |
| 1503 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1504 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1505 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1506 | if (errno == EBUSY) |
| 1507 | goto repeat; |
| 1508 | if (errno == EINTR) |
| 1509 | goto repeat; |
| 1510 | } |
| 1511 | return newfd; |
| 1512 | } |
| 1513 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1514 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1515 | { |
| 1516 | int newfd; |
| 1517 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1518 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1519 | if (newfd < 0) { |
| 1520 | if (errno == EBUSY) |
| 1521 | goto repeat; |
| 1522 | if (errno == EINTR) |
| 1523 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1524 | /* fd was not open? */ |
| 1525 | if (errno == EBADF) |
| 1526 | return fd; |
| 1527 | xfunc_die(); |
| 1528 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1529 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1530 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1531 | close(fd); |
| 1532 | return newfd; |
| 1533 | } |
| 1534 | |
| 1535 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1536 | /* Manipulating the list of open FILEs */ |
| 1537 | static FILE *remember_FILE(FILE *fp) |
| 1538 | { |
| 1539 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1540 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1541 | n->next = G.FILE_list; |
| 1542 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1543 | n->fp = fp; |
| 1544 | n->fd = fileno(fp); |
| 1545 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1546 | } |
| 1547 | return fp; |
| 1548 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1549 | static void fclose_and_forget(FILE *fp) |
| 1550 | { |
| 1551 | struct FILE_list **pp = &G.FILE_list; |
| 1552 | while (*pp) { |
| 1553 | struct FILE_list *cur = *pp; |
| 1554 | if (cur->fp == fp) { |
| 1555 | *pp = cur->next; |
| 1556 | free(cur); |
| 1557 | break; |
| 1558 | } |
| 1559 | pp = &cur->next; |
| 1560 | } |
| 1561 | fclose(fp); |
| 1562 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1563 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1564 | { |
| 1565 | struct FILE_list *fl = G.FILE_list; |
| 1566 | while (fl) { |
| 1567 | if (fd == fl->fd) { |
| 1568 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1569 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1570 | debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1571 | return 1; |
| 1572 | } |
| 1573 | fl = fl->next; |
| 1574 | } |
| 1575 | return 0; |
| 1576 | } |
| 1577 | static void restore_redirected_FILEs(void) |
| 1578 | { |
| 1579 | struct FILE_list *fl = G.FILE_list; |
| 1580 | while (fl) { |
| 1581 | int should_be = fileno(fl->fp); |
| 1582 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1583 | debug_printf_redir("restoring script fd from %d to %d\n", fl->fd, should_be); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1584 | xmove_fd(fl->fd, should_be); |
| 1585 | fl->fd = should_be; |
| 1586 | } |
| 1587 | fl = fl->next; |
| 1588 | } |
| 1589 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1590 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1591 | static void close_all_FILE_list(void) |
| 1592 | { |
| 1593 | struct FILE_list *fl = G.FILE_list; |
| 1594 | while (fl) { |
| 1595 | /* fclose would also free FILE object. |
| 1596 | * It is disastrous if we share memory with a vforked parent. |
| 1597 | * I'm not sure we never come here after vfork. |
| 1598 | * Therefore just close fd, nothing more. |
| 1599 | */ |
| 1600 | /*fclose(fl->fp); - unsafe */ |
| 1601 | close(fl->fd); |
| 1602 | fl = fl->next; |
| 1603 | } |
| 1604 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1605 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1606 | static int fd_in_FILEs(int fd) |
| 1607 | { |
| 1608 | struct FILE_list *fl = G.FILE_list; |
| 1609 | while (fl) { |
| 1610 | if (fl->fd == fd) |
| 1611 | return 1; |
| 1612 | fl = fl->next; |
| 1613 | } |
| 1614 | return 0; |
| 1615 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1616 | |
| 1617 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1618 | /* Helpers for setting new $n and restoring them back |
| 1619 | */ |
| 1620 | typedef struct save_arg_t { |
| 1621 | char *sv_argv0; |
| 1622 | char **sv_g_argv; |
| 1623 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1624 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1625 | } save_arg_t; |
| 1626 | |
| 1627 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1628 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1629 | sv->sv_argv0 = argv[0]; |
| 1630 | sv->sv_g_argv = G.global_argv; |
| 1631 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1632 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1633 | |
| 1634 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1635 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1636 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1637 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1638 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1642 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1643 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1644 | if (G.global_args_malloced) { |
| 1645 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1646 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1647 | while (*++pp) /* note: does not free $0 */ |
| 1648 | free(*pp); |
| 1649 | free(G.global_argv); |
| 1650 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1651 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1652 | argv[0] = sv->sv_argv0; |
| 1653 | G.global_argv = sv->sv_g_argv; |
| 1654 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1655 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1659 | /* Basic theory of signal handling in shell |
| 1660 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1661 | * This does not describe what hush does, rather, it is current understanding |
| 1662 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1663 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1664 | * |
| 1665 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1666 | * is finished or backgrounded. It is the same in interactive and |
| 1667 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1668 | * 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] | 1669 | * ^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] | 1670 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1671 | * pipe. |
| 1672 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1673 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1674 | * or by SIGINT in interactive shell. |
| 1675 | * |
| 1676 | * Trap handlers will execute even within trap handlers. (right?) |
| 1677 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1678 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1679 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1680 | * |
| 1681 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1682 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1683 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1684 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1685 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1686 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1687 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1688 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1689 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1690 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1691 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1692 | * |
| 1693 | * SIGQUIT: ignore |
| 1694 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1695 | * SIGHUP (interactive): |
| 1696 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1697 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1698 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1699 | * that all pipe members are stopped. Try this in bash: |
| 1700 | * while :; do :; done - ^Z does not background it |
| 1701 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1702 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1703 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1704 | * to interactive shell while shell is waiting for a pipe, |
| 1705 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1706 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1707 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1708 | * Example 2: this does not wait and does not execute ls: |
| 1709 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1710 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1711 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1712 | * Example 4: this does not wait and does not execute ls: |
| 1713 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1714 | * |
| 1715 | * (What happens to signals which are IGN on shell start?) |
| 1716 | * (What happens with signal mask on shell start?) |
| 1717 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1718 | * Old implementation |
| 1719 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1720 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1721 | * We block all signals which we don't want to take action immediately, |
| 1722 | * i.e. we block all signals which need to have special handling as described |
| 1723 | * above, and all signals which have traps set. |
| 1724 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1725 | * and act on them. |
| 1726 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1727 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1728 | * sigset_t blocked_set: current blocked signal set |
| 1729 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1730 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1731 | * 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] | 1732 | * "trap 'cmd' SIGxxx": |
| 1733 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1734 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1735 | * unblock signals with special interactive handling |
| 1736 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1737 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1738 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1739 | * 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] | 1740 | * 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] | 1741 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1742 | * 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] | 1743 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1744 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1745 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1746 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1747 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1748 | * 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] | 1749 | * 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] | 1750 | * |
| 1751 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1752 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1753 | * masked signals are not visible! |
| 1754 | * |
| 1755 | * New implementation |
| 1756 | * ================== |
| 1757 | * We record each signal we are interested in by installing signal handler |
| 1758 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1759 | * We are interested in: signals which need to have special handling |
| 1760 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1761 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1762 | * After each pipe execution, we extract any pending signals |
| 1763 | * and act on them. |
| 1764 | * |
| 1765 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1766 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1767 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1768 | * sigset_t pending_set: set of sigs we received. |
| 1769 | * |
| 1770 | * "trap - SIGxxx": |
| 1771 | * if sig is in special_sig_mask, set handler back to: |
| 1772 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1773 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1774 | * else: set handler back to SIG_DFL |
| 1775 | * "trap 'cmd' SIGxxx": |
| 1776 | * set handler to record_pending_signo. |
| 1777 | * "trap '' SIGxxx": |
| 1778 | * set handler to SIG_IGN. |
| 1779 | * after [v]fork, if we plan to be a shell: |
| 1780 | * set signals with special interactive handling to SIG_DFL |
| 1781 | * (because child shell is not interactive), |
| 1782 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1783 | * after [v]fork, if we plan to exec: |
| 1784 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1785 | * |
| 1786 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1787 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1788 | * |
| 1789 | * Note (compat): |
| 1790 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1791 | * are set to the default actions". bash interprets it so that traps which |
| 1792 | * 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] | 1793 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1794 | enum { |
| 1795 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1796 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1797 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1798 | | (1 << SIGHUP) |
| 1799 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1800 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1801 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1802 | | (1 << SIGTTIN) |
| 1803 | | (1 << SIGTTOU) |
| 1804 | | (1 << SIGTSTP) |
| 1805 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1806 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1807 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1808 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1809 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1810 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1811 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1812 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1813 | if (sig == SIGCHLD) { |
| 1814 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1815 | //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] | 1816 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1817 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1818 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1819 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1820 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1821 | { |
| 1822 | struct sigaction old_sa; |
| 1823 | |
| 1824 | /* We could use signal() to install handlers... almost: |
| 1825 | * except that we need to mask ALL signals while handlers run. |
| 1826 | * I saw signal nesting in strace, race window isn't small. |
| 1827 | * SA_RESTART is also needed, but in Linux, signal() |
| 1828 | * sets SA_RESTART too. |
| 1829 | */ |
| 1830 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1831 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1832 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1833 | G.sa.sa_handler = handler; |
| 1834 | sigaction(sig, &G.sa, &old_sa); |
| 1835 | return old_sa.sa_handler; |
| 1836 | } |
| 1837 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1838 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1839 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1840 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1841 | static void restore_ttypgrp_and__exit(void) |
| 1842 | { |
| 1843 | /* xfunc has failed! die die die */ |
| 1844 | /* no EXIT traps, this is an escape hatch! */ |
| 1845 | G.exiting = 1; |
| 1846 | hush_exit(xfunc_error_retval); |
| 1847 | } |
| 1848 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1849 | #if ENABLE_HUSH_JOB |
| 1850 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1851 | /* Needed only on some libc: |
| 1852 | * It was observed that on exit(), fgetc'ed buffered data |
| 1853 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1854 | * With the net effect that even after fork(), not vfork(), |
| 1855 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1856 | * noexec_applet_here |
| 1857 | * echo END_OF_SCRIPT |
| 1858 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1859 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1860 | * Similar problems can be seen with msg_and_die_if_script() -> xfunc_die() |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1861 | * and in `cmd` handling. |
| 1862 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1863 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1864 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1865 | static void fflush_and__exit(void) |
| 1866 | { |
| 1867 | fflush_all(); |
| 1868 | _exit(xfunc_error_retval); |
| 1869 | } |
| 1870 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1871 | /* 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] | 1872 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1873 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1874 | # 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] | 1875 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1876 | /* Restores tty foreground process group, and exits. |
| 1877 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1878 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1879 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1880 | * We also call it if xfunc is exiting. |
| 1881 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1882 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1883 | static void sigexit(int sig) |
| 1884 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1885 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1886 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1887 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1888 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1889 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1890 | */ |
| 1891 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1892 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1893 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1894 | |
| 1895 | /* Not a signal, just exit */ |
| 1896 | if (sig <= 0) |
| 1897 | _exit(- sig); |
| 1898 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1899 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1900 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1901 | #else |
| 1902 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1903 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1904 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1905 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1906 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1907 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1908 | static sighandler_t pick_sighandler(unsigned sig) |
| 1909 | { |
| 1910 | sighandler_t handler = SIG_DFL; |
| 1911 | if (sig < sizeof(unsigned)*8) { |
| 1912 | unsigned sigmask = (1 << sig); |
| 1913 | |
| 1914 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1915 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1916 | if (G_fatal_sig_mask & sigmask) |
| 1917 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1918 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1919 | #endif |
| 1920 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1921 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1922 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1923 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1924 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1925 | * in an endless loop when we try to do some |
| 1926 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1927 | */ |
| 1928 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1929 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1930 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1931 | } |
| 1932 | return handler; |
| 1933 | } |
| 1934 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1935 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1936 | static void hush_exit(int exitcode) |
| 1937 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1938 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1939 | save_history(G.line_input_state); |
| 1940 | #endif |
| 1941 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1942 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1943 | 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] | 1944 | char *argv[3]; |
| 1945 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1946 | argv[1] = xstrdup(G_traps[0]); /* copy, since EXIT trap handler may modify G_traps[0] */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1947 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1948 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1949 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1950 | * "trap" will still show it, if executed |
| 1951 | * in the handler */ |
| 1952 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1953 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1954 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1955 | #if ENABLE_FEATURE_CLEAN_UP |
| 1956 | { |
| 1957 | struct variable *cur_var; |
| 1958 | if (G.cwd != bb_msg_unknown) |
| 1959 | free((char*)G.cwd); |
| 1960 | cur_var = G.top_var; |
| 1961 | while (cur_var) { |
| 1962 | struct variable *tmp = cur_var; |
| 1963 | if (!cur_var->max_len) |
| 1964 | free(cur_var->varstr); |
| 1965 | cur_var = cur_var->next; |
| 1966 | free(tmp); |
| 1967 | } |
| 1968 | } |
| 1969 | #endif |
| 1970 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1971 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1972 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1973 | sigexit(- (exitcode & 0xff)); |
| 1974 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1975 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1976 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1979 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1980 | //TODO: return a mask of ALL handled sigs? |
| 1981 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1982 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1983 | int last_sig = 0; |
| 1984 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1985 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1986 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1987 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1988 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1989 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1990 | sig = 0; |
| 1991 | do { |
| 1992 | sig++; |
| 1993 | if (sigismember(&G.pending_set, sig)) { |
| 1994 | sigdelset(&G.pending_set, sig); |
| 1995 | goto got_sig; |
| 1996 | } |
| 1997 | } while (sig < NSIG); |
| 1998 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1999 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2000 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2001 | debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2002 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2003 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2004 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2005 | char *argv[3]; |
| 2006 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2007 | argv[1] = xstrdup(G_traps[sig]); |
| 2008 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2009 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2010 | save_rcode = G.last_exitcode; |
| 2011 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2012 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2013 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2014 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2015 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2016 | } /* else: "" trap, ignoring signal */ |
| 2017 | continue; |
| 2018 | } |
| 2019 | /* not a trap: special action */ |
| 2020 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2021 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2022 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2023 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2024 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2025 | break; |
| 2026 | #if ENABLE_HUSH_JOB |
| 2027 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2028 | //TODO: why are we doing this? ash and dash don't do this, |
| 2029 | //they have no handler for SIGHUP at all, |
| 2030 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2031 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2032 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2033 | /* bash is observed to signal whole process groups, |
| 2034 | * not individual processes */ |
| 2035 | for (job = G.job_list; job; job = job->next) { |
| 2036 | if (job->pgrp <= 0) |
| 2037 | continue; |
| 2038 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2039 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2040 | kill(- job->pgrp, SIGCONT); |
| 2041 | } |
| 2042 | sigexit(SIGHUP); |
| 2043 | } |
| 2044 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2045 | #if ENABLE_HUSH_FAST |
| 2046 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2047 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2048 | G.count_SIGCHLD++; |
| 2049 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2050 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2051 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2052 | * This simplifies wait builtin a bit. |
| 2053 | */ |
| 2054 | break; |
| 2055 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2056 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2057 | 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] | 2058 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2059 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2060 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2061 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2062 | * in interactive shell, because TERM is ignored. |
| 2063 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | return last_sig; |
| 2068 | } |
| 2069 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2070 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2071 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2072 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2073 | if (force || G.cwd == NULL) { |
| 2074 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2075 | * we must not try to free(bb_msg_unknown) */ |
| 2076 | if (G.cwd == bb_msg_unknown) |
| 2077 | G.cwd = NULL; |
| 2078 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2079 | if (!G.cwd) |
| 2080 | G.cwd = bb_msg_unknown; |
| 2081 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2082 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2085 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2086 | /* |
| 2087 | * Shell and environment variable support |
| 2088 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2089 | 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] | 2090 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2091 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2092 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2093 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2094 | pp = &G.top_var; |
| 2095 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2096 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2097 | return pp; |
| 2098 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2099 | } |
| 2100 | return NULL; |
| 2101 | } |
| 2102 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2103 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2104 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2105 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2106 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2107 | |
| 2108 | if (G.expanded_assignments) { |
| 2109 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2110 | while (*cpp) { |
| 2111 | char *cp = *cpp; |
| 2112 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2113 | return cp + len + 1; |
| 2114 | cpp++; |
| 2115 | } |
| 2116 | } |
| 2117 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2118 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2119 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2120 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2121 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2122 | if (strcmp(name, "PPID") == 0) |
| 2123 | return utoa(G.root_ppid); |
| 2124 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2125 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2126 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2127 | return utoa(next_random(&G.random_gen)); |
| 2128 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2129 | return NULL; |
| 2130 | } |
| 2131 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2132 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2133 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2134 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2135 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2136 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2137 | cmdedit_update_prompt(); |
| 2138 | return; |
| 2139 | } |
| 2140 | |
| 2141 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2142 | && name_len == 6 |
| 2143 | ) { |
| 2144 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2145 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2146 | G.lineno_var = NULL; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2147 | return; |
| 2148 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2149 | #endif |
| 2150 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2151 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2152 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2153 | return; |
| 2154 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2155 | #endif |
| 2156 | } |
| 2157 | } |
| 2158 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2159 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2160 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2161 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2162 | #define SETFLAG_EXPORT (1 << 0) |
| 2163 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2164 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2165 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2166 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2167 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2168 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2169 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2170 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2171 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2172 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2173 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2174 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2175 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2176 | if (HUSH_DEBUG && !eq_sign) |
| 2177 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2178 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2179 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2180 | cur_pp = &G.top_var; |
| 2181 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2182 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2183 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2184 | continue; |
| 2185 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2186 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2187 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2188 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2189 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2190 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2191 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2192 | //but export per se succeeds (does put the var in env). We don't mimic that. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2193 | return -1; |
| 2194 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2195 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2196 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2197 | *eq_sign = '\0'; |
| 2198 | unsetenv(str); |
| 2199 | *eq_sign = '='; |
| 2200 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2201 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2202 | /* bash 3.2.33(1) and exported vars: |
| 2203 | * # export z=z |
| 2204 | * # f() { local z=a; env | grep ^z; } |
| 2205 | * # f |
| 2206 | * z=a |
| 2207 | * # env | grep ^z |
| 2208 | * z=z |
| 2209 | */ |
| 2210 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2211 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2212 | /* New variable is local ("local VAR=VAL" or |
| 2213 | * "VAR=VAL cmd") |
| 2214 | * and existing one is global, or local |
| 2215 | * on a lower level that new one. |
| 2216 | * Remove it from global variable list: |
| 2217 | */ |
| 2218 | *cur_pp = cur->next; |
| 2219 | if (G.shadowed_vars_pp) { |
| 2220 | /* Save in "shadowed" list */ |
| 2221 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2222 | cur->flg_export ? "exported " : "", |
| 2223 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2224 | ); |
| 2225 | cur->next = *G.shadowed_vars_pp; |
| 2226 | *G.shadowed_vars_pp = cur; |
| 2227 | } else { |
| 2228 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2229 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2230 | cur->flg_export ? "exported " : "", |
| 2231 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2232 | ); |
| 2233 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2234 | free_me = cur->varstr; /* then free it later */ |
| 2235 | free(cur); |
| 2236 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2237 | break; |
| 2238 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2239 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2240 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2241 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2242 | free_and_exp: |
| 2243 | free(str); |
| 2244 | goto exp; |
| 2245 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2246 | |
| 2247 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2248 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2249 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2250 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2251 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2252 | strcpy(cur->varstr, str); |
| 2253 | goto free_and_exp; |
| 2254 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2255 | /* Can't reuse */ |
| 2256 | cur->max_len = 0; |
| 2257 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2258 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2259 | /* max_len == 0 signifies "malloced" var, which we can |
| 2260 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2261 | * if cur->flg_export is 1, it is in the environment. |
| 2262 | * We should either unsetenv+free, or wait until putenv, |
| 2263 | * then putenv(new)+free(old). |
| 2264 | */ |
| 2265 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2266 | goto set_str_and_exp; |
| 2267 | } |
| 2268 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2269 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2270 | debug_printf_env("%s: alloc new var '%s'/%u\n", __func__, str, local_lvl); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2271 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2272 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2273 | cur->next = *cur_pp; |
| 2274 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2275 | |
| 2276 | set_str_and_exp: |
| 2277 | cur->varstr = str; |
| 2278 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2279 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2280 | if (flags & SETFLAG_MAKE_RO) { |
| 2281 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2282 | } |
| 2283 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2284 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2285 | cur->flg_export = 1; |
| 2286 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2287 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2288 | cur->flg_export = 0; |
| 2289 | /* unsetenv was already done */ |
| 2290 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2291 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2292 | debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2293 | i = putenv(cur->varstr); |
| 2294 | /* only now we can free old exported malloced string */ |
| 2295 | free(free_me); |
| 2296 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2297 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2298 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2299 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2300 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2301 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2302 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2303 | return 0; |
| 2304 | } |
| 2305 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2306 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2307 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2308 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2309 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2312 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2313 | { |
| 2314 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2315 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2316 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2317 | cur_pp = &G.top_var; |
| 2318 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2319 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2320 | && cur->varstr[name_len] == '=' |
| 2321 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2322 | if (cur->flg_read_only) { |
| 2323 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2324 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2325 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2326 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2327 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2328 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2329 | bb_unsetenv(cur->varstr); |
| 2330 | if (!cur->max_len) |
| 2331 | free(cur->varstr); |
| 2332 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2333 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2334 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2335 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2336 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2337 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2338 | |
| 2339 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2340 | handle_changed_special_names(name, name_len); |
| 2341 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2342 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2345 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2346 | static int unset_local_var(const char *name) |
| 2347 | { |
| 2348 | return unset_local_var_len(name, strlen(name)); |
| 2349 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2350 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2351 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2352 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2353 | 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] | 2354 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2355 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2356 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2357 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2358 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2359 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2360 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2361 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2362 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2363 | */ |
| 2364 | static void add_vars(struct variable *var) |
| 2365 | { |
| 2366 | struct variable *next; |
| 2367 | |
| 2368 | while (var) { |
| 2369 | next = var->next; |
| 2370 | var->next = G.top_var; |
| 2371 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2372 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2373 | debug_printf_env("%s: restoring exported '%s'/%u\n", __func__, var->varstr, var->var_nest_level); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2374 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2375 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2376 | debug_printf_env("%s: restoring variable '%s'/%u\n", __func__, var->varstr, var->var_nest_level); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2377 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2378 | var = next; |
| 2379 | } |
| 2380 | } |
| 2381 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2382 | /* We put strings[i] into variable table and possibly putenv them. |
| 2383 | * If variable is read only, we can free the strings[i] |
| 2384 | * which attempts to overwrite it. |
| 2385 | * The strings[] vector itself is freed. |
| 2386 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2387 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2388 | { |
| 2389 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2390 | |
| 2391 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2392 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2393 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2394 | s = strings; |
| 2395 | while (*s) { |
| 2396 | struct variable *var_p; |
| 2397 | struct variable **var_pp; |
| 2398 | char *eq; |
| 2399 | |
| 2400 | eq = strchr(*s, '='); |
| 2401 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2402 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2403 | if (var_pp) { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2404 | var_p = *var_pp; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2405 | if (var_p->flg_read_only) { |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2406 | char **p; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2407 | bb_error_msg("%s: readonly variable", *s); |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2408 | /* |
| 2409 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2410 | * If VAR is readonly, leaving it in the list |
| 2411 | * after asssignment error (msg above) |
| 2412 | * causes doubled error message later, on unset. |
| 2413 | */ |
| 2414 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2415 | free(*s); |
| 2416 | p = s; |
| 2417 | do { *p = p[1]; p++; } while (*p); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2418 | goto next; |
| 2419 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2420 | /* below, set_local_var() with nest level will |
| 2421 | * "shadow" (remove) this variable from |
| 2422 | * global linked list. |
| 2423 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2424 | } |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2425 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2426 | set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT); |
| 2427 | } else if (HUSH_DEBUG) { |
| 2428 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2429 | } |
| 2430 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2431 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2432 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2433 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | |
| 2437 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2438 | * Unicode helper |
| 2439 | */ |
| 2440 | static void reinit_unicode_for_hush(void) |
| 2441 | { |
| 2442 | /* Unicode support should be activated even if LANG is set |
| 2443 | * _during_ shell execution, not only if it was set when |
| 2444 | * shell was started. Therefore, re-check LANG every time: |
| 2445 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2446 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2447 | || ENABLE_UNICODE_USING_LOCALE |
| 2448 | ) { |
| 2449 | const char *s = get_local_var_value("LC_ALL"); |
| 2450 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2451 | if (!s) s = get_local_var_value("LANG"); |
| 2452 | reinit_unicode(s); |
| 2453 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2456 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2457 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2458 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2459 | |
| 2460 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2461 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2462 | * echo $P\ |
| 2463 | * \ |
| 2464 | * AT\ |
| 2465 | * H\ |
| 2466 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2467 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2468 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2469 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2470 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2471 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2472 | G.PS1 = get_local_var_value("PS1"); |
| 2473 | if (G.PS1 == NULL) |
| 2474 | G.PS1 = ""; |
| 2475 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2476 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2477 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2478 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2479 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2480 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2481 | { |
| 2482 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2483 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2484 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2485 | |
| 2486 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2487 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2488 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2489 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2490 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2491 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2492 | /* bash uses $PWD value, even if it is set by user. |
| 2493 | * It uses current dir only if PWD is unset. |
| 2494 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2495 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2496 | } |
| 2497 | prompt_str = G.PS1; |
| 2498 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2499 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2500 | return prompt_str; |
| 2501 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2502 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2503 | { |
| 2504 | int r; |
| 2505 | const char *prompt_str; |
| 2506 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2507 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2508 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2509 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2510 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2511 | if (G.flag_SIGINT) { |
| 2512 | /* There was ^C'ed, make it look prettier: */ |
| 2513 | bb_putchar('\n'); |
| 2514 | G.flag_SIGINT = 0; |
| 2515 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2516 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2517 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2518 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2519 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2520 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2521 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2522 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2523 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2524 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2525 | if (r != 0 && !G.flag_SIGINT) |
| 2526 | break; |
| 2527 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2528 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2529 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2530 | G.last_exitcode = 128 + SIGINT; |
| 2531 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2532 | if (r < 0) { |
| 2533 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2534 | i->p = NULL; |
| 2535 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2536 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2537 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2538 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2539 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2540 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2541 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2542 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2543 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2544 | /* Why check_and_run_traps here? Try this interactively: |
| 2545 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2546 | * $ <[enter], repeatedly...> |
| 2547 | * Without check_and_run_traps, handler never runs. |
| 2548 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2549 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2550 | fputs(prompt_str, stdout); |
| 2551 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2552 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2553 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2554 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2555 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2556 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2557 | * it generates SIGINT as usual. |
| 2558 | */ |
| 2559 | check_and_run_traps(); |
| 2560 | if (G.flag_SIGINT) |
| 2561 | G.last_exitcode = 128 + SIGINT; |
| 2562 | if (r != '\0') |
| 2563 | break; |
| 2564 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2565 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2566 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2567 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2568 | /* This is the magic location that prints prompts |
| 2569 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2570 | static int fgetc_interactive(struct in_str *i) |
| 2571 | { |
| 2572 | int ch; |
| 2573 | /* If it's interactive stdin, get new line. */ |
| 2574 | if (G_interactive_fd && i->file == stdin) { |
| 2575 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2576 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2577 | G.promptmode = 1; /* PS2 */ |
| 2578 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2579 | } else { |
| 2580 | /* Not stdin: script file, sourced file, etc */ |
| 2581 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2582 | } |
| 2583 | return ch; |
| 2584 | } |
| 2585 | #else |
| 2586 | static inline int fgetc_interactive(struct in_str *i) |
| 2587 | { |
| 2588 | int ch; |
| 2589 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2590 | return ch; |
| 2591 | } |
| 2592 | #endif /* INTERACTIVE */ |
| 2593 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2594 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2595 | { |
| 2596 | int ch; |
| 2597 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2598 | if (!i->file) { |
| 2599 | /* string-based in_str */ |
| 2600 | ch = (unsigned char)*i->p; |
| 2601 | if (ch != '\0') { |
| 2602 | i->p++; |
| 2603 | i->last_char = ch; |
| 2604 | return ch; |
| 2605 | } |
| 2606 | return EOF; |
| 2607 | } |
| 2608 | |
| 2609 | /* FILE-based in_str */ |
| 2610 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2611 | #if ENABLE_FEATURE_EDITING |
| 2612 | /* This can be stdin, check line editing char[] buffer */ |
| 2613 | if (i->p && *i->p != '\0') { |
| 2614 | ch = (unsigned char)*i->p++; |
| 2615 | goto out; |
| 2616 | } |
| 2617 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2618 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2619 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2620 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2621 | int ch2 = i->peek_buf[1]; |
| 2622 | i->peek_buf[0] = ch2; |
| 2623 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2624 | goto out; |
| 2625 | i->peek_buf[1] = 0; |
| 2626 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2627 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2628 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2629 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2630 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2631 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2632 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2633 | #if ENABLE_HUSH_LINENO_VAR |
| 2634 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2635 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2636 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2637 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2638 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2639 | return ch; |
| 2640 | } |
| 2641 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2642 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2643 | { |
| 2644 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2645 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2646 | if (!i->file) { |
| 2647 | /* string-based in_str */ |
| 2648 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2649 | return (unsigned char)*i->p; |
| 2650 | } |
| 2651 | |
| 2652 | /* FILE-based in_str */ |
| 2653 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2654 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2655 | /* This can be stdin, check line editing char[] buffer */ |
| 2656 | if (i->p && *i->p != '\0') |
| 2657 | return (unsigned char)*i->p; |
| 2658 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2659 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2660 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2661 | if (ch != 0) |
| 2662 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2663 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2664 | /* Need to get a new char */ |
| 2665 | ch = fgetc_interactive(i); |
| 2666 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2667 | |
| 2668 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2669 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2670 | if (i->p) { |
| 2671 | i->p -= 1; |
| 2672 | return ch; |
| 2673 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2674 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2675 | i->peek_buf[0] = ch; |
| 2676 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2677 | return ch; |
| 2678 | } |
| 2679 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2680 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2681 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2682 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2683 | */ |
| 2684 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2685 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2686 | int ch; |
| 2687 | |
| 2688 | /* There are two cases when i->p[] buffer exists. |
| 2689 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2690 | * (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] | 2691 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2692 | * the peek2 result is in i->p[1]. |
| 2693 | */ |
| 2694 | if (i->p) |
| 2695 | return (unsigned char)i->p[1]; |
| 2696 | |
| 2697 | /* Now we know it is a file-based in_str. */ |
| 2698 | |
| 2699 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2700 | /* Is there 2nd char? */ |
| 2701 | ch = i->peek_buf[1]; |
| 2702 | if (ch == 0) { |
| 2703 | /* We did not read it yet, get it now */ |
| 2704 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2705 | i->peek_buf[1] = ch; |
| 2706 | } |
| 2707 | |
| 2708 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2709 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2710 | } |
| 2711 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2712 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2713 | { |
| 2714 | for (;;) { |
| 2715 | int ch, ch2; |
| 2716 | |
| 2717 | ch = i_getch(input); |
| 2718 | if (ch != '\\') |
| 2719 | return ch; |
| 2720 | ch2 = i_peek(input); |
| 2721 | if (ch2 != '\n') |
| 2722 | return ch; |
| 2723 | /* backslash+newline, skip it */ |
| 2724 | i_getch(input); |
| 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2729 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2730 | */ |
| 2731 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2732 | { |
| 2733 | for (;;) { |
| 2734 | int ch, ch2; |
| 2735 | |
| 2736 | ch = i_peek(input); |
| 2737 | if (ch != '\\') |
| 2738 | return ch; |
| 2739 | ch2 = i_peek2(input); |
| 2740 | if (ch2 != '\n') |
| 2741 | return ch; |
| 2742 | /* backslash+newline, skip it */ |
| 2743 | i_getch(input); |
| 2744 | i_getch(input); |
| 2745 | } |
| 2746 | } |
| 2747 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2748 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2749 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2750 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2751 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2752 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2756 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2757 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2758 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2759 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2763 | /* |
| 2764 | * o_string support |
| 2765 | */ |
| 2766 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2767 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2768 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2769 | { |
| 2770 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2771 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2772 | if (o->data) |
| 2773 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2776 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2777 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2778 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2779 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2780 | } |
| 2781 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2782 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2783 | { |
| 2784 | free(o->data); |
| 2785 | } |
| 2786 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2787 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2788 | { |
| 2789 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2790 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2791 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2792 | } |
| 2793 | } |
| 2794 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2795 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2796 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2797 | 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] | 2798 | if (o->length < o->maxlen) { |
| 2799 | /* likely. avoid o_grow_by() call */ |
| 2800 | add: |
| 2801 | o->data[o->length] = ch; |
| 2802 | o->length++; |
| 2803 | o->data[o->length] = '\0'; |
| 2804 | return; |
| 2805 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2806 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2807 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2810 | #if 0 |
| 2811 | /* Valid only if we know o_string is not empty */ |
| 2812 | static void o_delchr(o_string *o) |
| 2813 | { |
| 2814 | o->length--; |
| 2815 | o->data[o->length] = '\0'; |
| 2816 | } |
| 2817 | #endif |
| 2818 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2819 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2820 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2821 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2822 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2823 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2826 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2827 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2828 | o_addblock(o, str, strlen(str)); |
| 2829 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2830 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2831 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2832 | static void nommu_addchr(o_string *o, int ch) |
| 2833 | { |
| 2834 | if (o) |
| 2835 | o_addchr(o, ch); |
| 2836 | } |
| 2837 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2838 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2839 | #endif |
| 2840 | |
| 2841 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2842 | { |
| 2843 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2846 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2847 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2848 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2849 | * Apparently, on unquoted $v bash still does globbing |
| 2850 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2851 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2852 | * quoting mechanisms on $v expansion side: one protects |
| 2853 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2854 | * We have only second one. |
| 2855 | */ |
| 2856 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2857 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2858 | # define MAYBE_BRACES "{}" |
| 2859 | #else |
| 2860 | # define MAYBE_BRACES "" |
| 2861 | #endif |
| 2862 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2863 | /* My analysis of quoting semantics tells me that state information |
| 2864 | * is associated with a destination, not a source. |
| 2865 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2866 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2867 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2868 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2869 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2870 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2871 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2872 | o_grow_by(o, sz); |
| 2873 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2874 | o->data[o->length] = '\\'; |
| 2875 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2876 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2877 | o->data[o->length] = ch; |
| 2878 | o->length++; |
| 2879 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2882 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2883 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2884 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2885 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2886 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2887 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2888 | sz++; |
| 2889 | o->data[o->length] = '\\'; |
| 2890 | o->length++; |
| 2891 | } |
| 2892 | o_grow_by(o, sz); |
| 2893 | o->data[o->length] = ch; |
| 2894 | o->length++; |
| 2895 | o->data[o->length] = '\0'; |
| 2896 | } |
| 2897 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2898 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2899 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2900 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2901 | char ch; |
| 2902 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2903 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2904 | if (ordinary_cnt > len) /* paranoia */ |
| 2905 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2906 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2907 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2908 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2909 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2910 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2911 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2912 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2913 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2914 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2915 | sz++; |
| 2916 | o->data[o->length] = '\\'; |
| 2917 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2918 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2919 | o_grow_by(o, sz); |
| 2920 | o->data[o->length] = ch; |
| 2921 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2922 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2923 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2926 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2927 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2928 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2929 | o_addblock(o, str, len); |
| 2930 | return; |
| 2931 | } |
| 2932 | o_addqblock(o, str, len); |
| 2933 | } |
| 2934 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2935 | static void o_addQstr(o_string *o, const char *str) |
| 2936 | { |
| 2937 | o_addQblock(o, str, strlen(str)); |
| 2938 | } |
| 2939 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2940 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2941 | * 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] | 2942 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2943 | * list[i] contains an INDEX (int!) into this string data. |
| 2944 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2945 | * but list[i]'s need not be modified. |
| 2946 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2947 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2948 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2949 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2950 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2951 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2952 | { |
| 2953 | char **list = (char**)o->data; |
| 2954 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2955 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2956 | |
| 2957 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2958 | 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] | 2959 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2960 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2961 | o->has_quoted_part, |
| 2962 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2963 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2964 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2965 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2966 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2967 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2968 | i++; |
| 2969 | } |
| 2970 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2971 | 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] | 2972 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2973 | 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] | 2974 | } |
| 2975 | } |
| 2976 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2977 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2978 | #endif |
| 2979 | |
| 2980 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2981 | * in list[n] so that it points past last stored byte so far. |
| 2982 | * It returns n+1. */ |
| 2983 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2984 | { |
| 2985 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2986 | int string_start; |
| 2987 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2988 | |
| 2989 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2990 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2991 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2992 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2993 | 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] | 2994 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2995 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2996 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2997 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2998 | memmove(list + n + 0x10, list + n, string_len); |
| 2999 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3000 | } else { |
| 3001 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3002 | n, string_len, string_start); |
| 3003 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3004 | } else { |
| 3005 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3006 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3007 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3008 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3009 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3010 | o->has_empty_slot = 0; |
| 3011 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3012 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3013 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3014 | return n + 1; |
| 3015 | } |
| 3016 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3017 | /* "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] | 3018 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3019 | { |
| 3020 | char **list = (char**)o->data; |
| 3021 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3022 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3023 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3026 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3027 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3028 | * first, it processes even {a} (no commas), second, |
| 3029 | * 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] | 3030 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3031 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3032 | |
| 3033 | /* Helper */ |
| 3034 | static int glob_needed(const char *s) |
| 3035 | { |
| 3036 | while (*s) { |
| 3037 | if (*s == '\\') { |
| 3038 | if (!s[1]) |
| 3039 | return 0; |
| 3040 | s += 2; |
| 3041 | continue; |
| 3042 | } |
| 3043 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3044 | return 1; |
| 3045 | s++; |
| 3046 | } |
| 3047 | return 0; |
| 3048 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3049 | /* Return pointer to next closing brace or to comma */ |
| 3050 | static const char *next_brace_sub(const char *cp) |
| 3051 | { |
| 3052 | unsigned depth = 0; |
| 3053 | cp++; |
| 3054 | while (*cp != '\0') { |
| 3055 | if (*cp == '\\') { |
| 3056 | if (*++cp == '\0') |
| 3057 | break; |
| 3058 | cp++; |
| 3059 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3060 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3061 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3062 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3063 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3064 | depth++; |
| 3065 | } |
| 3066 | |
| 3067 | return *cp != '\0' ? cp : NULL; |
| 3068 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3069 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3070 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3071 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3072 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3073 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3074 | const char *next; |
| 3075 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3076 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3077 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3078 | |
| 3079 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3080 | |
| 3081 | begin = pattern; |
| 3082 | while (1) { |
| 3083 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3084 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3085 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3086 | /* Find the first sub-pattern and at the same time |
| 3087 | * find the rest after the closing brace */ |
| 3088 | next = next_brace_sub(begin); |
| 3089 | if (next == NULL) { |
| 3090 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3091 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3092 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3093 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3094 | /* "{abc}" with no commas - illegal |
| 3095 | * brace expr, disregard and skip it */ |
| 3096 | begin = next + 1; |
| 3097 | continue; |
| 3098 | } |
| 3099 | break; |
| 3100 | } |
| 3101 | if (*begin == '\\' && begin[1] != '\0') |
| 3102 | begin++; |
| 3103 | begin++; |
| 3104 | } |
| 3105 | debug_printf_glob("begin:%s\n", begin); |
| 3106 | debug_printf_glob("next:%s\n", next); |
| 3107 | |
| 3108 | /* Now find the end of the whole brace expression */ |
| 3109 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3110 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3111 | rest = next_brace_sub(rest); |
| 3112 | if (rest == NULL) { |
| 3113 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3114 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3115 | } |
| 3116 | debug_printf_glob("rest:%s\n", rest); |
| 3117 | } |
| 3118 | rest_len = strlen(++rest) + 1; |
| 3119 | |
| 3120 | /* We are sure the brace expression is well-formed */ |
| 3121 | |
| 3122 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3123 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3124 | |
| 3125 | /* We have a brace expression. BEGIN points to the opening {, |
| 3126 | * NEXT points past the terminator of the first element, and REST |
| 3127 | * points past the final }. We will accumulate result names from |
| 3128 | * recursive runs for each brace alternative in the buffer using |
| 3129 | * GLOB_APPEND. */ |
| 3130 | |
| 3131 | p = begin + 1; |
| 3132 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3133 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3134 | memcpy( |
| 3135 | mempcpy( |
| 3136 | mempcpy(new_pattern_buf, |
| 3137 | /* We know the prefix for all sub-patterns */ |
| 3138 | pattern, begin - pattern), |
| 3139 | p, next - p), |
| 3140 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3141 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3142 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3143 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3144 | */ |
| 3145 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3146 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3147 | /* We saw the last entry */ |
| 3148 | break; |
| 3149 | } |
| 3150 | p = next + 1; |
| 3151 | next = next_brace_sub(next); |
| 3152 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3153 | free(new_pattern_buf); |
| 3154 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3155 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3156 | simple_glob: |
| 3157 | { |
| 3158 | int gr; |
| 3159 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3160 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3161 | memset(&globdata, 0, sizeof(globdata)); |
| 3162 | gr = glob(pattern, 0, NULL, &globdata); |
| 3163 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3164 | if (gr != 0) { |
| 3165 | if (gr == GLOB_NOMATCH) { |
| 3166 | globfree(&globdata); |
| 3167 | /* NB: garbles parameter */ |
| 3168 | unbackslash(pattern); |
| 3169 | o_addstr_with_NUL(o, pattern); |
| 3170 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3171 | return o_save_ptr_helper(o, n); |
| 3172 | } |
| 3173 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3174 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3175 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3176 | * but we didn't specify it. Paranoia again. */ |
| 3177 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3178 | } |
| 3179 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3180 | char **argv = globdata.gl_pathv; |
| 3181 | while (1) { |
| 3182 | o_addstr_with_NUL(o, *argv); |
| 3183 | n = o_save_ptr_helper(o, n); |
| 3184 | argv++; |
| 3185 | if (!*argv) |
| 3186 | break; |
| 3187 | } |
| 3188 | } |
| 3189 | globfree(&globdata); |
| 3190 | } |
| 3191 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3192 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3193 | /* Performs globbing on last list[], |
| 3194 | * saving each result as a new list[]. |
| 3195 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3196 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3197 | { |
| 3198 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3199 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3200 | 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] | 3201 | if (!o->data) |
| 3202 | return o_save_ptr_helper(o, n); |
| 3203 | pattern = o->data + o_get_last_ptr(o, n); |
| 3204 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3205 | if (!glob_needed(pattern)) { |
| 3206 | /* unbackslash last string in o in place, fix length */ |
| 3207 | o->length = unbackslash(pattern) - o->data; |
| 3208 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3209 | return o_save_ptr_helper(o, n); |
| 3210 | } |
| 3211 | |
| 3212 | copy = xstrdup(pattern); |
| 3213 | /* "forget" pattern in o */ |
| 3214 | o->length = pattern - o->data; |
| 3215 | n = glob_brace(copy, o, n); |
| 3216 | free(copy); |
| 3217 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3218 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3219 | return n; |
| 3220 | } |
| 3221 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3222 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3223 | |
| 3224 | /* Helper */ |
| 3225 | static int glob_needed(const char *s) |
| 3226 | { |
| 3227 | while (*s) { |
| 3228 | if (*s == '\\') { |
| 3229 | if (!s[1]) |
| 3230 | return 0; |
| 3231 | s += 2; |
| 3232 | continue; |
| 3233 | } |
| 3234 | if (*s == '*' || *s == '[' || *s == '?') |
| 3235 | return 1; |
| 3236 | s++; |
| 3237 | } |
| 3238 | return 0; |
| 3239 | } |
| 3240 | /* Performs globbing on last list[], |
| 3241 | * saving each result as a new list[]. |
| 3242 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3243 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3244 | { |
| 3245 | glob_t globdata; |
| 3246 | int gr; |
| 3247 | char *pattern; |
| 3248 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3249 | 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] | 3250 | if (!o->data) |
| 3251 | return o_save_ptr_helper(o, n); |
| 3252 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3253 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3254 | if (!glob_needed(pattern)) { |
| 3255 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3256 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3257 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3258 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3259 | return o_save_ptr_helper(o, n); |
| 3260 | } |
| 3261 | |
| 3262 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3263 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3264 | * If we glob "*.\*" and don't find anything, we need |
| 3265 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3266 | * will return "*.\*"! |
| 3267 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3268 | gr = glob(pattern, 0, NULL, &globdata); |
| 3269 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3270 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3271 | if (gr == GLOB_NOMATCH) { |
| 3272 | globfree(&globdata); |
| 3273 | goto literal; |
| 3274 | } |
| 3275 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3276 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3277 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3278 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3279 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3280 | } |
| 3281 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3282 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3283 | /* "forget" pattern in o */ |
| 3284 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3285 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3286 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3287 | n = o_save_ptr_helper(o, n); |
| 3288 | argv++; |
| 3289 | if (!*argv) |
| 3290 | break; |
| 3291 | } |
| 3292 | } |
| 3293 | globfree(&globdata); |
| 3294 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3295 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3296 | return n; |
| 3297 | } |
| 3298 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3299 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3300 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3301 | /* 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] | 3302 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3303 | static int o_save_ptr(o_string *o, int n) |
| 3304 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3305 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3306 | /* If o->has_empty_slot, list[n] was already globbed |
| 3307 | * (if it was requested back then when it was filled) |
| 3308 | * so don't do that again! */ |
| 3309 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3310 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3311 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3312 | return o_save_ptr_helper(o, n); |
| 3313 | } |
| 3314 | |
| 3315 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3316 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3317 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3318 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3319 | int string_start; |
| 3320 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3321 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3322 | if (DEBUG_EXPAND) |
| 3323 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3324 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3325 | list = (char**)o->data; |
| 3326 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3327 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3328 | while (n) { |
| 3329 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3330 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3331 | } |
| 3332 | return list; |
| 3333 | } |
| 3334 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3335 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3336 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3337 | /* Returns pi->next - next pipe in the list */ |
| 3338 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3339 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3340 | struct pipe *next; |
| 3341 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3342 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3343 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3344 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3345 | struct command *command; |
| 3346 | struct redir_struct *r, *rnext; |
| 3347 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3348 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3349 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3350 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3351 | if (DEBUG_CLEAN) { |
| 3352 | int a; |
| 3353 | char **p; |
| 3354 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3355 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3356 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3357 | } |
| 3358 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3359 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3360 | } |
| 3361 | /* not "else if": on syntax error, we may have both! */ |
| 3362 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3363 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3364 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3365 | free_pipe_list(command->group); |
| 3366 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3367 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3368 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3369 | /* else is crucial here. |
| 3370 | * If group != NULL, child_func is meaningless */ |
| 3371 | #if ENABLE_HUSH_FUNCTIONS |
| 3372 | else if (command->child_func) { |
| 3373 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3374 | command->child_func->parent_cmd = NULL; |
| 3375 | } |
| 3376 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3377 | #if !BB_MMU |
| 3378 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3379 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3380 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3381 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3382 | debug_printf_clean(" redirect %d%s", |
| 3383 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3384 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3385 | if (r->rd_filename) { |
| 3386 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3387 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3388 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3389 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3390 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3391 | rnext = r->next; |
| 3392 | free(r); |
| 3393 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3394 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3395 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3396 | 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] | 3397 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3398 | #if ENABLE_HUSH_JOB |
| 3399 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3400 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3401 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3402 | |
| 3403 | next = pi->next; |
| 3404 | free(pi); |
| 3405 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3406 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3407 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3408 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3409 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3410 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3411 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3412 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3413 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3414 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3415 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3416 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3420 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3421 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3422 | #ifndef debug_print_tree |
| 3423 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3424 | { |
| 3425 | static const char *const PIPE[] = { |
| 3426 | [PIPE_SEQ] = "SEQ", |
| 3427 | [PIPE_AND] = "AND", |
| 3428 | [PIPE_OR ] = "OR" , |
| 3429 | [PIPE_BG ] = "BG" , |
| 3430 | }; |
| 3431 | static const char *RES[] = { |
| 3432 | [RES_NONE ] = "NONE" , |
| 3433 | # if ENABLE_HUSH_IF |
| 3434 | [RES_IF ] = "IF" , |
| 3435 | [RES_THEN ] = "THEN" , |
| 3436 | [RES_ELIF ] = "ELIF" , |
| 3437 | [RES_ELSE ] = "ELSE" , |
| 3438 | [RES_FI ] = "FI" , |
| 3439 | # endif |
| 3440 | # if ENABLE_HUSH_LOOPS |
| 3441 | [RES_FOR ] = "FOR" , |
| 3442 | [RES_WHILE] = "WHILE", |
| 3443 | [RES_UNTIL] = "UNTIL", |
| 3444 | [RES_DO ] = "DO" , |
| 3445 | [RES_DONE ] = "DONE" , |
| 3446 | # endif |
| 3447 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3448 | [RES_IN ] = "IN" , |
| 3449 | # endif |
| 3450 | # if ENABLE_HUSH_CASE |
| 3451 | [RES_CASE ] = "CASE" , |
| 3452 | [RES_CASE_IN ] = "CASE_IN" , |
| 3453 | [RES_MATCH] = "MATCH", |
| 3454 | [RES_CASE_BODY] = "CASE_BODY", |
| 3455 | [RES_ESAC ] = "ESAC" , |
| 3456 | # endif |
| 3457 | [RES_XXXX ] = "XXXX" , |
| 3458 | [RES_SNTX ] = "SNTX" , |
| 3459 | }; |
| 3460 | static const char *const CMDTYPE[] = { |
| 3461 | "{}", |
| 3462 | "()", |
| 3463 | "[noglob]", |
| 3464 | # if ENABLE_HUSH_FUNCTIONS |
| 3465 | "func()", |
| 3466 | # endif |
| 3467 | }; |
| 3468 | |
| 3469 | int pin, prn; |
| 3470 | |
| 3471 | pin = 0; |
| 3472 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3473 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3474 | lvl*2, "", |
| 3475 | pin, |
| 3476 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3477 | RES[pi->res_word], |
| 3478 | pi->followup, PIPE[pi->followup] |
| 3479 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3480 | prn = 0; |
| 3481 | while (prn < pi->num_cmds) { |
| 3482 | struct command *command = &pi->cmds[prn]; |
| 3483 | char **argv = command->argv; |
| 3484 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3485 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3486 | lvl*2, "", prn, |
| 3487 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3488 | #if ENABLE_HUSH_LINENO_VAR |
| 3489 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3490 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3491 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3492 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3493 | CMDTYPE[command->cmd_type], |
| 3494 | argv |
| 3495 | # if !BB_MMU |
| 3496 | , " group_as_string:", command->group_as_string |
| 3497 | # else |
| 3498 | , "", "" |
| 3499 | # endif |
| 3500 | ); |
| 3501 | debug_print_tree(command->group, lvl+1); |
| 3502 | prn++; |
| 3503 | continue; |
| 3504 | } |
| 3505 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3506 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3507 | argv++; |
| 3508 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3509 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3510 | prn++; |
| 3511 | } |
| 3512 | pi = pi->next; |
| 3513 | pin++; |
| 3514 | } |
| 3515 | } |
| 3516 | #endif /* debug_print_tree */ |
| 3517 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3518 | static struct pipe *new_pipe(void) |
| 3519 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3520 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3521 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3522 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3523 | return pi; |
| 3524 | } |
| 3525 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3526 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3527 | * if ctx->command is NULL. |
| 3528 | * No errors possible here. |
| 3529 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3530 | static int done_command(struct parse_context *ctx) |
| 3531 | { |
| 3532 | /* The command is really already in the pipe structure, so |
| 3533 | * advance the pipe counter and make a new, null command. */ |
| 3534 | struct pipe *pi = ctx->pipe; |
| 3535 | struct command *command = ctx->command; |
| 3536 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3537 | #if 0 /* Instead we emit error message at run time */ |
| 3538 | if (ctx->pending_redirect) { |
| 3539 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3540 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3541 | ctx->pending_redirect = NULL; |
| 3542 | } |
| 3543 | #endif |
| 3544 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3545 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3546 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3547 | 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] | 3548 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3549 | } |
| 3550 | pi->num_cmds++; |
| 3551 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3552 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3553 | } else { |
| 3554 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3555 | } |
| 3556 | |
| 3557 | /* Only real trickiness here is that the uncommitted |
| 3558 | * command structure is not counted in pi->num_cmds. */ |
| 3559 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3560 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3561 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3562 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3563 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3564 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3565 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3566 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3567 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3568 | } |
| 3569 | |
| 3570 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3571 | { |
| 3572 | int not_null; |
| 3573 | |
| 3574 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3575 | /* Close previous command */ |
| 3576 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3577 | #if HAS_KEYWORDS |
| 3578 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3579 | ctx->ctx_inverted = 0; |
| 3580 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3581 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3582 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3583 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3584 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3585 | * in a backgrounded subshell. |
| 3586 | */ |
| 3587 | struct pipe *pi; |
| 3588 | struct command *command; |
| 3589 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3590 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3591 | pi = ctx->list_head; |
| 3592 | while (pi != ctx->pipe) { |
| 3593 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3594 | goto no_conv; |
| 3595 | pi = pi->next; |
| 3596 | } |
| 3597 | |
| 3598 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3599 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3600 | pi = xzalloc(sizeof(*pi)); |
| 3601 | pi->followup = PIPE_BG; |
| 3602 | pi->num_cmds = 1; |
| 3603 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3604 | command = &pi->cmds[0]; |
| 3605 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3606 | command->cmd_type = CMD_NORMAL; |
| 3607 | command->group = ctx->list_head; |
| 3608 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3609 | command->group_as_string = xstrndup( |
| 3610 | ctx->as_string.data, |
| 3611 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3612 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3613 | #endif |
| 3614 | /* Replace all pipes in ctx with one newly created */ |
| 3615 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3616 | } else { |
| 3617 | no_conv: |
| 3618 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3619 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3620 | |
| 3621 | /* Without this check, even just <enter> on command line generates |
| 3622 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3623 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3624 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3625 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3626 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3627 | #endif |
| 3628 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3629 | || ctx->ctx_res_w == RES_DONE |
| 3630 | || ctx->ctx_res_w == RES_FOR |
| 3631 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3632 | #endif |
| 3633 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3634 | || ctx->ctx_res_w == RES_ESAC |
| 3635 | #endif |
| 3636 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3637 | struct pipe *new_p; |
| 3638 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3639 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3640 | not_null, ctx->ctx_res_w); |
| 3641 | new_p = new_pipe(); |
| 3642 | ctx->pipe->next = new_p; |
| 3643 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3644 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3645 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3646 | * This is used to control execution. |
| 3647 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3648 | * cases where variable or value happens to match a keyword): |
| 3649 | */ |
| 3650 | #if ENABLE_HUSH_LOOPS |
| 3651 | if (ctx->ctx_res_w == RES_FOR |
| 3652 | || ctx->ctx_res_w == RES_IN) |
| 3653 | ctx->ctx_res_w = RES_NONE; |
| 3654 | #endif |
| 3655 | #if ENABLE_HUSH_CASE |
| 3656 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3657 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3658 | if (ctx->ctx_res_w == RES_CASE) |
| 3659 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3660 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3661 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3662 | /* Create the memory for command, roughly: |
| 3663 | * ctx->pipe->cmds = new struct command; |
| 3664 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3665 | */ |
| 3666 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3667 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3668 | } |
| 3669 | debug_printf_parse("done_pipe return\n"); |
| 3670 | } |
| 3671 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3672 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3673 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3674 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3675 | if (MAYBE_ASSIGNMENT != 0) |
| 3676 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3677 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3678 | /* Create the memory for command, roughly: |
| 3679 | * ctx->pipe->cmds = new struct command; |
| 3680 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3681 | */ |
| 3682 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3685 | /* If a reserved word is found and processed, parse context is modified |
| 3686 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3687 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3688 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3689 | struct reserved_combo { |
| 3690 | char literal[6]; |
| 3691 | unsigned char res; |
| 3692 | unsigned char assignment_flag; |
| 3693 | int flag; |
| 3694 | }; |
| 3695 | enum { |
| 3696 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3697 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3698 | FLAG_IF = (1 << RES_IF ), |
| 3699 | FLAG_THEN = (1 << RES_THEN ), |
| 3700 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3701 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3702 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3703 | # endif |
| 3704 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3705 | FLAG_FOR = (1 << RES_FOR ), |
| 3706 | FLAG_WHILE = (1 << RES_WHILE), |
| 3707 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3708 | FLAG_DO = (1 << RES_DO ), |
| 3709 | FLAG_DONE = (1 << RES_DONE ), |
| 3710 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3711 | # endif |
| 3712 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3713 | FLAG_MATCH = (1 << RES_MATCH), |
| 3714 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3715 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3716 | FLAG_START = (1 << RES_XXXX ), |
| 3717 | }; |
| 3718 | |
| 3719 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3720 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3721 | /* Mostly a list of accepted follow-up reserved words. |
| 3722 | * FLAG_END means we are done with the sequence, and are ready |
| 3723 | * to turn the compound list into a command. |
| 3724 | * FLAG_START means the word must start a new compound list. |
| 3725 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3726 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3727 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3728 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3729 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3730 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3731 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3732 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3733 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3734 | # endif |
| 3735 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3736 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3737 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3738 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3739 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3740 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3741 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3742 | # endif |
| 3743 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3744 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3745 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3746 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3747 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3748 | const struct reserved_combo *r; |
| 3749 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3750 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3751 | if (strcmp(word->data, r->literal) == 0) |
| 3752 | return r; |
| 3753 | } |
| 3754 | return NULL; |
| 3755 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3756 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3757 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3758 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3759 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3760 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3761 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3762 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3763 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3764 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3765 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3766 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3767 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3768 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3769 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3770 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3771 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3772 | |
| 3773 | 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] | 3774 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3775 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3776 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3777 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3778 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3779 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3780 | if (r->flag == 0) { /* '!' */ |
| 3781 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3782 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3783 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3784 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3785 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3786 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3787 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3788 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3789 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3790 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3791 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3792 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3793 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3794 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3795 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3796 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3797 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3798 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3799 | } else { |
| 3800 | /* "{...} fi" is ok. "{...} if" is not |
| 3801 | * Example: |
| 3802 | * if { echo foo; } then { echo bar; } fi */ |
| 3803 | if (ctx->command->group) |
| 3804 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3805 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3806 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3807 | ctx->ctx_res_w = r->res; |
| 3808 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3809 | ctx->is_assignment = r->assignment_flag; |
| 3810 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3811 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3812 | if (ctx->old_flag & FLAG_END) { |
| 3813 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3814 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3815 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3816 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3817 | old = ctx->stack; |
| 3818 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3819 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3820 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3821 | /* At this point, the compound command's string is in |
| 3822 | * ctx->as_string... except for the leading keyword! |
| 3823 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3824 | * ctx->as_string will contain "true; then echo a; fi", |
| 3825 | * with "if " remaining in old->as_string! |
| 3826 | */ |
| 3827 | { |
| 3828 | char *str; |
| 3829 | int len = old->as_string.length; |
| 3830 | /* Concatenate halves */ |
| 3831 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3832 | o_free_unsafe(&ctx->as_string); |
| 3833 | /* Find where leading keyword starts in first half */ |
| 3834 | str = old->as_string.data + len; |
| 3835 | if (str > old->as_string.data) |
| 3836 | str--; /* skip whitespace after keyword */ |
| 3837 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3838 | str--; |
| 3839 | /* Ugh, we're done with this horrid hack */ |
| 3840 | old->command->group_as_string = xstrdup(str); |
| 3841 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3842 | old->command->group_as_string); |
| 3843 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3844 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3845 | *ctx = *old; /* physical copy */ |
| 3846 | free(old); |
| 3847 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3848 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3849 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3850 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3851 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3852 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3853 | * Normal return is 0. Syntax errors return 1. |
| 3854 | * Note: on return, word is reset, but not o_free'd! |
| 3855 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3856 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3857 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3858 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3859 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3860 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 3861 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3862 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3863 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3864 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3865 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3866 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3867 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3868 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3869 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3870 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3871 | * If the redirection operator is "<<" or "<<-", the word |
| 3872 | * that follows the redirection operator shall be |
| 3873 | * subjected to quote removal; it is unspecified whether |
| 3874 | * any of the other expansions occur. For the other |
| 3875 | * redirection operators, the word that follows the |
| 3876 | * redirection operator shall be subjected to tilde |
| 3877 | * expansion, parameter expansion, command substitution, |
| 3878 | * arithmetic expansion, and quote removal. |
| 3879 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3880 | * on the word by a non-interactive shell; an interactive |
| 3881 | * shell may perform it, but shall do so only when |
| 3882 | * the expansion would result in one word." |
| 3883 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3884 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3885 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3886 | // as written: |
| 3887 | // <<EOF$t |
| 3888 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3889 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3890 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3891 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3892 | /* Cater for >\file case: |
| 3893 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3894 | * Same with heredocs: |
| 3895 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3896 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3897 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3898 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3899 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3900 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3901 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3902 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3903 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3904 | debug_printf_parse("word stored in rd_filename: '%s'\n", ctx->word.data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3905 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3906 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3907 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3908 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3909 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3910 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3911 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3912 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3913 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3914 | ctx->ctx_dsemicolon = 0; |
| 3915 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3916 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3917 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3918 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3919 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3920 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3921 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3922 | # if ENABLE_HUSH_CASE |
| 3923 | && ctx->ctx_res_w != RES_CASE |
| 3924 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3925 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3926 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3927 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3928 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3929 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3930 | # if ENABLE_HUSH_LINENO_VAR |
| 3931 | /* Case: |
| 3932 | * "while ...; do |
| 3933 | * cmd ..." |
| 3934 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3935 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3936 | */ |
| 3937 | if (0 |
| 3938 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3939 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3940 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3941 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3942 | ) { |
| 3943 | done_pipe(ctx, PIPE_SEQ); |
| 3944 | } |
| 3945 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3946 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3947 | debug_printf_parse("done_word return %d\n", |
| 3948 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3949 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3950 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3951 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 3952 | if (0 |
| 3953 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3954 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3955 | # endif |
| 3956 | /* In bash, local/export/readonly are special, args |
| 3957 | * are assignments and therefore expansion of them |
| 3958 | * should be "one-word" expansion: |
| 3959 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 3960 | * compare with: |
| 3961 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 3962 | * ls: cannot access i=a: No such file or directory |
| 3963 | * ls: cannot access b: No such file or directory |
| 3964 | * Note: bash 3.2.33(1) does this only if export word |
| 3965 | * itself is not quoted: |
| 3966 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 3967 | * aaa bbb |
| 3968 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 3969 | * aaa |
| 3970 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3971 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 3972 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 3973 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3974 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3975 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3976 | } |
| 3977 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3978 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3979 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3980 | #endif /* HAS_KEYWORDS */ |
| 3981 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3982 | if (command->group) { |
| 3983 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3984 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3985 | debug_printf_parse("done_word return 1: syntax error, " |
| 3986 | "groups and arglists don't mix\n"); |
| 3987 | return 1; |
| 3988 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3989 | |
| 3990 | /* If this word wasn't an assignment, next ones definitely |
| 3991 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3992 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 3993 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3994 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3995 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3996 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3997 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3998 | command->assignment_cnt++; |
| 3999 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4000 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4001 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4002 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4003 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4004 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4005 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4006 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4007 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4008 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4009 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4010 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4011 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4012 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4013 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4014 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4015 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4016 | return 1; |
| 4017 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4018 | /* Force FOR to have just one word (variable name) */ |
| 4019 | /* NB: basically, this makes hush see "for v in ..." |
| 4020 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4021 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4022 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4023 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4024 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4025 | #if ENABLE_HUSH_CASE |
| 4026 | /* Force CASE to have just one word */ |
| 4027 | if (ctx->ctx_res_w == RES_CASE) { |
| 4028 | done_pipe(ctx, PIPE_SEQ); |
| 4029 | } |
| 4030 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4031 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4032 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4033 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4034 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4035 | return 0; |
| 4036 | } |
| 4037 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4038 | |
| 4039 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4040 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4041 | * Return: |
| 4042 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4043 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4044 | * REDIRFD_TO_FILE if no & was seen, |
| 4045 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4046 | */ |
| 4047 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4048 | #define parse_redir_right_fd(as_string, input) \ |
| 4049 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4050 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4051 | 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] | 4052 | { |
| 4053 | int ch, d, ok; |
| 4054 | |
| 4055 | ch = i_peek(input); |
| 4056 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4057 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4058 | |
| 4059 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4060 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4061 | ch = i_peek(input); |
| 4062 | if (ch == '-') { |
| 4063 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4064 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4065 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4066 | } |
| 4067 | d = 0; |
| 4068 | ok = 0; |
| 4069 | while (ch != EOF && isdigit(ch)) { |
| 4070 | d = d*10 + (ch-'0'); |
| 4071 | ok = 1; |
| 4072 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4073 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4074 | ch = i_peek(input); |
| 4075 | } |
| 4076 | if (ok) return d; |
| 4077 | |
| 4078 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4079 | |
| 4080 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4081 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4084 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4085 | */ |
| 4086 | static int parse_redirect(struct parse_context *ctx, |
| 4087 | int fd, |
| 4088 | redir_type style, |
| 4089 | struct in_str *input) |
| 4090 | { |
| 4091 | struct command *command = ctx->command; |
| 4092 | struct redir_struct *redir; |
| 4093 | struct redir_struct **redirp; |
| 4094 | int dup_num; |
| 4095 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4096 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4097 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4098 | /* Check for a '>&1' type redirect */ |
| 4099 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4100 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4101 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4102 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4103 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4104 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4105 | if (dup_num) { /* <<-... */ |
| 4106 | ch = i_getch(input); |
| 4107 | nommu_addchr(&ctx->as_string, ch); |
| 4108 | ch = i_peek(input); |
| 4109 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4110 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4111 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4112 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4113 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4114 | if (ch == '|') { |
| 4115 | /* >|FILE redirect ("clobbering" >). |
| 4116 | * Since we do not support "set -o noclobber" yet, |
| 4117 | * >| and > are the same for now. Just eat |. |
| 4118 | */ |
| 4119 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4120 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | |
| 4124 | /* Create a new redir_struct and append it to the linked list */ |
| 4125 | redirp = &command->redirects; |
| 4126 | while ((redir = *redirp) != NULL) { |
| 4127 | redirp = &(redir->next); |
| 4128 | } |
| 4129 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4130 | /* redir->next = NULL; */ |
| 4131 | /* redir->rd_filename = NULL; */ |
| 4132 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4133 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4134 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4135 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4136 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4137 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4138 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4139 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4140 | /* Erik had a check here that the file descriptor in question |
| 4141 | * is legit; I postpone that to "run time" |
| 4142 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4143 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4144 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4145 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4146 | #if 0 /* Instead we emit error message at run time */ |
| 4147 | if (ctx->pending_redirect) { |
| 4148 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4149 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4150 | } |
| 4151 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4152 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4153 | * end of the next parsed word. */ |
| 4154 | ctx->pending_redirect = redir; |
| 4155 | } |
| 4156 | return 0; |
| 4157 | } |
| 4158 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4159 | /* If a redirect is immediately preceded by a number, that number is |
| 4160 | * supposed to tell which file descriptor to redirect. This routine |
| 4161 | * looks for such preceding numbers. In an ideal world this routine |
| 4162 | * needs to handle all the following classes of redirects... |
| 4163 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4164 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4165 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4166 | * 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] | 4167 | * |
| 4168 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4169 | * "2.7 Redirection |
| 4170 | * ... If n is quoted, the number shall not be recognized as part of |
| 4171 | * the redirection expression. For example: |
| 4172 | * echo \2>a |
| 4173 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4174 | * 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] | 4175 | * |
| 4176 | * A -1 return means no valid number was found, |
| 4177 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4178 | */ |
| 4179 | static int redirect_opt_num(o_string *o) |
| 4180 | { |
| 4181 | int num; |
| 4182 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4183 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4184 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4185 | num = bb_strtou(o->data, NULL, 10); |
| 4186 | if (errno || num < 0) |
| 4187 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4188 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4189 | return num; |
| 4190 | } |
| 4191 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4192 | #if BB_MMU |
| 4193 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4194 | fetch_till_str(input, word, skip_tabs) |
| 4195 | #endif |
| 4196 | static char *fetch_till_str(o_string *as_string, |
| 4197 | struct in_str *input, |
| 4198 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4199 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4200 | { |
| 4201 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4202 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4203 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4204 | int ch; |
| 4205 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4206 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4207 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4208 | while (1) { |
| 4209 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4210 | if (ch != EOF) |
| 4211 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4212 | if (ch == '\n' || ch == EOF) { |
| 4213 | check_heredoc_end: |
| 4214 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4215 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4216 | heredoc.data[past_EOL] = '\0'; |
| 4217 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4218 | return heredoc.data; |
| 4219 | } |
| 4220 | if (ch == '\n') { |
| 4221 | /* This is a new line. |
| 4222 | * Remember position and backslash-escaping status. |
| 4223 | */ |
| 4224 | o_addchr(&heredoc, ch); |
| 4225 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4226 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4227 | past_EOL = heredoc.length; |
| 4228 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4229 | do { |
| 4230 | ch = i_getch(input); |
| 4231 | if (ch != EOF) |
| 4232 | nommu_addchr(as_string, ch); |
| 4233 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4234 | /* If this immediately ended the line, |
| 4235 | * go back to end-of-line checks. |
| 4236 | */ |
| 4237 | if (ch == '\n') |
| 4238 | goto check_heredoc_end; |
| 4239 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4240 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4241 | } |
| 4242 | if (ch == EOF) { |
| 4243 | o_free_unsafe(&heredoc); |
| 4244 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4245 | } |
| 4246 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4247 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4248 | if (prev == '\\' && ch == '\\') |
| 4249 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4250 | prev = 0; /* not \ */ |
| 4251 | else |
| 4252 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4256 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4257 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4258 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4259 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4260 | { |
| 4261 | struct pipe *pi = ctx->list_head; |
| 4262 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4263 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4264 | int i; |
| 4265 | struct command *cmd = pi->cmds; |
| 4266 | |
| 4267 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4268 | pi->num_cmds, |
| 4269 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4270 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4271 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4272 | |
| 4273 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4274 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4275 | while (redir) { |
| 4276 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4277 | char *p; |
| 4278 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4279 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4280 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4281 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4282 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4283 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4284 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4285 | return 1; |
| 4286 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4287 | free(redir->rd_filename); |
| 4288 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4289 | heredoc_cnt--; |
| 4290 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4291 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4292 | } |
| 4293 | cmd++; |
| 4294 | } |
| 4295 | pi = pi->next; |
| 4296 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4297 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4298 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4299 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4300 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4301 | #endif |
| 4302 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4306 | static int run_list(struct pipe *pi); |
| 4307 | #if BB_MMU |
| 4308 | #define parse_stream(pstring, input, end_trigger) \ |
| 4309 | parse_stream(input, end_trigger) |
| 4310 | #endif |
| 4311 | static struct pipe *parse_stream(char **pstring, |
| 4312 | struct in_str *input, |
| 4313 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4314 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4315 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4316 | static int parse_group(struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4317 | struct in_str *input, int ch) |
| 4318 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4319 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4320 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4321 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4322 | #if BB_MMU |
| 4323 | # define as_string NULL |
| 4324 | #else |
| 4325 | char *as_string = NULL; |
| 4326 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4327 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4328 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4329 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4330 | |
| 4331 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4332 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4333 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4334 | if (ctx->word.length) |
| 4335 | if (done_word(ctx)) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4336 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4337 | if (!command->argv) |
| 4338 | goto skip; /* (... */ |
| 4339 | if (command->argv[1]) { /* word word ... (... */ |
| 4340 | syntax_error_unexpected_ch('('); |
| 4341 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4342 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4343 | /* it is "word(..." or "word (..." */ |
| 4344 | do |
| 4345 | ch = i_getch(input); |
| 4346 | while (ch == ' ' || ch == '\t'); |
| 4347 | if (ch != ')') { |
| 4348 | syntax_error_unexpected_ch(ch); |
| 4349 | return 1; |
| 4350 | } |
| 4351 | nommu_addchr(&ctx->as_string, ch); |
| 4352 | do |
| 4353 | ch = i_getch(input); |
| 4354 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4355 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4356 | syntax_error_unexpected_ch(ch); |
| 4357 | return 1; |
| 4358 | } |
| 4359 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4360 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4361 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4362 | } |
| 4363 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4364 | |
| 4365 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4366 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4367 | || ctx->word.length /* word{... */ |
| 4368 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4369 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4370 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4371 | debug_printf_parse("parse_group return 1: " |
| 4372 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4373 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4374 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4375 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4376 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4377 | IF_HUSH_FUNCTIONS(skip:) |
| 4378 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4379 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4380 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4381 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4382 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4383 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4384 | } else { |
| 4385 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4386 | ch = i_peek(input); |
| 4387 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4388 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4389 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4390 | syntax_error_unexpected_ch(ch); |
| 4391 | return 1; |
| 4392 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4393 | if (ch != '(') { |
| 4394 | ch = i_getch(input); |
| 4395 | nommu_addchr(&ctx->as_string, ch); |
| 4396 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4397 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4398 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4399 | pipe_list = parse_stream(&as_string, input, endch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4400 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4401 | if (as_string) |
| 4402 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4403 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4404 | |
| 4405 | /* empty ()/{} or parse error? */ |
| 4406 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4407 | /* parse_stream already emitted error msg */ |
| 4408 | if (!BB_MMU) |
| 4409 | free(as_string); |
| 4410 | debug_printf_parse("parse_group return 1: " |
| 4411 | "parse_stream returned %p\n", pipe_list); |
| 4412 | return 1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4413 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4414 | #if !BB_MMU |
| 4415 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4416 | command->group_as_string = as_string; |
| 4417 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4418 | command->group_as_string); |
| 4419 | #endif |
| 4420 | |
| 4421 | #if ENABLE_HUSH_FUNCTIONS |
| 4422 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4423 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4424 | struct command *cmd2; |
| 4425 | |
| 4426 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4427 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4428 | cmd2->group = pipe_list; |
| 4429 | # if !BB_MMU |
| 4430 | //UNTESTED! |
| 4431 | cmd2->group_as_string = command->group_as_string; |
| 4432 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4433 | # endif |
| 4434 | |
| 4435 | pipe_list = new_pipe(); |
| 4436 | pipe_list->cmds = cmd2; |
| 4437 | pipe_list->num_cmds = 1; |
| 4438 | } |
| 4439 | #endif |
| 4440 | |
| 4441 | command->group = pipe_list; |
| 4442 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4443 | debug_printf_parse("parse_group return 0\n"); |
| 4444 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4445 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4446 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4449 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4450 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4451 | 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] | 4452 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4453 | 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] | 4454 | { |
| 4455 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4456 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4457 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4458 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4459 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4460 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4461 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4462 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4463 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4467 | 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] | 4468 | { |
| 4469 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4470 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4471 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4472 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4473 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4474 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4475 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4476 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4477 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4478 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4479 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4480 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4481 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4482 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4483 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4484 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4485 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4486 | continue; |
| 4487 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4488 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4492 | * \` quoting. |
| 4493 | * "Within the backquoted style of command substitution, backslash |
| 4494 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4495 | * The search for the matching backquote shall be satisfied by the first |
| 4496 | * backquote found without a preceding backslash; during this search, |
| 4497 | * if a non-escaped backquote is encountered within a shell comment, |
| 4498 | * a here-document, an embedded command substitution of the $(command) |
| 4499 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4500 | * or double-quoted string that begins, but does not end, within the |
| 4501 | * "`...`" sequence produces undefined results." |
| 4502 | * Example Output |
| 4503 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4504 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4505 | 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] | 4506 | { |
| 4507 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4508 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4509 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4510 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4511 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4512 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4513 | ch = i_getch(input); |
| 4514 | if (ch != '`' |
| 4515 | && ch != '$' |
| 4516 | && ch != '\\' |
| 4517 | && (!in_dquote || ch != '"') |
| 4518 | ) { |
| 4519 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4520 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4521 | } |
| 4522 | if (ch == EOF) { |
| 4523 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4524 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4525 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4526 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4530 | * quoting and nested ()s. |
| 4531 | * "With the $(command) style of command substitution, all characters |
| 4532 | * following the open parenthesis to the matching closing parenthesis |
| 4533 | * constitute the command. Any valid shell script can be used for command, |
| 4534 | * except a script consisting solely of redirections which produces |
| 4535 | * unspecified results." |
| 4536 | * Example Output |
| 4537 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4538 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4539 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4540 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4541 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4542 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4543 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4544 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4545 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4546 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4547 | 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] | 4548 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4549 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4550 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4551 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4552 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4553 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4554 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4555 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4556 | G.promptmode = 1; /* PS2 */ |
| 4557 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4558 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4559 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4560 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4561 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4562 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4563 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4564 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4565 | if (ch == end_ch |
| 4566 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4567 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4568 | # endif |
| 4569 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4570 | if (!dbl) |
| 4571 | break; |
| 4572 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4573 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4574 | i_getch(input); /* eat second ')' */ |
| 4575 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4576 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4577 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4578 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4579 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4580 | if (ch == '(' || ch == '{') { |
| 4581 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4582 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4583 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4584 | o_addchr(dest, ch); |
| 4585 | continue; |
| 4586 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4587 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4588 | if (!add_till_single_quote(dest, input)) |
| 4589 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4590 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4591 | continue; |
| 4592 | } |
| 4593 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4594 | if (!add_till_double_quote(dest, input)) |
| 4595 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4596 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4597 | continue; |
| 4598 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4599 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4600 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4601 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4602 | o_addchr(dest, ch); |
| 4603 | continue; |
| 4604 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4605 | if (ch == '\\') { |
| 4606 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4607 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4608 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4609 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4610 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4611 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4612 | #if 0 |
| 4613 | if (ch == '\n') { |
| 4614 | /* "backslash+newline", ignore both */ |
| 4615 | o_delchr(dest); /* undo insertion of '\' */ |
| 4616 | continue; |
| 4617 | } |
| 4618 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4619 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4620 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4621 | continue; |
| 4622 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4623 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4624 | debug_printf_parse("%s return '%s' ch:'%c'\n", __func__, dest->data, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4625 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4626 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4627 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4628 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4629 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4630 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4631 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4632 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4633 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4634 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4635 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4636 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4637 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4638 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4639 | 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] | 4640 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4641 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4642 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4643 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4644 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4645 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4646 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4647 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4648 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4649 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4650 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4651 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4652 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4653 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4654 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4655 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4656 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4657 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4658 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4659 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4660 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4661 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4662 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4663 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4664 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4665 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4666 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4667 | o_addchr(dest, ch | quote_mask); |
| 4668 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4669 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4670 | case '$': /* pid */ |
| 4671 | case '!': /* last bg pid */ |
| 4672 | case '?': /* last exit code */ |
| 4673 | case '#': /* number of args */ |
| 4674 | case '*': /* args */ |
| 4675 | case '@': /* args */ |
| 4676 | goto make_one_char_var; |
| 4677 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4678 | char len_single_ch; |
| 4679 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4680 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4681 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4682 | ch = i_getch(input); /* eat '{' */ |
| 4683 | nommu_addchr(as_string, ch); |
| 4684 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4685 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4686 | /* It should be ${?}, or ${#var}, |
| 4687 | * or even ${?+subst} - operator acting on a special variable, |
| 4688 | * or the beginning of variable name. |
| 4689 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4690 | if (ch == EOF |
| 4691 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4692 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4693 | bad_dollar_syntax: |
| 4694 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4695 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4696 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4697 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4698 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4699 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4700 | ch |= quote_mask; |
| 4701 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4702 | /* 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] | 4703 | * However, this regresses some of our testsuite cases |
| 4704 | * which check invalid constructs like ${%}. |
| 4705 | * Oh well... let's check that the var name part is fine... */ |
| 4706 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4707 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4708 | unsigned pos; |
| 4709 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4710 | o_addchr(dest, ch); |
| 4711 | debug_printf_parse(": '%c'\n", ch); |
| 4712 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4713 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4714 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4715 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4716 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4717 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4718 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4719 | unsigned end_ch; |
| 4720 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4721 | /* handle parameter expansions |
| 4722 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4723 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4724 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4725 | if (len_single_ch != '#' |
| 4726 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4727 | || i_peek(input) != '}' |
| 4728 | ) { |
| 4729 | goto bad_dollar_syntax; |
| 4730 | } |
| 4731 | /* else: it's "length of C" ${#C} op, |
| 4732 | * where C is a single char |
| 4733 | * special var name, e.g. ${#!}. |
| 4734 | */ |
| 4735 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4736 | /* Eat everything until closing '}' (or ':') */ |
| 4737 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4738 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4739 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4740 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4741 | ) { |
| 4742 | /* It's ${var:N[:M]} thing */ |
| 4743 | end_ch = '}' * 0x100 + ':'; |
| 4744 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4745 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4746 | && ch == '/' |
| 4747 | ) { |
| 4748 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4749 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4750 | i_getch(input); |
| 4751 | nommu_addchr(as_string, '/'); |
| 4752 | ch = '\\'; |
| 4753 | } |
| 4754 | end_ch = '}' * 0x100 + '/'; |
| 4755 | } |
| 4756 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4757 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4758 | if (!BB_MMU) |
| 4759 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4760 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4761 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4762 | if (last_ch == 0) /* error? */ |
| 4763 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4764 | #else |
| 4765 | #error Simple code to only allow ${var} is not implemented |
| 4766 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4767 | if (as_string) { |
| 4768 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4769 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4770 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4771 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4772 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4773 | && (end_ch & 0xff00) |
| 4774 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4775 | /* close the first block: */ |
| 4776 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4777 | /* while parsing N from ${var:N[:M]} |
| 4778 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4779 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4780 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4781 | end_ch = '}'; |
| 4782 | goto again; |
| 4783 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4784 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4785 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4786 | /* it's ${var:N} - emulate :999999999 */ |
| 4787 | o_addstr(dest, "999999999"); |
| 4788 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4789 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4790 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4791 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4792 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4793 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4794 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4795 | break; |
| 4796 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4797 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4798 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4799 | unsigned pos; |
| 4800 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4801 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4802 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4803 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4804 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4805 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4806 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4807 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4808 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4809 | if (!BB_MMU) |
| 4810 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4811 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4812 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4813 | if (as_string) { |
| 4814 | o_addstr(as_string, dest->data + pos); |
| 4815 | o_addchr(as_string, ')'); |
| 4816 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4817 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4818 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4819 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4820 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4821 | # endif |
| 4822 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4823 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4824 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4825 | if (!BB_MMU) |
| 4826 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4827 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4828 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4829 | if (as_string) { |
| 4830 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4831 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4832 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4833 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4834 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4835 | break; |
| 4836 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4837 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4838 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4839 | goto make_var; |
| 4840 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4841 | /* TODO: $_ and $-: */ |
| 4842 | /* $_ Shell or shell script name; or last argument of last command |
| 4843 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4844 | * 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] | 4845 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4846 | ch = i_getch(input); |
| 4847 | nommu_addchr(as_string, ch); |
| 4848 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4849 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4850 | ch = '_'; |
| 4851 | goto make_var1; |
| 4852 | } |
| 4853 | /* else: it's $_ */ |
| 4854 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4855 | default: |
| 4856 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4857 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4858 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4859 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4860 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4863 | #if BB_MMU |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4864 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4865 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4866 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4867 | # else |
| 4868 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4869 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4870 | encode_string(dest, input, dquote_end) |
| 4871 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4872 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4873 | |
| 4874 | #else /* !MMU */ |
| 4875 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4876 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4877 | /* all parameters are needed, no macro tricks */ |
| 4878 | # else |
| 4879 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4880 | encode_string(as_string, dest, input, dquote_end) |
| 4881 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4882 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4883 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4884 | o_string *dest, |
| 4885 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4886 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4887 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4888 | { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4889 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4890 | const int process_bkslash = 1; |
| 4891 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4892 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4893 | int next; |
| 4894 | |
| 4895 | again: |
| 4896 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4897 | if (ch != EOF) |
| 4898 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4899 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4900 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4901 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4902 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4903 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4904 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4905 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4906 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4907 | } |
| 4908 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4909 | if (ch != '\n') { |
| 4910 | next = i_peek(input); |
| 4911 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4912 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4913 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4914 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4915 | if (next == EOF) { |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 4916 | // TODO: what if in interactive shell a file with |
| 4917 | // echo "unterminated string\<eof> |
| 4918 | // is sourced? |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4919 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4920 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4921 | } |
| 4922 | /* bash: |
| 4923 | * "The backslash retains its special meaning [in "..."] |
| 4924 | * only when followed by one of the following characters: |
| 4925 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4926 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4927 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4928 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4929 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4930 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4931 | ch = i_getch(input); /* eat next */ |
| 4932 | if (ch == '\n') |
| 4933 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4934 | } /* else: ch remains == '\\', and we double it below: */ |
| 4935 | 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] | 4936 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4937 | goto again; |
| 4938 | } |
| 4939 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4940 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4941 | debug_printf_parse("encode_string return 0: " |
| 4942 | "parse_dollar returned 0 (error)\n"); |
| 4943 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4944 | } |
| 4945 | goto again; |
| 4946 | } |
| 4947 | #if ENABLE_HUSH_TICK |
| 4948 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4949 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4950 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4951 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4952 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4953 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4954 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4955 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4956 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4957 | } |
| 4958 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4959 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4960 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4961 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4964 | /* |
| 4965 | * Scan input until EOF or end_trigger char. |
| 4966 | * Return a list of pipes to execute, or NULL on EOF |
| 4967 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4968 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4969 | * reset parsing machinery and start parsing anew, |
| 4970 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4971 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4972 | static struct pipe *parse_stream(char **pstring, |
| 4973 | struct in_str *input, |
| 4974 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4975 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4976 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4977 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4978 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4979 | /* Single-quote triggers a bypass of the main loop until its mate is |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4980 | * found. When recursing, quote state is passed in via ctx.word.o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4981 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4982 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4983 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4984 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4985 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4986 | initialize_context(&ctx); |
| 4987 | |
| 4988 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 4989 | * Preventing this: |
| 4990 | */ |
| 4991 | o_addchr(&ctx.word, '\0'); |
| 4992 | ctx.word.length = 0; |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4993 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4994 | /* We used to separate words on $IFS here. This was wrong. |
| 4995 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4996 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4997 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4998 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4999 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5000 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5001 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5002 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5003 | int ch; |
| 5004 | int next; |
| 5005 | int redir_fd; |
| 5006 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5007 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5008 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5009 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5010 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5011 | if (ch == EOF) { |
| 5012 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5013 | |
| 5014 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5015 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5016 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5017 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5018 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5019 | syntax_error_unterm_ch('('); |
| 5020 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5021 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5022 | if (end_trigger == '}') { |
| 5023 | syntax_error_unterm_ch('{'); |
| 5024 | goto parse_error; |
| 5025 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5026 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5027 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5028 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5029 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5030 | o_free(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5031 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5032 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5033 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5034 | /* (this makes bare "&" cmd a no-op. |
| 5035 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5036 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5037 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5038 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5039 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5040 | pi = NULL; |
| 5041 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5042 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5043 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5044 | if (pstring) |
| 5045 | *pstring = ctx.as_string.data; |
| 5046 | else |
| 5047 | o_free_unsafe(&ctx.as_string); |
| 5048 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5049 | debug_leave(); |
| 5050 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5051 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5052 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5053 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5054 | |
| 5055 | next = '\0'; |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5056 | if (ch != '\n') { |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5057 | /* Do not break this case: |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5058 | * echo '\ |
| 5059 | * ' |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5060 | * and |
| 5061 | * echo z\\ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5062 | */ |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5063 | next = (ch == '\'' || ch == '\\') ? i_peek(input) : i_peek_and_eat_bkslash_nl(input); |
| 5064 | /// |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5065 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5066 | |
| 5067 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5068 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
| 5069 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5070 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5071 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5072 | || ctx.word.length /* word{... - non-special */ |
| 5073 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5074 | || (next != ';' /* }; - special */ |
| 5075 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5076 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5077 | && next != '&' /* }& and }&& ... - special */ |
| 5078 | && next != '|' /* }|| ... - special */ |
| 5079 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5080 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5081 | ) { |
| 5082 | /* They are not special, skip "{}" */ |
| 5083 | is_special += 2; |
| 5084 | } |
| 5085 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5086 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5087 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5088 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5089 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5090 | o_addQchr(&ctx.word, ch); |
| 5091 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5092 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5093 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5094 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5095 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5096 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5097 | debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5098 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5099 | continue; |
| 5100 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5101 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5102 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5103 | #if ENABLE_HUSH_LINENO_VAR |
| 5104 | /* Case: |
| 5105 | * "while ...; do<whitespace><newline> |
| 5106 | * cmd ..." |
| 5107 | * would think that "cmd" starts in <whitespace> - |
| 5108 | * i.e., at the previous line. |
| 5109 | * We need to skip all whitespace before newlines. |
| 5110 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5111 | while (ch != '\n') { |
| 5112 | next = i_peek(input); |
| 5113 | if (next != ' ' && next != '\t' && next != '\n') |
| 5114 | break; /* next char is not ws */ |
| 5115 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5116 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5117 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5118 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5119 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5120 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5121 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5122 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5123 | /* Is this a case when newline is simply ignored? |
| 5124 | * Some examples: |
| 5125 | * "cmd | <newline> cmd ..." |
| 5126 | * "case ... in <newline> word) ..." |
| 5127 | */ |
| 5128 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5129 | && ctx.word.length == 0 && !ctx.word.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5130 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5131 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5132 | * Without check #1, interactive shell |
| 5133 | * ignores even bare <newline>, |
| 5134 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5135 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5136 | * ps2> _ <=== wrong, should be ps1 |
| 5137 | * Without check #2, "cmd & <newline>" |
| 5138 | * is similarly mistreated. |
| 5139 | * (BTW, this makes "cmd & cmd" |
| 5140 | * and "cmd && cmd" non-orthogonal. |
| 5141 | * Really, ask yourself, why |
| 5142 | * "cmd && <newline>" doesn't start |
| 5143 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5144 | * The only reason is that it might be |
| 5145 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5146 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5147 | */ |
| 5148 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5149 | if (pi->num_cmds != 0 /* check #1 */ |
| 5150 | && pi->followup != PIPE_BG /* check #2 */ |
| 5151 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5152 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5153 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5154 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5155 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5156 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5157 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5158 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5159 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5160 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5161 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5162 | heredoc_cnt = 0; |
| 5163 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5164 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5165 | debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5166 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5167 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5168 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5169 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5170 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5171 | |
| 5172 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5173 | * } is an ordinary char in this case, even inside { cmd; } |
| 5174 | * Pathological example: { ""}; } should exec "}" cmd |
| 5175 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5176 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5177 | if (ctx.word.length != 0 /* word} */ |
| 5178 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5179 | ) { |
| 5180 | goto ordinary_char; |
| 5181 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5182 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5183 | /* Generally, there should be semicolon: "cmd; }" |
| 5184 | * However, bash allows to omit it if "cmd" is |
| 5185 | * a group. Examples: |
| 5186 | * { { echo 1; } } |
| 5187 | * {(echo 1)} |
| 5188 | * { echo 0 >&2 | { echo 1; } } |
| 5189 | * { while false; do :; done } |
| 5190 | * { case a in b) ;; esac } |
| 5191 | */ |
| 5192 | if (ctx.command->group) |
| 5193 | goto term_group; |
| 5194 | goto ordinary_char; |
| 5195 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5196 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5197 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5198 | goto skip_end_trigger; |
| 5199 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5200 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5201 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5202 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5203 | && (ch != ';' || heredoc_cnt == 0) |
| 5204 | #if ENABLE_HUSH_CASE |
| 5205 | && (ch != ')' |
| 5206 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5207 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5208 | ) |
| 5209 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5210 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5211 | if (heredoc_cnt) { |
| 5212 | /* This is technically valid: |
| 5213 | * { cat <<HERE; }; echo Ok |
| 5214 | * heredoc |
| 5215 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5216 | * HERE |
| 5217 | * but we don't support this. |
| 5218 | * We require heredoc to be in enclosing {}/(), |
| 5219 | * if any. |
| 5220 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5221 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5222 | goto parse_error; |
| 5223 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5224 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5225 | goto parse_error; |
| 5226 | } |
| 5227 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5228 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5229 | debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5230 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5231 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5232 | 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] | 5233 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5234 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5235 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5236 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5237 | if (pstring) |
| 5238 | *pstring = ctx.as_string.data; |
| 5239 | else |
| 5240 | o_free_unsafe(&ctx.as_string); |
| 5241 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5242 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5243 | /* Example: bare "{ }", "()" */ |
| 5244 | G.last_exitcode = 2; /* bash compat */ |
| 5245 | syntax_error_unexpected_ch(ch); |
| 5246 | goto parse_error2; |
| 5247 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5248 | debug_printf_parse("parse_stream return %p: " |
| 5249 | "end_trigger char found\n", |
| 5250 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5251 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5252 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5253 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5254 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5255 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5256 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5257 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5258 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5259 | /* Catch <, > before deciding whether this word is |
| 5260 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5261 | switch (ch) { |
| 5262 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5263 | redir_fd = redirect_opt_num(&ctx.word); |
| 5264 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5265 | goto parse_error; |
| 5266 | } |
| 5267 | redir_style = REDIRECT_OVERWRITE; |
| 5268 | if (next == '>') { |
| 5269 | redir_style = REDIRECT_APPEND; |
| 5270 | ch = i_getch(input); |
| 5271 | nommu_addchr(&ctx.as_string, ch); |
| 5272 | } |
| 5273 | #if 0 |
| 5274 | else if (next == '(') { |
| 5275 | syntax_error(">(process) not supported"); |
| 5276 | goto parse_error; |
| 5277 | } |
| 5278 | #endif |
| 5279 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5280 | goto parse_error; |
| 5281 | continue; /* back to top of while (1) */ |
| 5282 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5283 | redir_fd = redirect_opt_num(&ctx.word); |
| 5284 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5285 | goto parse_error; |
| 5286 | } |
| 5287 | redir_style = REDIRECT_INPUT; |
| 5288 | if (next == '<') { |
| 5289 | redir_style = REDIRECT_HEREDOC; |
| 5290 | heredoc_cnt++; |
| 5291 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5292 | ch = i_getch(input); |
| 5293 | nommu_addchr(&ctx.as_string, ch); |
| 5294 | } else if (next == '>') { |
| 5295 | redir_style = REDIRECT_IO; |
| 5296 | ch = i_getch(input); |
| 5297 | nommu_addchr(&ctx.as_string, ch); |
| 5298 | } |
| 5299 | #if 0 |
| 5300 | else if (next == '(') { |
| 5301 | syntax_error("<(process) not supported"); |
| 5302 | goto parse_error; |
| 5303 | } |
| 5304 | #endif |
| 5305 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5306 | goto parse_error; |
| 5307 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5308 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5309 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5310 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5311 | /* note: we do not add it to &ctx.as_string */ |
| 5312 | /* TODO: in bash: |
| 5313 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5314 | * cmd "$(cmd2 #comment)" - syntax error |
| 5315 | * cmd "`cmd2 #comment`" - ok |
| 5316 | * We accept both (comment ends where command subst ends, in both cases). |
| 5317 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5318 | while (1) { |
| 5319 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5320 | if (ch == '\n') { |
| 5321 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5322 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5323 | } |
| 5324 | ch = i_getch(input); |
| 5325 | if (ch == EOF) |
| 5326 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5327 | } |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5328 | continue; /* back to top of while (1) */ |
| 5329 | } |
| 5330 | break; |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5331 | #if 0 /* looks like we never reach this code */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5332 | case '\\': |
| 5333 | if (next == '\n') { |
| 5334 | /* It's "\<newline>" */ |
| 5335 | #if !BB_MMU |
| 5336 | /* Remove trailing '\' from ctx.as_string */ |
| 5337 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 5338 | #endif |
| 5339 | ch = i_getch(input); /* eat it */ |
| 5340 | continue; /* back to top of while (1) */ |
| 5341 | } |
| 5342 | break; |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5343 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5346 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5347 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5348 | && !ctx.pending_redirect |
| 5349 | ) { |
| 5350 | /* ch is a special char and thus this word |
| 5351 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5352 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5353 | debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5356 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5357 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5358 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5359 | case SPECIAL_VAR_SYMBOL: |
| 5360 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5361 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5362 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5363 | /* fall through */ |
| 5364 | case '#': |
| 5365 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5366 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5367 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5368 | case '\\': |
| 5369 | if (next == EOF) { |
Denys Vlasenko | 1c57269 | 2018-04-10 13:09:26 +0200 | [diff] [blame] | 5370 | //TODO: in ". FILE" containing "cmd\" (no newline) bash ignores last "\" |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5371 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5372 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5373 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5374 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5375 | /* note: ch != '\n' (that case does not reach this place) */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5376 | o_addchr(&ctx.word, '\\'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5377 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5378 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5379 | nommu_addchr(&ctx.as_string, ch); |
| 5380 | /* Example: echo Hello \2>file |
| 5381 | * we need to know that word 2 is quoted */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5382 | ctx.word.has_quoted_part = 1; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5383 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5384 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5385 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5386 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5387 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5388 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5389 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5390 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5391 | case '\'': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5392 | ctx.word.has_quoted_part = 1; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5393 | if (next == '\'' && !ctx.pending_redirect) |
| 5394 | goto insert_empty_quoted_str_marker; |
| 5395 | while (1) { |
| 5396 | ch = i_getch(input); |
| 5397 | if (ch == EOF) { |
| 5398 | syntax_error_unterm_ch('\''); |
| 5399 | goto parse_error; |
| 5400 | } |
| 5401 | nommu_addchr(&ctx.as_string, ch); |
| 5402 | if (ch == '\'') |
| 5403 | break; |
| 5404 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5405 | /* Convert raw ^C to corresponding special variable reference */ |
| 5406 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5407 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5408 | } |
| 5409 | o_addqchr(&ctx.word, ch); |
| 5410 | } |
| 5411 | continue; /* get next char */ |
| 5412 | case '"': |
| 5413 | ctx.word.has_quoted_part = 1; |
| 5414 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5415 | insert_empty_quoted_str_marker: |
| 5416 | nommu_addchr(&ctx.as_string, next); |
| 5417 | i_getch(input); /* eat second ' */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5418 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5419 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5420 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5421 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5422 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5423 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
| 5424 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5425 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5426 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5427 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5428 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5429 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5430 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5431 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5432 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5433 | o_addchr(&ctx.word, '`'); |
| 5434 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5435 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5436 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5437 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5438 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5439 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5440 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5441 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5442 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5443 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5444 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5445 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5446 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5447 | #if ENABLE_HUSH_CASE |
| 5448 | case_semi: |
| 5449 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5450 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5451 | goto parse_error; |
| 5452 | } |
| 5453 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5454 | #if ENABLE_HUSH_CASE |
| 5455 | /* Eat multiple semicolons, detect |
| 5456 | * whether it means something special */ |
| 5457 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5458 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5459 | if (ch != ';') |
| 5460 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5461 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5462 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5463 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5464 | ctx.ctx_dsemicolon = 1; |
| 5465 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5466 | break; |
| 5467 | } |
| 5468 | } |
| 5469 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5470 | new_cmd: |
| 5471 | /* We just finished a cmd. New one may start |
| 5472 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5473 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5474 | debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5475 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5476 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5477 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5478 | goto parse_error; |
| 5479 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5480 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5481 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5482 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5483 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5484 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5485 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5486 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5487 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5488 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5489 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5490 | goto parse_error; |
| 5491 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5492 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5493 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5494 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5495 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5496 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5497 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5498 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5499 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5500 | } else { |
| 5501 | /* we could pick up a file descriptor choice here |
| 5502 | * with redirect_opt_num(), but bash doesn't do it. |
| 5503 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5504 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5505 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5506 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5507 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5508 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5509 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5510 | if (ctx.ctx_res_w == RES_MATCH |
| 5511 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5512 | && ctx.word.length == 0 /* not word(... */ |
| 5513 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5514 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame^] | 5515 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5516 | } |
| 5517 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5518 | case '{': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5519 | if (parse_group(&ctx, input, ch) != 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5520 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5521 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5522 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5523 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5524 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5525 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5526 | goto case_semi; |
| 5527 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5528 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5529 | /* proper use of this character is caught by end_trigger: |
| 5530 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5531 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5532 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5533 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5534 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5535 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5536 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5537 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5538 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5539 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5540 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5541 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5542 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5543 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5544 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5545 | struct parse_context *pctx; |
| 5546 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5547 | |
| 5548 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5549 | * Sample for finding leaks on syntax error recovery path. |
| 5550 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5551 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5552 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5553 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5554 | * 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] | 5555 | */ |
| 5556 | pctx = &ctx; |
| 5557 | do { |
| 5558 | /* Update pipe/command counts, |
| 5559 | * otherwise freeing may miss some */ |
| 5560 | done_pipe(pctx, PIPE_SEQ); |
| 5561 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5562 | pctx->list_head, pctx); |
| 5563 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5564 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5565 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5566 | #if !BB_MMU |
| 5567 | o_free_unsafe(&pctx->as_string); |
| 5568 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5569 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5570 | if (pctx != &ctx) { |
| 5571 | free(pctx); |
| 5572 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5573 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5574 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5575 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5576 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5577 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5578 | if (pstring) |
| 5579 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5580 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5581 | debug_leave(); |
| 5582 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5583 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5586 | |
| 5587 | /*** Execution routines ***/ |
| 5588 | |
| 5589 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5590 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5591 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5592 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5593 | expand_string_to_string(str) |
| 5594 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5595 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5596 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5597 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5598 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5599 | |
| 5600 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5601 | * all variable references within and returns a pointer to |
| 5602 | * a list of expanded strings, possibly with larger number |
| 5603 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5604 | * This new list is allocated as a single malloc block. |
| 5605 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5606 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5607 | * Caller can deallocate entire list by single free(list). */ |
| 5608 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5609 | /* A horde of its helpers come first: */ |
| 5610 | |
| 5611 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5612 | { |
| 5613 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5614 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5615 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5616 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5617 | if (c == '{' || c == '}') { |
| 5618 | /* { -> \{, } -> \} */ |
| 5619 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5620 | /* And now we want to add { or } and continue: |
| 5621 | * o_addchr(o, c); |
| 5622 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5623 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5624 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5625 | } |
| 5626 | #endif |
| 5627 | o_addchr(o, c); |
| 5628 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5629 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5630 | o_addchr(o, '\\'); |
| 5631 | if (len) { |
| 5632 | len--; |
| 5633 | o_addchr(o, '\\'); |
| 5634 | o_addchr(o, *str++); |
| 5635 | } |
| 5636 | } |
| 5637 | } |
| 5638 | } |
| 5639 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5640 | /* Store given string, finalizing the word and starting new one whenever |
| 5641 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5642 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5643 | * Return in *ended_with_ifs: |
| 5644 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5645 | */ |
| 5646 | 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] | 5647 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5648 | int last_is_ifs = 0; |
| 5649 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5650 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5651 | int word_len; |
| 5652 | |
| 5653 | if (!*str) /* EOL - do not finalize word */ |
| 5654 | break; |
| 5655 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5656 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5657 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5658 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5659 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5660 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5661 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5662 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5663 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5664 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5665 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5666 | /*/ Why can't we do it easier? */ |
| 5667 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5668 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5669 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5670 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5671 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5672 | if (!*str) /* EOL - do not finalize word */ |
| 5673 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5674 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5675 | |
| 5676 | /* We know str here points to at least one IFS char */ |
| 5677 | last_is_ifs = 1; |
| 5678 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5679 | if (!*str) /* EOL - do not finalize word */ |
| 5680 | break; |
| 5681 | |
| 5682 | /* Start new word... but not always! */ |
| 5683 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5684 | if (output->has_quoted_part |
| 5685 | /* Case "v=' a'; echo $v": |
| 5686 | * here nothing precedes the space in $v expansion, |
| 5687 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5688 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5689 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5690 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5691 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5692 | o_addchr(output, '\0'); |
| 5693 | debug_print_list("expand_on_ifs", output, n); |
| 5694 | n = o_save_ptr(output, n); |
| 5695 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5696 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5697 | |
| 5698 | if (ended_with_ifs) |
| 5699 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5700 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5701 | return n; |
| 5702 | } |
| 5703 | |
| 5704 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5705 | * they are in double quotes, with the exception that they are not :). |
| 5706 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5707 | * |
| 5708 | * Returns malloced string. |
| 5709 | * As an optimization, we return NULL if expansion is not needed. |
| 5710 | */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5711 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5712 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5713 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5714 | encode_then_expand_string(str) |
| 5715 | #endif |
| 5716 | 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] | 5717 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5718 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 5719 | enum { do_unbackslash = 1 }; |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5720 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5721 | char *exp_str; |
| 5722 | struct in_str input; |
| 5723 | o_string dest = NULL_O_STRING; |
| 5724 | |
| 5725 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5726 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5727 | #if ENABLE_HUSH_TICK |
| 5728 | && !strchr(str, '`') |
| 5729 | #endif |
| 5730 | ) { |
| 5731 | return NULL; |
| 5732 | } |
| 5733 | |
| 5734 | /* We need to expand. Example: |
| 5735 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5736 | */ |
| 5737 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5738 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5739 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5740 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5741 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5742 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5743 | o_free_unsafe(&dest); |
| 5744 | return exp_str; |
| 5745 | } |
| 5746 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5747 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5748 | 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] | 5749 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5750 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5751 | arith_t res; |
| 5752 | char *exp_str; |
| 5753 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5754 | math_state.lookupvar = get_local_var_value; |
| 5755 | math_state.setvar = set_local_var_from_halves; |
| 5756 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5757 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5758 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5759 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5760 | if (errmsg_p) |
| 5761 | *errmsg_p = math_state.errmsg; |
| 5762 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5763 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5764 | return res; |
| 5765 | } |
| 5766 | #endif |
| 5767 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5768 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5769 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5770 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5771 | { |
| 5772 | while (1) { |
| 5773 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5774 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5775 | if (end) { |
| 5776 | *size = end - val; |
| 5777 | return val; |
| 5778 | } |
| 5779 | if (*val == '\0') |
| 5780 | return NULL; |
| 5781 | /* Optimization: if "*pat" did not match the start of "string", |
| 5782 | * we know that "tring", "ring" etc will not match too: |
| 5783 | */ |
| 5784 | if (pattern[0] == '*') |
| 5785 | return NULL; |
| 5786 | val++; |
| 5787 | } |
| 5788 | } |
| 5789 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5790 | { |
| 5791 | char *result = NULL; |
| 5792 | unsigned res_len = 0; |
| 5793 | unsigned repl_len = strlen(repl); |
| 5794 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5795 | /* Null pattern never matches, including if "var" is empty */ |
| 5796 | if (!pattern[0]) |
| 5797 | return result; /* NULL, no replaces happened */ |
| 5798 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5799 | while (1) { |
| 5800 | int size; |
| 5801 | char *s = strstr_pattern(val, pattern, &size); |
| 5802 | if (!s) |
| 5803 | break; |
| 5804 | |
| 5805 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5806 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5807 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5808 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5809 | |
| 5810 | val = s + size; |
| 5811 | if (exp_op == '/') |
| 5812 | break; |
| 5813 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5814 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5815 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5816 | strcpy(result + res_len, val); |
| 5817 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5818 | } |
| 5819 | debug_printf_varexp("result:'%s'\n", result); |
| 5820 | return result; |
| 5821 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5822 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5823 | |
| 5824 | /* Helper: |
| 5825 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5826 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5827 | 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] | 5828 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5829 | const char *val; |
| 5830 | char *to_be_freed; |
| 5831 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5832 | char *var; |
| 5833 | char first_char; |
| 5834 | char exp_op; |
| 5835 | char exp_save = exp_save; /* for compiler */ |
| 5836 | char *exp_saveptr; /* points to expansion operator */ |
| 5837 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5838 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5839 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5840 | val = NULL; |
| 5841 | to_be_freed = NULL; |
| 5842 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5843 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5844 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5845 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5846 | arg0 = arg[0]; |
| 5847 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5848 | exp_op = 0; |
| 5849 | |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5850 | if (first_char == '#' && arg[1] /* ${#...} but not ${#} */ |
| 5851 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 5852 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 5853 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5854 | ) { |
| 5855 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5856 | var++; |
| 5857 | exp_op = 'L'; |
| 5858 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5859 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5860 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5861 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5862 | ) { |
| 5863 | /* ${?:0}, ${#[:]%0} etc */ |
| 5864 | exp_saveptr = var + 1; |
| 5865 | } else { |
| 5866 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5867 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5868 | } |
| 5869 | exp_op = exp_save = *exp_saveptr; |
| 5870 | if (exp_op) { |
| 5871 | exp_word = exp_saveptr + 1; |
| 5872 | if (exp_op == ':') { |
| 5873 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5874 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5875 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5876 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5877 | ) { |
| 5878 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5879 | exp_op = ':'; |
| 5880 | exp_word--; |
| 5881 | } |
| 5882 | } |
| 5883 | *exp_saveptr = '\0'; |
| 5884 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5885 | } |
| 5886 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5887 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5888 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5889 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5890 | int n = xatoi_positive(var); |
| 5891 | if (n < G.global_argc) |
| 5892 | val = G.global_argv[n]; |
| 5893 | /* else val remains NULL: $N with too big N */ |
| 5894 | } else { |
| 5895 | switch (var[0]) { |
| 5896 | case '$': /* pid */ |
| 5897 | val = utoa(G.root_pid); |
| 5898 | break; |
| 5899 | case '!': /* bg pid */ |
| 5900 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5901 | break; |
| 5902 | case '?': /* exitcode */ |
| 5903 | val = utoa(G.last_exitcode); |
| 5904 | break; |
| 5905 | case '#': /* argc */ |
| 5906 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5907 | break; |
| 5908 | default: |
| 5909 | val = get_local_var_value(var); |
| 5910 | } |
| 5911 | } |
| 5912 | |
| 5913 | /* Handle any expansions */ |
| 5914 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5915 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5916 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5917 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5918 | debug_printf_expand("%s\n", val); |
| 5919 | } else if (exp_op) { |
| 5920 | if (exp_op == '%' || exp_op == '#') { |
| 5921 | /* Standard-mandated substring removal ops: |
| 5922 | * ${parameter%word} - remove smallest suffix pattern |
| 5923 | * ${parameter%%word} - remove largest suffix pattern |
| 5924 | * ${parameter#word} - remove smallest prefix pattern |
| 5925 | * ${parameter##word} - remove largest prefix pattern |
| 5926 | * |
| 5927 | * Word is expanded to produce a glob pattern. |
| 5928 | * Then var's value is matched to it and matching part removed. |
| 5929 | */ |
| 5930 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5931 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5932 | char *exp_exp_word; |
| 5933 | char *loc; |
| 5934 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5935 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5936 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5937 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 5938 | /* |
| 5939 | * process_bkslash:1 unbackslash:1 breaks this: |
| 5940 | * a='a\\'; echo ${a%\\\\} # correct output is: a |
| 5941 | * process_bkslash:1 unbackslash:0 breaks this: |
| 5942 | * a='a}'; echo ${a%\}} # correct output is: a |
| 5943 | */ |
| 5944 | exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5945 | if (exp_exp_word) |
| 5946 | exp_word = exp_exp_word; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5947 | debug_printf_expand("expand: exp_exp_word:'%s'\n", exp_word); |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5948 | /* HACK ALERT. We depend here on the fact that |
| 5949 | * G.global_argv and results of utoa and get_local_var_value |
| 5950 | * are actually in writable memory: |
| 5951 | * scan_and_match momentarily stores NULs there. */ |
| 5952 | t = (char*)val; |
| 5953 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5954 | debug_printf_expand("op:%c str:'%s' pat:'%s' res:'%s'\n", exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5955 | free(exp_exp_word); |
| 5956 | if (loc) { /* match was found */ |
| 5957 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5958 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5959 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5960 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5961 | } |
| 5962 | } |
| 5963 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5964 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5965 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5966 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5967 | * Note that in encoded form it has TWO parts: |
| 5968 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5969 | * and if // is used, it is encoded as \: |
| 5970 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5971 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5972 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5973 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5974 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5975 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 5976 | * >az >bz |
| 5977 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 5978 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 5979 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 5980 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5981 | * (note that a*z _pattern_ is never globbed!) |
| 5982 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5983 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5984 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5985 | if (!pattern) |
| 5986 | pattern = xstrdup(exp_word); |
| 5987 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5988 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5989 | exp_word = p; |
| 5990 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5991 | *p = '\0'; |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 5992 | repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5993 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5994 | /* HACK ALERT. We depend here on the fact that |
| 5995 | * G.global_argv and results of utoa and get_local_var_value |
| 5996 | * are actually in writable memory: |
| 5997 | * replace_pattern momentarily stores NULs there. */ |
| 5998 | t = (char*)val; |
| 5999 | to_be_freed = replace_pattern(t, |
| 6000 | pattern, |
| 6001 | (repl ? repl : exp_word), |
| 6002 | exp_op); |
| 6003 | if (to_be_freed) /* at least one replace happened */ |
| 6004 | val = to_be_freed; |
| 6005 | free(pattern); |
| 6006 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6007 | } else { |
| 6008 | /* Empty variable always gives nothing */ |
| 6009 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6010 | /* Just skip "replace" part */ |
| 6011 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6012 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6013 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6014 | } |
| 6015 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6016 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6017 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6018 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6019 | /* It's ${var:N[:M]} bashism. |
| 6020 | * Note that in encoded form it has TWO parts: |
| 6021 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6022 | */ |
| 6023 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6024 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6025 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6026 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6027 | if (errmsg) |
| 6028 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6029 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6030 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6031 | exp_word = p; |
| 6032 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6033 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6034 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6035 | if (errmsg) |
| 6036 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6037 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6038 | if (beg < 0) { |
| 6039 | /* negative beg counts from the end */ |
| 6040 | beg = (arith_t)strlen(val) + beg; |
| 6041 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6042 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6043 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6044 | debug_printf_varexp("from val:'%s'\n", val); |
| 6045 | if (len < 0) { |
| 6046 | /* in bash, len=-n means strlen()-n */ |
| 6047 | len = (arith_t)strlen(val) - beg + len; |
| 6048 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6049 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6050 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6051 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6052 | arith_err: |
| 6053 | val = NULL; |
| 6054 | } else { |
| 6055 | /* Paranoia. What if user entered 9999999999999 |
| 6056 | * which fits in arith_t but not int? */ |
| 6057 | if (len >= INT_MAX) |
| 6058 | len = INT_MAX; |
| 6059 | val = to_be_freed = xstrndup(val + beg, len); |
| 6060 | } |
| 6061 | debug_printf_varexp("val:'%s'\n", val); |
| 6062 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6063 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6064 | val = NULL; |
| 6065 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6066 | } else { /* one of "-=+?" */ |
| 6067 | /* Standard-mandated substitution ops: |
| 6068 | * ${var?word} - indicate error if unset |
| 6069 | * If var is unset, word (or a message indicating it is unset |
| 6070 | * if word is null) is written to standard error |
| 6071 | * and the shell exits with a non-zero exit status. |
| 6072 | * Otherwise, the value of var is substituted. |
| 6073 | * ${var-word} - use default value |
| 6074 | * If var is unset, word is substituted. |
| 6075 | * ${var=word} - assign and use default value |
| 6076 | * If var is unset, word is assigned to var. |
| 6077 | * In all cases, final value of var is substituted. |
| 6078 | * ${var+word} - use alternative value |
| 6079 | * If var is unset, null is substituted. |
| 6080 | * Otherwise, word is substituted. |
| 6081 | * |
| 6082 | * Word is subjected to tilde expansion, parameter expansion, |
| 6083 | * command substitution, and arithmetic expansion. |
| 6084 | * If word is not needed, it is not expanded. |
| 6085 | * |
| 6086 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6087 | * but also treat null var as if it is unset. |
| 6088 | */ |
| 6089 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6090 | if (exp_op == '+') |
| 6091 | use_word = !use_word; |
| 6092 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6093 | (exp_save == ':') ? "true" : "false", use_word); |
| 6094 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6095 | 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] | 6096 | if (to_be_freed) |
| 6097 | exp_word = to_be_freed; |
| 6098 | if (exp_op == '?') { |
| 6099 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6100 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6101 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 6102 | exp_word[0] |
| 6103 | ? exp_word |
| 6104 | : "parameter null or not set" |
| 6105 | /* ash has more specific messages, a-la: */ |
| 6106 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6107 | ); |
| 6108 | //TODO: how interactive bash aborts expansion mid-command? |
| 6109 | } else { |
| 6110 | val = exp_word; |
| 6111 | } |
| 6112 | |
| 6113 | if (exp_op == '=') { |
| 6114 | /* ${var=[word]} or ${var:=[word]} */ |
| 6115 | if (isdigit(var[0]) || var[0] == '#') { |
| 6116 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6117 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6118 | val = NULL; |
| 6119 | } else { |
| 6120 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 6121 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | } |
| 6125 | } /* one of "-=+?" */ |
| 6126 | |
| 6127 | *exp_saveptr = exp_save; |
| 6128 | } /* if (exp_op) */ |
| 6129 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6130 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6131 | |
| 6132 | *pp = p; |
| 6133 | *to_be_freed_pp = to_be_freed; |
| 6134 | return val; |
| 6135 | } |
| 6136 | |
| 6137 | /* Expand all variable references in given string, adding words to list[] |
| 6138 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6139 | * to be filled). This routine is extremely tricky: has to deal with |
| 6140 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6141 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6142 | 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] | 6143 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6144 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6145 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6146 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6147 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6148 | 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] | 6149 | char *p; |
| 6150 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6151 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6152 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6153 | debug_print_list("expand_vars_to_list", output, n); |
| 6154 | n = o_save_ptr(output, n); |
| 6155 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6156 | |
| 6157 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6158 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6159 | char *to_be_freed = NULL; |
| 6160 | const char *val = NULL; |
| 6161 | #if ENABLE_HUSH_TICK |
| 6162 | o_string subst_result = NULL_O_STRING; |
| 6163 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6164 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6165 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6166 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6167 | |
| 6168 | if (ended_in_ifs) { |
| 6169 | o_addchr(output, '\0'); |
| 6170 | n = o_save_ptr(output, n); |
| 6171 | ended_in_ifs = 0; |
| 6172 | } |
| 6173 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6174 | o_addblock(output, arg, p - arg); |
| 6175 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6176 | arg = ++p; |
| 6177 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6178 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6179 | /* Fetch special var name (if it is indeed one of them) |
| 6180 | * and quote bit, force the bit on if singleword expansion - |
| 6181 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6182 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6183 | |
| 6184 | /* Is this variable quoted and thus expansion can't be null? |
| 6185 | * "$@" is special. Even if quoted, it can still |
| 6186 | * expand to nothing (not even an empty string), |
| 6187 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6188 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6189 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6190 | |
| 6191 | switch (first_ch & 0x7f) { |
| 6192 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6193 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6194 | case '@': { |
| 6195 | int i; |
| 6196 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6197 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6198 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6199 | 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] | 6200 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6201 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6202 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6203 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6204 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6205 | /* this argv[] is not empty and not last: |
| 6206 | * put terminating NUL, start new word */ |
| 6207 | o_addchr(output, '\0'); |
| 6208 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6209 | n = o_save_ptr(output, n); |
| 6210 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6211 | } |
| 6212 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6213 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6214 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6215 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6216 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6217 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6218 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6219 | while (1) { |
| 6220 | o_addQstr(output, G.global_argv[i]); |
| 6221 | if (++i >= G.global_argc) |
| 6222 | break; |
| 6223 | o_addchr(output, '\0'); |
| 6224 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6225 | n = o_save_ptr(output, n); |
| 6226 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6227 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6228 | while (1) { |
| 6229 | o_addQstr(output, G.global_argv[i]); |
| 6230 | if (!G.global_argv[++i]) |
| 6231 | break; |
| 6232 | if (G.ifs[0]) |
| 6233 | o_addchr(output, G.ifs[0]); |
| 6234 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6235 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6236 | } |
| 6237 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6238 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6239 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 6240 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6241 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6242 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6243 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6244 | break; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6245 | case SPECIAL_VAR_QUOTED_SVS: |
| 6246 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
| 6247 | arg++; |
| 6248 | val = SPECIAL_VAR_SYMBOL_STR; |
| 6249 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6250 | #if ENABLE_HUSH_TICK |
| 6251 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6252 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6253 | arg++; |
| 6254 | /* Can't just stuff it into output o_string, |
| 6255 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6256 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6257 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6258 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6259 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6260 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 6261 | val = subst_result.data; |
| 6262 | goto store_val; |
| 6263 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6264 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6265 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 6266 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6267 | |
| 6268 | arg++; /* skip '+' */ |
| 6269 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6270 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6271 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6272 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6273 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6274 | val = arith_buf; |
| 6275 | break; |
| 6276 | } |
| 6277 | #endif |
| 6278 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6279 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6280 | IF_HUSH_TICK(store_val:) |
| 6281 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6282 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6283 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6284 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6285 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6286 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6287 | } |
| 6288 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6289 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6290 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6291 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6292 | } |
| 6293 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6294 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6295 | |
| 6296 | if (val && val[0]) { |
| 6297 | o_addQstr(output, val); |
| 6298 | } |
| 6299 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6300 | |
| 6301 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6302 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6303 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6304 | *p = SPECIAL_VAR_SYMBOL; |
| 6305 | |
| 6306 | #if ENABLE_HUSH_TICK |
| 6307 | o_free(&subst_result); |
| 6308 | #endif |
| 6309 | arg = ++p; |
| 6310 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6311 | |
| 6312 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6313 | if (ended_in_ifs) { |
| 6314 | o_addchr(output, '\0'); |
| 6315 | n = o_save_ptr(output, n); |
| 6316 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6317 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6318 | /* this part is literal, and it was already pre-quoted |
| 6319 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6320 | o_addstr_with_NUL(output, arg); |
| 6321 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6322 | } 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] | 6323 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6324 | ) { |
| 6325 | n--; |
| 6326 | /* allow to reuse list[n] later without re-growth */ |
| 6327 | output->has_empty_slot = 1; |
| 6328 | } else { |
| 6329 | o_addchr(output, '\0'); |
| 6330 | } |
| 6331 | |
| 6332 | return n; |
| 6333 | } |
| 6334 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6335 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6336 | { |
| 6337 | int n; |
| 6338 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6339 | o_string output = NULL_O_STRING; |
| 6340 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6341 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6342 | |
| 6343 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6344 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6345 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6346 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6347 | } |
| 6348 | debug_print_list("expand_variables", &output, n); |
| 6349 | |
| 6350 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6351 | list = o_finalize_list(&output, n); |
| 6352 | debug_print_strings("expand_variables[1]", list); |
| 6353 | return list; |
| 6354 | } |
| 6355 | |
| 6356 | static char **expand_strvec_to_strvec(char **argv) |
| 6357 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6358 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6359 | } |
| 6360 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6361 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6362 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6363 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6364 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6365 | } |
| 6366 | #endif |
| 6367 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6368 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6369 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6370 | * |
| 6371 | * NB: should NOT do globbing! |
| 6372 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6373 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6374 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6375 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6376 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6377 | const int do_unbackslash = 1; |
| 6378 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6379 | char *argv[2], **list; |
| 6380 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6381 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6382 | /* This is generally an optimization, but it also |
| 6383 | * handles "", which otherwise trips over !list[0] check below. |
| 6384 | * (is this ever happens that we actually get str="" here?) |
| 6385 | */ |
| 6386 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6387 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6388 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6389 | return xstrdup(str); |
| 6390 | } |
| 6391 | |
| 6392 | argv[0] = (char*)str; |
| 6393 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6394 | list = expand_variables(argv, do_unbackslash |
| 6395 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 6396 | : EXP_FLAG_SINGLEWORD |
| 6397 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6398 | if (HUSH_DEBUG) |
| 6399 | if (!list[0] || list[1]) |
| 6400 | bb_error_msg_and_die("BUG in varexp2"); |
| 6401 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6402 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6403 | if (do_unbackslash) |
| 6404 | unbackslash((char*)list); |
| 6405 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6406 | return (char*)list; |
| 6407 | } |
| 6408 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6409 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6410 | static char* expand_strvec_to_string(char **argv) |
| 6411 | { |
| 6412 | char **list; |
| 6413 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6414 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6415 | /* Convert all NULs to spaces */ |
| 6416 | if (list[0]) { |
| 6417 | int n = 1; |
| 6418 | while (list[n]) { |
| 6419 | if (HUSH_DEBUG) |
| 6420 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6421 | bb_error_msg_and_die("BUG in varexp3"); |
| 6422 | /* bash uses ' ' regardless of $IFS contents */ |
| 6423 | list[n][-1] = ' '; |
| 6424 | n++; |
| 6425 | } |
| 6426 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6427 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6428 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6429 | return (char*)list; |
| 6430 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6431 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6432 | |
| 6433 | static char **expand_assignments(char **argv, int count) |
| 6434 | { |
| 6435 | int i; |
| 6436 | char **p; |
| 6437 | |
| 6438 | G.expanded_assignments = p = NULL; |
| 6439 | /* Expand assignments into one string each */ |
| 6440 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6441 | 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] | 6442 | } |
| 6443 | G.expanded_assignments = NULL; |
| 6444 | return p; |
| 6445 | } |
| 6446 | |
| 6447 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6448 | static void switch_off_special_sigs(unsigned mask) |
| 6449 | { |
| 6450 | unsigned sig = 0; |
| 6451 | while ((mask >>= 1) != 0) { |
| 6452 | sig++; |
| 6453 | if (!(mask & 1)) |
| 6454 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6455 | #if ENABLE_HUSH_TRAP |
| 6456 | if (G_traps) { |
| 6457 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6458 | /* trap is '', has to remain SIG_IGN */ |
| 6459 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6460 | free(G_traps[sig]); |
| 6461 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6462 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6463 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6464 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6465 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6466 | } |
| 6467 | } |
| 6468 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6469 | #if BB_MMU |
| 6470 | /* never called */ |
| 6471 | void re_execute_shell(char ***to_free, const char *s, |
| 6472 | char *g_argv0, char **g_argv, |
| 6473 | char **builtin_argv) NORETURN; |
| 6474 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6475 | static void reset_traps_to_defaults(void) |
| 6476 | { |
| 6477 | /* This function is always called in a child shell |
| 6478 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6479 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6480 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6481 | unsigned mask; |
| 6482 | |
| 6483 | /* Child shells are not interactive. |
| 6484 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6485 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6486 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6487 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6488 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6489 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6490 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6491 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6492 | /* Switch off special sigs */ |
| 6493 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6494 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6495 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6496 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6497 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6498 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6499 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6500 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6501 | # if ENABLE_HUSH_TRAP |
| 6502 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6503 | return; |
| 6504 | |
| 6505 | /* Reset all sigs to default except ones with empty traps */ |
| 6506 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6507 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6508 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6509 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6510 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6511 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6512 | free(G_traps[sig]); |
| 6513 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6514 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6515 | if (sig == 0) |
| 6516 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6517 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6518 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6519 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | #else /* !BB_MMU */ |
| 6523 | |
| 6524 | static void re_execute_shell(char ***to_free, const char *s, |
| 6525 | char *g_argv0, char **g_argv, |
| 6526 | char **builtin_argv) NORETURN; |
| 6527 | static void re_execute_shell(char ***to_free, const char *s, |
| 6528 | char *g_argv0, char **g_argv, |
| 6529 | char **builtin_argv) |
| 6530 | { |
| 6531 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6532 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6533 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6534 | char *heredoc_argv[4]; |
| 6535 | struct variable *cur; |
| 6536 | # if ENABLE_HUSH_FUNCTIONS |
| 6537 | struct function *funcp; |
| 6538 | # endif |
| 6539 | char **argv, **pp; |
| 6540 | unsigned cnt; |
| 6541 | unsigned long long empty_trap_mask; |
| 6542 | |
| 6543 | if (!g_argv0) { /* heredoc */ |
| 6544 | argv = heredoc_argv; |
| 6545 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6546 | argv[1] = (char *) "-<"; |
| 6547 | argv[2] = (char *) s; |
| 6548 | argv[3] = NULL; |
| 6549 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6550 | goto do_exec; |
| 6551 | } |
| 6552 | |
| 6553 | cnt = 0; |
| 6554 | pp = builtin_argv; |
| 6555 | if (pp) while (*pp++) |
| 6556 | cnt++; |
| 6557 | |
| 6558 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6559 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6560 | int sig; |
| 6561 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6562 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6563 | empty_trap_mask |= 1LL << sig; |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6568 | , (unsigned) G.root_pid |
| 6569 | , (unsigned) G.root_ppid |
| 6570 | , (unsigned) G.last_bg_pid |
| 6571 | , (unsigned) G.last_exitcode |
| 6572 | , cnt |
| 6573 | , empty_trap_mask |
| 6574 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6575 | ); |
| 6576 | # undef NOMMU_HACK_FMT |
| 6577 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6578 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6579 | */ |
| 6580 | cnt += 6; |
| 6581 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6582 | if (!cur->flg_export || cur->flg_read_only) |
| 6583 | cnt += 2; |
| 6584 | } |
| 6585 | # if ENABLE_HUSH_FUNCTIONS |
| 6586 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6587 | cnt += 3; |
| 6588 | # endif |
| 6589 | pp = g_argv; |
| 6590 | while (*pp++) |
| 6591 | cnt++; |
| 6592 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6593 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6594 | *pp++ = param_buf; |
| 6595 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6596 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6597 | continue; |
| 6598 | if (cur->flg_read_only) { |
| 6599 | *pp++ = (char *) "-R"; |
| 6600 | *pp++ = cur->varstr; |
| 6601 | } else if (!cur->flg_export) { |
| 6602 | *pp++ = (char *) "-V"; |
| 6603 | *pp++ = cur->varstr; |
| 6604 | } |
| 6605 | } |
| 6606 | # if ENABLE_HUSH_FUNCTIONS |
| 6607 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6608 | *pp++ = (char *) "-F"; |
| 6609 | *pp++ = funcp->name; |
| 6610 | *pp++ = funcp->body_as_string; |
| 6611 | } |
| 6612 | # endif |
| 6613 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6614 | * |
| 6615 | * However, POSIX says that subshells reset signals with traps |
| 6616 | * to SIG_DFL. |
| 6617 | * I tested bash-3.2 and it not only does that with true subshells |
| 6618 | * of the form ( list ), but with any forked children shells. |
| 6619 | * I set trap "echo W" WINCH; and then tried: |
| 6620 | * |
| 6621 | * { echo 1; sleep 20; echo 2; } & |
| 6622 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6623 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6624 | * |
| 6625 | * In all these cases sending SIGWINCH to the child shell |
| 6626 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6627 | * _inside_ group (just before echo 1), it works. |
| 6628 | * |
| 6629 | * 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] | 6630 | */ |
| 6631 | *pp++ = (char *) "-c"; |
| 6632 | *pp++ = (char *) s; |
| 6633 | if (builtin_argv) { |
| 6634 | while (*++builtin_argv) |
| 6635 | *pp++ = *builtin_argv; |
| 6636 | *pp++ = (char *) ""; |
| 6637 | } |
| 6638 | *pp++ = g_argv0; |
| 6639 | while (*g_argv) |
| 6640 | *pp++ = *g_argv++; |
| 6641 | /* *pp = NULL; - is already there */ |
| 6642 | pp = environ; |
| 6643 | |
| 6644 | do_exec: |
| 6645 | 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] | 6646 | /* Don't propagate SIG_IGN to the child */ |
| 6647 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6648 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6649 | execve(bb_busybox_exec_path, argv, pp); |
| 6650 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6651 | if (argv[0][0] == '/') |
| 6652 | execve(argv[0], argv, pp); |
| 6653 | xfunc_error_retval = 127; |
| 6654 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6655 | } |
| 6656 | #endif /* !BB_MMU */ |
| 6657 | |
| 6658 | |
| 6659 | static int run_and_free_list(struct pipe *pi); |
| 6660 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6661 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6662 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6663 | * end_trigger controls how often we stop parsing |
| 6664 | * NUL: parse all, execute, return |
| 6665 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6666 | */ |
| 6667 | 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] | 6668 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6669 | /* Why we need empty flag? |
| 6670 | * An obscure corner case "false; ``; echo $?": |
| 6671 | * empty command in `` should still set $? to 0. |
| 6672 | * But we can't just set $? to 0 at the start, |
| 6673 | * this breaks "false; echo `echo $?`" case. |
| 6674 | */ |
| 6675 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6676 | while (1) { |
| 6677 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6678 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6679 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 6680 | if (end_trigger == ';') { |
| 6681 | G.promptmode = 0; /* PS1 */ |
| 6682 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 6683 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6684 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6685 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6686 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6687 | /* If we are in "big" script |
| 6688 | * (not in `cmd` or something similar)... |
| 6689 | */ |
| 6690 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6691 | /* Discard cached input (rest of line) */ |
| 6692 | int ch = inp->last_char; |
| 6693 | while (ch != EOF && ch != '\n') { |
| 6694 | //bb_error_msg("Discarded:'%c'", ch); |
| 6695 | ch = i_getch(inp); |
| 6696 | } |
| 6697 | /* Force prompt */ |
| 6698 | inp->p = NULL; |
| 6699 | /* This stream isn't empty */ |
| 6700 | empty = 0; |
| 6701 | continue; |
| 6702 | } |
| 6703 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6704 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6705 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6706 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6707 | debug_print_tree(pipe_list, 0); |
| 6708 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6709 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6710 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6711 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6712 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6713 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6716 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6717 | { |
| 6718 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6719 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 6720 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6721 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6722 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6723 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6726 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6727 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6728 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6729 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 6730 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6731 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 6732 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6733 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6734 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6735 | } |
| 6736 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6737 | #if ENABLE_HUSH_TICK |
| 6738 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6739 | { |
| 6740 | pid_t pid; |
| 6741 | int channel[2]; |
| 6742 | # if !BB_MMU |
| 6743 | char **to_free = NULL; |
| 6744 | # endif |
| 6745 | |
| 6746 | xpipe(channel); |
| 6747 | pid = BB_MMU ? xfork() : xvfork(); |
| 6748 | if (pid == 0) { /* child */ |
| 6749 | disable_restore_tty_pgrp_on_exit(); |
| 6750 | /* Process substitution is not considered to be usual |
| 6751 | * 'command execution'. |
| 6752 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6753 | */ |
| 6754 | bb_signals(0 |
| 6755 | + (1 << SIGTSTP) |
| 6756 | + (1 << SIGTTIN) |
| 6757 | + (1 << SIGTTOU) |
| 6758 | , SIG_IGN); |
| 6759 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6760 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6761 | xmove_fd(channel[1], 1); |
| 6762 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6763 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6764 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6765 | /* Awful hack for `trap` or $(trap). |
| 6766 | * |
| 6767 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6768 | * contains an example where "trap" is executed in a subshell: |
| 6769 | * |
| 6770 | * save_traps=$(trap) |
| 6771 | * ... |
| 6772 | * eval "$save_traps" |
| 6773 | * |
| 6774 | * Standard does not say that "trap" in subshell shall print |
| 6775 | * parent shell's traps. It only says that its output |
| 6776 | * must have suitable form, but then, in the above example |
| 6777 | * (which is not supposed to be normative), it implies that. |
| 6778 | * |
| 6779 | * bash (and probably other shell) does implement it |
| 6780 | * (traps are reset to defaults, but "trap" still shows them), |
| 6781 | * but as a result, "trap" logic is hopelessly messed up: |
| 6782 | * |
| 6783 | * # trap |
| 6784 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6785 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6786 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6787 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6788 | * trap -- 'echo Ho' SIGWINCH |
| 6789 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6790 | * trap -- 'echo Ho' SIGWINCH |
| 6791 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6792 | * trap -- 'echo Ho' SIGWINCH |
| 6793 | * |
| 6794 | * The rules when to forget and when to not forget traps |
| 6795 | * get really complex and nonsensical. |
| 6796 | * |
| 6797 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6798 | */ |
| 6799 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6800 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6801 | && skip_whitespace(s + 4)[0] == '\0' |
| 6802 | ) { |
| 6803 | static const char *const argv[] = { NULL, NULL }; |
| 6804 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6805 | fflush_all(); /* important */ |
| 6806 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6807 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6808 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6809 | # if BB_MMU |
| 6810 | reset_traps_to_defaults(); |
| 6811 | parse_and_run_string(s); |
| 6812 | _exit(G.last_exitcode); |
| 6813 | # else |
| 6814 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6815 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6816 | * huge=`cat BIG` # was blocking here forever |
| 6817 | * echo OK |
| 6818 | */ |
| 6819 | re_execute_shell(&to_free, |
| 6820 | s, |
| 6821 | G.global_argv[0], |
| 6822 | G.global_argv + 1, |
| 6823 | NULL); |
| 6824 | # endif |
| 6825 | } |
| 6826 | |
| 6827 | /* parent */ |
| 6828 | *pid_p = pid; |
| 6829 | # if ENABLE_HUSH_FAST |
| 6830 | G.count_SIGCHLD++; |
| 6831 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6832 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6833 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6834 | # endif |
| 6835 | enable_restore_tty_pgrp_on_exit(); |
| 6836 | # if !BB_MMU |
| 6837 | free(to_free); |
| 6838 | # endif |
| 6839 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6840 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /* Return code is exit status of the process that is run. */ |
| 6844 | static int process_command_subs(o_string *dest, const char *s) |
| 6845 | { |
| 6846 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6847 | pid_t pid; |
| 6848 | int status, ch, eol_cnt; |
| 6849 | |
| 6850 | fp = generate_stream_from_string(s, &pid); |
| 6851 | |
| 6852 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6853 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6854 | while ((ch = getc(fp)) != EOF) { |
| 6855 | if (ch == '\0') |
| 6856 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6857 | if (ch == '\n') { |
| 6858 | eol_cnt++; |
| 6859 | continue; |
| 6860 | } |
| 6861 | while (eol_cnt) { |
| 6862 | o_addchr(dest, '\n'); |
| 6863 | eol_cnt--; |
| 6864 | } |
| 6865 | o_addQchr(dest, ch); |
| 6866 | } |
| 6867 | |
| 6868 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6869 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6870 | /* We need to extract exitcode. Test case |
| 6871 | * "true; echo `sleep 1; false` $?" |
| 6872 | * should print 1 */ |
| 6873 | safe_waitpid(pid, &status, 0); |
| 6874 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6875 | return WEXITSTATUS(status); |
| 6876 | } |
| 6877 | #endif /* ENABLE_HUSH_TICK */ |
| 6878 | |
| 6879 | |
| 6880 | static void setup_heredoc(struct redir_struct *redir) |
| 6881 | { |
| 6882 | struct fd_pair pair; |
| 6883 | pid_t pid; |
| 6884 | int len, written; |
| 6885 | /* the _body_ of heredoc (misleading field name) */ |
| 6886 | const char *heredoc = redir->rd_filename; |
| 6887 | char *expanded; |
| 6888 | #if !BB_MMU |
| 6889 | char **to_free; |
| 6890 | #endif |
| 6891 | |
| 6892 | expanded = NULL; |
| 6893 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6894 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6895 | if (expanded) |
| 6896 | heredoc = expanded; |
| 6897 | } |
| 6898 | len = strlen(heredoc); |
| 6899 | |
| 6900 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6901 | xpiped_pair(pair); |
| 6902 | xmove_fd(pair.rd, redir->rd_fd); |
| 6903 | |
| 6904 | /* Try writing without forking. Newer kernels have |
| 6905 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6906 | ndelay_on(pair.wr); |
| 6907 | while (1) { |
| 6908 | written = write(pair.wr, heredoc, len); |
| 6909 | if (written <= 0) |
| 6910 | break; |
| 6911 | len -= written; |
| 6912 | if (len == 0) { |
| 6913 | close(pair.wr); |
| 6914 | free(expanded); |
| 6915 | return; |
| 6916 | } |
| 6917 | heredoc += written; |
| 6918 | } |
| 6919 | ndelay_off(pair.wr); |
| 6920 | |
| 6921 | /* Okay, pipe buffer was not big enough */ |
| 6922 | /* Note: we must not create a stray child (bastard? :) |
| 6923 | * for the unsuspecting parent process. Child creates a grandchild |
| 6924 | * and exits before parent execs the process which consumes heredoc |
| 6925 | * (that exec happens after we return from this function) */ |
| 6926 | #if !BB_MMU |
| 6927 | to_free = NULL; |
| 6928 | #endif |
| 6929 | pid = xvfork(); |
| 6930 | if (pid == 0) { |
| 6931 | /* child */ |
| 6932 | disable_restore_tty_pgrp_on_exit(); |
| 6933 | pid = BB_MMU ? xfork() : xvfork(); |
| 6934 | if (pid != 0) |
| 6935 | _exit(0); |
| 6936 | /* grandchild */ |
| 6937 | close(redir->rd_fd); /* read side of the pipe */ |
| 6938 | #if BB_MMU |
| 6939 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6940 | _exit(0); |
| 6941 | #else |
| 6942 | /* Delegate blocking writes to another process */ |
| 6943 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6944 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6945 | #endif |
| 6946 | } |
| 6947 | /* parent */ |
| 6948 | #if ENABLE_HUSH_FAST |
| 6949 | G.count_SIGCHLD++; |
| 6950 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6951 | #endif |
| 6952 | enable_restore_tty_pgrp_on_exit(); |
| 6953 | #if !BB_MMU |
| 6954 | free(to_free); |
| 6955 | #endif |
| 6956 | close(pair.wr); |
| 6957 | free(expanded); |
| 6958 | wait(NULL); /* wait till child has died */ |
| 6959 | } |
| 6960 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6961 | struct squirrel { |
| 6962 | int orig_fd; |
| 6963 | int moved_to; |
| 6964 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 6965 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 6966 | }; |
| 6967 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6968 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 6969 | { |
| 6970 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 6971 | sq[i].orig_fd = orig; |
| 6972 | sq[i].moved_to = moved; |
| 6973 | sq[i+1].orig_fd = -1; /* end marker */ |
| 6974 | return sq; |
| 6975 | } |
| 6976 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6977 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 6978 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6979 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6980 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6981 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 6982 | i = 0; |
| 6983 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6984 | /* If we collide with an already moved fd... */ |
| 6985 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 6986 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6987 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 6988 | if (sq[i].moved_to < 0) /* what? */ |
| 6989 | xfunc_die(); |
| 6990 | return sq; |
| 6991 | } |
| 6992 | if (fd == sq[i].orig_fd) { |
| 6993 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 6994 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 6995 | return sq; |
| 6996 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6997 | } |
| 6998 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6999 | /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7000 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7001 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7002 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7003 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7004 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7005 | } |
| 7006 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7007 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7008 | { |
| 7009 | int i; |
| 7010 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7011 | i = 0; |
| 7012 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7013 | /* If we collide with an already moved fd... */ |
| 7014 | if (fd == sq[i].orig_fd) { |
| 7015 | /* Examples: |
| 7016 | * "echo 3>FILE 3>&- 3>FILE" |
| 7017 | * "echo 3>&- 3>FILE" |
| 7018 | * No need for last redirect to insert |
| 7019 | * another "need to close 3" indicator. |
| 7020 | */ |
| 7021 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7022 | return sq; |
| 7023 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7024 | } |
| 7025 | |
| 7026 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7027 | return append_squirrel(sq, i, fd, -1); |
| 7028 | } |
| 7029 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7030 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7031 | * Move all conflicting internally used fds, |
| 7032 | * and remember them so that we can restore them later. |
| 7033 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7034 | static int save_fd_on_redirect(int fd, int avoid_fd, struct squirrel **sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7035 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7036 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7037 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7038 | |
| 7039 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7040 | if (fd == G.interactive_fd) { |
| 7041 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7042 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7043 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7044 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7045 | } |
| 7046 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7047 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 7048 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 7049 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7050 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 7051 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7052 | * "fileno(fd) = new_fd" can't be done. |
| 7053 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7054 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7055 | return 0; |
| 7056 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7057 | /* If this one of script's fds? */ |
| 7058 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 7059 | return 1; /* yes. "we closed fd" */ |
| 7060 | |
| 7061 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7062 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7063 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7064 | } |
| 7065 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7066 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7067 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7068 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7069 | int i; |
| 7070 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7071 | if (sq[i].moved_to >= 0) { |
| 7072 | /* We simply die on error */ |
| 7073 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7074 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7075 | } else { |
| 7076 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7077 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7078 | close(sq[i].orig_fd); |
| 7079 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7080 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7081 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7082 | } |
| 7083 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7084 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7085 | |
| 7086 | restore_redirected_FILEs(); |
| 7087 | } |
| 7088 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7089 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7090 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7091 | { |
| 7092 | if (G_interactive_fd) |
| 7093 | close(G_interactive_fd); |
| 7094 | close_all_FILE_list(); |
| 7095 | } |
| 7096 | #endif |
| 7097 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7098 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7099 | { |
| 7100 | int i; |
| 7101 | |
| 7102 | #if ENABLE_HUSH_INTERACTIVE |
| 7103 | if (fd == G.interactive_fd) |
| 7104 | return 1; |
| 7105 | #endif |
| 7106 | /* If this one of script's fds? */ |
| 7107 | if (fd_in_FILEs(fd)) |
| 7108 | return 1; |
| 7109 | |
| 7110 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7111 | if (fd == sq[i].moved_to) |
| 7112 | return 1; |
| 7113 | } |
| 7114 | return 0; |
| 7115 | } |
| 7116 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7117 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7118 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7119 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7120 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7121 | struct redir_struct *redir; |
| 7122 | |
| 7123 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7124 | int newfd; |
| 7125 | int closed; |
| 7126 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7127 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7128 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7129 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7130 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7131 | * of the heredoc */ |
| 7132 | debug_printf_parse("set heredoc '%s'\n", |
| 7133 | redir->rd_filename); |
| 7134 | setup_heredoc(redir); |
| 7135 | continue; |
| 7136 | } |
| 7137 | |
| 7138 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7139 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7140 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7141 | int mode; |
| 7142 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7143 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7144 | /* |
| 7145 | * Examples: |
| 7146 | * "cmd >" (no filename) |
| 7147 | * "cmd > <file" (2nd redirect starts too early) |
| 7148 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7149 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7150 | continue; |
| 7151 | } |
| 7152 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7153 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7154 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7155 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7156 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7157 | /* Error message from open_or_warn can be lost |
| 7158 | * if stderr has been redirected, but bash |
| 7159 | * and ash both lose it as well |
| 7160 | * (though zsh doesn't!) |
| 7161 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7162 | return 1; |
| 7163 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7164 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7165 | /* open() gave us precisely the fd we wanted. |
| 7166 | * This means that this fd was not busy |
| 7167 | * (not opened to anywhere). |
| 7168 | * Remember to close it on restore: |
| 7169 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7170 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7171 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7172 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7173 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7174 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7175 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7176 | } |
| 7177 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7178 | if (newfd == redir->rd_fd) |
| 7179 | continue; |
| 7180 | |
| 7181 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7182 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7183 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7184 | |
| 7185 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7186 | if (newfd == REDIRFD_CLOSE) { |
| 7187 | /* "N>&-" means "close me" */ |
| 7188 | if (!closed) { |
| 7189 | /* ^^^ optimization: saving may already |
| 7190 | * have closed it. If not... */ |
| 7191 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7192 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7193 | /* Sometimes we do another close on restore, getting EBADF. |
| 7194 | * Consider "echo 3>FILE 3>&-" |
| 7195 | * first redirect remembers "need to close 3", |
| 7196 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7197 | */ |
| 7198 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7199 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7200 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7201 | //errno = EBADF; |
| 7202 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7203 | newfd = -1; /* same effect as code above */ |
| 7204 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7205 | xdup2(newfd, redir->rd_fd); |
| 7206 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7207 | /* "rd_fd > FILE" */ |
| 7208 | close(newfd); |
| 7209 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7210 | } |
| 7211 | } |
| 7212 | return 0; |
| 7213 | } |
| 7214 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7215 | static char *find_in_path(const char *arg) |
| 7216 | { |
| 7217 | char *ret = NULL; |
| 7218 | const char *PATH = get_local_var_value("PATH"); |
| 7219 | |
| 7220 | if (!PATH) |
| 7221 | return NULL; |
| 7222 | |
| 7223 | while (1) { |
| 7224 | const char *end = strchrnul(PATH, ':'); |
| 7225 | int sz = end - PATH; /* must be int! */ |
| 7226 | |
| 7227 | free(ret); |
| 7228 | if (sz != 0) { |
| 7229 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7230 | } else { |
| 7231 | /* We have xxx::yyyy in $PATH, |
| 7232 | * it means "use current dir" */ |
| 7233 | ret = xstrdup(arg); |
| 7234 | } |
| 7235 | if (access(ret, F_OK) == 0) |
| 7236 | break; |
| 7237 | |
| 7238 | if (*end == '\0') { |
| 7239 | free(ret); |
| 7240 | return NULL; |
| 7241 | } |
| 7242 | PATH = end + 1; |
| 7243 | } |
| 7244 | |
| 7245 | return ret; |
| 7246 | } |
| 7247 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7248 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7249 | const struct built_in_command *x, |
| 7250 | const struct built_in_command *end) |
| 7251 | { |
| 7252 | while (x != end) { |
| 7253 | if (strcmp(name, x->b_cmd) != 0) { |
| 7254 | x++; |
| 7255 | continue; |
| 7256 | } |
| 7257 | debug_printf_exec("found builtin '%s'\n", name); |
| 7258 | return x; |
| 7259 | } |
| 7260 | return NULL; |
| 7261 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7262 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7263 | { |
| 7264 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7265 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7266 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7267 | { |
| 7268 | const struct built_in_command *x = find_builtin1(name); |
| 7269 | if (x) |
| 7270 | return x; |
| 7271 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7272 | } |
| 7273 | |
| 7274 | #if ENABLE_HUSH_FUNCTIONS |
| 7275 | static struct function **find_function_slot(const char *name) |
| 7276 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7277 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7278 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7279 | |
| 7280 | while ((funcp = *funcpp) != NULL) { |
| 7281 | if (strcmp(name, funcp->name) == 0) { |
| 7282 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7283 | break; |
| 7284 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7285 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7286 | } |
| 7287 | return funcpp; |
| 7288 | } |
| 7289 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7290 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7291 | { |
| 7292 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7293 | return funcp; |
| 7294 | } |
| 7295 | |
| 7296 | /* Note: takes ownership on name ptr */ |
| 7297 | static struct function *new_function(char *name) |
| 7298 | { |
| 7299 | struct function **funcpp = find_function_slot(name); |
| 7300 | struct function *funcp = *funcpp; |
| 7301 | |
| 7302 | if (funcp != NULL) { |
| 7303 | struct command *cmd = funcp->parent_cmd; |
| 7304 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7305 | if (!cmd) { |
| 7306 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7307 | free(funcp->name); |
| 7308 | /* Note: if !funcp->body, do not free body_as_string! |
| 7309 | * This is a special case of "-F name body" function: |
| 7310 | * body_as_string was not malloced! */ |
| 7311 | if (funcp->body) { |
| 7312 | free_pipe_list(funcp->body); |
| 7313 | # if !BB_MMU |
| 7314 | free(funcp->body_as_string); |
| 7315 | # endif |
| 7316 | } |
| 7317 | } else { |
| 7318 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7319 | cmd->argv[0] = funcp->name; |
| 7320 | cmd->group = funcp->body; |
| 7321 | # if !BB_MMU |
| 7322 | cmd->group_as_string = funcp->body_as_string; |
| 7323 | # endif |
| 7324 | } |
| 7325 | } else { |
| 7326 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7327 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7328 | /*funcp->next = NULL;*/ |
| 7329 | } |
| 7330 | |
| 7331 | funcp->name = name; |
| 7332 | return funcp; |
| 7333 | } |
| 7334 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7335 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7336 | static void unset_func(const char *name) |
| 7337 | { |
| 7338 | struct function **funcpp = find_function_slot(name); |
| 7339 | struct function *funcp = *funcpp; |
| 7340 | |
| 7341 | if (funcp != NULL) { |
| 7342 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7343 | *funcpp = funcp->next; |
| 7344 | /* funcp is unlinked now, deleting it. |
| 7345 | * Note: if !funcp->body, the function was created by |
| 7346 | * "-F name body", do not free ->body_as_string |
| 7347 | * and ->name as they were not malloced. */ |
| 7348 | if (funcp->body) { |
| 7349 | free_pipe_list(funcp->body); |
| 7350 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7351 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7352 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7353 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7354 | } |
| 7355 | free(funcp); |
| 7356 | } |
| 7357 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7358 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7359 | |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7360 | static void remove_nested_vars(void) |
| 7361 | { |
| 7362 | struct variable *cur; |
| 7363 | struct variable **cur_pp; |
| 7364 | |
| 7365 | cur_pp = &G.top_var; |
| 7366 | while ((cur = *cur_pp) != NULL) { |
| 7367 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7368 | cur_pp = &cur->next; |
| 7369 | continue; |
| 7370 | } |
| 7371 | /* Unexport */ |
| 7372 | if (cur->flg_export) { |
| 7373 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7374 | bb_unsetenv(cur->varstr); |
| 7375 | } |
| 7376 | /* Remove from global list */ |
| 7377 | *cur_pp = cur->next; |
| 7378 | /* Free */ |
| 7379 | if (!cur->max_len) { |
| 7380 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7381 | free(cur->varstr); |
| 7382 | } |
| 7383 | free(cur); |
| 7384 | } |
| 7385 | } |
| 7386 | |
| 7387 | static void enter_var_nest_level(void) |
| 7388 | { |
| 7389 | G.var_nest_level++; |
| 7390 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7391 | |
| 7392 | /* Try: f() { echo -n .; f; }; f |
| 7393 | * struct variable::var_nest_level is uint16_t, |
| 7394 | * thus limiting recursion to < 2^16. |
| 7395 | * In any case, with 8 Mbyte stack SEGV happens |
| 7396 | * not too long after 2^16 recursions anyway. |
| 7397 | */ |
| 7398 | if (G.var_nest_level > 0xff00) |
| 7399 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7400 | } |
| 7401 | |
| 7402 | static void leave_var_nest_level(void) |
| 7403 | { |
| 7404 | G.var_nest_level--; |
| 7405 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7406 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7407 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7408 | |
| 7409 | remove_nested_vars(); |
| 7410 | } |
| 7411 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7412 | # if BB_MMU |
| 7413 | #define exec_function(to_free, funcp, argv) \ |
| 7414 | exec_function(funcp, argv) |
| 7415 | # endif |
| 7416 | static void exec_function(char ***to_free, |
| 7417 | const struct function *funcp, |
| 7418 | char **argv) NORETURN; |
| 7419 | static void exec_function(char ***to_free, |
| 7420 | const struct function *funcp, |
| 7421 | char **argv) |
| 7422 | { |
| 7423 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7424 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7425 | |
| 7426 | argv[0] = G.global_argv[0]; |
| 7427 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7428 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7429 | |
| 7430 | // Example when we are here: "cmd | func" |
| 7431 | // func will run with saved-redirect fds open. |
| 7432 | // $ f() { echo /proc/self/fd/*; } |
| 7433 | // $ true | f |
| 7434 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7435 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7436 | // Same in script: |
| 7437 | // $ . ./SCRIPT |
| 7438 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7439 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7440 | // They are CLOEXEC so external programs won't see them, but |
| 7441 | // for "more correctness" we might want to close those extra fds here: |
| 7442 | //? close_saved_fds_and_FILE_fds(); |
| 7443 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7444 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7445 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7446 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7447 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7448 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7449 | /* On MMU, funcp->body is always non-NULL */ |
| 7450 | n = run_list(funcp->body); |
| 7451 | fflush_all(); |
| 7452 | _exit(n); |
| 7453 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7454 | //? close_saved_fds_and_FILE_fds(); |
| 7455 | |
| 7456 | //TODO: check whether "true | func_with_return" works |
| 7457 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7458 | re_execute_shell(to_free, |
| 7459 | funcp->body_as_string, |
| 7460 | G.global_argv[0], |
| 7461 | argv + 1, |
| 7462 | NULL); |
| 7463 | # endif |
| 7464 | } |
| 7465 | |
| 7466 | static int run_function(const struct function *funcp, char **argv) |
| 7467 | { |
| 7468 | int rc; |
| 7469 | save_arg_t sv; |
| 7470 | smallint sv_flg; |
| 7471 | |
| 7472 | save_and_replace_G_args(&sv, argv); |
| 7473 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7474 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7475 | sv_flg = G_flag_return_in_progress; |
| 7476 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7477 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7478 | /* Make "local" variables properly shadow previous ones */ |
| 7479 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7480 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7481 | |
| 7482 | /* On MMU, funcp->body is always non-NULL */ |
| 7483 | # if !BB_MMU |
| 7484 | if (!funcp->body) { |
| 7485 | /* Function defined by -F */ |
| 7486 | parse_and_run_string(funcp->body_as_string); |
| 7487 | rc = G.last_exitcode; |
| 7488 | } else |
| 7489 | # endif |
| 7490 | { |
| 7491 | rc = run_list(funcp->body); |
| 7492 | } |
| 7493 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7494 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7495 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7496 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7497 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7498 | |
| 7499 | restore_G_args(&sv, argv); |
| 7500 | |
| 7501 | return rc; |
| 7502 | } |
| 7503 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7504 | |
| 7505 | |
| 7506 | #if BB_MMU |
| 7507 | #define exec_builtin(to_free, x, argv) \ |
| 7508 | exec_builtin(x, argv) |
| 7509 | #else |
| 7510 | #define exec_builtin(to_free, x, argv) \ |
| 7511 | exec_builtin(to_free, argv) |
| 7512 | #endif |
| 7513 | static void exec_builtin(char ***to_free, |
| 7514 | const struct built_in_command *x, |
| 7515 | char **argv) NORETURN; |
| 7516 | static void exec_builtin(char ***to_free, |
| 7517 | const struct built_in_command *x, |
| 7518 | char **argv) |
| 7519 | { |
| 7520 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7521 | int rcode; |
| 7522 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7523 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7524 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7525 | fflush_all(); |
| 7526 | _exit(rcode); |
| 7527 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7528 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7529 | /* On NOMMU, we must never block! |
| 7530 | * Example: { sleep 99 | read line; } & echo Ok |
| 7531 | */ |
| 7532 | re_execute_shell(to_free, |
| 7533 | argv[0], |
| 7534 | G.global_argv[0], |
| 7535 | G.global_argv + 1, |
| 7536 | argv); |
| 7537 | #endif |
| 7538 | } |
| 7539 | |
| 7540 | |
| 7541 | static void execvp_or_die(char **argv) NORETURN; |
| 7542 | static void execvp_or_die(char **argv) |
| 7543 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7544 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7545 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7546 | /* Don't propagate SIG_IGN to the child */ |
| 7547 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7548 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7549 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7550 | e = 2; |
| 7551 | if (errno == EACCES) e = 126; |
| 7552 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7553 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7554 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7555 | } |
| 7556 | |
| 7557 | #if ENABLE_HUSH_MODE_X |
| 7558 | static void dump_cmd_in_x_mode(char **argv) |
| 7559 | { |
| 7560 | if (G_x_mode && argv) { |
| 7561 | /* We want to output the line in one write op */ |
| 7562 | char *buf, *p; |
| 7563 | int len; |
| 7564 | int n; |
| 7565 | |
| 7566 | len = 3; |
| 7567 | n = 0; |
| 7568 | while (argv[n]) |
| 7569 | len += strlen(argv[n++]) + 1; |
| 7570 | buf = xmalloc(len); |
| 7571 | buf[0] = '+'; |
| 7572 | p = buf + 1; |
| 7573 | n = 0; |
| 7574 | while (argv[n]) |
| 7575 | p += sprintf(p, " %s", argv[n++]); |
| 7576 | *p++ = '\n'; |
| 7577 | *p = '\0'; |
| 7578 | fputs(buf, stderr); |
| 7579 | free(buf); |
| 7580 | } |
| 7581 | } |
| 7582 | #else |
| 7583 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7584 | #endif |
| 7585 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7586 | #if ENABLE_HUSH_COMMAND |
| 7587 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7588 | { |
| 7589 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7590 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7591 | if (!opt_vV) |
| 7592 | return; |
| 7593 | |
| 7594 | to_free = NULL; |
| 7595 | if (!explanation) { |
| 7596 | char *path = getenv("PATH"); |
| 7597 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7598 | if (!explanation) |
| 7599 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7600 | if (opt_vV != 'V') |
| 7601 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7602 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7603 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7604 | free(to_free); |
| 7605 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7606 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7607 | } |
| 7608 | #else |
| 7609 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7610 | #endif |
| 7611 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7612 | #if BB_MMU |
| 7613 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7614 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7615 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7616 | pseudo_exec(command, argv_expanded) |
| 7617 | #endif |
| 7618 | |
| 7619 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7620 | * Never returns. |
| 7621 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7622 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7623 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7624 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7625 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7626 | char **argv, int assignment_cnt, |
| 7627 | char **argv_expanded) NORETURN; |
| 7628 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7629 | char **argv, int assignment_cnt, |
| 7630 | char **argv_expanded) |
| 7631 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7632 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7633 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7634 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7635 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 7636 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7637 | |
| 7638 | new_env = expand_assignments(argv, assignment_cnt); |
| 7639 | dump_cmd_in_x_mode(new_env); |
| 7640 | |
| 7641 | if (!argv[assignment_cnt]) { |
| 7642 | /* Case when we are here: ... | var=val | ... |
| 7643 | * (note that we do not exit early, i.e., do not optimize out |
| 7644 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7645 | */ |
| 7646 | free_strings(new_env); |
| 7647 | _exit(EXIT_SUCCESS); |
| 7648 | } |
| 7649 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7650 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7651 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7652 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7653 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7654 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7655 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7656 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7657 | set_vars_and_save_old(new_env); |
| 7658 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7659 | |
| 7660 | if (argv_expanded) { |
| 7661 | argv = argv_expanded; |
| 7662 | } else { |
| 7663 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7664 | #if !BB_MMU |
| 7665 | nommu_save->argv = argv; |
| 7666 | #endif |
| 7667 | } |
| 7668 | dump_cmd_in_x_mode(argv); |
| 7669 | |
| 7670 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7671 | if (strchr(argv[0], '/') != NULL) |
| 7672 | goto skip; |
| 7673 | #endif |
| 7674 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7675 | #if ENABLE_HUSH_FUNCTIONS |
| 7676 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7677 | funcp = find_function(argv[0]); |
| 7678 | if (funcp) |
| 7679 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7680 | #endif |
| 7681 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7682 | #if ENABLE_HUSH_COMMAND |
| 7683 | /* "command BAR": run BAR without looking it up among functions |
| 7684 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 7685 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 7686 | */ |
| 7687 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 7688 | char *p; |
| 7689 | |
| 7690 | argv++; |
| 7691 | p = *argv; |
| 7692 | if (p[0] != '-' || !p[1]) |
| 7693 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 7694 | |
| 7695 | for (;;) { |
| 7696 | p++; |
| 7697 | switch (*p) { |
| 7698 | case '\0': |
| 7699 | argv++; |
| 7700 | p = *argv; |
| 7701 | if (p[0] != '-' || !p[1]) |
| 7702 | goto after_opts; |
| 7703 | continue; /* next arg is also -opts, process it too */ |
| 7704 | case 'v': |
| 7705 | case 'V': |
| 7706 | opt_vV = *p; |
| 7707 | continue; |
| 7708 | default: |
| 7709 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 7710 | } |
| 7711 | } |
| 7712 | } |
| 7713 | after_opts: |
| 7714 | # if ENABLE_HUSH_FUNCTIONS |
| 7715 | if (opt_vV && find_function(argv[0])) |
| 7716 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 7717 | # endif |
| 7718 | #endif |
| 7719 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7720 | /* Check if the command matches any of the builtins. |
| 7721 | * Depending on context, this might be redundant. But it's |
| 7722 | * easier to waste a few CPU cycles than it is to figure out |
| 7723 | * if this is one of those cases. |
| 7724 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7725 | /* Why "BB_MMU ? :" difference in logic? - |
| 7726 | * On NOMMU, it is more expensive to re-execute shell |
| 7727 | * just in order to run echo or test builtin. |
| 7728 | * It's better to skip it here and run corresponding |
| 7729 | * non-builtin later. */ |
| 7730 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7731 | if (x) { |
| 7732 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 7733 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7734 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7735 | |
| 7736 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7737 | /* Check if the command matches any busybox applets */ |
| 7738 | { |
| 7739 | int a = find_applet_by_name(argv[0]); |
| 7740 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7741 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7742 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7743 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7744 | /* Do not leak open fds from opened script files etc. |
| 7745 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7746 | * should not show tty fd open. |
| 7747 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7748 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7749 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7750 | //This casuses test failures in |
| 7751 | //redir_children_should_not_see_saved_fd_2.tests |
| 7752 | //redir_children_should_not_see_saved_fd_3.tests |
| 7753 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7754 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7755 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7756 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7757 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7758 | } |
| 7759 | # endif |
| 7760 | /* Re-exec ourselves */ |
| 7761 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7762 | /* Don't propagate SIG_IGN to the child */ |
| 7763 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7764 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7765 | execv(bb_busybox_exec_path, argv); |
| 7766 | /* If they called chroot or otherwise made the binary no longer |
| 7767 | * executable, fall through */ |
| 7768 | } |
| 7769 | } |
| 7770 | #endif |
| 7771 | |
| 7772 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7773 | skip: |
| 7774 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7775 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7776 | execvp_or_die(argv); |
| 7777 | } |
| 7778 | |
| 7779 | /* Called after [v]fork() in run_pipe |
| 7780 | */ |
| 7781 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7782 | struct command *command, |
| 7783 | char **argv_expanded) NORETURN; |
| 7784 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7785 | struct command *command, |
| 7786 | char **argv_expanded) |
| 7787 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 7788 | #if ENABLE_HUSH_FUNCTIONS |
| 7789 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7790 | /* Ignore funcdefs in pipes: |
| 7791 | * true | f() { cmd } |
| 7792 | */ |
| 7793 | _exit(0); |
| 7794 | } |
| 7795 | #endif |
| 7796 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7797 | if (command->argv) { |
| 7798 | pseudo_exec_argv(nommu_save, command->argv, |
| 7799 | command->assignment_cnt, argv_expanded); |
| 7800 | } |
| 7801 | |
| 7802 | if (command->group) { |
| 7803 | /* Cases when we are here: |
| 7804 | * ( list ) |
| 7805 | * { list } & |
| 7806 | * ... | ( list ) | ... |
| 7807 | * ... | { list } | ... |
| 7808 | */ |
| 7809 | #if BB_MMU |
| 7810 | int rcode; |
| 7811 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7812 | reset_traps_to_defaults(); |
| 7813 | rcode = run_list(command->group); |
| 7814 | /* OK to leak memory by not calling free_pipe_list, |
| 7815 | * since this process is about to exit */ |
| 7816 | _exit(rcode); |
| 7817 | #else |
| 7818 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7819 | command->group_as_string, |
| 7820 | G.global_argv[0], |
| 7821 | G.global_argv + 1, |
| 7822 | NULL); |
| 7823 | #endif |
| 7824 | } |
| 7825 | |
| 7826 | /* Case when we are here: ... | >file */ |
| 7827 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7828 | _exit(EXIT_SUCCESS); |
| 7829 | } |
| 7830 | |
| 7831 | #if ENABLE_HUSH_JOB |
| 7832 | static const char *get_cmdtext(struct pipe *pi) |
| 7833 | { |
| 7834 | char **argv; |
| 7835 | char *p; |
| 7836 | int len; |
| 7837 | |
| 7838 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7839 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7840 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7841 | if (pi->cmdtext) |
| 7842 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7843 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7844 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7845 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7846 | pi->cmdtext = xzalloc(1); |
| 7847 | return pi->cmdtext; |
| 7848 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7849 | len = 0; |
| 7850 | do { |
| 7851 | len += strlen(*argv) + 1; |
| 7852 | } while (*++argv); |
| 7853 | p = xmalloc(len); |
| 7854 | pi->cmdtext = p; |
| 7855 | argv = pi->cmds[0].argv; |
| 7856 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7857 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7858 | *p++ = ' '; |
| 7859 | } while (*++argv); |
| 7860 | p[-1] = '\0'; |
| 7861 | return pi->cmdtext; |
| 7862 | } |
| 7863 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7864 | static void remove_job_from_table(struct pipe *pi) |
| 7865 | { |
| 7866 | struct pipe *prev_pipe; |
| 7867 | |
| 7868 | if (pi == G.job_list) { |
| 7869 | G.job_list = pi->next; |
| 7870 | } else { |
| 7871 | prev_pipe = G.job_list; |
| 7872 | while (prev_pipe->next != pi) |
| 7873 | prev_pipe = prev_pipe->next; |
| 7874 | prev_pipe->next = pi->next; |
| 7875 | } |
| 7876 | G.last_jobid = 0; |
| 7877 | if (G.job_list) |
| 7878 | G.last_jobid = G.job_list->jobid; |
| 7879 | } |
| 7880 | |
| 7881 | static void delete_finished_job(struct pipe *pi) |
| 7882 | { |
| 7883 | remove_job_from_table(pi); |
| 7884 | free_pipe(pi); |
| 7885 | } |
| 7886 | |
| 7887 | static void clean_up_last_dead_job(void) |
| 7888 | { |
| 7889 | if (G.job_list && !G.job_list->alive_cmds) |
| 7890 | delete_finished_job(G.job_list); |
| 7891 | } |
| 7892 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7893 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7894 | { |
| 7895 | struct pipe *job, **jobp; |
| 7896 | int i; |
| 7897 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7898 | clean_up_last_dead_job(); |
| 7899 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7900 | /* Find the end of the list, and find next job ID to use */ |
| 7901 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7902 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7903 | while ((job = *jobp) != NULL) { |
| 7904 | if (job->jobid > i) |
| 7905 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7906 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7907 | } |
| 7908 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7909 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7910 | /* Create a new job struct at the end */ |
| 7911 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7912 | job->next = NULL; |
| 7913 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7914 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7915 | for (i = 0; i < pi->num_cmds; i++) { |
| 7916 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7917 | /* all other fields are not used and stay zero */ |
| 7918 | } |
| 7919 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7920 | |
| 7921 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7922 | 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] | 7923 | G.last_jobid = job->jobid; |
| 7924 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7925 | #endif /* JOB */ |
| 7926 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7927 | static int job_exited_or_stopped(struct pipe *pi) |
| 7928 | { |
| 7929 | int rcode, i; |
| 7930 | |
| 7931 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7932 | return -1; |
| 7933 | |
| 7934 | /* All processes in fg pipe have exited or stopped */ |
| 7935 | rcode = 0; |
| 7936 | i = pi->num_cmds; |
| 7937 | while (--i >= 0) { |
| 7938 | rcode = pi->cmds[i].cmd_exitcode; |
| 7939 | /* usually last process gives overall exitstatus, |
| 7940 | * but with "set -o pipefail", last *failed* process does */ |
| 7941 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7942 | break; |
| 7943 | } |
| 7944 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7945 | return rcode; |
| 7946 | } |
| 7947 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7948 | 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] | 7949 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7950 | #if ENABLE_HUSH_JOB |
| 7951 | struct pipe *pi; |
| 7952 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7953 | int i, dead; |
| 7954 | |
| 7955 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7956 | |
| 7957 | #if DEBUG_JOBS |
| 7958 | if (WIFSTOPPED(status)) |
| 7959 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7960 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7961 | if (WIFSIGNALED(status)) |
| 7962 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7963 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7964 | if (WIFEXITED(status)) |
| 7965 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7966 | childpid, WEXITSTATUS(status)); |
| 7967 | #endif |
| 7968 | /* Were we asked to wait for a fg pipe? */ |
| 7969 | if (fg_pipe) { |
| 7970 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7971 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7972 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7973 | int rcode; |
| 7974 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7975 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7976 | if (fg_pipe->cmds[i].pid != childpid) |
| 7977 | continue; |
| 7978 | if (dead) { |
| 7979 | int ex; |
| 7980 | fg_pipe->cmds[i].pid = 0; |
| 7981 | fg_pipe->alive_cmds--; |
| 7982 | ex = WEXITSTATUS(status); |
| 7983 | /* bash prints killer signal's name for *last* |
| 7984 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 7985 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7986 | */ |
| 7987 | if (WIFSIGNALED(status)) { |
| 7988 | int sig = WTERMSIG(status); |
| 7989 | if (i == fg_pipe->num_cmds-1) |
| 7990 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 7991 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 7992 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 7993 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7994 | * Maybe we need to use sig | 128? */ |
| 7995 | ex = sig + 128; |
| 7996 | } |
| 7997 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 7998 | } else { |
| 7999 | fg_pipe->stopped_cmds++; |
| 8000 | } |
| 8001 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8002 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8003 | rcode = job_exited_or_stopped(fg_pipe); |
| 8004 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8005 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8006 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8007 | * and "killall -STOP cat" */ |
| 8008 | if (G_interactive_fd) { |
| 8009 | #if ENABLE_HUSH_JOB |
| 8010 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8011 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8012 | #endif |
| 8013 | return rcode; |
| 8014 | } |
| 8015 | if (fg_pipe->alive_cmds == 0) |
| 8016 | return rcode; |
| 8017 | } |
| 8018 | /* There are still running processes in the fg_pipe */ |
| 8019 | return -1; |
| 8020 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8021 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | #if ENABLE_HUSH_JOB |
| 8025 | /* We were asked to wait for bg or orphaned children */ |
| 8026 | /* No need to remember exitcode in this case */ |
| 8027 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8028 | for (i = 0; i < pi->num_cmds; i++) { |
| 8029 | if (pi->cmds[i].pid == childpid) |
| 8030 | goto found_pi_and_prognum; |
| 8031 | } |
| 8032 | } |
| 8033 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8034 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8035 | return -1; /* this wasn't a process from fg_pipe */ |
| 8036 | |
| 8037 | found_pi_and_prognum: |
| 8038 | if (dead) { |
| 8039 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8040 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8041 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8042 | rcode = 128 + WTERMSIG(status); |
| 8043 | pi->cmds[i].cmd_exitcode = rcode; |
| 8044 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8045 | G.last_bg_pid_exitcode = rcode; |
| 8046 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8047 | pi->alive_cmds--; |
| 8048 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8049 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8050 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8051 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8052 | delete_finished_job(pi); |
| 8053 | } else { |
| 8054 | /* |
| 8055 | * bash deletes finished jobs from job table only in interactive mode, |
| 8056 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8057 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8058 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8059 | * We only retain one "dead" job, if it's the single job on the list. |
| 8060 | * This covers most of real-world scenarios where this is useful. |
| 8061 | */ |
| 8062 | if (pi != G.job_list) |
| 8063 | delete_finished_job(pi); |
| 8064 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8065 | } |
| 8066 | } else { |
| 8067 | /* child stopped */ |
| 8068 | pi->stopped_cmds++; |
| 8069 | } |
| 8070 | #endif |
| 8071 | return -1; /* this wasn't a process from fg_pipe */ |
| 8072 | } |
| 8073 | |
| 8074 | /* Check to see if any processes have exited -- if they have, |
| 8075 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8076 | * |
| 8077 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8078 | * Return its exitcode or zero if stopped. |
| 8079 | * |
| 8080 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8081 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8082 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8083 | * or 0 if no children changed status. |
| 8084 | * |
| 8085 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8086 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8087 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8088 | */ |
| 8089 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8090 | { |
| 8091 | int attributes; |
| 8092 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8093 | int rcode = 0; |
| 8094 | |
| 8095 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8096 | |
| 8097 | attributes = WUNTRACED; |
| 8098 | if (fg_pipe == NULL) |
| 8099 | attributes |= WNOHANG; |
| 8100 | |
| 8101 | errno = 0; |
| 8102 | #if ENABLE_HUSH_FAST |
| 8103 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8104 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8105 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8106 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8107 | /* Avoid doing waitpid syscall if possible */ |
| 8108 | if (!G.we_have_children) { |
| 8109 | errno = ECHILD; |
| 8110 | return -1; |
| 8111 | } |
| 8112 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8113 | /* We have children, but they did not exit |
| 8114 | * or stop yet (we saw no SIGCHLD) */ |
| 8115 | return 0; |
| 8116 | } |
| 8117 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8118 | } |
| 8119 | #endif |
| 8120 | |
| 8121 | /* Do we do this right? |
| 8122 | * bash-3.00# sleep 20 | false |
| 8123 | * <ctrl-Z pressed> |
| 8124 | * [3]+ Stopped sleep 20 | false |
| 8125 | * bash-3.00# echo $? |
| 8126 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8127 | * [hush 1.14.0: yes we do it right] |
| 8128 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8129 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8130 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8131 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8132 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8133 | i = G.count_SIGCHLD; |
| 8134 | #endif |
| 8135 | childpid = waitpid(-1, &status, attributes); |
| 8136 | if (childpid <= 0) { |
| 8137 | if (childpid && errno != ECHILD) |
| 8138 | bb_perror_msg("waitpid"); |
| 8139 | #if ENABLE_HUSH_FAST |
| 8140 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8141 | G.we_have_children = (childpid == 0); |
| 8142 | G.handled_SIGCHLD = i; |
| 8143 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8144 | } |
| 8145 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8146 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8147 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8148 | break; |
| 8149 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8150 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8151 | if (rcode >= 0) { |
| 8152 | /* fg_pipe exited or stopped */ |
| 8153 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8154 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8155 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8156 | 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] | 8157 | rcode = WEXITSTATUS(status); |
| 8158 | if (WIFSIGNALED(status)) |
| 8159 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8160 | if (WIFSTOPPED(status)) |
| 8161 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8162 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8163 | rcode++; |
| 8164 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8165 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8166 | /* This wasn't one of our processes, or */ |
| 8167 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8168 | } /* while (waitpid succeeds)... */ |
| 8169 | |
| 8170 | return rcode; |
| 8171 | } |
| 8172 | |
| 8173 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8174 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8175 | { |
| 8176 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8177 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8178 | if (G_saved_tty_pgrp) { |
| 8179 | /* Job finished, move the shell to the foreground */ |
| 8180 | p = getpgrp(); /* our process group id */ |
| 8181 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8182 | tcsetpgrp(G_interactive_fd, p); |
| 8183 | } |
| 8184 | return rcode; |
| 8185 | } |
| 8186 | #endif |
| 8187 | |
| 8188 | /* Start all the jobs, but don't wait for anything to finish. |
| 8189 | * See checkjobs(). |
| 8190 | * |
| 8191 | * Return code is normally -1, when the caller has to wait for children |
| 8192 | * to finish to determine the exit status of the pipe. If the pipe |
| 8193 | * is a simple builtin command, however, the action is done by the |
| 8194 | * time run_pipe returns, and the exit code is provided as the |
| 8195 | * return value. |
| 8196 | * |
| 8197 | * Returns -1 only if started some children. IOW: we have to |
| 8198 | * mask out retvals of builtins etc with 0xff! |
| 8199 | * |
| 8200 | * The only case when we do not need to [v]fork is when the pipe |
| 8201 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8202 | * cmd ; ... { list } ; ... |
| 8203 | * cmd && ... { list } && ... |
| 8204 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8205 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8206 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8207 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8208 | * |
| 8209 | * Cases when we must fork: |
| 8210 | * non-single: cmd | cmd |
| 8211 | * backgrounded: cmd & { list } & |
| 8212 | * subshell: ( list ) [&] |
| 8213 | */ |
| 8214 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8215 | #define redirect_and_varexp_helper(old_vars_p, command, squirrel, argv_expanded) \ |
| 8216 | redirect_and_varexp_helper(old_vars_p, command, squirrel) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8217 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8218 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8219 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8220 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8221 | char **argv_expanded) |
| 8222 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8223 | /* Assignments occur before redirects. Try: |
| 8224 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8225 | */ |
| 8226 | |
| 8227 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8228 | dump_cmd_in_x_mode(new_env); |
| 8229 | dump_cmd_in_x_mode(argv_expanded); |
| 8230 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8231 | set_vars_and_save_old(new_env); |
| 8232 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8233 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8234 | * This is perfect for work that comes after exec(). |
| 8235 | * Is it really safe for inline use? Experimentally, |
| 8236 | * things seem to work. */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8237 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8238 | } |
| 8239 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8240 | { |
| 8241 | static const char *const null_ptr = NULL; |
| 8242 | |
| 8243 | int cmd_no; |
| 8244 | int next_infd; |
| 8245 | struct command *command; |
| 8246 | char **argv_expanded; |
| 8247 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8248 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8249 | int rcode; |
| 8250 | |
| 8251 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8252 | debug_enter(); |
| 8253 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8254 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8255 | * Result should be 3 lines: q w e, qwe, q w e |
| 8256 | */ |
| 8257 | G.ifs = get_local_var_value("IFS"); |
| 8258 | if (!G.ifs) |
| 8259 | G.ifs = defifs; |
| 8260 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8261 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8262 | pi->stopped_cmds = 0; |
| 8263 | command = &pi->cmds[0]; |
| 8264 | argv_expanded = NULL; |
| 8265 | |
| 8266 | if (pi->num_cmds != 1 |
| 8267 | || pi->followup == PIPE_BG |
| 8268 | || command->cmd_type == CMD_SUBSHELL |
| 8269 | ) { |
| 8270 | goto must_fork; |
| 8271 | } |
| 8272 | |
| 8273 | pi->alive_cmds = 1; |
| 8274 | |
| 8275 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8276 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8277 | |
| 8278 | if (command->group) { |
| 8279 | #if ENABLE_HUSH_FUNCTIONS |
| 8280 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8281 | /* "executing" func () { list } */ |
| 8282 | struct function *funcp; |
| 8283 | |
| 8284 | funcp = new_function(command->argv[0]); |
| 8285 | /* funcp->name is already set to argv[0] */ |
| 8286 | funcp->body = command->group; |
| 8287 | # if !BB_MMU |
| 8288 | funcp->body_as_string = command->group_as_string; |
| 8289 | command->group_as_string = NULL; |
| 8290 | # endif |
| 8291 | command->group = NULL; |
| 8292 | command->argv[0] = NULL; |
| 8293 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8294 | funcp->parent_cmd = command; |
| 8295 | command->child_func = funcp; |
| 8296 | |
| 8297 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8298 | debug_leave(); |
| 8299 | return EXIT_SUCCESS; |
| 8300 | } |
| 8301 | #endif |
| 8302 | /* { list } */ |
| 8303 | debug_printf("non-subshell group\n"); |
| 8304 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8305 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8306 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8307 | //FIXME: we need to pass squirrel down into run_list() |
| 8308 | //for SH_STANDALONE case, or else this construct: |
| 8309 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8310 | //has no way of closing saved fd#1 for "find", |
| 8311 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8312 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8313 | rcode = run_list(command->group) & 0xff; |
| 8314 | } |
| 8315 | restore_redirects(squirrel); |
| 8316 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8317 | debug_leave(); |
| 8318 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8319 | return rcode; |
| 8320 | } |
| 8321 | |
| 8322 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8323 | { |
| 8324 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8325 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8326 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8327 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8328 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8329 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8330 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8331 | if (G.lineno_var) |
| 8332 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8333 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8334 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8335 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8336 | /* Assignments, but no command. |
| 8337 | * Ensure redirects take effect (that is, create files). |
| 8338 | * Try "a=t >file" |
| 8339 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8340 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8341 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8342 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8343 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8344 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8345 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8346 | /* Set shell variables */ |
| 8347 | if (G_x_mode) |
| 8348 | bb_putchar_stderr('+'); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8349 | i = 0; |
| 8350 | while (i < command->assignment_cnt) { |
| 8351 | char *p = expand_string_to_string(argv[i], /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8352 | if (G_x_mode) |
| 8353 | fprintf(stderr, " %s", p); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8354 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8355 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8356 | /* assignment to readonly var / putenv error? */ |
| 8357 | rcode = 1; |
| 8358 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8359 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8360 | } |
| 8361 | if (G_x_mode) |
| 8362 | bb_putchar_stderr('\n'); |
| 8363 | /* Redirect error sets $? to 1. Otherwise, |
| 8364 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8365 | * Else, clear $?: |
| 8366 | * false; q=`exit 2`; echo $? - should print 2 |
| 8367 | * false; x=1; echo $? - should print 0 |
| 8368 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 8369 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8370 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8371 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8372 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8373 | debug_leave(); |
| 8374 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8375 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8376 | } |
| 8377 | |
| 8378 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 8379 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8380 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8381 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8382 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8383 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8384 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8385 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8386 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8387 | if (!argv_expanded[0]) { |
| 8388 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8389 | /* `false` still has to set exitcode 1 */ |
| 8390 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8391 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8392 | } |
| 8393 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8394 | old_vars = NULL; |
| 8395 | sv_shadowed = G.shadowed_vars_pp; |
| 8396 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8397 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8398 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 8399 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 8400 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8401 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8402 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8403 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8404 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8405 | /* |
| 8406 | * Variable assignments are executed, but then "forgotten": |
| 8407 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 8408 | * sleeps, but prints nothing. |
| 8409 | */ |
| 8410 | enter_var_nest_level(); |
| 8411 | G.shadowed_vars_pp = &old_vars; |
| 8412 | rcode = redirect_and_varexp_helper(command, /*squirrel:*/ NULL, argv_expanded); |
| 8413 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8414 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8415 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8416 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8417 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8418 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8419 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8420 | * a=b true; env | grep ^a= |
| 8421 | */ |
| 8422 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8423 | /* Collect all variables "shadowed" by helper |
| 8424 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 8425 | * into old_vars list: |
| 8426 | */ |
| 8427 | G.shadowed_vars_pp = &old_vars; |
| 8428 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8429 | if (rcode == 0) { |
| 8430 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8431 | /* Do not collect *to old_vars list* vars shadowed |
| 8432 | * by e.g. "local VAR" builtin (collect them |
| 8433 | * in the previously nested list instead): |
| 8434 | * don't want them to be restored immediately |
| 8435 | * after "local" completes. |
| 8436 | */ |
| 8437 | G.shadowed_vars_pp = sv_shadowed; |
| 8438 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8439 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8440 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8441 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8442 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8443 | fflush_all(); |
| 8444 | } |
| 8445 | #if ENABLE_HUSH_FUNCTIONS |
| 8446 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8447 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8448 | funcp->name, argv_expanded[1]); |
| 8449 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8450 | /* |
| 8451 | * But do collect *to old_vars list* vars shadowed |
| 8452 | * within function execution. To that end, restore |
| 8453 | * this pointer _after_ function run: |
| 8454 | */ |
| 8455 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8456 | } |
| 8457 | #endif |
| 8458 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8459 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8460 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8461 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8462 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 8463 | goto must_fork; |
| 8464 | |
| 8465 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8466 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 8467 | G.shadowed_vars_pp = &old_vars; |
| 8468 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 8469 | G.shadowed_vars_pp = sv_shadowed; |
| 8470 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8471 | if (rcode == 0) { |
| 8472 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8473 | argv_expanded[0], argv_expanded[1]); |
| 8474 | /* |
| 8475 | * Note: signals (^C) can't interrupt here. |
| 8476 | * We remember them and they will be acted upon |
| 8477 | * after applet returns. |
| 8478 | * This makes applets which can run for a long time |
| 8479 | * and/or wait for user input ineligible for NOFORK: |
| 8480 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8481 | */ |
| 8482 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8483 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 8484 | } else |
| 8485 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8486 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8487 | restore_redirects(squirrel); |
| 8488 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8489 | leave_var_nest_level(); |
| 8490 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8491 | |
| 8492 | /* |
| 8493 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8494 | * with FEATURE_SH_NOFORK=y. |
| 8495 | */ |
| 8496 | if (!funcp) { |
| 8497 | /* It was builtin or nofork. |
| 8498 | * if this would be a real fork/execed program, |
| 8499 | * it should have died if a fatal sig was received. |
| 8500 | * But OTOH, there was no separate process, |
| 8501 | * the sig was sent to _shell_, not to non-existing |
| 8502 | * child. |
| 8503 | * Let's just handle ^C only, this one is obvious: |
| 8504 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8505 | * during builtin/nofork. |
| 8506 | */ |
| 8507 | if (sigismember(&G.pending_set, SIGINT)) |
| 8508 | rcode = 128 + SIGINT; |
| 8509 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8510 | free(argv_expanded); |
| 8511 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8512 | debug_leave(); |
| 8513 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8514 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8515 | } |
| 8516 | |
| 8517 | must_fork: |
| 8518 | /* NB: argv_expanded may already be created, and that |
| 8519 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8520 | * use argv_expanded if it's non-NULL */ |
| 8521 | |
| 8522 | /* Going to fork a child per each pipe member */ |
| 8523 | pi->alive_cmds = 0; |
| 8524 | next_infd = 0; |
| 8525 | |
| 8526 | cmd_no = 0; |
| 8527 | while (cmd_no < pi->num_cmds) { |
| 8528 | struct fd_pair pipefds; |
| 8529 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8530 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8531 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8532 | nommu_save.old_vars = NULL; |
| 8533 | nommu_save.argv = NULL; |
| 8534 | nommu_save.argv_from_re_execing = NULL; |
| 8535 | #endif |
| 8536 | command = &pi->cmds[cmd_no]; |
| 8537 | cmd_no++; |
| 8538 | if (command->argv) { |
| 8539 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8540 | command->argv[0], command->argv[1]); |
| 8541 | } else { |
| 8542 | debug_printf_exec(": pipe member with no argv\n"); |
| 8543 | } |
| 8544 | |
| 8545 | /* pipes are inserted between pairs of commands */ |
| 8546 | pipefds.rd = 0; |
| 8547 | pipefds.wr = 1; |
| 8548 | if (cmd_no < pi->num_cmds) |
| 8549 | xpiped_pair(pipefds); |
| 8550 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8551 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8552 | if (G.lineno_var) |
| 8553 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8554 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8555 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8556 | command->pid = BB_MMU ? fork() : vfork(); |
| 8557 | if (!command->pid) { /* child */ |
| 8558 | #if ENABLE_HUSH_JOB |
| 8559 | disable_restore_tty_pgrp_on_exit(); |
| 8560 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8561 | |
| 8562 | /* Every child adds itself to new process group |
| 8563 | * with pgid == pid_of_first_child_in_pipe */ |
| 8564 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8565 | pid_t pgrp; |
| 8566 | pgrp = pi->pgrp; |
| 8567 | if (pgrp < 0) /* true for 1st process only */ |
| 8568 | pgrp = getpid(); |
| 8569 | if (setpgid(0, pgrp) == 0 |
| 8570 | && pi->followup != PIPE_BG |
| 8571 | && G_saved_tty_pgrp /* we have ctty */ |
| 8572 | ) { |
| 8573 | /* We do it in *every* child, not just first, |
| 8574 | * to avoid races */ |
| 8575 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8576 | } |
| 8577 | } |
| 8578 | #endif |
| 8579 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8580 | /* 1st cmd in backgrounded pipe |
| 8581 | * should have its stdin /dev/null'ed */ |
| 8582 | close(0); |
| 8583 | if (open(bb_dev_null, O_RDONLY)) |
| 8584 | xopen("/", O_RDONLY); |
| 8585 | } else { |
| 8586 | xmove_fd(next_infd, 0); |
| 8587 | } |
| 8588 | xmove_fd(pipefds.wr, 1); |
| 8589 | if (pipefds.rd > 1) |
| 8590 | close(pipefds.rd); |
| 8591 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8592 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8593 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8594 | * of cmd1 goes into pipe. |
| 8595 | */ |
| 8596 | if (setup_redirects(command, NULL)) { |
| 8597 | /* Happens when redir file can't be opened: |
| 8598 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8599 | * FOO |
| 8600 | * hush: can't open '/qwe/rty': No such file or directory |
| 8601 | * BAZ |
| 8602 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8603 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8604 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8605 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8606 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8607 | /* Stores to nommu_save list of env vars putenv'ed |
| 8608 | * (NOMMU, on MMU we don't need that) */ |
| 8609 | /* cast away volatility... */ |
| 8610 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8611 | /* pseudo_exec() does not return */ |
| 8612 | } |
| 8613 | |
| 8614 | /* parent or error */ |
| 8615 | #if ENABLE_HUSH_FAST |
| 8616 | G.count_SIGCHLD++; |
| 8617 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8618 | #endif |
| 8619 | enable_restore_tty_pgrp_on_exit(); |
| 8620 | #if !BB_MMU |
| 8621 | /* Clean up after vforked child */ |
| 8622 | free(nommu_save.argv); |
| 8623 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8624 | G.var_nest_level = sv_var_nest_level; |
| 8625 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8626 | add_vars(nommu_save.old_vars); |
| 8627 | #endif |
| 8628 | free(argv_expanded); |
| 8629 | argv_expanded = NULL; |
| 8630 | if (command->pid < 0) { /* [v]fork failed */ |
| 8631 | /* Clearly indicate, was it fork or vfork */ |
| 8632 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8633 | } else { |
| 8634 | pi->alive_cmds++; |
| 8635 | #if ENABLE_HUSH_JOB |
| 8636 | /* Second and next children need to know pid of first one */ |
| 8637 | if (pi->pgrp < 0) |
| 8638 | pi->pgrp = command->pid; |
| 8639 | #endif |
| 8640 | } |
| 8641 | |
| 8642 | if (cmd_no > 1) |
| 8643 | close(next_infd); |
| 8644 | if (cmd_no < pi->num_cmds) |
| 8645 | close(pipefds.wr); |
| 8646 | /* Pass read (output) pipe end to next iteration */ |
| 8647 | next_infd = pipefds.rd; |
| 8648 | } |
| 8649 | |
| 8650 | if (!pi->alive_cmds) { |
| 8651 | debug_leave(); |
| 8652 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8653 | return 1; |
| 8654 | } |
| 8655 | |
| 8656 | debug_leave(); |
| 8657 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8658 | return -1; |
| 8659 | } |
| 8660 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8661 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8662 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8663 | static int run_list(struct pipe *pi) |
| 8664 | { |
| 8665 | #if ENABLE_HUSH_CASE |
| 8666 | char *case_word = NULL; |
| 8667 | #endif |
| 8668 | #if ENABLE_HUSH_LOOPS |
| 8669 | struct pipe *loop_top = NULL; |
| 8670 | char **for_lcur = NULL; |
| 8671 | char **for_list = NULL; |
| 8672 | #endif |
| 8673 | smallint last_followup; |
| 8674 | smalluint rcode; |
| 8675 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8676 | smalluint cond_code = 0; |
| 8677 | #else |
| 8678 | enum { cond_code = 0 }; |
| 8679 | #endif |
| 8680 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8681 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8682 | smallint last_rword; /* ditto */ |
| 8683 | #endif |
| 8684 | |
| 8685 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8686 | debug_enter(); |
| 8687 | |
| 8688 | #if ENABLE_HUSH_LOOPS |
| 8689 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8690 | { |
| 8691 | struct pipe *cpipe; |
| 8692 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8693 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8694 | continue; |
| 8695 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8696 | if (cpipe->next == NULL) { |
| 8697 | syntax_error("malformed for"); |
| 8698 | debug_leave(); |
| 8699 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8700 | return 1; |
| 8701 | } |
| 8702 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8703 | if (cpipe->next->res_word == RES_DO) |
| 8704 | continue; |
| 8705 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8706 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8707 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8708 | ) { |
| 8709 | syntax_error("malformed for"); |
| 8710 | debug_leave(); |
| 8711 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8712 | return 1; |
| 8713 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8714 | } |
| 8715 | } |
| 8716 | #endif |
| 8717 | |
| 8718 | /* Past this point, all code paths should jump to ret: label |
| 8719 | * in order to return, no direct "return" statements please. |
| 8720 | * This helps to ensure that no memory is leaked. */ |
| 8721 | |
| 8722 | #if ENABLE_HUSH_JOB |
| 8723 | G.run_list_level++; |
| 8724 | #endif |
| 8725 | |
| 8726 | #if HAS_KEYWORDS |
| 8727 | rword = RES_NONE; |
| 8728 | last_rword = RES_XXXX; |
| 8729 | #endif |
| 8730 | last_followup = PIPE_SEQ; |
| 8731 | rcode = G.last_exitcode; |
| 8732 | |
| 8733 | /* Go through list of pipes, (maybe) executing them. */ |
| 8734 | 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] | 8735 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8736 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8737 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8738 | if (G.flag_SIGINT) |
| 8739 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8740 | if (G_flag_return_in_progress == 1) |
| 8741 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8742 | |
| 8743 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8744 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8745 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8746 | |
| 8747 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8748 | if ( |
| 8749 | #if ENABLE_HUSH_IF |
| 8750 | rword == RES_IF || rword == RES_ELIF || |
| 8751 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8752 | pi->followup != PIPE_SEQ |
| 8753 | ) { |
| 8754 | G.errexit_depth++; |
| 8755 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8756 | #if ENABLE_HUSH_LOOPS |
| 8757 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8758 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8759 | ) { |
| 8760 | /* start of a loop: remember where loop starts */ |
| 8761 | loop_top = pi; |
| 8762 | G.depth_of_loop++; |
| 8763 | } |
| 8764 | #endif |
| 8765 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8766 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8767 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8768 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8769 | ) { |
| 8770 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8771 | * and we should not execute CMD */ |
| 8772 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8773 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8774 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8775 | } |
| 8776 | } |
| 8777 | last_followup = pi->followup; |
| 8778 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8779 | #if ENABLE_HUSH_IF |
| 8780 | if (cond_code) { |
| 8781 | if (rword == RES_THEN) { |
| 8782 | /* if false; then ... fi has exitcode 0! */ |
| 8783 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8784 | /* "if <false> THEN cmd": skip cmd */ |
| 8785 | continue; |
| 8786 | } |
| 8787 | } else { |
| 8788 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8789 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8790 | * skip cmd and all following ones */ |
| 8791 | break; |
| 8792 | } |
| 8793 | } |
| 8794 | #endif |
| 8795 | #if ENABLE_HUSH_LOOPS |
| 8796 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 8797 | if (!for_lcur) { |
| 8798 | /* first loop through for */ |
| 8799 | |
| 8800 | static const char encoded_dollar_at[] ALIGN1 = { |
| 8801 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 8802 | }; /* encoded representation of "$@" */ |
| 8803 | static const char *const encoded_dollar_at_argv[] = { |
| 8804 | encoded_dollar_at, NULL |
| 8805 | }; /* argv list with one element: "$@" */ |
| 8806 | char **vals; |
| 8807 | |
| 8808 | vals = (char**)encoded_dollar_at_argv; |
| 8809 | if (pi->next->res_word == RES_IN) { |
| 8810 | /* if no variable values after "in" we skip "for" */ |
| 8811 | if (!pi->next->cmds[0].argv) { |
| 8812 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8813 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 8814 | break; |
| 8815 | } |
| 8816 | vals = pi->next->cmds[0].argv; |
| 8817 | } /* else: "for var; do..." -> assume "$@" list */ |
| 8818 | /* create list of variable values */ |
| 8819 | debug_print_strings("for_list made from", vals); |
| 8820 | for_list = expand_strvec_to_strvec(vals); |
| 8821 | for_lcur = for_list; |
| 8822 | debug_print_strings("for_list", for_list); |
| 8823 | } |
| 8824 | if (!*for_lcur) { |
| 8825 | /* "for" loop is over, clean up */ |
| 8826 | free(for_list); |
| 8827 | for_list = NULL; |
| 8828 | for_lcur = NULL; |
| 8829 | break; |
| 8830 | } |
| 8831 | /* Insert next value from for_lcur */ |
| 8832 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8833 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8834 | continue; |
| 8835 | } |
| 8836 | if (rword == RES_IN) { |
| 8837 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 8838 | } |
| 8839 | if (rword == RES_DONE) { |
| 8840 | continue; /* "done" has no cmds too */ |
| 8841 | } |
| 8842 | #endif |
| 8843 | #if ENABLE_HUSH_CASE |
| 8844 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8845 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 8846 | case_word = expand_string_to_string(pi->cmds->argv[0], 1); |
| 8847 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 8848 | //unbackslash(case_word); |
| 8849 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8850 | continue; |
| 8851 | } |
| 8852 | if (rword == RES_MATCH) { |
| 8853 | char **argv; |
| 8854 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8855 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8856 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 8857 | break; |
| 8858 | /* all prev words didn't match, does this one match? */ |
| 8859 | argv = pi->cmds->argv; |
| 8860 | while (*argv) { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8861 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8862 | /* TODO: which FNM_xxx flags to use? */ |
| 8863 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8864 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8865 | free(pattern); |
| 8866 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8867 | free(case_word); |
| 8868 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8869 | break; |
| 8870 | } |
| 8871 | argv++; |
| 8872 | } |
| 8873 | continue; |
| 8874 | } |
| 8875 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8876 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8877 | if (cond_code != 0) |
| 8878 | continue; /* not matched yet, skip this pipe */ |
| 8879 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8880 | if (rword == RES_ESAC) { |
| 8881 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 8882 | if (case_word) { |
| 8883 | /* "case" did not match anything: still set $? (to 0) */ |
| 8884 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8885 | } |
| 8886 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8887 | #endif |
| 8888 | /* Just pressing <enter> in shell should check for jobs. |
| 8889 | * OTOH, in non-interactive shell this is useless |
| 8890 | * and only leads to extra job checks */ |
| 8891 | if (pi->num_cmds == 0) { |
| 8892 | if (G_interactive_fd) |
| 8893 | goto check_jobs_and_continue; |
| 8894 | continue; |
| 8895 | } |
| 8896 | |
| 8897 | /* After analyzing all keywords and conditions, we decided |
| 8898 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8899 | * after run_pipe to collect any background children, |
| 8900 | * even if list execution is to be stopped. */ |
| 8901 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8902 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8903 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8904 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8905 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8906 | if (r != -1) { |
| 8907 | /* We ran a builtin, function, or group. |
| 8908 | * rcode is already known |
| 8909 | * and we don't need to wait for anything. */ |
| 8910 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8911 | G.last_exitcode = rcode; |
| 8912 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8913 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8914 | /* Was it "break" or "continue"? */ |
| 8915 | if (G.flag_break_continue) { |
| 8916 | smallint fbc = G.flag_break_continue; |
| 8917 | /* We might fall into outer *loop*, |
| 8918 | * don't want to break it too */ |
| 8919 | if (loop_top) { |
| 8920 | G.depth_break_continue--; |
| 8921 | if (G.depth_break_continue == 0) |
| 8922 | G.flag_break_continue = 0; |
| 8923 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 8924 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 8925 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8926 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8927 | break; |
| 8928 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8929 | /* "continue": simulate end of loop */ |
| 8930 | rword = RES_DONE; |
| 8931 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8932 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8933 | #endif |
| 8934 | if (G_flag_return_in_progress == 1) { |
| 8935 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 8936 | break; |
| 8937 | } |
| 8938 | } else if (pi->followup == PIPE_BG) { |
| 8939 | /* What does bash do with attempts to background builtins? */ |
| 8940 | /* even bash 3.2 doesn't do that well with nested bg: |
| 8941 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 8942 | * I'm NOT treating inner &'s as jobs */ |
| 8943 | #if ENABLE_HUSH_JOB |
| 8944 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8945 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8946 | #endif |
| 8947 | /* Last command's pid goes to $! */ |
| 8948 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8949 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8950 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8951 | /* Check pi->pi_inverted? "! sleep 1 & echo $?": bash says 1. dash and ash say 0 */ |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8952 | rcode = EXIT_SUCCESS; |
| 8953 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8954 | } else { |
| 8955 | #if ENABLE_HUSH_JOB |
| 8956 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8957 | /* Waits for completion, then fg's main shell */ |
| 8958 | rcode = checkjobs_and_fg_shell(pi); |
| 8959 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8960 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8961 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8962 | #endif |
| 8963 | /* This one just waits for completion */ |
| 8964 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8965 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8966 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8967 | G.last_exitcode = rcode; |
| 8968 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8969 | } |
| 8970 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8971 | /* Handle "set -e" */ |
| 8972 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 8973 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 8974 | if (G.errexit_depth == 0) |
| 8975 | hush_exit(rcode); |
| 8976 | } |
| 8977 | G.errexit_depth = sv_errexit_depth; |
| 8978 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8979 | /* Analyze how result affects subsequent commands */ |
| 8980 | #if ENABLE_HUSH_IF |
| 8981 | if (rword == RES_IF || rword == RES_ELIF) |
| 8982 | cond_code = rcode; |
| 8983 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8984 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8985 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8986 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8987 | #if ENABLE_HUSH_LOOPS |
| 8988 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8989 | if (pi->next |
| 8990 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 8991 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8992 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8993 | if (rword == RES_WHILE) { |
| 8994 | if (rcode) { |
| 8995 | /* "while false; do...done" - exitcode 0 */ |
| 8996 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8997 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8998 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8999 | } |
| 9000 | } |
| 9001 | if (rword == RES_UNTIL) { |
| 9002 | if (!rcode) { |
| 9003 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9004 | break; |
| 9005 | } |
| 9006 | } |
| 9007 | } |
| 9008 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9009 | } /* for (pi) */ |
| 9010 | |
| 9011 | #if ENABLE_HUSH_JOB |
| 9012 | G.run_list_level--; |
| 9013 | #endif |
| 9014 | #if ENABLE_HUSH_LOOPS |
| 9015 | if (loop_top) |
| 9016 | G.depth_of_loop--; |
| 9017 | free(for_list); |
| 9018 | #endif |
| 9019 | #if ENABLE_HUSH_CASE |
| 9020 | free(case_word); |
| 9021 | #endif |
| 9022 | debug_leave(); |
| 9023 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9024 | return rcode; |
| 9025 | } |
| 9026 | |
| 9027 | /* Select which version we will use */ |
| 9028 | static int run_and_free_list(struct pipe *pi) |
| 9029 | { |
| 9030 | int rcode = 0; |
| 9031 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9032 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9033 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9034 | rcode = run_list(pi); |
| 9035 | } |
| 9036 | /* free_pipe_list has the side effect of clearing memory. |
| 9037 | * In the long run that function can be merged with run_list, |
| 9038 | * but doing that now would hobble the debugging effort. */ |
| 9039 | free_pipe_list(pi); |
| 9040 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9041 | return rcode; |
| 9042 | } |
| 9043 | |
| 9044 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9045 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9046 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9047 | sighandler_t old_handler; |
| 9048 | unsigned sig = 0; |
| 9049 | while ((mask >>= 1) != 0) { |
| 9050 | sig++; |
| 9051 | if (!(mask & 1)) |
| 9052 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9053 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9054 | /* POSIX allows shell to re-enable SIGCHLD |
| 9055 | * even if it was SIG_IGN on entry. |
| 9056 | * Therefore we skip IGN check for it: |
| 9057 | */ |
| 9058 | if (sig == SIGCHLD) |
| 9059 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9060 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9061 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9062 | */ |
| 9063 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9064 | if (old_handler == SIG_IGN) { |
| 9065 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9066 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9067 | #if ENABLE_HUSH_TRAP |
| 9068 | if (!G_traps) |
| 9069 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9070 | free(G_traps[sig]); |
| 9071 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9072 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9073 | } |
| 9074 | } |
| 9075 | } |
| 9076 | |
| 9077 | /* Called a few times only (or even once if "sh -c") */ |
| 9078 | static void install_special_sighandlers(void) |
| 9079 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9080 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9081 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9082 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9083 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9084 | if (G_interactive_fd) { |
| 9085 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9086 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9087 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9088 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9089 | /* Careful, do not re-install handlers we already installed */ |
| 9090 | if (G.special_sig_mask != mask) { |
| 9091 | unsigned diff = mask & ~G.special_sig_mask; |
| 9092 | G.special_sig_mask = mask; |
| 9093 | install_sighandlers(diff); |
| 9094 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9095 | } |
| 9096 | |
| 9097 | #if ENABLE_HUSH_JOB |
| 9098 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9099 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9100 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9101 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9102 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9103 | |
| 9104 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9105 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9106 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9107 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9108 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9109 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9110 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9111 | + (1 << SIGABRT) |
| 9112 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9113 | + (1 << SIGPIPE) |
| 9114 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9115 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9116 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9117 | * we never want to restore pgrp on exit, and this fn is not called |
| 9118 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9119 | /*+ (1 << SIGHUP )*/ |
| 9120 | /*+ (1 << SIGTERM)*/ |
| 9121 | /*+ (1 << SIGINT )*/ |
| 9122 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9123 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9124 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9125 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9126 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9127 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9128 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9129 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9130 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9131 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9132 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9133 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9134 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9135 | break; |
| 9136 | case 'x': |
| 9137 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9138 | break; |
| 9139 | case 'o': |
| 9140 | if (!o_opt) { |
| 9141 | /* "set -+o" without parameter. |
| 9142 | * in bash, set -o produces this output: |
| 9143 | * pipefail off |
| 9144 | * and set +o: |
| 9145 | * set +o pipefail |
| 9146 | * We always use the second form. |
| 9147 | */ |
| 9148 | const char *p = o_opt_strings; |
| 9149 | idx = 0; |
| 9150 | while (*p) { |
| 9151 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9152 | idx++; |
| 9153 | p += strlen(p) + 1; |
| 9154 | } |
| 9155 | break; |
| 9156 | } |
| 9157 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9158 | if (idx >= 0) { |
| 9159 | G.o_opt[idx] = state; |
| 9160 | break; |
| 9161 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9162 | case 'e': |
| 9163 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9164 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9165 | default: |
| 9166 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9167 | } |
| 9168 | return EXIT_SUCCESS; |
| 9169 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9170 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9171 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9172 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9173 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9174 | enum { |
| 9175 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9176 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9177 | }; |
| 9178 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9179 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9180 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9181 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9182 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9183 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9184 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9185 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9186 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9187 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9188 | #if ENABLE_HUSH_FAST |
| 9189 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9190 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9191 | #if !BB_MMU |
| 9192 | G.argv0_for_re_execing = argv[0]; |
| 9193 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9194 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9195 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9196 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9197 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9198 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9199 | shell_ver->flg_export = 1; |
| 9200 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9201 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9202 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9203 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9204 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9205 | /* Create shell local variables from the values |
| 9206 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9207 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9208 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9209 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9210 | if (e) while (*e) { |
| 9211 | char *value = strchr(*e, '='); |
| 9212 | if (value) { /* paranoia */ |
| 9213 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9214 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9215 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9216 | cur_var->max_len = strlen(*e); |
| 9217 | cur_var->flg_export = 1; |
| 9218 | } |
| 9219 | e++; |
| 9220 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9221 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9222 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9223 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9224 | |
| 9225 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9226 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9227 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9228 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9229 | /* Set (but not export) PS1/2 unless already set */ |
| 9230 | if (!get_local_var_value("PS1")) |
| 9231 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9232 | if (!get_local_var_value("PS2")) |
| 9233 | set_local_var_from_halves("PS2", "> "); |
| 9234 | #endif |
| 9235 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9236 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9237 | /* Set (but not export) HOSTNAME unless already set */ |
| 9238 | if (!get_local_var_value("HOSTNAME")) { |
| 9239 | struct utsname uts; |
| 9240 | uname(&uts); |
| 9241 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9242 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9243 | /* bash also exports SHLVL and _, |
| 9244 | * and sets (but doesn't export) the following variables: |
| 9245 | * BASH=/bin/bash |
| 9246 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9247 | * BASH_VERSION='3.2.0(1)-release' |
| 9248 | * HOSTTYPE=i386 |
| 9249 | * MACHTYPE=i386-pc-linux-gnu |
| 9250 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9251 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9252 | * EUID=<NNNNN> |
| 9253 | * UID=<NNNNN> |
| 9254 | * GROUPS=() |
| 9255 | * LINES=<NNN> |
| 9256 | * COLUMNS=<NNN> |
| 9257 | * BASH_ARGC=() |
| 9258 | * BASH_ARGV=() |
| 9259 | * BASH_LINENO=() |
| 9260 | * BASH_SOURCE=() |
| 9261 | * DIRSTACK=() |
| 9262 | * PIPESTATUS=([0]="0") |
| 9263 | * HISTFILE=/<xxx>/.bash_history |
| 9264 | * HISTFILESIZE=500 |
| 9265 | * HISTSIZE=500 |
| 9266 | * MAILCHECK=60 |
| 9267 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9268 | * SHELL=/bin/bash |
| 9269 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9270 | * TERM=dumb |
| 9271 | * OPTERR=1 |
| 9272 | * OPTIND=1 |
| 9273 | * IFS=$' \t\n' |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9274 | * PS4='+ ' |
| 9275 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9276 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9277 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9278 | #if ENABLE_HUSH_LINENO_VAR |
| 9279 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9280 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9281 | set_local_var(p, /*flags*/ 0); |
| 9282 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9283 | } |
| 9284 | #endif |
| 9285 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9286 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9287 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9288 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9289 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9290 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9291 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9292 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9293 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9294 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9295 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9296 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9297 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9298 | * 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] | 9299 | * in order to intercept (more) signals. |
| 9300 | */ |
| 9301 | |
| 9302 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9303 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9304 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9305 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9306 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9307 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9308 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9309 | "<:$:R:V:" |
| 9310 | # if ENABLE_HUSH_FUNCTIONS |
| 9311 | "F:" |
| 9312 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9313 | #endif |
| 9314 | ); |
| 9315 | if (opt <= 0) |
| 9316 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9317 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9318 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9319 | /* Possibilities: |
| 9320 | * sh ... -c 'script' |
| 9321 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9322 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9323 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9324 | * "" needs to be replaced with NULL |
| 9325 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9326 | * Note: the form without ARG0 never happens: |
| 9327 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9328 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9329 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9330 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9331 | G.root_ppid = getppid(); |
| 9332 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9333 | G.global_argv = argv + optind; |
| 9334 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9335 | if (builtin_argc) { |
| 9336 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9337 | const struct built_in_command *x; |
| 9338 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9339 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9340 | x = find_builtin(optarg); |
| 9341 | if (x) { /* paranoia */ |
| 9342 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9343 | G.global_argv += builtin_argc; |
| 9344 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9345 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9346 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9347 | } |
| 9348 | goto final_return; |
| 9349 | } |
| 9350 | if (!G.global_argv[0]) { |
| 9351 | /* -c 'script' (no params): prevent empty $0 */ |
| 9352 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9353 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9354 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9355 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9356 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9357 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9358 | goto final_return; |
| 9359 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9360 | /* Well, we cannot just declare interactiveness, |
| 9361 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9362 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9363 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9364 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9365 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9366 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9367 | case 'l': |
| 9368 | flags |= OPT_login; |
| 9369 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9370 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9371 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9372 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9373 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9374 | case '$': { |
| 9375 | unsigned long long empty_trap_mask; |
| 9376 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9377 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9378 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9379 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9380 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9381 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9382 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9383 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9384 | optarg++; |
| 9385 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9386 | optarg++; |
| 9387 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9388 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9389 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9390 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9391 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9392 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9393 | for (sig = 1; sig < NSIG; sig++) { |
| 9394 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9395 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9396 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9397 | } |
| 9398 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9399 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9400 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9401 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9402 | optarg++; |
| 9403 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9404 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 9405 | # if ENABLE_HUSH_FUNCTIONS |
| 9406 | /* nommu uses re-exec trick for "... | func | ...", |
| 9407 | * should allow "return". |
| 9408 | * This accidentally allows returns in subshells. |
| 9409 | */ |
| 9410 | G_flag_return_in_progress = -1; |
| 9411 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9412 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9413 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9414 | case 'R': |
| 9415 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9416 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9417 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9418 | # if ENABLE_HUSH_FUNCTIONS |
| 9419 | case 'F': { |
| 9420 | struct function *funcp = new_function(optarg); |
| 9421 | /* funcp->name is already set to optarg */ |
| 9422 | /* funcp->body is set to NULL. It's a special case. */ |
| 9423 | funcp->body_as_string = argv[optind]; |
| 9424 | optind++; |
| 9425 | break; |
| 9426 | } |
| 9427 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9428 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9429 | case 'n': |
| 9430 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9431 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9432 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9433 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9434 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9435 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9436 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9437 | " or: sh -c command [args]...\n\n"); |
| 9438 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9439 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9440 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9441 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9442 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9443 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9444 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9445 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9446 | G.global_argc = argc - (optind - 1); |
| 9447 | G.global_argv = argv + (optind - 1); |
| 9448 | G.global_argv[0] = argv[0]; |
| 9449 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9450 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9451 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9452 | G.root_ppid = getppid(); |
| 9453 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9454 | |
| 9455 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9456 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9457 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9458 | debug_printf("sourcing /etc/profile\n"); |
| 9459 | input = fopen_for_read("/etc/profile"); |
| 9460 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9461 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9462 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9463 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9464 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9465 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9466 | /* bash: after sourcing /etc/profile, |
| 9467 | * tries to source (in the given order): |
| 9468 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9469 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9470 | * bash also sources ~/.bash_logout on exit. |
| 9471 | * If called as sh, skips .bash_XXX files. |
| 9472 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9475 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 9476 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9477 | FILE *input; |
| 9478 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9479 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9480 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9481 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9482 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9483 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9484 | G.global_argc--; |
| 9485 | G.global_argv++; |
| 9486 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9487 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9488 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9489 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9490 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9491 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9492 | parse_and_run_file(input); |
| 9493 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9494 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9495 | #endif |
| 9496 | goto final_return; |
| 9497 | } |
| 9498 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9499 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9500 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9501 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9502 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9503 | /* A shell is interactive if the '-i' flag was given, |
| 9504 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9505 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9506 | * no arguments remaining or the -s flag given |
| 9507 | * standard input is a terminal |
| 9508 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9509 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9510 | */ |
| 9511 | #if ENABLE_HUSH_JOB |
| 9512 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9513 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9514 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9515 | if (G_saved_tty_pgrp < 0) |
| 9516 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9517 | |
| 9518 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9519 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9520 | if (G_interactive_fd < 0) { |
| 9521 | /* try to dup to any fd */ |
| 9522 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9523 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9524 | /* give up */ |
| 9525 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9526 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9527 | } |
| 9528 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9529 | // TODO: track & disallow any attempts of user |
| 9530 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9531 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9532 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9533 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9534 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9535 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9536 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9537 | /* If we were run as 'hush &', sleep until we are |
| 9538 | * in the foreground (tty pgrp == our pgrp). |
| 9539 | * If we get started under a job aware app (like bash), |
| 9540 | * make sure we are now in charge so we don't fight over |
| 9541 | * who gets the foreground */ |
| 9542 | while (1) { |
| 9543 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9544 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9545 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9546 | break; |
| 9547 | /* send TTIN to ourself (should stop us) */ |
| 9548 | kill(- shell_pgrp, SIGTTIN); |
| 9549 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9550 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9551 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9552 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9553 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9554 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9555 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9556 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9557 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9558 | /* Put ourselves in our own process group |
| 9559 | * (bash, too, does this only if ctty is available) */ |
| 9560 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9561 | /* Grab control of the terminal */ |
| 9562 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9563 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9564 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9565 | |
| 9566 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9567 | { |
| 9568 | const char *hp = get_local_var_value("HISTFILE"); |
| 9569 | if (!hp) { |
| 9570 | hp = get_local_var_value("HOME"); |
| 9571 | if (hp) |
| 9572 | hp = concat_path_file(hp, ".hush_history"); |
| 9573 | } else { |
| 9574 | hp = xstrdup(hp); |
| 9575 | } |
| 9576 | if (hp) { |
| 9577 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9578 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9579 | } |
| 9580 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9581 | hp = get_local_var_value("HISTFILESIZE"); |
| 9582 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9583 | # endif |
| 9584 | } |
| 9585 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9586 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9587 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9588 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9589 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9590 | /* No job control compiled in, only prompt/line editing */ |
| 9591 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9592 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9593 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9594 | /* try to dup to any fd */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9595 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9596 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9597 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9598 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9599 | } |
| 9600 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9601 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9602 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9603 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9604 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9605 | #else |
| 9606 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9607 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9608 | #endif |
| 9609 | /* bash: |
| 9610 | * if interactive but not a login shell, sources ~/.bashrc |
| 9611 | * (--norc turns this off, --rcfile <file> overrides) |
| 9612 | */ |
| 9613 | |
| 9614 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9615 | /* note: ash and hush share this string */ |
| 9616 | printf("\n\n%s %s\n" |
| 9617 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9618 | "\n", |
| 9619 | bb_banner, |
| 9620 | "hush - the humble shell" |
| 9621 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9622 | } |
| 9623 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9624 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9625 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9626 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9627 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9628 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9629 | |
| 9630 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9631 | /* |
| 9632 | * Built-ins |
| 9633 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9634 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9635 | { |
| 9636 | return 0; |
| 9637 | } |
| 9638 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9639 | #if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 9640 | 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] | 9641 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9642 | int argc = string_array_len(argv); |
| 9643 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9644 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9645 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9646 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9647 | static int FAST_FUNC builtin_test(char **argv) |
| 9648 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9649 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9650 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9651 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9652 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9653 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9654 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9655 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9656 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9657 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9658 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9659 | static int FAST_FUNC builtin_printf(char **argv) |
| 9660 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9661 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9662 | } |
| 9663 | #endif |
| 9664 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9665 | #if ENABLE_HUSH_HELP |
| 9666 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9667 | { |
| 9668 | const struct built_in_command *x; |
| 9669 | |
| 9670 | printf( |
| 9671 | "Built-in commands:\n" |
| 9672 | "------------------\n"); |
| 9673 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9674 | if (x->b_descr) |
| 9675 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9676 | } |
| 9677 | return EXIT_SUCCESS; |
| 9678 | } |
| 9679 | #endif |
| 9680 | |
| 9681 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9682 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9683 | { |
| 9684 | show_history(G.line_input_state); |
| 9685 | return EXIT_SUCCESS; |
| 9686 | } |
| 9687 | #endif |
| 9688 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9689 | static char **skip_dash_dash(char **argv) |
| 9690 | { |
| 9691 | argv++; |
| 9692 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9693 | argv++; |
| 9694 | return argv; |
| 9695 | } |
| 9696 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9697 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9698 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9699 | const char *newdir; |
| 9700 | |
| 9701 | argv = skip_dash_dash(argv); |
| 9702 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9703 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9704 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9705 | * bash says "bash: cd: HOME not set" and does nothing |
| 9706 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9707 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9708 | const char *home = get_local_var_value("HOME"); |
| 9709 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9710 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9711 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9712 | /* Mimic bash message exactly */ |
| 9713 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9714 | return EXIT_FAILURE; |
| 9715 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9716 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9717 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9718 | * set it again, but do not export. bash does the same. |
| 9719 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9720 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9721 | return EXIT_SUCCESS; |
| 9722 | } |
| 9723 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9724 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9725 | { |
| 9726 | puts(get_cwd(0)); |
| 9727 | return EXIT_SUCCESS; |
| 9728 | } |
| 9729 | |
| 9730 | static int FAST_FUNC builtin_eval(char **argv) |
| 9731 | { |
| 9732 | int rcode = EXIT_SUCCESS; |
| 9733 | |
| 9734 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9735 | if (argv[0]) { |
| 9736 | char *str = NULL; |
| 9737 | |
| 9738 | if (argv[1]) { |
| 9739 | /* "The eval utility shall construct a command by |
| 9740 | * concatenating arguments together, separating |
| 9741 | * each with a <space> character." |
| 9742 | */ |
| 9743 | char *p; |
| 9744 | unsigned len = 0; |
| 9745 | char **pp = argv; |
| 9746 | do |
| 9747 | len += strlen(*pp) + 1; |
| 9748 | while (*++pp); |
| 9749 | str = p = xmalloc(len); |
| 9750 | pp = argv; |
| 9751 | do { |
| 9752 | p = stpcpy(p, *pp); |
| 9753 | *p++ = ' '; |
| 9754 | } while (*++pp); |
| 9755 | p[-1] = '\0'; |
| 9756 | } |
| 9757 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9758 | /* bash: |
| 9759 | * eval "echo Hi; done" ("done" is syntax error): |
| 9760 | * "echo Hi" will not execute too. |
| 9761 | */ |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9762 | parse_and_run_string(str ? str : argv[0]); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9763 | free(str); |
| 9764 | rcode = G.last_exitcode; |
| 9765 | } |
| 9766 | return rcode; |
| 9767 | } |
| 9768 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9769 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9770 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9771 | argv = skip_dash_dash(argv); |
| 9772 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9773 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9774 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9775 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9776 | * tty pgrp then, only top-level shell process does that */ |
| 9777 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9778 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9779 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9780 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9781 | * open here. However, they are all CLOEXEC, and execv below |
| 9782 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9783 | * it should show no extra open fds in the "ls" process. |
| 9784 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 9785 | */ |
| 9786 | //close_saved_fds_and_FILE_fds(); |
| 9787 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 9788 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9789 | * 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] | 9790 | * and tcsetpgrp, and this is inherently racy. |
| 9791 | */ |
| 9792 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9793 | } |
| 9794 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9795 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9796 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 9797 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9798 | |
| 9799 | /* interactive bash: |
| 9800 | * # trap "echo EEE" EXIT |
| 9801 | * # exit |
| 9802 | * exit |
| 9803 | * There are stopped jobs. |
| 9804 | * (if there are _stopped_ jobs, running ones don't count) |
| 9805 | * # exit |
| 9806 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 9807 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9808 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 9809 | * 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] | 9810 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 9811 | |
| 9812 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9813 | argv = skip_dash_dash(argv); |
| 9814 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9815 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9816 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 9817 | xfunc_error_retval = 255; |
| 9818 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9819 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9820 | } |
| 9821 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9822 | #if ENABLE_HUSH_TYPE |
| 9823 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 9824 | static int FAST_FUNC builtin_type(char **argv) |
| 9825 | { |
| 9826 | int ret = EXIT_SUCCESS; |
| 9827 | |
| 9828 | while (*++argv) { |
| 9829 | const char *type; |
| 9830 | char *path = NULL; |
| 9831 | |
| 9832 | if (0) {} /* make conditional compile easier below */ |
| 9833 | /*else if (find_alias(*argv)) |
| 9834 | type = "an alias";*/ |
| 9835 | #if ENABLE_HUSH_FUNCTIONS |
| 9836 | else if (find_function(*argv)) |
| 9837 | type = "a function"; |
| 9838 | #endif |
| 9839 | else if (find_builtin(*argv)) |
| 9840 | type = "a shell builtin"; |
| 9841 | else if ((path = find_in_path(*argv)) != NULL) |
| 9842 | type = path; |
| 9843 | else { |
| 9844 | bb_error_msg("type: %s: not found", *argv); |
| 9845 | ret = EXIT_FAILURE; |
| 9846 | continue; |
| 9847 | } |
| 9848 | |
| 9849 | printf("%s is %s\n", *argv, type); |
| 9850 | free(path); |
| 9851 | } |
| 9852 | |
| 9853 | return ret; |
| 9854 | } |
| 9855 | #endif |
| 9856 | |
| 9857 | #if ENABLE_HUSH_READ |
| 9858 | /* Interruptibility of read builtin in bash |
| 9859 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9860 | * |
| 9861 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9862 | * |
| 9863 | * SIGINT: |
| 9864 | * - terminates non-interactive shell; |
| 9865 | * - interrupts read in interactive shell; |
| 9866 | * if it has non-empty trap: |
| 9867 | * - executes trap and returns to command prompt in interactive shell; |
| 9868 | * - executes trap and returns to read in non-interactive shell; |
| 9869 | * SIGTERM: |
| 9870 | * - is ignored (does not interrupt) read in interactive shell; |
| 9871 | * - terminates non-interactive shell; |
| 9872 | * if it has non-empty trap: |
| 9873 | * - executes trap and returns to read; |
| 9874 | * SIGHUP: |
| 9875 | * - terminates shell (regardless of interactivity); |
| 9876 | * if it has non-empty trap: |
| 9877 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9878 | * SIGCHLD from children: |
| 9879 | * - does not interrupt read regardless of interactivity: |
| 9880 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9881 | */ |
| 9882 | static int FAST_FUNC builtin_read(char **argv) |
| 9883 | { |
| 9884 | const char *r; |
| 9885 | char *opt_n = NULL; |
| 9886 | char *opt_p = NULL; |
| 9887 | char *opt_t = NULL; |
| 9888 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9889 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9890 | const char *ifs; |
| 9891 | int read_flags; |
| 9892 | |
| 9893 | /* "!": do not abort on errors. |
| 9894 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9895 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9896 | read_flags = getopt32(argv, |
| 9897 | #if BASH_READ_D |
| 9898 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 9899 | #else |
| 9900 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 9901 | #endif |
| 9902 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9903 | if (read_flags == (uint32_t)-1) |
| 9904 | return EXIT_FAILURE; |
| 9905 | argv += optind; |
| 9906 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9907 | |
| 9908 | again: |
| 9909 | r = shell_builtin_read(set_local_var_from_halves, |
| 9910 | argv, |
| 9911 | ifs, |
| 9912 | read_flags, |
| 9913 | opt_n, |
| 9914 | opt_p, |
| 9915 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9916 | opt_u, |
| 9917 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9918 | ); |
| 9919 | |
| 9920 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9921 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9922 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9923 | goto again; |
| 9924 | } |
| 9925 | |
| 9926 | if ((uintptr_t)r > 1) { |
| 9927 | bb_error_msg("%s", r); |
| 9928 | r = (char*)(uintptr_t)1; |
| 9929 | } |
| 9930 | |
| 9931 | return (uintptr_t)r; |
| 9932 | } |
| 9933 | #endif |
| 9934 | |
| 9935 | #if ENABLE_HUSH_UMASK |
| 9936 | static int FAST_FUNC builtin_umask(char **argv) |
| 9937 | { |
| 9938 | int rc; |
| 9939 | mode_t mask; |
| 9940 | |
| 9941 | rc = 1; |
| 9942 | mask = umask(0); |
| 9943 | argv = skip_dash_dash(argv); |
| 9944 | if (argv[0]) { |
| 9945 | mode_t old_mask = mask; |
| 9946 | |
| 9947 | /* numeric umasks are taken as-is */ |
| 9948 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9949 | if (!isdigit(argv[0][0])) |
| 9950 | mask ^= 0777; |
| 9951 | mask = bb_parse_mode(argv[0], mask); |
| 9952 | if (!isdigit(argv[0][0])) |
| 9953 | mask ^= 0777; |
| 9954 | if ((unsigned)mask > 0777) { |
| 9955 | mask = old_mask; |
| 9956 | /* bash messages: |
| 9957 | * bash: umask: 'q': invalid symbolic mode operator |
| 9958 | * bash: umask: 999: octal number out of range |
| 9959 | */ |
| 9960 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 9961 | rc = 0; |
| 9962 | } |
| 9963 | } else { |
| 9964 | /* Mimic bash */ |
| 9965 | printf("%04o\n", (unsigned) mask); |
| 9966 | /* fall through and restore mask which we set to 0 */ |
| 9967 | } |
| 9968 | umask(mask); |
| 9969 | |
| 9970 | return !rc; /* rc != 0 - success */ |
| 9971 | } |
| 9972 | #endif |
| 9973 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9974 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9975 | static void print_escaped(const char *s) |
| 9976 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9977 | if (*s == '\'') |
| 9978 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9979 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9980 | const char *p = strchrnul(s, '\''); |
| 9981 | /* print 'xxxx', possibly just '' */ |
| 9982 | printf("'%.*s'", (int)(p - s), s); |
| 9983 | if (*p == '\0') |
| 9984 | break; |
| 9985 | s = p; |
| 9986 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9987 | /* s points to '; print "'''...'''" */ |
| 9988 | putchar('"'); |
| 9989 | do putchar('\''); while (*++s == '\''); |
| 9990 | putchar('"'); |
| 9991 | } while (*s); |
| 9992 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9993 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9994 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9995 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9996 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9997 | { |
| 9998 | do { |
| 9999 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10000 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10001 | |
| 10002 | /* So far we do not check that name is valid (TODO?) */ |
| 10003 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10004 | if (*name_end == '\0') { |
| 10005 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10006 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10007 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10008 | var = vpp ? *vpp : NULL; |
| 10009 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10010 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10011 | /* export -n NAME (without =VALUE) */ |
| 10012 | if (var) { |
| 10013 | var->flg_export = 0; |
| 10014 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10015 | unsetenv(name); |
| 10016 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10017 | continue; |
| 10018 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10019 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10020 | /* export NAME (without =VALUE) */ |
| 10021 | if (var) { |
| 10022 | var->flg_export = 1; |
| 10023 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10024 | putenv(var->varstr); |
| 10025 | continue; |
| 10026 | } |
| 10027 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10028 | if (flags & SETFLAG_MAKE_RO) { |
| 10029 | /* readonly NAME (without =VALUE) */ |
| 10030 | if (var) { |
| 10031 | var->flg_read_only = 1; |
| 10032 | continue; |
| 10033 | } |
| 10034 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10035 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10036 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10037 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10038 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10039 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10040 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10041 | continue; |
| 10042 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10043 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10044 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10045 | /* Exporting non-existing variable. |
| 10046 | * bash does not put it in environment, |
| 10047 | * but remembers that it is exported, |
| 10048 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10049 | * We just set it to "" and export. |
| 10050 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10051 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10052 | * bash sets the value to "". |
| 10053 | */ |
| 10054 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10055 | * bash remembers NAME and disallows its creation |
| 10056 | * in the future. |
| 10057 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10058 | name = xasprintf("%s=", name); |
| 10059 | } else { |
| 10060 | /* (Un)exporting/making local NAME=VALUE */ |
| 10061 | name = xstrdup(name); |
| 10062 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10063 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10064 | if (set_local_var(name, flags)) |
| 10065 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10066 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10067 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10068 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10069 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10070 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10071 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10072 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10073 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10074 | unsigned opt_unexport; |
| 10075 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10076 | #if ENABLE_HUSH_EXPORT_N |
| 10077 | /* "!": do not abort on errors */ |
| 10078 | opt_unexport = getopt32(argv, "!n"); |
| 10079 | if (opt_unexport == (uint32_t)-1) |
| 10080 | return EXIT_FAILURE; |
| 10081 | argv += optind; |
| 10082 | #else |
| 10083 | opt_unexport = 0; |
| 10084 | argv++; |
| 10085 | #endif |
| 10086 | |
| 10087 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10088 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10089 | if (e) { |
| 10090 | while (*e) { |
| 10091 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10092 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10093 | #else |
| 10094 | /* ash emits: export VAR='VAL' |
| 10095 | * bash: declare -x VAR="VAL" |
| 10096 | * we follow ash example */ |
| 10097 | const char *s = *e++; |
| 10098 | const char *p = strchr(s, '='); |
| 10099 | |
| 10100 | if (!p) /* wtf? take next variable */ |
| 10101 | continue; |
| 10102 | /* export var= */ |
| 10103 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10104 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10105 | putchar('\n'); |
| 10106 | #endif |
| 10107 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10108 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10109 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10110 | return EXIT_SUCCESS; |
| 10111 | } |
| 10112 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10113 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10114 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10115 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10116 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10117 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10118 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10119 | { |
| 10120 | if (G.func_nest_level == 0) { |
| 10121 | bb_error_msg("%s: not in a function", argv[0]); |
| 10122 | return EXIT_FAILURE; /* bash compat */ |
| 10123 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10124 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10125 | /* Since all builtins run in a nested variable level, |
| 10126 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10127 | * after builtin returns. |
| 10128 | */ |
| 10129 | return helper_export_local(argv, (G.var_nest_level - 1) << SETFLAG_VARLVL_SHIFT); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10130 | } |
| 10131 | #endif |
| 10132 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10133 | #if ENABLE_HUSH_READONLY |
| 10134 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10135 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10136 | argv++; |
| 10137 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10138 | /* bash: readonly [-p]: list all readonly VARs |
| 10139 | * (-p has no effect in bash) |
| 10140 | */ |
| 10141 | struct variable *e; |
| 10142 | for (e = G.top_var; e; e = e->next) { |
| 10143 | if (e->flg_read_only) { |
| 10144 | //TODO: quote value: readonly VAR='VAL' |
| 10145 | printf("readonly %s\n", e->varstr); |
| 10146 | } |
| 10147 | } |
| 10148 | return EXIT_SUCCESS; |
| 10149 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10150 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10151 | } |
| 10152 | #endif |
| 10153 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10154 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10155 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10156 | static int FAST_FUNC builtin_unset(char **argv) |
| 10157 | { |
| 10158 | int ret; |
| 10159 | unsigned opts; |
| 10160 | |
| 10161 | /* "!": do not abort on errors */ |
| 10162 | /* "+": stop at 1st non-option */ |
| 10163 | opts = getopt32(argv, "!+vf"); |
| 10164 | if (opts == (unsigned)-1) |
| 10165 | return EXIT_FAILURE; |
| 10166 | if (opts == 3) { |
| 10167 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10168 | return EXIT_FAILURE; |
| 10169 | } |
| 10170 | argv += optind; |
| 10171 | |
| 10172 | ret = EXIT_SUCCESS; |
| 10173 | while (*argv) { |
| 10174 | if (!(opts & 2)) { /* not -f */ |
| 10175 | if (unset_local_var(*argv)) { |
| 10176 | /* unset <nonexistent_var> doesn't fail. |
| 10177 | * Error is when one tries to unset RO var. |
| 10178 | * Message was printed by unset_local_var. */ |
| 10179 | ret = EXIT_FAILURE; |
| 10180 | } |
| 10181 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10182 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10183 | else { |
| 10184 | unset_func(*argv); |
| 10185 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10186 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10187 | argv++; |
| 10188 | } |
| 10189 | return ret; |
| 10190 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10191 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10192 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10193 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10194 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10195 | * built-in 'set' handler |
| 10196 | * SUSv3 says: |
| 10197 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10198 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10199 | * set -- [argument...] |
| 10200 | * set -o |
| 10201 | * set +o |
| 10202 | * Implementations shall support the options in both their hyphen and |
| 10203 | * plus-sign forms. These options can also be specified as options to sh. |
| 10204 | * Examples: |
| 10205 | * Write out all variables and their values: set |
| 10206 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10207 | * Turn on the -x and -v options: set -xv |
| 10208 | * Unset all positional parameters: set -- |
| 10209 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10210 | * Set the positional parameters to the expansion of x, even if x expands |
| 10211 | * with a leading '-' or '+': set -- $x |
| 10212 | * |
| 10213 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10214 | */ |
| 10215 | static int FAST_FUNC builtin_set(char **argv) |
| 10216 | { |
| 10217 | int n; |
| 10218 | char **pp, **g_argv; |
| 10219 | char *arg = *++argv; |
| 10220 | |
| 10221 | if (arg == NULL) { |
| 10222 | struct variable *e; |
| 10223 | for (e = G.top_var; e; e = e->next) |
| 10224 | puts(e->varstr); |
| 10225 | return EXIT_SUCCESS; |
| 10226 | } |
| 10227 | |
| 10228 | do { |
| 10229 | if (strcmp(arg, "--") == 0) { |
| 10230 | ++argv; |
| 10231 | goto set_argv; |
| 10232 | } |
| 10233 | if (arg[0] != '+' && arg[0] != '-') |
| 10234 | break; |
| 10235 | for (n = 1; arg[n]; ++n) { |
| 10236 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10237 | goto error; |
| 10238 | if (arg[n] == 'o' && argv[1]) |
| 10239 | argv++; |
| 10240 | } |
| 10241 | } while ((arg = *++argv) != NULL); |
| 10242 | /* Now argv[0] is 1st argument */ |
| 10243 | |
| 10244 | if (arg == NULL) |
| 10245 | return EXIT_SUCCESS; |
| 10246 | set_argv: |
| 10247 | |
| 10248 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10249 | g_argv = G.global_argv; |
| 10250 | if (G.global_args_malloced) { |
| 10251 | pp = g_argv; |
| 10252 | while (*++pp) |
| 10253 | free(*pp); |
| 10254 | g_argv[1] = NULL; |
| 10255 | } else { |
| 10256 | G.global_args_malloced = 1; |
| 10257 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10258 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10259 | g_argv = pp; |
| 10260 | } |
| 10261 | /* This realloc's G.global_argv */ |
| 10262 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10263 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10264 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10265 | |
| 10266 | return EXIT_SUCCESS; |
| 10267 | |
| 10268 | /* Nothing known, so abort */ |
| 10269 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10270 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10271 | return EXIT_FAILURE; |
| 10272 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10273 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10274 | |
| 10275 | static int FAST_FUNC builtin_shift(char **argv) |
| 10276 | { |
| 10277 | int n = 1; |
| 10278 | argv = skip_dash_dash(argv); |
| 10279 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10280 | n = bb_strtou(argv[0], NULL, 10); |
| 10281 | if (errno || n < 0) { |
| 10282 | /* shared string with ash.c */ |
| 10283 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10284 | /* |
| 10285 | * ash aborts in this case. |
| 10286 | * bash prints error message and set $? to 1. |
| 10287 | * Interestingly, for "shift 99999" bash does not |
| 10288 | * print error message, but does set $? to 1 |
| 10289 | * (and does no shifting at all). |
| 10290 | */ |
| 10291 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10292 | } |
| 10293 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10294 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10295 | int m = 1; |
| 10296 | while (m <= n) |
| 10297 | free(G.global_argv[m++]); |
| 10298 | } |
| 10299 | G.global_argc -= n; |
| 10300 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10301 | G.global_argc * sizeof(G.global_argv[0])); |
| 10302 | return EXIT_SUCCESS; |
| 10303 | } |
| 10304 | return EXIT_FAILURE; |
| 10305 | } |
| 10306 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10307 | #if ENABLE_HUSH_GETOPTS |
| 10308 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10309 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10310 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10311 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10312 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10313 | If a required argument is not found, and getopts is not silent, |
| 10314 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10315 | diagnostic message is printed. If getopts is silent, then a |
| 10316 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10317 | character found. |
| 10318 | |
| 10319 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10320 | |
| 10321 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10322 | */ |
| 10323 | char cbuf[2]; |
| 10324 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10325 | int c, n, exitcode, my_opterr; |
| 10326 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10327 | |
| 10328 | optstring = *++argv; |
| 10329 | if (!optstring || !(var = *++argv)) { |
| 10330 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10331 | return EXIT_FAILURE; |
| 10332 | } |
| 10333 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10334 | if (argv[1]) |
| 10335 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10336 | else |
| 10337 | argv = G.global_argv; |
| 10338 | cbuf[1] = '\0'; |
| 10339 | |
| 10340 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10341 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10342 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10343 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10344 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10345 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10346 | |
| 10347 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10348 | /*if (optstring[0] != '+')*/ { |
| 10349 | char *s = alloca(strlen(optstring) + 2); |
| 10350 | sprintf(s, "+%s", optstring); |
| 10351 | optstring = s; |
| 10352 | } |
| 10353 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10354 | /* Naively, now we should just |
| 10355 | * cp = get_local_var_value("OPTIND"); |
| 10356 | * optind = cp ? atoi(cp) : 0; |
| 10357 | * optarg = NULL; |
| 10358 | * opterr = my_opterr; |
| 10359 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10360 | * and be done? Not so fast... |
| 10361 | * Unlike normal getopt() usage in C programs, here |
| 10362 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10363 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10364 | * invocations of "getopts", there will be calls to shell builtins |
| 10365 | * which use getopt() internally. Example: |
| 10366 | * while getopts "abc" RES -a -bc -abc de; do |
| 10367 | * unset -ff func |
| 10368 | * done |
| 10369 | * This would not work correctly: getopt() call inside "unset" |
| 10370 | * modifies internal libc state which is tracking position in |
| 10371 | * multi-option strings ("-abc"). At best, it can skip options |
| 10372 | * or return the same option infinitely. With glibc implementation |
| 10373 | * of getopt(), it would use outright invalid pointers and return |
| 10374 | * garbage even _without_ "unset" mangling internal state. |
| 10375 | * |
| 10376 | * We resort to resetting getopt() state and calling it N times, |
| 10377 | * until we get Nth result (or failure). |
| 10378 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10379 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10380 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10381 | count = 0; |
| 10382 | n = string_array_len(argv); |
| 10383 | do { |
| 10384 | optarg = NULL; |
| 10385 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10386 | c = getopt(n, argv, optstring); |
| 10387 | if (c < 0) |
| 10388 | break; |
| 10389 | count++; |
| 10390 | } while (count <= G.getopt_count); |
| 10391 | |
| 10392 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10393 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10394 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10395 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10396 | |
| 10397 | /* Set OPTARG */ |
| 10398 | /* Always set or unset, never left as-is, even on exit/error: |
| 10399 | * "If no option was found, or if the option that was found |
| 10400 | * does not have an option-argument, OPTARG shall be unset." |
| 10401 | */ |
| 10402 | cp = optarg; |
| 10403 | if (c == '?') { |
| 10404 | /* If ":optstring" and unknown option is seen, |
| 10405 | * it is stored to OPTARG. |
| 10406 | */ |
| 10407 | if (optstring[1] == ':') { |
| 10408 | cbuf[0] = optopt; |
| 10409 | cp = cbuf; |
| 10410 | } |
| 10411 | } |
| 10412 | if (cp) |
| 10413 | set_local_var_from_halves("OPTARG", cp); |
| 10414 | else |
| 10415 | unset_local_var("OPTARG"); |
| 10416 | |
| 10417 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10418 | exitcode = EXIT_SUCCESS; |
| 10419 | if (c < 0) { /* -1: end of options */ |
| 10420 | exitcode = EXIT_FAILURE; |
| 10421 | c = '?'; |
| 10422 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10423 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10424 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10425 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10426 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10427 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10428 | return exitcode; |
| 10429 | } |
| 10430 | #endif |
| 10431 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10432 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10433 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10434 | char *arg_path, *filename; |
| 10435 | FILE *input; |
| 10436 | save_arg_t sv; |
| 10437 | char *args_need_save; |
| 10438 | #if ENABLE_HUSH_FUNCTIONS |
| 10439 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10440 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10441 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10442 | argv = skip_dash_dash(argv); |
| 10443 | filename = argv[0]; |
| 10444 | if (!filename) { |
| 10445 | /* bash says: "bash: .: filename argument required" */ |
| 10446 | return 2; /* bash compat */ |
| 10447 | } |
| 10448 | arg_path = NULL; |
| 10449 | if (!strchr(filename, '/')) { |
| 10450 | arg_path = find_in_path(filename); |
| 10451 | if (arg_path) |
| 10452 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10453 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10454 | errno = ENOENT; |
| 10455 | bb_simple_perror_msg(filename); |
| 10456 | return EXIT_FAILURE; |
| 10457 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10458 | } |
| 10459 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10460 | free(arg_path); |
| 10461 | if (!input) { |
| 10462 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10463 | /* POSIX: non-interactive shell should abort here, |
| 10464 | * not merely fail. So far no one complained :) |
| 10465 | */ |
| 10466 | return EXIT_FAILURE; |
| 10467 | } |
| 10468 | |
| 10469 | #if ENABLE_HUSH_FUNCTIONS |
| 10470 | sv_flg = G_flag_return_in_progress; |
| 10471 | /* "we are inside sourced file, ok to use return" */ |
| 10472 | G_flag_return_in_progress = -1; |
| 10473 | #endif |
| 10474 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10475 | if (args_need_save) |
| 10476 | save_and_replace_G_args(&sv, argv); |
| 10477 | |
| 10478 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10479 | G.last_exitcode = 0; |
| 10480 | parse_and_run_file(input); |
| 10481 | fclose_and_forget(input); |
| 10482 | |
| 10483 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10484 | restore_G_args(&sv, argv); |
| 10485 | #if ENABLE_HUSH_FUNCTIONS |
| 10486 | G_flag_return_in_progress = sv_flg; |
| 10487 | #endif |
| 10488 | |
| 10489 | return G.last_exitcode; |
| 10490 | } |
| 10491 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10492 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10493 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10494 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10495 | int sig; |
| 10496 | char *new_cmd; |
| 10497 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10498 | if (!G_traps) |
| 10499 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10500 | |
| 10501 | argv++; |
| 10502 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10503 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10504 | /* No args: print all trapped */ |
| 10505 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10506 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10507 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10508 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10509 | /* note: bash adds "SIG", but only if invoked |
| 10510 | * as "bash". If called as "sh", or if set -o posix, |
| 10511 | * then it prints short signal names. |
| 10512 | * We are printing short names: */ |
| 10513 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10514 | } |
| 10515 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10516 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10517 | return EXIT_SUCCESS; |
| 10518 | } |
| 10519 | |
| 10520 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10521 | /* If first arg is a number: reset all specified signals */ |
| 10522 | sig = bb_strtou(*argv, NULL, 10); |
| 10523 | if (errno == 0) { |
| 10524 | int ret; |
| 10525 | process_sig_list: |
| 10526 | ret = EXIT_SUCCESS; |
| 10527 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10528 | sighandler_t handler; |
| 10529 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10530 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10531 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10532 | ret = EXIT_FAILURE; |
| 10533 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10534 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10535 | continue; |
| 10536 | } |
| 10537 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10538 | free(G_traps[sig]); |
| 10539 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10540 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10541 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10542 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10543 | |
| 10544 | /* There is no signal for 0 (EXIT) */ |
| 10545 | if (sig == 0) |
| 10546 | continue; |
| 10547 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10548 | if (new_cmd) |
| 10549 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10550 | else |
| 10551 | /* We are removing trap handler */ |
| 10552 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10553 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10554 | } |
| 10555 | return ret; |
| 10556 | } |
| 10557 | |
| 10558 | if (!argv[1]) { /* no second arg */ |
| 10559 | bb_error_msg("trap: invalid arguments"); |
| 10560 | return EXIT_FAILURE; |
| 10561 | } |
| 10562 | |
| 10563 | /* First arg is "-": reset all specified to default */ |
| 10564 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10565 | /* Everything else: set arg as signal handler |
| 10566 | * (includes "" case, which ignores signal) */ |
| 10567 | if (argv[0][0] == '-') { |
| 10568 | if (argv[0][1] == '\0') { /* "-" */ |
| 10569 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10570 | goto reset_traps; |
| 10571 | } |
| 10572 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10573 | argv++; |
| 10574 | } |
| 10575 | /* else: "-something", no special meaning */ |
| 10576 | } |
| 10577 | new_cmd = *argv; |
| 10578 | reset_traps: |
| 10579 | argv++; |
| 10580 | goto process_sig_list; |
| 10581 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10582 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10583 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10584 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10585 | static struct pipe *parse_jobspec(const char *str) |
| 10586 | { |
| 10587 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10588 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10589 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10590 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10591 | if (str[0] != '%' |
| 10592 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10593 | ) { |
| 10594 | bb_error_msg("bad argument '%s'", str); |
| 10595 | return NULL; |
| 10596 | } |
| 10597 | /* It is "%%", "%+" or "%" - current job */ |
| 10598 | jobnum = G.last_jobid; |
| 10599 | if (jobnum == 0) { |
| 10600 | bb_error_msg("no current job"); |
| 10601 | return NULL; |
| 10602 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10603 | } |
| 10604 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10605 | if (pi->jobid == jobnum) { |
| 10606 | return pi; |
| 10607 | } |
| 10608 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10609 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10610 | return NULL; |
| 10611 | } |
| 10612 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10613 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10614 | { |
| 10615 | struct pipe *job; |
| 10616 | const char *status_string; |
| 10617 | |
| 10618 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10619 | for (job = G.job_list; job; job = job->next) { |
| 10620 | if (job->alive_cmds == job->stopped_cmds) |
| 10621 | status_string = "Stopped"; |
| 10622 | else |
| 10623 | status_string = "Running"; |
| 10624 | |
| 10625 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10626 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10627 | |
| 10628 | clean_up_last_dead_job(); |
| 10629 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10630 | return EXIT_SUCCESS; |
| 10631 | } |
| 10632 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10633 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10634 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10635 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10636 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10637 | struct pipe *pi; |
| 10638 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10639 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10640 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10641 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10642 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10643 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10644 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10645 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10646 | goto found; |
| 10647 | } |
| 10648 | } |
| 10649 | bb_error_msg("%s: no current job", argv[0]); |
| 10650 | return EXIT_FAILURE; |
| 10651 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10652 | |
| 10653 | pi = parse_jobspec(argv[1]); |
| 10654 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10655 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10656 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10657 | /* TODO: bash prints a string representation |
| 10658 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10659 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10660 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10661 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10662 | } |
| 10663 | |
| 10664 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10665 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10666 | for (i = 0; i < pi->num_cmds; i++) { |
| 10667 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10668 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10669 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10670 | |
| 10671 | i = kill(- pi->pgrp, SIGCONT); |
| 10672 | if (i < 0) { |
| 10673 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10674 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10675 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10676 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10677 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10678 | } |
| 10679 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10680 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10681 | remove_job_from_table(pi); /* FG job shouldn't be in job table */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10682 | return checkjobs_and_fg_shell(pi); |
| 10683 | } |
| 10684 | return EXIT_SUCCESS; |
| 10685 | } |
| 10686 | #endif |
| 10687 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10688 | #if ENABLE_HUSH_KILL |
| 10689 | static int FAST_FUNC builtin_kill(char **argv) |
| 10690 | { |
| 10691 | int ret = 0; |
| 10692 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10693 | # if ENABLE_HUSH_JOB |
| 10694 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10695 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10696 | |
| 10697 | do { |
| 10698 | struct pipe *pi; |
| 10699 | char *dst; |
| 10700 | int j, n; |
| 10701 | |
| 10702 | if (argv[i][0] != '%') |
| 10703 | continue; |
| 10704 | /* |
| 10705 | * "kill %N" - job kill |
| 10706 | * Converting to pgrp / pid kill |
| 10707 | */ |
| 10708 | pi = parse_jobspec(argv[i]); |
| 10709 | if (!pi) { |
| 10710 | /* Eat bad jobspec */ |
| 10711 | j = i; |
| 10712 | do { |
| 10713 | j++; |
| 10714 | argv[j - 1] = argv[j]; |
| 10715 | } while (argv[j]); |
| 10716 | ret = 1; |
| 10717 | i--; |
| 10718 | continue; |
| 10719 | } |
| 10720 | /* |
| 10721 | * In jobs started under job control, we signal |
| 10722 | * entire process group by kill -PGRP_ID. |
| 10723 | * This happens, f.e., in interactive shell. |
| 10724 | * |
| 10725 | * Otherwise, we signal each child via |
| 10726 | * kill PID1 PID2 PID3. |
| 10727 | * Testcases: |
| 10728 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10729 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10730 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10731 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10732 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10733 | dst = alloca(n * sizeof(int)*4); |
| 10734 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10735 | if (G_interactive_fd) |
| 10736 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10737 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10738 | struct command *cmd = &pi->cmds[j]; |
| 10739 | /* Skip exited members of the job */ |
| 10740 | if (cmd->pid == 0) |
| 10741 | continue; |
| 10742 | /* |
| 10743 | * kill_main has matching code to expect |
| 10744 | * leading space. Needed to not confuse |
| 10745 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10746 | */ |
| 10747 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10748 | } |
| 10749 | *dst = '\0'; |
| 10750 | } while (argv[++i]); |
| 10751 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10752 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10753 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10754 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10755 | ret = run_applet_main(argv, kill_main); |
| 10756 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10757 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10758 | return ret; |
| 10759 | } |
| 10760 | #endif |
| 10761 | |
| 10762 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10763 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10764 | #if !ENABLE_HUSH_JOB |
| 10765 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10766 | #endif |
| 10767 | 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] | 10768 | { |
| 10769 | int ret = 0; |
| 10770 | for (;;) { |
| 10771 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10772 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10773 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10774 | if (!sigisemptyset(&G.pending_set)) |
| 10775 | goto check_sig; |
| 10776 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10777 | /* waitpid is not interruptible by SA_RESTARTed |
| 10778 | * signals which we use. Thus, this ugly dance: |
| 10779 | */ |
| 10780 | |
| 10781 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10782 | * pending signal mask before we call waitpid. |
| 10783 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10784 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10785 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10786 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 10787 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10788 | |
| 10789 | if (!sigisemptyset(&G.pending_set)) { |
| 10790 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10791 | goto restore; |
| 10792 | } |
| 10793 | |
| 10794 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10795 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10796 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10797 | debug_printf_exec("checkjobs:%d\n", ret); |
| 10798 | #if ENABLE_HUSH_JOB |
| 10799 | if (waitfor_pipe) { |
| 10800 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 10801 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 10802 | if (rcode >= 0) { |
| 10803 | ret = rcode; |
| 10804 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10805 | break; |
| 10806 | } |
| 10807 | } |
| 10808 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10809 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 10810 | /* if ret == 0, no children changed state */ |
| 10811 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10812 | if (errno == ECHILD || ret) { |
| 10813 | ret--; |
| 10814 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10815 | ret = 0; |
| 10816 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10817 | break; |
| 10818 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10819 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10820 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 10821 | /* Note: sigsuspend invokes signal handler */ |
| 10822 | sigsuspend(&oldset); |
| 10823 | restore: |
| 10824 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10825 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10826 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10827 | sig = check_and_run_traps(); |
| 10828 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 10829 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10830 | ret = 128 + sig; |
| 10831 | break; |
| 10832 | } |
| 10833 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 10834 | } |
| 10835 | return ret; |
| 10836 | } |
| 10837 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10838 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10839 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10840 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10841 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10842 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10843 | argv = skip_dash_dash(argv); |
| 10844 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 10845 | /* Don't care about wait results */ |
| 10846 | /* Note 1: must wait until there are no more children */ |
| 10847 | /* Note 2: must be interruptible */ |
| 10848 | /* Examples: |
| 10849 | * $ sleep 3 & sleep 6 & wait |
| 10850 | * [1] 30934 sleep 3 |
| 10851 | * [2] 30935 sleep 6 |
| 10852 | * [1] Done sleep 3 |
| 10853 | * [2] Done sleep 6 |
| 10854 | * $ sleep 3 & sleep 6 & wait |
| 10855 | * [1] 30936 sleep 3 |
| 10856 | * [2] 30937 sleep 6 |
| 10857 | * [1] Done sleep 3 |
| 10858 | * ^C <-- after ~4 sec from keyboard |
| 10859 | * $ |
| 10860 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10861 | 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] | 10862 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10863 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10864 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10865 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10866 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10867 | #if ENABLE_HUSH_JOB |
| 10868 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10869 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10870 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10871 | wait_pipe = parse_jobspec(*argv); |
| 10872 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10873 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10874 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10875 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10876 | } else { |
| 10877 | /* waiting on "last dead job" removes it */ |
| 10878 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 10879 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10880 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10881 | /* else: parse_jobspec() already emitted error msg */ |
| 10882 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10883 | } |
| 10884 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10885 | /* mimic bash message */ |
| 10886 | 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] | 10887 | ret = EXIT_FAILURE; |
| 10888 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10889 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10890 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10891 | /* Do we have such child? */ |
| 10892 | ret = waitpid(pid, &status, WNOHANG); |
| 10893 | if (ret < 0) { |
| 10894 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10895 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10896 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 10897 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10898 | /* "wait $!" but last bg task has already exited. Try: |
| 10899 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 10900 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10901 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10902 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10903 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10904 | } else { |
| 10905 | /* Example: "wait 1". mimic bash message */ |
| 10906 | 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] | 10907 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10908 | } else { |
| 10909 | /* ??? */ |
| 10910 | bb_perror_msg("wait %s", *argv); |
| 10911 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10912 | continue; /* bash checks all argv[] */ |
| 10913 | } |
| 10914 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10915 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10916 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10917 | } else { |
| 10918 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10919 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 10920 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10921 | if (WIFSIGNALED(status)) |
| 10922 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10923 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10924 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10925 | |
| 10926 | return ret; |
| 10927 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10928 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10929 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10930 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 10931 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 10932 | { |
| 10933 | if (argv[1]) { |
| 10934 | def = bb_strtou(argv[1], NULL, 10); |
| 10935 | if (errno || def < def_min || argv[2]) { |
| 10936 | bb_error_msg("%s: bad arguments", argv[0]); |
| 10937 | def = UINT_MAX; |
| 10938 | } |
| 10939 | } |
| 10940 | return def; |
| 10941 | } |
| 10942 | #endif |
| 10943 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10944 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10945 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10946 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10947 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10948 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10949 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10950 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 10951 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 10952 | return EXIT_SUCCESS; /* bash compat */ |
| 10953 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10954 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10955 | |
| 10956 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 10957 | if (depth == UINT_MAX) |
| 10958 | G.flag_break_continue = BC_BREAK; |
| 10959 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10960 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10961 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10962 | return EXIT_SUCCESS; |
| 10963 | } |
| 10964 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10965 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10966 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10967 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 10968 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10969 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10970 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10971 | |
| 10972 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10973 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10974 | { |
| 10975 | int rc; |
| 10976 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10977 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10978 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 10979 | return EXIT_FAILURE; /* bash compat */ |
| 10980 | } |
| 10981 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10982 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10983 | |
| 10984 | /* bash: |
| 10985 | * out of range: wraps around at 256, does not error out |
| 10986 | * non-numeric param: |
| 10987 | * f() { false; return qwe; }; f; echo $? |
| 10988 | * bash: return: qwe: numeric argument required <== we do this |
| 10989 | * 255 <== we also do this |
| 10990 | */ |
| 10991 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 10992 | return rc; |
| 10993 | } |
| 10994 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10995 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 10996 | #if ENABLE_HUSH_TIMES |
| 10997 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 10998 | { |
| 10999 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11000 | ' ', offsetof(struct tms, tms_utime), |
| 11001 | '\n', offsetof(struct tms, tms_stime), |
| 11002 | ' ', offsetof(struct tms, tms_cutime), |
| 11003 | '\n', offsetof(struct tms, tms_cstime), |
| 11004 | 0 |
| 11005 | }; |
| 11006 | const uint8_t *p; |
| 11007 | unsigned clk_tck; |
| 11008 | struct tms buf; |
| 11009 | |
| 11010 | clk_tck = bb_clk_tck(); |
| 11011 | |
| 11012 | times(&buf); |
| 11013 | p = times_tbl; |
| 11014 | do { |
| 11015 | unsigned sec, frac; |
| 11016 | unsigned long t; |
| 11017 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11018 | sec = t / clk_tck; |
| 11019 | frac = t % clk_tck; |
| 11020 | printf("%um%u.%03us%c", |
| 11021 | sec / 60, sec % 60, |
| 11022 | (frac * 1000) / clk_tck, |
| 11023 | p[0]); |
| 11024 | p += 2; |
| 11025 | } while (*p); |
| 11026 | |
| 11027 | return EXIT_SUCCESS; |
| 11028 | } |
| 11029 | #endif |
| 11030 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11031 | #if ENABLE_HUSH_MEMLEAK |
| 11032 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11033 | { |
| 11034 | void *p; |
| 11035 | unsigned long l; |
| 11036 | |
| 11037 | # ifdef M_TRIM_THRESHOLD |
| 11038 | /* Optional. Reduces probability of false positives */ |
| 11039 | malloc_trim(0); |
| 11040 | # endif |
| 11041 | /* Crude attempt to find where "free memory" starts, |
| 11042 | * sans fragmentation. */ |
| 11043 | p = malloc(240); |
| 11044 | l = (unsigned long)p; |
| 11045 | free(p); |
| 11046 | p = malloc(3400); |
| 11047 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11048 | free(p); |
| 11049 | |
| 11050 | |
| 11051 | # if 0 /* debug */ |
| 11052 | { |
| 11053 | struct mallinfo mi = mallinfo(); |
| 11054 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11055 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11056 | } |
| 11057 | # endif |
| 11058 | |
| 11059 | if (!G.memleak_value) |
| 11060 | G.memleak_value = l; |
| 11061 | |
| 11062 | l -= G.memleak_value; |
| 11063 | if ((long)l < 0) |
| 11064 | l = 0; |
| 11065 | l /= 1024; |
| 11066 | if (l > 127) |
| 11067 | l = 127; |
| 11068 | |
| 11069 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11070 | return l; |
| 11071 | } |
| 11072 | #endif |