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 |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 82 | * |
| 83 | * Status of [[ support: |
| 84 | * [[ args ]] are CMD_SINGLEWORD_NOGLOB: |
| 85 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 86 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 87 | * TODO: |
| 88 | * &&/|| are AND/OR ops, -a/-o are not |
| 89 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
| 90 | * = is glob match operator, not equality operator: STR = GLOB |
| 91 | * (in GLOB, quoting is significant on char-by-char basis: a*cd"*") |
| 92 | * == same as = |
| 93 | * add =~ regex match operator: STR =~ REGEX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 94 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 95 | //config:config HUSH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 96 | //config: bool "hush (68 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 97 | //config: default y |
| 98 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 99 | //config: hush is a small shell. It handles the normal flow control |
| 100 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 101 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 102 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 104 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 105 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 106 | //config: It does not handle select, aliases, tilde expansion, |
| 107 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 108 | //config: |
| 109 | //config:config HUSH_BASH_COMPAT |
| 110 | //config: bool "bash-compatible extensions" |
| 111 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 112 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 113 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 114 | //config:config HUSH_BRACE_EXPANSION |
| 115 | //config: bool "Brace expansion" |
| 116 | //config: default y |
| 117 | //config: depends on HUSH_BASH_COMPAT |
| 118 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 119 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 120 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 121 | //config:config HUSH_LINENO_VAR |
| 122 | //config: bool "$LINENO variable" |
| 123 | //config: default y |
| 124 | //config: depends on HUSH_BASH_COMPAT |
| 125 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 126 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 127 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 128 | //config: default n # do not encourage non-standard behavior |
| 129 | //config: depends on HUSH_BASH_COMPAT |
| 130 | //config: help |
| 131 | //config: This is not compliant with standards. Avoid if possible. |
| 132 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 133 | //config:config HUSH_INTERACTIVE |
| 134 | //config: bool "Interactive mode" |
| 135 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 136 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 137 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 138 | //config: Enable interactive mode (prompt and command editing). |
| 139 | //config: Without this, hush simply reads and executes commands |
| 140 | //config: from stdin just like a shell script from a file. |
| 141 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 143 | //config:config HUSH_SAVEHISTORY |
| 144 | //config: bool "Save command history to .hush_history" |
| 145 | //config: default y |
| 146 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 147 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 148 | //config:config HUSH_JOB |
| 149 | //config: bool "Job control" |
| 150 | //config: default y |
| 151 | //config: depends on HUSH_INTERACTIVE |
| 152 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 153 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 154 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 155 | //config: "cmd &" still works by simply spawning a process and immediately |
| 156 | //config: prompting for next command (or executing next command in a script), |
| 157 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 158 | //config: |
| 159 | //config:config HUSH_TICK |
Ron Yorston | 060f0a0 | 2018-11-09 12:00:39 +0000 | [diff] [blame] | 160 | //config: bool "Support command substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 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: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 164 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config: |
| 166 | //config:config HUSH_IF |
| 167 | //config: bool "Support if/then/elif/else/fi" |
| 168 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 169 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 170 | //config: |
| 171 | //config:config HUSH_LOOPS |
| 172 | //config: bool "Support for, while and until loops" |
| 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: |
| 176 | //config:config HUSH_CASE |
| 177 | //config: bool "Support case ... esac statement" |
| 178 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 179 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 180 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 181 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 182 | //config: |
| 183 | //config:config HUSH_FUNCTIONS |
| 184 | //config: bool "Support funcname() { commands; } syntax" |
| 185 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 186 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 187 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 188 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: |
| 190 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 191 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 192 | //config: default y |
| 193 | //config: depends on HUSH_FUNCTIONS |
| 194 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 195 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 196 | //config: |
| 197 | //config:config HUSH_RANDOM_SUPPORT |
| 198 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 199 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 200 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 201 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 202 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 203 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 204 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 205 | //config:config HUSH_MODE_X |
| 206 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 207 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 208 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 209 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 210 | //config: This instructs hush to print commands before execution. |
| 211 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 212 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 213 | //config:config HUSH_ECHO |
| 214 | //config: bool "echo builtin" |
| 215 | //config: default y |
| 216 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 217 | //config: |
| 218 | //config:config HUSH_PRINTF |
| 219 | //config: bool "printf builtin" |
| 220 | //config: default y |
| 221 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 222 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 223 | //config:config HUSH_TEST |
| 224 | //config: bool "test builtin" |
| 225 | //config: default y |
| 226 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 227 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 228 | //config:config HUSH_HELP |
| 229 | //config: bool "help builtin" |
| 230 | //config: default y |
| 231 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 232 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 233 | //config:config HUSH_EXPORT |
| 234 | //config: bool "export builtin" |
| 235 | //config: default y |
| 236 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 237 | //config: |
| 238 | //config:config HUSH_EXPORT_N |
| 239 | //config: bool "Support 'export -n' option" |
| 240 | //config: default y |
| 241 | //config: depends on HUSH_EXPORT |
| 242 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 243 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 244 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 245 | //config:config HUSH_READONLY |
| 246 | //config: bool "readonly builtin" |
| 247 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 248 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 249 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 250 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 251 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 252 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 253 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 254 | //config: default y |
| 255 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 256 | //config: |
| 257 | //config:config HUSH_WAIT |
| 258 | //config: bool "wait builtin" |
| 259 | //config: default y |
| 260 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 261 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 262 | //config:config HUSH_COMMAND |
| 263 | //config: bool "command builtin" |
| 264 | //config: default y |
| 265 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 266 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 267 | //config:config HUSH_TRAP |
| 268 | //config: bool "trap builtin" |
| 269 | //config: default y |
| 270 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 271 | //config: |
| 272 | //config:config HUSH_TYPE |
| 273 | //config: bool "type builtin" |
| 274 | //config: default y |
| 275 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 276 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 277 | //config:config HUSH_TIMES |
| 278 | //config: bool "times builtin" |
| 279 | //config: default y |
| 280 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 281 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 282 | //config:config HUSH_READ |
| 283 | //config: bool "read builtin" |
| 284 | //config: default y |
| 285 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 286 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 287 | //config:config HUSH_SET |
| 288 | //config: bool "set builtin" |
| 289 | //config: default y |
| 290 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 291 | //config: |
| 292 | //config:config HUSH_UNSET |
| 293 | //config: bool "unset builtin" |
| 294 | //config: default y |
| 295 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 296 | //config: |
| 297 | //config:config HUSH_ULIMIT |
| 298 | //config: bool "ulimit builtin" |
| 299 | //config: default y |
| 300 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 301 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 302 | //config:config HUSH_UMASK |
| 303 | //config: bool "umask builtin" |
| 304 | //config: default y |
| 305 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 306 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 307 | //config:config HUSH_GETOPTS |
| 308 | //config: bool "getopts builtin" |
| 309 | //config: default y |
| 310 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 311 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 312 | //config:config HUSH_MEMLEAK |
| 313 | //config: bool "memleak builtin (debugging)" |
| 314 | //config: default n |
| 315 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 317 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 318 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 319 | //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] | 320 | //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] | 321 | |
| 322 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 323 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 324 | //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] | 325 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 326 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 327 | /* -i (interactive) is also accepted, |
| 328 | * but does nothing, therefore not shown in help. |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 329 | * NOMMU-specific options are not meant to be used by users, |
| 330 | * therefore we don't show them either. |
| 331 | */ |
| 332 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 333 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 334 | //usage:#define hush_full_usage "\n\n" |
| 335 | //usage: "Unix shell interpreter" |
| 336 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 337 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 338 | || defined(__APPLE__) \ |
| 339 | ) |
| 340 | # include <malloc.h> /* for malloc_trim */ |
| 341 | #endif |
| 342 | #include <glob.h> |
| 343 | /* #include <dmalloc.h> */ |
| 344 | #if ENABLE_HUSH_CASE |
| 345 | # include <fnmatch.h> |
| 346 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 347 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 348 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 349 | |
| 350 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 351 | #include "unicode.h" |
| 352 | #include "shell_common.h" |
| 353 | #include "math.h" |
| 354 | #include "match.h" |
| 355 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 356 | # include "random.h" |
| 357 | #else |
| 358 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 359 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 360 | #ifndef O_CLOEXEC |
| 361 | # define O_CLOEXEC 0 |
| 362 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 363 | #ifndef F_DUPFD_CLOEXEC |
| 364 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 365 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 366 | |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 367 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !(ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH) |
| 368 | # include "embedded_scripts.h" |
| 369 | #else |
| 370 | # define NUM_SCRIPTS 0 |
| 371 | #endif |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 372 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 373 | /* So far, all bash compat is controlled by one config option */ |
| 374 | /* Separate defines document which part of code implements what */ |
| 375 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 376 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 377 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 378 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 379 | #define BASH_EPOCH_VARS ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 380 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 381 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 382 | |
| 383 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 384 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 385 | #define LEAK_HUNTING 0 |
| 386 | #define BUILD_AS_NOMMU 0 |
| 387 | /* Enable/disable sanity checks. Ok to enable in production, |
| 388 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 389 | * Keeping 1 for now even in released versions. |
| 390 | */ |
| 391 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 392 | /* Slightly bigger (+200 bytes), but faster hush. |
| 393 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 394 | * which allows us to do fewer waitpid's. |
| 395 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 396 | * and therefore waitpid will return the same result as last time) |
| 397 | */ |
| 398 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 399 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 400 | * So far ${var%...} ops are always enabled: |
| 401 | */ |
| 402 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 403 | |
| 404 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 405 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 406 | # undef BB_MMU |
| 407 | # undef USE_FOR_NOMMU |
| 408 | # undef USE_FOR_MMU |
| 409 | # define BB_MMU 0 |
| 410 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 411 | # define USE_FOR_MMU(...) |
| 412 | #endif |
| 413 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 414 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 415 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 416 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 417 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 418 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 419 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 420 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 421 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 422 | # define IF_FEATURE_SH_STANDALONE(...) |
| 423 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 424 | #endif |
| 425 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 426 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 427 | # undef ENABLE_FEATURE_EDITING |
| 428 | # define ENABLE_FEATURE_EDITING 0 |
| 429 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 430 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 431 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 432 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 433 | #endif |
| 434 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 435 | /* Do we support ANY keywords? */ |
| 436 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 437 | # define HAS_KEYWORDS 1 |
| 438 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 439 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 440 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 441 | # define HAS_KEYWORDS 0 |
| 442 | # define IF_HAS_KEYWORDS(...) |
| 443 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 444 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 446 | /* If you comment out one of these below, it will be #defined later |
| 447 | * to perform debug printfs to stderr: */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 448 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 449 | /* Finer-grained debug switches */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 450 | #define debug_printf_parse(...) do {} while (0) |
| 451 | #define debug_printf_heredoc(...) do {} while (0) |
| 452 | #define debug_print_tree(a, b) do {} while (0) |
| 453 | #define debug_printf_exec(...) do {} while (0) |
| 454 | #define debug_printf_env(...) do {} while (0) |
| 455 | #define debug_printf_jobs(...) do {} while (0) |
| 456 | #define debug_printf_expand(...) do {} while (0) |
| 457 | #define debug_printf_varexp(...) do {} while (0) |
| 458 | #define debug_printf_glob(...) do {} while (0) |
| 459 | #define debug_printf_redir(...) do {} while (0) |
| 460 | #define debug_printf_list(...) do {} while (0) |
| 461 | #define debug_printf_subst(...) do {} while (0) |
| 462 | #define debug_printf_prompt(...) do {} while (0) |
| 463 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 464 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 465 | #define ERR_PTR ((void*)(long)1) |
| 466 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 467 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 468 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 469 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 470 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 471 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 472 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 473 | /* Support / and // replace ops */ |
| 474 | /* Note that // is stored as \ in "encoded" string representation */ |
| 475 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 476 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 477 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 478 | #else |
| 479 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 480 | # define VAR_SUBST_OPS "%#:-=+?" |
| 481 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 482 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 483 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 484 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 485 | #define SPECIAL_VAR_SYMBOL 3 |
| 486 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 487 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 488 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 489 | struct variable; |
| 490 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 491 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 492 | |
| 493 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 494 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 495 | */ |
| 496 | #if !BB_MMU |
| 497 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 498 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 499 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 500 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 501 | } nommu_save_t; |
| 502 | #endif |
| 503 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 504 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 505 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 506 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 507 | RES_IF , |
| 508 | RES_THEN , |
| 509 | RES_ELIF , |
| 510 | RES_ELSE , |
| 511 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 512 | #endif |
| 513 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 514 | RES_FOR , |
| 515 | RES_WHILE , |
| 516 | RES_UNTIL , |
| 517 | RES_DO , |
| 518 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 519 | #endif |
| 520 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 521 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 522 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 523 | #if ENABLE_HUSH_CASE |
| 524 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 525 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 526 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 527 | RES_MATCH , /* "word)" */ |
| 528 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 529 | RES_ESAC , |
| 530 | #endif |
| 531 | RES_XXXX , |
| 532 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 533 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 534 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 535 | typedef struct o_string { |
| 536 | char *data; |
| 537 | int length; /* position where data is appended */ |
| 538 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 539 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 540 | /* At least some part of the string was inside '' or "", |
| 541 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 542 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 543 | smallint has_empty_slot; |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 544 | smallint ended_in_ifs; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 545 | } o_string; |
| 546 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 547 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 548 | EXP_FLAG_GLOB = 0x2, |
| 549 | /* Protect newly added chars against globbing |
| 550 | * by prepending \ to *, ?, [, \ */ |
| 551 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 552 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 553 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 554 | #define NULL_O_STRING { NULL } |
| 555 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 556 | #ifndef debug_printf_parse |
| 557 | static const char *const assignment_flag[] = { |
| 558 | "MAYBE_ASSIGNMENT", |
| 559 | "DEFINITELY_ASSIGNMENT", |
| 560 | "NOT_ASSIGNMENT", |
| 561 | "WORD_IS_KEYWORD", |
| 562 | }; |
| 563 | #endif |
| 564 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 565 | /* We almost can use standard FILE api, but we need an ability to move |
| 566 | * its fd when redirects coincide with it. No api exists for that |
| 567 | * (RFE for it at https://sourceware.org/bugzilla/show_bug.cgi?id=21902). |
| 568 | * HFILE is our internal alternative. Only supports reading. |
| 569 | * Since we now can, we incorporate linked list of all opened HFILEs |
| 570 | * into the struct (used to be a separate mini-list). |
| 571 | */ |
| 572 | typedef struct HFILE { |
| 573 | char *cur; |
| 574 | char *end; |
| 575 | struct HFILE *next_hfile; |
| 576 | int is_stdin; |
| 577 | int fd; |
| 578 | char buf[1024]; |
| 579 | } HFILE; |
| 580 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 581 | typedef struct in_str { |
| 582 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 583 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 584 | int last_char; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 585 | HFILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 586 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 587 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 588 | /* The descrip member of this structure is only used to make |
| 589 | * debugging output pretty */ |
| 590 | static const struct { |
| 591 | int mode; |
| 592 | signed char default_fd; |
| 593 | char descrip[3]; |
| 594 | } redir_table[] = { |
| 595 | { O_RDONLY, 0, "<" }, |
| 596 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 597 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 598 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 599 | { O_RDONLY, 0, "<<" }, |
| 600 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 601 | /* { O_RDONLY, 77, "<<" }, */ |
| 602 | }; |
| 603 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 604 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 605 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 606 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 607 | int rd_fd; /* fd to redirect */ |
| 608 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 609 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 610 | smallint rd_type; /* (enum redir_type) */ |
| 611 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 612 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 613 | * bit 0: do we need to trim leading tabs? |
| 614 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 615 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 616 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 617 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 618 | REDIRECT_INPUT = 0, |
| 619 | REDIRECT_OVERWRITE = 1, |
| 620 | REDIRECT_APPEND = 2, |
| 621 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 622 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 623 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 624 | |
| 625 | REDIRFD_CLOSE = -3, |
| 626 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 627 | REDIRFD_TO_FILE = -1, |
| 628 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 629 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 630 | HEREDOC_SKIPTABS = 1, |
| 631 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 632 | } redir_type; |
| 633 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 634 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 635 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 636 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 637 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 638 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 639 | unsigned lineno; |
| 640 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 641 | smallint cmd_type; /* CMD_xxx */ |
| 642 | #define CMD_NORMAL 0 |
| 643 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 644 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 645 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 646 | * "export v=t*" |
| 647 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 648 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 649 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 650 | #if ENABLE_HUSH_FUNCTIONS |
| 651 | # define CMD_FUNCDEF 3 |
| 652 | #endif |
| 653 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 654 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 655 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 656 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 657 | #if !BB_MMU |
| 658 | char *group_as_string; |
| 659 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 660 | #if ENABLE_HUSH_FUNCTIONS |
| 661 | struct function *child_func; |
| 662 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 663 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 664 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 665 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 666 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 667 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 668 | * we put those fields back into cmd->xxx |
| 669 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 670 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 671 | * Without this trick, loop would execute a;b;b;b;... |
| 672 | * instead of correct sequence a;b;a;b;... |
| 673 | * When command is freed, it severs the link |
| 674 | * (sets ->child_func->parent_cmd to NULL). |
| 675 | */ |
| 676 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 677 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 678 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 679 | * and on execution these are substituted with their values. |
| 680 | * Substitution can make _several_ words out of one argv[n]! |
| 681 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 682 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 683 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 684 | struct redir_struct *redirects; /* I/O redirections */ |
| 685 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 686 | /* Is there anything in this command at all? */ |
| 687 | #define IS_NULL_CMD(cmd) \ |
| 688 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 689 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 690 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 691 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 692 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 693 | int alive_cmds; /* number of commands running (not exited) */ |
| 694 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 695 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 696 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 697 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 698 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 699 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 700 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 701 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 702 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 703 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 704 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 705 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 706 | PIPE_SEQ = 0, |
| 707 | PIPE_AND = 1, |
| 708 | PIPE_OR = 2, |
| 709 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 710 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 711 | /* Is there anything in this pipe at all? */ |
| 712 | #define IS_NULL_PIPE(pi) \ |
| 713 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 714 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 715 | /* This holds pointers to the various results of parsing */ |
| 716 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 717 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 718 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 719 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 720 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 721 | /* last command in pipe (being constructed right now) */ |
| 722 | struct command *command; |
| 723 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 724 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 725 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 726 | #if !BB_MMU |
| 727 | o_string as_string; |
| 728 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 729 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 730 | #if HAS_KEYWORDS |
| 731 | smallint ctx_res_w; |
| 732 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 733 | #if ENABLE_HUSH_CASE |
| 734 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 735 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 736 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 737 | int old_flag; |
| 738 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 739 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 740 | * when we see "if" or "then", we malloc and copy current context, |
| 741 | * and make ->stack point to it. then we parse pipeN. |
| 742 | * when closing "then" / fi" / whatever is found, |
| 743 | * we move list_head into ->stack->command->group, |
| 744 | * copy ->stack into current context, and delete ->stack. |
| 745 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 746 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 747 | struct parse_context *stack; |
| 748 | #endif |
| 749 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 750 | enum { |
| 751 | MAYBE_ASSIGNMENT = 0, |
| 752 | DEFINITELY_ASSIGNMENT = 1, |
| 753 | NOT_ASSIGNMENT = 2, |
| 754 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 755 | WORD_IS_KEYWORD = 3, |
| 756 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 757 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 758 | /* On program start, environ points to initial environment. |
| 759 | * putenv adds new pointers into it, unsetenv removes them. |
| 760 | * Neither of these (de)allocates the strings. |
| 761 | * setenv allocates new strings in malloc space and does putenv, |
| 762 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 763 | #define setenv(...) setenv_is_leaky_dont_use() |
| 764 | struct variable { |
| 765 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 766 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 767 | 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] | 768 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 769 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 770 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 773 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 774 | BC_BREAK = 1, |
| 775 | BC_CONTINUE = 2, |
| 776 | }; |
| 777 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 778 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 779 | struct function { |
| 780 | struct function *next; |
| 781 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 782 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 783 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 784 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 785 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 786 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 787 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 788 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 789 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 790 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 791 | /* set -/+o OPT support. (TODO: make it optional) |
| 792 | * bash supports the following opts: |
| 793 | * allexport off |
| 794 | * braceexpand on |
| 795 | * emacs on |
| 796 | * errexit off |
| 797 | * errtrace off |
| 798 | * functrace off |
| 799 | * hashall on |
| 800 | * histexpand off |
| 801 | * history on |
| 802 | * ignoreeof off |
| 803 | * interactive-comments on |
| 804 | * keyword off |
| 805 | * monitor on |
| 806 | * noclobber off |
| 807 | * noexec off |
| 808 | * noglob off |
| 809 | * nolog off |
| 810 | * notify off |
| 811 | * nounset off |
| 812 | * onecmd off |
| 813 | * physical off |
| 814 | * pipefail off |
| 815 | * posix off |
| 816 | * privileged off |
| 817 | * verbose off |
| 818 | * vi off |
| 819 | * xtrace off |
| 820 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 821 | static const char o_opt_strings[] ALIGN1 = |
| 822 | "pipefail\0" |
| 823 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 824 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 825 | #if ENABLE_HUSH_MODE_X |
| 826 | "xtrace\0" |
| 827 | #endif |
| 828 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 829 | enum { |
| 830 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 831 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 832 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 833 | #if ENABLE_HUSH_MODE_X |
| 834 | OPT_O_XTRACE, |
| 835 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 836 | NUM_OPT_O |
| 837 | }; |
| 838 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 839 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 840 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 841 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 842 | /* interactive_fd != 0 means we are an interactive shell. |
| 843 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 844 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 845 | * job control still works, but terminal signals |
| 846 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 847 | * process groups can only be created with "cmd &". |
| 848 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 849 | * to give tty to the foreground process group, |
| 850 | * and will take it back when the group is stopped (^Z) |
| 851 | * or killed (^C). |
| 852 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 853 | #if ENABLE_HUSH_INTERACTIVE |
| 854 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 855 | * _AND_ if we decided to act interactively */ |
| 856 | int interactive_fd; |
| 857 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 858 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 859 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 860 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 861 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 862 | #endif |
| 863 | #if ENABLE_FEATURE_EDITING |
| 864 | line_input_t *line_input_state; |
| 865 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 866 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 867 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 868 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 869 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 870 | random_t random_gen; |
| 871 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 872 | #if ENABLE_HUSH_JOB |
| 873 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 874 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 875 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 876 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 877 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 878 | #else |
| 879 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 880 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 881 | /* How deeply are we in context where "set -e" is ignored */ |
| 882 | int errexit_depth; |
| 883 | /* "set -e" rules (do we follow them correctly?): |
| 884 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 885 | * Shell does not exit if failed command is part of condition in |
| 886 | * if/while, part of && or || list except the last command, any command |
| 887 | * in a pipe but the last, or if the command's return value is being |
| 888 | * inverted with !. If a compound command other than a subshell returns a |
| 889 | * non-zero status because a command failed while -e was being ignored, the |
| 890 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 891 | * exits [ERR is a bashism]. |
| 892 | * |
| 893 | * If a compound command or function executes in a context where -e is |
| 894 | * ignored, none of the commands executed within are affected by the -e |
| 895 | * setting. If a compound command or function sets -e while executing in a |
| 896 | * context where -e is ignored, that setting does not have any effect until |
| 897 | * the compound command or the command containing the function call completes. |
| 898 | */ |
| 899 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 900 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 901 | #if ENABLE_HUSH_MODE_X |
| 902 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 903 | #else |
| 904 | # define G_x_mode 0 |
| 905 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 906 | #if ENABLE_HUSH_INTERACTIVE |
| 907 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 908 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 909 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 910 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 911 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 912 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 913 | #if ENABLE_HUSH_FUNCTIONS |
| 914 | /* 0: outside of a function (or sourced file) |
| 915 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 916 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 917 | */ |
| 918 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 919 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 920 | #else |
| 921 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 922 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 923 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 924 | /* These support $? */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 925 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 926 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 927 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 928 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 929 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 930 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 931 | # define G_global_args_malloced (G.global_args_malloced) |
| 932 | #else |
| 933 | # define G_global_args_malloced 0 |
| 934 | #endif |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 935 | #if ENABLE_HUSH_BASH_COMPAT |
| 936 | int dead_job_exitcode; /* for "wait -n" */ |
| 937 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 938 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 939 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 940 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 941 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 942 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 943 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 944 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 945 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 946 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 947 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 948 | #if ENABLE_HUSH_GETOPTS |
| 949 | unsigned getopt_count; |
| 950 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 951 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 952 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 953 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 954 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 955 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 956 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 957 | unsigned var_nest_level; |
| 958 | #if ENABLE_HUSH_FUNCTIONS |
| 959 | # if ENABLE_HUSH_LOCAL |
| 960 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 961 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 962 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 963 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 964 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 965 | #if ENABLE_HUSH_FAST |
| 966 | unsigned count_SIGCHLD; |
| 967 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 968 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 969 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 970 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 971 | unsigned lineno; |
| 972 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 973 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 974 | HFILE *HFILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 975 | /* Which signals have non-DFL handler (even with no traps set)? |
| 976 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 977 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 978 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 979 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 980 | * Other than these two times, never modified. |
| 981 | */ |
| 982 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 983 | #if ENABLE_HUSH_JOB |
| 984 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 985 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 986 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 987 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 988 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 989 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 990 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 991 | # define G_traps G.traps |
| 992 | #else |
| 993 | # define G_traps ((char**)NULL) |
| 994 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 995 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 996 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 997 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 998 | #endif |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 999 | #if ENABLE_HUSH_MODE_X |
| 1000 | unsigned x_mode_depth; |
| 1001 | /* "set -x" output should not be redirectable with subsequent 2>FILE. |
| 1002 | * We dup fd#2 to x_mode_fd when "set -x" is executed, and use it |
| 1003 | * for all subsequent output. |
| 1004 | */ |
| 1005 | int x_mode_fd; |
| 1006 | o_string x_mode_buf; |
| 1007 | #endif |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1008 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1009 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1010 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1011 | struct sigaction sa; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 1012 | #if BASH_EPOCH_VARS |
| 1013 | char epoch_buf[sizeof("%lu.nnnnnn") + sizeof(long)*3]; |
| 1014 | #endif |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 1015 | #if ENABLE_FEATURE_EDITING |
| 1016 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 1017 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1018 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1019 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1020 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 1021 | * (too many defines). Also, I actually prefer to see when a variable |
| 1022 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1023 | #define INIT_G() do { \ |
| 1024 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1025 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 1026 | sigfillset(&G.sa.sa_mask); \ |
| 1027 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1028 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1029 | |
| 1030 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1031 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1032 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1033 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1034 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1035 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1036 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1037 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1038 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1039 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1040 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1041 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1042 | #if ENABLE_HUSH_READONLY |
| 1043 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1044 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1045 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1046 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1047 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1048 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1049 | #if ENABLE_HUSH_GETOPTS |
| 1050 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1051 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1052 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1053 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1054 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1055 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1056 | static int builtin_history(char **argv) FAST_FUNC; |
| 1057 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1058 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1059 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1060 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1061 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1062 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1063 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1064 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1065 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1066 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1067 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1068 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1069 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1070 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1071 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1072 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1073 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1074 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1075 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1076 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1077 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1078 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1079 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1080 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1081 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1082 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1083 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1084 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1085 | #if ENABLE_HUSH_TIMES |
| 1086 | static int builtin_times(char **argv) FAST_FUNC; |
| 1087 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1088 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1089 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1090 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1091 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1092 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1093 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1094 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1095 | #if ENABLE_HUSH_KILL |
| 1096 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1097 | #endif |
| 1098 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1099 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1100 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1101 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1102 | static int builtin_break(char **argv) FAST_FUNC; |
| 1103 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1104 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1105 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1106 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1107 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* Table of built-in functions. They can be forked or not, depending on |
| 1110 | * context: within pipes, they fork. As simple commands, they do not. |
| 1111 | * When used in non-forking context, they can change global variables |
| 1112 | * in the parent shell process. If forked, of course they cannot. |
| 1113 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1114 | * still be set at the end. */ |
| 1115 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1116 | const char *b_cmd; |
| 1117 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1118 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1119 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1120 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1121 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1122 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1123 | #endif |
| 1124 | }; |
| 1125 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1126 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1127 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1128 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1129 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1130 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1131 | #endif |
| 1132 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1133 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1134 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1135 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1136 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1137 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1138 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1139 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1140 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1141 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1142 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1143 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1144 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1145 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1146 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1147 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1148 | #if ENABLE_HUSH_GETOPTS |
| 1149 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1150 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1151 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1152 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1153 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1154 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1155 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1156 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1157 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1158 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1159 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1160 | #if ENABLE_HUSH_KILL |
| 1161 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1162 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1163 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1164 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1165 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1166 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1167 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1168 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1169 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1170 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1171 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1172 | #if ENABLE_HUSH_READONLY |
| 1173 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1174 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1175 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1176 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1177 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1178 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1179 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1180 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1181 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1182 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1183 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1184 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1185 | #if ENABLE_HUSH_TIMES |
| 1186 | BLTIN("times" , builtin_times , NULL), |
| 1187 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1188 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1189 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1190 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1191 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1192 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1193 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1194 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1195 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1196 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1197 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1198 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1199 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1200 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1201 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1202 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1203 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1204 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1205 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1206 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1207 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1208 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1209 | * (it's cheaper to run an external program in this case): |
| 1210 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1211 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1212 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1213 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1214 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1215 | #if BASH_TEST2 |
| 1216 | BLTIN("[[" , builtin_test , NULL), |
| 1217 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1218 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1219 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1220 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1221 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1222 | BLTIN("printf" , builtin_printf , NULL), |
| 1223 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1224 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1225 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1226 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1227 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1228 | }; |
| 1229 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1230 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1231 | /* Debug printouts. |
| 1232 | */ |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1233 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1234 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1235 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1236 | # define debug_enter() (G.debug_indent++) |
| 1237 | # define debug_leave() (G.debug_indent--) |
| 1238 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1239 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1240 | # define debug_enter() ((void)0) |
| 1241 | # define debug_leave() ((void)0) |
| 1242 | #endif |
| 1243 | |
| 1244 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1245 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1246 | #endif |
| 1247 | |
| 1248 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1249 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1250 | #endif |
| 1251 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 1252 | #ifndef debug_printf_heredoc |
| 1253 | # define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1254 | #endif |
| 1255 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1256 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1257 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1258 | #endif |
| 1259 | |
| 1260 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1261 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | |
| 1264 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1265 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1266 | # define DEBUG_JOBS 1 |
| 1267 | #else |
| 1268 | # define DEBUG_JOBS 0 |
| 1269 | #endif |
| 1270 | |
| 1271 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1272 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1273 | # define DEBUG_EXPAND 1 |
| 1274 | #else |
| 1275 | # define DEBUG_EXPAND 0 |
| 1276 | #endif |
| 1277 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1278 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1279 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1280 | #endif |
| 1281 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1282 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1283 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1284 | # define DEBUG_GLOB 1 |
| 1285 | #else |
| 1286 | # define DEBUG_GLOB 0 |
| 1287 | #endif |
| 1288 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1289 | #ifndef debug_printf_redir |
| 1290 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1291 | #endif |
| 1292 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1293 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1294 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1295 | #endif |
| 1296 | |
| 1297 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1298 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1299 | #endif |
| 1300 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1301 | #ifndef debug_printf_prompt |
| 1302 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1303 | #endif |
| 1304 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1305 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1306 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1307 | # define DEBUG_CLEAN 1 |
| 1308 | #else |
| 1309 | # define DEBUG_CLEAN 0 |
| 1310 | #endif |
| 1311 | |
| 1312 | #if DEBUG_EXPAND |
| 1313 | static void debug_print_strings(const char *prefix, char **vv) |
| 1314 | { |
| 1315 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1316 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1317 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1318 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1319 | } |
| 1320 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1321 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | |
| 1324 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1325 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1326 | */ |
| 1327 | #if LEAK_HUNTING |
| 1328 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1329 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1330 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1331 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1332 | return ptr; |
| 1333 | } |
| 1334 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1335 | { |
| 1336 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1337 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1338 | return ptr; |
| 1339 | } |
| 1340 | static char *xxstrdup(int lineno, const char *str) |
| 1341 | { |
| 1342 | char *ptr = xstrdup(str); |
| 1343 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1344 | return ptr; |
| 1345 | } |
| 1346 | static void xxfree(void *ptr) |
| 1347 | { |
| 1348 | fdprintf(2, "free %p\n", ptr); |
| 1349 | free(ptr); |
| 1350 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1351 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1352 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1353 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1354 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1355 | #endif |
| 1356 | |
| 1357 | |
| 1358 | /* Syntax and runtime errors. They always abort scripts. |
| 1359 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1360 | * and return to the prompt. |
| 1361 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1362 | */ |
| 1363 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1364 | # 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] | 1365 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1366 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1367 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1368 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1369 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1370 | #endif |
| 1371 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1372 | static void die_if_script(void) |
| 1373 | { |
| 1374 | if (!G_interactive_fd) { |
| 1375 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1376 | xfunc_error_retval = G.last_exitcode; |
| 1377 | xfunc_die(); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1382 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1383 | va_list p; |
| 1384 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1385 | #if HUSH_DEBUG >= 2 |
| 1386 | bb_error_msg("hush.c:%u", lineno); |
| 1387 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1388 | va_start(p, fmt); |
| 1389 | bb_verror_msg(fmt, p, NULL); |
| 1390 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1391 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1392 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1393 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1394 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1395 | { |
| 1396 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1397 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1398 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1399 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1400 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1403 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1404 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1405 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1406 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1409 | 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] | 1410 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1411 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1412 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1413 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1416 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1417 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1418 | char msg[2] = { ch, '\0' }; |
| 1419 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1422 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1423 | { |
| 1424 | char msg[2]; |
| 1425 | msg[0] = ch; |
| 1426 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1427 | #if HUSH_DEBUG >= 2 |
| 1428 | bb_error_msg("hush.c:%u", lineno); |
| 1429 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1430 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1431 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1434 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1435 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1436 | # undef syntax_error |
| 1437 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1438 | # undef syntax_error_unterm_ch |
| 1439 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1440 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1441 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1442 | # 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] | 1443 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1444 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1445 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1446 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1447 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1448 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1449 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1450 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1451 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1452 | static void cmdedit_update_prompt(void); |
| 1453 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1454 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1455 | #endif |
| 1456 | |
| 1457 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1458 | /* Utility functions |
| 1459 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1460 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1461 | static char *unbackslash(char *src) |
| 1462 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1463 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1464 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1465 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1466 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1467 | if (*src != '\0') { |
| 1468 | /* \x -> x */ |
| 1469 | *dst++ = *src++; |
| 1470 | continue; |
| 1471 | } |
| 1472 | /* else: "\<nul>". Do not delete this backslash. |
| 1473 | * Testcase: eval 'echo ok\' |
| 1474 | */ |
| 1475 | *dst++ = '\\'; |
| 1476 | /* fallthrough */ |
| 1477 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1478 | if ((*dst++ = *src++) == '\0') |
| 1479 | break; |
| 1480 | } |
| 1481 | return dst; |
| 1482 | } |
| 1483 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1484 | 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] | 1485 | { |
| 1486 | int i; |
| 1487 | unsigned count1; |
| 1488 | unsigned count2; |
| 1489 | char **v; |
| 1490 | |
| 1491 | v = strings; |
| 1492 | count1 = 0; |
| 1493 | if (v) { |
| 1494 | while (*v) { |
| 1495 | count1++; |
| 1496 | v++; |
| 1497 | } |
| 1498 | } |
| 1499 | count2 = 0; |
| 1500 | v = add; |
| 1501 | while (*v) { |
| 1502 | count2++; |
| 1503 | v++; |
| 1504 | } |
| 1505 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1506 | v[count1 + count2] = NULL; |
| 1507 | i = count2; |
| 1508 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1509 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1510 | return v; |
| 1511 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1512 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1513 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1514 | { |
| 1515 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1516 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1517 | return ptr; |
| 1518 | } |
| 1519 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1520 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1521 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1522 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1523 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1524 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1525 | { |
| 1526 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1527 | v[0] = add; |
| 1528 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1529 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1530 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1531 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1532 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1533 | { |
| 1534 | char **ptr = add_string_to_strings(strings, add); |
| 1535 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1536 | return ptr; |
| 1537 | } |
| 1538 | #define add_string_to_strings(strings, add) \ |
| 1539 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1540 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1541 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1542 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1543 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1544 | char **v; |
| 1545 | |
| 1546 | if (!strings) |
| 1547 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1548 | v = strings; |
| 1549 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1550 | free(*v); |
| 1551 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1552 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1553 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1556 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1557 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1558 | int newfd; |
| 1559 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1560 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1561 | if (newfd >= 0) { |
| 1562 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1563 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1564 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1565 | if (errno == EBUSY) |
| 1566 | goto repeat; |
| 1567 | if (errno == EINTR) |
| 1568 | goto repeat; |
| 1569 | } |
| 1570 | return newfd; |
| 1571 | } |
| 1572 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1573 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1574 | { |
| 1575 | int newfd; |
| 1576 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1577 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1578 | if (newfd < 0) { |
| 1579 | if (errno == EBUSY) |
| 1580 | goto repeat; |
| 1581 | if (errno == EINTR) |
| 1582 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1583 | /* fd was not open? */ |
| 1584 | if (errno == EBADF) |
| 1585 | return fd; |
| 1586 | xfunc_die(); |
| 1587 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1588 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1589 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1590 | close(fd); |
| 1591 | return newfd; |
| 1592 | } |
| 1593 | |
| 1594 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1595 | /* Manipulating HFILEs */ |
| 1596 | static HFILE *hfopen(const char *name) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1597 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1598 | HFILE *fp; |
| 1599 | int fd; |
| 1600 | |
| 1601 | fd = STDIN_FILENO; |
| 1602 | if (name) { |
| 1603 | fd = open(name, O_RDONLY | O_CLOEXEC); |
| 1604 | if (fd < 0) |
| 1605 | return NULL; |
| 1606 | if (O_CLOEXEC == 0) /* ancient libc */ |
| 1607 | close_on_exec_on(fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1608 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1609 | |
| 1610 | fp = xmalloc(sizeof(*fp)); |
| 1611 | fp->is_stdin = (name == NULL); |
| 1612 | fp->fd = fd; |
| 1613 | fp->cur = fp->end = fp->buf; |
| 1614 | fp->next_hfile = G.HFILE_list; |
| 1615 | G.HFILE_list = fp; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1616 | return fp; |
| 1617 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1618 | static void hfclose(HFILE *fp) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1619 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1620 | HFILE **pp = &G.HFILE_list; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1621 | while (*pp) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1622 | HFILE *cur = *pp; |
| 1623 | if (cur == fp) { |
| 1624 | *pp = cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1625 | break; |
| 1626 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1627 | pp = &cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1628 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1629 | if (fp->fd >= 0) |
| 1630 | close(fp->fd); |
| 1631 | free(fp); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1632 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1633 | static int refill_HFILE_and_getc(HFILE *fp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1634 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1635 | int n; |
| 1636 | |
| 1637 | if (fp->fd < 0) { |
| 1638 | /* Already saw EOF */ |
| 1639 | return EOF; |
| 1640 | } |
| 1641 | /* Try to buffer more input */ |
| 1642 | fp->cur = fp->buf; |
| 1643 | n = safe_read(fp->fd, fp->buf, sizeof(fp->buf)); |
| 1644 | if (n < 0) { |
| 1645 | bb_perror_msg("read error"); |
| 1646 | n = 0; |
| 1647 | } |
| 1648 | fp->end = fp->buf + n; |
| 1649 | if (n == 0) { |
| 1650 | /* EOF/error */ |
| 1651 | close(fp->fd); |
| 1652 | fp->fd = -1; |
| 1653 | return EOF; |
| 1654 | } |
| 1655 | return (unsigned char)(*fp->cur++); |
| 1656 | } |
| 1657 | /* Inlined for common case of non-empty buffer. |
| 1658 | */ |
| 1659 | static ALWAYS_INLINE int hfgetc(HFILE *fp) |
| 1660 | { |
| 1661 | if (fp->cur < fp->end) |
| 1662 | return (unsigned char)(*fp->cur++); |
| 1663 | /* Buffer empty */ |
| 1664 | return refill_HFILE_and_getc(fp); |
| 1665 | } |
| 1666 | static int move_HFILEs_on_redirect(int fd, int avoid_fd) |
| 1667 | { |
| 1668 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1669 | while (fl) { |
| 1670 | if (fd == fl->fd) { |
| 1671 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1672 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1673 | debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1674 | return 1; /* "found and moved" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1675 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1676 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1677 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 1678 | #if ENABLE_HUSH_MODE_X |
| 1679 | if (G.x_mode_fd > 0 && fd == G.x_mode_fd) { |
| 1680 | G.x_mode_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 1681 | return 1; /* "found and moved" */ |
| 1682 | } |
| 1683 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1684 | return 0; /* "not in the list" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1685 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1686 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1687 | static void close_all_HFILE_list(void) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1688 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1689 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1690 | while (fl) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1691 | /* hfclose would also free HFILE object. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1692 | * It is disastrous if we share memory with a vforked parent. |
| 1693 | * I'm not sure we never come here after vfork. |
| 1694 | * Therefore just close fd, nothing more. |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1695 | * |
| 1696 | * ">" instead of ">=": we don't close fd#0, |
| 1697 | * interactive shell uses hfopen(NULL) as stdin input |
| 1698 | * which has fl->fd == 0, but fd#0 gets redirected in pipes. |
| 1699 | * If we'd close it here, then e.g. interactive "set | sort" |
| 1700 | * with NOFORKed sort, would have sort's input fd closed. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1701 | */ |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1702 | if (fl->fd > 0) |
| 1703 | /*hfclose(fl); - unsafe */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1704 | close(fl->fd); |
| 1705 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1706 | } |
| 1707 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1708 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1709 | static int fd_in_HFILEs(int fd) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1710 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1711 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1712 | while (fl) { |
| 1713 | if (fl->fd == fd) |
| 1714 | return 1; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1715 | fl = fl->next_hfile; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1716 | } |
| 1717 | return 0; |
| 1718 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1719 | |
| 1720 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1721 | /* Helpers for setting new $n and restoring them back |
| 1722 | */ |
| 1723 | typedef struct save_arg_t { |
| 1724 | char *sv_argv0; |
| 1725 | char **sv_g_argv; |
| 1726 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1727 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1728 | } save_arg_t; |
| 1729 | |
| 1730 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1731 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1732 | sv->sv_argv0 = argv[0]; |
| 1733 | sv->sv_g_argv = G.global_argv; |
| 1734 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1735 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1736 | |
| 1737 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1738 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1739 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1740 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1741 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1745 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1746 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1747 | if (G.global_args_malloced) { |
| 1748 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1749 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1750 | while (*++pp) /* note: does not free $0 */ |
| 1751 | free(*pp); |
| 1752 | free(G.global_argv); |
| 1753 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1754 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1755 | argv[0] = sv->sv_argv0; |
| 1756 | G.global_argv = sv->sv_g_argv; |
| 1757 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1758 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1762 | /* Basic theory of signal handling in shell |
| 1763 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1764 | * This does not describe what hush does, rather, it is current understanding |
| 1765 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1766 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1767 | * |
| 1768 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1769 | * is finished or backgrounded. It is the same in interactive and |
| 1770 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1771 | * 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] | 1772 | * ^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] | 1773 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1774 | * pipe. |
| 1775 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1776 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1777 | * or by SIGINT in interactive shell. |
| 1778 | * |
| 1779 | * Trap handlers will execute even within trap handlers. (right?) |
| 1780 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1781 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1782 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1783 | * |
| 1784 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1785 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1786 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1787 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1788 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1789 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1790 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1791 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1792 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1793 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1794 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1795 | * |
| 1796 | * SIGQUIT: ignore |
| 1797 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1798 | * SIGHUP (interactive): |
| 1799 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1800 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1801 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1802 | * that all pipe members are stopped. Try this in bash: |
| 1803 | * while :; do :; done - ^Z does not background it |
| 1804 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1805 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1806 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1807 | * to interactive shell while shell is waiting for a pipe, |
| 1808 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1809 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1810 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1811 | * Example 2: this does not wait and does not execute ls: |
| 1812 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1813 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1814 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1815 | * Example 4: this does not wait and does not execute ls: |
| 1816 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1817 | * |
| 1818 | * (What happens to signals which are IGN on shell start?) |
| 1819 | * (What happens with signal mask on shell start?) |
| 1820 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1821 | * Old implementation |
| 1822 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1823 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1824 | * We block all signals which we don't want to take action immediately, |
| 1825 | * i.e. we block all signals which need to have special handling as described |
| 1826 | * above, and all signals which have traps set. |
| 1827 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1828 | * and act on them. |
| 1829 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1830 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1831 | * sigset_t blocked_set: current blocked signal set |
| 1832 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1833 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1834 | * 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] | 1835 | * "trap 'cmd' SIGxxx": |
| 1836 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1837 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1838 | * unblock signals with special interactive handling |
| 1839 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1840 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1841 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1842 | * 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] | 1843 | * 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] | 1844 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1845 | * 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] | 1846 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1847 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1848 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1849 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1850 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1851 | * 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] | 1852 | * 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] | 1853 | * |
| 1854 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1855 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1856 | * masked signals are not visible! |
| 1857 | * |
| 1858 | * New implementation |
| 1859 | * ================== |
| 1860 | * We record each signal we are interested in by installing signal handler |
| 1861 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1862 | * We are interested in: signals which need to have special handling |
| 1863 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1864 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1865 | * After each pipe execution, we extract any pending signals |
| 1866 | * and act on them. |
| 1867 | * |
| 1868 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1869 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1870 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1871 | * sigset_t pending_set: set of sigs we received. |
| 1872 | * |
| 1873 | * "trap - SIGxxx": |
| 1874 | * if sig is in special_sig_mask, set handler back to: |
| 1875 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1876 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1877 | * else: set handler back to SIG_DFL |
| 1878 | * "trap 'cmd' SIGxxx": |
| 1879 | * set handler to record_pending_signo. |
| 1880 | * "trap '' SIGxxx": |
| 1881 | * set handler to SIG_IGN. |
| 1882 | * after [v]fork, if we plan to be a shell: |
| 1883 | * set signals with special interactive handling to SIG_DFL |
| 1884 | * (because child shell is not interactive), |
| 1885 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1886 | * after [v]fork, if we plan to exec: |
| 1887 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1888 | * |
| 1889 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1890 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1891 | * |
| 1892 | * Note (compat): |
| 1893 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1894 | * are set to the default actions". bash interprets it so that traps which |
| 1895 | * 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] | 1896 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1897 | enum { |
| 1898 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1899 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1900 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1901 | | (1 << SIGHUP) |
| 1902 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1903 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1904 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1905 | | (1 << SIGTTIN) |
| 1906 | | (1 << SIGTTOU) |
| 1907 | | (1 << SIGTSTP) |
| 1908 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1909 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1910 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1911 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1912 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1913 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1914 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1915 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1916 | if (sig == SIGCHLD) { |
| 1917 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1918 | //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] | 1919 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1920 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1921 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1922 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1923 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1924 | { |
| 1925 | struct sigaction old_sa; |
| 1926 | |
| 1927 | /* We could use signal() to install handlers... almost: |
| 1928 | * except that we need to mask ALL signals while handlers run. |
| 1929 | * I saw signal nesting in strace, race window isn't small. |
| 1930 | * SA_RESTART is also needed, but in Linux, signal() |
| 1931 | * sets SA_RESTART too. |
| 1932 | */ |
| 1933 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1934 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1935 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1936 | G.sa.sa_handler = handler; |
| 1937 | sigaction(sig, &G.sa, &old_sa); |
| 1938 | return old_sa.sa_handler; |
| 1939 | } |
| 1940 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1941 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1942 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1943 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1944 | static void restore_ttypgrp_and__exit(void) |
| 1945 | { |
| 1946 | /* xfunc has failed! die die die */ |
| 1947 | /* no EXIT traps, this is an escape hatch! */ |
| 1948 | G.exiting = 1; |
| 1949 | hush_exit(xfunc_error_retval); |
| 1950 | } |
| 1951 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1952 | #if ENABLE_HUSH_JOB |
| 1953 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1954 | /* Needed only on some libc: |
| 1955 | * It was observed that on exit(), fgetc'ed buffered data |
| 1956 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1957 | * With the net effect that even after fork(), not vfork(), |
| 1958 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1959 | * noexec_applet_here |
| 1960 | * echo END_OF_SCRIPT |
| 1961 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1962 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1963 | * 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] | 1964 | * and in `cmd` handling. |
| 1965 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1966 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1967 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1968 | static void fflush_and__exit(void) |
| 1969 | { |
| 1970 | fflush_all(); |
| 1971 | _exit(xfunc_error_retval); |
| 1972 | } |
| 1973 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1974 | /* 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] | 1975 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1976 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1977 | # 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] | 1978 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1979 | /* Restores tty foreground process group, and exits. |
| 1980 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1981 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1982 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1983 | * We also call it if xfunc is exiting. |
| 1984 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1985 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1986 | static void sigexit(int sig) |
| 1987 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1988 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1989 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1990 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1991 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1992 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1993 | */ |
| 1994 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1995 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1996 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1997 | |
| 1998 | /* Not a signal, just exit */ |
| 1999 | if (sig <= 0) |
| 2000 | _exit(- sig); |
| 2001 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 2002 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2003 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2004 | #else |
| 2005 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2006 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 2007 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2008 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 2009 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2010 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2011 | static sighandler_t pick_sighandler(unsigned sig) |
| 2012 | { |
| 2013 | sighandler_t handler = SIG_DFL; |
| 2014 | if (sig < sizeof(unsigned)*8) { |
| 2015 | unsigned sigmask = (1 << sig); |
| 2016 | |
| 2017 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2018 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2019 | if (G_fatal_sig_mask & sigmask) |
| 2020 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2021 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2022 | #endif |
| 2023 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2024 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2025 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2026 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2027 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 2028 | * in an endless loop when we try to do some |
| 2029 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2030 | */ |
| 2031 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 2032 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2033 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2034 | } |
| 2035 | return handler; |
| 2036 | } |
| 2037 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2038 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2039 | static void hush_exit(int exitcode) |
| 2040 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 2041 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 2042 | save_history(G.line_input_state); |
| 2043 | #endif |
| 2044 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 2045 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2046 | 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] | 2047 | char *argv[3]; |
| 2048 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 2049 | 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] | 2050 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 2051 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2052 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 2053 | * "trap" will still show it, if executed |
| 2054 | * in the handler */ |
| 2055 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2056 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2057 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2058 | #if ENABLE_FEATURE_CLEAN_UP |
| 2059 | { |
| 2060 | struct variable *cur_var; |
| 2061 | if (G.cwd != bb_msg_unknown) |
| 2062 | free((char*)G.cwd); |
| 2063 | cur_var = G.top_var; |
| 2064 | while (cur_var) { |
| 2065 | struct variable *tmp = cur_var; |
| 2066 | if (!cur_var->max_len) |
| 2067 | free(cur_var->varstr); |
| 2068 | cur_var = cur_var->next; |
| 2069 | free(tmp); |
| 2070 | } |
| 2071 | } |
| 2072 | #endif |
| 2073 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2074 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2075 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2076 | sigexit(- (exitcode & 0xff)); |
| 2077 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2078 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2079 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2082 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2083 | //TODO: return a mask of ALL handled sigs? |
| 2084 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2085 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2086 | int last_sig = 0; |
| 2087 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2088 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2089 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2090 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2091 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2092 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2093 | sig = 0; |
| 2094 | do { |
| 2095 | sig++; |
| 2096 | if (sigismember(&G.pending_set, sig)) { |
| 2097 | sigdelset(&G.pending_set, sig); |
| 2098 | goto got_sig; |
| 2099 | } |
| 2100 | } while (sig < NSIG); |
| 2101 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2102 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2103 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2104 | 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] | 2105 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2106 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2107 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2108 | char *argv[3]; |
| 2109 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2110 | argv[1] = xstrdup(G_traps[sig]); |
| 2111 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2112 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2113 | save_rcode = G.last_exitcode; |
| 2114 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2115 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2116 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2117 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2118 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2119 | } /* else: "" trap, ignoring signal */ |
| 2120 | continue; |
| 2121 | } |
| 2122 | /* not a trap: special action */ |
| 2123 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2124 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2125 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2126 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2127 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2128 | break; |
| 2129 | #if ENABLE_HUSH_JOB |
| 2130 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2131 | //TODO: why are we doing this? ash and dash don't do this, |
| 2132 | //they have no handler for SIGHUP at all, |
| 2133 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2134 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2135 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2136 | /* bash is observed to signal whole process groups, |
| 2137 | * not individual processes */ |
| 2138 | for (job = G.job_list; job; job = job->next) { |
| 2139 | if (job->pgrp <= 0) |
| 2140 | continue; |
| 2141 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2142 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2143 | kill(- job->pgrp, SIGCONT); |
| 2144 | } |
| 2145 | sigexit(SIGHUP); |
| 2146 | } |
| 2147 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2148 | #if ENABLE_HUSH_FAST |
| 2149 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2150 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2151 | G.count_SIGCHLD++; |
| 2152 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2153 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2154 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2155 | * This simplifies wait builtin a bit. |
| 2156 | */ |
| 2157 | break; |
| 2158 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2159 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2160 | 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] | 2161 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2162 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2163 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2164 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2165 | * in interactive shell, because TERM is ignored. |
| 2166 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | } |
| 2169 | } |
| 2170 | return last_sig; |
| 2171 | } |
| 2172 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2173 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2174 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2175 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2176 | if (force || G.cwd == NULL) { |
| 2177 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2178 | * we must not try to free(bb_msg_unknown) */ |
| 2179 | if (G.cwd == bb_msg_unknown) |
| 2180 | G.cwd = NULL; |
| 2181 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2182 | if (!G.cwd) |
| 2183 | G.cwd = bb_msg_unknown; |
| 2184 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2185 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2188 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2189 | /* |
| 2190 | * Shell and environment variable support |
| 2191 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2192 | 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] | 2193 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2194 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2195 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2196 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2197 | pp = &G.top_var; |
| 2198 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2199 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2200 | return pp; |
| 2201 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2202 | } |
| 2203 | return NULL; |
| 2204 | } |
| 2205 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2206 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2207 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2208 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2209 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2210 | |
| 2211 | if (G.expanded_assignments) { |
| 2212 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2213 | while (*cpp) { |
| 2214 | char *cp = *cpp; |
| 2215 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2216 | return cp + len + 1; |
| 2217 | cpp++; |
| 2218 | } |
| 2219 | } |
| 2220 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2221 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2222 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2223 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2224 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2225 | if (strcmp(name, "PPID") == 0) |
| 2226 | return utoa(G.root_ppid); |
| 2227 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2228 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2229 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2230 | return utoa(next_random(&G.random_gen)); |
| 2231 | #endif |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2232 | #if BASH_EPOCH_VARS |
| 2233 | { |
| 2234 | const char *fmt = NULL; |
| 2235 | if (strcmp(name, "EPOCHSECONDS") == 0) |
| 2236 | fmt = "%lu"; |
| 2237 | else if (strcmp(name, "EPOCHREALTIME") == 0) |
| 2238 | fmt = "%lu.%06u"; |
| 2239 | if (fmt) { |
| 2240 | struct timeval tv; |
| 2241 | gettimeofday(&tv, NULL); |
| 2242 | sprintf(G.epoch_buf, fmt, (unsigned long)tv.tv_sec, |
| 2243 | (unsigned)tv.tv_usec); |
| 2244 | return G.epoch_buf; |
| 2245 | } |
| 2246 | } |
| 2247 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2248 | return NULL; |
| 2249 | } |
| 2250 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2251 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2252 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2253 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2254 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2255 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2256 | cmdedit_update_prompt(); |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2261 | && name_len == 6 |
| 2262 | ) { |
| 2263 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2264 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2265 | G.lineno_var = NULL; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2266 | return; |
| 2267 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2268 | #endif |
| 2269 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2270 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2271 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2274 | #endif |
| 2275 | } |
| 2276 | } |
| 2277 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2278 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2279 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2280 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2281 | #define SETFLAG_EXPORT (1 << 0) |
| 2282 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2283 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2284 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2285 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2286 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2287 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2288 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2289 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2290 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2291 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2292 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2293 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2294 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2295 | if (HUSH_DEBUG && !eq_sign) |
| 2296 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2297 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2298 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2299 | cur_pp = &G.top_var; |
| 2300 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2301 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2302 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2303 | continue; |
| 2304 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2305 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2306 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2307 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2308 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2309 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2310 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2311 | //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] | 2312 | return -1; |
| 2313 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2314 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2315 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2316 | *eq_sign = '\0'; |
| 2317 | unsetenv(str); |
| 2318 | *eq_sign = '='; |
| 2319 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2320 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2321 | /* bash 3.2.33(1) and exported vars: |
| 2322 | * # export z=z |
| 2323 | * # f() { local z=a; env | grep ^z; } |
| 2324 | * # f |
| 2325 | * z=a |
| 2326 | * # env | grep ^z |
| 2327 | * z=z |
| 2328 | */ |
| 2329 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2330 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2331 | /* New variable is local ("local VAR=VAL" or |
| 2332 | * "VAR=VAL cmd") |
| 2333 | * and existing one is global, or local |
| 2334 | * on a lower level that new one. |
| 2335 | * Remove it from global variable list: |
| 2336 | */ |
| 2337 | *cur_pp = cur->next; |
| 2338 | if (G.shadowed_vars_pp) { |
| 2339 | /* Save in "shadowed" list */ |
| 2340 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2341 | cur->flg_export ? "exported " : "", |
| 2342 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2343 | ); |
| 2344 | cur->next = *G.shadowed_vars_pp; |
| 2345 | *G.shadowed_vars_pp = cur; |
| 2346 | } else { |
| 2347 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2348 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2349 | cur->flg_export ? "exported " : "", |
| 2350 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2351 | ); |
| 2352 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2353 | free_me = cur->varstr; /* then free it later */ |
| 2354 | free(cur); |
| 2355 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2356 | break; |
| 2357 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2358 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2359 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2360 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2361 | free_and_exp: |
| 2362 | free(str); |
| 2363 | goto exp; |
| 2364 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2367 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2368 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2369 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2370 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2371 | strcpy(cur->varstr, str); |
| 2372 | goto free_and_exp; |
| 2373 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2374 | /* Can't reuse */ |
| 2375 | cur->max_len = 0; |
| 2376 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2377 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2378 | /* max_len == 0 signifies "malloced" var, which we can |
| 2379 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2380 | * if cur->flg_export is 1, it is in the environment. |
| 2381 | * We should either unsetenv+free, or wait until putenv, |
| 2382 | * then putenv(new)+free(old). |
| 2383 | */ |
| 2384 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2385 | goto set_str_and_exp; |
| 2386 | } |
| 2387 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2388 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2389 | 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] | 2390 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2391 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2392 | cur->next = *cur_pp; |
| 2393 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2394 | |
| 2395 | set_str_and_exp: |
| 2396 | cur->varstr = str; |
| 2397 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2398 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2399 | if (flags & SETFLAG_MAKE_RO) { |
| 2400 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2401 | } |
| 2402 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2403 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2404 | cur->flg_export = 1; |
| 2405 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2406 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2407 | cur->flg_export = 0; |
| 2408 | /* unsetenv was already done */ |
| 2409 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2410 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2411 | 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] | 2412 | i = putenv(cur->varstr); |
| 2413 | /* only now we can free old exported malloced string */ |
| 2414 | free(free_me); |
| 2415 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2416 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2417 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2418 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2419 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2420 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2421 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2422 | return 0; |
| 2423 | } |
| 2424 | |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 2425 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
| 2426 | { |
| 2427 | char *var = xasprintf("%s=%s", name, val); |
| 2428 | set_local_var(var, /*flag:*/ 0); |
| 2429 | } |
| 2430 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2431 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2432 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2433 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2434 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
Denys Vlasenko | 35a017c | 2018-06-26 18:27:54 +0200 | [diff] [blame] | 2437 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2438 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2439 | { |
| 2440 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2441 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2442 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2443 | cur_pp = &G.top_var; |
| 2444 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2445 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2446 | && cur->varstr[name_len] == '=' |
| 2447 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2448 | if (cur->flg_read_only) { |
| 2449 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2450 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2451 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2452 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2453 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2454 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2455 | bb_unsetenv(cur->varstr); |
| 2456 | if (!cur->max_len) |
| 2457 | free(cur->varstr); |
| 2458 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2459 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2460 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2461 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2462 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2463 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2464 | |
| 2465 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2466 | handle_changed_special_names(name, name_len); |
| 2467 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2468 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2471 | static int unset_local_var(const char *name) |
| 2472 | { |
| 2473 | return unset_local_var_len(name, strlen(name)); |
| 2474 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2475 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2476 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2477 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2478 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2479 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2480 | */ |
| 2481 | static void add_vars(struct variable *var) |
| 2482 | { |
| 2483 | struct variable *next; |
| 2484 | |
| 2485 | while (var) { |
| 2486 | next = var->next; |
| 2487 | var->next = G.top_var; |
| 2488 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2489 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2490 | 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] | 2491 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2492 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2493 | 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] | 2494 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2495 | var = next; |
| 2496 | } |
| 2497 | } |
| 2498 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2499 | /* We put strings[i] into variable table and possibly putenv them. |
| 2500 | * If variable is read only, we can free the strings[i] |
| 2501 | * which attempts to overwrite it. |
| 2502 | * The strings[] vector itself is freed. |
| 2503 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2504 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2505 | { |
| 2506 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2507 | |
| 2508 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2509 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2510 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2511 | s = strings; |
| 2512 | while (*s) { |
| 2513 | struct variable *var_p; |
| 2514 | struct variable **var_pp; |
| 2515 | char *eq; |
| 2516 | |
| 2517 | eq = strchr(*s, '='); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2518 | if (HUSH_DEBUG && !eq) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2519 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2520 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
| 2521 | if (var_pp) { |
| 2522 | var_p = *var_pp; |
| 2523 | if (var_p->flg_read_only) { |
| 2524 | char **p; |
| 2525 | bb_error_msg("%s: readonly variable", *s); |
| 2526 | /* |
| 2527 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2528 | * If VAR is readonly, leaving it in the list |
| 2529 | * after asssignment error (msg above) |
| 2530 | * causes doubled error message later, on unset. |
| 2531 | */ |
| 2532 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2533 | free(*s); |
| 2534 | p = s; |
| 2535 | do { *p = p[1]; p++; } while (*p); |
| 2536 | goto next; |
| 2537 | } |
| 2538 | /* below, set_local_var() with nest level will |
| 2539 | * "shadow" (remove) this variable from |
| 2540 | * global linked list. |
| 2541 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2542 | } |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2543 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
| 2544 | set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2545 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2546 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2547 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2548 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2553 | * Unicode helper |
| 2554 | */ |
| 2555 | static void reinit_unicode_for_hush(void) |
| 2556 | { |
| 2557 | /* Unicode support should be activated even if LANG is set |
| 2558 | * _during_ shell execution, not only if it was set when |
| 2559 | * shell was started. Therefore, re-check LANG every time: |
| 2560 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2561 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2562 | || ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 4c201c0 | 2018-07-17 15:04:17 +0200 | [diff] [blame] | 2563 | ) { |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2564 | const char *s = get_local_var_value("LC_ALL"); |
| 2565 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2566 | if (!s) s = get_local_var_value("LANG"); |
| 2567 | reinit_unicode(s); |
| 2568 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2569 | } |
| 2570 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2571 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2572 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2573 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2574 | |
| 2575 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2576 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2577 | * echo $P\ |
| 2578 | * \ |
| 2579 | * AT\ |
| 2580 | * H\ |
| 2581 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2582 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2583 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2584 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2585 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2586 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2587 | G.PS1 = get_local_var_value("PS1"); |
| 2588 | if (G.PS1 == NULL) |
| 2589 | G.PS1 = ""; |
| 2590 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2591 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2592 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2593 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2594 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2595 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2596 | { |
| 2597 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2598 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2599 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2600 | |
| 2601 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2602 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2603 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2604 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2605 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2606 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2607 | /* bash uses $PWD value, even if it is set by user. |
| 2608 | * It uses current dir only if PWD is unset. |
| 2609 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2610 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2611 | } |
| 2612 | prompt_str = G.PS1; |
| 2613 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2614 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2615 | return prompt_str; |
| 2616 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2617 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2618 | { |
| 2619 | int r; |
| 2620 | const char *prompt_str; |
| 2621 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2622 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2623 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2624 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2625 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2626 | if (G.flag_SIGINT) { |
| 2627 | /* There was ^C'ed, make it look prettier: */ |
| 2628 | bb_putchar('\n'); |
| 2629 | G.flag_SIGINT = 0; |
| 2630 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2631 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2632 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2633 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2634 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2635 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2636 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2637 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2638 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2639 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2640 | if (r != 0 && !G.flag_SIGINT) |
| 2641 | break; |
| 2642 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2643 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2644 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2645 | G.last_exitcode = 128 + SIGINT; |
| 2646 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2647 | if (r < 0) { |
| 2648 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2649 | i->p = NULL; |
| 2650 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2651 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2652 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2653 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2654 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2655 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2656 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2657 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2658 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2659 | /* Why check_and_run_traps here? Try this interactively: |
| 2660 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2661 | * $ <[enter], repeatedly...> |
| 2662 | * Without check_and_run_traps, handler never runs. |
| 2663 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2664 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2665 | fputs(prompt_str, stdout); |
| 2666 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2667 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2668 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2669 | r = hfgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2670 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2671 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2672 | * it generates SIGINT as usual. |
| 2673 | */ |
| 2674 | check_and_run_traps(); |
| 2675 | if (G.flag_SIGINT) |
| 2676 | G.last_exitcode = 128 + SIGINT; |
| 2677 | if (r != '\0') |
| 2678 | break; |
| 2679 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2680 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2681 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2682 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2683 | /* This is the magic location that prints prompts |
| 2684 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2685 | static int fgetc_interactive(struct in_str *i) |
| 2686 | { |
| 2687 | int ch; |
| 2688 | /* If it's interactive stdin, get new line. */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2689 | if (G_interactive_fd && i->file->is_stdin) { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2690 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2691 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2692 | G.promptmode = 1; /* PS2 */ |
| 2693 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2694 | } else { |
| 2695 | /* Not stdin: script file, sourced file, etc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2696 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2697 | } |
| 2698 | return ch; |
| 2699 | } |
| 2700 | #else |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2701 | static ALWAYS_INLINE int fgetc_interactive(struct in_str *i) |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2702 | { |
| 2703 | int ch; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2704 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2705 | return ch; |
| 2706 | } |
| 2707 | #endif /* INTERACTIVE */ |
| 2708 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2709 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2710 | { |
| 2711 | int ch; |
| 2712 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2713 | if (!i->file) { |
| 2714 | /* string-based in_str */ |
| 2715 | ch = (unsigned char)*i->p; |
| 2716 | if (ch != '\0') { |
| 2717 | i->p++; |
| 2718 | i->last_char = ch; |
| 2719 | return ch; |
| 2720 | } |
| 2721 | return EOF; |
| 2722 | } |
| 2723 | |
| 2724 | /* FILE-based in_str */ |
| 2725 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2726 | #if ENABLE_FEATURE_EDITING |
| 2727 | /* This can be stdin, check line editing char[] buffer */ |
| 2728 | if (i->p && *i->p != '\0') { |
| 2729 | ch = (unsigned char)*i->p++; |
| 2730 | goto out; |
| 2731 | } |
| 2732 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2733 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2734 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2735 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2736 | int ch2 = i->peek_buf[1]; |
| 2737 | i->peek_buf[0] = ch2; |
| 2738 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2739 | goto out; |
| 2740 | i->peek_buf[1] = 0; |
| 2741 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2742 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2743 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2744 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2745 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2746 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2747 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2748 | #if ENABLE_HUSH_LINENO_VAR |
| 2749 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2750 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2751 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2752 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2753 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2754 | return ch; |
| 2755 | } |
| 2756 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2757 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2758 | { |
| 2759 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2760 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2761 | if (!i->file) { |
| 2762 | /* string-based in_str */ |
| 2763 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2764 | return (unsigned char)*i->p; |
| 2765 | } |
| 2766 | |
| 2767 | /* FILE-based in_str */ |
| 2768 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2769 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2770 | /* This can be stdin, check line editing char[] buffer */ |
| 2771 | if (i->p && *i->p != '\0') |
| 2772 | return (unsigned char)*i->p; |
| 2773 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2774 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2775 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2776 | if (ch != 0) |
| 2777 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2778 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2779 | /* Need to get a new char */ |
| 2780 | ch = fgetc_interactive(i); |
| 2781 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2782 | |
| 2783 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2784 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2785 | if (i->p) { |
| 2786 | i->p -= 1; |
| 2787 | return ch; |
| 2788 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2789 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2790 | i->peek_buf[0] = ch; |
| 2791 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2792 | return ch; |
| 2793 | } |
| 2794 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2795 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2796 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2797 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2798 | */ |
| 2799 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2800 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2801 | int ch; |
| 2802 | |
| 2803 | /* There are two cases when i->p[] buffer exists. |
| 2804 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2805 | * (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] | 2806 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2807 | * the peek2 result is in i->p[1]. |
| 2808 | */ |
| 2809 | if (i->p) |
| 2810 | return (unsigned char)i->p[1]; |
| 2811 | |
| 2812 | /* Now we know it is a file-based in_str. */ |
| 2813 | |
| 2814 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2815 | /* Is there 2nd char? */ |
| 2816 | ch = i->peek_buf[1]; |
| 2817 | if (ch == 0) { |
| 2818 | /* We did not read it yet, get it now */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2819 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2820 | i->peek_buf[1] = ch; |
| 2821 | } |
| 2822 | |
| 2823 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2824 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2825 | } |
| 2826 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2827 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2828 | { |
| 2829 | for (;;) { |
| 2830 | int ch, ch2; |
| 2831 | |
| 2832 | ch = i_getch(input); |
| 2833 | if (ch != '\\') |
| 2834 | return ch; |
| 2835 | ch2 = i_peek(input); |
| 2836 | if (ch2 != '\n') |
| 2837 | return ch; |
| 2838 | /* backslash+newline, skip it */ |
| 2839 | i_getch(input); |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2844 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2845 | */ |
| 2846 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2847 | { |
| 2848 | for (;;) { |
| 2849 | int ch, ch2; |
| 2850 | |
| 2851 | ch = i_peek(input); |
| 2852 | if (ch != '\\') |
| 2853 | return ch; |
| 2854 | ch2 = i_peek2(input); |
| 2855 | if (ch2 != '\n') |
| 2856 | return ch; |
| 2857 | /* backslash+newline, skip it */ |
| 2858 | i_getch(input); |
| 2859 | i_getch(input); |
| 2860 | } |
| 2861 | } |
| 2862 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2863 | static void setup_file_in_str(struct in_str *i, HFILE *fp) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2864 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2865 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2866 | i->file = fp; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2867 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2871 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2872 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2873 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2874 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2878 | /* |
| 2879 | * o_string support |
| 2880 | */ |
| 2881 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2882 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2883 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2884 | { |
| 2885 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2886 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2887 | if (o->data) |
| 2888 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2891 | static void o_free_and_set_NULL(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2892 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2893 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2894 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2897 | static ALWAYS_INLINE void o_free(o_string *o) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2898 | { |
| 2899 | free(o->data); |
| 2900 | } |
| 2901 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2902 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2903 | { |
| 2904 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2905 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2906 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2907 | } |
| 2908 | } |
| 2909 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2910 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2911 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2912 | 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] | 2913 | if (o->length < o->maxlen) { |
| 2914 | /* likely. avoid o_grow_by() call */ |
| 2915 | add: |
| 2916 | o->data[o->length] = ch; |
| 2917 | o->length++; |
| 2918 | o->data[o->length] = '\0'; |
| 2919 | return; |
| 2920 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2921 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2922 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2925 | #if 0 |
| 2926 | /* Valid only if we know o_string is not empty */ |
| 2927 | static void o_delchr(o_string *o) |
| 2928 | { |
| 2929 | o->length--; |
| 2930 | o->data[o->length] = '\0'; |
| 2931 | } |
| 2932 | #endif |
| 2933 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2934 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2935 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2936 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2937 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2938 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2941 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2942 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2943 | o_addblock(o, str, strlen(str)); |
| 2944 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2945 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2946 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2947 | { |
| 2948 | o_addblock(o, str, strlen(str) + 1); |
| 2949 | } |
| 2950 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2951 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2952 | static void nommu_addchr(o_string *o, int ch) |
| 2953 | { |
| 2954 | if (o) |
| 2955 | o_addchr(o, ch); |
| 2956 | } |
| 2957 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2958 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2959 | #endif |
| 2960 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2961 | #if ENABLE_HUSH_MODE_X |
| 2962 | static void x_mode_addchr(int ch) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2963 | { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2964 | o_addchr(&G.x_mode_buf, ch); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2965 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2966 | static void x_mode_addstr(const char *str) |
| 2967 | { |
| 2968 | o_addstr(&G.x_mode_buf, str); |
| 2969 | } |
| 2970 | static void x_mode_addblock(const char *str, int len) |
| 2971 | { |
| 2972 | o_addblock(&G.x_mode_buf, str, len); |
| 2973 | } |
| 2974 | static void x_mode_prefix(void) |
| 2975 | { |
| 2976 | int n = G.x_mode_depth; |
| 2977 | do x_mode_addchr('+'); while (--n >= 0); |
| 2978 | } |
| 2979 | static void x_mode_flush(void) |
| 2980 | { |
| 2981 | int len = G.x_mode_buf.length; |
| 2982 | if (len <= 0) |
| 2983 | return; |
| 2984 | if (G.x_mode_fd > 0) { |
| 2985 | G.x_mode_buf.data[len] = '\n'; |
| 2986 | full_write(G.x_mode_fd, G.x_mode_buf.data, len + 1); |
| 2987 | } |
| 2988 | G.x_mode_buf.length = 0; |
| 2989 | } |
| 2990 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2991 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2992 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2993 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2994 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2995 | * Apparently, on unquoted $v bash still does globbing |
| 2996 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2997 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2998 | * quoting mechanisms on $v expansion side: one protects |
| 2999 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 3000 | * We have only second one. |
| 3001 | */ |
| 3002 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3003 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3004 | # define MAYBE_BRACES "{}" |
| 3005 | #else |
| 3006 | # define MAYBE_BRACES "" |
| 3007 | #endif |
| 3008 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3009 | /* My analysis of quoting semantics tells me that state information |
| 3010 | * is associated with a destination, not a source. |
| 3011 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3012 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3013 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3014 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3015 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3016 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3017 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3018 | o_grow_by(o, sz); |
| 3019 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3020 | o->data[o->length] = '\\'; |
| 3021 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3022 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3023 | o->data[o->length] = ch; |
| 3024 | o->length++; |
| 3025 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3028 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3029 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3030 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3031 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 3032 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 3033 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3034 | sz++; |
| 3035 | o->data[o->length] = '\\'; |
| 3036 | o->length++; |
| 3037 | } |
| 3038 | o_grow_by(o, sz); |
| 3039 | o->data[o->length] = ch; |
| 3040 | o->length++; |
| 3041 | o->data[o->length] = '\0'; |
| 3042 | } |
| 3043 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3044 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3045 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3046 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3047 | char ch; |
| 3048 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3049 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3050 | if (ordinary_cnt > len) /* paranoia */ |
| 3051 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3052 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3053 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3054 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3055 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3056 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3057 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3058 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3059 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3060 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3061 | sz++; |
| 3062 | o->data[o->length] = '\\'; |
| 3063 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3064 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3065 | o_grow_by(o, sz); |
| 3066 | o->data[o->length] = ch; |
| 3067 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3068 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3069 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3072 | static void o_addQblock(o_string *o, const char *str, int len) |
| 3073 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3074 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3075 | o_addblock(o, str, len); |
| 3076 | return; |
| 3077 | } |
| 3078 | o_addqblock(o, str, len); |
| 3079 | } |
| 3080 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3081 | static void o_addQstr(o_string *o, const char *str) |
| 3082 | { |
| 3083 | o_addQblock(o, str, strlen(str)); |
| 3084 | } |
| 3085 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3086 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 3087 | * 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] | 3088 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3089 | * list[i] contains an INDEX (int!) into this string data. |
| 3090 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 3091 | * but list[i]'s need not be modified. |
| 3092 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3093 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3094 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 3095 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3096 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 3097 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 3098 | { |
| 3099 | char **list = (char**)o->data; |
| 3100 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3101 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3102 | |
| 3103 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3104 | 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] | 3105 | prefix, list, n, string_start, o->length, o->maxlen, |
| 3106 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 3107 | o->has_quoted_part, |
| 3108 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3109 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3110 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3111 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 3112 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 3113 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3114 | i++; |
| 3115 | } |
| 3116 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3117 | 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] | 3118 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3119 | 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] | 3120 | } |
| 3121 | } |
| 3122 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3123 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3124 | #endif |
| 3125 | |
| 3126 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3127 | * in list[n] so that it points past last stored byte so far. |
| 3128 | * It returns n+1. */ |
| 3129 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3130 | { |
| 3131 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3132 | int string_start; |
| 3133 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3134 | |
| 3135 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3136 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3137 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3138 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3139 | 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] | 3140 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3141 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3142 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3143 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3144 | memmove(list + n + 0x10, list + n, string_len); |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 3145 | /* |
| 3146 | * expand_on_ifs() has a "previous argv[] ends in IFS?" |
| 3147 | * check. (grep for -prev-ifs-check-). |
| 3148 | * Ensure that argv[-1][last] is not garbage |
| 3149 | * but zero bytes, to save index check there. |
| 3150 | */ |
| 3151 | list[n + 0x10 - 1] = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3152 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3153 | } else { |
| 3154 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3155 | n, string_len, string_start); |
| 3156 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3157 | } else { |
| 3158 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3159 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3160 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3161 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3162 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3163 | o->has_empty_slot = 0; |
| 3164 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3165 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3166 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3167 | return n + 1; |
| 3168 | } |
| 3169 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3170 | /* "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] | 3171 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3172 | { |
| 3173 | char **list = (char**)o->data; |
| 3174 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3175 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3176 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3179 | /* |
| 3180 | * Globbing routines. |
| 3181 | * |
| 3182 | * Most words in commands need to be globbed, even ones which are |
| 3183 | * (single or double) quoted. This stems from the possiblity of |
| 3184 | * constructs like "abc"* and 'abc'* - these should be globbed. |
| 3185 | * Having a different code path for fully-quoted strings ("abc", |
| 3186 | * 'abc') would only help performance-wise, but we still need |
| 3187 | * code for partially-quoted strings. |
| 3188 | * |
| 3189 | * Unfortunately, if we want to match bash and ash behavior in all cases, |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3190 | * the logic can't be "shell-syntax argument is first transformed |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3191 | * to a string, then globbed, and if globbing does not match anything, |
| 3192 | * it is used verbatim". Here are two examples where it fails: |
| 3193 | * |
| 3194 | * echo 'b\*'? |
| 3195 | * |
| 3196 | * The globbing can't be avoided (because of '?' at the end). |
| 3197 | * The glob pattern is: b\\\*? - IOW, both \ and * are literals |
| 3198 | * and are glob-escaped. If this does not match, bash/ash print b\*? |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3199 | * - IOW: they "unbackslash" the glob pattern. |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3200 | * Now, look at this: |
| 3201 | * |
| 3202 | * v='\\\*'; echo b$v? |
| 3203 | * |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3204 | * The glob pattern is the same here: b\\\*? - the unquoted $v expansion |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3205 | * should be used as glob pattern with no changes. However, if glob |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3206 | * does not match, bash/ash print b\\\*? - NOT THE SAME as first example! |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3207 | * |
| 3208 | * ash implements this by having an encoded representation of the word |
| 3209 | * to glob, which IS NOT THE SAME as the glob pattern - it has more data. |
| 3210 | * Glob pattern is derived from it. If glob fails, the decision what result |
| 3211 | * should be is made using that encoded representation. Not glob pattern. |
| 3212 | */ |
| 3213 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3214 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3215 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3216 | * first, it processes even {a} (no commas), second, |
| 3217 | * 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] | 3218 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3219 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3220 | |
| 3221 | /* Helper */ |
| 3222 | static int glob_needed(const char *s) |
| 3223 | { |
| 3224 | while (*s) { |
| 3225 | if (*s == '\\') { |
| 3226 | if (!s[1]) |
| 3227 | return 0; |
| 3228 | s += 2; |
| 3229 | continue; |
| 3230 | } |
| 3231 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3232 | return 1; |
| 3233 | s++; |
| 3234 | } |
| 3235 | return 0; |
| 3236 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3237 | /* Return pointer to next closing brace or to comma */ |
| 3238 | static const char *next_brace_sub(const char *cp) |
| 3239 | { |
| 3240 | unsigned depth = 0; |
| 3241 | cp++; |
| 3242 | while (*cp != '\0') { |
| 3243 | if (*cp == '\\') { |
| 3244 | if (*++cp == '\0') |
| 3245 | break; |
| 3246 | cp++; |
| 3247 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3248 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3249 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3250 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3251 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3252 | depth++; |
| 3253 | } |
| 3254 | |
| 3255 | return *cp != '\0' ? cp : NULL; |
| 3256 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3257 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3258 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3259 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3260 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3261 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3262 | const char *next; |
| 3263 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3264 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3265 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3266 | |
| 3267 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3268 | |
| 3269 | begin = pattern; |
| 3270 | while (1) { |
| 3271 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3272 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3273 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3274 | /* Find the first sub-pattern and at the same time |
| 3275 | * find the rest after the closing brace */ |
| 3276 | next = next_brace_sub(begin); |
| 3277 | if (next == NULL) { |
| 3278 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3279 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3280 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3281 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3282 | /* "{abc}" with no commas - illegal |
| 3283 | * brace expr, disregard and skip it */ |
| 3284 | begin = next + 1; |
| 3285 | continue; |
| 3286 | } |
| 3287 | break; |
| 3288 | } |
| 3289 | if (*begin == '\\' && begin[1] != '\0') |
| 3290 | begin++; |
| 3291 | begin++; |
| 3292 | } |
| 3293 | debug_printf_glob("begin:%s\n", begin); |
| 3294 | debug_printf_glob("next:%s\n", next); |
| 3295 | |
| 3296 | /* Now find the end of the whole brace expression */ |
| 3297 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3298 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3299 | rest = next_brace_sub(rest); |
| 3300 | if (rest == NULL) { |
| 3301 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3302 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3303 | } |
| 3304 | debug_printf_glob("rest:%s\n", rest); |
| 3305 | } |
| 3306 | rest_len = strlen(++rest) + 1; |
| 3307 | |
| 3308 | /* We are sure the brace expression is well-formed */ |
| 3309 | |
| 3310 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3311 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3312 | |
| 3313 | /* We have a brace expression. BEGIN points to the opening {, |
| 3314 | * NEXT points past the terminator of the first element, and REST |
| 3315 | * points past the final }. We will accumulate result names from |
| 3316 | * recursive runs for each brace alternative in the buffer using |
| 3317 | * GLOB_APPEND. */ |
| 3318 | |
| 3319 | p = begin + 1; |
| 3320 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3321 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3322 | memcpy( |
| 3323 | mempcpy( |
| 3324 | mempcpy(new_pattern_buf, |
| 3325 | /* We know the prefix for all sub-patterns */ |
| 3326 | pattern, begin - pattern), |
| 3327 | p, next - p), |
| 3328 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3329 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3330 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3331 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3332 | */ |
| 3333 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3334 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3335 | /* We saw the last entry */ |
| 3336 | break; |
| 3337 | } |
| 3338 | p = next + 1; |
| 3339 | next = next_brace_sub(next); |
| 3340 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3341 | free(new_pattern_buf); |
| 3342 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3343 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3344 | simple_glob: |
| 3345 | { |
| 3346 | int gr; |
| 3347 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3348 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3349 | memset(&globdata, 0, sizeof(globdata)); |
| 3350 | gr = glob(pattern, 0, NULL, &globdata); |
| 3351 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3352 | if (gr != 0) { |
| 3353 | if (gr == GLOB_NOMATCH) { |
| 3354 | globfree(&globdata); |
| 3355 | /* NB: garbles parameter */ |
| 3356 | unbackslash(pattern); |
| 3357 | o_addstr_with_NUL(o, pattern); |
| 3358 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3359 | return o_save_ptr_helper(o, n); |
| 3360 | } |
| 3361 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3362 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3363 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3364 | * but we didn't specify it. Paranoia again. */ |
| 3365 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3366 | } |
| 3367 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3368 | char **argv = globdata.gl_pathv; |
| 3369 | while (1) { |
| 3370 | o_addstr_with_NUL(o, *argv); |
| 3371 | n = o_save_ptr_helper(o, n); |
| 3372 | argv++; |
| 3373 | if (!*argv) |
| 3374 | break; |
| 3375 | } |
| 3376 | } |
| 3377 | globfree(&globdata); |
| 3378 | } |
| 3379 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3380 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3381 | /* Performs globbing on last list[], |
| 3382 | * saving each result as a new list[]. |
| 3383 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3384 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3385 | { |
| 3386 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3387 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3388 | 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] | 3389 | if (!o->data) |
| 3390 | return o_save_ptr_helper(o, n); |
| 3391 | pattern = o->data + o_get_last_ptr(o, n); |
| 3392 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3393 | if (!glob_needed(pattern)) { |
| 3394 | /* unbackslash last string in o in place, fix length */ |
| 3395 | o->length = unbackslash(pattern) - o->data; |
| 3396 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3397 | return o_save_ptr_helper(o, n); |
| 3398 | } |
| 3399 | |
| 3400 | copy = xstrdup(pattern); |
| 3401 | /* "forget" pattern in o */ |
| 3402 | o->length = pattern - o->data; |
| 3403 | n = glob_brace(copy, o, n); |
| 3404 | free(copy); |
| 3405 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3406 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3407 | return n; |
| 3408 | } |
| 3409 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3410 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3411 | |
| 3412 | /* Helper */ |
| 3413 | static int glob_needed(const char *s) |
| 3414 | { |
| 3415 | while (*s) { |
| 3416 | if (*s == '\\') { |
| 3417 | if (!s[1]) |
| 3418 | return 0; |
| 3419 | s += 2; |
| 3420 | continue; |
| 3421 | } |
| 3422 | if (*s == '*' || *s == '[' || *s == '?') |
| 3423 | return 1; |
| 3424 | s++; |
| 3425 | } |
| 3426 | return 0; |
| 3427 | } |
| 3428 | /* Performs globbing on last list[], |
| 3429 | * saving each result as a new list[]. |
| 3430 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3431 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3432 | { |
| 3433 | glob_t globdata; |
| 3434 | int gr; |
| 3435 | char *pattern; |
| 3436 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3437 | 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] | 3438 | if (!o->data) |
| 3439 | return o_save_ptr_helper(o, n); |
| 3440 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3441 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3442 | if (!glob_needed(pattern)) { |
| 3443 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3444 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3445 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3446 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3447 | return o_save_ptr_helper(o, n); |
| 3448 | } |
| 3449 | |
| 3450 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3451 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3452 | * If we glob "*.\*" and don't find anything, we need |
| 3453 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3454 | * will return "*.\*"! |
| 3455 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3456 | gr = glob(pattern, 0, NULL, &globdata); |
| 3457 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3458 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3459 | if (gr == GLOB_NOMATCH) { |
| 3460 | globfree(&globdata); |
| 3461 | goto literal; |
| 3462 | } |
| 3463 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3464 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3465 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3466 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3467 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3468 | } |
| 3469 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3470 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3471 | /* "forget" pattern in o */ |
| 3472 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3473 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3474 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3475 | n = o_save_ptr_helper(o, n); |
| 3476 | argv++; |
| 3477 | if (!*argv) |
| 3478 | break; |
| 3479 | } |
| 3480 | } |
| 3481 | globfree(&globdata); |
| 3482 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3483 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3484 | return n; |
| 3485 | } |
| 3486 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3487 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3488 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3489 | /* 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] | 3490 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3491 | static int o_save_ptr(o_string *o, int n) |
| 3492 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3493 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3494 | /* If o->has_empty_slot, list[n] was already globbed |
| 3495 | * (if it was requested back then when it was filled) |
| 3496 | * so don't do that again! */ |
| 3497 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3498 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3499 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3500 | return o_save_ptr_helper(o, n); |
| 3501 | } |
| 3502 | |
| 3503 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3504 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3505 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3506 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3507 | int string_start; |
| 3508 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3509 | if (DEBUG_EXPAND) |
| 3510 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3511 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3512 | list = (char**)o->data; |
| 3513 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3514 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3515 | while (n) { |
| 3516 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3517 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3518 | } |
| 3519 | return list; |
| 3520 | } |
| 3521 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3522 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3523 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3524 | /* Returns pi->next - next pipe in the list */ |
| 3525 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3526 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3527 | struct pipe *next; |
| 3528 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3529 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3530 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3531 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3532 | struct command *command; |
| 3533 | struct redir_struct *r, *rnext; |
| 3534 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3535 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3536 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3537 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3538 | if (DEBUG_CLEAN) { |
| 3539 | int a; |
| 3540 | char **p; |
| 3541 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3542 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3543 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3544 | } |
| 3545 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3546 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3547 | } |
| 3548 | /* not "else if": on syntax error, we may have both! */ |
| 3549 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3550 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3551 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3552 | free_pipe_list(command->group); |
| 3553 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3554 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3555 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3556 | /* else is crucial here. |
| 3557 | * If group != NULL, child_func is meaningless */ |
| 3558 | #if ENABLE_HUSH_FUNCTIONS |
| 3559 | else if (command->child_func) { |
| 3560 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3561 | command->child_func->parent_cmd = NULL; |
| 3562 | } |
| 3563 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3564 | #if !BB_MMU |
| 3565 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3566 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3567 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3568 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3569 | debug_printf_clean(" redirect %d%s", |
| 3570 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3571 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3572 | if (r->rd_filename) { |
| 3573 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3574 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3575 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3576 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3577 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3578 | rnext = r->next; |
| 3579 | free(r); |
| 3580 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3581 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3582 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3583 | 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] | 3584 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3585 | #if ENABLE_HUSH_JOB |
| 3586 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3587 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3588 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3589 | |
| 3590 | next = pi->next; |
| 3591 | free(pi); |
| 3592 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3593 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3594 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3595 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3596 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3597 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3598 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3599 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3600 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3601 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3602 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3603 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
| 3606 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3607 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3608 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3609 | #ifndef debug_print_tree |
| 3610 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3611 | { |
| 3612 | static const char *const PIPE[] = { |
| 3613 | [PIPE_SEQ] = "SEQ", |
| 3614 | [PIPE_AND] = "AND", |
| 3615 | [PIPE_OR ] = "OR" , |
| 3616 | [PIPE_BG ] = "BG" , |
| 3617 | }; |
| 3618 | static const char *RES[] = { |
| 3619 | [RES_NONE ] = "NONE" , |
| 3620 | # if ENABLE_HUSH_IF |
| 3621 | [RES_IF ] = "IF" , |
| 3622 | [RES_THEN ] = "THEN" , |
| 3623 | [RES_ELIF ] = "ELIF" , |
| 3624 | [RES_ELSE ] = "ELSE" , |
| 3625 | [RES_FI ] = "FI" , |
| 3626 | # endif |
| 3627 | # if ENABLE_HUSH_LOOPS |
| 3628 | [RES_FOR ] = "FOR" , |
| 3629 | [RES_WHILE] = "WHILE", |
| 3630 | [RES_UNTIL] = "UNTIL", |
| 3631 | [RES_DO ] = "DO" , |
| 3632 | [RES_DONE ] = "DONE" , |
| 3633 | # endif |
| 3634 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3635 | [RES_IN ] = "IN" , |
| 3636 | # endif |
| 3637 | # if ENABLE_HUSH_CASE |
| 3638 | [RES_CASE ] = "CASE" , |
| 3639 | [RES_CASE_IN ] = "CASE_IN" , |
| 3640 | [RES_MATCH] = "MATCH", |
| 3641 | [RES_CASE_BODY] = "CASE_BODY", |
| 3642 | [RES_ESAC ] = "ESAC" , |
| 3643 | # endif |
| 3644 | [RES_XXXX ] = "XXXX" , |
| 3645 | [RES_SNTX ] = "SNTX" , |
| 3646 | }; |
| 3647 | static const char *const CMDTYPE[] = { |
| 3648 | "{}", |
| 3649 | "()", |
| 3650 | "[noglob]", |
| 3651 | # if ENABLE_HUSH_FUNCTIONS |
| 3652 | "func()", |
| 3653 | # endif |
| 3654 | }; |
| 3655 | |
| 3656 | int pin, prn; |
| 3657 | |
| 3658 | pin = 0; |
| 3659 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3660 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3661 | lvl*2, "", |
| 3662 | pin, |
| 3663 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3664 | RES[pi->res_word], |
| 3665 | pi->followup, PIPE[pi->followup] |
| 3666 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3667 | prn = 0; |
| 3668 | while (prn < pi->num_cmds) { |
| 3669 | struct command *command = &pi->cmds[prn]; |
| 3670 | char **argv = command->argv; |
| 3671 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3672 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3673 | lvl*2, "", prn, |
| 3674 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3675 | #if ENABLE_HUSH_LINENO_VAR |
| 3676 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3677 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3678 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3679 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3680 | CMDTYPE[command->cmd_type], |
| 3681 | argv |
| 3682 | # if !BB_MMU |
| 3683 | , " group_as_string:", command->group_as_string |
| 3684 | # else |
| 3685 | , "", "" |
| 3686 | # endif |
| 3687 | ); |
| 3688 | debug_print_tree(command->group, lvl+1); |
| 3689 | prn++; |
| 3690 | continue; |
| 3691 | } |
| 3692 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3693 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3694 | argv++; |
| 3695 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 3696 | if (command->redirects) |
| 3697 | fdprintf(2, " {redir}"); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3698 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3699 | prn++; |
| 3700 | } |
| 3701 | pi = pi->next; |
| 3702 | pin++; |
| 3703 | } |
| 3704 | } |
| 3705 | #endif /* debug_print_tree */ |
| 3706 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3707 | static struct pipe *new_pipe(void) |
| 3708 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3709 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3710 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3711 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3712 | return pi; |
| 3713 | } |
| 3714 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3715 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3716 | * if ctx->command is NULL. |
| 3717 | * No errors possible here. |
| 3718 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3719 | static int done_command(struct parse_context *ctx) |
| 3720 | { |
| 3721 | /* The command is really already in the pipe structure, so |
| 3722 | * advance the pipe counter and make a new, null command. */ |
| 3723 | struct pipe *pi = ctx->pipe; |
| 3724 | struct command *command = ctx->command; |
| 3725 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3726 | #if 0 /* Instead we emit error message at run time */ |
| 3727 | if (ctx->pending_redirect) { |
| 3728 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3729 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3730 | ctx->pending_redirect = NULL; |
| 3731 | } |
| 3732 | #endif |
| 3733 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3734 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3735 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3736 | 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] | 3737 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3738 | } |
| 3739 | pi->num_cmds++; |
| 3740 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3741 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3742 | } else { |
| 3743 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3744 | } |
| 3745 | |
| 3746 | /* Only real trickiness here is that the uncommitted |
| 3747 | * command structure is not counted in pi->num_cmds. */ |
| 3748 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3749 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3750 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3751 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3752 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3753 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3754 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3755 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3756 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3757 | } |
| 3758 | |
| 3759 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3760 | { |
| 3761 | int not_null; |
| 3762 | |
| 3763 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3764 | /* Close previous command */ |
| 3765 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3766 | #if HAS_KEYWORDS |
| 3767 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3768 | ctx->ctx_inverted = 0; |
| 3769 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3770 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3771 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3772 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3773 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3774 | * in a backgrounded subshell. |
| 3775 | */ |
| 3776 | struct pipe *pi; |
| 3777 | struct command *command; |
| 3778 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3779 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3780 | pi = ctx->list_head; |
| 3781 | while (pi != ctx->pipe) { |
| 3782 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3783 | goto no_conv; |
| 3784 | pi = pi->next; |
| 3785 | } |
| 3786 | |
| 3787 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3788 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3789 | pi = xzalloc(sizeof(*pi)); |
| 3790 | pi->followup = PIPE_BG; |
| 3791 | pi->num_cmds = 1; |
| 3792 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3793 | command = &pi->cmds[0]; |
| 3794 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3795 | command->cmd_type = CMD_NORMAL; |
| 3796 | command->group = ctx->list_head; |
| 3797 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3798 | command->group_as_string = xstrndup( |
| 3799 | ctx->as_string.data, |
| 3800 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3801 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3802 | #endif |
| 3803 | /* Replace all pipes in ctx with one newly created */ |
| 3804 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3805 | } else { |
| 3806 | no_conv: |
| 3807 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3808 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3809 | |
| 3810 | /* Without this check, even just <enter> on command line generates |
| 3811 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3812 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3813 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3814 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3815 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3816 | #endif |
| 3817 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3818 | || ctx->ctx_res_w == RES_DONE |
| 3819 | || ctx->ctx_res_w == RES_FOR |
| 3820 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3821 | #endif |
| 3822 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3823 | || ctx->ctx_res_w == RES_ESAC |
| 3824 | #endif |
| 3825 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3826 | struct pipe *new_p; |
| 3827 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3828 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3829 | not_null, ctx->ctx_res_w); |
| 3830 | new_p = new_pipe(); |
| 3831 | ctx->pipe->next = new_p; |
| 3832 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3833 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3834 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3835 | * This is used to control execution. |
| 3836 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3837 | * cases where variable or value happens to match a keyword): |
| 3838 | */ |
| 3839 | #if ENABLE_HUSH_LOOPS |
| 3840 | if (ctx->ctx_res_w == RES_FOR |
| 3841 | || ctx->ctx_res_w == RES_IN) |
| 3842 | ctx->ctx_res_w = RES_NONE; |
| 3843 | #endif |
| 3844 | #if ENABLE_HUSH_CASE |
| 3845 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3846 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3847 | if (ctx->ctx_res_w == RES_CASE) |
| 3848 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3849 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3850 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3851 | /* Create the memory for command, roughly: |
| 3852 | * ctx->pipe->cmds = new struct command; |
| 3853 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3854 | */ |
| 3855 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3856 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3857 | } |
| 3858 | debug_printf_parse("done_pipe return\n"); |
| 3859 | } |
| 3860 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3861 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3862 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3863 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3864 | if (MAYBE_ASSIGNMENT != 0) |
| 3865 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3866 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3867 | /* Create the memory for command, roughly: |
| 3868 | * ctx->pipe->cmds = new struct command; |
| 3869 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3870 | */ |
| 3871 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3874 | /* If a reserved word is found and processed, parse context is modified |
| 3875 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3876 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3877 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3878 | struct reserved_combo { |
| 3879 | char literal[6]; |
| 3880 | unsigned char res; |
| 3881 | unsigned char assignment_flag; |
| 3882 | int flag; |
| 3883 | }; |
| 3884 | enum { |
| 3885 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3886 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3887 | FLAG_IF = (1 << RES_IF ), |
| 3888 | FLAG_THEN = (1 << RES_THEN ), |
| 3889 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3890 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3891 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3892 | # endif |
| 3893 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3894 | FLAG_FOR = (1 << RES_FOR ), |
| 3895 | FLAG_WHILE = (1 << RES_WHILE), |
| 3896 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3897 | FLAG_DO = (1 << RES_DO ), |
| 3898 | FLAG_DONE = (1 << RES_DONE ), |
| 3899 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3900 | # endif |
| 3901 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3902 | FLAG_MATCH = (1 << RES_MATCH), |
| 3903 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3904 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3905 | FLAG_START = (1 << RES_XXXX ), |
| 3906 | }; |
| 3907 | |
| 3908 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3909 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3910 | /* Mostly a list of accepted follow-up reserved words. |
| 3911 | * FLAG_END means we are done with the sequence, and are ready |
| 3912 | * to turn the compound list into a command. |
| 3913 | * FLAG_START means the word must start a new compound list. |
| 3914 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3915 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3916 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3917 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3918 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3919 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3920 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3921 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3922 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3923 | # endif |
| 3924 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3925 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3926 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3927 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3928 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3929 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3930 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3931 | # endif |
| 3932 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3933 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3934 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3935 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3936 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3937 | const struct reserved_combo *r; |
| 3938 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3939 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3940 | if (strcmp(word->data, r->literal) == 0) |
| 3941 | return r; |
| 3942 | } |
| 3943 | return NULL; |
| 3944 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3945 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3946 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3947 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3948 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3949 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3950 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3951 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3952 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3953 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3954 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3955 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3956 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3957 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3958 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3959 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3960 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3961 | |
| 3962 | 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] | 3963 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3964 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3965 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3966 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3967 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3968 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3969 | if (r->flag == 0) { /* '!' */ |
| 3970 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3971 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3972 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3973 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3974 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3975 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3976 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3977 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3978 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3979 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3980 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3981 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3982 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3983 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3984 | } 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] | 3985 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3986 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3987 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3988 | } else { |
| 3989 | /* "{...} fi" is ok. "{...} if" is not |
| 3990 | * Example: |
| 3991 | * if { echo foo; } then { echo bar; } fi */ |
| 3992 | if (ctx->command->group) |
| 3993 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3994 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3995 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3996 | ctx->ctx_res_w = r->res; |
| 3997 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3998 | ctx->is_assignment = r->assignment_flag; |
| 3999 | 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] | 4000 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4001 | if (ctx->old_flag & FLAG_END) { |
| 4002 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4003 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4004 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4005 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4006 | old = ctx->stack; |
| 4007 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4008 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4009 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4010 | /* At this point, the compound command's string is in |
| 4011 | * ctx->as_string... except for the leading keyword! |
| 4012 | * Consider this example: "echo a | if true; then echo a; fi" |
| 4013 | * ctx->as_string will contain "true; then echo a; fi", |
| 4014 | * with "if " remaining in old->as_string! |
| 4015 | */ |
| 4016 | { |
| 4017 | char *str; |
| 4018 | int len = old->as_string.length; |
| 4019 | /* Concatenate halves */ |
| 4020 | o_addstr(&old->as_string, ctx->as_string.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4021 | o_free(&ctx->as_string); |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4022 | /* Find where leading keyword starts in first half */ |
| 4023 | str = old->as_string.data + len; |
| 4024 | if (str > old->as_string.data) |
| 4025 | str--; /* skip whitespace after keyword */ |
| 4026 | while (str > old->as_string.data && isalpha(str[-1])) |
| 4027 | str--; |
| 4028 | /* Ugh, we're done with this horrid hack */ |
| 4029 | old->command->group_as_string = xstrdup(str); |
| 4030 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4031 | old->command->group_as_string); |
| 4032 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4033 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4034 | *ctx = *old; /* physical copy */ |
| 4035 | free(old); |
| 4036 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4037 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4038 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4039 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4040 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4041 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4042 | * Normal return is 0. Syntax errors return 1. |
| 4043 | * Note: on return, word is reset, but not o_free'd! |
| 4044 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4045 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4046 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4047 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4048 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4049 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 4050 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4051 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4052 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4053 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4054 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4055 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4056 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4057 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4058 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4059 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4060 | * If the redirection operator is "<<" or "<<-", the word |
| 4061 | * that follows the redirection operator shall be |
| 4062 | * subjected to quote removal; it is unspecified whether |
| 4063 | * any of the other expansions occur. For the other |
| 4064 | * redirection operators, the word that follows the |
| 4065 | * redirection operator shall be subjected to tilde |
| 4066 | * expansion, parameter expansion, command substitution, |
| 4067 | * arithmetic expansion, and quote removal. |
| 4068 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4069 | * on the word by a non-interactive shell; an interactive |
| 4070 | * shell may perform it, but shall do so only when |
| 4071 | * the expansion would result in one word." |
| 4072 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 4073 | //bash does not do parameter/command substitution or arithmetic expansion |
| 4074 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 4075 | // as written: |
| 4076 | // <<EOF$t |
| 4077 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4078 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 4079 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4080 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4081 | /* Cater for >\file case: |
| 4082 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4083 | * Same with heredocs: |
| 4084 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4085 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4086 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4087 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4088 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4089 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4090 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4091 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4092 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4093 | 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] | 4094 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4095 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4096 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4097 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4098 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4099 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4100 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4101 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4102 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4103 | ctx->ctx_dsemicolon = 0; |
| 4104 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4105 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4106 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4107 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4108 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4109 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4110 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4111 | # if ENABLE_HUSH_CASE |
| 4112 | && ctx->ctx_res_w != RES_CASE |
| 4113 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4114 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4115 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4116 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4117 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4118 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4119 | # if ENABLE_HUSH_LINENO_VAR |
| 4120 | /* Case: |
| 4121 | * "while ...; do |
| 4122 | * cmd ..." |
| 4123 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 4124 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 4125 | */ |
| 4126 | if (0 |
| 4127 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 4128 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 4129 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 4130 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 4131 | ) { |
| 4132 | done_pipe(ctx, PIPE_SEQ); |
| 4133 | } |
| 4134 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4135 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4136 | debug_printf_parse("done_word return %d\n", |
| 4137 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4138 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4139 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4140 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 4141 | if (0 |
| 4142 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4143 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4144 | # endif |
| 4145 | /* In bash, local/export/readonly are special, args |
| 4146 | * are assignments and therefore expansion of them |
| 4147 | * should be "one-word" expansion: |
| 4148 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 4149 | * compare with: |
| 4150 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 4151 | * ls: cannot access i=a: No such file or directory |
| 4152 | * ls: cannot access b: No such file or directory |
| 4153 | * Note: bash 3.2.33(1) does this only if export word |
| 4154 | * itself is not quoted: |
| 4155 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 4156 | * aaa bbb |
| 4157 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 4158 | * aaa |
| 4159 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4160 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 4161 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 4162 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4163 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4164 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4165 | } |
| 4166 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4167 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4168 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4169 | #endif /* HAS_KEYWORDS */ |
| 4170 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4171 | if (command->group) { |
| 4172 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4173 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4174 | debug_printf_parse("done_word return 1: syntax error, " |
| 4175 | "groups and arglists don't mix\n"); |
| 4176 | return 1; |
| 4177 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4178 | |
| 4179 | /* If this word wasn't an assignment, next ones definitely |
| 4180 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4181 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4182 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4183 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4184 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4185 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4186 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4187 | command->assignment_cnt++; |
| 4188 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4189 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4190 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4191 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4192 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4193 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4194 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4195 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4196 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4197 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4198 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4199 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4200 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4201 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4202 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4203 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4204 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4205 | return 1; |
| 4206 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4207 | /* Force FOR to have just one word (variable name) */ |
| 4208 | /* NB: basically, this makes hush see "for v in ..." |
| 4209 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4210 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4211 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4212 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4213 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4214 | #if ENABLE_HUSH_CASE |
| 4215 | /* Force CASE to have just one word */ |
| 4216 | if (ctx->ctx_res_w == RES_CASE) { |
| 4217 | done_pipe(ctx, PIPE_SEQ); |
| 4218 | } |
| 4219 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4220 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4221 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4222 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4223 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4224 | return 0; |
| 4225 | } |
| 4226 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4227 | |
| 4228 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4229 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4230 | * Return: |
| 4231 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4232 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4233 | * REDIRFD_TO_FILE if no & was seen, |
| 4234 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4235 | */ |
| 4236 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4237 | #define parse_redir_right_fd(as_string, input) \ |
| 4238 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4239 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4240 | 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] | 4241 | { |
| 4242 | int ch, d, ok; |
| 4243 | |
| 4244 | ch = i_peek(input); |
| 4245 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4246 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4247 | |
| 4248 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4249 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4250 | ch = i_peek(input); |
| 4251 | if (ch == '-') { |
| 4252 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4253 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4254 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4255 | } |
| 4256 | d = 0; |
| 4257 | ok = 0; |
| 4258 | while (ch != EOF && isdigit(ch)) { |
| 4259 | d = d*10 + (ch-'0'); |
| 4260 | ok = 1; |
| 4261 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4262 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4263 | ch = i_peek(input); |
| 4264 | } |
| 4265 | if (ok) return d; |
| 4266 | |
| 4267 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4268 | |
| 4269 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4270 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4273 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4274 | */ |
| 4275 | static int parse_redirect(struct parse_context *ctx, |
| 4276 | int fd, |
| 4277 | redir_type style, |
| 4278 | struct in_str *input) |
| 4279 | { |
| 4280 | struct command *command = ctx->command; |
| 4281 | struct redir_struct *redir; |
| 4282 | struct redir_struct **redirp; |
| 4283 | int dup_num; |
| 4284 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4285 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4286 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4287 | /* Check for a '>&1' type redirect */ |
| 4288 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4289 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4290 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4291 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4292 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4293 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4294 | if (dup_num) { /* <<-... */ |
| 4295 | ch = i_getch(input); |
| 4296 | nommu_addchr(&ctx->as_string, ch); |
| 4297 | ch = i_peek(input); |
| 4298 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4299 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4300 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4301 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4302 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4303 | if (ch == '|') { |
| 4304 | /* >|FILE redirect ("clobbering" >). |
| 4305 | * Since we do not support "set -o noclobber" yet, |
| 4306 | * >| and > are the same for now. Just eat |. |
| 4307 | */ |
| 4308 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4309 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | |
| 4313 | /* Create a new redir_struct and append it to the linked list */ |
| 4314 | redirp = &command->redirects; |
| 4315 | while ((redir = *redirp) != NULL) { |
| 4316 | redirp = &(redir->next); |
| 4317 | } |
| 4318 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4319 | /* redir->next = NULL; */ |
| 4320 | /* redir->rd_filename = NULL; */ |
| 4321 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4322 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4323 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4324 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4325 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4326 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4327 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4328 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4329 | /* Erik had a check here that the file descriptor in question |
| 4330 | * is legit; I postpone that to "run time" |
| 4331 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4332 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4333 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4334 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4335 | #if 0 /* Instead we emit error message at run time */ |
| 4336 | if (ctx->pending_redirect) { |
| 4337 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4338 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4339 | } |
| 4340 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4341 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4342 | * end of the next parsed word. */ |
| 4343 | ctx->pending_redirect = redir; |
| 4344 | } |
| 4345 | return 0; |
| 4346 | } |
| 4347 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4348 | /* If a redirect is immediately preceded by a number, that number is |
| 4349 | * supposed to tell which file descriptor to redirect. This routine |
| 4350 | * looks for such preceding numbers. In an ideal world this routine |
| 4351 | * needs to handle all the following classes of redirects... |
| 4352 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4353 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4354 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4355 | * 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] | 4356 | * |
| 4357 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4358 | * "2.7 Redirection |
| 4359 | * ... If n is quoted, the number shall not be recognized as part of |
| 4360 | * the redirection expression. For example: |
| 4361 | * echo \2>a |
| 4362 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4363 | * 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] | 4364 | * |
| 4365 | * A -1 return means no valid number was found, |
| 4366 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4367 | */ |
| 4368 | static int redirect_opt_num(o_string *o) |
| 4369 | { |
| 4370 | int num; |
| 4371 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4372 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4373 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4374 | num = bb_strtou(o->data, NULL, 10); |
| 4375 | if (errno || num < 0) |
| 4376 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4377 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4378 | return num; |
| 4379 | } |
| 4380 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4381 | #if BB_MMU |
| 4382 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4383 | fetch_till_str(input, word, skip_tabs) |
| 4384 | #endif |
| 4385 | static char *fetch_till_str(o_string *as_string, |
| 4386 | struct in_str *input, |
| 4387 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4388 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4389 | { |
| 4390 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4391 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4392 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4393 | int ch; |
| 4394 | |
Denys Vlasenko | d73cdbf | 2018-07-23 15:43:57 +0200 | [diff] [blame] | 4395 | /* Starting with "" is necessary for this case: |
| 4396 | * cat <<EOF |
| 4397 | * |
| 4398 | * xxx |
| 4399 | * EOF |
| 4400 | */ |
| 4401 | heredoc.data = xzalloc(1); /* start as "", not as NULL */ |
| 4402 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4403 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4404 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4405 | while (1) { |
| 4406 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4407 | if (ch != EOF) |
| 4408 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4409 | if (ch == '\n' || ch == EOF) { |
| 4410 | check_heredoc_end: |
| 4411 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4412 | /* End-of-line, and not a line continuation */ |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4413 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4414 | heredoc.data[past_EOL] = '\0'; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4415 | debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4416 | return heredoc.data; |
| 4417 | } |
| 4418 | if (ch == '\n') { |
| 4419 | /* This is a new line. |
| 4420 | * Remember position and backslash-escaping status. |
| 4421 | */ |
| 4422 | o_addchr(&heredoc, ch); |
| 4423 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4424 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4425 | past_EOL = heredoc.length; |
| 4426 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4427 | do { |
| 4428 | ch = i_getch(input); |
| 4429 | if (ch != EOF) |
| 4430 | nommu_addchr(as_string, ch); |
| 4431 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4432 | /* If this immediately ended the line, |
| 4433 | * go back to end-of-line checks. |
| 4434 | */ |
| 4435 | if (ch == '\n') |
| 4436 | goto check_heredoc_end; |
| 4437 | } |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4438 | } else { |
| 4439 | /* Backslash-line continuation in an unquoted |
| 4440 | * heredoc. This does not need special handling |
| 4441 | * for heredoc body (unquoted heredocs are |
| 4442 | * expanded on "execution" and that would take |
| 4443 | * care of this case too), but not the case |
| 4444 | * of line continuation *in terminator*: |
| 4445 | * cat <<EOF |
| 4446 | * Ok1 |
| 4447 | * EO\ |
| 4448 | * F |
| 4449 | */ |
| 4450 | heredoc.data[--heredoc.length] = '\0'; |
| 4451 | prev = 0; /* not '\' */ |
| 4452 | continue; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4453 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4454 | } |
| 4455 | if (ch == EOF) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4456 | o_free(&heredoc); |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4457 | return NULL; /* error */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4458 | } |
| 4459 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4460 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4461 | if (prev == '\\' && ch == '\\') |
| 4462 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4463 | prev = 0; /* not '\' */ |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4464 | else |
| 4465 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4466 | } |
| 4467 | } |
| 4468 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4469 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4470 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4471 | */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4472 | #if BB_MMU |
| 4473 | #define fetch_heredocs(as_string, pi, heredoc_cnt, input) \ |
| 4474 | fetch_heredocs(pi, heredoc_cnt, input) |
| 4475 | #endif |
| 4476 | static int fetch_heredocs(o_string *as_string, struct pipe *pi, int heredoc_cnt, struct in_str *input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4477 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4478 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4479 | int i; |
| 4480 | struct command *cmd = pi->cmds; |
| 4481 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4482 | debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4483 | pi->num_cmds, |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4484 | cmd->argv ? cmd->argv[0] : "NONE" |
| 4485 | ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4486 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4487 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4488 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4489 | debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4490 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4491 | while (redir) { |
| 4492 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4493 | char *p; |
| 4494 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4495 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4496 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4497 | p = fetch_till_str(as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4498 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4499 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4500 | syntax_error("unexpected EOF in here document"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4501 | return -1; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4502 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4503 | free(redir->rd_filename); |
| 4504 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4505 | heredoc_cnt--; |
| 4506 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4507 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4508 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4509 | if (cmd->group) { |
| 4510 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4511 | heredoc_cnt = fetch_heredocs(as_string, cmd->group, heredoc_cnt, input); |
| 4512 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4513 | if (heredoc_cnt < 0) |
| 4514 | return heredoc_cnt; /* error */ |
| 4515 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4516 | cmd++; |
| 4517 | } |
| 4518 | pi = pi->next; |
| 4519 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4520 | return heredoc_cnt; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4524 | static int run_list(struct pipe *pi); |
| 4525 | #if BB_MMU |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4526 | #define parse_stream(pstring, heredoc_cnt_ptr, input, end_trigger) \ |
| 4527 | parse_stream(heredoc_cnt_ptr, input, end_trigger) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4528 | #endif |
| 4529 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4530 | int *heredoc_cnt_ptr, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4531 | struct in_str *input, |
| 4532 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4533 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4534 | /* Returns number of heredocs not yet consumed, |
| 4535 | * or -1 on error. |
| 4536 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4537 | static int parse_group(struct parse_context *ctx, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4538 | struct in_str *input, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4539 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4540 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4541 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4542 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4543 | #if BB_MMU |
| 4544 | # define as_string NULL |
| 4545 | #else |
| 4546 | char *as_string = NULL; |
| 4547 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4548 | struct pipe *pipe_list; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4549 | int heredoc_cnt = 0; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4550 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4551 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4552 | |
| 4553 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4554 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4555 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4556 | if (ctx->word.length) |
| 4557 | if (done_word(ctx)) |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4558 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4559 | if (!command->argv) |
| 4560 | goto skip; /* (... */ |
| 4561 | if (command->argv[1]) { /* word word ... (... */ |
| 4562 | syntax_error_unexpected_ch('('); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4563 | return -1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4564 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4565 | /* it is "word(..." or "word (..." */ |
| 4566 | do |
| 4567 | ch = i_getch(input); |
| 4568 | while (ch == ' ' || ch == '\t'); |
| 4569 | if (ch != ')') { |
| 4570 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4571 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4572 | } |
| 4573 | nommu_addchr(&ctx->as_string, ch); |
| 4574 | do |
| 4575 | ch = i_getch(input); |
| 4576 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4577 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4578 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4579 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4580 | } |
| 4581 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4582 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4583 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4584 | } |
| 4585 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4586 | |
| 4587 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4588 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4589 | || ctx->word.length /* word{... */ |
| 4590 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4591 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4592 | syntax_error(NULL); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4593 | debug_printf_parse("parse_group return -1: " |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4594 | "syntax error, groups and arglists don't mix\n"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4595 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4596 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4597 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4598 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4599 | IF_HUSH_FUNCTIONS(skip:) |
| 4600 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4601 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4602 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4603 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4604 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4605 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4606 | } else { |
| 4607 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4608 | ch = i_peek(input); |
| 4609 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4610 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4611 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4612 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4613 | return -1; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4614 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4615 | if (ch != '(') { |
| 4616 | ch = i_getch(input); |
| 4617 | nommu_addchr(&ctx->as_string, ch); |
| 4618 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4619 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4620 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4621 | debug_printf_heredoc("calling parse_stream, heredoc_cnt:%d\n", heredoc_cnt); |
| 4622 | pipe_list = parse_stream(&as_string, &heredoc_cnt, input, endch); |
| 4623 | debug_printf_heredoc("parse_stream returned: heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4624 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4625 | if (as_string) |
| 4626 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4627 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4628 | |
| 4629 | /* empty ()/{} or parse error? */ |
| 4630 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4631 | /* parse_stream already emitted error msg */ |
| 4632 | if (!BB_MMU) |
| 4633 | free(as_string); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4634 | debug_printf_parse("parse_group return -1: " |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4635 | "parse_stream returned %p\n", pipe_list); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4636 | return -1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4637 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4638 | #if !BB_MMU |
| 4639 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4640 | command->group_as_string = as_string; |
| 4641 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4642 | command->group_as_string); |
| 4643 | #endif |
| 4644 | |
| 4645 | #if ENABLE_HUSH_FUNCTIONS |
| 4646 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4647 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4648 | struct command *cmd2; |
| 4649 | |
| 4650 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4651 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4652 | cmd2->group = pipe_list; |
| 4653 | # if !BB_MMU |
| 4654 | //UNTESTED! |
| 4655 | cmd2->group_as_string = command->group_as_string; |
| 4656 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4657 | # endif |
| 4658 | |
| 4659 | pipe_list = new_pipe(); |
| 4660 | pipe_list->cmds = cmd2; |
| 4661 | pipe_list->num_cmds = 1; |
| 4662 | } |
| 4663 | #endif |
| 4664 | |
| 4665 | command->group = pipe_list; |
| 4666 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4667 | debug_printf_parse("parse_group return %d\n", heredoc_cnt); |
| 4668 | return heredoc_cnt; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4669 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4670 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4673 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4674 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4675 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4676 | 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] | 4677 | { |
| 4678 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4679 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4680 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4681 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4682 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4683 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4684 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4685 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4686 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4687 | } |
| 4688 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4689 | static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input) |
| 4690 | { |
| 4691 | while (1) { |
| 4692 | int ch = i_getch(input); |
| 4693 | if (ch == EOF) { |
| 4694 | syntax_error_unterm_ch('\''); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | if (ch == '\'') |
| 4698 | return 1; |
| 4699 | o_addqchr(dest, ch); |
| 4700 | } |
| 4701 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4702 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4703 | static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4704 | 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] | 4705 | { |
| 4706 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4707 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4708 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4709 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4710 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4711 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4712 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4713 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4714 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4715 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4716 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4717 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4718 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4719 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4720 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4721 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4722 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4723 | continue; |
| 4724 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4725 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4726 | } |
| 4727 | } |
| 4728 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4729 | * \` quoting. |
| 4730 | * "Within the backquoted style of command substitution, backslash |
| 4731 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4732 | * The search for the matching backquote shall be satisfied by the first |
| 4733 | * backquote found without a preceding backslash; during this search, |
| 4734 | * if a non-escaped backquote is encountered within a shell comment, |
| 4735 | * a here-document, an embedded command substitution of the $(command) |
| 4736 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4737 | * or double-quoted string that begins, but does not end, within the |
| 4738 | * "`...`" sequence produces undefined results." |
| 4739 | * Example Output |
| 4740 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4741 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4742 | 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] | 4743 | { |
| 4744 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4745 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4746 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4747 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4748 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4749 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4750 | ch = i_getch(input); |
| 4751 | if (ch != '`' |
| 4752 | && ch != '$' |
| 4753 | && ch != '\\' |
| 4754 | && (!in_dquote || ch != '"') |
| 4755 | ) { |
| 4756 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4757 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4758 | } |
| 4759 | if (ch == EOF) { |
| 4760 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4761 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4762 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4763 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4767 | * quoting and nested ()s. |
| 4768 | * "With the $(command) style of command substitution, all characters |
| 4769 | * following the open parenthesis to the matching closing parenthesis |
| 4770 | * constitute the command. Any valid shell script can be used for command, |
| 4771 | * except a script consisting solely of redirections which produces |
| 4772 | * unspecified results." |
| 4773 | * Example Output |
| 4774 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4775 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4776 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4777 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4778 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4779 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4780 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4781 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4782 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4783 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4784 | 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] | 4785 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4786 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4787 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4788 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4789 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4790 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4791 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4792 | |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4793 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4794 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4795 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4796 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4797 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4798 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4799 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4800 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4801 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4802 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4803 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4804 | if (ch == end_ch |
| 4805 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4806 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4807 | # endif |
| 4808 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4809 | if (!dbl) |
| 4810 | break; |
| 4811 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4812 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4813 | i_getch(input); /* eat second ')' */ |
| 4814 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4815 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4816 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4817 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4818 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4819 | if (ch == '(' || ch == '{') { |
| 4820 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4821 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4822 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4823 | o_addchr(dest, ch); |
| 4824 | continue; |
| 4825 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4826 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4827 | if (!add_till_single_quote(dest, input)) |
| 4828 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4829 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4830 | continue; |
| 4831 | } |
| 4832 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4833 | if (!add_till_double_quote(dest, input)) |
| 4834 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4835 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4836 | continue; |
| 4837 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4838 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4839 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4840 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4841 | o_addchr(dest, ch); |
| 4842 | continue; |
| 4843 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4844 | if (ch == '\\') { |
| 4845 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4846 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4847 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4848 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4849 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4850 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4851 | #if 0 |
| 4852 | if (ch == '\n') { |
| 4853 | /* "backslash+newline", ignore both */ |
| 4854 | o_delchr(dest); /* undo insertion of '\' */ |
| 4855 | continue; |
| 4856 | } |
| 4857 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4858 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4859 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4860 | continue; |
| 4861 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4862 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4863 | 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] | 4864 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4865 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4866 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4867 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4868 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4869 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4870 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4871 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4872 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4873 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4874 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4875 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4876 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4877 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4878 | 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] | 4879 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4880 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4881 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4882 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4883 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4884 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4885 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4886 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4887 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4888 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4889 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4890 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4891 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4892 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4893 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4894 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4895 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4896 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4897 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4898 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4899 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4900 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4901 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4902 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4903 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4904 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4905 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4906 | o_addchr(dest, ch | quote_mask); |
| 4907 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4908 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4909 | case '$': /* pid */ |
| 4910 | case '!': /* last bg pid */ |
| 4911 | case '?': /* last exit code */ |
| 4912 | case '#': /* number of args */ |
| 4913 | case '*': /* args */ |
| 4914 | case '@': /* args */ |
| 4915 | goto make_one_char_var; |
| 4916 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4917 | char len_single_ch; |
| 4918 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4919 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4920 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4921 | ch = i_getch(input); /* eat '{' */ |
| 4922 | nommu_addchr(as_string, ch); |
| 4923 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4924 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4925 | /* It should be ${?}, or ${#var}, |
| 4926 | * or even ${?+subst} - operator acting on a special variable, |
| 4927 | * or the beginning of variable name. |
| 4928 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4929 | if (ch == EOF |
| 4930 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4931 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4932 | bad_dollar_syntax: |
| 4933 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4934 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4935 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4936 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4937 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4938 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4939 | ch |= quote_mask; |
| 4940 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4941 | /* 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] | 4942 | * However, this regresses some of our testsuite cases |
| 4943 | * which check invalid constructs like ${%}. |
| 4944 | * Oh well... let's check that the var name part is fine... */ |
| 4945 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4946 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4947 | unsigned pos; |
| 4948 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4949 | o_addchr(dest, ch); |
| 4950 | debug_printf_parse(": '%c'\n", ch); |
| 4951 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4952 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4953 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4954 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4955 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4956 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4957 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4958 | unsigned end_ch; |
| 4959 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4960 | /* handle parameter expansions |
| 4961 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4962 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4963 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4964 | if (len_single_ch != '#' |
| 4965 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4966 | || i_peek(input) != '}' |
| 4967 | ) { |
| 4968 | goto bad_dollar_syntax; |
| 4969 | } |
| 4970 | /* else: it's "length of C" ${#C} op, |
| 4971 | * where C is a single char |
| 4972 | * special var name, e.g. ${#!}. |
| 4973 | */ |
| 4974 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4975 | /* Eat everything until closing '}' (or ':') */ |
| 4976 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4977 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4978 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4979 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4980 | ) { |
| 4981 | /* It's ${var:N[:M]} thing */ |
| 4982 | end_ch = '}' * 0x100 + ':'; |
| 4983 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4984 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4985 | && ch == '/' |
| 4986 | ) { |
| 4987 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4988 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4989 | i_getch(input); |
| 4990 | nommu_addchr(as_string, '/'); |
| 4991 | ch = '\\'; |
| 4992 | } |
| 4993 | end_ch = '}' * 0x100 + '/'; |
| 4994 | } |
| 4995 | o_addchr(dest, ch); |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 4996 | /* The pattern can't be empty. |
| 4997 | * IOW: if the first char after "${v//" is a slash, |
| 4998 | * it does not terminate the pattern - it's the first char of the pattern: |
| 4999 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 5000 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 5001 | */ |
| 5002 | if (i_peek(input) == '/') { |
| 5003 | o_addchr(dest, i_getch(input)); |
| 5004 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5005 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5006 | if (!BB_MMU) |
| 5007 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5008 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5009 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5010 | if (last_ch == 0) /* error? */ |
| 5011 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5012 | #else |
| 5013 | #error Simple code to only allow ${var} is not implemented |
| 5014 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5015 | if (as_string) { |
| 5016 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5017 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5018 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5019 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5020 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 5021 | && (end_ch & 0xff00) |
| 5022 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5023 | /* close the first block: */ |
| 5024 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5025 | /* while parsing N from ${var:N[:M]} |
| 5026 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5027 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5028 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5029 | end_ch = '}'; |
| 5030 | goto again; |
| 5031 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5032 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5033 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5034 | /* it's ${var:N} - emulate :999999999 */ |
| 5035 | o_addstr(dest, "999999999"); |
| 5036 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5037 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5038 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5039 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5040 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5041 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5042 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5043 | break; |
| 5044 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5045 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5046 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5047 | unsigned pos; |
| 5048 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5049 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5050 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5051 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 5052 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5053 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5054 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5055 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5056 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5057 | if (!BB_MMU) |
| 5058 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5059 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 5060 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5061 | if (as_string) { |
| 5062 | o_addstr(as_string, dest->data + pos); |
| 5063 | o_addchr(as_string, ')'); |
| 5064 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5065 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5066 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5067 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5068 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5069 | # endif |
| 5070 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5071 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5072 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5073 | if (!BB_MMU) |
| 5074 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5075 | if (!add_till_closing_bracket(dest, input, ')')) |
| 5076 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5077 | if (as_string) { |
| 5078 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 5079 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5080 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5081 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5082 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5083 | break; |
| 5084 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5085 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5086 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5087 | goto make_var; |
| 5088 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 5089 | /* TODO: $_ and $-: */ |
| 5090 | /* $_ Shell or shell script name; or last argument of last command |
| 5091 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 5092 | * 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] | 5093 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5094 | ch = i_getch(input); |
| 5095 | nommu_addchr(as_string, ch); |
| 5096 | ch = i_peek_and_eat_bkslash_nl(input); |
| 5097 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5098 | ch = '_'; |
| 5099 | goto make_var1; |
| 5100 | } |
| 5101 | /* else: it's $_ */ |
| 5102 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5103 | default: |
| 5104 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5105 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5106 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 5107 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5108 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5111 | #if BB_MMU |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5112 | #define encode_string(as_string, dest, input, dquote_end) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5113 | encode_string(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5114 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5115 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5116 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5117 | o_string *dest, |
| 5118 | struct in_str *input, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5119 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5120 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5121 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5122 | int next; |
| 5123 | |
| 5124 | again: |
| 5125 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5126 | if (ch != EOF) |
| 5127 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5128 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5129 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 5130 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5131 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5132 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5133 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5134 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5135 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5136 | } |
| 5137 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5138 | if (ch != '\n') { |
| 5139 | next = i_peek(input); |
| 5140 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5141 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5142 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5143 | if (ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5144 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 5145 | /* Testcase: in interactive shell a file with |
| 5146 | * echo "unterminated string\<eof> |
| 5147 | * is sourced. |
| 5148 | */ |
| 5149 | syntax_error_unterm_ch('"'); |
| 5150 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5151 | } |
| 5152 | /* bash: |
| 5153 | * "The backslash retains its special meaning [in "..."] |
| 5154 | * only when followed by one of the following characters: |
| 5155 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5156 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5157 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 5158 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5159 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5160 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 5161 | ch = i_getch(input); /* eat next */ |
| 5162 | if (ch == '\n') |
| 5163 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5164 | } /* else: ch remains == '\\', and we double it below: */ |
| 5165 | 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] | 5166 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5167 | goto again; |
| 5168 | } |
| 5169 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5170 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 5171 | debug_printf_parse("encode_string return 0: " |
| 5172 | "parse_dollar returned 0 (error)\n"); |
| 5173 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5174 | } |
| 5175 | goto again; |
| 5176 | } |
| 5177 | #if ENABLE_HUSH_TICK |
| 5178 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5179 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5180 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5181 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5182 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 5183 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5184 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5185 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5186 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5187 | } |
| 5188 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5189 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5190 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5191 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5194 | /* |
| 5195 | * Scan input until EOF or end_trigger char. |
| 5196 | * Return a list of pipes to execute, or NULL on EOF |
| 5197 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5198 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5199 | * reset parsing machinery and start parsing anew, |
| 5200 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5201 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5202 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5203 | int *heredoc_cnt_ptr, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5204 | struct in_str *input, |
| 5205 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5206 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5207 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5208 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5209 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5210 | /* 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] | 5211 | * 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] | 5212 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5213 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5214 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5215 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5216 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5217 | initialize_context(&ctx); |
| 5218 | |
| 5219 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5220 | * Preventing this: |
| 5221 | */ |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5222 | ctx.word.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5223 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5224 | /* We used to separate words on $IFS here. This was wrong. |
| 5225 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5226 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5227 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5228 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5229 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5230 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5231 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5232 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5233 | int ch; |
| 5234 | int next; |
| 5235 | int redir_fd; |
| 5236 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5237 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5238 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5239 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5240 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5241 | if (ch == EOF) { |
| 5242 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5243 | |
| 5244 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5245 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5246 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5247 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5248 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5249 | syntax_error_unterm_ch('('); |
| 5250 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5251 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5252 | if (end_trigger == '}') { |
| 5253 | syntax_error_unterm_ch('{'); |
| 5254 | goto parse_error; |
| 5255 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5256 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5257 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5258 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5259 | } |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5260 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5261 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5262 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5263 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5264 | /* (this makes bare "&" cmd a no-op. |
| 5265 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5266 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5267 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5268 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5269 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5270 | pi = NULL; |
| 5271 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5272 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5273 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5274 | if (pstring) |
| 5275 | *pstring = ctx.as_string.data; |
| 5276 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5277 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5278 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5279 | // heredoc_cnt must be 0 here anyway |
| 5280 | //if (heredoc_cnt_ptr) |
| 5281 | // *heredoc_cnt_ptr = heredoc_cnt; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5282 | debug_leave(); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5283 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5284 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5285 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5286 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5287 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5288 | /* Handle "'" and "\" first, as they won't play nice with |
| 5289 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5290 | * echo z\\ |
| 5291 | * and |
| 5292 | * echo '\ |
| 5293 | * ' |
| 5294 | * would break. |
| 5295 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5296 | if (ch == '\\') { |
| 5297 | ch = i_getch(input); |
| 5298 | if (ch == '\n') |
| 5299 | continue; /* drop \<newline>, get next char */ |
| 5300 | nommu_addchr(&ctx.as_string, '\\'); |
| 5301 | o_addchr(&ctx.word, '\\'); |
| 5302 | if (ch == EOF) { |
| 5303 | /* Testcase: eval 'echo Ok\' */ |
| 5304 | /* bash-4.3.43 was removing backslash, |
| 5305 | * but 4.4.19 retains it, most other shells too |
| 5306 | */ |
| 5307 | continue; /* get next char */ |
| 5308 | } |
| 5309 | /* Example: echo Hello \2>file |
| 5310 | * we need to know that word 2 is quoted |
| 5311 | */ |
| 5312 | ctx.word.has_quoted_part = 1; |
| 5313 | nommu_addchr(&ctx.as_string, ch); |
| 5314 | o_addchr(&ctx.word, ch); |
| 5315 | continue; /* get next char */ |
| 5316 | } |
| 5317 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5318 | if (ch == '\'') { |
| 5319 | ctx.word.has_quoted_part = 1; |
| 5320 | next = i_getch(input); |
| 5321 | if (next == '\'' && !ctx.pending_redirect) |
| 5322 | goto insert_empty_quoted_str_marker; |
| 5323 | |
| 5324 | ch = next; |
| 5325 | while (1) { |
| 5326 | if (ch == EOF) { |
| 5327 | syntax_error_unterm_ch('\''); |
| 5328 | goto parse_error; |
| 5329 | } |
| 5330 | nommu_addchr(&ctx.as_string, ch); |
| 5331 | if (ch == '\'') |
| 5332 | break; |
| 5333 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5334 | /* Convert raw ^C to corresponding special variable reference */ |
| 5335 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5336 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5337 | } |
| 5338 | o_addqchr(&ctx.word, ch); |
| 5339 | ch = i_getch(input); |
| 5340 | } |
| 5341 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5342 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5343 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5344 | next = '\0'; |
| 5345 | if (ch != '\n') |
| 5346 | next = i_peek_and_eat_bkslash_nl(input); |
| 5347 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5348 | is_special = "{}<>;&|()#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5349 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5350 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5351 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5352 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5353 | || ctx.word.length /* word{... - non-special */ |
| 5354 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5355 | || (next != ';' /* }; - special */ |
| 5356 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5357 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5358 | && next != '&' /* }& and }&& ... - special */ |
| 5359 | && next != '|' /* }|| ... - special */ |
| 5360 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5361 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5362 | ) { |
| 5363 | /* They are not special, skip "{}" */ |
| 5364 | is_special += 2; |
| 5365 | } |
| 5366 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5367 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5368 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5369 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5370 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5371 | o_addQchr(&ctx.word, ch); |
| 5372 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5373 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5374 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5375 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5376 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5377 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5378 | 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] | 5379 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5380 | continue; |
| 5381 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5382 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5383 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5384 | #if ENABLE_HUSH_LINENO_VAR |
| 5385 | /* Case: |
| 5386 | * "while ...; do<whitespace><newline> |
| 5387 | * cmd ..." |
| 5388 | * would think that "cmd" starts in <whitespace> - |
| 5389 | * i.e., at the previous line. |
| 5390 | * We need to skip all whitespace before newlines. |
| 5391 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5392 | while (ch != '\n') { |
| 5393 | next = i_peek(input); |
| 5394 | if (next != ' ' && next != '\t' && next != '\n') |
| 5395 | break; /* next char is not ws */ |
| 5396 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5397 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5398 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5399 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5400 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5401 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5402 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5403 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5404 | /* Is this a case when newline is simply ignored? |
| 5405 | * Some examples: |
| 5406 | * "cmd | <newline> cmd ..." |
| 5407 | * "case ... in <newline> word) ..." |
| 5408 | */ |
| 5409 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5410 | && ctx.word.length == 0 |
| 5411 | && !ctx.word.has_quoted_part |
| 5412 | && heredoc_cnt == 0 |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5413 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5414 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5415 | * Without check #1, interactive shell |
| 5416 | * ignores even bare <newline>, |
| 5417 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5418 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5419 | * ps2> _ <=== wrong, should be ps1 |
| 5420 | * Without check #2, "cmd & <newline>" |
| 5421 | * is similarly mistreated. |
| 5422 | * (BTW, this makes "cmd & cmd" |
| 5423 | * and "cmd && cmd" non-orthogonal. |
| 5424 | * Really, ask yourself, why |
| 5425 | * "cmd && <newline>" doesn't start |
| 5426 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5427 | * The only reason is that it might be |
| 5428 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5429 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5430 | */ |
| 5431 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5432 | if (pi->num_cmds != 0 /* check #1 */ |
| 5433 | && pi->followup != PIPE_BG /* check #2 */ |
| 5434 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5435 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5436 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5437 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5438 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5439 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5440 | debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5441 | if (heredoc_cnt) { |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5442 | heredoc_cnt = fetch_heredocs(&ctx.as_string, ctx.list_head, heredoc_cnt, input); |
| 5443 | if (heredoc_cnt != 0) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5444 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5445 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5446 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5447 | 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] | 5448 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5449 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5450 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5451 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5452 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5453 | |
| 5454 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5455 | * } is an ordinary char in this case, even inside { cmd; } |
| 5456 | * Pathological example: { ""}; } should exec "}" cmd |
| 5457 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5458 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5459 | if (ctx.word.length != 0 /* word} */ |
| 5460 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5461 | ) { |
| 5462 | goto ordinary_char; |
| 5463 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5464 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5465 | /* Generally, there should be semicolon: "cmd; }" |
| 5466 | * However, bash allows to omit it if "cmd" is |
| 5467 | * a group. Examples: |
| 5468 | * { { echo 1; } } |
| 5469 | * {(echo 1)} |
| 5470 | * { echo 0 >&2 | { echo 1; } } |
| 5471 | * { while false; do :; done } |
| 5472 | * { case a in b) ;; esac } |
| 5473 | */ |
| 5474 | if (ctx.command->group) |
| 5475 | goto term_group; |
| 5476 | goto ordinary_char; |
| 5477 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5478 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5479 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5480 | goto skip_end_trigger; |
| 5481 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5482 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5483 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5484 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5485 | && (ch != ';' || heredoc_cnt == 0) |
| 5486 | #if ENABLE_HUSH_CASE |
| 5487 | && (ch != ')' |
| 5488 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5489 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5490 | ) |
| 5491 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5492 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5493 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5494 | goto parse_error; |
| 5495 | } |
| 5496 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5497 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5498 | 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] | 5499 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5500 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5501 | 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] | 5502 | ) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5503 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5504 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5505 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5506 | if (pstring) |
| 5507 | *pstring = ctx.as_string.data; |
| 5508 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5509 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5510 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5511 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5512 | /* Example: bare "{ }", "()" */ |
| 5513 | G.last_exitcode = 2; /* bash compat */ |
| 5514 | syntax_error_unexpected_ch(ch); |
| 5515 | goto parse_error2; |
| 5516 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5517 | if (heredoc_cnt_ptr) |
| 5518 | *heredoc_cnt_ptr = heredoc_cnt; |
| 5519 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5520 | debug_printf_parse("parse_stream return %p: " |
| 5521 | "end_trigger char found\n", |
| 5522 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5523 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5524 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5525 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5526 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5527 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5528 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5529 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5530 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5531 | /* Catch <, > before deciding whether this word is |
| 5532 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5533 | switch (ch) { |
| 5534 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5535 | redir_fd = redirect_opt_num(&ctx.word); |
| 5536 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5537 | goto parse_error; |
| 5538 | } |
| 5539 | redir_style = REDIRECT_OVERWRITE; |
| 5540 | if (next == '>') { |
| 5541 | redir_style = REDIRECT_APPEND; |
| 5542 | ch = i_getch(input); |
| 5543 | nommu_addchr(&ctx.as_string, ch); |
| 5544 | } |
| 5545 | #if 0 |
| 5546 | else if (next == '(') { |
| 5547 | syntax_error(">(process) not supported"); |
| 5548 | goto parse_error; |
| 5549 | } |
| 5550 | #endif |
| 5551 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5552 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5553 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5554 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5555 | redir_fd = redirect_opt_num(&ctx.word); |
| 5556 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5557 | goto parse_error; |
| 5558 | } |
| 5559 | redir_style = REDIRECT_INPUT; |
| 5560 | if (next == '<') { |
| 5561 | redir_style = REDIRECT_HEREDOC; |
| 5562 | heredoc_cnt++; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5563 | debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5564 | ch = i_getch(input); |
| 5565 | nommu_addchr(&ctx.as_string, ch); |
| 5566 | } else if (next == '>') { |
| 5567 | redir_style = REDIRECT_IO; |
| 5568 | ch = i_getch(input); |
| 5569 | nommu_addchr(&ctx.as_string, ch); |
| 5570 | } |
| 5571 | #if 0 |
| 5572 | else if (next == '(') { |
| 5573 | syntax_error("<(process) not supported"); |
| 5574 | goto parse_error; |
| 5575 | } |
| 5576 | #endif |
| 5577 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5578 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5579 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5580 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5581 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5582 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5583 | /* note: we do not add it to &ctx.as_string */ |
| 5584 | /* TODO: in bash: |
| 5585 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5586 | * cmd "$(cmd2 #comment)" - syntax error |
| 5587 | * cmd "`cmd2 #comment`" - ok |
| 5588 | * We accept both (comment ends where command subst ends, in both cases). |
| 5589 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5590 | while (1) { |
| 5591 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5592 | if (ch == '\n') { |
| 5593 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5594 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5595 | } |
| 5596 | ch = i_getch(input); |
| 5597 | if (ch == EOF) |
| 5598 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5599 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5600 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5601 | } |
| 5602 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5603 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5604 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5605 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5606 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5607 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5608 | && !ctx.pending_redirect |
| 5609 | ) { |
| 5610 | /* ch is a special char and thus this word |
| 5611 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5612 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5613 | 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] | 5614 | } |
| 5615 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5616 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5617 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5618 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5619 | case SPECIAL_VAR_SYMBOL: |
| 5620 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5621 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5622 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5623 | /* fall through */ |
| 5624 | case '#': |
| 5625 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5626 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5627 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5628 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5629 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5630 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5631 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5632 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5633 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5634 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5635 | case '"': |
| 5636 | ctx.word.has_quoted_part = 1; |
| 5637 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5638 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5639 | insert_empty_quoted_str_marker: |
| 5640 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5641 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5642 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5643 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5644 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5645 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5646 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5647 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"')) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5648 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5649 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5650 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5651 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5652 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5653 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5654 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5655 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5656 | o_addchr(&ctx.word, '`'); |
| 5657 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5658 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5659 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5660 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5661 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5662 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5663 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5664 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5665 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5666 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5667 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5668 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5669 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5670 | #if ENABLE_HUSH_CASE |
| 5671 | case_semi: |
| 5672 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5673 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5674 | goto parse_error; |
| 5675 | } |
| 5676 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5677 | #if ENABLE_HUSH_CASE |
| 5678 | /* Eat multiple semicolons, detect |
| 5679 | * whether it means something special */ |
| 5680 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5681 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5682 | if (ch != ';') |
| 5683 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5684 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5685 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5686 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5687 | ctx.ctx_dsemicolon = 1; |
| 5688 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5689 | break; |
| 5690 | } |
| 5691 | } |
| 5692 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5693 | new_cmd: |
| 5694 | /* We just finished a cmd. New one may start |
| 5695 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5696 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5697 | 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] | 5698 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5699 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5700 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5701 | goto parse_error; |
| 5702 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5703 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5704 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5705 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5706 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5707 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5708 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5709 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5710 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5711 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5712 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5713 | goto parse_error; |
| 5714 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5715 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5716 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5717 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5718 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5719 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5720 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5721 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5722 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5723 | } else { |
| 5724 | /* we could pick up a file descriptor choice here |
| 5725 | * with redirect_opt_num(), but bash doesn't do it. |
| 5726 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5727 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5728 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5729 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5730 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5731 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5732 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5733 | if (ctx.ctx_res_w == RES_MATCH |
| 5734 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5735 | && ctx.word.length == 0 /* not word(... */ |
| 5736 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5737 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5738 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5739 | } |
| 5740 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5741 | /* fall through */ |
| 5742 | case '{': { |
| 5743 | int n = parse_group(&ctx, input, ch); |
| 5744 | if (n < 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5745 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5746 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5747 | debug_printf_heredoc("parse_group done, needs heredocs:%d\n", n); |
| 5748 | heredoc_cnt += n; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5749 | goto new_cmd; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5750 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5751 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5752 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5753 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5754 | goto case_semi; |
| 5755 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5756 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5757 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5758 | /* proper use of this character is caught by end_trigger: |
| 5759 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5760 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5761 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5762 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5763 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5764 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5765 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5766 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5767 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5768 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5769 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5770 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5771 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5772 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5773 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5774 | struct parse_context *pctx; |
| 5775 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5776 | |
| 5777 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5778 | * Sample for finding leaks on syntax error recovery path. |
| 5779 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5780 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5781 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5782 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5783 | * 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] | 5784 | */ |
| 5785 | pctx = &ctx; |
| 5786 | do { |
| 5787 | /* Update pipe/command counts, |
| 5788 | * otherwise freeing may miss some */ |
| 5789 | done_pipe(pctx, PIPE_SEQ); |
| 5790 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5791 | pctx->list_head, pctx); |
| 5792 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5793 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5794 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5795 | #if !BB_MMU |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5796 | o_free(&pctx->as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5797 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5798 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5799 | if (pctx != &ctx) { |
| 5800 | free(pctx); |
| 5801 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5802 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5803 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5804 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5805 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5806 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5807 | if (pstring) |
| 5808 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5809 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5810 | debug_leave(); |
| 5811 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5812 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5815 | |
| 5816 | /*** Execution routines ***/ |
| 5817 | |
| 5818 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5819 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5820 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5821 | expand_string_to_string(str) |
| 5822 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5823 | static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5824 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5825 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5826 | #endif |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5827 | static int expand_vars_to_list(o_string *output, int n, char *arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5828 | |
| 5829 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5830 | * all variable references within and returns a pointer to |
| 5831 | * a list of expanded strings, possibly with larger number |
| 5832 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5833 | * This new list is allocated as a single malloc block. |
| 5834 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5835 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5836 | * Caller can deallocate entire list by single free(list). */ |
| 5837 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5838 | /* A horde of its helpers come first: */ |
| 5839 | |
| 5840 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5841 | { |
| 5842 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5843 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5844 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5845 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5846 | if (c == '{' || c == '}') { |
| 5847 | /* { -> \{, } -> \} */ |
| 5848 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5849 | /* And now we want to add { or } and continue: |
| 5850 | * o_addchr(o, c); |
| 5851 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5852 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5853 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5854 | } |
| 5855 | #endif |
| 5856 | o_addchr(o, c); |
| 5857 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5858 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5859 | o_addchr(o, '\\'); |
| 5860 | if (len) { |
| 5861 | len--; |
| 5862 | o_addchr(o, '\\'); |
| 5863 | o_addchr(o, *str++); |
| 5864 | } |
| 5865 | } |
| 5866 | } |
| 5867 | } |
| 5868 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5869 | /* Store given string, finalizing the word and starting new one whenever |
| 5870 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5871 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5872 | * Return in output->ended_in_ifs: |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5873 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5874 | */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5875 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5876 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5877 | int last_is_ifs = 0; |
| 5878 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5879 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5880 | int word_len; |
| 5881 | |
| 5882 | if (!*str) /* EOL - do not finalize word */ |
| 5883 | break; |
| 5884 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5885 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5886 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5887 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5888 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5889 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5890 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5891 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5892 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5893 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5894 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5895 | /*/ Why can't we do it easier? */ |
| 5896 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5897 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5898 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5899 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5900 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5901 | if (!*str) /* EOL - do not finalize word */ |
| 5902 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5903 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5904 | |
| 5905 | /* We know str here points to at least one IFS char */ |
| 5906 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5907 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5908 | if (!*str) /* EOL - do not finalize word */ |
| 5909 | break; |
| 5910 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5911 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 5912 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 5913 | ) { |
| 5914 | /* This is a non-whitespace $IFS char */ |
| 5915 | /* Skip it and IFS whitespace chars, start new word */ |
| 5916 | str++; |
| 5917 | str += strspn(str, G.ifs_whitespace); |
| 5918 | goto new_word; |
| 5919 | } |
| 5920 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5921 | /* Start new word... but not always! */ |
| 5922 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5923 | if (output->has_quoted_part |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5924 | /* |
| 5925 | * Case "v=' a'; echo $v": |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5926 | * here nothing precedes the space in $v expansion, |
| 5927 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5928 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5929 | * It's okay if this accesses the byte before first argv[]: |
| 5930 | * past call to o_save_ptr() cleared it to zero byte |
| 5931 | * (grep for -prev-ifs-check-). |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5932 | */ |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5933 | || output->data[output->length - 1] |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5934 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5935 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5936 | o_addchr(output, '\0'); |
| 5937 | debug_print_list("expand_on_ifs", output, n); |
| 5938 | n = o_save_ptr(output, n); |
| 5939 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5940 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5941 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5942 | output->ended_in_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5943 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5944 | return n; |
| 5945 | } |
| 5946 | |
| 5947 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5948 | * they are in double quotes, with the exception that they are not :). |
| 5949 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5950 | * |
| 5951 | * Returns malloced string. |
| 5952 | * As an optimization, we return NULL if expansion is not needed. |
| 5953 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5954 | static char *encode_then_expand_string(const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5955 | { |
| 5956 | char *exp_str; |
| 5957 | struct in_str input; |
| 5958 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5959 | const char *cp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5960 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5961 | cp = str; |
| 5962 | for (;;) { |
| 5963 | if (!*cp) return NULL; /* string has no special chars */ |
| 5964 | if (*cp == '$') break; |
| 5965 | if (*cp == '\\') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5966 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5967 | if (*cp == '`') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5968 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5969 | cp++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | /* We need to expand. Example: |
| 5973 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5974 | */ |
| 5975 | setup_string_in_str(&input, str); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5976 | encode_string(NULL, &dest, &input, EOF); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5977 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5978 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5979 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5980 | EXP_FLAG_ESC_GLOB_CHARS, |
| 5981 | /*unbackslash:*/ 1 |
| 5982 | ); |
| 5983 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5984 | o_free(&dest); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5985 | return exp_str; |
| 5986 | } |
| 5987 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 5988 | static const char *first_special_char_in_vararg(const char *cp) |
| 5989 | { |
| 5990 | for (;;) { |
| 5991 | if (!*cp) return NULL; /* string has no special chars */ |
| 5992 | if (*cp == '$') return cp; |
| 5993 | if (*cp == '\\') return cp; |
| 5994 | if (*cp == '\'') return cp; |
| 5995 | if (*cp == '"') return cp; |
| 5996 | #if ENABLE_HUSH_TICK |
| 5997 | if (*cp == '`') return cp; |
| 5998 | #endif |
| 5999 | /* dquoted "${x:+ARG}" should not glob, therefore |
| 6000 | * '*' et al require some non-literal processing: */ |
| 6001 | if (*cp == '*') return cp; |
| 6002 | if (*cp == '?') return cp; |
| 6003 | if (*cp == '[') return cp; |
| 6004 | cp++; |
| 6005 | } |
| 6006 | } |
| 6007 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6008 | /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}. |
| 6009 | * These can contain single- and double-quoted strings, |
| 6010 | * and treated as if the ARG string is initially unquoted. IOW: |
| 6011 | * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be |
| 6012 | * a dquoted string: "${var#"zz"}"), the difference only comes later |
| 6013 | * (word splitting and globbing of the ${var...} result). |
| 6014 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6015 | #if !BASH_PATTERN_SUBST |
| 6016 | #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ |
| 6017 | encode_then_expand_vararg(str, handle_squotes) |
| 6018 | #endif |
| 6019 | static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash) |
| 6020 | { |
Denys Vlasenko | 3d27d43 | 2018-12-27 18:03:20 +0100 | [diff] [blame] | 6021 | #if !BASH_PATTERN_SUBST && ENABLE_HUSH_CASE |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6022 | const int do_unbackslash = 0; |
| 6023 | #endif |
| 6024 | char *exp_str; |
| 6025 | struct in_str input; |
| 6026 | o_string dest = NULL_O_STRING; |
| 6027 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6028 | if (!first_special_char_in_vararg(str)) { |
| 6029 | /* string has no special chars */ |
| 6030 | return NULL; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6031 | } |
| 6032 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6033 | setup_string_in_str(&input, str); |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 6034 | dest.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6035 | exp_str = NULL; |
| 6036 | |
| 6037 | for (;;) { |
| 6038 | int ch; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6039 | |
| 6040 | ch = i_getch(&input); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6041 | debug_printf_parse("%s: ch=%c (%d) escape=%d\n", |
| 6042 | __func__, ch, ch, !!dest.o_expflags); |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6043 | |
| 6044 | if (!dest.o_expflags) { |
| 6045 | if (ch == EOF) |
| 6046 | break; |
| 6047 | if (handle_squotes && ch == '\'') { |
| 6048 | if (!add_till_single_quote_dquoted(&dest, &input)) |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6049 | goto ret; /* error */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6050 | continue; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6051 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6052 | } |
| 6053 | if (ch == EOF) { |
| 6054 | syntax_error_unterm_ch('"'); |
| 6055 | goto ret; /* error */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6056 | } |
| 6057 | if (ch == '"') { |
| 6058 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
| 6059 | continue; |
| 6060 | } |
| 6061 | if (ch == '\\') { |
| 6062 | ch = i_getch(&input); |
| 6063 | if (ch == EOF) { |
| 6064 | //example? error message? syntax_error_unterm_ch('"'); |
| 6065 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6066 | goto ret; |
| 6067 | } |
| 6068 | o_addqchr(&dest, ch); |
| 6069 | continue; |
| 6070 | } |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6071 | if (ch == '$') { |
| 6072 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) { |
| 6073 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6074 | goto ret; |
| 6075 | } |
| 6076 | continue; |
| 6077 | } |
| 6078 | #if ENABLE_HUSH_TICK |
| 6079 | if (ch == '`') { |
| 6080 | //unsigned pos = dest->length; |
| 6081 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6082 | o_addchr(&dest, 0x80 | '`'); |
| 6083 | if (!add_till_backquote(&dest, &input, |
| 6084 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6085 | ) |
| 6086 | ) { |
| 6087 | goto ret; /* error */ |
| 6088 | } |
| 6089 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6090 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6091 | continue; |
| 6092 | } |
| 6093 | #endif |
| 6094 | o_addQchr(&dest, ch); |
| 6095 | } /* for (;;) */ |
| 6096 | |
| 6097 | debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data); |
| 6098 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6099 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 6100 | do_unbackslash |
| 6101 | ); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6102 | ret: |
| 6103 | debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6104 | o_free(&dest); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6105 | return exp_str; |
| 6106 | } |
| 6107 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6108 | /* Expanding ARG in ${var+ARG}, ${var-ARG} |
| 6109 | */ |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6110 | static int encode_then_append_var_plusminus(o_string *output, int n, |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6111 | char *str, int dquoted) |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6112 | { |
| 6113 | struct in_str input; |
| 6114 | o_string dest = NULL_O_STRING; |
| 6115 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6116 | if (!first_special_char_in_vararg(str) |
| 6117 | && '\0' == str[strcspn(str, G.ifs)] |
| 6118 | ) { |
| 6119 | /* string has no special chars |
| 6120 | * && string has no $IFS chars |
| 6121 | */ |
| 6122 | return expand_vars_to_list(output, n, str); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6123 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6124 | |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6125 | setup_string_in_str(&input, str); |
| 6126 | |
| 6127 | for (;;) { |
| 6128 | int ch; |
| 6129 | |
| 6130 | ch = i_getch(&input); |
| 6131 | debug_printf_parse("%s: ch=%c (%d) escape=%x\n", |
| 6132 | __func__, ch, ch, dest.o_expflags); |
| 6133 | |
| 6134 | if (!dest.o_expflags) { |
| 6135 | if (ch == EOF) |
| 6136 | break; |
| 6137 | if (!dquoted && strchr(G.ifs, ch)) { |
| 6138 | /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word. |
| 6139 | * do not assume we are at the start of the word (PREFIX above). |
| 6140 | */ |
| 6141 | if (dest.data) { |
| 6142 | n = expand_vars_to_list(output, n, dest.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6143 | o_free_and_set_NULL(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6144 | o_addchr(output, '\0'); |
| 6145 | n = o_save_ptr(output, n); /* create next word */ |
| 6146 | } else |
| 6147 | if (output->length != o_get_last_ptr(output, n) |
| 6148 | || output->has_quoted_part |
| 6149 | ) { |
| 6150 | /* For these cases: |
| 6151 | * f() { for i; do echo "|$i|"; done; }; x=x |
| 6152 | * f a${x:+ }b # 1st condition |
| 6153 | * |a| |
| 6154 | * |b| |
| 6155 | * f ""${x:+ }b # 2nd condition |
| 6156 | * || |
| 6157 | * |b| |
| 6158 | */ |
| 6159 | o_addchr(output, '\0'); |
| 6160 | n = o_save_ptr(output, n); /* create next word */ |
| 6161 | } |
| 6162 | continue; |
| 6163 | } |
| 6164 | if (!dquoted && ch == '\'') { |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6165 | if (!add_till_single_quote_dquoted(&dest, &input)) |
| 6166 | goto ret; /* error */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6167 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6168 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6169 | continue; |
| 6170 | } |
| 6171 | } |
| 6172 | if (ch == EOF) { |
| 6173 | syntax_error_unterm_ch('"'); |
| 6174 | goto ret; /* error */ |
| 6175 | } |
| 6176 | if (ch == '"') { |
| 6177 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6178 | if (dest.o_expflags) { |
| 6179 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6180 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6181 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6182 | continue; |
| 6183 | } |
| 6184 | if (ch == '\\') { |
| 6185 | ch = i_getch(&input); |
| 6186 | if (ch == EOF) { |
| 6187 | //example? error message? syntax_error_unterm_ch('"'); |
| 6188 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6189 | goto ret; |
| 6190 | } |
| 6191 | o_addqchr(&dest, ch); |
| 6192 | continue; |
| 6193 | } |
| 6194 | if (ch == '$') { |
| 6195 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) { |
| 6196 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6197 | goto ret; |
| 6198 | } |
| 6199 | continue; |
| 6200 | } |
| 6201 | #if ENABLE_HUSH_TICK |
| 6202 | if (ch == '`') { |
| 6203 | //unsigned pos = dest->length; |
| 6204 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6205 | o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`'); |
| 6206 | if (!add_till_backquote(&dest, &input, |
| 6207 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6208 | ) |
| 6209 | ) { |
| 6210 | goto ret; /* error */ |
| 6211 | } |
| 6212 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6213 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6214 | continue; |
| 6215 | } |
| 6216 | #endif |
Denys Vlasenko | f36caa4 | 2018-07-20 19:29:41 +0200 | [diff] [blame] | 6217 | if (dquoted) { |
| 6218 | /* Always glob-protect if in dquotes: |
| 6219 | * x=x; echo "${x:+/bin/c*}" - prints: /bin/c* |
| 6220 | * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c* |
| 6221 | */ |
| 6222 | o_addqchr(&dest, ch); |
| 6223 | } else { |
| 6224 | /* Glob-protect only if char is quoted: |
| 6225 | * x=x; echo ${x:+/bin/c*} - prints many filenames |
| 6226 | * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c* |
| 6227 | */ |
| 6228 | o_addQchr(&dest, ch); |
| 6229 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6230 | } /* for (;;) */ |
| 6231 | |
| 6232 | if (dest.data) { |
| 6233 | n = expand_vars_to_list(output, n, dest.data); |
| 6234 | } |
| 6235 | ret: |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6236 | o_free(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6237 | return n; |
| 6238 | } |
| 6239 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6240 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6241 | 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] | 6242 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6243 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6244 | arith_t res; |
| 6245 | char *exp_str; |
| 6246 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6247 | math_state.lookupvar = get_local_var_value; |
| 6248 | math_state.setvar = set_local_var_from_halves; |
| 6249 | //math_state.endofname = endofname; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6250 | exp_str = encode_then_expand_string(arg); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6251 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6252 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6253 | if (errmsg_p) |
| 6254 | *errmsg_p = math_state.errmsg; |
| 6255 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6256 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6257 | return res; |
| 6258 | } |
| 6259 | #endif |
| 6260 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6261 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6262 | /* ${var/[/]pattern[/repl]} helpers */ |
| 6263 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 6264 | { |
| 6265 | while (1) { |
| 6266 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 6267 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 6268 | if (end) { |
| 6269 | *size = end - val; |
| 6270 | return val; |
| 6271 | } |
| 6272 | if (*val == '\0') |
| 6273 | return NULL; |
| 6274 | /* Optimization: if "*pat" did not match the start of "string", |
| 6275 | * we know that "tring", "ring" etc will not match too: |
| 6276 | */ |
| 6277 | if (pattern[0] == '*') |
| 6278 | return NULL; |
| 6279 | val++; |
| 6280 | } |
| 6281 | } |
| 6282 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 6283 | { |
| 6284 | char *result = NULL; |
| 6285 | unsigned res_len = 0; |
| 6286 | unsigned repl_len = strlen(repl); |
| 6287 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6288 | /* Null pattern never matches, including if "var" is empty */ |
| 6289 | if (!pattern[0]) |
| 6290 | return result; /* NULL, no replaces happened */ |
| 6291 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6292 | while (1) { |
| 6293 | int size; |
| 6294 | char *s = strstr_pattern(val, pattern, &size); |
| 6295 | if (!s) |
| 6296 | break; |
| 6297 | |
| 6298 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6299 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 6300 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6301 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 6302 | |
| 6303 | val = s + size; |
| 6304 | if (exp_op == '/') |
| 6305 | break; |
| 6306 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6307 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6308 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 6309 | strcpy(result + res_len, val); |
| 6310 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 6311 | } |
| 6312 | debug_printf_varexp("result:'%s'\n", result); |
| 6313 | return result; |
| 6314 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6315 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6316 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6317 | static int append_str_maybe_ifs_split(o_string *output, int n, |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6318 | int first_ch, const char *val) |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6319 | { |
| 6320 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 6321 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6322 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6323 | if (val && val[0]) |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6324 | n = expand_on_ifs(output, n, val); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6325 | } else { /* quoted "$VAR" */ |
| 6326 | output->has_quoted_part = 1; |
| 6327 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6328 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6329 | if (val && val[0]) |
| 6330 | o_addQstr(output, val); |
| 6331 | } |
| 6332 | return n; |
| 6333 | } |
| 6334 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6335 | /* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6336 | */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6337 | static NOINLINE int expand_one_var(o_string *output, int n, |
| 6338 | int first_ch, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6339 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6340 | const char *val; |
| 6341 | char *to_be_freed; |
| 6342 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6343 | char *var; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6344 | char exp_op; |
| 6345 | char exp_save = exp_save; /* for compiler */ |
| 6346 | char *exp_saveptr; /* points to expansion operator */ |
| 6347 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6348 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6349 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6350 | val = NULL; |
| 6351 | to_be_freed = NULL; |
| 6352 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6353 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6354 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6355 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6356 | arg0 = arg[0]; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6357 | arg[0] = (arg0 & 0x7f); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6358 | exp_op = 0; |
| 6359 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6360 | if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 6361 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 6362 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 6363 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6364 | ) { |
| 6365 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6366 | var++; |
| 6367 | exp_op = 'L'; |
| 6368 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6369 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6370 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6371 | && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6372 | ) { |
| 6373 | /* ${?:0}, ${#[:]%0} etc */ |
| 6374 | exp_saveptr = var + 1; |
| 6375 | } else { |
| 6376 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 6377 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 6378 | } |
| 6379 | exp_op = exp_save = *exp_saveptr; |
| 6380 | if (exp_op) { |
| 6381 | exp_word = exp_saveptr + 1; |
| 6382 | if (exp_op == ':') { |
| 6383 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6384 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6385 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6386 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6387 | ) { |
| 6388 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 6389 | exp_op = ':'; |
| 6390 | exp_word--; |
| 6391 | } |
| 6392 | } |
| 6393 | *exp_saveptr = '\0'; |
| 6394 | } /* else: it's not an expansion op, but bare ${var} */ |
| 6395 | } |
| 6396 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6397 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6398 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 6399 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6400 | int nn = xatoi_positive(var); |
| 6401 | if (nn < G.global_argc) |
| 6402 | val = G.global_argv[nn]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6403 | /* else val remains NULL: $N with too big N */ |
| 6404 | } else { |
| 6405 | switch (var[0]) { |
| 6406 | case '$': /* pid */ |
| 6407 | val = utoa(G.root_pid); |
| 6408 | break; |
| 6409 | case '!': /* bg pid */ |
| 6410 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 6411 | break; |
| 6412 | case '?': /* exitcode */ |
| 6413 | val = utoa(G.last_exitcode); |
| 6414 | break; |
| 6415 | case '#': /* argc */ |
| 6416 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 6417 | break; |
| 6418 | default: |
| 6419 | val = get_local_var_value(var); |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | /* Handle any expansions */ |
| 6424 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6425 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6426 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6427 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6428 | debug_printf_expand("%s\n", val); |
| 6429 | } else if (exp_op) { |
| 6430 | if (exp_op == '%' || exp_op == '#') { |
| 6431 | /* Standard-mandated substring removal ops: |
| 6432 | * ${parameter%word} - remove smallest suffix pattern |
| 6433 | * ${parameter%%word} - remove largest suffix pattern |
| 6434 | * ${parameter#word} - remove smallest prefix pattern |
| 6435 | * ${parameter##word} - remove largest prefix pattern |
| 6436 | * |
| 6437 | * Word is expanded to produce a glob pattern. |
| 6438 | * Then var's value is matched to it and matching part removed. |
| 6439 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6440 | //FIXME: ${x#...${...}...} |
| 6441 | //should evaluate inner ${...} even if x is "" and no shrinking of it is possible - |
| 6442 | //inner ${...} may have side effects! |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6443 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6444 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6445 | char *exp_exp_word; |
| 6446 | char *loc; |
| 6447 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 6448 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6449 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6450 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6451 | exp_exp_word = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6452 | if (exp_exp_word) |
| 6453 | exp_word = exp_exp_word; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6454 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
| 6455 | /* |
| 6456 | * HACK ALERT. We depend here on the fact that |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6457 | * G.global_argv and results of utoa and get_local_var_value |
| 6458 | * are actually in writable memory: |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6459 | * scan_and_match momentarily stores NULs there. |
| 6460 | */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6461 | t = (char*)val; |
| 6462 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6463 | 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] | 6464 | free(exp_exp_word); |
| 6465 | if (loc) { /* match was found */ |
| 6466 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6467 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6468 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6469 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6470 | } |
| 6471 | } |
| 6472 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6473 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6474 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6475 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6476 | * Note that in encoded form it has TWO parts: |
| 6477 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6478 | * and if // is used, it is encoded as \: |
| 6479 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6480 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6481 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6482 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6483 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6484 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6485 | * >az >bz |
| 6486 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6487 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6488 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6489 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6490 | * (note that a*z _pattern_ is never globbed!) |
| 6491 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6492 | char *pattern, *repl, *t; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6493 | pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6494 | if (!pattern) |
| 6495 | pattern = xstrdup(exp_word); |
| 6496 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6497 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6498 | exp_word = p; |
| 6499 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6500 | *p = '\0'; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6501 | repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6502 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6503 | /* HACK ALERT. We depend here on the fact that |
| 6504 | * G.global_argv and results of utoa and get_local_var_value |
| 6505 | * are actually in writable memory: |
| 6506 | * replace_pattern momentarily stores NULs there. */ |
| 6507 | t = (char*)val; |
| 6508 | to_be_freed = replace_pattern(t, |
| 6509 | pattern, |
| 6510 | (repl ? repl : exp_word), |
| 6511 | exp_op); |
| 6512 | if (to_be_freed) /* at least one replace happened */ |
| 6513 | val = to_be_freed; |
| 6514 | free(pattern); |
| 6515 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6516 | } else { |
| 6517 | /* Empty variable always gives nothing */ |
| 6518 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6519 | /* Just skip "replace" part */ |
| 6520 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6521 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6522 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6523 | } |
| 6524 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6525 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6526 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6527 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6528 | /* It's ${var:N[:M]} bashism. |
| 6529 | * Note that in encoded form it has TWO parts: |
| 6530 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6531 | */ |
| 6532 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6533 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6534 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6535 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6536 | if (errmsg) |
| 6537 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6538 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6539 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6540 | exp_word = p; |
| 6541 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6542 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6543 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6544 | if (errmsg) |
| 6545 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6546 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6547 | if (beg < 0) { |
| 6548 | /* negative beg counts from the end */ |
| 6549 | beg = (arith_t)strlen(val) + beg; |
| 6550 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6551 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6552 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6553 | debug_printf_varexp("from val:'%s'\n", val); |
| 6554 | if (len < 0) { |
| 6555 | /* in bash, len=-n means strlen()-n */ |
| 6556 | len = (arith_t)strlen(val) - beg + len; |
| 6557 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6558 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6559 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6560 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6561 | arith_err: |
| 6562 | val = NULL; |
| 6563 | } else { |
| 6564 | /* Paranoia. What if user entered 9999999999999 |
| 6565 | * which fits in arith_t but not int? */ |
| 6566 | if (len >= INT_MAX) |
| 6567 | len = INT_MAX; |
| 6568 | val = to_be_freed = xstrndup(val + beg, len); |
| 6569 | } |
| 6570 | debug_printf_varexp("val:'%s'\n", val); |
| 6571 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6572 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6573 | val = NULL; |
| 6574 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6575 | } else { /* one of "-=+?" */ |
| 6576 | /* Standard-mandated substitution ops: |
| 6577 | * ${var?word} - indicate error if unset |
| 6578 | * If var is unset, word (or a message indicating it is unset |
| 6579 | * if word is null) is written to standard error |
| 6580 | * and the shell exits with a non-zero exit status. |
| 6581 | * Otherwise, the value of var is substituted. |
| 6582 | * ${var-word} - use default value |
| 6583 | * If var is unset, word is substituted. |
| 6584 | * ${var=word} - assign and use default value |
| 6585 | * If var is unset, word is assigned to var. |
| 6586 | * In all cases, final value of var is substituted. |
| 6587 | * ${var+word} - use alternative value |
| 6588 | * If var is unset, null is substituted. |
| 6589 | * Otherwise, word is substituted. |
| 6590 | * |
| 6591 | * Word is subjected to tilde expansion, parameter expansion, |
| 6592 | * command substitution, and arithmetic expansion. |
| 6593 | * If word is not needed, it is not expanded. |
| 6594 | * |
| 6595 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6596 | * but also treat null var as if it is unset. |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6597 | * |
| 6598 | * Word-splitting and single quote behavior: |
| 6599 | * |
| 6600 | * $ f() { for i; do echo "|$i|"; done; }; |
| 6601 | * |
| 6602 | * $ x=; f ${x:?'x y' z} |
| 6603 | * bash: x: x y z #BUG: does not abort, ${} results in empty expansion |
| 6604 | * $ x=; f "${x:?'x y' z}" |
| 6605 | * bash: x: x y z # dash prints: dash: x: 'x y' z #BUG: does not abort, ${} results in "" |
| 6606 | * |
| 6607 | * $ x=; f ${x:='x y' z} |
| 6608 | * |x| |
| 6609 | * |y| |
| 6610 | * |z| |
| 6611 | * $ x=; f "${x:='x y' z}" |
| 6612 | * |'x y' z| |
| 6613 | * |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6614 | * $ x=x; f ${x:+'x y' z} |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6615 | * |x y| |
| 6616 | * |z| |
| 6617 | * $ x=x; f "${x:+'x y' z}" |
| 6618 | * |'x y' z| |
| 6619 | * |
| 6620 | * $ x=; f ${x:-'x y' z} |
| 6621 | * |x y| |
| 6622 | * |z| |
| 6623 | * $ x=; f "${x:-'x y' z}" |
| 6624 | * |'x y' z| |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6625 | */ |
| 6626 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6627 | if (exp_op == '+') |
| 6628 | use_word = !use_word; |
| 6629 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6630 | (exp_save == ':') ? "true" : "false", use_word); |
| 6631 | if (use_word) { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6632 | if (exp_op == '+' || exp_op == '-') { |
| 6633 | /* ${var+word} - use alternative value */ |
| 6634 | /* ${var-word} - use default value */ |
| 6635 | n = encode_then_append_var_plusminus(output, n, exp_word, |
| 6636 | /*dquoted:*/ (arg0 & 0x80) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6637 | ); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6638 | val = NULL; |
| 6639 | } else { |
| 6640 | /* ${var?word} - indicate error if unset */ |
| 6641 | /* ${var=word} - assign and use default value */ |
| 6642 | to_be_freed = encode_then_expand_vararg(exp_word, |
| 6643 | /*handle_squotes:*/ !(arg0 & 0x80), |
| 6644 | /*unbackslash:*/ 0 |
| 6645 | ); |
| 6646 | if (to_be_freed) |
| 6647 | exp_word = to_be_freed; |
| 6648 | if (exp_op == '?') { |
| 6649 | /* mimic bash message */ |
| 6650 | msg_and_die_if_script("%s: %s", |
| 6651 | var, |
| 6652 | exp_word[0] |
| 6653 | ? exp_word |
| 6654 | : "parameter null or not set" |
| 6655 | /* ash has more specific messages, a-la: */ |
| 6656 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
| 6657 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6658 | //TODO: how interactive bash aborts expansion mid-command? |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6659 | //It aborts the entire line, returns to prompt: |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6660 | // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO |
| 6661 | // bash: x: x y z |
| 6662 | // $ |
| 6663 | // ("echo YO" is not executed, neither the f function call) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6664 | } else { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6665 | val = exp_word; |
| 6666 | } |
| 6667 | if (exp_op == '=') { |
| 6668 | /* ${var=[word]} or ${var:=[word]} */ |
| 6669 | if (isdigit(var[0]) || var[0] == '#') { |
| 6670 | /* mimic bash message */ |
| 6671 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
| 6672 | val = NULL; |
| 6673 | } else { |
| 6674 | char *new_var = xasprintf("%s=%s", var, val); |
| 6675 | set_local_var(new_var, /*flag:*/ 0); |
| 6676 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6677 | } |
| 6678 | } |
| 6679 | } |
| 6680 | } /* one of "-=+?" */ |
| 6681 | |
| 6682 | *exp_saveptr = exp_save; |
| 6683 | } /* if (exp_op) */ |
| 6684 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6685 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6686 | *pp = p; |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6687 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6688 | n = append_str_maybe_ifs_split(output, n, first_ch, val); |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6689 | |
| 6690 | free(to_be_freed); |
| 6691 | return n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6692 | } |
| 6693 | |
| 6694 | /* Expand all variable references in given string, adding words to list[] |
| 6695 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6696 | * to be filled). This routine is extremely tricky: has to deal with |
| 6697 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6698 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6699 | 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] | 6700 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6701 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6702 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6703 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6704 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6705 | char *p; |
| 6706 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6707 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6708 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6709 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6710 | |
| 6711 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6712 | char first_ch; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6713 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6714 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6715 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6716 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6717 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6718 | o_addchr(output, '\0'); |
| 6719 | n = o_save_ptr(output, n); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6720 | output->ended_in_ifs = 0; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6723 | o_addblock(output, arg, p - arg); |
| 6724 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6725 | arg = ++p; |
| 6726 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6727 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6728 | /* Fetch special var name (if it is indeed one of them) |
| 6729 | * and quote bit, force the bit on if singleword expansion - |
| 6730 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6731 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6732 | |
| 6733 | /* Is this variable quoted and thus expansion can't be null? |
| 6734 | * "$@" is special. Even if quoted, it can still |
| 6735 | * expand to nothing (not even an empty string), |
| 6736 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6737 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6738 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6739 | |
| 6740 | switch (first_ch & 0x7f) { |
| 6741 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6742 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6743 | case '@': { |
| 6744 | int i; |
| 6745 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6746 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6747 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6748 | 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] | 6749 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6750 | while (G.global_argv[i]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6751 | n = expand_on_ifs(output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6752 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6753 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6754 | /* this argv[] is not empty and not last: |
| 6755 | * put terminating NUL, start new word */ |
| 6756 | o_addchr(output, '\0'); |
| 6757 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6758 | n = o_save_ptr(output, n); |
| 6759 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6760 | } |
| 6761 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6762 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6763 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6764 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6765 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6766 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6767 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6768 | while (1) { |
| 6769 | o_addQstr(output, G.global_argv[i]); |
| 6770 | if (++i >= G.global_argc) |
| 6771 | break; |
| 6772 | o_addchr(output, '\0'); |
| 6773 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6774 | n = o_save_ptr(output, n); |
| 6775 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6776 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6777 | while (1) { |
| 6778 | o_addQstr(output, G.global_argv[i]); |
| 6779 | if (!G.global_argv[++i]) |
| 6780 | break; |
| 6781 | if (G.ifs[0]) |
| 6782 | o_addchr(output, G.ifs[0]); |
| 6783 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6784 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6785 | } |
| 6786 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6787 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6788 | case SPECIAL_VAR_SYMBOL: { |
| 6789 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6790 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6791 | output->has_quoted_part = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6792 | cant_be_null = 0x80; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6793 | arg++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6794 | break; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6795 | } |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6796 | case SPECIAL_VAR_QUOTED_SVS: |
| 6797 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6798 | /* "^C variable", represents literal ^C char (possible in scripts) */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6799 | o_addchr(output, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6800 | arg++; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6801 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6802 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6803 | case '`': { |
| 6804 | /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6805 | o_string subst_result = NULL_O_STRING; |
| 6806 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6807 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6808 | arg++; |
| 6809 | /* Can't just stuff it into output o_string, |
| 6810 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6811 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6812 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6813 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6814 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6815 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6816 | n = append_str_maybe_ifs_split(output, n, first_ch, subst_result.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6817 | o_free(&subst_result); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6818 | break; |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6819 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6820 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6821 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6822 | case '+': { |
| 6823 | /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6824 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6825 | |
| 6826 | arg++; /* skip '+' */ |
| 6827 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6828 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6829 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6830 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6831 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6832 | o_addstr(output, arith_buf); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6833 | break; |
| 6834 | } |
| 6835 | #endif |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6836 | default: |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6837 | /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6838 | n = expand_one_var(output, n, first_ch, arg, &p); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6839 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6840 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6841 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6842 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6843 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6844 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6845 | *p = SPECIAL_VAR_SYMBOL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6846 | arg = ++p; |
| 6847 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6848 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6849 | if (*arg) { |
| 6850 | /* handle trailing string */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6851 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6852 | o_addchr(output, '\0'); |
| 6853 | n = o_save_ptr(output, n); |
| 6854 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6855 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6856 | /* this part is literal, and it was already pre-quoted |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6857 | * if needed (much earlier), do not use o_addQstr here! |
| 6858 | */ |
| 6859 | o_addstr(output, arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6860 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6861 | } else |
| 6862 | if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6863 | && !(cant_be_null & 0x80) /* and all vars were not quoted */ |
| 6864 | && !output->has_quoted_part |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6865 | ) { |
| 6866 | n--; |
| 6867 | /* allow to reuse list[n] later without re-growth */ |
| 6868 | output->has_empty_slot = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6869 | } |
| 6870 | |
| 6871 | return n; |
| 6872 | } |
| 6873 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6874 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6875 | { |
| 6876 | int n; |
| 6877 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6878 | o_string output = NULL_O_STRING; |
| 6879 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6880 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6881 | |
| 6882 | n = 0; |
Denys Vlasenko | 57235be | 2018-07-20 14:45:12 +0200 | [diff] [blame] | 6883 | for (;;) { |
| 6884 | /* go to next list[n] */ |
| 6885 | output.ended_in_ifs = 0; |
| 6886 | n = o_save_ptr(&output, n); |
| 6887 | |
| 6888 | if (!*argv) |
| 6889 | break; |
| 6890 | |
| 6891 | /* expand argv[i] */ |
| 6892 | n = expand_vars_to_list(&output, n, *argv++); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6893 | /* if (!output->has_empty_slot) -- need this?? */ |
| 6894 | o_addchr(&output, '\0'); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6895 | } |
| 6896 | debug_print_list("expand_variables", &output, n); |
| 6897 | |
| 6898 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6899 | list = o_finalize_list(&output, n); |
| 6900 | debug_print_strings("expand_variables[1]", list); |
| 6901 | return list; |
| 6902 | } |
| 6903 | |
| 6904 | static char **expand_strvec_to_strvec(char **argv) |
| 6905 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6906 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6907 | } |
| 6908 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6909 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6910 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6911 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6912 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6913 | } |
| 6914 | #endif |
| 6915 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6916 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6917 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6918 | * |
| 6919 | * NB: should NOT do globbing! |
| 6920 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6921 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6922 | static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6923 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6924 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6925 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6926 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6927 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6928 | char *argv[2], **list; |
| 6929 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6930 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6931 | /* This is generally an optimization, but it also |
| 6932 | * handles "", which otherwise trips over !list[0] check below. |
| 6933 | * (is this ever happens that we actually get str="" here?) |
| 6934 | */ |
| 6935 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6936 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6937 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6938 | return xstrdup(str); |
| 6939 | } |
| 6940 | |
| 6941 | argv[0] = (char*)str; |
| 6942 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6943 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 6944 | if (!list[0]) { |
| 6945 | /* Example where it happens: |
| 6946 | * x=; echo ${x:-"$@"} |
| 6947 | */ |
| 6948 | ((char*)list)[0] = '\0'; |
| 6949 | } else { |
| 6950 | if (HUSH_DEBUG) |
| 6951 | if (list[1]) |
| 6952 | bb_error_msg_and_die("BUG in varexp2"); |
| 6953 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6954 | overlapping_strcpy((char*)list, list[0]); |
| 6955 | if (do_unbackslash) |
| 6956 | unbackslash((char*)list); |
| 6957 | } |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6958 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6959 | return (char*)list; |
| 6960 | } |
| 6961 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6962 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6963 | static char* expand_strvec_to_string(char **argv) |
| 6964 | { |
| 6965 | char **list; |
| 6966 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6967 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6968 | /* Convert all NULs to spaces */ |
| 6969 | if (list[0]) { |
| 6970 | int n = 1; |
| 6971 | while (list[n]) { |
| 6972 | if (HUSH_DEBUG) |
| 6973 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6974 | bb_error_msg_and_die("BUG in varexp3"); |
| 6975 | /* bash uses ' ' regardless of $IFS contents */ |
| 6976 | list[n][-1] = ' '; |
| 6977 | n++; |
| 6978 | } |
| 6979 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6980 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6981 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6982 | return (char*)list; |
| 6983 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6984 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6985 | |
| 6986 | static char **expand_assignments(char **argv, int count) |
| 6987 | { |
| 6988 | int i; |
| 6989 | char **p; |
| 6990 | |
| 6991 | G.expanded_assignments = p = NULL; |
| 6992 | /* Expand assignments into one string each */ |
| 6993 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6994 | p = add_string_to_strings(p, |
| 6995 | expand_string_to_string(argv[i], |
| 6996 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6997 | /*unbackslash:*/ 1 |
| 6998 | ) |
| 6999 | ); |
| 7000 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7001 | } |
| 7002 | G.expanded_assignments = NULL; |
| 7003 | return p; |
| 7004 | } |
| 7005 | |
| 7006 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7007 | static void switch_off_special_sigs(unsigned mask) |
| 7008 | { |
| 7009 | unsigned sig = 0; |
| 7010 | while ((mask >>= 1) != 0) { |
| 7011 | sig++; |
| 7012 | if (!(mask & 1)) |
| 7013 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7014 | #if ENABLE_HUSH_TRAP |
| 7015 | if (G_traps) { |
| 7016 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7017 | /* trap is '', has to remain SIG_IGN */ |
| 7018 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7019 | free(G_traps[sig]); |
| 7020 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7021 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7022 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7023 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7024 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7025 | } |
| 7026 | } |
| 7027 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 7028 | #if BB_MMU |
| 7029 | /* never called */ |
| 7030 | void re_execute_shell(char ***to_free, const char *s, |
| 7031 | char *g_argv0, char **g_argv, |
| 7032 | char **builtin_argv) NORETURN; |
| 7033 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7034 | static void reset_traps_to_defaults(void) |
| 7035 | { |
| 7036 | /* This function is always called in a child shell |
| 7037 | * after fork (not vfork, NOMMU doesn't use this function). |
| 7038 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7039 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7040 | unsigned mask; |
| 7041 | |
| 7042 | /* Child shells are not interactive. |
| 7043 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 7044 | * Testcase: (while :; do :; done) + ^Z should background. |
| 7045 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 7046 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7047 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7048 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7049 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7050 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7051 | /* Switch off special sigs */ |
| 7052 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7053 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7054 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7055 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7056 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7057 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 7058 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7059 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7060 | # if ENABLE_HUSH_TRAP |
| 7061 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7062 | return; |
| 7063 | |
| 7064 | /* Reset all sigs to default except ones with empty traps */ |
| 7065 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7066 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7067 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7068 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7069 | continue; /* empty trap: has to remain SIG_IGN */ |
| 7070 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7071 | free(G_traps[sig]); |
| 7072 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7073 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7074 | if (sig == 0) |
| 7075 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7076 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7077 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7078 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7079 | } |
| 7080 | |
| 7081 | #else /* !BB_MMU */ |
| 7082 | |
| 7083 | static void re_execute_shell(char ***to_free, const char *s, |
| 7084 | char *g_argv0, char **g_argv, |
| 7085 | char **builtin_argv) NORETURN; |
| 7086 | static void re_execute_shell(char ***to_free, const char *s, |
| 7087 | char *g_argv0, char **g_argv, |
| 7088 | char **builtin_argv) |
| 7089 | { |
| 7090 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 7091 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 7092 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 7093 | char *heredoc_argv[4]; |
| 7094 | struct variable *cur; |
| 7095 | # if ENABLE_HUSH_FUNCTIONS |
| 7096 | struct function *funcp; |
| 7097 | # endif |
| 7098 | char **argv, **pp; |
| 7099 | unsigned cnt; |
| 7100 | unsigned long long empty_trap_mask; |
| 7101 | |
| 7102 | if (!g_argv0) { /* heredoc */ |
| 7103 | argv = heredoc_argv; |
| 7104 | argv[0] = (char *) G.argv0_for_re_execing; |
| 7105 | argv[1] = (char *) "-<"; |
| 7106 | argv[2] = (char *) s; |
| 7107 | argv[3] = NULL; |
| 7108 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 7109 | goto do_exec; |
| 7110 | } |
| 7111 | |
| 7112 | cnt = 0; |
| 7113 | pp = builtin_argv; |
| 7114 | if (pp) while (*pp++) |
| 7115 | cnt++; |
| 7116 | |
| 7117 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7118 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7119 | int sig; |
| 7120 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7121 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7122 | empty_trap_mask |= 1LL << sig; |
| 7123 | } |
| 7124 | } |
| 7125 | |
| 7126 | sprintf(param_buf, NOMMU_HACK_FMT |
| 7127 | , (unsigned) G.root_pid |
| 7128 | , (unsigned) G.root_ppid |
| 7129 | , (unsigned) G.last_bg_pid |
| 7130 | , (unsigned) G.last_exitcode |
| 7131 | , cnt |
| 7132 | , empty_trap_mask |
| 7133 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 7134 | ); |
| 7135 | # undef NOMMU_HACK_FMT |
| 7136 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 7137 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 7138 | */ |
| 7139 | cnt += 6; |
| 7140 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7141 | if (!cur->flg_export || cur->flg_read_only) |
| 7142 | cnt += 2; |
| 7143 | } |
| 7144 | # if ENABLE_HUSH_FUNCTIONS |
| 7145 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 7146 | cnt += 3; |
| 7147 | # endif |
| 7148 | pp = g_argv; |
| 7149 | while (*pp++) |
| 7150 | cnt++; |
| 7151 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 7152 | *pp++ = (char *) G.argv0_for_re_execing; |
| 7153 | *pp++ = param_buf; |
| 7154 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7155 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 7156 | continue; |
| 7157 | if (cur->flg_read_only) { |
| 7158 | *pp++ = (char *) "-R"; |
| 7159 | *pp++ = cur->varstr; |
| 7160 | } else if (!cur->flg_export) { |
| 7161 | *pp++ = (char *) "-V"; |
| 7162 | *pp++ = cur->varstr; |
| 7163 | } |
| 7164 | } |
| 7165 | # if ENABLE_HUSH_FUNCTIONS |
| 7166 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 7167 | *pp++ = (char *) "-F"; |
| 7168 | *pp++ = funcp->name; |
| 7169 | *pp++ = funcp->body_as_string; |
| 7170 | } |
| 7171 | # endif |
| 7172 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 7173 | * |
| 7174 | * However, POSIX says that subshells reset signals with traps |
| 7175 | * to SIG_DFL. |
| 7176 | * I tested bash-3.2 and it not only does that with true subshells |
| 7177 | * of the form ( list ), but with any forked children shells. |
| 7178 | * I set trap "echo W" WINCH; and then tried: |
| 7179 | * |
| 7180 | * { echo 1; sleep 20; echo 2; } & |
| 7181 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 7182 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 7183 | * |
| 7184 | * In all these cases sending SIGWINCH to the child shell |
| 7185 | * did not run the trap. If I add trap "echo V" WINCH; |
| 7186 | * _inside_ group (just before echo 1), it works. |
| 7187 | * |
| 7188 | * 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] | 7189 | */ |
| 7190 | *pp++ = (char *) "-c"; |
| 7191 | *pp++ = (char *) s; |
| 7192 | if (builtin_argv) { |
| 7193 | while (*++builtin_argv) |
| 7194 | *pp++ = *builtin_argv; |
| 7195 | *pp++ = (char *) ""; |
| 7196 | } |
| 7197 | *pp++ = g_argv0; |
| 7198 | while (*g_argv) |
| 7199 | *pp++ = *g_argv++; |
| 7200 | /* *pp = NULL; - is already there */ |
| 7201 | pp = environ; |
| 7202 | |
| 7203 | do_exec: |
| 7204 | 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] | 7205 | /* Don't propagate SIG_IGN to the child */ |
| 7206 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7207 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7208 | execve(bb_busybox_exec_path, argv, pp); |
| 7209 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 7210 | if (argv[0][0] == '/') |
| 7211 | execve(argv[0], argv, pp); |
| 7212 | xfunc_error_retval = 127; |
| 7213 | bb_error_msg_and_die("can't re-execute the shell"); |
| 7214 | } |
| 7215 | #endif /* !BB_MMU */ |
| 7216 | |
| 7217 | |
| 7218 | static int run_and_free_list(struct pipe *pi); |
| 7219 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7220 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7221 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 7222 | * end_trigger controls how often we stop parsing |
| 7223 | * NUL: parse all, execute, return |
| 7224 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 7225 | */ |
| 7226 | 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] | 7227 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7228 | /* Why we need empty flag? |
| 7229 | * An obscure corner case "false; ``; echo $?": |
| 7230 | * empty command in `` should still set $? to 0. |
| 7231 | * But we can't just set $? to 0 at the start, |
| 7232 | * this breaks "false; echo `echo $?`" case. |
| 7233 | */ |
| 7234 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7235 | while (1) { |
| 7236 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 7237 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7238 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 7239 | if (end_trigger == ';') { |
| 7240 | G.promptmode = 0; /* PS1 */ |
| 7241 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 7242 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7243 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 7244 | pipe_list = parse_stream(NULL, NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 7245 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 7246 | /* If we are in "big" script |
| 7247 | * (not in `cmd` or something similar)... |
| 7248 | */ |
| 7249 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 7250 | /* Discard cached input (rest of line) */ |
| 7251 | int ch = inp->last_char; |
| 7252 | while (ch != EOF && ch != '\n') { |
| 7253 | //bb_error_msg("Discarded:'%c'", ch); |
| 7254 | ch = i_getch(inp); |
| 7255 | } |
| 7256 | /* Force prompt */ |
| 7257 | inp->p = NULL; |
| 7258 | /* This stream isn't empty */ |
| 7259 | empty = 0; |
| 7260 | continue; |
| 7261 | } |
| 7262 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7263 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7264 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7265 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7266 | debug_print_tree(pipe_list, 0); |
| 7267 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7268 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7269 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7270 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 7271 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7272 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7275 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7276 | { |
| 7277 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7278 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 7279 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7280 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7281 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7282 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7283 | } |
| 7284 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7285 | static void parse_and_run_file(HFILE *fp) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7286 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7287 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7288 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 7289 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7290 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7291 | setup_file_in_str(&input, fp); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7292 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7293 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7294 | } |
| 7295 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7296 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7297 | static int generate_stream_from_string(const char *s, pid_t *pid_p) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7298 | { |
| 7299 | pid_t pid; |
| 7300 | int channel[2]; |
| 7301 | # if !BB_MMU |
| 7302 | char **to_free = NULL; |
| 7303 | # endif |
| 7304 | |
| 7305 | xpipe(channel); |
| 7306 | pid = BB_MMU ? xfork() : xvfork(); |
| 7307 | if (pid == 0) { /* child */ |
| 7308 | disable_restore_tty_pgrp_on_exit(); |
| 7309 | /* Process substitution is not considered to be usual |
| 7310 | * 'command execution'. |
| 7311 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 7312 | */ |
| 7313 | bb_signals(0 |
| 7314 | + (1 << SIGTSTP) |
| 7315 | + (1 << SIGTTIN) |
| 7316 | + (1 << SIGTTOU) |
| 7317 | , SIG_IGN); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7318 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 7319 | xmove_fd(channel[1], 1); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7320 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7321 | /* Awful hack for `trap` or $(trap). |
| 7322 | * |
| 7323 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 7324 | * contains an example where "trap" is executed in a subshell: |
| 7325 | * |
| 7326 | * save_traps=$(trap) |
| 7327 | * ... |
| 7328 | * eval "$save_traps" |
| 7329 | * |
| 7330 | * Standard does not say that "trap" in subshell shall print |
| 7331 | * parent shell's traps. It only says that its output |
| 7332 | * must have suitable form, but then, in the above example |
| 7333 | * (which is not supposed to be normative), it implies that. |
| 7334 | * |
| 7335 | * bash (and probably other shell) does implement it |
| 7336 | * (traps are reset to defaults, but "trap" still shows them), |
| 7337 | * but as a result, "trap" logic is hopelessly messed up: |
| 7338 | * |
| 7339 | * # trap |
| 7340 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 7341 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 7342 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 7343 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 7344 | * trap -- 'echo Ho' SIGWINCH |
| 7345 | * # echo `(trap)` <--- in subshell in subshell - output |
| 7346 | * trap -- 'echo Ho' SIGWINCH |
| 7347 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 7348 | * trap -- 'echo Ho' SIGWINCH |
| 7349 | * |
| 7350 | * The rules when to forget and when to not forget traps |
| 7351 | * get really complex and nonsensical. |
| 7352 | * |
| 7353 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 7354 | */ |
| 7355 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 7356 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7357 | && skip_whitespace(s + 4)[0] == '\0' |
| 7358 | ) { |
| 7359 | static const char *const argv[] = { NULL, NULL }; |
| 7360 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 7361 | fflush_all(); /* important */ |
| 7362 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7363 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7364 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7365 | # if BB_MMU |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7366 | /* Prevent it from trying to handle ctrl-z etc */ |
| 7367 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 7368 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7369 | reset_traps_to_defaults(); |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7370 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 7371 | //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7372 | parse_and_run_string(s); |
| 7373 | _exit(G.last_exitcode); |
| 7374 | # else |
| 7375 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 7376 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 7377 | * huge=`cat BIG` # was blocking here forever |
| 7378 | * echo OK |
| 7379 | */ |
| 7380 | re_execute_shell(&to_free, |
| 7381 | s, |
| 7382 | G.global_argv[0], |
| 7383 | G.global_argv + 1, |
| 7384 | NULL); |
| 7385 | # endif |
| 7386 | } |
| 7387 | |
| 7388 | /* parent */ |
| 7389 | *pid_p = pid; |
| 7390 | # if ENABLE_HUSH_FAST |
| 7391 | G.count_SIGCHLD++; |
| 7392 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 7393 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 7394 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7395 | # endif |
| 7396 | enable_restore_tty_pgrp_on_exit(); |
| 7397 | # if !BB_MMU |
| 7398 | free(to_free); |
| 7399 | # endif |
| 7400 | close(channel[1]); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7401 | return channel[0]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | /* Return code is exit status of the process that is run. */ |
| 7405 | static int process_command_subs(o_string *dest, const char *s) |
| 7406 | { |
| 7407 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7408 | pid_t pid; |
| 7409 | int status, ch, eol_cnt; |
| 7410 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7411 | fp = xfdopen_for_read(generate_stream_from_string(s, &pid)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7412 | |
| 7413 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7414 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7415 | while ((ch = getc(fp)) != EOF) { |
| 7416 | if (ch == '\0') |
| 7417 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7418 | if (ch == '\n') { |
| 7419 | eol_cnt++; |
| 7420 | continue; |
| 7421 | } |
| 7422 | while (eol_cnt) { |
| 7423 | o_addchr(dest, '\n'); |
| 7424 | eol_cnt--; |
| 7425 | } |
| 7426 | o_addQchr(dest, ch); |
| 7427 | } |
| 7428 | |
| 7429 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7430 | fclose(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7431 | /* We need to extract exitcode. Test case |
| 7432 | * "true; echo `sleep 1; false` $?" |
| 7433 | * should print 1 */ |
| 7434 | safe_waitpid(pid, &status, 0); |
| 7435 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 7436 | return WEXITSTATUS(status); |
| 7437 | } |
| 7438 | #endif /* ENABLE_HUSH_TICK */ |
| 7439 | |
| 7440 | |
| 7441 | static void setup_heredoc(struct redir_struct *redir) |
| 7442 | { |
| 7443 | struct fd_pair pair; |
| 7444 | pid_t pid; |
| 7445 | int len, written; |
| 7446 | /* the _body_ of heredoc (misleading field name) */ |
| 7447 | const char *heredoc = redir->rd_filename; |
| 7448 | char *expanded; |
| 7449 | #if !BB_MMU |
| 7450 | char **to_free; |
| 7451 | #endif |
| 7452 | |
| 7453 | expanded = NULL; |
| 7454 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 7455 | expanded = encode_then_expand_string(heredoc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7456 | if (expanded) |
| 7457 | heredoc = expanded; |
| 7458 | } |
| 7459 | len = strlen(heredoc); |
| 7460 | |
| 7461 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 7462 | xpiped_pair(pair); |
| 7463 | xmove_fd(pair.rd, redir->rd_fd); |
| 7464 | |
| 7465 | /* Try writing without forking. Newer kernels have |
| 7466 | * dynamically growing pipes. Must use non-blocking write! */ |
| 7467 | ndelay_on(pair.wr); |
| 7468 | while (1) { |
| 7469 | written = write(pair.wr, heredoc, len); |
| 7470 | if (written <= 0) |
| 7471 | break; |
| 7472 | len -= written; |
| 7473 | if (len == 0) { |
| 7474 | close(pair.wr); |
| 7475 | free(expanded); |
| 7476 | return; |
| 7477 | } |
| 7478 | heredoc += written; |
| 7479 | } |
| 7480 | ndelay_off(pair.wr); |
| 7481 | |
| 7482 | /* Okay, pipe buffer was not big enough */ |
| 7483 | /* Note: we must not create a stray child (bastard? :) |
| 7484 | * for the unsuspecting parent process. Child creates a grandchild |
| 7485 | * and exits before parent execs the process which consumes heredoc |
| 7486 | * (that exec happens after we return from this function) */ |
| 7487 | #if !BB_MMU |
| 7488 | to_free = NULL; |
| 7489 | #endif |
| 7490 | pid = xvfork(); |
| 7491 | if (pid == 0) { |
| 7492 | /* child */ |
| 7493 | disable_restore_tty_pgrp_on_exit(); |
| 7494 | pid = BB_MMU ? xfork() : xvfork(); |
| 7495 | if (pid != 0) |
| 7496 | _exit(0); |
| 7497 | /* grandchild */ |
| 7498 | close(redir->rd_fd); /* read side of the pipe */ |
| 7499 | #if BB_MMU |
| 7500 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 7501 | _exit(0); |
| 7502 | #else |
| 7503 | /* Delegate blocking writes to another process */ |
| 7504 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 7505 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 7506 | #endif |
| 7507 | } |
| 7508 | /* parent */ |
| 7509 | #if ENABLE_HUSH_FAST |
| 7510 | G.count_SIGCHLD++; |
| 7511 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7512 | #endif |
| 7513 | enable_restore_tty_pgrp_on_exit(); |
| 7514 | #if !BB_MMU |
| 7515 | free(to_free); |
| 7516 | #endif |
| 7517 | close(pair.wr); |
| 7518 | free(expanded); |
| 7519 | wait(NULL); /* wait till child has died */ |
| 7520 | } |
| 7521 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7522 | struct squirrel { |
| 7523 | int orig_fd; |
| 7524 | int moved_to; |
| 7525 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7526 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7527 | }; |
| 7528 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7529 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7530 | { |
| 7531 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7532 | sq[i].orig_fd = orig; |
| 7533 | sq[i].moved_to = moved; |
| 7534 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7535 | return sq; |
| 7536 | } |
| 7537 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7538 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7539 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7540 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7541 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7542 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7543 | i = 0; |
| 7544 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7545 | /* If we collide with an already moved fd... */ |
| 7546 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7547 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7548 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7549 | if (sq[i].moved_to < 0) /* what? */ |
| 7550 | xfunc_die(); |
| 7551 | return sq; |
| 7552 | } |
| 7553 | if (fd == sq[i].orig_fd) { |
| 7554 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7555 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7556 | return sq; |
| 7557 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7558 | } |
| 7559 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7560 | /* 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] | 7561 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7562 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7563 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7564 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7565 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7566 | } |
| 7567 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7568 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7569 | { |
| 7570 | int i; |
| 7571 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7572 | i = 0; |
| 7573 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7574 | /* If we collide with an already moved fd... */ |
| 7575 | if (fd == sq[i].orig_fd) { |
| 7576 | /* Examples: |
| 7577 | * "echo 3>FILE 3>&- 3>FILE" |
| 7578 | * "echo 3>&- 3>FILE" |
| 7579 | * No need for last redirect to insert |
| 7580 | * another "need to close 3" indicator. |
| 7581 | */ |
| 7582 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7583 | return sq; |
| 7584 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7588 | return append_squirrel(sq, i, fd, -1); |
| 7589 | } |
| 7590 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7591 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7592 | * Move all conflicting internally used fds, |
| 7593 | * and remember them so that we can restore them later. |
| 7594 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7595 | 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] | 7596 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7597 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7598 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7599 | |
| 7600 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7601 | if (fd == G.interactive_fd) { |
| 7602 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7603 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7604 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7605 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7606 | } |
| 7607 | #endif |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7608 | /* Are we called from setup_redirects(squirrel==NULL) |
| 7609 | * in redirect in a [v]forked child? |
| 7610 | */ |
| 7611 | if (sqp == NULL) { |
| 7612 | /* No need to move script fds. |
| 7613 | * For NOMMU case, it's actively wrong: we'd change ->fd |
| 7614 | * fields in memory for the parent, but parent's fds |
| 7615 | * aren't be moved, it would use wrong fd! |
| 7616 | * Reproducer: "cmd 3>FILE" in script. |
| 7617 | * If we would call move_HFILEs_on_redirect(), child would: |
| 7618 | * fcntl64(3, F_DUPFD_CLOEXEC, 10) = 10 |
| 7619 | * close(3) = 0 |
| 7620 | * and change ->fd to 10 if fd#3 is a script fd. WRONG. |
| 7621 | */ |
| 7622 | //bb_error_msg("sqp == NULL: [v]forked child"); |
| 7623 | return 0; |
| 7624 | } |
| 7625 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7626 | /* If this one of script's fds? */ |
| 7627 | if (move_HFILEs_on_redirect(fd, avoid_fd)) |
| 7628 | return 1; /* yes. "we closed fd" (actually moved it) */ |
| 7629 | |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7630 | /* Are we called for "exec 3>FILE"? Came through |
| 7631 | * redirect_and_varexp_helper(squirrel=ERR_PTR) -> setup_redirects(ERR_PTR) |
| 7632 | * This case used to fail for this script: |
| 7633 | * exec 3>FILE |
| 7634 | * echo Ok |
| 7635 | * ...100000 more lines... |
| 7636 | * echo Ok |
| 7637 | * as follows: |
| 7638 | * read(3, "exec 3>FILE\necho Ok\necho Ok"..., 1024) = 1024 |
| 7639 | * open("FILE", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 4 |
| 7640 | * dup2(4, 3) = 3 |
| 7641 | * ^^^^^^^^ oops, we lost fd#3 opened to our script! |
| 7642 | * close(4) = 0 |
| 7643 | * write(1, "Ok\n", 3) = 3 |
| 7644 | * ... = 3 |
| 7645 | * write(1, "Ok\n", 3) = 3 |
| 7646 | * read(3, 0x94fbc08, 1024) = -1 EBADF (Bad file descriptor) |
| 7647 | * ^^^^^^^^ oops, wrong fd!!! |
| 7648 | * With this case separate from sqp == NULL and *after* move_HFILEs, |
| 7649 | * it now works: |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7650 | */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7651 | if (sqp == ERR_PTR) { |
| 7652 | /* Don't preserve redirected fds: exec is _meant_ to change these */ |
| 7653 | //bb_error_msg("sqp == ERR_PTR: exec >FILE"); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7654 | return 0; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7655 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7656 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7657 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7658 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7659 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7660 | } |
| 7661 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7662 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7663 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7664 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7665 | int i; |
| 7666 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7667 | if (sq[i].moved_to >= 0) { |
| 7668 | /* We simply die on error */ |
| 7669 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7670 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7671 | } else { |
| 7672 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7673 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7674 | close(sq[i].orig_fd); |
| 7675 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7676 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7677 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7678 | } |
| 7679 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7680 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7681 | } |
| 7682 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7683 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7684 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7685 | { |
| 7686 | if (G_interactive_fd) |
| 7687 | close(G_interactive_fd); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7688 | close_all_HFILE_list(); |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7689 | } |
| 7690 | #endif |
| 7691 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7692 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7693 | { |
| 7694 | int i; |
| 7695 | |
| 7696 | #if ENABLE_HUSH_INTERACTIVE |
| 7697 | if (fd == G.interactive_fd) |
| 7698 | return 1; |
| 7699 | #endif |
| 7700 | /* If this one of script's fds? */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7701 | if (fd_in_HFILEs(fd)) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7702 | return 1; |
| 7703 | |
| 7704 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7705 | if (fd == sq[i].moved_to) |
| 7706 | return 1; |
| 7707 | } |
| 7708 | return 0; |
| 7709 | } |
| 7710 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7711 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7712 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7713 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7714 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7715 | struct redir_struct *redir; |
| 7716 | |
| 7717 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7718 | int newfd; |
| 7719 | int closed; |
| 7720 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7721 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7722 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7723 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7724 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7725 | * of the heredoc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7726 | debug_printf_redir("set heredoc '%s'\n", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7727 | redir->rd_filename); |
| 7728 | setup_heredoc(redir); |
| 7729 | continue; |
| 7730 | } |
| 7731 | |
| 7732 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7733 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7734 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7735 | int mode; |
| 7736 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7737 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7738 | /* Examples: |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7739 | * "cmd >" (no filename) |
| 7740 | * "cmd > <file" (2nd redirect starts too early) |
| 7741 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7742 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7743 | continue; |
| 7744 | } |
| 7745 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7746 | p = expand_string_to_string(redir->rd_filename, |
| 7747 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7748 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7749 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7750 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7751 | /* Error message from open_or_warn can be lost |
| 7752 | * if stderr has been redirected, but bash |
| 7753 | * and ash both lose it as well |
| 7754 | * (though zsh doesn't!) |
| 7755 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7756 | return 1; |
| 7757 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7758 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7759 | /* open() gave us precisely the fd we wanted. |
| 7760 | * This means that this fd was not busy |
| 7761 | * (not opened to anywhere). |
| 7762 | * Remember to close it on restore: |
| 7763 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7764 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7765 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7766 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7767 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7768 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7769 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7770 | } |
| 7771 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7772 | if (newfd == redir->rd_fd) |
| 7773 | continue; |
| 7774 | |
| 7775 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7776 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7777 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7778 | |
| 7779 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7780 | if (newfd == REDIRFD_CLOSE) { |
| 7781 | /* "N>&-" means "close me" */ |
| 7782 | if (!closed) { |
| 7783 | /* ^^^ optimization: saving may already |
| 7784 | * have closed it. If not... */ |
| 7785 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7786 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7787 | /* Sometimes we do another close on restore, getting EBADF. |
| 7788 | * Consider "echo 3>FILE 3>&-" |
| 7789 | * first redirect remembers "need to close 3", |
| 7790 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7791 | */ |
| 7792 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7793 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7794 | if (internally_opened_fd(newfd, sqp && sqp != ERR_PTR ? *sqp : NULL)) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7795 | //errno = EBADF; |
| 7796 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7797 | newfd = -1; /* same effect as code above */ |
| 7798 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7799 | xdup2(newfd, redir->rd_fd); |
| 7800 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7801 | /* "rd_fd > FILE" */ |
| 7802 | close(newfd); |
| 7803 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7804 | } |
| 7805 | } |
| 7806 | return 0; |
| 7807 | } |
| 7808 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7809 | static char *find_in_path(const char *arg) |
| 7810 | { |
| 7811 | char *ret = NULL; |
| 7812 | const char *PATH = get_local_var_value("PATH"); |
| 7813 | |
| 7814 | if (!PATH) |
| 7815 | return NULL; |
| 7816 | |
| 7817 | while (1) { |
| 7818 | const char *end = strchrnul(PATH, ':'); |
| 7819 | int sz = end - PATH; /* must be int! */ |
| 7820 | |
| 7821 | free(ret); |
| 7822 | if (sz != 0) { |
| 7823 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7824 | } else { |
| 7825 | /* We have xxx::yyyy in $PATH, |
| 7826 | * it means "use current dir" */ |
| 7827 | ret = xstrdup(arg); |
| 7828 | } |
| 7829 | if (access(ret, F_OK) == 0) |
| 7830 | break; |
| 7831 | |
| 7832 | if (*end == '\0') { |
| 7833 | free(ret); |
| 7834 | return NULL; |
| 7835 | } |
| 7836 | PATH = end + 1; |
| 7837 | } |
| 7838 | |
| 7839 | return ret; |
| 7840 | } |
| 7841 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7842 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7843 | const struct built_in_command *x, |
| 7844 | const struct built_in_command *end) |
| 7845 | { |
| 7846 | while (x != end) { |
| 7847 | if (strcmp(name, x->b_cmd) != 0) { |
| 7848 | x++; |
| 7849 | continue; |
| 7850 | } |
| 7851 | debug_printf_exec("found builtin '%s'\n", name); |
| 7852 | return x; |
| 7853 | } |
| 7854 | return NULL; |
| 7855 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7856 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7857 | { |
| 7858 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7859 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7860 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7861 | { |
| 7862 | const struct built_in_command *x = find_builtin1(name); |
| 7863 | if (x) |
| 7864 | return x; |
| 7865 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7866 | } |
| 7867 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 7868 | static void remove_nested_vars(void) |
| 7869 | { |
| 7870 | struct variable *cur; |
| 7871 | struct variable **cur_pp; |
| 7872 | |
| 7873 | cur_pp = &G.top_var; |
| 7874 | while ((cur = *cur_pp) != NULL) { |
| 7875 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7876 | cur_pp = &cur->next; |
| 7877 | continue; |
| 7878 | } |
| 7879 | /* Unexport */ |
| 7880 | if (cur->flg_export) { |
| 7881 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7882 | bb_unsetenv(cur->varstr); |
| 7883 | } |
| 7884 | /* Remove from global list */ |
| 7885 | *cur_pp = cur->next; |
| 7886 | /* Free */ |
| 7887 | if (!cur->max_len) { |
| 7888 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7889 | free(cur->varstr); |
| 7890 | } |
| 7891 | free(cur); |
| 7892 | } |
| 7893 | } |
| 7894 | |
| 7895 | static void enter_var_nest_level(void) |
| 7896 | { |
| 7897 | G.var_nest_level++; |
| 7898 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7899 | |
| 7900 | /* Try: f() { echo -n .; f; }; f |
| 7901 | * struct variable::var_nest_level is uint16_t, |
| 7902 | * thus limiting recursion to < 2^16. |
| 7903 | * In any case, with 8 Mbyte stack SEGV happens |
| 7904 | * not too long after 2^16 recursions anyway. |
| 7905 | */ |
| 7906 | if (G.var_nest_level > 0xff00) |
| 7907 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7908 | } |
| 7909 | |
| 7910 | static void leave_var_nest_level(void) |
| 7911 | { |
| 7912 | G.var_nest_level--; |
| 7913 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7914 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7915 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7916 | |
| 7917 | remove_nested_vars(); |
| 7918 | } |
| 7919 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7920 | #if ENABLE_HUSH_FUNCTIONS |
| 7921 | static struct function **find_function_slot(const char *name) |
| 7922 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7923 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7924 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7925 | |
| 7926 | while ((funcp = *funcpp) != NULL) { |
| 7927 | if (strcmp(name, funcp->name) == 0) { |
| 7928 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7929 | break; |
| 7930 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7931 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7932 | } |
| 7933 | return funcpp; |
| 7934 | } |
| 7935 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7936 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7937 | { |
| 7938 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7939 | return funcp; |
| 7940 | } |
| 7941 | |
| 7942 | /* Note: takes ownership on name ptr */ |
| 7943 | static struct function *new_function(char *name) |
| 7944 | { |
| 7945 | struct function **funcpp = find_function_slot(name); |
| 7946 | struct function *funcp = *funcpp; |
| 7947 | |
| 7948 | if (funcp != NULL) { |
| 7949 | struct command *cmd = funcp->parent_cmd; |
| 7950 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7951 | if (!cmd) { |
| 7952 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7953 | free(funcp->name); |
| 7954 | /* Note: if !funcp->body, do not free body_as_string! |
| 7955 | * This is a special case of "-F name body" function: |
| 7956 | * body_as_string was not malloced! */ |
| 7957 | if (funcp->body) { |
| 7958 | free_pipe_list(funcp->body); |
| 7959 | # if !BB_MMU |
| 7960 | free(funcp->body_as_string); |
| 7961 | # endif |
| 7962 | } |
| 7963 | } else { |
| 7964 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7965 | cmd->argv[0] = funcp->name; |
| 7966 | cmd->group = funcp->body; |
| 7967 | # if !BB_MMU |
| 7968 | cmd->group_as_string = funcp->body_as_string; |
| 7969 | # endif |
| 7970 | } |
| 7971 | } else { |
| 7972 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7973 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7974 | /*funcp->next = NULL;*/ |
| 7975 | } |
| 7976 | |
| 7977 | funcp->name = name; |
| 7978 | return funcp; |
| 7979 | } |
| 7980 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7981 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7982 | static void unset_func(const char *name) |
| 7983 | { |
| 7984 | struct function **funcpp = find_function_slot(name); |
| 7985 | struct function *funcp = *funcpp; |
| 7986 | |
| 7987 | if (funcp != NULL) { |
| 7988 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7989 | *funcpp = funcp->next; |
| 7990 | /* funcp is unlinked now, deleting it. |
| 7991 | * Note: if !funcp->body, the function was created by |
| 7992 | * "-F name body", do not free ->body_as_string |
| 7993 | * and ->name as they were not malloced. */ |
| 7994 | if (funcp->body) { |
| 7995 | free_pipe_list(funcp->body); |
| 7996 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7997 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7998 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7999 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8000 | } |
| 8001 | free(funcp); |
| 8002 | } |
| 8003 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8004 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8005 | |
| 8006 | # if BB_MMU |
| 8007 | #define exec_function(to_free, funcp, argv) \ |
| 8008 | exec_function(funcp, argv) |
| 8009 | # endif |
| 8010 | static void exec_function(char ***to_free, |
| 8011 | const struct function *funcp, |
| 8012 | char **argv) NORETURN; |
| 8013 | static void exec_function(char ***to_free, |
| 8014 | const struct function *funcp, |
| 8015 | char **argv) |
| 8016 | { |
| 8017 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8018 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8019 | |
| 8020 | argv[0] = G.global_argv[0]; |
| 8021 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8022 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8023 | |
| 8024 | // Example when we are here: "cmd | func" |
| 8025 | // func will run with saved-redirect fds open. |
| 8026 | // $ f() { echo /proc/self/fd/*; } |
| 8027 | // $ true | f |
| 8028 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 8029 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 8030 | // Same in script: |
| 8031 | // $ . ./SCRIPT |
| 8032 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 8033 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 8034 | // They are CLOEXEC so external programs won't see them, but |
| 8035 | // for "more correctness" we might want to close those extra fds here: |
| 8036 | //? close_saved_fds_and_FILE_fds(); |
| 8037 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8038 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8039 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8040 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8041 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 8042 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8043 | /* On MMU, funcp->body is always non-NULL */ |
| 8044 | n = run_list(funcp->body); |
| 8045 | fflush_all(); |
| 8046 | _exit(n); |
| 8047 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8048 | //? close_saved_fds_and_FILE_fds(); |
| 8049 | |
| 8050 | //TODO: check whether "true | func_with_return" works |
| 8051 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8052 | re_execute_shell(to_free, |
| 8053 | funcp->body_as_string, |
| 8054 | G.global_argv[0], |
| 8055 | argv + 1, |
| 8056 | NULL); |
| 8057 | # endif |
| 8058 | } |
| 8059 | |
| 8060 | static int run_function(const struct function *funcp, char **argv) |
| 8061 | { |
| 8062 | int rc; |
| 8063 | save_arg_t sv; |
| 8064 | smallint sv_flg; |
| 8065 | |
| 8066 | save_and_replace_G_args(&sv, argv); |
| 8067 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8068 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8069 | sv_flg = G_flag_return_in_progress; |
| 8070 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8071 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8072 | /* Make "local" variables properly shadow previous ones */ |
| 8073 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8074 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8075 | |
| 8076 | /* On MMU, funcp->body is always non-NULL */ |
| 8077 | # if !BB_MMU |
| 8078 | if (!funcp->body) { |
| 8079 | /* Function defined by -F */ |
| 8080 | parse_and_run_string(funcp->body_as_string); |
| 8081 | rc = G.last_exitcode; |
| 8082 | } else |
| 8083 | # endif |
| 8084 | { |
| 8085 | rc = run_list(funcp->body); |
| 8086 | } |
| 8087 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8088 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8089 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8090 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8091 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8092 | |
| 8093 | restore_G_args(&sv, argv); |
| 8094 | |
| 8095 | return rc; |
| 8096 | } |
| 8097 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 8098 | |
| 8099 | |
| 8100 | #if BB_MMU |
| 8101 | #define exec_builtin(to_free, x, argv) \ |
| 8102 | exec_builtin(x, argv) |
| 8103 | #else |
| 8104 | #define exec_builtin(to_free, x, argv) \ |
| 8105 | exec_builtin(to_free, argv) |
| 8106 | #endif |
| 8107 | static void exec_builtin(char ***to_free, |
| 8108 | const struct built_in_command *x, |
| 8109 | char **argv) NORETURN; |
| 8110 | static void exec_builtin(char ***to_free, |
| 8111 | const struct built_in_command *x, |
| 8112 | char **argv) |
| 8113 | { |
| 8114 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8115 | int rcode; |
| 8116 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8117 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8118 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8119 | fflush_all(); |
| 8120 | _exit(rcode); |
| 8121 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8122 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8123 | /* On NOMMU, we must never block! |
| 8124 | * Example: { sleep 99 | read line; } & echo Ok |
| 8125 | */ |
| 8126 | re_execute_shell(to_free, |
| 8127 | argv[0], |
| 8128 | G.global_argv[0], |
| 8129 | G.global_argv + 1, |
| 8130 | argv); |
| 8131 | #endif |
| 8132 | } |
| 8133 | |
| 8134 | |
| 8135 | static void execvp_or_die(char **argv) NORETURN; |
| 8136 | static void execvp_or_die(char **argv) |
| 8137 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8138 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8139 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8140 | /* Don't propagate SIG_IGN to the child */ |
| 8141 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8142 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8143 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8144 | e = 2; |
| 8145 | if (errno == EACCES) e = 126; |
| 8146 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8147 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8148 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8149 | } |
| 8150 | |
| 8151 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8152 | static void x_mode_print_optionally_squoted(const char *str) |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8153 | { |
| 8154 | unsigned len; |
| 8155 | const char *cp; |
| 8156 | |
| 8157 | cp = str; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8158 | |
| 8159 | /* the set of chars which-cause-string-to-be-squoted mimics bash */ |
| 8160 | /* test a char with: bash -c 'set -x; echo "CH"' */ |
| 8161 | if (str[strcspn(str, "\\\"'`$(){}[]<>;#&|~*?!^" |
| 8162 | " " "\001\002\003\004\005\006\007" |
| 8163 | "\010\011\012\013\014\015\016\017" |
| 8164 | "\020\021\022\023\024\025\026\027" |
| 8165 | "\030\031\032\033\034\035\036\037" |
| 8166 | ) |
| 8167 | ] == '\0' |
| 8168 | ) { |
| 8169 | /* string has no special chars */ |
| 8170 | x_mode_addstr(str); |
| 8171 | return; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8172 | } |
| 8173 | |
| 8174 | cp = str; |
| 8175 | for (;;) { |
| 8176 | /* print '....' up to EOL or first squote */ |
| 8177 | len = (int)(strchrnul(cp, '\'') - cp); |
| 8178 | if (len != 0) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8179 | x_mode_addchr('\''); |
| 8180 | x_mode_addblock(cp, len); |
| 8181 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8182 | cp += len; |
| 8183 | } |
| 8184 | if (*cp == '\0') |
| 8185 | break; |
| 8186 | /* string contains squote(s), print them as \' */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8187 | x_mode_addchr('\\'); |
| 8188 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8189 | cp++; |
| 8190 | } |
| 8191 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8192 | static void dump_cmd_in_x_mode(char **argv) |
| 8193 | { |
| 8194 | if (G_x_mode && argv) { |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 8195 | unsigned n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8196 | |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8197 | /* "+[+++...][ cmd...]\n\0" */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8198 | x_mode_prefix(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8199 | n = 0; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8200 | while (argv[n]) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8201 | x_mode_addchr(' '); |
| 8202 | if (argv[n][0] == '\0') { |
| 8203 | x_mode_addchr('\''); |
| 8204 | x_mode_addchr('\''); |
| 8205 | } else { |
| 8206 | x_mode_print_optionally_squoted(argv[n]); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8207 | } |
| 8208 | n++; |
| 8209 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8210 | x_mode_flush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8211 | } |
| 8212 | } |
| 8213 | #else |
| 8214 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 8215 | #endif |
| 8216 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8217 | #if ENABLE_HUSH_COMMAND |
| 8218 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 8219 | { |
| 8220 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8221 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8222 | if (!opt_vV) |
| 8223 | return; |
| 8224 | |
| 8225 | to_free = NULL; |
| 8226 | if (!explanation) { |
| 8227 | char *path = getenv("PATH"); |
| 8228 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8229 | if (!explanation) |
| 8230 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8231 | if (opt_vV != 'V') |
| 8232 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 8233 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8234 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8235 | free(to_free); |
| 8236 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8237 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8238 | } |
| 8239 | #else |
| 8240 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 8241 | #endif |
| 8242 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8243 | #if BB_MMU |
| 8244 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 8245 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 8246 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 8247 | pseudo_exec(command, argv_expanded) |
| 8248 | #endif |
| 8249 | |
| 8250 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 8251 | * Never returns. |
| 8252 | * Don't exit() here. If you don't exec, use _exit instead. |
| 8253 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8254 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8255 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8256 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8257 | char **argv, int assignment_cnt, |
| 8258 | char **argv_expanded) NORETURN; |
| 8259 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8260 | char **argv, int assignment_cnt, |
| 8261 | char **argv_expanded) |
| 8262 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8263 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8264 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8265 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8266 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 8267 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8268 | |
| 8269 | new_env = expand_assignments(argv, assignment_cnt); |
| 8270 | dump_cmd_in_x_mode(new_env); |
| 8271 | |
| 8272 | if (!argv[assignment_cnt]) { |
| 8273 | /* Case when we are here: ... | var=val | ... |
| 8274 | * (note that we do not exit early, i.e., do not optimize out |
| 8275 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 8276 | */ |
| 8277 | free_strings(new_env); |
| 8278 | _exit(EXIT_SUCCESS); |
| 8279 | } |
| 8280 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8281 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8282 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8283 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8284 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8285 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8286 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8287 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8288 | set_vars_and_save_old(new_env); |
| 8289 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8290 | |
| 8291 | if (argv_expanded) { |
| 8292 | argv = argv_expanded; |
| 8293 | } else { |
| 8294 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 8295 | #if !BB_MMU |
| 8296 | nommu_save->argv = argv; |
| 8297 | #endif |
| 8298 | } |
| 8299 | dump_cmd_in_x_mode(argv); |
| 8300 | |
| 8301 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8302 | if (strchr(argv[0], '/') != NULL) |
| 8303 | goto skip; |
| 8304 | #endif |
| 8305 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8306 | #if ENABLE_HUSH_FUNCTIONS |
| 8307 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8308 | funcp = find_function(argv[0]); |
| 8309 | if (funcp) |
| 8310 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8311 | #endif |
| 8312 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8313 | #if ENABLE_HUSH_COMMAND |
| 8314 | /* "command BAR": run BAR without looking it up among functions |
| 8315 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 8316 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 8317 | */ |
| 8318 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 8319 | char *p; |
| 8320 | |
| 8321 | argv++; |
| 8322 | p = *argv; |
| 8323 | if (p[0] != '-' || !p[1]) |
| 8324 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 8325 | |
| 8326 | for (;;) { |
| 8327 | p++; |
| 8328 | switch (*p) { |
| 8329 | case '\0': |
| 8330 | argv++; |
| 8331 | p = *argv; |
| 8332 | if (p[0] != '-' || !p[1]) |
| 8333 | goto after_opts; |
| 8334 | continue; /* next arg is also -opts, process it too */ |
| 8335 | case 'v': |
| 8336 | case 'V': |
| 8337 | opt_vV = *p; |
| 8338 | continue; |
| 8339 | default: |
| 8340 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 8341 | } |
| 8342 | } |
| 8343 | } |
| 8344 | after_opts: |
| 8345 | # if ENABLE_HUSH_FUNCTIONS |
| 8346 | if (opt_vV && find_function(argv[0])) |
| 8347 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 8348 | # endif |
| 8349 | #endif |
| 8350 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8351 | /* Check if the command matches any of the builtins. |
| 8352 | * Depending on context, this might be redundant. But it's |
| 8353 | * easier to waste a few CPU cycles than it is to figure out |
| 8354 | * if this is one of those cases. |
| 8355 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8356 | /* Why "BB_MMU ? :" difference in logic? - |
| 8357 | * On NOMMU, it is more expensive to re-execute shell |
| 8358 | * just in order to run echo or test builtin. |
| 8359 | * It's better to skip it here and run corresponding |
| 8360 | * non-builtin later. */ |
| 8361 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 8362 | if (x) { |
| 8363 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 8364 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8365 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8366 | |
| 8367 | #if ENABLE_FEATURE_SH_STANDALONE |
| 8368 | /* Check if the command matches any busybox applets */ |
| 8369 | { |
| 8370 | int a = find_applet_by_name(argv[0]); |
| 8371 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8372 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8373 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 8374 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8375 | /* Do not leak open fds from opened script files etc. |
| 8376 | * Testcase: interactive "ls -l /proc/self/fd" |
| 8377 | * should not show tty fd open. |
| 8378 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8379 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8380 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 8381 | //This casuses test failures in |
| 8382 | //redir_children_should_not_see_saved_fd_2.tests |
| 8383 | //redir_children_should_not_see_saved_fd_3.tests |
| 8384 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8385 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 8386 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 8387 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8388 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8389 | } |
| 8390 | # endif |
| 8391 | /* Re-exec ourselves */ |
| 8392 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8393 | /* Don't propagate SIG_IGN to the child */ |
| 8394 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8395 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8396 | execv(bb_busybox_exec_path, argv); |
| 8397 | /* If they called chroot or otherwise made the binary no longer |
| 8398 | * executable, fall through */ |
| 8399 | } |
| 8400 | } |
| 8401 | #endif |
| 8402 | |
| 8403 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8404 | skip: |
| 8405 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8406 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8407 | execvp_or_die(argv); |
| 8408 | } |
| 8409 | |
| 8410 | /* Called after [v]fork() in run_pipe |
| 8411 | */ |
| 8412 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8413 | struct command *command, |
| 8414 | char **argv_expanded) NORETURN; |
| 8415 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8416 | struct command *command, |
| 8417 | char **argv_expanded) |
| 8418 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 8419 | #if ENABLE_HUSH_FUNCTIONS |
| 8420 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8421 | /* Ignore funcdefs in pipes: |
| 8422 | * true | f() { cmd } |
| 8423 | */ |
| 8424 | _exit(0); |
| 8425 | } |
| 8426 | #endif |
| 8427 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8428 | if (command->argv) { |
| 8429 | pseudo_exec_argv(nommu_save, command->argv, |
| 8430 | command->assignment_cnt, argv_expanded); |
| 8431 | } |
| 8432 | |
| 8433 | if (command->group) { |
| 8434 | /* Cases when we are here: |
| 8435 | * ( list ) |
| 8436 | * { list } & |
| 8437 | * ... | ( list ) | ... |
| 8438 | * ... | { list } | ... |
| 8439 | */ |
| 8440 | #if BB_MMU |
| 8441 | int rcode; |
| 8442 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 8443 | reset_traps_to_defaults(); |
| 8444 | rcode = run_list(command->group); |
| 8445 | /* OK to leak memory by not calling free_pipe_list, |
| 8446 | * since this process is about to exit */ |
| 8447 | _exit(rcode); |
| 8448 | #else |
| 8449 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 8450 | command->group_as_string, |
| 8451 | G.global_argv[0], |
| 8452 | G.global_argv + 1, |
| 8453 | NULL); |
| 8454 | #endif |
| 8455 | } |
| 8456 | |
| 8457 | /* Case when we are here: ... | >file */ |
| 8458 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 8459 | _exit(EXIT_SUCCESS); |
| 8460 | } |
| 8461 | |
| 8462 | #if ENABLE_HUSH_JOB |
| 8463 | static const char *get_cmdtext(struct pipe *pi) |
| 8464 | { |
| 8465 | char **argv; |
| 8466 | char *p; |
| 8467 | int len; |
| 8468 | |
| 8469 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 8470 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 8471 | * On subsequent bg argv is trashed, but we won't use it */ |
| 8472 | if (pi->cmdtext) |
| 8473 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8474 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8475 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8476 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8477 | pi->cmdtext = xzalloc(1); |
| 8478 | return pi->cmdtext; |
| 8479 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8480 | len = 0; |
| 8481 | do { |
| 8482 | len += strlen(*argv) + 1; |
| 8483 | } while (*++argv); |
| 8484 | p = xmalloc(len); |
| 8485 | pi->cmdtext = p; |
| 8486 | argv = pi->cmds[0].argv; |
| 8487 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8488 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8489 | *p++ = ' '; |
| 8490 | } while (*++argv); |
| 8491 | p[-1] = '\0'; |
| 8492 | return pi->cmdtext; |
| 8493 | } |
| 8494 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8495 | static void remove_job_from_table(struct pipe *pi) |
| 8496 | { |
| 8497 | struct pipe *prev_pipe; |
| 8498 | |
| 8499 | if (pi == G.job_list) { |
| 8500 | G.job_list = pi->next; |
| 8501 | } else { |
| 8502 | prev_pipe = G.job_list; |
| 8503 | while (prev_pipe->next != pi) |
| 8504 | prev_pipe = prev_pipe->next; |
| 8505 | prev_pipe->next = pi->next; |
| 8506 | } |
| 8507 | G.last_jobid = 0; |
| 8508 | if (G.job_list) |
| 8509 | G.last_jobid = G.job_list->jobid; |
| 8510 | } |
| 8511 | |
| 8512 | static void delete_finished_job(struct pipe *pi) |
| 8513 | { |
| 8514 | remove_job_from_table(pi); |
| 8515 | free_pipe(pi); |
| 8516 | } |
| 8517 | |
| 8518 | static void clean_up_last_dead_job(void) |
| 8519 | { |
| 8520 | if (G.job_list && !G.job_list->alive_cmds) |
| 8521 | delete_finished_job(G.job_list); |
| 8522 | } |
| 8523 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8524 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8525 | { |
| 8526 | struct pipe *job, **jobp; |
| 8527 | int i; |
| 8528 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8529 | clean_up_last_dead_job(); |
| 8530 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8531 | /* Find the end of the list, and find next job ID to use */ |
| 8532 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8533 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8534 | while ((job = *jobp) != NULL) { |
| 8535 | if (job->jobid > i) |
| 8536 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8537 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8538 | } |
| 8539 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8540 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8541 | /* Create a new job struct at the end */ |
| 8542 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8543 | job->next = NULL; |
| 8544 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 8545 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 8546 | for (i = 0; i < pi->num_cmds; i++) { |
| 8547 | job->cmds[i].pid = pi->cmds[i].pid; |
| 8548 | /* all other fields are not used and stay zero */ |
| 8549 | } |
| 8550 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 8551 | |
| 8552 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 8553 | 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] | 8554 | G.last_jobid = job->jobid; |
| 8555 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8556 | #endif /* JOB */ |
| 8557 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8558 | static int job_exited_or_stopped(struct pipe *pi) |
| 8559 | { |
| 8560 | int rcode, i; |
| 8561 | |
| 8562 | if (pi->alive_cmds != pi->stopped_cmds) |
| 8563 | return -1; |
| 8564 | |
| 8565 | /* All processes in fg pipe have exited or stopped */ |
| 8566 | rcode = 0; |
| 8567 | i = pi->num_cmds; |
| 8568 | while (--i >= 0) { |
| 8569 | rcode = pi->cmds[i].cmd_exitcode; |
| 8570 | /* usually last process gives overall exitstatus, |
| 8571 | * but with "set -o pipefail", last *failed* process does */ |
| 8572 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 8573 | break; |
| 8574 | } |
| 8575 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8576 | return rcode; |
| 8577 | } |
| 8578 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8579 | 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] | 8580 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8581 | #if ENABLE_HUSH_JOB |
| 8582 | struct pipe *pi; |
| 8583 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8584 | int i, dead; |
| 8585 | |
| 8586 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 8587 | |
| 8588 | #if DEBUG_JOBS |
| 8589 | if (WIFSTOPPED(status)) |
| 8590 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8591 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8592 | if (WIFSIGNALED(status)) |
| 8593 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8594 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8595 | if (WIFEXITED(status)) |
| 8596 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8597 | childpid, WEXITSTATUS(status)); |
| 8598 | #endif |
| 8599 | /* Were we asked to wait for a fg pipe? */ |
| 8600 | if (fg_pipe) { |
| 8601 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8602 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8603 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8604 | int rcode; |
| 8605 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8606 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8607 | if (fg_pipe->cmds[i].pid != childpid) |
| 8608 | continue; |
| 8609 | if (dead) { |
| 8610 | int ex; |
| 8611 | fg_pipe->cmds[i].pid = 0; |
| 8612 | fg_pipe->alive_cmds--; |
| 8613 | ex = WEXITSTATUS(status); |
| 8614 | /* bash prints killer signal's name for *last* |
| 8615 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8616 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8617 | */ |
| 8618 | if (WIFSIGNALED(status)) { |
| 8619 | int sig = WTERMSIG(status); |
| 8620 | if (i == fg_pipe->num_cmds-1) |
| 8621 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 8622 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 8623 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8624 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8625 | * Maybe we need to use sig | 128? */ |
| 8626 | ex = sig + 128; |
| 8627 | } |
| 8628 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8629 | } else { |
| 8630 | fg_pipe->stopped_cmds++; |
| 8631 | } |
| 8632 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8633 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8634 | rcode = job_exited_or_stopped(fg_pipe); |
| 8635 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8636 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8637 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8638 | * and "killall -STOP cat" */ |
| 8639 | if (G_interactive_fd) { |
| 8640 | #if ENABLE_HUSH_JOB |
| 8641 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8642 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8643 | #endif |
| 8644 | return rcode; |
| 8645 | } |
| 8646 | if (fg_pipe->alive_cmds == 0) |
| 8647 | return rcode; |
| 8648 | } |
| 8649 | /* There are still running processes in the fg_pipe */ |
| 8650 | return -1; |
| 8651 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8652 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | #if ENABLE_HUSH_JOB |
| 8656 | /* We were asked to wait for bg or orphaned children */ |
| 8657 | /* No need to remember exitcode in this case */ |
| 8658 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8659 | for (i = 0; i < pi->num_cmds; i++) { |
| 8660 | if (pi->cmds[i].pid == childpid) |
| 8661 | goto found_pi_and_prognum; |
| 8662 | } |
| 8663 | } |
| 8664 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8665 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8666 | return -1; /* this wasn't a process from fg_pipe */ |
| 8667 | |
| 8668 | found_pi_and_prognum: |
| 8669 | if (dead) { |
| 8670 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8671 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8672 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8673 | rcode = 128 + WTERMSIG(status); |
| 8674 | pi->cmds[i].cmd_exitcode = rcode; |
| 8675 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8676 | G.last_bg_pid_exitcode = rcode; |
| 8677 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8678 | pi->alive_cmds--; |
| 8679 | if (!pi->alive_cmds) { |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8680 | #if ENABLE_HUSH_BASH_COMPAT |
| 8681 | G.dead_job_exitcode = job_exited_or_stopped(pi); |
| 8682 | #endif |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8683 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8684 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8685 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8686 | delete_finished_job(pi); |
| 8687 | } else { |
| 8688 | /* |
| 8689 | * bash deletes finished jobs from job table only in interactive mode, |
| 8690 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8691 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8692 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8693 | * We only retain one "dead" job, if it's the single job on the list. |
| 8694 | * This covers most of real-world scenarios where this is useful. |
| 8695 | */ |
| 8696 | if (pi != G.job_list) |
| 8697 | delete_finished_job(pi); |
| 8698 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8699 | } |
| 8700 | } else { |
| 8701 | /* child stopped */ |
| 8702 | pi->stopped_cmds++; |
| 8703 | } |
| 8704 | #endif |
| 8705 | return -1; /* this wasn't a process from fg_pipe */ |
| 8706 | } |
| 8707 | |
| 8708 | /* Check to see if any processes have exited -- if they have, |
| 8709 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8710 | * |
| 8711 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8712 | * Return its exitcode or zero if stopped. |
| 8713 | * |
| 8714 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8715 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8716 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8717 | * or 0 if no children changed status. |
| 8718 | * |
| 8719 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8720 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8721 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8722 | */ |
| 8723 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8724 | { |
| 8725 | int attributes; |
| 8726 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8727 | int rcode = 0; |
| 8728 | |
| 8729 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8730 | |
| 8731 | attributes = WUNTRACED; |
| 8732 | if (fg_pipe == NULL) |
| 8733 | attributes |= WNOHANG; |
| 8734 | |
| 8735 | errno = 0; |
| 8736 | #if ENABLE_HUSH_FAST |
| 8737 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8738 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8739 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8740 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8741 | /* Avoid doing waitpid syscall if possible */ |
| 8742 | if (!G.we_have_children) { |
| 8743 | errno = ECHILD; |
| 8744 | return -1; |
| 8745 | } |
| 8746 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8747 | /* We have children, but they did not exit |
| 8748 | * or stop yet (we saw no SIGCHLD) */ |
| 8749 | return 0; |
| 8750 | } |
| 8751 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8752 | } |
| 8753 | #endif |
| 8754 | |
| 8755 | /* Do we do this right? |
| 8756 | * bash-3.00# sleep 20 | false |
| 8757 | * <ctrl-Z pressed> |
| 8758 | * [3]+ Stopped sleep 20 | false |
| 8759 | * bash-3.00# echo $? |
| 8760 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8761 | * [hush 1.14.0: yes we do it right] |
| 8762 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8763 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8764 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8765 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8766 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8767 | i = G.count_SIGCHLD; |
| 8768 | #endif |
| 8769 | childpid = waitpid(-1, &status, attributes); |
| 8770 | if (childpid <= 0) { |
| 8771 | if (childpid && errno != ECHILD) |
| 8772 | bb_perror_msg("waitpid"); |
| 8773 | #if ENABLE_HUSH_FAST |
| 8774 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8775 | G.we_have_children = (childpid == 0); |
| 8776 | G.handled_SIGCHLD = i; |
| 8777 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8778 | } |
| 8779 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8780 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8781 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8782 | break; |
| 8783 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8784 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8785 | if (rcode >= 0) { |
| 8786 | /* fg_pipe exited or stopped */ |
| 8787 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8788 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8789 | if (childpid == waitfor_pid) { /* "wait PID" */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8790 | 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] | 8791 | rcode = WEXITSTATUS(status); |
| 8792 | if (WIFSIGNALED(status)) |
| 8793 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8794 | if (WIFSTOPPED(status)) |
| 8795 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8796 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8797 | rcode++; |
| 8798 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8799 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8800 | #if ENABLE_HUSH_BASH_COMPAT |
| 8801 | if (-1 == waitfor_pid /* "wait -n" (wait for any one job) */ |
| 8802 | && G.dead_job_exitcode >= 0 /* some job did finish */ |
| 8803 | ) { |
| 8804 | debug_printf_exec("waitfor_pid:-1\n"); |
| 8805 | rcode = G.dead_job_exitcode + 1; |
| 8806 | break; |
| 8807 | } |
| 8808 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8809 | /* This wasn't one of our processes, or */ |
| 8810 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8811 | } /* while (waitpid succeeds)... */ |
| 8812 | |
| 8813 | return rcode; |
| 8814 | } |
| 8815 | |
| 8816 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8817 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8818 | { |
| 8819 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8820 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8821 | if (G_saved_tty_pgrp) { |
| 8822 | /* Job finished, move the shell to the foreground */ |
| 8823 | p = getpgrp(); /* our process group id */ |
| 8824 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8825 | tcsetpgrp(G_interactive_fd, p); |
| 8826 | } |
| 8827 | return rcode; |
| 8828 | } |
| 8829 | #endif |
| 8830 | |
| 8831 | /* Start all the jobs, but don't wait for anything to finish. |
| 8832 | * See checkjobs(). |
| 8833 | * |
| 8834 | * Return code is normally -1, when the caller has to wait for children |
| 8835 | * to finish to determine the exit status of the pipe. If the pipe |
| 8836 | * is a simple builtin command, however, the action is done by the |
| 8837 | * time run_pipe returns, and the exit code is provided as the |
| 8838 | * return value. |
| 8839 | * |
| 8840 | * Returns -1 only if started some children. IOW: we have to |
| 8841 | * mask out retvals of builtins etc with 0xff! |
| 8842 | * |
| 8843 | * The only case when we do not need to [v]fork is when the pipe |
| 8844 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8845 | * cmd ; ... { list } ; ... |
| 8846 | * cmd && ... { list } && ... |
| 8847 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8848 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8849 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8850 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8851 | * |
| 8852 | * Cases when we must fork: |
| 8853 | * non-single: cmd | cmd |
| 8854 | * backgrounded: cmd & { list } & |
| 8855 | * subshell: ( list ) [&] |
| 8856 | */ |
| 8857 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 8858 | #define redirect_and_varexp_helper(command, sqp, argv_expanded) \ |
| 8859 | redirect_and_varexp_helper(command, sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8860 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8861 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8862 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8863 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8864 | char **argv_expanded) |
| 8865 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8866 | /* Assignments occur before redirects. Try: |
| 8867 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8868 | */ |
| 8869 | |
| 8870 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8871 | dump_cmd_in_x_mode(new_env); |
| 8872 | dump_cmd_in_x_mode(argv_expanded); |
| 8873 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8874 | set_vars_and_save_old(new_env); |
| 8875 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8876 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8877 | } |
| 8878 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8879 | { |
| 8880 | static const char *const null_ptr = NULL; |
| 8881 | |
| 8882 | int cmd_no; |
| 8883 | int next_infd; |
| 8884 | struct command *command; |
| 8885 | char **argv_expanded; |
| 8886 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8887 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8888 | int rcode; |
| 8889 | |
| 8890 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8891 | debug_enter(); |
| 8892 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8893 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8894 | * Result should be 3 lines: q w e, qwe, q w e |
| 8895 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8896 | if (G.ifs_whitespace != G.ifs) |
| 8897 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8898 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8899 | if (G.ifs) { |
| 8900 | char *p; |
| 8901 | G.ifs_whitespace = (char*)G.ifs; |
| 8902 | p = skip_whitespace(G.ifs); |
| 8903 | if (*p) { |
| 8904 | /* Not all $IFS is whitespace */ |
| 8905 | char *d; |
| 8906 | int len = p - G.ifs; |
| 8907 | p = skip_non_whitespace(p); |
| 8908 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 8909 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 8910 | while (*p) { |
| 8911 | if (isspace(*p)) |
| 8912 | *d++ = *p; |
| 8913 | p++; |
| 8914 | } |
| 8915 | *d = '\0'; |
| 8916 | } |
| 8917 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8918 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8919 | G.ifs_whitespace = (char*)G.ifs; |
| 8920 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8921 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8922 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8923 | pi->stopped_cmds = 0; |
| 8924 | command = &pi->cmds[0]; |
| 8925 | argv_expanded = NULL; |
| 8926 | |
| 8927 | if (pi->num_cmds != 1 |
| 8928 | || pi->followup == PIPE_BG |
| 8929 | || command->cmd_type == CMD_SUBSHELL |
| 8930 | ) { |
| 8931 | goto must_fork; |
| 8932 | } |
| 8933 | |
| 8934 | pi->alive_cmds = 1; |
| 8935 | |
| 8936 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8937 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8938 | |
| 8939 | if (command->group) { |
| 8940 | #if ENABLE_HUSH_FUNCTIONS |
| 8941 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8942 | /* "executing" func () { list } */ |
| 8943 | struct function *funcp; |
| 8944 | |
| 8945 | funcp = new_function(command->argv[0]); |
| 8946 | /* funcp->name is already set to argv[0] */ |
| 8947 | funcp->body = command->group; |
| 8948 | # if !BB_MMU |
| 8949 | funcp->body_as_string = command->group_as_string; |
| 8950 | command->group_as_string = NULL; |
| 8951 | # endif |
| 8952 | command->group = NULL; |
| 8953 | command->argv[0] = NULL; |
| 8954 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8955 | funcp->parent_cmd = command; |
| 8956 | command->child_func = funcp; |
| 8957 | |
| 8958 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8959 | debug_leave(); |
| 8960 | return EXIT_SUCCESS; |
| 8961 | } |
| 8962 | #endif |
| 8963 | /* { list } */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 8964 | debug_printf_exec("non-subshell group\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8965 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8966 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8967 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8968 | //FIXME: we need to pass squirrel down into run_list() |
| 8969 | //for SH_STANDALONE case, or else this construct: |
| 8970 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8971 | //has no way of closing saved fd#1 for "find", |
| 8972 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8973 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8974 | rcode = run_list(command->group) & 0xff; |
| 8975 | } |
| 8976 | restore_redirects(squirrel); |
| 8977 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8978 | debug_leave(); |
| 8979 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8980 | return rcode; |
| 8981 | } |
| 8982 | |
| 8983 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8984 | { |
| 8985 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8986 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8987 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8988 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8989 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8990 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8991 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8992 | if (G.lineno_var) |
| 8993 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8994 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8995 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8996 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8997 | /* Assignments, but no command. |
| 8998 | * Ensure redirects take effect (that is, create files). |
| 8999 | * Try "a=t >file" |
| 9000 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9001 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9002 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9003 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9004 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9005 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9006 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9007 | /* Set shell variables */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9008 | i = 0; |
| 9009 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9010 | char *p = expand_string_to_string(argv[i], |
| 9011 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9012 | /*unbackslash:*/ 1 |
| 9013 | ); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9014 | #if ENABLE_HUSH_MODE_X |
| 9015 | if (G_x_mode) { |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9016 | char *eq; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9017 | if (i == 0) |
| 9018 | x_mode_prefix(); |
| 9019 | x_mode_addchr(' '); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9020 | eq = strchrnul(p, '='); |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9021 | if (*eq) eq++; |
| 9022 | x_mode_addblock(p, (eq - p)); |
| 9023 | x_mode_print_optionally_squoted(eq); |
| 9024 | x_mode_flush(); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9025 | } |
| 9026 | #endif |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9027 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9028 | if (set_local_var(p, /*flag:*/ 0)) { |
| 9029 | /* assignment to readonly var / putenv error? */ |
| 9030 | rcode = 1; |
| 9031 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9032 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9033 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9034 | /* Redirect error sets $? to 1. Otherwise, |
| 9035 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9036 | * Else, clear $?: |
| 9037 | * false; q=`exit 2`; echo $? - should print 2 |
| 9038 | * false; x=1; echo $? - should print 0 |
| 9039 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 9040 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9041 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9042 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9043 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9044 | debug_leave(); |
| 9045 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 9046 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9047 | } |
| 9048 | |
| 9049 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 9050 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9051 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9052 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9053 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9054 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9055 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9056 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9057 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9058 | if (!argv_expanded[0]) { |
| 9059 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9060 | /* `false` still has to set exitcode 1 */ |
| 9061 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9062 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9063 | } |
| 9064 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9065 | old_vars = NULL; |
| 9066 | sv_shadowed = G.shadowed_vars_pp; |
| 9067 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9068 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9069 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 9070 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 9071 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9072 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9073 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9074 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 9075 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9076 | /* |
| 9077 | * Variable assignments are executed, but then "forgotten": |
| 9078 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 9079 | * sleeps, but prints nothing. |
| 9080 | */ |
| 9081 | enter_var_nest_level(); |
| 9082 | G.shadowed_vars_pp = &old_vars; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 9083 | rcode = redirect_and_varexp_helper(command, |
| 9084 | /*squirrel:*/ ERR_PTR, |
| 9085 | argv_expanded |
| 9086 | ); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9087 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9088 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9089 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9090 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9091 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9092 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9093 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9094 | * a=b true; env | grep ^a= |
| 9095 | */ |
| 9096 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9097 | /* Collect all variables "shadowed" by helper |
| 9098 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 9099 | * into old_vars list: |
| 9100 | */ |
| 9101 | G.shadowed_vars_pp = &old_vars; |
| 9102 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9103 | if (rcode == 0) { |
| 9104 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9105 | /* Do not collect *to old_vars list* vars shadowed |
| 9106 | * by e.g. "local VAR" builtin (collect them |
| 9107 | * in the previously nested list instead): |
| 9108 | * don't want them to be restored immediately |
| 9109 | * after "local" completes. |
| 9110 | */ |
| 9111 | G.shadowed_vars_pp = sv_shadowed; |
| 9112 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9113 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 9114 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9115 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9116 | rcode = x->b_function(argv_expanded) & 0xff; |
| 9117 | fflush_all(); |
| 9118 | } |
| 9119 | #if ENABLE_HUSH_FUNCTIONS |
| 9120 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9121 | debug_printf_exec(": function '%s' '%s'...\n", |
| 9122 | funcp->name, argv_expanded[1]); |
| 9123 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9124 | /* |
| 9125 | * But do collect *to old_vars list* vars shadowed |
| 9126 | * within function execution. To that end, restore |
| 9127 | * this pointer _after_ function run: |
| 9128 | */ |
| 9129 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9130 | } |
| 9131 | #endif |
| 9132 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9133 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9134 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9135 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9136 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 9137 | goto must_fork; |
| 9138 | |
| 9139 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9140 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 9141 | G.shadowed_vars_pp = &old_vars; |
| 9142 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 9143 | G.shadowed_vars_pp = sv_shadowed; |
| 9144 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9145 | if (rcode == 0) { |
| 9146 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 9147 | argv_expanded[0], argv_expanded[1]); |
| 9148 | /* |
| 9149 | * Note: signals (^C) can't interrupt here. |
| 9150 | * We remember them and they will be acted upon |
| 9151 | * after applet returns. |
| 9152 | * This makes applets which can run for a long time |
| 9153 | * and/or wait for user input ineligible for NOFORK: |
| 9154 | * for example, "yes" or "rm" (rm -i waits for input). |
| 9155 | */ |
| 9156 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9157 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 9158 | } else |
| 9159 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9160 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9161 | restore_redirects(squirrel); |
| 9162 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9163 | leave_var_nest_level(); |
| 9164 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9165 | |
| 9166 | /* |
| 9167 | * Try "usleep 99999999" + ^C + "echo $?" |
| 9168 | * with FEATURE_SH_NOFORK=y. |
| 9169 | */ |
| 9170 | if (!funcp) { |
| 9171 | /* It was builtin or nofork. |
| 9172 | * if this would be a real fork/execed program, |
| 9173 | * it should have died if a fatal sig was received. |
| 9174 | * But OTOH, there was no separate process, |
| 9175 | * the sig was sent to _shell_, not to non-existing |
| 9176 | * child. |
| 9177 | * Let's just handle ^C only, this one is obvious: |
| 9178 | * we aren't ok with exitcode 0 when ^C was pressed |
| 9179 | * during builtin/nofork. |
| 9180 | */ |
| 9181 | if (sigismember(&G.pending_set, SIGINT)) |
| 9182 | rcode = 128 + SIGINT; |
| 9183 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9184 | free(argv_expanded); |
| 9185 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9186 | debug_leave(); |
| 9187 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 9188 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9189 | } |
| 9190 | |
| 9191 | must_fork: |
| 9192 | /* NB: argv_expanded may already be created, and that |
| 9193 | * might include `cmd` runs! Do not rerun it! We *must* |
| 9194 | * use argv_expanded if it's non-NULL */ |
| 9195 | |
| 9196 | /* Going to fork a child per each pipe member */ |
| 9197 | pi->alive_cmds = 0; |
| 9198 | next_infd = 0; |
| 9199 | |
| 9200 | cmd_no = 0; |
| 9201 | while (cmd_no < pi->num_cmds) { |
| 9202 | struct fd_pair pipefds; |
| 9203 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9204 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9205 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9206 | nommu_save.old_vars = NULL; |
| 9207 | nommu_save.argv = NULL; |
| 9208 | nommu_save.argv_from_re_execing = NULL; |
| 9209 | #endif |
| 9210 | command = &pi->cmds[cmd_no]; |
| 9211 | cmd_no++; |
| 9212 | if (command->argv) { |
| 9213 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 9214 | command->argv[0], command->argv[1]); |
| 9215 | } else { |
| 9216 | debug_printf_exec(": pipe member with no argv\n"); |
| 9217 | } |
| 9218 | |
| 9219 | /* pipes are inserted between pairs of commands */ |
| 9220 | pipefds.rd = 0; |
| 9221 | pipefds.wr = 1; |
| 9222 | if (cmd_no < pi->num_cmds) |
| 9223 | xpiped_pair(pipefds); |
| 9224 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9225 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 9226 | if (G.lineno_var) |
| 9227 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 9228 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9229 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9230 | command->pid = BB_MMU ? fork() : vfork(); |
| 9231 | if (!command->pid) { /* child */ |
| 9232 | #if ENABLE_HUSH_JOB |
| 9233 | disable_restore_tty_pgrp_on_exit(); |
| 9234 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 9235 | |
| 9236 | /* Every child adds itself to new process group |
| 9237 | * with pgid == pid_of_first_child_in_pipe */ |
| 9238 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9239 | pid_t pgrp; |
| 9240 | pgrp = pi->pgrp; |
| 9241 | if (pgrp < 0) /* true for 1st process only */ |
| 9242 | pgrp = getpid(); |
| 9243 | if (setpgid(0, pgrp) == 0 |
| 9244 | && pi->followup != PIPE_BG |
| 9245 | && G_saved_tty_pgrp /* we have ctty */ |
| 9246 | ) { |
| 9247 | /* We do it in *every* child, not just first, |
| 9248 | * to avoid races */ |
| 9249 | tcsetpgrp(G_interactive_fd, pgrp); |
| 9250 | } |
| 9251 | } |
| 9252 | #endif |
| 9253 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 9254 | /* 1st cmd in backgrounded pipe |
| 9255 | * should have its stdin /dev/null'ed */ |
| 9256 | close(0); |
| 9257 | if (open(bb_dev_null, O_RDONLY)) |
| 9258 | xopen("/", O_RDONLY); |
| 9259 | } else { |
| 9260 | xmove_fd(next_infd, 0); |
| 9261 | } |
| 9262 | xmove_fd(pipefds.wr, 1); |
| 9263 | if (pipefds.rd > 1) |
| 9264 | close(pipefds.rd); |
| 9265 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9266 | * and the pipe fd (fd#1) is available for dup'ing: |
| 9267 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 9268 | * of cmd1 goes into pipe. |
| 9269 | */ |
| 9270 | if (setup_redirects(command, NULL)) { |
| 9271 | /* Happens when redir file can't be opened: |
| 9272 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 9273 | * FOO |
| 9274 | * hush: can't open '/qwe/rty': No such file or directory |
| 9275 | * BAZ |
| 9276 | * (echo BAR is not executed, it hits _exit(1) below) |
| 9277 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9278 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9279 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9280 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9281 | /* Stores to nommu_save list of env vars putenv'ed |
| 9282 | * (NOMMU, on MMU we don't need that) */ |
| 9283 | /* cast away volatility... */ |
| 9284 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 9285 | /* pseudo_exec() does not return */ |
| 9286 | } |
| 9287 | |
| 9288 | /* parent or error */ |
| 9289 | #if ENABLE_HUSH_FAST |
| 9290 | G.count_SIGCHLD++; |
| 9291 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 9292 | #endif |
| 9293 | enable_restore_tty_pgrp_on_exit(); |
| 9294 | #if !BB_MMU |
| 9295 | /* Clean up after vforked child */ |
| 9296 | free(nommu_save.argv); |
| 9297 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9298 | G.var_nest_level = sv_var_nest_level; |
| 9299 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9300 | add_vars(nommu_save.old_vars); |
| 9301 | #endif |
| 9302 | free(argv_expanded); |
| 9303 | argv_expanded = NULL; |
| 9304 | if (command->pid < 0) { /* [v]fork failed */ |
| 9305 | /* Clearly indicate, was it fork or vfork */ |
| 9306 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 9307 | } else { |
| 9308 | pi->alive_cmds++; |
| 9309 | #if ENABLE_HUSH_JOB |
| 9310 | /* Second and next children need to know pid of first one */ |
| 9311 | if (pi->pgrp < 0) |
| 9312 | pi->pgrp = command->pid; |
| 9313 | #endif |
| 9314 | } |
| 9315 | |
| 9316 | if (cmd_no > 1) |
| 9317 | close(next_infd); |
| 9318 | if (cmd_no < pi->num_cmds) |
| 9319 | close(pipefds.wr); |
| 9320 | /* Pass read (output) pipe end to next iteration */ |
| 9321 | next_infd = pipefds.rd; |
| 9322 | } |
| 9323 | |
| 9324 | if (!pi->alive_cmds) { |
| 9325 | debug_leave(); |
| 9326 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 9327 | return 1; |
| 9328 | } |
| 9329 | |
| 9330 | debug_leave(); |
| 9331 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 9332 | return -1; |
| 9333 | } |
| 9334 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9335 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 9336 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 9337 | static int run_list(struct pipe *pi) |
| 9338 | { |
| 9339 | #if ENABLE_HUSH_CASE |
| 9340 | char *case_word = NULL; |
| 9341 | #endif |
| 9342 | #if ENABLE_HUSH_LOOPS |
| 9343 | struct pipe *loop_top = NULL; |
| 9344 | char **for_lcur = NULL; |
| 9345 | char **for_list = NULL; |
| 9346 | #endif |
| 9347 | smallint last_followup; |
| 9348 | smalluint rcode; |
| 9349 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 9350 | smalluint cond_code = 0; |
| 9351 | #else |
| 9352 | enum { cond_code = 0 }; |
| 9353 | #endif |
| 9354 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 9355 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9356 | smallint last_rword; /* ditto */ |
| 9357 | #endif |
| 9358 | |
| 9359 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 9360 | debug_enter(); |
| 9361 | |
| 9362 | #if ENABLE_HUSH_LOOPS |
| 9363 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 9364 | { |
| 9365 | struct pipe *cpipe; |
| 9366 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 9367 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 9368 | continue; |
| 9369 | /* current word is FOR or IN (BOLD in comments below) */ |
| 9370 | if (cpipe->next == NULL) { |
| 9371 | syntax_error("malformed for"); |
| 9372 | debug_leave(); |
| 9373 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9374 | return 1; |
| 9375 | } |
| 9376 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 9377 | if (cpipe->next->res_word == RES_DO) |
| 9378 | continue; |
| 9379 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 9380 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 9381 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 9382 | ) { |
| 9383 | syntax_error("malformed for"); |
| 9384 | debug_leave(); |
| 9385 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9386 | return 1; |
| 9387 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9388 | } |
| 9389 | } |
| 9390 | #endif |
| 9391 | |
| 9392 | /* Past this point, all code paths should jump to ret: label |
| 9393 | * in order to return, no direct "return" statements please. |
| 9394 | * This helps to ensure that no memory is leaked. */ |
| 9395 | |
| 9396 | #if ENABLE_HUSH_JOB |
| 9397 | G.run_list_level++; |
| 9398 | #endif |
| 9399 | |
| 9400 | #if HAS_KEYWORDS |
| 9401 | rword = RES_NONE; |
| 9402 | last_rword = RES_XXXX; |
| 9403 | #endif |
| 9404 | last_followup = PIPE_SEQ; |
| 9405 | rcode = G.last_exitcode; |
| 9406 | |
| 9407 | /* Go through list of pipes, (maybe) executing them. */ |
| 9408 | 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] | 9409 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9410 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9411 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9412 | if (G.flag_SIGINT) |
| 9413 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9414 | if (G_flag_return_in_progress == 1) |
| 9415 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9416 | |
| 9417 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 9418 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 9419 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9420 | |
| 9421 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9422 | if ( |
| 9423 | #if ENABLE_HUSH_IF |
| 9424 | rword == RES_IF || rword == RES_ELIF || |
| 9425 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9426 | pi->followup != PIPE_SEQ |
| 9427 | ) { |
| 9428 | G.errexit_depth++; |
| 9429 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9430 | #if ENABLE_HUSH_LOOPS |
| 9431 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 9432 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 9433 | ) { |
| 9434 | /* start of a loop: remember where loop starts */ |
| 9435 | loop_top = pi; |
| 9436 | G.depth_of_loop++; |
| 9437 | } |
| 9438 | #endif |
| 9439 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 9440 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 9441 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 9442 | || (rcode != 0 && last_followup == PIPE_AND) |
| 9443 | ) { |
| 9444 | /* It is "<true> || CMD" or "<false> && CMD" |
| 9445 | * and we should not execute CMD */ |
| 9446 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 9447 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9448 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9449 | } |
| 9450 | } |
| 9451 | last_followup = pi->followup; |
| 9452 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 9453 | #if ENABLE_HUSH_IF |
| 9454 | if (cond_code) { |
| 9455 | if (rword == RES_THEN) { |
| 9456 | /* if false; then ... fi has exitcode 0! */ |
| 9457 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9458 | /* "if <false> THEN cmd": skip cmd */ |
| 9459 | continue; |
| 9460 | } |
| 9461 | } else { |
| 9462 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 9463 | /* "if <true> then ... ELSE/ELIF cmd": |
| 9464 | * skip cmd and all following ones */ |
| 9465 | break; |
| 9466 | } |
| 9467 | } |
| 9468 | #endif |
| 9469 | #if ENABLE_HUSH_LOOPS |
| 9470 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 9471 | if (!for_lcur) { |
| 9472 | /* first loop through for */ |
| 9473 | |
| 9474 | static const char encoded_dollar_at[] ALIGN1 = { |
| 9475 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 9476 | }; /* encoded representation of "$@" */ |
| 9477 | static const char *const encoded_dollar_at_argv[] = { |
| 9478 | encoded_dollar_at, NULL |
| 9479 | }; /* argv list with one element: "$@" */ |
| 9480 | char **vals; |
| 9481 | |
Denys Vlasenko | a5db1d7 | 2018-07-28 12:42:08 +0200 | [diff] [blame] | 9482 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9483 | vals = (char**)encoded_dollar_at_argv; |
| 9484 | if (pi->next->res_word == RES_IN) { |
| 9485 | /* if no variable values after "in" we skip "for" */ |
| 9486 | if (!pi->next->cmds[0].argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9487 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 9488 | break; |
| 9489 | } |
| 9490 | vals = pi->next->cmds[0].argv; |
| 9491 | } /* else: "for var; do..." -> assume "$@" list */ |
| 9492 | /* create list of variable values */ |
| 9493 | debug_print_strings("for_list made from", vals); |
| 9494 | for_list = expand_strvec_to_strvec(vals); |
| 9495 | for_lcur = for_list; |
| 9496 | debug_print_strings("for_list", for_list); |
| 9497 | } |
| 9498 | if (!*for_lcur) { |
| 9499 | /* "for" loop is over, clean up */ |
| 9500 | free(for_list); |
| 9501 | for_list = NULL; |
| 9502 | for_lcur = NULL; |
| 9503 | break; |
| 9504 | } |
| 9505 | /* Insert next value from for_lcur */ |
| 9506 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9507 | 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] | 9508 | continue; |
| 9509 | } |
| 9510 | if (rword == RES_IN) { |
| 9511 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 9512 | } |
| 9513 | if (rword == RES_DONE) { |
| 9514 | continue; /* "done" has no cmds too */ |
| 9515 | } |
| 9516 | #endif |
| 9517 | #if ENABLE_HUSH_CASE |
| 9518 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9519 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9520 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 9521 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 9522 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 9523 | //unbackslash(case_word); |
| 9524 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9525 | continue; |
| 9526 | } |
| 9527 | if (rword == RES_MATCH) { |
| 9528 | char **argv; |
| 9529 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9530 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9531 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 9532 | break; |
| 9533 | /* all prev words didn't match, does this one match? */ |
| 9534 | argv = pi->cmds->argv; |
| 9535 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9536 | char *pattern; |
| 9537 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 9538 | pattern = expand_string_to_string(*argv, |
| 9539 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9540 | /*unbackslash:*/ 0 |
| 9541 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9542 | /* TODO: which FNM_xxx flags to use? */ |
| 9543 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9544 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 9545 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9546 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9547 | if (cond_code == 0) { |
| 9548 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9549 | free(case_word); |
| 9550 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9551 | break; |
| 9552 | } |
| 9553 | argv++; |
| 9554 | } |
| 9555 | continue; |
| 9556 | } |
| 9557 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9558 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9559 | if (cond_code != 0) |
| 9560 | continue; /* not matched yet, skip this pipe */ |
| 9561 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9562 | if (rword == RES_ESAC) { |
| 9563 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 9564 | if (case_word) { |
| 9565 | /* "case" did not match anything: still set $? (to 0) */ |
| 9566 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9567 | } |
| 9568 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9569 | #endif |
| 9570 | /* Just pressing <enter> in shell should check for jobs. |
| 9571 | * OTOH, in non-interactive shell this is useless |
| 9572 | * and only leads to extra job checks */ |
| 9573 | if (pi->num_cmds == 0) { |
| 9574 | if (G_interactive_fd) |
| 9575 | goto check_jobs_and_continue; |
| 9576 | continue; |
| 9577 | } |
| 9578 | |
| 9579 | /* After analyzing all keywords and conditions, we decided |
| 9580 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 9581 | * after run_pipe to collect any background children, |
| 9582 | * even if list execution is to be stopped. */ |
| 9583 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9584 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9585 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9586 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9587 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 9588 | if (r != -1) { |
| 9589 | /* We ran a builtin, function, or group. |
| 9590 | * rcode is already known |
| 9591 | * and we don't need to wait for anything. */ |
| 9592 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 9593 | G.last_exitcode = rcode; |
| 9594 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9595 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9596 | /* Was it "break" or "continue"? */ |
| 9597 | if (G.flag_break_continue) { |
| 9598 | smallint fbc = G.flag_break_continue; |
| 9599 | /* We might fall into outer *loop*, |
| 9600 | * don't want to break it too */ |
| 9601 | if (loop_top) { |
| 9602 | G.depth_break_continue--; |
| 9603 | if (G.depth_break_continue == 0) |
| 9604 | G.flag_break_continue = 0; |
| 9605 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9606 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9607 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9608 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9609 | break; |
| 9610 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9611 | /* "continue": simulate end of loop */ |
| 9612 | rword = RES_DONE; |
| 9613 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9614 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9615 | #endif |
| 9616 | if (G_flag_return_in_progress == 1) { |
| 9617 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9618 | break; |
| 9619 | } |
| 9620 | } else if (pi->followup == PIPE_BG) { |
| 9621 | /* What does bash do with attempts to background builtins? */ |
| 9622 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9623 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9624 | * I'm NOT treating inner &'s as jobs */ |
| 9625 | #if ENABLE_HUSH_JOB |
| 9626 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9627 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9628 | #endif |
| 9629 | /* Last command's pid goes to $! */ |
| 9630 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9631 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9632 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9633 | /* 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] | 9634 | rcode = EXIT_SUCCESS; |
| 9635 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9636 | } else { |
| 9637 | #if ENABLE_HUSH_JOB |
| 9638 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9639 | /* Waits for completion, then fg's main shell */ |
| 9640 | rcode = checkjobs_and_fg_shell(pi); |
| 9641 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9642 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9643 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9644 | #endif |
| 9645 | /* This one just waits for completion */ |
| 9646 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 9647 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 9648 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9649 | G.last_exitcode = rcode; |
| 9650 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9651 | } |
| 9652 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9653 | /* Handle "set -e" */ |
| 9654 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 9655 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 9656 | if (G.errexit_depth == 0) |
| 9657 | hush_exit(rcode); |
| 9658 | } |
| 9659 | G.errexit_depth = sv_errexit_depth; |
| 9660 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9661 | /* Analyze how result affects subsequent commands */ |
| 9662 | #if ENABLE_HUSH_IF |
| 9663 | if (rword == RES_IF || rword == RES_ELIF) |
| 9664 | cond_code = rcode; |
| 9665 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9666 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9667 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9668 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9669 | #if ENABLE_HUSH_LOOPS |
| 9670 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9671 | if (pi->next |
| 9672 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9673 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9674 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9675 | if (rword == RES_WHILE) { |
| 9676 | if (rcode) { |
| 9677 | /* "while false; do...done" - exitcode 0 */ |
| 9678 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9679 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9680 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9681 | } |
| 9682 | } |
| 9683 | if (rword == RES_UNTIL) { |
| 9684 | if (!rcode) { |
| 9685 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9686 | break; |
| 9687 | } |
| 9688 | } |
| 9689 | } |
| 9690 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9691 | } /* for (pi) */ |
| 9692 | |
| 9693 | #if ENABLE_HUSH_JOB |
| 9694 | G.run_list_level--; |
| 9695 | #endif |
| 9696 | #if ENABLE_HUSH_LOOPS |
| 9697 | if (loop_top) |
| 9698 | G.depth_of_loop--; |
| 9699 | free(for_list); |
| 9700 | #endif |
| 9701 | #if ENABLE_HUSH_CASE |
| 9702 | free(case_word); |
| 9703 | #endif |
| 9704 | debug_leave(); |
| 9705 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9706 | return rcode; |
| 9707 | } |
| 9708 | |
| 9709 | /* Select which version we will use */ |
| 9710 | static int run_and_free_list(struct pipe *pi) |
| 9711 | { |
| 9712 | int rcode = 0; |
| 9713 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9714 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9715 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9716 | rcode = run_list(pi); |
| 9717 | } |
| 9718 | /* free_pipe_list has the side effect of clearing memory. |
| 9719 | * In the long run that function can be merged with run_list, |
| 9720 | * but doing that now would hobble the debugging effort. */ |
| 9721 | free_pipe_list(pi); |
| 9722 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9723 | return rcode; |
| 9724 | } |
| 9725 | |
| 9726 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9727 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9728 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9729 | sighandler_t old_handler; |
| 9730 | unsigned sig = 0; |
| 9731 | while ((mask >>= 1) != 0) { |
| 9732 | sig++; |
| 9733 | if (!(mask & 1)) |
| 9734 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9735 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9736 | /* POSIX allows shell to re-enable SIGCHLD |
| 9737 | * even if it was SIG_IGN on entry. |
| 9738 | * Therefore we skip IGN check for it: |
| 9739 | */ |
| 9740 | if (sig == SIGCHLD) |
| 9741 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9742 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9743 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9744 | */ |
| 9745 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9746 | if (old_handler == SIG_IGN) { |
| 9747 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9748 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9749 | #if ENABLE_HUSH_TRAP |
| 9750 | if (!G_traps) |
| 9751 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9752 | free(G_traps[sig]); |
| 9753 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9754 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9755 | } |
| 9756 | } |
| 9757 | } |
| 9758 | |
| 9759 | /* Called a few times only (or even once if "sh -c") */ |
| 9760 | static void install_special_sighandlers(void) |
| 9761 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9762 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9763 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9764 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9765 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9766 | if (G_interactive_fd) { |
| 9767 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9768 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9769 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9770 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9771 | /* Careful, do not re-install handlers we already installed */ |
| 9772 | if (G.special_sig_mask != mask) { |
| 9773 | unsigned diff = mask & ~G.special_sig_mask; |
| 9774 | G.special_sig_mask = mask; |
| 9775 | install_sighandlers(diff); |
| 9776 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9777 | } |
| 9778 | |
| 9779 | #if ENABLE_HUSH_JOB |
| 9780 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9781 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9782 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9783 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9784 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9785 | |
| 9786 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9787 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9788 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9789 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9790 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9791 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9792 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9793 | + (1 << SIGABRT) |
| 9794 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9795 | + (1 << SIGPIPE) |
| 9796 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9797 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9798 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9799 | * we never want to restore pgrp on exit, and this fn is not called |
| 9800 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9801 | /*+ (1 << SIGHUP )*/ |
| 9802 | /*+ (1 << SIGTERM)*/ |
| 9803 | /*+ (1 << SIGINT )*/ |
| 9804 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9805 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9806 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9807 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9808 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9809 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9810 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9811 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9812 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9813 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9814 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9815 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9816 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9817 | break; |
| 9818 | case 'x': |
| 9819 | IF_HUSH_MODE_X(G_x_mode = state;) |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9820 | IF_HUSH_MODE_X(if (G.x_mode_fd <= 0) G.x_mode_fd = dup_CLOEXEC(2, 10);) |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9821 | break; |
| 9822 | case 'o': |
| 9823 | if (!o_opt) { |
| 9824 | /* "set -+o" without parameter. |
| 9825 | * in bash, set -o produces this output: |
| 9826 | * pipefail off |
| 9827 | * and set +o: |
| 9828 | * set +o pipefail |
| 9829 | * We always use the second form. |
| 9830 | */ |
| 9831 | const char *p = o_opt_strings; |
| 9832 | idx = 0; |
| 9833 | while (*p) { |
| 9834 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9835 | idx++; |
| 9836 | p += strlen(p) + 1; |
| 9837 | } |
| 9838 | break; |
| 9839 | } |
| 9840 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9841 | if (idx >= 0) { |
| 9842 | G.o_opt[idx] = state; |
| 9843 | break; |
| 9844 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9845 | case 'e': |
| 9846 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9847 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9848 | default: |
| 9849 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9850 | } |
| 9851 | return EXIT_SUCCESS; |
| 9852 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9853 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9854 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9855 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9856 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9857 | enum { |
| 9858 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9859 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9860 | }; |
| 9861 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9862 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9863 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9864 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9865 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9866 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9867 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9868 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9869 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9870 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9871 | #if ENABLE_HUSH_FAST |
| 9872 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9873 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9874 | #if !BB_MMU |
| 9875 | G.argv0_for_re_execing = argv[0]; |
| 9876 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9877 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9878 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9879 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9880 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9881 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9882 | shell_ver->flg_export = 1; |
| 9883 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9884 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9885 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9886 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9887 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9888 | /* Create shell local variables from the values |
| 9889 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9890 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9891 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9892 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9893 | if (e) while (*e) { |
| 9894 | char *value = strchr(*e, '='); |
| 9895 | if (value) { /* paranoia */ |
| 9896 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9897 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9898 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9899 | cur_var->max_len = strlen(*e); |
| 9900 | cur_var->flg_export = 1; |
| 9901 | } |
| 9902 | e++; |
| 9903 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9904 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9905 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9906 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9907 | |
| 9908 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9909 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9910 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9911 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9912 | /* Set (but not export) PS1/2 unless already set */ |
| 9913 | if (!get_local_var_value("PS1")) |
| 9914 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9915 | if (!get_local_var_value("PS2")) |
| 9916 | set_local_var_from_halves("PS2", "> "); |
| 9917 | #endif |
| 9918 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9919 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9920 | /* Set (but not export) HOSTNAME unless already set */ |
| 9921 | if (!get_local_var_value("HOSTNAME")) { |
| 9922 | struct utsname uts; |
| 9923 | uname(&uts); |
| 9924 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9925 | } |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 9926 | #endif |
| 9927 | /* IFS is not inherited from the parent environment */ |
| 9928 | set_local_var_from_halves("IFS", defifs); |
| 9929 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9930 | /* bash also exports SHLVL and _, |
| 9931 | * and sets (but doesn't export) the following variables: |
| 9932 | * BASH=/bin/bash |
| 9933 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9934 | * BASH_VERSION='3.2.0(1)-release' |
| 9935 | * HOSTTYPE=i386 |
| 9936 | * MACHTYPE=i386-pc-linux-gnu |
| 9937 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9938 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9939 | * EUID=<NNNNN> |
| 9940 | * UID=<NNNNN> |
| 9941 | * GROUPS=() |
| 9942 | * LINES=<NNN> |
| 9943 | * COLUMNS=<NNN> |
| 9944 | * BASH_ARGC=() |
| 9945 | * BASH_ARGV=() |
| 9946 | * BASH_LINENO=() |
| 9947 | * BASH_SOURCE=() |
| 9948 | * DIRSTACK=() |
| 9949 | * PIPESTATUS=([0]="0") |
| 9950 | * HISTFILE=/<xxx>/.bash_history |
| 9951 | * HISTFILESIZE=500 |
| 9952 | * HISTSIZE=500 |
| 9953 | * MAILCHECK=60 |
| 9954 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9955 | * SHELL=/bin/bash |
| 9956 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9957 | * TERM=dumb |
| 9958 | * OPTERR=1 |
| 9959 | * OPTIND=1 |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9960 | * PS4='+ ' |
| 9961 | */ |
| 9962 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9963 | #if ENABLE_HUSH_LINENO_VAR |
| 9964 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9965 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9966 | set_local_var(p, /*flags*/ 0); |
| 9967 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9968 | } |
| 9969 | #endif |
| 9970 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9971 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9972 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9973 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9974 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9975 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9976 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9977 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9978 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9979 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9980 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9981 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9982 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9983 | * 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] | 9984 | * in order to intercept (more) signals. |
| 9985 | */ |
| 9986 | |
| 9987 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9988 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9989 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9990 | builtin_argc = 0; |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 9991 | #if NUM_SCRIPTS > 0 |
| 9992 | if (argc < 0) { |
| 9993 | optarg = get_script_content(-argc - 1); |
| 9994 | optind = 0; |
| 9995 | argc = string_array_len(argv); |
| 9996 | goto run_script; |
| 9997 | } |
| 9998 | #endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9999 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10000 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10001 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10002 | "<:$:R:V:" |
| 10003 | # if ENABLE_HUSH_FUNCTIONS |
| 10004 | "F:" |
| 10005 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10006 | #endif |
| 10007 | ); |
| 10008 | if (opt <= 0) |
| 10009 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10010 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10011 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10012 | /* Possibilities: |
| 10013 | * sh ... -c 'script' |
| 10014 | * sh ... -c 'script' ARG0 [ARG1...] |
| 10015 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10016 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10017 | * "" needs to be replaced with NULL |
| 10018 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10019 | * Note: the form without ARG0 never happens: |
| 10020 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10021 | */ |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 10022 | #if NUM_SCRIPTS > 0 |
| 10023 | run_script: |
| 10024 | #endif |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10025 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10026 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10027 | G.root_ppid = getppid(); |
| 10028 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10029 | G.global_argv = argv + optind; |
| 10030 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10031 | if (builtin_argc) { |
| 10032 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 10033 | const struct built_in_command *x; |
| 10034 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10035 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10036 | x = find_builtin(optarg); |
| 10037 | if (x) { /* paranoia */ |
| 10038 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 10039 | G.global_argv += builtin_argc; |
| 10040 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 10041 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10042 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10043 | } |
| 10044 | goto final_return; |
| 10045 | } |
| 10046 | if (!G.global_argv[0]) { |
| 10047 | /* -c 'script' (no params): prevent empty $0 */ |
| 10048 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 10049 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 10050 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10051 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10052 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10053 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10054 | goto final_return; |
| 10055 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 10056 | /* Well, we cannot just declare interactiveness, |
| 10057 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10058 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10059 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10060 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10061 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10062 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10063 | case 'l': |
| 10064 | flags |= OPT_login; |
| 10065 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10066 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10067 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 10068 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10069 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10070 | case '$': { |
| 10071 | unsigned long long empty_trap_mask; |
| 10072 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10073 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 10074 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10075 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 10076 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10077 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 10078 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10079 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10080 | optarg++; |
| 10081 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10082 | optarg++; |
| 10083 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 10084 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10085 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10086 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10087 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10088 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10089 | for (sig = 1; sig < NSIG; sig++) { |
| 10090 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10091 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10092 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10093 | } |
| 10094 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10095 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10096 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10097 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10098 | optarg++; |
| 10099 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10100 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 10101 | # if ENABLE_HUSH_FUNCTIONS |
| 10102 | /* nommu uses re-exec trick for "... | func | ...", |
| 10103 | * should allow "return". |
| 10104 | * This accidentally allows returns in subshells. |
| 10105 | */ |
| 10106 | G_flag_return_in_progress = -1; |
| 10107 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10108 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10109 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10110 | case 'R': |
| 10111 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10112 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10113 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10114 | # if ENABLE_HUSH_FUNCTIONS |
| 10115 | case 'F': { |
| 10116 | struct function *funcp = new_function(optarg); |
| 10117 | /* funcp->name is already set to optarg */ |
| 10118 | /* funcp->body is set to NULL. It's a special case. */ |
| 10119 | funcp->body_as_string = argv[optind]; |
| 10120 | optind++; |
| 10121 | break; |
| 10122 | } |
| 10123 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10124 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 10125 | case 'n': |
| 10126 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 10127 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10128 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 10129 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10130 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10131 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10132 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 10133 | " or: sh -c command [args]...\n\n"); |
| 10134 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10135 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10136 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10137 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10138 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10139 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10140 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10141 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 10142 | G.global_argc = argc - (optind - 1); |
| 10143 | G.global_argv = argv + (optind - 1); |
| 10144 | G.global_argv[0] = argv[0]; |
| 10145 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10146 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10147 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10148 | G.root_ppid = getppid(); |
| 10149 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10150 | |
| 10151 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10152 | if (flags & OPT_login) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10153 | HFILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10154 | debug_printf("sourcing /etc/profile\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10155 | input = hfopen("/etc/profile"); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10156 | if (input != NULL) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10157 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10158 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10159 | hfclose(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10160 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10161 | /* bash: after sourcing /etc/profile, |
| 10162 | * tries to source (in the given order): |
| 10163 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10164 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10165 | * bash also sources ~/.bash_logout on exit. |
| 10166 | * If called as sh, skips .bash_XXX files. |
| 10167 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10168 | } |
| 10169 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10170 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 10171 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10172 | HFILE *input; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10173 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10174 | * "bash <script>" (which is never interactive (unless -i?)) |
| 10175 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10176 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 10177 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10178 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10179 | G.global_argc--; |
| 10180 | G.global_argv++; |
| 10181 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10182 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10183 | input = hfopen(G.global_argv[0]); |
| 10184 | if (!input) { |
| 10185 | bb_simple_perror_msg_and_die(G.global_argv[0]); |
| 10186 | } |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10187 | xfunc_error_retval = 1; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10188 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10189 | parse_and_run_file(input); |
| 10190 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10191 | hfclose(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10192 | #endif |
| 10193 | goto final_return; |
| 10194 | } |
| 10195 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10196 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10197 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10198 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10199 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10200 | /* A shell is interactive if the '-i' flag was given, |
| 10201 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 10202 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10203 | * no arguments remaining or the -s flag given |
| 10204 | * standard input is a terminal |
| 10205 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10206 | * Refer to Posix.2, the description of the 'sh' utility. |
| 10207 | */ |
| 10208 | #if ENABLE_HUSH_JOB |
| 10209 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10210 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 10211 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 10212 | if (G_saved_tty_pgrp < 0) |
| 10213 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10214 | |
| 10215 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10216 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10217 | if (G_interactive_fd < 0) { |
| 10218 | /* try to dup to any fd */ |
| 10219 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10220 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10221 | /* give up */ |
| 10222 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10223 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 10224 | } |
| 10225 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10226 | // TODO: track & disallow any attempts of user |
| 10227 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10228 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10229 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10230 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10231 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10232 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10233 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10234 | /* If we were run as 'hush &', sleep until we are |
| 10235 | * in the foreground (tty pgrp == our pgrp). |
| 10236 | * If we get started under a job aware app (like bash), |
| 10237 | * make sure we are now in charge so we don't fight over |
| 10238 | * who gets the foreground */ |
| 10239 | while (1) { |
| 10240 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10241 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 10242 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10243 | break; |
| 10244 | /* send TTIN to ourself (should stop us) */ |
| 10245 | kill(- shell_pgrp, SIGTTIN); |
| 10246 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10247 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10248 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10249 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10250 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10251 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10252 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10253 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10254 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10255 | /* Put ourselves in our own process group |
| 10256 | * (bash, too, does this only if ctty is available) */ |
| 10257 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 10258 | /* Grab control of the terminal */ |
| 10259 | tcsetpgrp(G_interactive_fd, getpid()); |
| 10260 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 10261 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10262 | |
| 10263 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 10264 | { |
| 10265 | const char *hp = get_local_var_value("HISTFILE"); |
| 10266 | if (!hp) { |
| 10267 | hp = get_local_var_value("HOME"); |
| 10268 | if (hp) |
| 10269 | hp = concat_path_file(hp, ".hush_history"); |
| 10270 | } else { |
| 10271 | hp = xstrdup(hp); |
| 10272 | } |
| 10273 | if (hp) { |
| 10274 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10275 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 10276 | } |
| 10277 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 10278 | hp = get_local_var_value("HISTFILESIZE"); |
| 10279 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 10280 | # endif |
| 10281 | } |
| 10282 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10283 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10284 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10285 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10286 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10287 | /* No job control compiled in, only prompt/line editing */ |
| 10288 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10289 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10290 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10291 | /* try to dup to any fd */ |
Denys Vlasenko | d1a8323 | 2018-06-26 15:50:33 +0200 | [diff] [blame] | 10292 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10293 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10294 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10295 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10296 | } |
| 10297 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10298 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10299 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10300 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10301 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10302 | #else |
| 10303 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10304 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10305 | #endif |
| 10306 | /* bash: |
| 10307 | * if interactive but not a login shell, sources ~/.bashrc |
| 10308 | * (--norc turns this off, --rcfile <file> overrides) |
| 10309 | */ |
| 10310 | |
| 10311 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 10312 | /* note: ash and hush share this string */ |
| 10313 | printf("\n\n%s %s\n" |
| 10314 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 10315 | "\n", |
| 10316 | bb_banner, |
| 10317 | "hush - the humble shell" |
| 10318 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 10319 | } |
| 10320 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10321 | parse_and_run_file(hfopen(NULL)); /* stdin */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10322 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10323 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10324 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10325 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 10326 | |
| 10327 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10328 | /* |
| 10329 | * Built-ins |
| 10330 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10331 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10332 | { |
| 10333 | return 0; |
| 10334 | } |
| 10335 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10336 | #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] | 10337 | 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] | 10338 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10339 | int argc = string_array_len(argv); |
| 10340 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10341 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10342 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10343 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10344 | static int FAST_FUNC builtin_test(char **argv) |
| 10345 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10346 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10347 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10348 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10349 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10350 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10351 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10352 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10353 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10354 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10355 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10356 | static int FAST_FUNC builtin_printf(char **argv) |
| 10357 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10358 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10359 | } |
| 10360 | #endif |
| 10361 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10362 | #if ENABLE_HUSH_HELP |
| 10363 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 10364 | { |
| 10365 | const struct built_in_command *x; |
| 10366 | |
| 10367 | printf( |
| 10368 | "Built-in commands:\n" |
| 10369 | "------------------\n"); |
| 10370 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 10371 | if (x->b_descr) |
| 10372 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 10373 | } |
| 10374 | return EXIT_SUCCESS; |
| 10375 | } |
| 10376 | #endif |
| 10377 | |
| 10378 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 10379 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 10380 | { |
| 10381 | show_history(G.line_input_state); |
| 10382 | return EXIT_SUCCESS; |
| 10383 | } |
| 10384 | #endif |
| 10385 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10386 | static char **skip_dash_dash(char **argv) |
| 10387 | { |
| 10388 | argv++; |
| 10389 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 10390 | argv++; |
| 10391 | return argv; |
| 10392 | } |
| 10393 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10394 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10395 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10396 | const char *newdir; |
| 10397 | |
| 10398 | argv = skip_dash_dash(argv); |
| 10399 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10400 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10401 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10402 | * bash says "bash: cd: HOME not set" and does nothing |
| 10403 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10404 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 10405 | const char *home = get_local_var_value("HOME"); |
| 10406 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 10407 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10408 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10409 | /* Mimic bash message exactly */ |
| 10410 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10411 | return EXIT_FAILURE; |
| 10412 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10413 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 10414 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 10415 | * set it again, but do not export. bash does the same. |
| 10416 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10417 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10418 | return EXIT_SUCCESS; |
| 10419 | } |
| 10420 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10421 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 10422 | { |
| 10423 | puts(get_cwd(0)); |
| 10424 | return EXIT_SUCCESS; |
| 10425 | } |
| 10426 | |
| 10427 | static int FAST_FUNC builtin_eval(char **argv) |
| 10428 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10429 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10430 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10431 | if (!argv[0]) |
| 10432 | return EXIT_SUCCESS; |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10433 | |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10434 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10435 | //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10436 | if (!argv[1]) { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10437 | /* bash: |
| 10438 | * eval "echo Hi; done" ("done" is syntax error): |
| 10439 | * "echo Hi" will not execute too. |
| 10440 | */ |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10441 | parse_and_run_string(argv[0]); |
| 10442 | } else { |
| 10443 | /* "The eval utility shall construct a command by |
| 10444 | * concatenating arguments together, separating |
| 10445 | * each with a <space> character." |
| 10446 | */ |
| 10447 | char *str, *p; |
| 10448 | unsigned len = 0; |
| 10449 | char **pp = argv; |
| 10450 | do |
| 10451 | len += strlen(*pp) + 1; |
| 10452 | while (*++pp); |
| 10453 | str = p = xmalloc(len); |
| 10454 | pp = argv; |
| 10455 | for (;;) { |
| 10456 | p = stpcpy(p, *pp); |
| 10457 | pp++; |
| 10458 | if (!*pp) |
| 10459 | break; |
| 10460 | *p++ = ' '; |
| 10461 | } |
| 10462 | parse_and_run_string(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10463 | free(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10464 | } |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10465 | IF_HUSH_MODE_X(G.x_mode_depth--;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10466 | //bb_error_msg("%s: --x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10467 | return G.last_exitcode; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10468 | } |
| 10469 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10470 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10471 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10472 | argv = skip_dash_dash(argv); |
| 10473 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10474 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10475 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10476 | /* Careful: we can end up here after [v]fork. Do not restore |
| 10477 | * tty pgrp then, only top-level shell process does that */ |
| 10478 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 10479 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 10480 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 10481 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 10482 | * open here. However, they are all CLOEXEC, and execv below |
| 10483 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 10484 | * it should show no extra open fds in the "ls" process. |
| 10485 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 10486 | */ |
| 10487 | //close_saved_fds_and_FILE_fds(); |
| 10488 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 10489 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10490 | * 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] | 10491 | * and tcsetpgrp, and this is inherently racy. |
| 10492 | */ |
| 10493 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10494 | } |
| 10495 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10496 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10497 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 10498 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10499 | |
| 10500 | /* interactive bash: |
| 10501 | * # trap "echo EEE" EXIT |
| 10502 | * # exit |
| 10503 | * exit |
| 10504 | * There are stopped jobs. |
| 10505 | * (if there are _stopped_ jobs, running ones don't count) |
| 10506 | * # exit |
| 10507 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 10508 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10509 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 10510 | * 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] | 10511 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 10512 | |
| 10513 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10514 | argv = skip_dash_dash(argv); |
| 10515 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10516 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10517 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 10518 | xfunc_error_retval = 255; |
| 10519 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10520 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10521 | } |
| 10522 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10523 | #if ENABLE_HUSH_TYPE |
| 10524 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 10525 | static int FAST_FUNC builtin_type(char **argv) |
| 10526 | { |
| 10527 | int ret = EXIT_SUCCESS; |
| 10528 | |
| 10529 | while (*++argv) { |
| 10530 | const char *type; |
| 10531 | char *path = NULL; |
| 10532 | |
| 10533 | if (0) {} /* make conditional compile easier below */ |
| 10534 | /*else if (find_alias(*argv)) |
| 10535 | type = "an alias";*/ |
| 10536 | #if ENABLE_HUSH_FUNCTIONS |
| 10537 | else if (find_function(*argv)) |
| 10538 | type = "a function"; |
| 10539 | #endif |
| 10540 | else if (find_builtin(*argv)) |
| 10541 | type = "a shell builtin"; |
| 10542 | else if ((path = find_in_path(*argv)) != NULL) |
| 10543 | type = path; |
| 10544 | else { |
| 10545 | bb_error_msg("type: %s: not found", *argv); |
| 10546 | ret = EXIT_FAILURE; |
| 10547 | continue; |
| 10548 | } |
| 10549 | |
| 10550 | printf("%s is %s\n", *argv, type); |
| 10551 | free(path); |
| 10552 | } |
| 10553 | |
| 10554 | return ret; |
| 10555 | } |
| 10556 | #endif |
| 10557 | |
| 10558 | #if ENABLE_HUSH_READ |
| 10559 | /* Interruptibility of read builtin in bash |
| 10560 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 10561 | * |
| 10562 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 10563 | * |
| 10564 | * SIGINT: |
| 10565 | * - terminates non-interactive shell; |
| 10566 | * - interrupts read in interactive shell; |
| 10567 | * if it has non-empty trap: |
| 10568 | * - executes trap and returns to command prompt in interactive shell; |
| 10569 | * - executes trap and returns to read in non-interactive shell; |
| 10570 | * SIGTERM: |
| 10571 | * - is ignored (does not interrupt) read in interactive shell; |
| 10572 | * - terminates non-interactive shell; |
| 10573 | * if it has non-empty trap: |
| 10574 | * - executes trap and returns to read; |
| 10575 | * SIGHUP: |
| 10576 | * - terminates shell (regardless of interactivity); |
| 10577 | * if it has non-empty trap: |
| 10578 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10579 | * SIGCHLD from children: |
| 10580 | * - does not interrupt read regardless of interactivity: |
| 10581 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10582 | */ |
| 10583 | static int FAST_FUNC builtin_read(char **argv) |
| 10584 | { |
| 10585 | const char *r; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10586 | struct builtin_read_params params; |
| 10587 | |
| 10588 | memset(¶ms, 0, sizeof(params)); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10589 | |
| 10590 | /* "!": do not abort on errors. |
| 10591 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 10592 | */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10593 | params.read_flags = getopt32(argv, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10594 | #if BASH_READ_D |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10595 | "!srn:p:t:u:d:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u, ¶ms.opt_d |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10596 | #else |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10597 | "!srn:p:t:u:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10598 | #endif |
| 10599 | ); |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10600 | if ((uint32_t)params.read_flags == (uint32_t)-1) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10601 | return EXIT_FAILURE; |
| 10602 | argv += optind; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10603 | params.argv = argv; |
| 10604 | params.setvar = set_local_var_from_halves; |
| 10605 | params.ifs = get_local_var_value("IFS"); /* can be NULL */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10606 | |
| 10607 | again: |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10608 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10609 | |
| 10610 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 10611 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10612 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10613 | goto again; |
| 10614 | } |
| 10615 | |
| 10616 | if ((uintptr_t)r > 1) { |
| 10617 | bb_error_msg("%s", r); |
| 10618 | r = (char*)(uintptr_t)1; |
| 10619 | } |
| 10620 | |
| 10621 | return (uintptr_t)r; |
| 10622 | } |
| 10623 | #endif |
| 10624 | |
| 10625 | #if ENABLE_HUSH_UMASK |
| 10626 | static int FAST_FUNC builtin_umask(char **argv) |
| 10627 | { |
| 10628 | int rc; |
| 10629 | mode_t mask; |
| 10630 | |
| 10631 | rc = 1; |
| 10632 | mask = umask(0); |
| 10633 | argv = skip_dash_dash(argv); |
| 10634 | if (argv[0]) { |
| 10635 | mode_t old_mask = mask; |
| 10636 | |
| 10637 | /* numeric umasks are taken as-is */ |
| 10638 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 10639 | if (!isdigit(argv[0][0])) |
| 10640 | mask ^= 0777; |
| 10641 | mask = bb_parse_mode(argv[0], mask); |
| 10642 | if (!isdigit(argv[0][0])) |
| 10643 | mask ^= 0777; |
| 10644 | if ((unsigned)mask > 0777) { |
| 10645 | mask = old_mask; |
| 10646 | /* bash messages: |
| 10647 | * bash: umask: 'q': invalid symbolic mode operator |
| 10648 | * bash: umask: 999: octal number out of range |
| 10649 | */ |
| 10650 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 10651 | rc = 0; |
| 10652 | } |
| 10653 | } else { |
| 10654 | /* Mimic bash */ |
| 10655 | printf("%04o\n", (unsigned) mask); |
| 10656 | /* fall through and restore mask which we set to 0 */ |
| 10657 | } |
| 10658 | umask(mask); |
| 10659 | |
| 10660 | return !rc; /* rc != 0 - success */ |
| 10661 | } |
| 10662 | #endif |
| 10663 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10664 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10665 | static void print_escaped(const char *s) |
| 10666 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10667 | if (*s == '\'') |
| 10668 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10669 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10670 | const char *p = strchrnul(s, '\''); |
| 10671 | /* print 'xxxx', possibly just '' */ |
| 10672 | printf("'%.*s'", (int)(p - s), s); |
| 10673 | if (*p == '\0') |
| 10674 | break; |
| 10675 | s = p; |
| 10676 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10677 | /* s points to '; print "'''...'''" */ |
| 10678 | putchar('"'); |
| 10679 | do putchar('\''); while (*++s == '\''); |
| 10680 | putchar('"'); |
| 10681 | } while (*s); |
| 10682 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10683 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10684 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10685 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10686 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10687 | { |
| 10688 | do { |
| 10689 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10690 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10691 | |
| 10692 | /* So far we do not check that name is valid (TODO?) */ |
| 10693 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10694 | if (*name_end == '\0') { |
| 10695 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10696 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10697 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10698 | var = vpp ? *vpp : NULL; |
| 10699 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10700 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10701 | /* export -n NAME (without =VALUE) */ |
| 10702 | if (var) { |
| 10703 | var->flg_export = 0; |
| 10704 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10705 | unsetenv(name); |
| 10706 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10707 | continue; |
| 10708 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10709 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10710 | /* export NAME (without =VALUE) */ |
| 10711 | if (var) { |
| 10712 | var->flg_export = 1; |
| 10713 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10714 | putenv(var->varstr); |
| 10715 | continue; |
| 10716 | } |
| 10717 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10718 | if (flags & SETFLAG_MAKE_RO) { |
| 10719 | /* readonly NAME (without =VALUE) */ |
| 10720 | if (var) { |
| 10721 | var->flg_read_only = 1; |
| 10722 | continue; |
| 10723 | } |
| 10724 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10725 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10726 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10727 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10728 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10729 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10730 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10731 | continue; |
| 10732 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10733 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10734 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10735 | /* Exporting non-existing variable. |
| 10736 | * bash does not put it in environment, |
| 10737 | * but remembers that it is exported, |
| 10738 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10739 | * We just set it to "" and export. |
| 10740 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10741 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10742 | * bash sets the value to "". |
| 10743 | */ |
| 10744 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10745 | * bash remembers NAME and disallows its creation |
| 10746 | * in the future. |
| 10747 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10748 | name = xasprintf("%s=", name); |
| 10749 | } else { |
| 10750 | /* (Un)exporting/making local NAME=VALUE */ |
| 10751 | name = xstrdup(name); |
| 10752 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10753 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10754 | if (set_local_var(name, flags)) |
| 10755 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10756 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10757 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10758 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10759 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10760 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10761 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10762 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10763 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10764 | unsigned opt_unexport; |
| 10765 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10766 | #if ENABLE_HUSH_EXPORT_N |
| 10767 | /* "!": do not abort on errors */ |
| 10768 | opt_unexport = getopt32(argv, "!n"); |
| 10769 | if (opt_unexport == (uint32_t)-1) |
| 10770 | return EXIT_FAILURE; |
| 10771 | argv += optind; |
| 10772 | #else |
| 10773 | opt_unexport = 0; |
| 10774 | argv++; |
| 10775 | #endif |
| 10776 | |
| 10777 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10778 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10779 | if (e) { |
| 10780 | while (*e) { |
| 10781 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10782 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10783 | #else |
| 10784 | /* ash emits: export VAR='VAL' |
| 10785 | * bash: declare -x VAR="VAL" |
| 10786 | * we follow ash example */ |
| 10787 | const char *s = *e++; |
| 10788 | const char *p = strchr(s, '='); |
| 10789 | |
| 10790 | if (!p) /* wtf? take next variable */ |
| 10791 | continue; |
| 10792 | /* export var= */ |
| 10793 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10794 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10795 | putchar('\n'); |
| 10796 | #endif |
| 10797 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10798 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10799 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10800 | return EXIT_SUCCESS; |
| 10801 | } |
| 10802 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10803 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10804 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10805 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10806 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10807 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10808 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10809 | { |
| 10810 | if (G.func_nest_level == 0) { |
| 10811 | bb_error_msg("%s: not in a function", argv[0]); |
| 10812 | return EXIT_FAILURE; /* bash compat */ |
| 10813 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10814 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10815 | /* Since all builtins run in a nested variable level, |
| 10816 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10817 | * after builtin returns. |
| 10818 | */ |
| 10819 | 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] | 10820 | } |
| 10821 | #endif |
| 10822 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10823 | #if ENABLE_HUSH_READONLY |
| 10824 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10825 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10826 | argv++; |
| 10827 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10828 | /* bash: readonly [-p]: list all readonly VARs |
| 10829 | * (-p has no effect in bash) |
| 10830 | */ |
| 10831 | struct variable *e; |
| 10832 | for (e = G.top_var; e; e = e->next) { |
| 10833 | if (e->flg_read_only) { |
| 10834 | //TODO: quote value: readonly VAR='VAL' |
| 10835 | printf("readonly %s\n", e->varstr); |
| 10836 | } |
| 10837 | } |
| 10838 | return EXIT_SUCCESS; |
| 10839 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10840 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10841 | } |
| 10842 | #endif |
| 10843 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10844 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10845 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10846 | static int FAST_FUNC builtin_unset(char **argv) |
| 10847 | { |
| 10848 | int ret; |
| 10849 | unsigned opts; |
| 10850 | |
| 10851 | /* "!": do not abort on errors */ |
| 10852 | /* "+": stop at 1st non-option */ |
| 10853 | opts = getopt32(argv, "!+vf"); |
| 10854 | if (opts == (unsigned)-1) |
| 10855 | return EXIT_FAILURE; |
| 10856 | if (opts == 3) { |
| 10857 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10858 | return EXIT_FAILURE; |
| 10859 | } |
| 10860 | argv += optind; |
| 10861 | |
| 10862 | ret = EXIT_SUCCESS; |
| 10863 | while (*argv) { |
| 10864 | if (!(opts & 2)) { /* not -f */ |
| 10865 | if (unset_local_var(*argv)) { |
| 10866 | /* unset <nonexistent_var> doesn't fail. |
| 10867 | * Error is when one tries to unset RO var. |
| 10868 | * Message was printed by unset_local_var. */ |
| 10869 | ret = EXIT_FAILURE; |
| 10870 | } |
| 10871 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10872 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10873 | else { |
| 10874 | unset_func(*argv); |
| 10875 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10876 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10877 | argv++; |
| 10878 | } |
| 10879 | return ret; |
| 10880 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10881 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10882 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10883 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10884 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10885 | * built-in 'set' handler |
| 10886 | * SUSv3 says: |
| 10887 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10888 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10889 | * set -- [argument...] |
| 10890 | * set -o |
| 10891 | * set +o |
| 10892 | * Implementations shall support the options in both their hyphen and |
| 10893 | * plus-sign forms. These options can also be specified as options to sh. |
| 10894 | * Examples: |
| 10895 | * Write out all variables and their values: set |
| 10896 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10897 | * Turn on the -x and -v options: set -xv |
| 10898 | * Unset all positional parameters: set -- |
| 10899 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10900 | * Set the positional parameters to the expansion of x, even if x expands |
| 10901 | * with a leading '-' or '+': set -- $x |
| 10902 | * |
| 10903 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10904 | */ |
| 10905 | static int FAST_FUNC builtin_set(char **argv) |
| 10906 | { |
| 10907 | int n; |
| 10908 | char **pp, **g_argv; |
| 10909 | char *arg = *++argv; |
| 10910 | |
| 10911 | if (arg == NULL) { |
| 10912 | struct variable *e; |
| 10913 | for (e = G.top_var; e; e = e->next) |
| 10914 | puts(e->varstr); |
| 10915 | return EXIT_SUCCESS; |
| 10916 | } |
| 10917 | |
| 10918 | do { |
| 10919 | if (strcmp(arg, "--") == 0) { |
| 10920 | ++argv; |
| 10921 | goto set_argv; |
| 10922 | } |
| 10923 | if (arg[0] != '+' && arg[0] != '-') |
| 10924 | break; |
| 10925 | for (n = 1; arg[n]; ++n) { |
| 10926 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10927 | goto error; |
| 10928 | if (arg[n] == 'o' && argv[1]) |
| 10929 | argv++; |
| 10930 | } |
| 10931 | } while ((arg = *++argv) != NULL); |
| 10932 | /* Now argv[0] is 1st argument */ |
| 10933 | |
| 10934 | if (arg == NULL) |
| 10935 | return EXIT_SUCCESS; |
| 10936 | set_argv: |
| 10937 | |
| 10938 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10939 | g_argv = G.global_argv; |
| 10940 | if (G.global_args_malloced) { |
| 10941 | pp = g_argv; |
| 10942 | while (*++pp) |
| 10943 | free(*pp); |
| 10944 | g_argv[1] = NULL; |
| 10945 | } else { |
| 10946 | G.global_args_malloced = 1; |
| 10947 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10948 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10949 | g_argv = pp; |
| 10950 | } |
| 10951 | /* This realloc's G.global_argv */ |
| 10952 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10953 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10954 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10955 | |
| 10956 | return EXIT_SUCCESS; |
| 10957 | |
| 10958 | /* Nothing known, so abort */ |
| 10959 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10960 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10961 | return EXIT_FAILURE; |
| 10962 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10963 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10964 | |
| 10965 | static int FAST_FUNC builtin_shift(char **argv) |
| 10966 | { |
| 10967 | int n = 1; |
| 10968 | argv = skip_dash_dash(argv); |
| 10969 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10970 | n = bb_strtou(argv[0], NULL, 10); |
| 10971 | if (errno || n < 0) { |
| 10972 | /* shared string with ash.c */ |
| 10973 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10974 | /* |
| 10975 | * ash aborts in this case. |
| 10976 | * bash prints error message and set $? to 1. |
| 10977 | * Interestingly, for "shift 99999" bash does not |
| 10978 | * print error message, but does set $? to 1 |
| 10979 | * (and does no shifting at all). |
| 10980 | */ |
| 10981 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10982 | } |
| 10983 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10984 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10985 | int m = 1; |
| 10986 | while (m <= n) |
| 10987 | free(G.global_argv[m++]); |
| 10988 | } |
| 10989 | G.global_argc -= n; |
| 10990 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10991 | G.global_argc * sizeof(G.global_argv[0])); |
| 10992 | return EXIT_SUCCESS; |
| 10993 | } |
| 10994 | return EXIT_FAILURE; |
| 10995 | } |
| 10996 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10997 | #if ENABLE_HUSH_GETOPTS |
| 10998 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10999 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11000 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 11001 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11002 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11003 | If a required argument is not found, and getopts is not silent, |
| 11004 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 11005 | diagnostic message is printed. If getopts is silent, then a |
| 11006 | colon (:) is placed in VAR and OPTARG is set to the option |
| 11007 | character found. |
| 11008 | |
| 11009 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11010 | |
| 11011 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11012 | */ |
| 11013 | char cbuf[2]; |
| 11014 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11015 | int c, n, exitcode, my_opterr; |
| 11016 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11017 | |
| 11018 | optstring = *++argv; |
| 11019 | if (!optstring || !(var = *++argv)) { |
| 11020 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 11021 | return EXIT_FAILURE; |
| 11022 | } |
| 11023 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11024 | if (argv[1]) |
| 11025 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 11026 | else |
| 11027 | argv = G.global_argv; |
| 11028 | cbuf[1] = '\0'; |
| 11029 | |
| 11030 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11031 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11032 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11033 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11034 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11035 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11036 | |
| 11037 | /* getopts stops on first non-option. Add "+" to force that */ |
| 11038 | /*if (optstring[0] != '+')*/ { |
| 11039 | char *s = alloca(strlen(optstring) + 2); |
| 11040 | sprintf(s, "+%s", optstring); |
| 11041 | optstring = s; |
| 11042 | } |
| 11043 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11044 | /* Naively, now we should just |
| 11045 | * cp = get_local_var_value("OPTIND"); |
| 11046 | * optind = cp ? atoi(cp) : 0; |
| 11047 | * optarg = NULL; |
| 11048 | * opterr = my_opterr; |
| 11049 | * c = getopt(string_array_len(argv), argv, optstring); |
| 11050 | * and be done? Not so fast... |
| 11051 | * Unlike normal getopt() usage in C programs, here |
| 11052 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 11053 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 11054 | * invocations of "getopts", there will be calls to shell builtins |
| 11055 | * which use getopt() internally. Example: |
| 11056 | * while getopts "abc" RES -a -bc -abc de; do |
| 11057 | * unset -ff func |
| 11058 | * done |
| 11059 | * This would not work correctly: getopt() call inside "unset" |
| 11060 | * modifies internal libc state which is tracking position in |
| 11061 | * multi-option strings ("-abc"). At best, it can skip options |
| 11062 | * or return the same option infinitely. With glibc implementation |
| 11063 | * of getopt(), it would use outright invalid pointers and return |
| 11064 | * garbage even _without_ "unset" mangling internal state. |
| 11065 | * |
| 11066 | * We resort to resetting getopt() state and calling it N times, |
| 11067 | * until we get Nth result (or failure). |
| 11068 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 11069 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11070 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11071 | count = 0; |
| 11072 | n = string_array_len(argv); |
| 11073 | do { |
| 11074 | optarg = NULL; |
| 11075 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 11076 | c = getopt(n, argv, optstring); |
| 11077 | if (c < 0) |
| 11078 | break; |
| 11079 | count++; |
| 11080 | } while (count <= G.getopt_count); |
| 11081 | |
| 11082 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 11083 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 11084 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11085 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11086 | |
| 11087 | /* Set OPTARG */ |
| 11088 | /* Always set or unset, never left as-is, even on exit/error: |
| 11089 | * "If no option was found, or if the option that was found |
| 11090 | * does not have an option-argument, OPTARG shall be unset." |
| 11091 | */ |
| 11092 | cp = optarg; |
| 11093 | if (c == '?') { |
| 11094 | /* If ":optstring" and unknown option is seen, |
| 11095 | * it is stored to OPTARG. |
| 11096 | */ |
| 11097 | if (optstring[1] == ':') { |
| 11098 | cbuf[0] = optopt; |
| 11099 | cp = cbuf; |
| 11100 | } |
| 11101 | } |
| 11102 | if (cp) |
| 11103 | set_local_var_from_halves("OPTARG", cp); |
| 11104 | else |
| 11105 | unset_local_var("OPTARG"); |
| 11106 | |
| 11107 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11108 | exitcode = EXIT_SUCCESS; |
| 11109 | if (c < 0) { /* -1: end of options */ |
| 11110 | exitcode = EXIT_FAILURE; |
| 11111 | c = '?'; |
| 11112 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11113 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11114 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11115 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11116 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11117 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11118 | return exitcode; |
| 11119 | } |
| 11120 | #endif |
| 11121 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11122 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11123 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11124 | char *arg_path, *filename; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11125 | HFILE *input; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11126 | save_arg_t sv; |
| 11127 | char *args_need_save; |
| 11128 | #if ENABLE_HUSH_FUNCTIONS |
| 11129 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11130 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11131 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11132 | argv = skip_dash_dash(argv); |
| 11133 | filename = argv[0]; |
| 11134 | if (!filename) { |
| 11135 | /* bash says: "bash: .: filename argument required" */ |
| 11136 | return 2; /* bash compat */ |
| 11137 | } |
| 11138 | arg_path = NULL; |
| 11139 | if (!strchr(filename, '/')) { |
| 11140 | arg_path = find_in_path(filename); |
| 11141 | if (arg_path) |
| 11142 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 11143 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 11144 | errno = ENOENT; |
| 11145 | bb_simple_perror_msg(filename); |
| 11146 | return EXIT_FAILURE; |
| 11147 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11148 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11149 | input = hfopen(filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11150 | free(arg_path); |
| 11151 | if (!input) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11152 | bb_perror_msg("%s", filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11153 | /* POSIX: non-interactive shell should abort here, |
| 11154 | * not merely fail. So far no one complained :) |
| 11155 | */ |
| 11156 | return EXIT_FAILURE; |
| 11157 | } |
| 11158 | |
| 11159 | #if ENABLE_HUSH_FUNCTIONS |
| 11160 | sv_flg = G_flag_return_in_progress; |
| 11161 | /* "we are inside sourced file, ok to use return" */ |
| 11162 | G_flag_return_in_progress = -1; |
| 11163 | #endif |
| 11164 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 11165 | if (args_need_save) |
| 11166 | save_and_replace_G_args(&sv, argv); |
| 11167 | |
| 11168 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 11169 | G.last_exitcode = 0; |
| 11170 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11171 | hfclose(input); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11172 | |
| 11173 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 11174 | restore_G_args(&sv, argv); |
| 11175 | #if ENABLE_HUSH_FUNCTIONS |
| 11176 | G_flag_return_in_progress = sv_flg; |
| 11177 | #endif |
| 11178 | |
| 11179 | return G.last_exitcode; |
| 11180 | } |
| 11181 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11182 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11183 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11184 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11185 | int sig; |
| 11186 | char *new_cmd; |
| 11187 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11188 | if (!G_traps) |
| 11189 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11190 | |
| 11191 | argv++; |
| 11192 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 11193 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11194 | /* No args: print all trapped */ |
| 11195 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11196 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11197 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11198 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 11199 | /* note: bash adds "SIG", but only if invoked |
| 11200 | * as "bash". If called as "sh", or if set -o posix, |
| 11201 | * then it prints short signal names. |
| 11202 | * We are printing short names: */ |
| 11203 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11204 | } |
| 11205 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 11206 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11207 | return EXIT_SUCCESS; |
| 11208 | } |
| 11209 | |
| 11210 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11211 | /* If first arg is a number: reset all specified signals */ |
| 11212 | sig = bb_strtou(*argv, NULL, 10); |
| 11213 | if (errno == 0) { |
| 11214 | int ret; |
| 11215 | process_sig_list: |
| 11216 | ret = EXIT_SUCCESS; |
| 11217 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11218 | sighandler_t handler; |
| 11219 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11220 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 11221 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11222 | ret = EXIT_FAILURE; |
| 11223 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 11224 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11225 | continue; |
| 11226 | } |
| 11227 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11228 | free(G_traps[sig]); |
| 11229 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11230 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 11231 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11232 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11233 | |
| 11234 | /* There is no signal for 0 (EXIT) */ |
| 11235 | if (sig == 0) |
| 11236 | continue; |
| 11237 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11238 | if (new_cmd) |
| 11239 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 11240 | else |
| 11241 | /* We are removing trap handler */ |
| 11242 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 11243 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11244 | } |
| 11245 | return ret; |
| 11246 | } |
| 11247 | |
| 11248 | if (!argv[1]) { /* no second arg */ |
| 11249 | bb_error_msg("trap: invalid arguments"); |
| 11250 | return EXIT_FAILURE; |
| 11251 | } |
| 11252 | |
| 11253 | /* First arg is "-": reset all specified to default */ |
| 11254 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 11255 | /* Everything else: set arg as signal handler |
| 11256 | * (includes "" case, which ignores signal) */ |
| 11257 | if (argv[0][0] == '-') { |
| 11258 | if (argv[0][1] == '\0') { /* "-" */ |
| 11259 | /* new_cmd remains NULL: "reset these sigs" */ |
| 11260 | goto reset_traps; |
| 11261 | } |
| 11262 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 11263 | argv++; |
| 11264 | } |
| 11265 | /* else: "-something", no special meaning */ |
| 11266 | } |
| 11267 | new_cmd = *argv; |
| 11268 | reset_traps: |
| 11269 | argv++; |
| 11270 | goto process_sig_list; |
| 11271 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11272 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11273 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11274 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11275 | static struct pipe *parse_jobspec(const char *str) |
| 11276 | { |
| 11277 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11278 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11279 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11280 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 11281 | if (str[0] != '%' |
| 11282 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 11283 | ) { |
| 11284 | bb_error_msg("bad argument '%s'", str); |
| 11285 | return NULL; |
| 11286 | } |
| 11287 | /* It is "%%", "%+" or "%" - current job */ |
| 11288 | jobnum = G.last_jobid; |
| 11289 | if (jobnum == 0) { |
| 11290 | bb_error_msg("no current job"); |
| 11291 | return NULL; |
| 11292 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11293 | } |
| 11294 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11295 | if (pi->jobid == jobnum) { |
| 11296 | return pi; |
| 11297 | } |
| 11298 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11299 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11300 | return NULL; |
| 11301 | } |
| 11302 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11303 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 11304 | { |
| 11305 | struct pipe *job; |
| 11306 | const char *status_string; |
| 11307 | |
| 11308 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 11309 | for (job = G.job_list; job; job = job->next) { |
| 11310 | if (job->alive_cmds == job->stopped_cmds) |
| 11311 | status_string = "Stopped"; |
| 11312 | else |
| 11313 | status_string = "Running"; |
| 11314 | |
| 11315 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 11316 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11317 | |
| 11318 | clean_up_last_dead_job(); |
| 11319 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11320 | return EXIT_SUCCESS; |
| 11321 | } |
| 11322 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11323 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11324 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11325 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11326 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11327 | struct pipe *pi; |
| 11328 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11329 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11330 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 11331 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11332 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 11333 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11334 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11335 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11336 | goto found; |
| 11337 | } |
| 11338 | } |
| 11339 | bb_error_msg("%s: no current job", argv[0]); |
| 11340 | return EXIT_FAILURE; |
| 11341 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11342 | |
| 11343 | pi = parse_jobspec(argv[1]); |
| 11344 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11345 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11346 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 11347 | /* TODO: bash prints a string representation |
| 11348 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 11349 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11350 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11351 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11352 | } |
| 11353 | |
| 11354 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11355 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 11356 | for (i = 0; i < pi->num_cmds; i++) { |
| 11357 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11358 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11359 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11360 | |
| 11361 | i = kill(- pi->pgrp, SIGCONT); |
| 11362 | if (i < 0) { |
| 11363 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11364 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11365 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11366 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11367 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11368 | } |
| 11369 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11370 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11371 | 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] | 11372 | return checkjobs_and_fg_shell(pi); |
| 11373 | } |
| 11374 | return EXIT_SUCCESS; |
| 11375 | } |
| 11376 | #endif |
| 11377 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11378 | #if ENABLE_HUSH_KILL |
| 11379 | static int FAST_FUNC builtin_kill(char **argv) |
| 11380 | { |
| 11381 | int ret = 0; |
| 11382 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11383 | # if ENABLE_HUSH_JOB |
| 11384 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 11385 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11386 | |
| 11387 | do { |
| 11388 | struct pipe *pi; |
| 11389 | char *dst; |
| 11390 | int j, n; |
| 11391 | |
| 11392 | if (argv[i][0] != '%') |
| 11393 | continue; |
| 11394 | /* |
| 11395 | * "kill %N" - job kill |
| 11396 | * Converting to pgrp / pid kill |
| 11397 | */ |
| 11398 | pi = parse_jobspec(argv[i]); |
| 11399 | if (!pi) { |
| 11400 | /* Eat bad jobspec */ |
| 11401 | j = i; |
| 11402 | do { |
| 11403 | j++; |
| 11404 | argv[j - 1] = argv[j]; |
| 11405 | } while (argv[j]); |
| 11406 | ret = 1; |
| 11407 | i--; |
| 11408 | continue; |
| 11409 | } |
| 11410 | /* |
| 11411 | * In jobs started under job control, we signal |
| 11412 | * entire process group by kill -PGRP_ID. |
| 11413 | * This happens, f.e., in interactive shell. |
| 11414 | * |
| 11415 | * Otherwise, we signal each child via |
| 11416 | * kill PID1 PID2 PID3. |
| 11417 | * Testcases: |
| 11418 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 11419 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 11420 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 11421 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 11422 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11423 | dst = alloca(n * sizeof(int)*4); |
| 11424 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11425 | if (G_interactive_fd) |
| 11426 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11427 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11428 | struct command *cmd = &pi->cmds[j]; |
| 11429 | /* Skip exited members of the job */ |
| 11430 | if (cmd->pid == 0) |
| 11431 | continue; |
| 11432 | /* |
| 11433 | * kill_main has matching code to expect |
| 11434 | * leading space. Needed to not confuse |
| 11435 | * negative pids with "kill -SIGNAL_NO" syntax |
| 11436 | */ |
| 11437 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 11438 | } |
| 11439 | *dst = '\0'; |
| 11440 | } while (argv[++i]); |
| 11441 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11442 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11443 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11444 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11445 | ret = run_applet_main(argv, kill_main); |
| 11446 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11447 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11448 | return ret; |
| 11449 | } |
| 11450 | #endif |
| 11451 | |
| 11452 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11453 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11454 | #if !ENABLE_HUSH_JOB |
| 11455 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 11456 | #endif |
| 11457 | 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] | 11458 | { |
| 11459 | int ret = 0; |
| 11460 | for (;;) { |
| 11461 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11462 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11463 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11464 | if (!sigisemptyset(&G.pending_set)) |
| 11465 | goto check_sig; |
| 11466 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11467 | /* waitpid is not interruptible by SA_RESTARTed |
| 11468 | * signals which we use. Thus, this ugly dance: |
| 11469 | */ |
| 11470 | |
| 11471 | /* Make sure possible SIGCHLD is stored in kernel's |
| 11472 | * pending signal mask before we call waitpid. |
| 11473 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11474 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11475 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11476 | sigfillset(&oldset); /* block all signals, remember old set */ |
Denys Vlasenko | b437df1 | 2018-12-08 15:35:24 +0100 | [diff] [blame] | 11477 | sigprocmask2(SIG_SETMASK, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11478 | |
| 11479 | if (!sigisemptyset(&G.pending_set)) { |
| 11480 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11481 | goto restore; |
| 11482 | } |
| 11483 | |
| 11484 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11485 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11486 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11487 | debug_printf_exec("checkjobs:%d\n", ret); |
| 11488 | #if ENABLE_HUSH_JOB |
| 11489 | if (waitfor_pipe) { |
| 11490 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 11491 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 11492 | if (rcode >= 0) { |
| 11493 | ret = rcode; |
| 11494 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11495 | break; |
| 11496 | } |
| 11497 | } |
| 11498 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11499 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 11500 | /* if ret == 0, no children changed state */ |
| 11501 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11502 | if (errno == ECHILD || ret) { |
| 11503 | ret--; |
| 11504 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11505 | ret = 0; |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11506 | #if ENABLE_HUSH_BASH_COMPAT |
| 11507 | if (waitfor_pid == -1 && errno == ECHILD) { |
| 11508 | /* exitcode of "wait -n" with no children is 127, not 0 */ |
| 11509 | ret = 127; |
| 11510 | } |
| 11511 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11512 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11513 | break; |
| 11514 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11515 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11516 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 11517 | /* Note: sigsuspend invokes signal handler */ |
| 11518 | sigsuspend(&oldset); |
| 11519 | restore: |
| 11520 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11521 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11522 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11523 | sig = check_and_run_traps(); |
| 11524 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 11525 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11526 | ret = 128 + sig; |
| 11527 | break; |
| 11528 | } |
| 11529 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 11530 | } |
| 11531 | return ret; |
| 11532 | } |
| 11533 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11534 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11535 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11536 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11537 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11538 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11539 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11540 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11541 | if (argv[0] && strcmp(argv[0], "-n") == 0) { |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11542 | /* wait -n */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11543 | /* (bash accepts "wait -n PID" too and ignores PID) */ |
| 11544 | G.dead_job_exitcode = -1; |
| 11545 | return wait_for_child_or_signal(NULL, -1 /*no job, wait for one job*/); |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11546 | } |
| 11547 | #endif |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11548 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 11549 | /* Don't care about wait results */ |
| 11550 | /* Note 1: must wait until there are no more children */ |
| 11551 | /* Note 2: must be interruptible */ |
| 11552 | /* Examples: |
| 11553 | * $ sleep 3 & sleep 6 & wait |
| 11554 | * [1] 30934 sleep 3 |
| 11555 | * [2] 30935 sleep 6 |
| 11556 | * [1] Done sleep 3 |
| 11557 | * [2] Done sleep 6 |
| 11558 | * $ sleep 3 & sleep 6 & wait |
| 11559 | * [1] 30936 sleep 3 |
| 11560 | * [2] 30937 sleep 6 |
| 11561 | * [1] Done sleep 3 |
| 11562 | * ^C <-- after ~4 sec from keyboard |
| 11563 | * $ |
| 11564 | */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11565 | 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] | 11566 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11567 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11568 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11569 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11570 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11571 | #if ENABLE_HUSH_JOB |
| 11572 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11573 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11574 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11575 | wait_pipe = parse_jobspec(*argv); |
| 11576 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11577 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11578 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11579 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11580 | } else { |
| 11581 | /* waiting on "last dead job" removes it */ |
| 11582 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 11583 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11584 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11585 | /* else: parse_jobspec() already emitted error msg */ |
| 11586 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11587 | } |
| 11588 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11589 | /* mimic bash message */ |
| 11590 | 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] | 11591 | ret = EXIT_FAILURE; |
| 11592 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11593 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11594 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11595 | /* Do we have such child? */ |
| 11596 | ret = waitpid(pid, &status, WNOHANG); |
| 11597 | if (ret < 0) { |
| 11598 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11599 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11600 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 11601 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11602 | /* "wait $!" but last bg task has already exited. Try: |
| 11603 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 11604 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11605 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11606 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11607 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11608 | } else { |
| 11609 | /* Example: "wait 1". mimic bash message */ |
| 11610 | 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] | 11611 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11612 | } else { |
| 11613 | /* ??? */ |
| 11614 | bb_perror_msg("wait %s", *argv); |
| 11615 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11616 | continue; /* bash checks all argv[] */ |
| 11617 | } |
| 11618 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11619 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11620 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11621 | } else { |
| 11622 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11623 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 11624 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11625 | if (WIFSIGNALED(status)) |
| 11626 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11627 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11628 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11629 | |
| 11630 | return ret; |
| 11631 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11632 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11633 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11634 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 11635 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 11636 | { |
| 11637 | if (argv[1]) { |
| 11638 | def = bb_strtou(argv[1], NULL, 10); |
| 11639 | if (errno || def < def_min || argv[2]) { |
| 11640 | bb_error_msg("%s: bad arguments", argv[0]); |
| 11641 | def = UINT_MAX; |
| 11642 | } |
| 11643 | } |
| 11644 | return def; |
| 11645 | } |
| 11646 | #endif |
| 11647 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11648 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11649 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11650 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11651 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11652 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11653 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11654 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 11655 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 11656 | return EXIT_SUCCESS; /* bash compat */ |
| 11657 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11658 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11659 | |
| 11660 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 11661 | if (depth == UINT_MAX) |
| 11662 | G.flag_break_continue = BC_BREAK; |
| 11663 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11664 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11665 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11666 | return EXIT_SUCCESS; |
| 11667 | } |
| 11668 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11669 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11670 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11671 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 11672 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11673 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11674 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11675 | |
| 11676 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11677 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11678 | { |
| 11679 | int rc; |
| 11680 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11681 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11682 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 11683 | return EXIT_FAILURE; /* bash compat */ |
| 11684 | } |
| 11685 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11686 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11687 | |
| 11688 | /* bash: |
| 11689 | * out of range: wraps around at 256, does not error out |
| 11690 | * non-numeric param: |
| 11691 | * f() { false; return qwe; }; f; echo $? |
| 11692 | * bash: return: qwe: numeric argument required <== we do this |
| 11693 | * 255 <== we also do this |
| 11694 | */ |
| 11695 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 11696 | return rc; |
| 11697 | } |
| 11698 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11699 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11700 | #if ENABLE_HUSH_TIMES |
| 11701 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11702 | { |
| 11703 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11704 | ' ', offsetof(struct tms, tms_utime), |
| 11705 | '\n', offsetof(struct tms, tms_stime), |
| 11706 | ' ', offsetof(struct tms, tms_cutime), |
| 11707 | '\n', offsetof(struct tms, tms_cstime), |
| 11708 | 0 |
| 11709 | }; |
| 11710 | const uint8_t *p; |
| 11711 | unsigned clk_tck; |
| 11712 | struct tms buf; |
| 11713 | |
| 11714 | clk_tck = bb_clk_tck(); |
| 11715 | |
| 11716 | times(&buf); |
| 11717 | p = times_tbl; |
| 11718 | do { |
| 11719 | unsigned sec, frac; |
| 11720 | unsigned long t; |
| 11721 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11722 | sec = t / clk_tck; |
| 11723 | frac = t % clk_tck; |
| 11724 | printf("%um%u.%03us%c", |
| 11725 | sec / 60, sec % 60, |
| 11726 | (frac * 1000) / clk_tck, |
| 11727 | p[0]); |
| 11728 | p += 2; |
| 11729 | } while (*p); |
| 11730 | |
| 11731 | return EXIT_SUCCESS; |
| 11732 | } |
| 11733 | #endif |
| 11734 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11735 | #if ENABLE_HUSH_MEMLEAK |
| 11736 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11737 | { |
| 11738 | void *p; |
| 11739 | unsigned long l; |
| 11740 | |
| 11741 | # ifdef M_TRIM_THRESHOLD |
| 11742 | /* Optional. Reduces probability of false positives */ |
| 11743 | malloc_trim(0); |
| 11744 | # endif |
| 11745 | /* Crude attempt to find where "free memory" starts, |
| 11746 | * sans fragmentation. */ |
| 11747 | p = malloc(240); |
| 11748 | l = (unsigned long)p; |
| 11749 | free(p); |
| 11750 | p = malloc(3400); |
| 11751 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11752 | free(p); |
| 11753 | |
| 11754 | |
| 11755 | # if 0 /* debug */ |
| 11756 | { |
| 11757 | struct mallinfo mi = mallinfo(); |
| 11758 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11759 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11760 | } |
| 11761 | # endif |
| 11762 | |
| 11763 | if (!G.memleak_value) |
| 11764 | G.memleak_value = l; |
| 11765 | |
| 11766 | l -= G.memleak_value; |
| 11767 | if ((long)l < 0) |
| 11768 | l = 0; |
| 11769 | l /= 1024; |
| 11770 | if (l > 127) |
| 11771 | l = 127; |
| 11772 | |
| 11773 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11774 | return l; |
| 11775 | } |
| 11776 | #endif |