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 | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 96 | //config: bool "hush (64 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 |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 160 | //config: bool "Support process 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 |
| 360 | #ifndef F_DUPFD_CLOEXEC |
| 361 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 362 | #endif |
| 363 | #ifndef PIPE_BUF |
| 364 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 365 | #endif |
| 366 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 367 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 368 | /* So far, all bash compat is controlled by one config option */ |
| 369 | /* Separate defines document which part of code implements what */ |
| 370 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 371 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 372 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 373 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 374 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 375 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 376 | |
| 377 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 378 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 379 | #define LEAK_HUNTING 0 |
| 380 | #define BUILD_AS_NOMMU 0 |
| 381 | /* Enable/disable sanity checks. Ok to enable in production, |
| 382 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 383 | * Keeping 1 for now even in released versions. |
| 384 | */ |
| 385 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 386 | /* Slightly bigger (+200 bytes), but faster hush. |
| 387 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 388 | * which allows us to do fewer waitpid's. |
| 389 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 390 | * and therefore waitpid will return the same result as last time) |
| 391 | */ |
| 392 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 393 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 394 | * So far ${var%...} ops are always enabled: |
| 395 | */ |
| 396 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 397 | |
| 398 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 399 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 400 | # undef BB_MMU |
| 401 | # undef USE_FOR_NOMMU |
| 402 | # undef USE_FOR_MMU |
| 403 | # define BB_MMU 0 |
| 404 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 405 | # define USE_FOR_MMU(...) |
| 406 | #endif |
| 407 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 408 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 409 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 410 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 411 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 412 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 413 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 414 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 415 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 416 | # define IF_FEATURE_SH_STANDALONE(...) |
| 417 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 418 | #endif |
| 419 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 420 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 421 | # undef ENABLE_FEATURE_EDITING |
| 422 | # define ENABLE_FEATURE_EDITING 0 |
| 423 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 424 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 425 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 426 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 427 | #endif |
| 428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 429 | /* Do we support ANY keywords? */ |
| 430 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 431 | # define HAS_KEYWORDS 1 |
| 432 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 433 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 434 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 435 | # define HAS_KEYWORDS 0 |
| 436 | # define IF_HAS_KEYWORDS(...) |
| 437 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 438 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 439 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 440 | /* If you comment out one of these below, it will be #defined later |
| 441 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 442 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 443 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf_parse(...) do {} while (0) |
| 445 | #define debug_print_tree(a, b) do {} while (0) |
| 446 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 447 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 448 | #define debug_printf_jobs(...) do {} while (0) |
| 449 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 450 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 451 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 452 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 453 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 454 | #define debug_printf_subst(...) do {} while (0) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 455 | #define debug_printf_prompt(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 456 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 458 | #define ERR_PTR ((void*)(long)1) |
| 459 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 460 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 461 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 462 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 463 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 464 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 465 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 466 | /* Support / and // replace ops */ |
| 467 | /* Note that // is stored as \ in "encoded" string representation */ |
| 468 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 469 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 470 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 471 | #else |
| 472 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 473 | # define VAR_SUBST_OPS "%#:-=+?" |
| 474 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 475 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 476 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 477 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 478 | #define SPECIAL_VAR_SYMBOL 3 |
| 479 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 480 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 481 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 482 | struct variable; |
| 483 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 484 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 485 | |
| 486 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 487 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 488 | */ |
| 489 | #if !BB_MMU |
| 490 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 491 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 492 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 493 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 494 | } nommu_save_t; |
| 495 | #endif |
| 496 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 497 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 498 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 499 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 500 | RES_IF , |
| 501 | RES_THEN , |
| 502 | RES_ELIF , |
| 503 | RES_ELSE , |
| 504 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 505 | #endif |
| 506 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 507 | RES_FOR , |
| 508 | RES_WHILE , |
| 509 | RES_UNTIL , |
| 510 | RES_DO , |
| 511 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 512 | #endif |
| 513 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 514 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 515 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 516 | #if ENABLE_HUSH_CASE |
| 517 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 518 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 519 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 520 | RES_MATCH , /* "word)" */ |
| 521 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 522 | RES_ESAC , |
| 523 | #endif |
| 524 | RES_XXXX , |
| 525 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 526 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 527 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 528 | typedef struct o_string { |
| 529 | char *data; |
| 530 | int length; /* position where data is appended */ |
| 531 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 532 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 533 | /* At least some part of the string was inside '' or "", |
| 534 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 535 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 536 | smallint has_empty_slot; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 537 | } o_string; |
| 538 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 539 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 540 | EXP_FLAG_GLOB = 0x2, |
| 541 | /* Protect newly added chars against globbing |
| 542 | * by prepending \ to *, ?, [, \ */ |
| 543 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 544 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 545 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 546 | #define NULL_O_STRING { NULL } |
| 547 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 548 | #ifndef debug_printf_parse |
| 549 | static const char *const assignment_flag[] = { |
| 550 | "MAYBE_ASSIGNMENT", |
| 551 | "DEFINITELY_ASSIGNMENT", |
| 552 | "NOT_ASSIGNMENT", |
| 553 | "WORD_IS_KEYWORD", |
| 554 | }; |
| 555 | #endif |
| 556 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 557 | typedef struct in_str { |
| 558 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 559 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 560 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 561 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 562 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 563 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 564 | /* The descrip member of this structure is only used to make |
| 565 | * debugging output pretty */ |
| 566 | static const struct { |
| 567 | int mode; |
| 568 | signed char default_fd; |
| 569 | char descrip[3]; |
| 570 | } redir_table[] = { |
| 571 | { O_RDONLY, 0, "<" }, |
| 572 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 573 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 574 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 575 | { O_RDONLY, 0, "<<" }, |
| 576 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 577 | /* { O_RDONLY, 77, "<<" }, */ |
| 578 | }; |
| 579 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 580 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 581 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 582 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 583 | int rd_fd; /* fd to redirect */ |
| 584 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 585 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 586 | smallint rd_type; /* (enum redir_type) */ |
| 587 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 588 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 589 | * bit 0: do we need to trim leading tabs? |
| 590 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 591 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 592 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 593 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 594 | REDIRECT_INPUT = 0, |
| 595 | REDIRECT_OVERWRITE = 1, |
| 596 | REDIRECT_APPEND = 2, |
| 597 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 598 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 599 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 600 | |
| 601 | REDIRFD_CLOSE = -3, |
| 602 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 603 | REDIRFD_TO_FILE = -1, |
| 604 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 605 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 606 | HEREDOC_SKIPTABS = 1, |
| 607 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 608 | } redir_type; |
| 609 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 611 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 612 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 613 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 614 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 615 | unsigned lineno; |
| 616 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 617 | smallint cmd_type; /* CMD_xxx */ |
| 618 | #define CMD_NORMAL 0 |
| 619 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 620 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 621 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 622 | * "export v=t*" |
| 623 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 624 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 625 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 626 | #if ENABLE_HUSH_FUNCTIONS |
| 627 | # define CMD_FUNCDEF 3 |
| 628 | #endif |
| 629 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 630 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 631 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 632 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 633 | #if !BB_MMU |
| 634 | char *group_as_string; |
| 635 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 636 | #if ENABLE_HUSH_FUNCTIONS |
| 637 | struct function *child_func; |
| 638 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 639 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 640 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 641 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 642 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 643 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 644 | * we put those fields back into cmd->xxx |
| 645 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 646 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 647 | * Without this trick, loop would execute a;b;b;b;... |
| 648 | * instead of correct sequence a;b;a;b;... |
| 649 | * When command is freed, it severs the link |
| 650 | * (sets ->child_func->parent_cmd to NULL). |
| 651 | */ |
| 652 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 653 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 654 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 655 | * and on execution these are substituted with their values. |
| 656 | * Substitution can make _several_ words out of one argv[n]! |
| 657 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 658 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 659 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 660 | struct redir_struct *redirects; /* I/O redirections */ |
| 661 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 662 | /* Is there anything in this command at all? */ |
| 663 | #define IS_NULL_CMD(cmd) \ |
| 664 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 665 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 666 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 667 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 668 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 669 | int alive_cmds; /* number of commands running (not exited) */ |
| 670 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 671 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 672 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 673 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 674 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 675 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 676 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 677 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 678 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 679 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 680 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 681 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 682 | PIPE_SEQ = 0, |
| 683 | PIPE_AND = 1, |
| 684 | PIPE_OR = 2, |
| 685 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 686 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 687 | /* Is there anything in this pipe at all? */ |
| 688 | #define IS_NULL_PIPE(pi) \ |
| 689 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 690 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 691 | /* This holds pointers to the various results of parsing */ |
| 692 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 693 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 694 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 695 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 696 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 697 | /* last command in pipe (being constructed right now) */ |
| 698 | struct command *command; |
| 699 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 700 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 701 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 702 | #if !BB_MMU |
| 703 | o_string as_string; |
| 704 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 705 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 706 | #if HAS_KEYWORDS |
| 707 | smallint ctx_res_w; |
| 708 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 709 | #if ENABLE_HUSH_CASE |
| 710 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 711 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 712 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 713 | int old_flag; |
| 714 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 715 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 716 | * when we see "if" or "then", we malloc and copy current context, |
| 717 | * and make ->stack point to it. then we parse pipeN. |
| 718 | * when closing "then" / fi" / whatever is found, |
| 719 | * we move list_head into ->stack->command->group, |
| 720 | * copy ->stack into current context, and delete ->stack. |
| 721 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 722 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 723 | struct parse_context *stack; |
| 724 | #endif |
| 725 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 726 | enum { |
| 727 | MAYBE_ASSIGNMENT = 0, |
| 728 | DEFINITELY_ASSIGNMENT = 1, |
| 729 | NOT_ASSIGNMENT = 2, |
| 730 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 731 | WORD_IS_KEYWORD = 3, |
| 732 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 733 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 734 | /* On program start, environ points to initial environment. |
| 735 | * putenv adds new pointers into it, unsetenv removes them. |
| 736 | * Neither of these (de)allocates the strings. |
| 737 | * setenv allocates new strings in malloc space and does putenv, |
| 738 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 739 | #define setenv(...) setenv_is_leaky_dont_use() |
| 740 | struct variable { |
| 741 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 742 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 743 | 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] | 744 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 745 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 746 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 747 | }; |
| 748 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 749 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 750 | BC_BREAK = 1, |
| 751 | BC_CONTINUE = 2, |
| 752 | }; |
| 753 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 754 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 755 | struct function { |
| 756 | struct function *next; |
| 757 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 758 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 759 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 760 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 761 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 762 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 763 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 764 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 765 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 766 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 767 | /* set -/+o OPT support. (TODO: make it optional) |
| 768 | * bash supports the following opts: |
| 769 | * allexport off |
| 770 | * braceexpand on |
| 771 | * emacs on |
| 772 | * errexit off |
| 773 | * errtrace off |
| 774 | * functrace off |
| 775 | * hashall on |
| 776 | * histexpand off |
| 777 | * history on |
| 778 | * ignoreeof off |
| 779 | * interactive-comments on |
| 780 | * keyword off |
| 781 | * monitor on |
| 782 | * noclobber off |
| 783 | * noexec off |
| 784 | * noglob off |
| 785 | * nolog off |
| 786 | * notify off |
| 787 | * nounset off |
| 788 | * onecmd off |
| 789 | * physical off |
| 790 | * pipefail off |
| 791 | * posix off |
| 792 | * privileged off |
| 793 | * verbose off |
| 794 | * vi off |
| 795 | * xtrace off |
| 796 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 797 | static const char o_opt_strings[] ALIGN1 = |
| 798 | "pipefail\0" |
| 799 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 800 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 801 | #if ENABLE_HUSH_MODE_X |
| 802 | "xtrace\0" |
| 803 | #endif |
| 804 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 805 | enum { |
| 806 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 807 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 808 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 809 | #if ENABLE_HUSH_MODE_X |
| 810 | OPT_O_XTRACE, |
| 811 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 812 | NUM_OPT_O |
| 813 | }; |
| 814 | |
| 815 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 816 | struct FILE_list { |
| 817 | struct FILE_list *next; |
| 818 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 819 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 820 | }; |
| 821 | |
| 822 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 823 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 824 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 825 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 826 | /* interactive_fd != 0 means we are an interactive shell. |
| 827 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 828 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 829 | * job control still works, but terminal signals |
| 830 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 831 | * process groups can only be created with "cmd &". |
| 832 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 833 | * to give tty to the foreground process group, |
| 834 | * and will take it back when the group is stopped (^Z) |
| 835 | * or killed (^C). |
| 836 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 837 | #if ENABLE_HUSH_INTERACTIVE |
| 838 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 839 | * _AND_ if we decided to act interactively */ |
| 840 | int interactive_fd; |
| 841 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 842 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 843 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 844 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 845 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 846 | #endif |
| 847 | #if ENABLE_FEATURE_EDITING |
| 848 | line_input_t *line_input_state; |
| 849 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 850 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 851 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 852 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 853 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 854 | random_t random_gen; |
| 855 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 856 | #if ENABLE_HUSH_JOB |
| 857 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 858 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 859 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 860 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 861 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 862 | #else |
| 863 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 864 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 865 | /* How deeply are we in context where "set -e" is ignored */ |
| 866 | int errexit_depth; |
| 867 | /* "set -e" rules (do we follow them correctly?): |
| 868 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 869 | * Shell does not exit if failed command is part of condition in |
| 870 | * if/while, part of && or || list except the last command, any command |
| 871 | * in a pipe but the last, or if the command's return value is being |
| 872 | * inverted with !. If a compound command other than a subshell returns a |
| 873 | * non-zero status because a command failed while -e was being ignored, the |
| 874 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 875 | * exits [ERR is a bashism]. |
| 876 | * |
| 877 | * If a compound command or function executes in a context where -e is |
| 878 | * ignored, none of the commands executed within are affected by the -e |
| 879 | * setting. If a compound command or function sets -e while executing in a |
| 880 | * context where -e is ignored, that setting does not have any effect until |
| 881 | * the compound command or the command containing the function call completes. |
| 882 | */ |
| 883 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 884 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 885 | #if ENABLE_HUSH_MODE_X |
| 886 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 887 | #else |
| 888 | # define G_x_mode 0 |
| 889 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 890 | #if ENABLE_HUSH_INTERACTIVE |
| 891 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 892 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 893 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 894 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 895 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 896 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 897 | #if ENABLE_HUSH_FUNCTIONS |
| 898 | /* 0: outside of a function (or sourced file) |
| 899 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 900 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 901 | */ |
| 902 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 903 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 904 | #else |
| 905 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 906 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 907 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 908 | /* These support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 909 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 910 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 911 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 912 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 913 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 914 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 915 | # define G_global_args_malloced (G.global_args_malloced) |
| 916 | #else |
| 917 | # define G_global_args_malloced 0 |
| 918 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 919 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 920 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 921 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 922 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 923 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 924 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 925 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 926 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 927 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 928 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 929 | #if ENABLE_HUSH_GETOPTS |
| 930 | unsigned getopt_count; |
| 931 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 932 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 933 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 934 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 935 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 936 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 937 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 938 | unsigned var_nest_level; |
| 939 | #if ENABLE_HUSH_FUNCTIONS |
| 940 | # if ENABLE_HUSH_LOCAL |
| 941 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 942 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 943 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 944 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 945 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 946 | #if ENABLE_HUSH_FAST |
| 947 | unsigned count_SIGCHLD; |
| 948 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 949 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 950 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 951 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 952 | unsigned lineno; |
| 953 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 954 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 955 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 956 | /* Which signals have non-DFL handler (even with no traps set)? |
| 957 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 958 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 959 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 960 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 961 | * Other than these two times, never modified. |
| 962 | */ |
| 963 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 964 | #if ENABLE_HUSH_JOB |
| 965 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 966 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 967 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 968 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 969 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 970 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 971 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 972 | # define G_traps G.traps |
| 973 | #else |
| 974 | # define G_traps ((char**)NULL) |
| 975 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 976 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 977 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 978 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 979 | #endif |
| 980 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 981 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 982 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 983 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 984 | #if ENABLE_FEATURE_EDITING |
| 985 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 986 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 987 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 988 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 989 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 990 | * (too many defines). Also, I actually prefer to see when a variable |
| 991 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 992 | #define INIT_G() do { \ |
| 993 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 994 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 995 | sigfillset(&G.sa.sa_mask); \ |
| 996 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 997 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 998 | |
| 999 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1000 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1001 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1002 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1003 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1004 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1005 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1006 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1007 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1008 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1009 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1010 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1011 | #if ENABLE_HUSH_READONLY |
| 1012 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1013 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1014 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1015 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1016 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1017 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1018 | #if ENABLE_HUSH_GETOPTS |
| 1019 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1020 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1021 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1022 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1023 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1024 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1025 | static int builtin_history(char **argv) FAST_FUNC; |
| 1026 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1027 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1028 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1029 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1030 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1031 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1032 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1033 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1034 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1035 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1036 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1037 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1038 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1039 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1040 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1041 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1042 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1043 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1044 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1045 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1046 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1047 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1048 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1049 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1050 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1051 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1052 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1053 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1054 | #if ENABLE_HUSH_TIMES |
| 1055 | static int builtin_times(char **argv) FAST_FUNC; |
| 1056 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1057 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1058 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1059 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1060 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1061 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1062 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1063 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1064 | #if ENABLE_HUSH_KILL |
| 1065 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1066 | #endif |
| 1067 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1068 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1069 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1070 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1071 | static int builtin_break(char **argv) FAST_FUNC; |
| 1072 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1073 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1074 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1075 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1076 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* Table of built-in functions. They can be forked or not, depending on |
| 1079 | * context: within pipes, they fork. As simple commands, they do not. |
| 1080 | * When used in non-forking context, they can change global variables |
| 1081 | * in the parent shell process. If forked, of course they cannot. |
| 1082 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1083 | * still be set at the end. */ |
| 1084 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1085 | const char *b_cmd; |
| 1086 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1087 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1088 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1089 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1090 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1091 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1092 | #endif |
| 1093 | }; |
| 1094 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1095 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1096 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1097 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1098 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1099 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1100 | #endif |
| 1101 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1102 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1103 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1104 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1105 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1106 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1107 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1108 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1109 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1110 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1111 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1112 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1113 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1114 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1115 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1116 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1117 | #if ENABLE_HUSH_GETOPTS |
| 1118 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1119 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1120 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1121 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1122 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1123 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1124 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1125 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1126 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1127 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1128 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1129 | #if ENABLE_HUSH_KILL |
| 1130 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1131 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1132 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1133 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1134 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1135 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1136 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1137 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1138 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1139 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1140 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1141 | #if ENABLE_HUSH_READONLY |
| 1142 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1143 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1144 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1145 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1146 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1147 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1148 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1149 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1150 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1151 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1152 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1153 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1154 | #if ENABLE_HUSH_TIMES |
| 1155 | BLTIN("times" , builtin_times , NULL), |
| 1156 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1157 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1158 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1159 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1160 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1161 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1162 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1163 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1164 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1165 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1166 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1167 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1168 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1169 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1170 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1171 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1172 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1173 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1174 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1175 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1176 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1177 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1178 | * (it's cheaper to run an external program in this case): |
| 1179 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1180 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1181 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1182 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1183 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1184 | #if BASH_TEST2 |
| 1185 | BLTIN("[[" , builtin_test , NULL), |
| 1186 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1187 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1188 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1189 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1190 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1191 | BLTIN("printf" , builtin_printf , NULL), |
| 1192 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1193 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1194 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1195 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1196 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1197 | }; |
| 1198 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1199 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1200 | /* Debug printouts. |
| 1201 | */ |
| 1202 | #if HUSH_DEBUG |
| 1203 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1204 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1205 | # define debug_enter() (G.debug_indent++) |
| 1206 | # define debug_leave() (G.debug_indent--) |
| 1207 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1208 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1209 | # define debug_enter() ((void)0) |
| 1210 | # define debug_leave() ((void)0) |
| 1211 | #endif |
| 1212 | |
| 1213 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1214 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1215 | #endif |
| 1216 | |
| 1217 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1218 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1219 | #endif |
| 1220 | |
| 1221 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1222 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1223 | #endif |
| 1224 | |
| 1225 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1226 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1227 | #endif |
| 1228 | |
| 1229 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1230 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1231 | # define DEBUG_JOBS 1 |
| 1232 | #else |
| 1233 | # define DEBUG_JOBS 0 |
| 1234 | #endif |
| 1235 | |
| 1236 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1237 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1238 | # define DEBUG_EXPAND 1 |
| 1239 | #else |
| 1240 | # define DEBUG_EXPAND 0 |
| 1241 | #endif |
| 1242 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1243 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1244 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1245 | #endif |
| 1246 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1247 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1248 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1249 | # define DEBUG_GLOB 1 |
| 1250 | #else |
| 1251 | # define DEBUG_GLOB 0 |
| 1252 | #endif |
| 1253 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1254 | #ifndef debug_printf_redir |
| 1255 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1256 | #endif |
| 1257 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1258 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1259 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1260 | #endif |
| 1261 | |
| 1262 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1263 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1264 | #endif |
| 1265 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1266 | #ifndef debug_printf_prompt |
| 1267 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1268 | #endif |
| 1269 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1270 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1271 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1272 | # define DEBUG_CLEAN 1 |
| 1273 | #else |
| 1274 | # define DEBUG_CLEAN 0 |
| 1275 | #endif |
| 1276 | |
| 1277 | #if DEBUG_EXPAND |
| 1278 | static void debug_print_strings(const char *prefix, char **vv) |
| 1279 | { |
| 1280 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1281 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1282 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1283 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1284 | } |
| 1285 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1286 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1287 | #endif |
| 1288 | |
| 1289 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1290 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1291 | */ |
| 1292 | #if LEAK_HUNTING |
| 1293 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1294 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1295 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1296 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1297 | return ptr; |
| 1298 | } |
| 1299 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1300 | { |
| 1301 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1302 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1303 | return ptr; |
| 1304 | } |
| 1305 | static char *xxstrdup(int lineno, const char *str) |
| 1306 | { |
| 1307 | char *ptr = xstrdup(str); |
| 1308 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1309 | return ptr; |
| 1310 | } |
| 1311 | static void xxfree(void *ptr) |
| 1312 | { |
| 1313 | fdprintf(2, "free %p\n", ptr); |
| 1314 | free(ptr); |
| 1315 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1316 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1317 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1318 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1319 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1320 | #endif |
| 1321 | |
| 1322 | |
| 1323 | /* Syntax and runtime errors. They always abort scripts. |
| 1324 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1325 | * and return to the prompt. |
| 1326 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1327 | */ |
| 1328 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1329 | # 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] | 1330 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1331 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1332 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1333 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1334 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1335 | #endif |
| 1336 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1337 | static void die_if_script(void) |
| 1338 | { |
| 1339 | if (!G_interactive_fd) { |
| 1340 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1341 | xfunc_error_retval = G.last_exitcode; |
| 1342 | xfunc_die(); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1347 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1348 | va_list p; |
| 1349 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1350 | #if HUSH_DEBUG >= 2 |
| 1351 | bb_error_msg("hush.c:%u", lineno); |
| 1352 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1353 | va_start(p, fmt); |
| 1354 | bb_verror_msg(fmt, p, NULL); |
| 1355 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1356 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1357 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1358 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1359 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1360 | { |
| 1361 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1362 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1363 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1364 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1365 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1368 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1369 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1370 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1371 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1374 | static void syntax_error_unterm_str(unsigned lineno UNUSED_PARAM, const char *s) |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1375 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1376 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1377 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1378 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1379 | } |
| 1380 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1381 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1382 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1383 | char msg[2] = { ch, '\0' }; |
| 1384 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1387 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1388 | { |
| 1389 | char msg[2]; |
| 1390 | msg[0] = ch; |
| 1391 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1392 | #if HUSH_DEBUG >= 2 |
| 1393 | bb_error_msg("hush.c:%u", lineno); |
| 1394 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1395 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1396 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1399 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1400 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1401 | # undef syntax_error |
| 1402 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1403 | # undef syntax_error_unterm_ch |
| 1404 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1405 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1406 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1407 | # 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] | 1408 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1409 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1410 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1411 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1412 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1413 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1414 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1415 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1416 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1417 | static void cmdedit_update_prompt(void); |
| 1418 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1419 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1420 | #endif |
| 1421 | |
| 1422 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1423 | /* Utility functions |
| 1424 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1425 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1426 | static char *unbackslash(char *src) |
| 1427 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1428 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1429 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1430 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1431 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1432 | if (*src != '\0') { |
| 1433 | /* \x -> x */ |
| 1434 | *dst++ = *src++; |
| 1435 | continue; |
| 1436 | } |
| 1437 | /* else: "\<nul>". Do not delete this backslash. |
| 1438 | * Testcase: eval 'echo ok\' |
| 1439 | */ |
| 1440 | *dst++ = '\\'; |
| 1441 | /* fallthrough */ |
| 1442 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1443 | if ((*dst++ = *src++) == '\0') |
| 1444 | break; |
| 1445 | } |
| 1446 | return dst; |
| 1447 | } |
| 1448 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1449 | 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] | 1450 | { |
| 1451 | int i; |
| 1452 | unsigned count1; |
| 1453 | unsigned count2; |
| 1454 | char **v; |
| 1455 | |
| 1456 | v = strings; |
| 1457 | count1 = 0; |
| 1458 | if (v) { |
| 1459 | while (*v) { |
| 1460 | count1++; |
| 1461 | v++; |
| 1462 | } |
| 1463 | } |
| 1464 | count2 = 0; |
| 1465 | v = add; |
| 1466 | while (*v) { |
| 1467 | count2++; |
| 1468 | v++; |
| 1469 | } |
| 1470 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1471 | v[count1 + count2] = NULL; |
| 1472 | i = count2; |
| 1473 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1474 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1475 | return v; |
| 1476 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1477 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1478 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1479 | { |
| 1480 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1481 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1482 | return ptr; |
| 1483 | } |
| 1484 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1485 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1486 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1487 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1488 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1489 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1490 | { |
| 1491 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1492 | v[0] = add; |
| 1493 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1494 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1495 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1496 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1497 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1498 | { |
| 1499 | char **ptr = add_string_to_strings(strings, add); |
| 1500 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1501 | return ptr; |
| 1502 | } |
| 1503 | #define add_string_to_strings(strings, add) \ |
| 1504 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1505 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1506 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1507 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1508 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1509 | char **v; |
| 1510 | |
| 1511 | if (!strings) |
| 1512 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1513 | v = strings; |
| 1514 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1515 | free(*v); |
| 1516 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1517 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1518 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1521 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1522 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1523 | int newfd; |
| 1524 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1525 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1526 | if (newfd >= 0) { |
| 1527 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1528 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1529 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1530 | if (errno == EBUSY) |
| 1531 | goto repeat; |
| 1532 | if (errno == EINTR) |
| 1533 | goto repeat; |
| 1534 | } |
| 1535 | return newfd; |
| 1536 | } |
| 1537 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1538 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1539 | { |
| 1540 | int newfd; |
| 1541 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1542 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1543 | if (newfd < 0) { |
| 1544 | if (errno == EBUSY) |
| 1545 | goto repeat; |
| 1546 | if (errno == EINTR) |
| 1547 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1548 | /* fd was not open? */ |
| 1549 | if (errno == EBADF) |
| 1550 | return fd; |
| 1551 | xfunc_die(); |
| 1552 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1553 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1554 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1555 | close(fd); |
| 1556 | return newfd; |
| 1557 | } |
| 1558 | |
| 1559 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1560 | /* Manipulating the list of open FILEs */ |
| 1561 | static FILE *remember_FILE(FILE *fp) |
| 1562 | { |
| 1563 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1564 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1565 | n->next = G.FILE_list; |
| 1566 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1567 | n->fp = fp; |
| 1568 | n->fd = fileno(fp); |
| 1569 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1570 | } |
| 1571 | return fp; |
| 1572 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1573 | static void fclose_and_forget(FILE *fp) |
| 1574 | { |
| 1575 | struct FILE_list **pp = &G.FILE_list; |
| 1576 | while (*pp) { |
| 1577 | struct FILE_list *cur = *pp; |
| 1578 | if (cur->fp == fp) { |
| 1579 | *pp = cur->next; |
| 1580 | free(cur); |
| 1581 | break; |
| 1582 | } |
| 1583 | pp = &cur->next; |
| 1584 | } |
| 1585 | fclose(fp); |
| 1586 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1587 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1588 | { |
| 1589 | struct FILE_list *fl = G.FILE_list; |
| 1590 | while (fl) { |
| 1591 | if (fd == fl->fd) { |
| 1592 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1593 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1594 | debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1595 | return 1; |
| 1596 | } |
| 1597 | fl = fl->next; |
| 1598 | } |
| 1599 | return 0; |
| 1600 | } |
| 1601 | static void restore_redirected_FILEs(void) |
| 1602 | { |
| 1603 | struct FILE_list *fl = G.FILE_list; |
| 1604 | while (fl) { |
| 1605 | int should_be = fileno(fl->fp); |
| 1606 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1607 | debug_printf_redir("restoring script fd from %d to %d\n", fl->fd, should_be); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1608 | xmove_fd(fl->fd, should_be); |
| 1609 | fl->fd = should_be; |
| 1610 | } |
| 1611 | fl = fl->next; |
| 1612 | } |
| 1613 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1614 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1615 | static void close_all_FILE_list(void) |
| 1616 | { |
| 1617 | struct FILE_list *fl = G.FILE_list; |
| 1618 | while (fl) { |
| 1619 | /* fclose would also free FILE object. |
| 1620 | * It is disastrous if we share memory with a vforked parent. |
| 1621 | * I'm not sure we never come here after vfork. |
| 1622 | * Therefore just close fd, nothing more. |
| 1623 | */ |
| 1624 | /*fclose(fl->fp); - unsafe */ |
| 1625 | close(fl->fd); |
| 1626 | fl = fl->next; |
| 1627 | } |
| 1628 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1629 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1630 | static int fd_in_FILEs(int fd) |
| 1631 | { |
| 1632 | struct FILE_list *fl = G.FILE_list; |
| 1633 | while (fl) { |
| 1634 | if (fl->fd == fd) |
| 1635 | return 1; |
| 1636 | fl = fl->next; |
| 1637 | } |
| 1638 | return 0; |
| 1639 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1640 | |
| 1641 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1642 | /* Helpers for setting new $n and restoring them back |
| 1643 | */ |
| 1644 | typedef struct save_arg_t { |
| 1645 | char *sv_argv0; |
| 1646 | char **sv_g_argv; |
| 1647 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1648 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1649 | } save_arg_t; |
| 1650 | |
| 1651 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1652 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1653 | sv->sv_argv0 = argv[0]; |
| 1654 | sv->sv_g_argv = G.global_argv; |
| 1655 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1656 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1657 | |
| 1658 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1659 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1660 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1661 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1662 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1666 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1667 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1668 | if (G.global_args_malloced) { |
| 1669 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1670 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1671 | while (*++pp) /* note: does not free $0 */ |
| 1672 | free(*pp); |
| 1673 | free(G.global_argv); |
| 1674 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1675 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1676 | argv[0] = sv->sv_argv0; |
| 1677 | G.global_argv = sv->sv_g_argv; |
| 1678 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1679 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1683 | /* Basic theory of signal handling in shell |
| 1684 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1685 | * This does not describe what hush does, rather, it is current understanding |
| 1686 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1687 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1688 | * |
| 1689 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1690 | * is finished or backgrounded. It is the same in interactive and |
| 1691 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1692 | * 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] | 1693 | * ^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] | 1694 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1695 | * pipe. |
| 1696 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1697 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1698 | * or by SIGINT in interactive shell. |
| 1699 | * |
| 1700 | * Trap handlers will execute even within trap handlers. (right?) |
| 1701 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1702 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1703 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1704 | * |
| 1705 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1706 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1707 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1708 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1709 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1710 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1711 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1712 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1713 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1714 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1715 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1716 | * |
| 1717 | * SIGQUIT: ignore |
| 1718 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1719 | * SIGHUP (interactive): |
| 1720 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1721 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1722 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1723 | * that all pipe members are stopped. Try this in bash: |
| 1724 | * while :; do :; done - ^Z does not background it |
| 1725 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1726 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1727 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1728 | * to interactive shell while shell is waiting for a pipe, |
| 1729 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1730 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1731 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1732 | * Example 2: this does not wait and does not execute ls: |
| 1733 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1734 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1735 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1736 | * Example 4: this does not wait and does not execute ls: |
| 1737 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1738 | * |
| 1739 | * (What happens to signals which are IGN on shell start?) |
| 1740 | * (What happens with signal mask on shell start?) |
| 1741 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1742 | * Old implementation |
| 1743 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1744 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1745 | * We block all signals which we don't want to take action immediately, |
| 1746 | * i.e. we block all signals which need to have special handling as described |
| 1747 | * above, and all signals which have traps set. |
| 1748 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1749 | * and act on them. |
| 1750 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1751 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1752 | * sigset_t blocked_set: current blocked signal set |
| 1753 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1754 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1755 | * 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] | 1756 | * "trap 'cmd' SIGxxx": |
| 1757 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1758 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1759 | * unblock signals with special interactive handling |
| 1760 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1761 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1762 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1763 | * 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] | 1764 | * 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] | 1765 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1766 | * 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] | 1767 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1768 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1769 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1770 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1771 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1772 | * 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] | 1773 | * 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] | 1774 | * |
| 1775 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1776 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1777 | * masked signals are not visible! |
| 1778 | * |
| 1779 | * New implementation |
| 1780 | * ================== |
| 1781 | * We record each signal we are interested in by installing signal handler |
| 1782 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1783 | * We are interested in: signals which need to have special handling |
| 1784 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1785 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1786 | * After each pipe execution, we extract any pending signals |
| 1787 | * and act on them. |
| 1788 | * |
| 1789 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1790 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1791 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1792 | * sigset_t pending_set: set of sigs we received. |
| 1793 | * |
| 1794 | * "trap - SIGxxx": |
| 1795 | * if sig is in special_sig_mask, set handler back to: |
| 1796 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1797 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1798 | * else: set handler back to SIG_DFL |
| 1799 | * "trap 'cmd' SIGxxx": |
| 1800 | * set handler to record_pending_signo. |
| 1801 | * "trap '' SIGxxx": |
| 1802 | * set handler to SIG_IGN. |
| 1803 | * after [v]fork, if we plan to be a shell: |
| 1804 | * set signals with special interactive handling to SIG_DFL |
| 1805 | * (because child shell is not interactive), |
| 1806 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1807 | * after [v]fork, if we plan to exec: |
| 1808 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1809 | * |
| 1810 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1811 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1812 | * |
| 1813 | * Note (compat): |
| 1814 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1815 | * are set to the default actions". bash interprets it so that traps which |
| 1816 | * 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] | 1817 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1818 | enum { |
| 1819 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1820 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1821 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1822 | | (1 << SIGHUP) |
| 1823 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1824 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1825 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1826 | | (1 << SIGTTIN) |
| 1827 | | (1 << SIGTTOU) |
| 1828 | | (1 << SIGTSTP) |
| 1829 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1830 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1831 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1832 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1833 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1834 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1835 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1836 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1837 | if (sig == SIGCHLD) { |
| 1838 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1839 | //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] | 1840 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1841 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1842 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1843 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1844 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1845 | { |
| 1846 | struct sigaction old_sa; |
| 1847 | |
| 1848 | /* We could use signal() to install handlers... almost: |
| 1849 | * except that we need to mask ALL signals while handlers run. |
| 1850 | * I saw signal nesting in strace, race window isn't small. |
| 1851 | * SA_RESTART is also needed, but in Linux, signal() |
| 1852 | * sets SA_RESTART too. |
| 1853 | */ |
| 1854 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1855 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1856 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1857 | G.sa.sa_handler = handler; |
| 1858 | sigaction(sig, &G.sa, &old_sa); |
| 1859 | return old_sa.sa_handler; |
| 1860 | } |
| 1861 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1862 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1863 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1864 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1865 | static void restore_ttypgrp_and__exit(void) |
| 1866 | { |
| 1867 | /* xfunc has failed! die die die */ |
| 1868 | /* no EXIT traps, this is an escape hatch! */ |
| 1869 | G.exiting = 1; |
| 1870 | hush_exit(xfunc_error_retval); |
| 1871 | } |
| 1872 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1873 | #if ENABLE_HUSH_JOB |
| 1874 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1875 | /* Needed only on some libc: |
| 1876 | * It was observed that on exit(), fgetc'ed buffered data |
| 1877 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1878 | * With the net effect that even after fork(), not vfork(), |
| 1879 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1880 | * noexec_applet_here |
| 1881 | * echo END_OF_SCRIPT |
| 1882 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1883 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1884 | * 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] | 1885 | * and in `cmd` handling. |
| 1886 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1887 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1888 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1889 | static void fflush_and__exit(void) |
| 1890 | { |
| 1891 | fflush_all(); |
| 1892 | _exit(xfunc_error_retval); |
| 1893 | } |
| 1894 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1895 | /* 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] | 1896 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1897 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1898 | # 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] | 1899 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1900 | /* Restores tty foreground process group, and exits. |
| 1901 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1902 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1903 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1904 | * We also call it if xfunc is exiting. |
| 1905 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1906 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1907 | static void sigexit(int sig) |
| 1908 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1909 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1910 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1911 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1912 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1913 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1914 | */ |
| 1915 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1916 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1917 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1918 | |
| 1919 | /* Not a signal, just exit */ |
| 1920 | if (sig <= 0) |
| 1921 | _exit(- sig); |
| 1922 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1923 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1924 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1925 | #else |
| 1926 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1927 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1928 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1929 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1930 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1931 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1932 | static sighandler_t pick_sighandler(unsigned sig) |
| 1933 | { |
| 1934 | sighandler_t handler = SIG_DFL; |
| 1935 | if (sig < sizeof(unsigned)*8) { |
| 1936 | unsigned sigmask = (1 << sig); |
| 1937 | |
| 1938 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1939 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1940 | if (G_fatal_sig_mask & sigmask) |
| 1941 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1942 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1943 | #endif |
| 1944 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1945 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1946 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1947 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1948 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1949 | * in an endless loop when we try to do some |
| 1950 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1951 | */ |
| 1952 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1953 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1954 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1955 | } |
| 1956 | return handler; |
| 1957 | } |
| 1958 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1959 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1960 | static void hush_exit(int exitcode) |
| 1961 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1962 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1963 | save_history(G.line_input_state); |
| 1964 | #endif |
| 1965 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1966 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1967 | 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] | 1968 | char *argv[3]; |
| 1969 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1970 | 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] | 1971 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1972 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1973 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1974 | * "trap" will still show it, if executed |
| 1975 | * in the handler */ |
| 1976 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1977 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1978 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1979 | #if ENABLE_FEATURE_CLEAN_UP |
| 1980 | { |
| 1981 | struct variable *cur_var; |
| 1982 | if (G.cwd != bb_msg_unknown) |
| 1983 | free((char*)G.cwd); |
| 1984 | cur_var = G.top_var; |
| 1985 | while (cur_var) { |
| 1986 | struct variable *tmp = cur_var; |
| 1987 | if (!cur_var->max_len) |
| 1988 | free(cur_var->varstr); |
| 1989 | cur_var = cur_var->next; |
| 1990 | free(tmp); |
| 1991 | } |
| 1992 | } |
| 1993 | #endif |
| 1994 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1995 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1996 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1997 | sigexit(- (exitcode & 0xff)); |
| 1998 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1999 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2000 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2003 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2004 | //TODO: return a mask of ALL handled sigs? |
| 2005 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2006 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2007 | int last_sig = 0; |
| 2008 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2009 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2010 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2011 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2012 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2013 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2014 | sig = 0; |
| 2015 | do { |
| 2016 | sig++; |
| 2017 | if (sigismember(&G.pending_set, sig)) { |
| 2018 | sigdelset(&G.pending_set, sig); |
| 2019 | goto got_sig; |
| 2020 | } |
| 2021 | } while (sig < NSIG); |
| 2022 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2023 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2024 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2025 | 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] | 2026 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2027 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2028 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2029 | char *argv[3]; |
| 2030 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2031 | argv[1] = xstrdup(G_traps[sig]); |
| 2032 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2033 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2034 | save_rcode = G.last_exitcode; |
| 2035 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2036 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2037 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2038 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2039 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2040 | } /* else: "" trap, ignoring signal */ |
| 2041 | continue; |
| 2042 | } |
| 2043 | /* not a trap: special action */ |
| 2044 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2045 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2046 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2047 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2048 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2049 | break; |
| 2050 | #if ENABLE_HUSH_JOB |
| 2051 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2052 | //TODO: why are we doing this? ash and dash don't do this, |
| 2053 | //they have no handler for SIGHUP at all, |
| 2054 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2055 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2056 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2057 | /* bash is observed to signal whole process groups, |
| 2058 | * not individual processes */ |
| 2059 | for (job = G.job_list; job; job = job->next) { |
| 2060 | if (job->pgrp <= 0) |
| 2061 | continue; |
| 2062 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2063 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2064 | kill(- job->pgrp, SIGCONT); |
| 2065 | } |
| 2066 | sigexit(SIGHUP); |
| 2067 | } |
| 2068 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2069 | #if ENABLE_HUSH_FAST |
| 2070 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2071 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2072 | G.count_SIGCHLD++; |
| 2073 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2074 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2075 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2076 | * This simplifies wait builtin a bit. |
| 2077 | */ |
| 2078 | break; |
| 2079 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2080 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2081 | 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] | 2082 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2083 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2084 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2085 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2086 | * in interactive shell, because TERM is ignored. |
| 2087 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2088 | break; |
| 2089 | } |
| 2090 | } |
| 2091 | return last_sig; |
| 2092 | } |
| 2093 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2094 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2095 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2096 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2097 | if (force || G.cwd == NULL) { |
| 2098 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2099 | * we must not try to free(bb_msg_unknown) */ |
| 2100 | if (G.cwd == bb_msg_unknown) |
| 2101 | G.cwd = NULL; |
| 2102 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2103 | if (!G.cwd) |
| 2104 | G.cwd = bb_msg_unknown; |
| 2105 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2106 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2109 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2110 | /* |
| 2111 | * Shell and environment variable support |
| 2112 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2113 | 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] | 2114 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2115 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2116 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2117 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2118 | pp = &G.top_var; |
| 2119 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2120 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2121 | return pp; |
| 2122 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2123 | } |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2127 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2128 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2129 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2130 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2131 | |
| 2132 | if (G.expanded_assignments) { |
| 2133 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2134 | while (*cpp) { |
| 2135 | char *cp = *cpp; |
| 2136 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2137 | return cp + len + 1; |
| 2138 | cpp++; |
| 2139 | } |
| 2140 | } |
| 2141 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2142 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2143 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2144 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2145 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2146 | if (strcmp(name, "PPID") == 0) |
| 2147 | return utoa(G.root_ppid); |
| 2148 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2149 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2150 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2151 | return utoa(next_random(&G.random_gen)); |
| 2152 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2153 | return NULL; |
| 2154 | } |
| 2155 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2156 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2157 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2158 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2159 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2160 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2161 | cmdedit_update_prompt(); |
| 2162 | return; |
| 2163 | } |
| 2164 | |
| 2165 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2166 | && name_len == 6 |
| 2167 | ) { |
| 2168 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2169 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2170 | G.lineno_var = NULL; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2171 | return; |
| 2172 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2173 | #endif |
| 2174 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2175 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2176 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2177 | return; |
| 2178 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2179 | #endif |
| 2180 | } |
| 2181 | } |
| 2182 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2183 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2184 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2185 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2186 | #define SETFLAG_EXPORT (1 << 0) |
| 2187 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2188 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2189 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2190 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2191 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2192 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2193 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2194 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2195 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2196 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2197 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2198 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2199 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2200 | if (HUSH_DEBUG && !eq_sign) |
| 2201 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2202 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2203 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2204 | cur_pp = &G.top_var; |
| 2205 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2206 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2207 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2208 | continue; |
| 2209 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2210 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2211 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2212 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2213 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2214 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2215 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2216 | //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] | 2217 | return -1; |
| 2218 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2219 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2220 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2221 | *eq_sign = '\0'; |
| 2222 | unsetenv(str); |
| 2223 | *eq_sign = '='; |
| 2224 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2225 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2226 | /* bash 3.2.33(1) and exported vars: |
| 2227 | * # export z=z |
| 2228 | * # f() { local z=a; env | grep ^z; } |
| 2229 | * # f |
| 2230 | * z=a |
| 2231 | * # env | grep ^z |
| 2232 | * z=z |
| 2233 | */ |
| 2234 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2235 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2236 | /* New variable is local ("local VAR=VAL" or |
| 2237 | * "VAR=VAL cmd") |
| 2238 | * and existing one is global, or local |
| 2239 | * on a lower level that new one. |
| 2240 | * Remove it from global variable list: |
| 2241 | */ |
| 2242 | *cur_pp = cur->next; |
| 2243 | if (G.shadowed_vars_pp) { |
| 2244 | /* Save in "shadowed" list */ |
| 2245 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2246 | cur->flg_export ? "exported " : "", |
| 2247 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2248 | ); |
| 2249 | cur->next = *G.shadowed_vars_pp; |
| 2250 | *G.shadowed_vars_pp = cur; |
| 2251 | } else { |
| 2252 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2253 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2254 | cur->flg_export ? "exported " : "", |
| 2255 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2256 | ); |
| 2257 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2258 | free_me = cur->varstr; /* then free it later */ |
| 2259 | free(cur); |
| 2260 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2261 | break; |
| 2262 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2263 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2264 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2265 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2266 | free_and_exp: |
| 2267 | free(str); |
| 2268 | goto exp; |
| 2269 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2270 | |
| 2271 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2272 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2273 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2274 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2275 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2276 | strcpy(cur->varstr, str); |
| 2277 | goto free_and_exp; |
| 2278 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2279 | /* Can't reuse */ |
| 2280 | cur->max_len = 0; |
| 2281 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2282 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2283 | /* max_len == 0 signifies "malloced" var, which we can |
| 2284 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2285 | * if cur->flg_export is 1, it is in the environment. |
| 2286 | * We should either unsetenv+free, or wait until putenv, |
| 2287 | * then putenv(new)+free(old). |
| 2288 | */ |
| 2289 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2290 | goto set_str_and_exp; |
| 2291 | } |
| 2292 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2293 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2294 | 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] | 2295 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2296 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2297 | cur->next = *cur_pp; |
| 2298 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2299 | |
| 2300 | set_str_and_exp: |
| 2301 | cur->varstr = str; |
| 2302 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2303 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2304 | if (flags & SETFLAG_MAKE_RO) { |
| 2305 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2306 | } |
| 2307 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2308 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2309 | cur->flg_export = 1; |
| 2310 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2311 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2312 | cur->flg_export = 0; |
| 2313 | /* unsetenv was already done */ |
| 2314 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2315 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2316 | 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] | 2317 | i = putenv(cur->varstr); |
| 2318 | /* only now we can free old exported malloced string */ |
| 2319 | free(free_me); |
| 2320 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2321 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2322 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2323 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2324 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2325 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2326 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2327 | return 0; |
| 2328 | } |
| 2329 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2330 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2331 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2332 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2333 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2334 | } |
| 2335 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2336 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2337 | { |
| 2338 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2339 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2340 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2341 | cur_pp = &G.top_var; |
| 2342 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2343 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2344 | && cur->varstr[name_len] == '=' |
| 2345 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2346 | if (cur->flg_read_only) { |
| 2347 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2348 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2349 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2350 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2351 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2352 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2353 | bb_unsetenv(cur->varstr); |
| 2354 | if (!cur->max_len) |
| 2355 | free(cur->varstr); |
| 2356 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2357 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2358 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2359 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2360 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2361 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2362 | |
| 2363 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2364 | handle_changed_special_names(name, name_len); |
| 2365 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2366 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2369 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2370 | static int unset_local_var(const char *name) |
| 2371 | { |
| 2372 | return unset_local_var_len(name, strlen(name)); |
| 2373 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2374 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2375 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2376 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2377 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2378 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2379 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2380 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2381 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2382 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2383 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2384 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2385 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2386 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2387 | */ |
| 2388 | static void add_vars(struct variable *var) |
| 2389 | { |
| 2390 | struct variable *next; |
| 2391 | |
| 2392 | while (var) { |
| 2393 | next = var->next; |
| 2394 | var->next = G.top_var; |
| 2395 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2396 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2397 | 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] | 2398 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2399 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2400 | 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] | 2401 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2402 | var = next; |
| 2403 | } |
| 2404 | } |
| 2405 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2406 | /* We put strings[i] into variable table and possibly putenv them. |
| 2407 | * If variable is read only, we can free the strings[i] |
| 2408 | * which attempts to overwrite it. |
| 2409 | * The strings[] vector itself is freed. |
| 2410 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2411 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2412 | { |
| 2413 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2414 | |
| 2415 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2416 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2417 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2418 | s = strings; |
| 2419 | while (*s) { |
| 2420 | struct variable *var_p; |
| 2421 | struct variable **var_pp; |
| 2422 | char *eq; |
| 2423 | |
| 2424 | eq = strchr(*s, '='); |
| 2425 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2426 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2427 | if (var_pp) { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2428 | var_p = *var_pp; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2429 | if (var_p->flg_read_only) { |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2430 | char **p; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2431 | bb_error_msg("%s: readonly variable", *s); |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2432 | /* |
| 2433 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2434 | * If VAR is readonly, leaving it in the list |
| 2435 | * after asssignment error (msg above) |
| 2436 | * causes doubled error message later, on unset. |
| 2437 | */ |
| 2438 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2439 | free(*s); |
| 2440 | p = s; |
| 2441 | do { *p = p[1]; p++; } while (*p); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2442 | goto next; |
| 2443 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2444 | /* below, set_local_var() with nest level will |
| 2445 | * "shadow" (remove) this variable from |
| 2446 | * global linked list. |
| 2447 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2448 | } |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2449 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2450 | set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT); |
| 2451 | } else if (HUSH_DEBUG) { |
| 2452 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2453 | } |
| 2454 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2455 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2456 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2457 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | |
| 2461 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2462 | * Unicode helper |
| 2463 | */ |
| 2464 | static void reinit_unicode_for_hush(void) |
| 2465 | { |
| 2466 | /* Unicode support should be activated even if LANG is set |
| 2467 | * _during_ shell execution, not only if it was set when |
| 2468 | * shell was started. Therefore, re-check LANG every time: |
| 2469 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2470 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2471 | || ENABLE_UNICODE_USING_LOCALE |
| 2472 | ) { |
| 2473 | const char *s = get_local_var_value("LC_ALL"); |
| 2474 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2475 | if (!s) s = get_local_var_value("LANG"); |
| 2476 | reinit_unicode(s); |
| 2477 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2478 | } |
| 2479 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2480 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2481 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2482 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2483 | |
| 2484 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2485 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2486 | * echo $P\ |
| 2487 | * \ |
| 2488 | * AT\ |
| 2489 | * H\ |
| 2490 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2491 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2492 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2493 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2494 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2495 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2496 | G.PS1 = get_local_var_value("PS1"); |
| 2497 | if (G.PS1 == NULL) |
| 2498 | G.PS1 = ""; |
| 2499 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2500 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2501 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2502 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2503 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2504 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2505 | { |
| 2506 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2507 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2508 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2509 | |
| 2510 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2511 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2512 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2513 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2514 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2515 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2516 | /* bash uses $PWD value, even if it is set by user. |
| 2517 | * It uses current dir only if PWD is unset. |
| 2518 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2519 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2520 | } |
| 2521 | prompt_str = G.PS1; |
| 2522 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2523 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2524 | return prompt_str; |
| 2525 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2526 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2527 | { |
| 2528 | int r; |
| 2529 | const char *prompt_str; |
| 2530 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2531 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2532 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2533 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2534 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2535 | if (G.flag_SIGINT) { |
| 2536 | /* There was ^C'ed, make it look prettier: */ |
| 2537 | bb_putchar('\n'); |
| 2538 | G.flag_SIGINT = 0; |
| 2539 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2540 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2541 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2542 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2543 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2544 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2545 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2546 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2547 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2548 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2549 | if (r != 0 && !G.flag_SIGINT) |
| 2550 | break; |
| 2551 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2552 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2553 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2554 | G.last_exitcode = 128 + SIGINT; |
| 2555 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2556 | if (r < 0) { |
| 2557 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2558 | i->p = NULL; |
| 2559 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2560 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2561 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2562 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2563 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2564 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2565 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2566 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2567 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2568 | /* Why check_and_run_traps here? Try this interactively: |
| 2569 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2570 | * $ <[enter], repeatedly...> |
| 2571 | * Without check_and_run_traps, handler never runs. |
| 2572 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2573 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2574 | fputs(prompt_str, stdout); |
| 2575 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2576 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2577 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2578 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2579 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2580 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2581 | * it generates SIGINT as usual. |
| 2582 | */ |
| 2583 | check_and_run_traps(); |
| 2584 | if (G.flag_SIGINT) |
| 2585 | G.last_exitcode = 128 + SIGINT; |
| 2586 | if (r != '\0') |
| 2587 | break; |
| 2588 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2589 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2590 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2591 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2592 | /* This is the magic location that prints prompts |
| 2593 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2594 | static int fgetc_interactive(struct in_str *i) |
| 2595 | { |
| 2596 | int ch; |
| 2597 | /* If it's interactive stdin, get new line. */ |
| 2598 | if (G_interactive_fd && i->file == stdin) { |
| 2599 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2600 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2601 | G.promptmode = 1; /* PS2 */ |
| 2602 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2603 | } else { |
| 2604 | /* Not stdin: script file, sourced file, etc */ |
| 2605 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2606 | } |
| 2607 | return ch; |
| 2608 | } |
| 2609 | #else |
| 2610 | static inline int fgetc_interactive(struct in_str *i) |
| 2611 | { |
| 2612 | int ch; |
| 2613 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2614 | return ch; |
| 2615 | } |
| 2616 | #endif /* INTERACTIVE */ |
| 2617 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2618 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2619 | { |
| 2620 | int ch; |
| 2621 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2622 | if (!i->file) { |
| 2623 | /* string-based in_str */ |
| 2624 | ch = (unsigned char)*i->p; |
| 2625 | if (ch != '\0') { |
| 2626 | i->p++; |
| 2627 | i->last_char = ch; |
| 2628 | return ch; |
| 2629 | } |
| 2630 | return EOF; |
| 2631 | } |
| 2632 | |
| 2633 | /* FILE-based in_str */ |
| 2634 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2635 | #if ENABLE_FEATURE_EDITING |
| 2636 | /* This can be stdin, check line editing char[] buffer */ |
| 2637 | if (i->p && *i->p != '\0') { |
| 2638 | ch = (unsigned char)*i->p++; |
| 2639 | goto out; |
| 2640 | } |
| 2641 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2642 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2643 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2644 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2645 | int ch2 = i->peek_buf[1]; |
| 2646 | i->peek_buf[0] = ch2; |
| 2647 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2648 | goto out; |
| 2649 | i->peek_buf[1] = 0; |
| 2650 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2651 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2652 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2653 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2654 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2655 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2656 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2657 | #if ENABLE_HUSH_LINENO_VAR |
| 2658 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2659 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2660 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2661 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2662 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2663 | return ch; |
| 2664 | } |
| 2665 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2666 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2667 | { |
| 2668 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2669 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2670 | if (!i->file) { |
| 2671 | /* string-based in_str */ |
| 2672 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2673 | return (unsigned char)*i->p; |
| 2674 | } |
| 2675 | |
| 2676 | /* FILE-based in_str */ |
| 2677 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2678 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2679 | /* This can be stdin, check line editing char[] buffer */ |
| 2680 | if (i->p && *i->p != '\0') |
| 2681 | return (unsigned char)*i->p; |
| 2682 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2683 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2684 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2685 | if (ch != 0) |
| 2686 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2687 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2688 | /* Need to get a new char */ |
| 2689 | ch = fgetc_interactive(i); |
| 2690 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2691 | |
| 2692 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2693 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2694 | if (i->p) { |
| 2695 | i->p -= 1; |
| 2696 | return ch; |
| 2697 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2698 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2699 | i->peek_buf[0] = ch; |
| 2700 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2701 | return ch; |
| 2702 | } |
| 2703 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2704 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2705 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2706 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2707 | */ |
| 2708 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2709 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2710 | int ch; |
| 2711 | |
| 2712 | /* There are two cases when i->p[] buffer exists. |
| 2713 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2714 | * (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] | 2715 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2716 | * the peek2 result is in i->p[1]. |
| 2717 | */ |
| 2718 | if (i->p) |
| 2719 | return (unsigned char)i->p[1]; |
| 2720 | |
| 2721 | /* Now we know it is a file-based in_str. */ |
| 2722 | |
| 2723 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2724 | /* Is there 2nd char? */ |
| 2725 | ch = i->peek_buf[1]; |
| 2726 | if (ch == 0) { |
| 2727 | /* We did not read it yet, get it now */ |
| 2728 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2729 | i->peek_buf[1] = ch; |
| 2730 | } |
| 2731 | |
| 2732 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2733 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2736 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2737 | { |
| 2738 | for (;;) { |
| 2739 | int ch, ch2; |
| 2740 | |
| 2741 | ch = i_getch(input); |
| 2742 | if (ch != '\\') |
| 2743 | return ch; |
| 2744 | ch2 = i_peek(input); |
| 2745 | if (ch2 != '\n') |
| 2746 | return ch; |
| 2747 | /* backslash+newline, skip it */ |
| 2748 | i_getch(input); |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2753 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2754 | */ |
| 2755 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2756 | { |
| 2757 | for (;;) { |
| 2758 | int ch, ch2; |
| 2759 | |
| 2760 | ch = i_peek(input); |
| 2761 | if (ch != '\\') |
| 2762 | return ch; |
| 2763 | ch2 = i_peek2(input); |
| 2764 | if (ch2 != '\n') |
| 2765 | return ch; |
| 2766 | /* backslash+newline, skip it */ |
| 2767 | i_getch(input); |
| 2768 | i_getch(input); |
| 2769 | } |
| 2770 | } |
| 2771 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2772 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2773 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2774 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2775 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2776 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2780 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2781 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2782 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2783 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
| 2786 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2787 | /* |
| 2788 | * o_string support |
| 2789 | */ |
| 2790 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2791 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2792 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2793 | { |
| 2794 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2795 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2796 | if (o->data) |
| 2797 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2800 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2801 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2802 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2803 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2806 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2807 | { |
| 2808 | free(o->data); |
| 2809 | } |
| 2810 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2811 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2812 | { |
| 2813 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2814 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2815 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2816 | } |
| 2817 | } |
| 2818 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2819 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2820 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2821 | 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] | 2822 | if (o->length < o->maxlen) { |
| 2823 | /* likely. avoid o_grow_by() call */ |
| 2824 | add: |
| 2825 | o->data[o->length] = ch; |
| 2826 | o->length++; |
| 2827 | o->data[o->length] = '\0'; |
| 2828 | return; |
| 2829 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2830 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2831 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2834 | #if 0 |
| 2835 | /* Valid only if we know o_string is not empty */ |
| 2836 | static void o_delchr(o_string *o) |
| 2837 | { |
| 2838 | o->length--; |
| 2839 | o->data[o->length] = '\0'; |
| 2840 | } |
| 2841 | #endif |
| 2842 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2843 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2844 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2845 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2846 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2847 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2850 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2851 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2852 | o_addblock(o, str, strlen(str)); |
| 2853 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2854 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2855 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2856 | static void nommu_addchr(o_string *o, int ch) |
| 2857 | { |
| 2858 | if (o) |
| 2859 | o_addchr(o, ch); |
| 2860 | } |
| 2861 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2862 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2863 | #endif |
| 2864 | |
| 2865 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2866 | { |
| 2867 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2870 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2871 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2872 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2873 | * Apparently, on unquoted $v bash still does globbing |
| 2874 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2875 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2876 | * quoting mechanisms on $v expansion side: one protects |
| 2877 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2878 | * We have only second one. |
| 2879 | */ |
| 2880 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2881 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2882 | # define MAYBE_BRACES "{}" |
| 2883 | #else |
| 2884 | # define MAYBE_BRACES "" |
| 2885 | #endif |
| 2886 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2887 | /* My analysis of quoting semantics tells me that state information |
| 2888 | * is associated with a destination, not a source. |
| 2889 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2890 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2891 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2892 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2893 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2894 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2895 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2896 | o_grow_by(o, sz); |
| 2897 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2898 | o->data[o->length] = '\\'; |
| 2899 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2900 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2901 | o->data[o->length] = ch; |
| 2902 | o->length++; |
| 2903 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2906 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2907 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2908 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2909 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2910 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2911 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2912 | sz++; |
| 2913 | o->data[o->length] = '\\'; |
| 2914 | o->length++; |
| 2915 | } |
| 2916 | o_grow_by(o, sz); |
| 2917 | o->data[o->length] = ch; |
| 2918 | o->length++; |
| 2919 | o->data[o->length] = '\0'; |
| 2920 | } |
| 2921 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2922 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2923 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2924 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2925 | char ch; |
| 2926 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2927 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2928 | if (ordinary_cnt > len) /* paranoia */ |
| 2929 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2930 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2931 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2932 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2933 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2934 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2935 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2936 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2937 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2938 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2939 | sz++; |
| 2940 | o->data[o->length] = '\\'; |
| 2941 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2942 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2943 | o_grow_by(o, sz); |
| 2944 | o->data[o->length] = ch; |
| 2945 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2946 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2947 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2950 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2951 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2952 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2953 | o_addblock(o, str, len); |
| 2954 | return; |
| 2955 | } |
| 2956 | o_addqblock(o, str, len); |
| 2957 | } |
| 2958 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2959 | static void o_addQstr(o_string *o, const char *str) |
| 2960 | { |
| 2961 | o_addQblock(o, str, strlen(str)); |
| 2962 | } |
| 2963 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2964 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2965 | * 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] | 2966 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2967 | * list[i] contains an INDEX (int!) into this string data. |
| 2968 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2969 | * but list[i]'s need not be modified. |
| 2970 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2971 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2972 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2973 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2974 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2975 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2976 | { |
| 2977 | char **list = (char**)o->data; |
| 2978 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2979 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2980 | |
| 2981 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2982 | 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] | 2983 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2984 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2985 | o->has_quoted_part, |
| 2986 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2987 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2988 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2989 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2990 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2991 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2992 | i++; |
| 2993 | } |
| 2994 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2995 | 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] | 2996 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2997 | 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] | 2998 | } |
| 2999 | } |
| 3000 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3001 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3002 | #endif |
| 3003 | |
| 3004 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3005 | * in list[n] so that it points past last stored byte so far. |
| 3006 | * It returns n+1. */ |
| 3007 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3008 | { |
| 3009 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3010 | int string_start; |
| 3011 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3012 | |
| 3013 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3014 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3015 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3016 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3017 | 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] | 3018 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3019 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3020 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3021 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3022 | memmove(list + n + 0x10, list + n, string_len); |
| 3023 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3024 | } else { |
| 3025 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3026 | n, string_len, string_start); |
| 3027 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3028 | } else { |
| 3029 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3030 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3031 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3032 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3033 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3034 | o->has_empty_slot = 0; |
| 3035 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3036 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3037 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3038 | return n + 1; |
| 3039 | } |
| 3040 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3041 | /* "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] | 3042 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3043 | { |
| 3044 | char **list = (char**)o->data; |
| 3045 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3046 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3047 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3050 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3051 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3052 | * first, it processes even {a} (no commas), second, |
| 3053 | * 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] | 3054 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3055 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3056 | |
| 3057 | /* Helper */ |
| 3058 | static int glob_needed(const char *s) |
| 3059 | { |
| 3060 | while (*s) { |
| 3061 | if (*s == '\\') { |
| 3062 | if (!s[1]) |
| 3063 | return 0; |
| 3064 | s += 2; |
| 3065 | continue; |
| 3066 | } |
| 3067 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3068 | return 1; |
| 3069 | s++; |
| 3070 | } |
| 3071 | return 0; |
| 3072 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3073 | /* Return pointer to next closing brace or to comma */ |
| 3074 | static const char *next_brace_sub(const char *cp) |
| 3075 | { |
| 3076 | unsigned depth = 0; |
| 3077 | cp++; |
| 3078 | while (*cp != '\0') { |
| 3079 | if (*cp == '\\') { |
| 3080 | if (*++cp == '\0') |
| 3081 | break; |
| 3082 | cp++; |
| 3083 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3084 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3085 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3086 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3087 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3088 | depth++; |
| 3089 | } |
| 3090 | |
| 3091 | return *cp != '\0' ? cp : NULL; |
| 3092 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3093 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3094 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3095 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3096 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3097 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3098 | const char *next; |
| 3099 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3100 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3101 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3102 | |
| 3103 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3104 | |
| 3105 | begin = pattern; |
| 3106 | while (1) { |
| 3107 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3108 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3109 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3110 | /* Find the first sub-pattern and at the same time |
| 3111 | * find the rest after the closing brace */ |
| 3112 | next = next_brace_sub(begin); |
| 3113 | if (next == NULL) { |
| 3114 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3115 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3116 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3117 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3118 | /* "{abc}" with no commas - illegal |
| 3119 | * brace expr, disregard and skip it */ |
| 3120 | begin = next + 1; |
| 3121 | continue; |
| 3122 | } |
| 3123 | break; |
| 3124 | } |
| 3125 | if (*begin == '\\' && begin[1] != '\0') |
| 3126 | begin++; |
| 3127 | begin++; |
| 3128 | } |
| 3129 | debug_printf_glob("begin:%s\n", begin); |
| 3130 | debug_printf_glob("next:%s\n", next); |
| 3131 | |
| 3132 | /* Now find the end of the whole brace expression */ |
| 3133 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3134 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3135 | rest = next_brace_sub(rest); |
| 3136 | if (rest == NULL) { |
| 3137 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3138 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3139 | } |
| 3140 | debug_printf_glob("rest:%s\n", rest); |
| 3141 | } |
| 3142 | rest_len = strlen(++rest) + 1; |
| 3143 | |
| 3144 | /* We are sure the brace expression is well-formed */ |
| 3145 | |
| 3146 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3147 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3148 | |
| 3149 | /* We have a brace expression. BEGIN points to the opening {, |
| 3150 | * NEXT points past the terminator of the first element, and REST |
| 3151 | * points past the final }. We will accumulate result names from |
| 3152 | * recursive runs for each brace alternative in the buffer using |
| 3153 | * GLOB_APPEND. */ |
| 3154 | |
| 3155 | p = begin + 1; |
| 3156 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3157 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3158 | memcpy( |
| 3159 | mempcpy( |
| 3160 | mempcpy(new_pattern_buf, |
| 3161 | /* We know the prefix for all sub-patterns */ |
| 3162 | pattern, begin - pattern), |
| 3163 | p, next - p), |
| 3164 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3165 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3166 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3167 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3168 | */ |
| 3169 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3170 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3171 | /* We saw the last entry */ |
| 3172 | break; |
| 3173 | } |
| 3174 | p = next + 1; |
| 3175 | next = next_brace_sub(next); |
| 3176 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3177 | free(new_pattern_buf); |
| 3178 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3179 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3180 | simple_glob: |
| 3181 | { |
| 3182 | int gr; |
| 3183 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3184 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3185 | memset(&globdata, 0, sizeof(globdata)); |
| 3186 | gr = glob(pattern, 0, NULL, &globdata); |
| 3187 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3188 | if (gr != 0) { |
| 3189 | if (gr == GLOB_NOMATCH) { |
| 3190 | globfree(&globdata); |
| 3191 | /* NB: garbles parameter */ |
| 3192 | unbackslash(pattern); |
| 3193 | o_addstr_with_NUL(o, pattern); |
| 3194 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3195 | return o_save_ptr_helper(o, n); |
| 3196 | } |
| 3197 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3198 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3199 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3200 | * but we didn't specify it. Paranoia again. */ |
| 3201 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3202 | } |
| 3203 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3204 | char **argv = globdata.gl_pathv; |
| 3205 | while (1) { |
| 3206 | o_addstr_with_NUL(o, *argv); |
| 3207 | n = o_save_ptr_helper(o, n); |
| 3208 | argv++; |
| 3209 | if (!*argv) |
| 3210 | break; |
| 3211 | } |
| 3212 | } |
| 3213 | globfree(&globdata); |
| 3214 | } |
| 3215 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3216 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3217 | /* Performs globbing on last list[], |
| 3218 | * saving each result as a new list[]. |
| 3219 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3220 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3221 | { |
| 3222 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3223 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3224 | 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] | 3225 | if (!o->data) |
| 3226 | return o_save_ptr_helper(o, n); |
| 3227 | pattern = o->data + o_get_last_ptr(o, n); |
| 3228 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3229 | if (!glob_needed(pattern)) { |
| 3230 | /* unbackslash last string in o in place, fix length */ |
| 3231 | o->length = unbackslash(pattern) - o->data; |
| 3232 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3233 | return o_save_ptr_helper(o, n); |
| 3234 | } |
| 3235 | |
| 3236 | copy = xstrdup(pattern); |
| 3237 | /* "forget" pattern in o */ |
| 3238 | o->length = pattern - o->data; |
| 3239 | n = glob_brace(copy, o, n); |
| 3240 | free(copy); |
| 3241 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3242 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3243 | return n; |
| 3244 | } |
| 3245 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3246 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3247 | |
| 3248 | /* Helper */ |
| 3249 | static int glob_needed(const char *s) |
| 3250 | { |
| 3251 | while (*s) { |
| 3252 | if (*s == '\\') { |
| 3253 | if (!s[1]) |
| 3254 | return 0; |
| 3255 | s += 2; |
| 3256 | continue; |
| 3257 | } |
| 3258 | if (*s == '*' || *s == '[' || *s == '?') |
| 3259 | return 1; |
| 3260 | s++; |
| 3261 | } |
| 3262 | return 0; |
| 3263 | } |
| 3264 | /* Performs globbing on last list[], |
| 3265 | * saving each result as a new list[]. |
| 3266 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3267 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3268 | { |
| 3269 | glob_t globdata; |
| 3270 | int gr; |
| 3271 | char *pattern; |
| 3272 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3273 | 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] | 3274 | if (!o->data) |
| 3275 | return o_save_ptr_helper(o, n); |
| 3276 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3277 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3278 | if (!glob_needed(pattern)) { |
| 3279 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3280 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3281 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3282 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3283 | return o_save_ptr_helper(o, n); |
| 3284 | } |
| 3285 | |
| 3286 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3287 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3288 | * If we glob "*.\*" and don't find anything, we need |
| 3289 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3290 | * will return "*.\*"! |
| 3291 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3292 | gr = glob(pattern, 0, NULL, &globdata); |
| 3293 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3294 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3295 | if (gr == GLOB_NOMATCH) { |
| 3296 | globfree(&globdata); |
| 3297 | goto literal; |
| 3298 | } |
| 3299 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3300 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3301 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3302 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3303 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3304 | } |
| 3305 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3306 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3307 | /* "forget" pattern in o */ |
| 3308 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3309 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3310 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3311 | n = o_save_ptr_helper(o, n); |
| 3312 | argv++; |
| 3313 | if (!*argv) |
| 3314 | break; |
| 3315 | } |
| 3316 | } |
| 3317 | globfree(&globdata); |
| 3318 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3319 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3320 | return n; |
| 3321 | } |
| 3322 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3323 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3324 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3325 | /* 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] | 3326 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3327 | static int o_save_ptr(o_string *o, int n) |
| 3328 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3329 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3330 | /* If o->has_empty_slot, list[n] was already globbed |
| 3331 | * (if it was requested back then when it was filled) |
| 3332 | * so don't do that again! */ |
| 3333 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3334 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3335 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3336 | return o_save_ptr_helper(o, n); |
| 3337 | } |
| 3338 | |
| 3339 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3340 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3341 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3342 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3343 | int string_start; |
| 3344 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3345 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3346 | if (DEBUG_EXPAND) |
| 3347 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3348 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3349 | list = (char**)o->data; |
| 3350 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3351 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3352 | while (n) { |
| 3353 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3354 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3355 | } |
| 3356 | return list; |
| 3357 | } |
| 3358 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3359 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3360 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3361 | /* Returns pi->next - next pipe in the list */ |
| 3362 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3363 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3364 | struct pipe *next; |
| 3365 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3366 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3367 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3368 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3369 | struct command *command; |
| 3370 | struct redir_struct *r, *rnext; |
| 3371 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3372 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3373 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3374 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3375 | if (DEBUG_CLEAN) { |
| 3376 | int a; |
| 3377 | char **p; |
| 3378 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3379 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3380 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3381 | } |
| 3382 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3383 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3384 | } |
| 3385 | /* not "else if": on syntax error, we may have both! */ |
| 3386 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3387 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3388 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3389 | free_pipe_list(command->group); |
| 3390 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3391 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3392 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3393 | /* else is crucial here. |
| 3394 | * If group != NULL, child_func is meaningless */ |
| 3395 | #if ENABLE_HUSH_FUNCTIONS |
| 3396 | else if (command->child_func) { |
| 3397 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3398 | command->child_func->parent_cmd = NULL; |
| 3399 | } |
| 3400 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3401 | #if !BB_MMU |
| 3402 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3403 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3404 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3405 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3406 | debug_printf_clean(" redirect %d%s", |
| 3407 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3408 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3409 | if (r->rd_filename) { |
| 3410 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3411 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3412 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3413 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3414 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3415 | rnext = r->next; |
| 3416 | free(r); |
| 3417 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3418 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3419 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3420 | 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] | 3421 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3422 | #if ENABLE_HUSH_JOB |
| 3423 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3424 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3425 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3426 | |
| 3427 | next = pi->next; |
| 3428 | free(pi); |
| 3429 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3430 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3431 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3432 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3433 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3434 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3435 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3436 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3437 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3438 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3439 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3440 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3444 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3445 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3446 | #ifndef debug_print_tree |
| 3447 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3448 | { |
| 3449 | static const char *const PIPE[] = { |
| 3450 | [PIPE_SEQ] = "SEQ", |
| 3451 | [PIPE_AND] = "AND", |
| 3452 | [PIPE_OR ] = "OR" , |
| 3453 | [PIPE_BG ] = "BG" , |
| 3454 | }; |
| 3455 | static const char *RES[] = { |
| 3456 | [RES_NONE ] = "NONE" , |
| 3457 | # if ENABLE_HUSH_IF |
| 3458 | [RES_IF ] = "IF" , |
| 3459 | [RES_THEN ] = "THEN" , |
| 3460 | [RES_ELIF ] = "ELIF" , |
| 3461 | [RES_ELSE ] = "ELSE" , |
| 3462 | [RES_FI ] = "FI" , |
| 3463 | # endif |
| 3464 | # if ENABLE_HUSH_LOOPS |
| 3465 | [RES_FOR ] = "FOR" , |
| 3466 | [RES_WHILE] = "WHILE", |
| 3467 | [RES_UNTIL] = "UNTIL", |
| 3468 | [RES_DO ] = "DO" , |
| 3469 | [RES_DONE ] = "DONE" , |
| 3470 | # endif |
| 3471 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3472 | [RES_IN ] = "IN" , |
| 3473 | # endif |
| 3474 | # if ENABLE_HUSH_CASE |
| 3475 | [RES_CASE ] = "CASE" , |
| 3476 | [RES_CASE_IN ] = "CASE_IN" , |
| 3477 | [RES_MATCH] = "MATCH", |
| 3478 | [RES_CASE_BODY] = "CASE_BODY", |
| 3479 | [RES_ESAC ] = "ESAC" , |
| 3480 | # endif |
| 3481 | [RES_XXXX ] = "XXXX" , |
| 3482 | [RES_SNTX ] = "SNTX" , |
| 3483 | }; |
| 3484 | static const char *const CMDTYPE[] = { |
| 3485 | "{}", |
| 3486 | "()", |
| 3487 | "[noglob]", |
| 3488 | # if ENABLE_HUSH_FUNCTIONS |
| 3489 | "func()", |
| 3490 | # endif |
| 3491 | }; |
| 3492 | |
| 3493 | int pin, prn; |
| 3494 | |
| 3495 | pin = 0; |
| 3496 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3497 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3498 | lvl*2, "", |
| 3499 | pin, |
| 3500 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3501 | RES[pi->res_word], |
| 3502 | pi->followup, PIPE[pi->followup] |
| 3503 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3504 | prn = 0; |
| 3505 | while (prn < pi->num_cmds) { |
| 3506 | struct command *command = &pi->cmds[prn]; |
| 3507 | char **argv = command->argv; |
| 3508 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3509 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3510 | lvl*2, "", prn, |
| 3511 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3512 | #if ENABLE_HUSH_LINENO_VAR |
| 3513 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3514 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3515 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3516 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3517 | CMDTYPE[command->cmd_type], |
| 3518 | argv |
| 3519 | # if !BB_MMU |
| 3520 | , " group_as_string:", command->group_as_string |
| 3521 | # else |
| 3522 | , "", "" |
| 3523 | # endif |
| 3524 | ); |
| 3525 | debug_print_tree(command->group, lvl+1); |
| 3526 | prn++; |
| 3527 | continue; |
| 3528 | } |
| 3529 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3530 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3531 | argv++; |
| 3532 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3533 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3534 | prn++; |
| 3535 | } |
| 3536 | pi = pi->next; |
| 3537 | pin++; |
| 3538 | } |
| 3539 | } |
| 3540 | #endif /* debug_print_tree */ |
| 3541 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3542 | static struct pipe *new_pipe(void) |
| 3543 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3544 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3545 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3546 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3547 | return pi; |
| 3548 | } |
| 3549 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3550 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3551 | * if ctx->command is NULL. |
| 3552 | * No errors possible here. |
| 3553 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3554 | static int done_command(struct parse_context *ctx) |
| 3555 | { |
| 3556 | /* The command is really already in the pipe structure, so |
| 3557 | * advance the pipe counter and make a new, null command. */ |
| 3558 | struct pipe *pi = ctx->pipe; |
| 3559 | struct command *command = ctx->command; |
| 3560 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3561 | #if 0 /* Instead we emit error message at run time */ |
| 3562 | if (ctx->pending_redirect) { |
| 3563 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3564 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3565 | ctx->pending_redirect = NULL; |
| 3566 | } |
| 3567 | #endif |
| 3568 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3569 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3570 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3571 | 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] | 3572 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3573 | } |
| 3574 | pi->num_cmds++; |
| 3575 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3576 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3577 | } else { |
| 3578 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3579 | } |
| 3580 | |
| 3581 | /* Only real trickiness here is that the uncommitted |
| 3582 | * command structure is not counted in pi->num_cmds. */ |
| 3583 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3584 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3585 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3586 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3587 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3588 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3589 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3590 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3591 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3592 | } |
| 3593 | |
| 3594 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3595 | { |
| 3596 | int not_null; |
| 3597 | |
| 3598 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3599 | /* Close previous command */ |
| 3600 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3601 | #if HAS_KEYWORDS |
| 3602 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3603 | ctx->ctx_inverted = 0; |
| 3604 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3605 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3606 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3607 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3608 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3609 | * in a backgrounded subshell. |
| 3610 | */ |
| 3611 | struct pipe *pi; |
| 3612 | struct command *command; |
| 3613 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3614 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3615 | pi = ctx->list_head; |
| 3616 | while (pi != ctx->pipe) { |
| 3617 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3618 | goto no_conv; |
| 3619 | pi = pi->next; |
| 3620 | } |
| 3621 | |
| 3622 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3623 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3624 | pi = xzalloc(sizeof(*pi)); |
| 3625 | pi->followup = PIPE_BG; |
| 3626 | pi->num_cmds = 1; |
| 3627 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3628 | command = &pi->cmds[0]; |
| 3629 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3630 | command->cmd_type = CMD_NORMAL; |
| 3631 | command->group = ctx->list_head; |
| 3632 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3633 | command->group_as_string = xstrndup( |
| 3634 | ctx->as_string.data, |
| 3635 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3636 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3637 | #endif |
| 3638 | /* Replace all pipes in ctx with one newly created */ |
| 3639 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3640 | } else { |
| 3641 | no_conv: |
| 3642 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3643 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3644 | |
| 3645 | /* Without this check, even just <enter> on command line generates |
| 3646 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3647 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3648 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3649 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3650 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3651 | #endif |
| 3652 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3653 | || ctx->ctx_res_w == RES_DONE |
| 3654 | || ctx->ctx_res_w == RES_FOR |
| 3655 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3656 | #endif |
| 3657 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3658 | || ctx->ctx_res_w == RES_ESAC |
| 3659 | #endif |
| 3660 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3661 | struct pipe *new_p; |
| 3662 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3663 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3664 | not_null, ctx->ctx_res_w); |
| 3665 | new_p = new_pipe(); |
| 3666 | ctx->pipe->next = new_p; |
| 3667 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3668 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3669 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3670 | * This is used to control execution. |
| 3671 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3672 | * cases where variable or value happens to match a keyword): |
| 3673 | */ |
| 3674 | #if ENABLE_HUSH_LOOPS |
| 3675 | if (ctx->ctx_res_w == RES_FOR |
| 3676 | || ctx->ctx_res_w == RES_IN) |
| 3677 | ctx->ctx_res_w = RES_NONE; |
| 3678 | #endif |
| 3679 | #if ENABLE_HUSH_CASE |
| 3680 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3681 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3682 | if (ctx->ctx_res_w == RES_CASE) |
| 3683 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3684 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3685 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3686 | /* Create the memory for command, roughly: |
| 3687 | * ctx->pipe->cmds = new struct command; |
| 3688 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3689 | */ |
| 3690 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3691 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3692 | } |
| 3693 | debug_printf_parse("done_pipe return\n"); |
| 3694 | } |
| 3695 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3696 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3697 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3698 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3699 | if (MAYBE_ASSIGNMENT != 0) |
| 3700 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3701 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3702 | /* Create the memory for command, roughly: |
| 3703 | * ctx->pipe->cmds = new struct command; |
| 3704 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3705 | */ |
| 3706 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3709 | /* If a reserved word is found and processed, parse context is modified |
| 3710 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3711 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3712 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3713 | struct reserved_combo { |
| 3714 | char literal[6]; |
| 3715 | unsigned char res; |
| 3716 | unsigned char assignment_flag; |
| 3717 | int flag; |
| 3718 | }; |
| 3719 | enum { |
| 3720 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3721 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3722 | FLAG_IF = (1 << RES_IF ), |
| 3723 | FLAG_THEN = (1 << RES_THEN ), |
| 3724 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3725 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3726 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3727 | # endif |
| 3728 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3729 | FLAG_FOR = (1 << RES_FOR ), |
| 3730 | FLAG_WHILE = (1 << RES_WHILE), |
| 3731 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3732 | FLAG_DO = (1 << RES_DO ), |
| 3733 | FLAG_DONE = (1 << RES_DONE ), |
| 3734 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3735 | # endif |
| 3736 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3737 | FLAG_MATCH = (1 << RES_MATCH), |
| 3738 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3739 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3740 | FLAG_START = (1 << RES_XXXX ), |
| 3741 | }; |
| 3742 | |
| 3743 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3744 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3745 | /* Mostly a list of accepted follow-up reserved words. |
| 3746 | * FLAG_END means we are done with the sequence, and are ready |
| 3747 | * to turn the compound list into a command. |
| 3748 | * FLAG_START means the word must start a new compound list. |
| 3749 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3750 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3751 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3752 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3753 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3754 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3755 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3756 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3757 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3758 | # endif |
| 3759 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3760 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3761 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3762 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3763 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3764 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3765 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3766 | # endif |
| 3767 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3768 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3769 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3770 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3771 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3772 | const struct reserved_combo *r; |
| 3773 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3774 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3775 | if (strcmp(word->data, r->literal) == 0) |
| 3776 | return r; |
| 3777 | } |
| 3778 | return NULL; |
| 3779 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3780 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3781 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3782 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3783 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3784 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3785 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3786 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3787 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3788 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3789 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3790 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3791 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3792 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3793 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3794 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3795 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3796 | |
| 3797 | 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] | 3798 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3799 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3800 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3801 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3802 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3803 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3804 | if (r->flag == 0) { /* '!' */ |
| 3805 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3806 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3807 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3808 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3809 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3810 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3811 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3812 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3813 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3814 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3815 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3816 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3817 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3818 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3819 | } 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] | 3820 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3821 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3822 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3823 | } else { |
| 3824 | /* "{...} fi" is ok. "{...} if" is not |
| 3825 | * Example: |
| 3826 | * if { echo foo; } then { echo bar; } fi */ |
| 3827 | if (ctx->command->group) |
| 3828 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3829 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3830 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3831 | ctx->ctx_res_w = r->res; |
| 3832 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3833 | ctx->is_assignment = r->assignment_flag; |
| 3834 | 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] | 3835 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3836 | if (ctx->old_flag & FLAG_END) { |
| 3837 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3838 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3839 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3840 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3841 | old = ctx->stack; |
| 3842 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3843 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3844 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3845 | /* At this point, the compound command's string is in |
| 3846 | * ctx->as_string... except for the leading keyword! |
| 3847 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3848 | * ctx->as_string will contain "true; then echo a; fi", |
| 3849 | * with "if " remaining in old->as_string! |
| 3850 | */ |
| 3851 | { |
| 3852 | char *str; |
| 3853 | int len = old->as_string.length; |
| 3854 | /* Concatenate halves */ |
| 3855 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3856 | o_free_unsafe(&ctx->as_string); |
| 3857 | /* Find where leading keyword starts in first half */ |
| 3858 | str = old->as_string.data + len; |
| 3859 | if (str > old->as_string.data) |
| 3860 | str--; /* skip whitespace after keyword */ |
| 3861 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3862 | str--; |
| 3863 | /* Ugh, we're done with this horrid hack */ |
| 3864 | old->command->group_as_string = xstrdup(str); |
| 3865 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3866 | old->command->group_as_string); |
| 3867 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3868 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3869 | *ctx = *old; /* physical copy */ |
| 3870 | free(old); |
| 3871 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3872 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3873 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3874 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3875 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3876 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3877 | * Normal return is 0. Syntax errors return 1. |
| 3878 | * Note: on return, word is reset, but not o_free'd! |
| 3879 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3880 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3881 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3882 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3883 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3884 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 3885 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3886 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3887 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3888 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3889 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3890 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3891 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3892 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3893 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3894 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3895 | * If the redirection operator is "<<" or "<<-", the word |
| 3896 | * that follows the redirection operator shall be |
| 3897 | * subjected to quote removal; it is unspecified whether |
| 3898 | * any of the other expansions occur. For the other |
| 3899 | * redirection operators, the word that follows the |
| 3900 | * redirection operator shall be subjected to tilde |
| 3901 | * expansion, parameter expansion, command substitution, |
| 3902 | * arithmetic expansion, and quote removal. |
| 3903 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3904 | * on the word by a non-interactive shell; an interactive |
| 3905 | * shell may perform it, but shall do so only when |
| 3906 | * the expansion would result in one word." |
| 3907 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3908 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3909 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3910 | // as written: |
| 3911 | // <<EOF$t |
| 3912 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3913 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3914 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3915 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3916 | /* Cater for >\file case: |
| 3917 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3918 | * Same with heredocs: |
| 3919 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3920 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3921 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3922 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3923 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3924 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3925 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3926 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3927 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3928 | 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] | 3929 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3930 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3931 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3932 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3933 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3934 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3935 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3936 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3937 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3938 | ctx->ctx_dsemicolon = 0; |
| 3939 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3940 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3941 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3942 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3943 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3944 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3945 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3946 | # if ENABLE_HUSH_CASE |
| 3947 | && ctx->ctx_res_w != RES_CASE |
| 3948 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3949 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3950 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3951 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3952 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3953 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3954 | # if ENABLE_HUSH_LINENO_VAR |
| 3955 | /* Case: |
| 3956 | * "while ...; do |
| 3957 | * cmd ..." |
| 3958 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3959 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3960 | */ |
| 3961 | if (0 |
| 3962 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3963 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3964 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3965 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3966 | ) { |
| 3967 | done_pipe(ctx, PIPE_SEQ); |
| 3968 | } |
| 3969 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3970 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3971 | debug_printf_parse("done_word return %d\n", |
| 3972 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3973 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3974 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3975 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 3976 | if (0 |
| 3977 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3978 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3979 | # endif |
| 3980 | /* In bash, local/export/readonly are special, args |
| 3981 | * are assignments and therefore expansion of them |
| 3982 | * should be "one-word" expansion: |
| 3983 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 3984 | * compare with: |
| 3985 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 3986 | * ls: cannot access i=a: No such file or directory |
| 3987 | * ls: cannot access b: No such file or directory |
| 3988 | * Note: bash 3.2.33(1) does this only if export word |
| 3989 | * itself is not quoted: |
| 3990 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 3991 | * aaa bbb |
| 3992 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 3993 | * aaa |
| 3994 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3995 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 3996 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 3997 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3998 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3999 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4000 | } |
| 4001 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4002 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4003 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4004 | #endif /* HAS_KEYWORDS */ |
| 4005 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4006 | if (command->group) { |
| 4007 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4008 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4009 | debug_printf_parse("done_word return 1: syntax error, " |
| 4010 | "groups and arglists don't mix\n"); |
| 4011 | return 1; |
| 4012 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4013 | |
| 4014 | /* If this word wasn't an assignment, next ones definitely |
| 4015 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4016 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4017 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4018 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4019 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4020 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4021 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4022 | command->assignment_cnt++; |
| 4023 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4024 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4025 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4026 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4027 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4028 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4029 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4030 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4031 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4032 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4033 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4034 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4035 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4036 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4037 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4038 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4039 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4040 | return 1; |
| 4041 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4042 | /* Force FOR to have just one word (variable name) */ |
| 4043 | /* NB: basically, this makes hush see "for v in ..." |
| 4044 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4045 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4046 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4047 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4048 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4049 | #if ENABLE_HUSH_CASE |
| 4050 | /* Force CASE to have just one word */ |
| 4051 | if (ctx->ctx_res_w == RES_CASE) { |
| 4052 | done_pipe(ctx, PIPE_SEQ); |
| 4053 | } |
| 4054 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4055 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4056 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4057 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4058 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4059 | return 0; |
| 4060 | } |
| 4061 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4062 | |
| 4063 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4064 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4065 | * Return: |
| 4066 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4067 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4068 | * REDIRFD_TO_FILE if no & was seen, |
| 4069 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4070 | */ |
| 4071 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4072 | #define parse_redir_right_fd(as_string, input) \ |
| 4073 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4074 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4075 | 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] | 4076 | { |
| 4077 | int ch, d, ok; |
| 4078 | |
| 4079 | ch = i_peek(input); |
| 4080 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4081 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4082 | |
| 4083 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4084 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4085 | ch = i_peek(input); |
| 4086 | if (ch == '-') { |
| 4087 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4088 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4089 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4090 | } |
| 4091 | d = 0; |
| 4092 | ok = 0; |
| 4093 | while (ch != EOF && isdigit(ch)) { |
| 4094 | d = d*10 + (ch-'0'); |
| 4095 | ok = 1; |
| 4096 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4097 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4098 | ch = i_peek(input); |
| 4099 | } |
| 4100 | if (ok) return d; |
| 4101 | |
| 4102 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4103 | |
| 4104 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4105 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4108 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4109 | */ |
| 4110 | static int parse_redirect(struct parse_context *ctx, |
| 4111 | int fd, |
| 4112 | redir_type style, |
| 4113 | struct in_str *input) |
| 4114 | { |
| 4115 | struct command *command = ctx->command; |
| 4116 | struct redir_struct *redir; |
| 4117 | struct redir_struct **redirp; |
| 4118 | int dup_num; |
| 4119 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4120 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4121 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4122 | /* Check for a '>&1' type redirect */ |
| 4123 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4124 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4125 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4126 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4127 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4128 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4129 | if (dup_num) { /* <<-... */ |
| 4130 | ch = i_getch(input); |
| 4131 | nommu_addchr(&ctx->as_string, ch); |
| 4132 | ch = i_peek(input); |
| 4133 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4134 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4135 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4136 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4137 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4138 | if (ch == '|') { |
| 4139 | /* >|FILE redirect ("clobbering" >). |
| 4140 | * Since we do not support "set -o noclobber" yet, |
| 4141 | * >| and > are the same for now. Just eat |. |
| 4142 | */ |
| 4143 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4144 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | /* Create a new redir_struct and append it to the linked list */ |
| 4149 | redirp = &command->redirects; |
| 4150 | while ((redir = *redirp) != NULL) { |
| 4151 | redirp = &(redir->next); |
| 4152 | } |
| 4153 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4154 | /* redir->next = NULL; */ |
| 4155 | /* redir->rd_filename = NULL; */ |
| 4156 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4157 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4158 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4159 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4160 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4161 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4162 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4163 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4164 | /* Erik had a check here that the file descriptor in question |
| 4165 | * is legit; I postpone that to "run time" |
| 4166 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4167 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4168 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4169 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4170 | #if 0 /* Instead we emit error message at run time */ |
| 4171 | if (ctx->pending_redirect) { |
| 4172 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4173 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4174 | } |
| 4175 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4176 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4177 | * end of the next parsed word. */ |
| 4178 | ctx->pending_redirect = redir; |
| 4179 | } |
| 4180 | return 0; |
| 4181 | } |
| 4182 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4183 | /* If a redirect is immediately preceded by a number, that number is |
| 4184 | * supposed to tell which file descriptor to redirect. This routine |
| 4185 | * looks for such preceding numbers. In an ideal world this routine |
| 4186 | * needs to handle all the following classes of redirects... |
| 4187 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4188 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4189 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4190 | * 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] | 4191 | * |
| 4192 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4193 | * "2.7 Redirection |
| 4194 | * ... If n is quoted, the number shall not be recognized as part of |
| 4195 | * the redirection expression. For example: |
| 4196 | * echo \2>a |
| 4197 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4198 | * 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] | 4199 | * |
| 4200 | * A -1 return means no valid number was found, |
| 4201 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4202 | */ |
| 4203 | static int redirect_opt_num(o_string *o) |
| 4204 | { |
| 4205 | int num; |
| 4206 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4207 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4208 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4209 | num = bb_strtou(o->data, NULL, 10); |
| 4210 | if (errno || num < 0) |
| 4211 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4212 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4213 | return num; |
| 4214 | } |
| 4215 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4216 | #if BB_MMU |
| 4217 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4218 | fetch_till_str(input, word, skip_tabs) |
| 4219 | #endif |
| 4220 | static char *fetch_till_str(o_string *as_string, |
| 4221 | struct in_str *input, |
| 4222 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4223 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4224 | { |
| 4225 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4226 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4227 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4228 | int ch; |
| 4229 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4230 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4231 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4232 | while (1) { |
| 4233 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4234 | if (ch != EOF) |
| 4235 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4236 | if (ch == '\n' || ch == EOF) { |
| 4237 | check_heredoc_end: |
| 4238 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4239 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4240 | heredoc.data[past_EOL] = '\0'; |
| 4241 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4242 | return heredoc.data; |
| 4243 | } |
| 4244 | if (ch == '\n') { |
| 4245 | /* This is a new line. |
| 4246 | * Remember position and backslash-escaping status. |
| 4247 | */ |
| 4248 | o_addchr(&heredoc, ch); |
| 4249 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4250 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4251 | past_EOL = heredoc.length; |
| 4252 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4253 | do { |
| 4254 | ch = i_getch(input); |
| 4255 | if (ch != EOF) |
| 4256 | nommu_addchr(as_string, ch); |
| 4257 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4258 | /* If this immediately ended the line, |
| 4259 | * go back to end-of-line checks. |
| 4260 | */ |
| 4261 | if (ch == '\n') |
| 4262 | goto check_heredoc_end; |
| 4263 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4264 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4265 | } |
| 4266 | if (ch == EOF) { |
| 4267 | o_free_unsafe(&heredoc); |
| 4268 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4269 | } |
| 4270 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4271 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4272 | if (prev == '\\' && ch == '\\') |
| 4273 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4274 | prev = 0; /* not \ */ |
| 4275 | else |
| 4276 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4280 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4281 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4282 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4283 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4284 | { |
| 4285 | struct pipe *pi = ctx->list_head; |
| 4286 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4287 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4288 | int i; |
| 4289 | struct command *cmd = pi->cmds; |
| 4290 | |
| 4291 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4292 | pi->num_cmds, |
| 4293 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4294 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4295 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4296 | |
| 4297 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4298 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4299 | while (redir) { |
| 4300 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4301 | char *p; |
| 4302 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4303 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4304 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4305 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4306 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4307 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4308 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4309 | return 1; |
| 4310 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4311 | free(redir->rd_filename); |
| 4312 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4313 | heredoc_cnt--; |
| 4314 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4315 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4316 | } |
| 4317 | cmd++; |
| 4318 | } |
| 4319 | pi = pi->next; |
| 4320 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4321 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4322 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4323 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4324 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4325 | #endif |
| 4326 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4330 | static int run_list(struct pipe *pi); |
| 4331 | #if BB_MMU |
| 4332 | #define parse_stream(pstring, input, end_trigger) \ |
| 4333 | parse_stream(input, end_trigger) |
| 4334 | #endif |
| 4335 | static struct pipe *parse_stream(char **pstring, |
| 4336 | struct in_str *input, |
| 4337 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4338 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4339 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4340 | static int parse_group(struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4341 | struct in_str *input, int ch) |
| 4342 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4343 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4344 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4345 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4346 | #if BB_MMU |
| 4347 | # define as_string NULL |
| 4348 | #else |
| 4349 | char *as_string = NULL; |
| 4350 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4351 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4352 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4353 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4354 | |
| 4355 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4356 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4357 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4358 | if (ctx->word.length) |
| 4359 | if (done_word(ctx)) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4360 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4361 | if (!command->argv) |
| 4362 | goto skip; /* (... */ |
| 4363 | if (command->argv[1]) { /* word word ... (... */ |
| 4364 | syntax_error_unexpected_ch('('); |
| 4365 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4366 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4367 | /* it is "word(..." or "word (..." */ |
| 4368 | do |
| 4369 | ch = i_getch(input); |
| 4370 | while (ch == ' ' || ch == '\t'); |
| 4371 | if (ch != ')') { |
| 4372 | syntax_error_unexpected_ch(ch); |
| 4373 | return 1; |
| 4374 | } |
| 4375 | nommu_addchr(&ctx->as_string, ch); |
| 4376 | do |
| 4377 | ch = i_getch(input); |
| 4378 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4379 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4380 | syntax_error_unexpected_ch(ch); |
| 4381 | return 1; |
| 4382 | } |
| 4383 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4384 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4385 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4386 | } |
| 4387 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4388 | |
| 4389 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4390 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4391 | || ctx->word.length /* word{... */ |
| 4392 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4393 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4394 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4395 | debug_printf_parse("parse_group return 1: " |
| 4396 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4397 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4398 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4399 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4400 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4401 | IF_HUSH_FUNCTIONS(skip:) |
| 4402 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4403 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4404 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4405 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4406 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4407 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4408 | } else { |
| 4409 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4410 | ch = i_peek(input); |
| 4411 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4412 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4413 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4414 | syntax_error_unexpected_ch(ch); |
| 4415 | return 1; |
| 4416 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4417 | if (ch != '(') { |
| 4418 | ch = i_getch(input); |
| 4419 | nommu_addchr(&ctx->as_string, ch); |
| 4420 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4421 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4422 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4423 | pipe_list = parse_stream(&as_string, input, endch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4424 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4425 | if (as_string) |
| 4426 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4427 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4428 | |
| 4429 | /* empty ()/{} or parse error? */ |
| 4430 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4431 | /* parse_stream already emitted error msg */ |
| 4432 | if (!BB_MMU) |
| 4433 | free(as_string); |
| 4434 | debug_printf_parse("parse_group return 1: " |
| 4435 | "parse_stream returned %p\n", pipe_list); |
| 4436 | return 1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4437 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4438 | #if !BB_MMU |
| 4439 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4440 | command->group_as_string = as_string; |
| 4441 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4442 | command->group_as_string); |
| 4443 | #endif |
| 4444 | |
| 4445 | #if ENABLE_HUSH_FUNCTIONS |
| 4446 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4447 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4448 | struct command *cmd2; |
| 4449 | |
| 4450 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4451 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4452 | cmd2->group = pipe_list; |
| 4453 | # if !BB_MMU |
| 4454 | //UNTESTED! |
| 4455 | cmd2->group_as_string = command->group_as_string; |
| 4456 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4457 | # endif |
| 4458 | |
| 4459 | pipe_list = new_pipe(); |
| 4460 | pipe_list->cmds = cmd2; |
| 4461 | pipe_list->num_cmds = 1; |
| 4462 | } |
| 4463 | #endif |
| 4464 | |
| 4465 | command->group = pipe_list; |
| 4466 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4467 | debug_printf_parse("parse_group return 0\n"); |
| 4468 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4469 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4470 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4473 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4474 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4475 | 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] | 4476 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4477 | 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] | 4478 | { |
| 4479 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4480 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4481 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4482 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4483 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4484 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4485 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4486 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4487 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4491 | 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] | 4492 | { |
| 4493 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4494 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4495 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4496 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4497 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4498 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4499 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4500 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4501 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4502 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4503 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4504 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4505 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4506 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4507 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4508 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4509 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4510 | continue; |
| 4511 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4512 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4513 | } |
| 4514 | } |
| 4515 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4516 | * \` quoting. |
| 4517 | * "Within the backquoted style of command substitution, backslash |
| 4518 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4519 | * The search for the matching backquote shall be satisfied by the first |
| 4520 | * backquote found without a preceding backslash; during this search, |
| 4521 | * if a non-escaped backquote is encountered within a shell comment, |
| 4522 | * a here-document, an embedded command substitution of the $(command) |
| 4523 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4524 | * or double-quoted string that begins, but does not end, within the |
| 4525 | * "`...`" sequence produces undefined results." |
| 4526 | * Example Output |
| 4527 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4528 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4529 | 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] | 4530 | { |
| 4531 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4532 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4533 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4534 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4535 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4536 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4537 | ch = i_getch(input); |
| 4538 | if (ch != '`' |
| 4539 | && ch != '$' |
| 4540 | && ch != '\\' |
| 4541 | && (!in_dquote || ch != '"') |
| 4542 | ) { |
| 4543 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4544 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4545 | } |
| 4546 | if (ch == EOF) { |
| 4547 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4548 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4549 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4550 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4554 | * quoting and nested ()s. |
| 4555 | * "With the $(command) style of command substitution, all characters |
| 4556 | * following the open parenthesis to the matching closing parenthesis |
| 4557 | * constitute the command. Any valid shell script can be used for command, |
| 4558 | * except a script consisting solely of redirections which produces |
| 4559 | * unspecified results." |
| 4560 | * Example Output |
| 4561 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4562 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4563 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4564 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4565 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4566 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4567 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4568 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4569 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4570 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4571 | 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] | 4572 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4573 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4574 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4575 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4576 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4577 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4578 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4579 | |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4580 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4581 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4582 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4583 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4584 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4585 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4586 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4587 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4588 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4589 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4590 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4591 | if (ch == end_ch |
| 4592 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4593 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4594 | # endif |
| 4595 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4596 | if (!dbl) |
| 4597 | break; |
| 4598 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4599 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4600 | i_getch(input); /* eat second ')' */ |
| 4601 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4602 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4603 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4604 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4605 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4606 | if (ch == '(' || ch == '{') { |
| 4607 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4608 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4609 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4610 | o_addchr(dest, ch); |
| 4611 | continue; |
| 4612 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4613 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4614 | if (!add_till_single_quote(dest, input)) |
| 4615 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4616 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4617 | continue; |
| 4618 | } |
| 4619 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4620 | if (!add_till_double_quote(dest, input)) |
| 4621 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4622 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4623 | continue; |
| 4624 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4625 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4626 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4627 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4628 | o_addchr(dest, ch); |
| 4629 | continue; |
| 4630 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4631 | if (ch == '\\') { |
| 4632 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4633 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4634 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4635 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4636 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4637 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4638 | #if 0 |
| 4639 | if (ch == '\n') { |
| 4640 | /* "backslash+newline", ignore both */ |
| 4641 | o_delchr(dest); /* undo insertion of '\' */ |
| 4642 | continue; |
| 4643 | } |
| 4644 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4645 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4646 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4647 | continue; |
| 4648 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4649 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4650 | 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] | 4651 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4652 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4653 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4654 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4655 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4656 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4657 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4658 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4659 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4660 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4661 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4662 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4663 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4664 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4665 | 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] | 4666 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4667 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4668 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4669 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4670 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4671 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4672 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4673 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4674 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4675 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4676 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4677 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4678 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4679 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4680 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4681 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4682 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4683 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4684 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4685 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4686 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4687 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4688 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4689 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4690 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4691 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4692 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4693 | o_addchr(dest, ch | quote_mask); |
| 4694 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4695 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4696 | case '$': /* pid */ |
| 4697 | case '!': /* last bg pid */ |
| 4698 | case '?': /* last exit code */ |
| 4699 | case '#': /* number of args */ |
| 4700 | case '*': /* args */ |
| 4701 | case '@': /* args */ |
| 4702 | goto make_one_char_var; |
| 4703 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4704 | char len_single_ch; |
| 4705 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4706 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4707 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4708 | ch = i_getch(input); /* eat '{' */ |
| 4709 | nommu_addchr(as_string, ch); |
| 4710 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4711 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4712 | /* It should be ${?}, or ${#var}, |
| 4713 | * or even ${?+subst} - operator acting on a special variable, |
| 4714 | * or the beginning of variable name. |
| 4715 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4716 | if (ch == EOF |
| 4717 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4718 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4719 | bad_dollar_syntax: |
| 4720 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4721 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4722 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4723 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4724 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4725 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4726 | ch |= quote_mask; |
| 4727 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4728 | /* 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] | 4729 | * However, this regresses some of our testsuite cases |
| 4730 | * which check invalid constructs like ${%}. |
| 4731 | * Oh well... let's check that the var name part is fine... */ |
| 4732 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4733 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4734 | unsigned pos; |
| 4735 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4736 | o_addchr(dest, ch); |
| 4737 | debug_printf_parse(": '%c'\n", ch); |
| 4738 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4739 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4740 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4741 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4742 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4743 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4744 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4745 | unsigned end_ch; |
| 4746 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4747 | /* handle parameter expansions |
| 4748 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4749 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4750 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4751 | if (len_single_ch != '#' |
| 4752 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4753 | || i_peek(input) != '}' |
| 4754 | ) { |
| 4755 | goto bad_dollar_syntax; |
| 4756 | } |
| 4757 | /* else: it's "length of C" ${#C} op, |
| 4758 | * where C is a single char |
| 4759 | * special var name, e.g. ${#!}. |
| 4760 | */ |
| 4761 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4762 | /* Eat everything until closing '}' (or ':') */ |
| 4763 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4764 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4765 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4766 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4767 | ) { |
| 4768 | /* It's ${var:N[:M]} thing */ |
| 4769 | end_ch = '}' * 0x100 + ':'; |
| 4770 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4771 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4772 | && ch == '/' |
| 4773 | ) { |
| 4774 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4775 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4776 | i_getch(input); |
| 4777 | nommu_addchr(as_string, '/'); |
| 4778 | ch = '\\'; |
| 4779 | } |
| 4780 | end_ch = '}' * 0x100 + '/'; |
| 4781 | } |
| 4782 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4783 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4784 | if (!BB_MMU) |
| 4785 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4786 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4787 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4788 | if (last_ch == 0) /* error? */ |
| 4789 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4790 | #else |
| 4791 | #error Simple code to only allow ${var} is not implemented |
| 4792 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4793 | if (as_string) { |
| 4794 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4795 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4796 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4797 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4798 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4799 | && (end_ch & 0xff00) |
| 4800 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4801 | /* close the first block: */ |
| 4802 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4803 | /* while parsing N from ${var:N[:M]} |
| 4804 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4805 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4806 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4807 | end_ch = '}'; |
| 4808 | goto again; |
| 4809 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4810 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4811 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4812 | /* it's ${var:N} - emulate :999999999 */ |
| 4813 | o_addstr(dest, "999999999"); |
| 4814 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4815 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4816 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4817 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4818 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4819 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4820 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4821 | break; |
| 4822 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4823 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4824 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4825 | unsigned pos; |
| 4826 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4827 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4828 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4829 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4830 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4831 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4832 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4833 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4834 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4835 | if (!BB_MMU) |
| 4836 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4837 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4838 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4839 | if (as_string) { |
| 4840 | o_addstr(as_string, dest->data + pos); |
| 4841 | o_addchr(as_string, ')'); |
| 4842 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4843 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4844 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4845 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4846 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4847 | # endif |
| 4848 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4849 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4850 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4851 | if (!BB_MMU) |
| 4852 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4853 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4854 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4855 | if (as_string) { |
| 4856 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4857 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4858 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4859 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4860 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4861 | break; |
| 4862 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4863 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4864 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4865 | goto make_var; |
| 4866 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4867 | /* TODO: $_ and $-: */ |
| 4868 | /* $_ Shell or shell script name; or last argument of last command |
| 4869 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4870 | * 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] | 4871 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4872 | ch = i_getch(input); |
| 4873 | nommu_addchr(as_string, ch); |
| 4874 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4875 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4876 | ch = '_'; |
| 4877 | goto make_var1; |
| 4878 | } |
| 4879 | /* else: it's $_ */ |
| 4880 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4881 | default: |
| 4882 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4883 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4884 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4885 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4886 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4889 | #if BB_MMU |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4890 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4891 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4892 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4893 | # else |
| 4894 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4895 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4896 | encode_string(dest, input, dquote_end) |
| 4897 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4898 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4899 | |
| 4900 | #else /* !MMU */ |
| 4901 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4902 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4903 | /* all parameters are needed, no macro tricks */ |
| 4904 | # else |
| 4905 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4906 | encode_string(as_string, dest, input, dquote_end) |
| 4907 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4908 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4909 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4910 | o_string *dest, |
| 4911 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4912 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4913 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4914 | { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4915 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4916 | const int process_bkslash = 1; |
| 4917 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4918 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4919 | int next; |
| 4920 | |
| 4921 | again: |
| 4922 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4923 | if (ch != EOF) |
| 4924 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4925 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4926 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4927 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4928 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4929 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4930 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4931 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4932 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4933 | } |
| 4934 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4935 | if (ch != '\n') { |
| 4936 | next = i_peek(input); |
| 4937 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4938 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4939 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4940 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4941 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 4942 | /* Testcase: in interactive shell a file with |
| 4943 | * echo "unterminated string\<eof> |
| 4944 | * is sourced. |
| 4945 | */ |
| 4946 | syntax_error_unterm_ch('"'); |
| 4947 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4948 | } |
| 4949 | /* bash: |
| 4950 | * "The backslash retains its special meaning [in "..."] |
| 4951 | * only when followed by one of the following characters: |
| 4952 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4953 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4954 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4955 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4956 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4957 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4958 | ch = i_getch(input); /* eat next */ |
| 4959 | if (ch == '\n') |
| 4960 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4961 | } /* else: ch remains == '\\', and we double it below: */ |
| 4962 | 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] | 4963 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4964 | goto again; |
| 4965 | } |
| 4966 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4967 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4968 | debug_printf_parse("encode_string return 0: " |
| 4969 | "parse_dollar returned 0 (error)\n"); |
| 4970 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4971 | } |
| 4972 | goto again; |
| 4973 | } |
| 4974 | #if ENABLE_HUSH_TICK |
| 4975 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4976 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4977 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4978 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4979 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4980 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4981 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4982 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4983 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4984 | } |
| 4985 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4986 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4987 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4988 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4989 | } |
| 4990 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4991 | /* |
| 4992 | * Scan input until EOF or end_trigger char. |
| 4993 | * Return a list of pipes to execute, or NULL on EOF |
| 4994 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4995 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4996 | * reset parsing machinery and start parsing anew, |
| 4997 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4998 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4999 | static struct pipe *parse_stream(char **pstring, |
| 5000 | struct in_str *input, |
| 5001 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5002 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5003 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5004 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5005 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5006 | /* 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] | 5007 | * 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] | 5008 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5009 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5010 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5011 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5012 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5013 | initialize_context(&ctx); |
| 5014 | |
| 5015 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5016 | * Preventing this: |
| 5017 | */ |
| 5018 | o_addchr(&ctx.word, '\0'); |
| 5019 | ctx.word.length = 0; |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5020 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5021 | /* We used to separate words on $IFS here. This was wrong. |
| 5022 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5023 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5024 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5025 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5026 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5027 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5028 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5029 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5030 | int ch; |
| 5031 | int next; |
| 5032 | int redir_fd; |
| 5033 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5034 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5035 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5036 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5037 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5038 | if (ch == EOF) { |
| 5039 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5040 | |
| 5041 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5042 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5043 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5044 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5045 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5046 | syntax_error_unterm_ch('('); |
| 5047 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5048 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5049 | if (end_trigger == '}') { |
| 5050 | syntax_error_unterm_ch('{'); |
| 5051 | goto parse_error; |
| 5052 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5053 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5054 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5055 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5056 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5057 | o_free(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5058 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5059 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5060 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5061 | /* (this makes bare "&" cmd a no-op. |
| 5062 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5063 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5064 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5065 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5066 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5067 | pi = NULL; |
| 5068 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5069 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5070 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5071 | if (pstring) |
| 5072 | *pstring = ctx.as_string.data; |
| 5073 | else |
| 5074 | o_free_unsafe(&ctx.as_string); |
| 5075 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5076 | debug_leave(); |
| 5077 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5078 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5079 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5080 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5081 | /* Handle "'" and "\" first, as they won't play nice with |
| 5082 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5083 | * echo z\\ |
| 5084 | * and |
| 5085 | * echo '\ |
| 5086 | * ' |
| 5087 | * would break. |
| 5088 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5089 | if (ch == '\\') { |
| 5090 | ch = i_getch(input); |
| 5091 | if (ch == '\n') |
| 5092 | continue; /* drop \<newline>, get next char */ |
| 5093 | nommu_addchr(&ctx.as_string, '\\'); |
| 5094 | o_addchr(&ctx.word, '\\'); |
| 5095 | if (ch == EOF) { |
| 5096 | /* Testcase: eval 'echo Ok\' */ |
| 5097 | /* bash-4.3.43 was removing backslash, |
| 5098 | * but 4.4.19 retains it, most other shells too |
| 5099 | */ |
| 5100 | continue; /* get next char */ |
| 5101 | } |
| 5102 | /* Example: echo Hello \2>file |
| 5103 | * we need to know that word 2 is quoted |
| 5104 | */ |
| 5105 | ctx.word.has_quoted_part = 1; |
| 5106 | nommu_addchr(&ctx.as_string, ch); |
| 5107 | o_addchr(&ctx.word, ch); |
| 5108 | continue; /* get next char */ |
| 5109 | } |
| 5110 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5111 | if (ch == '\'') { |
| 5112 | ctx.word.has_quoted_part = 1; |
| 5113 | next = i_getch(input); |
| 5114 | if (next == '\'' && !ctx.pending_redirect) |
| 5115 | goto insert_empty_quoted_str_marker; |
| 5116 | |
| 5117 | ch = next; |
| 5118 | while (1) { |
| 5119 | if (ch == EOF) { |
| 5120 | syntax_error_unterm_ch('\''); |
| 5121 | goto parse_error; |
| 5122 | } |
| 5123 | nommu_addchr(&ctx.as_string, ch); |
| 5124 | if (ch == '\'') |
| 5125 | break; |
| 5126 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5127 | /* Convert raw ^C to corresponding special variable reference */ |
| 5128 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5129 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5130 | } |
| 5131 | o_addqchr(&ctx.word, ch); |
| 5132 | ch = i_getch(input); |
| 5133 | } |
| 5134 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5135 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5136 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5137 | next = '\0'; |
| 5138 | if (ch != '\n') |
| 5139 | next = i_peek_and_eat_bkslash_nl(input); |
| 5140 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5141 | is_special = "{}<>;&|()#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5142 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5143 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5144 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5145 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5146 | || ctx.word.length /* word{... - non-special */ |
| 5147 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5148 | || (next != ';' /* }; - special */ |
| 5149 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5150 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5151 | && next != '&' /* }& and }&& ... - special */ |
| 5152 | && next != '|' /* }|| ... - special */ |
| 5153 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5154 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5155 | ) { |
| 5156 | /* They are not special, skip "{}" */ |
| 5157 | is_special += 2; |
| 5158 | } |
| 5159 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5160 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5161 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5162 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5163 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5164 | o_addQchr(&ctx.word, ch); |
| 5165 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5166 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5167 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5168 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5169 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5170 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5171 | 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] | 5172 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5173 | continue; |
| 5174 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5175 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5176 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5177 | #if ENABLE_HUSH_LINENO_VAR |
| 5178 | /* Case: |
| 5179 | * "while ...; do<whitespace><newline> |
| 5180 | * cmd ..." |
| 5181 | * would think that "cmd" starts in <whitespace> - |
| 5182 | * i.e., at the previous line. |
| 5183 | * We need to skip all whitespace before newlines. |
| 5184 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5185 | while (ch != '\n') { |
| 5186 | next = i_peek(input); |
| 5187 | if (next != ' ' && next != '\t' && next != '\n') |
| 5188 | break; /* next char is not ws */ |
| 5189 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5190 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5191 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5192 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5193 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5194 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5195 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5196 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5197 | /* Is this a case when newline is simply ignored? |
| 5198 | * Some examples: |
| 5199 | * "cmd | <newline> cmd ..." |
| 5200 | * "case ... in <newline> word) ..." |
| 5201 | */ |
| 5202 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5203 | && ctx.word.length == 0 && !ctx.word.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5204 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5205 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5206 | * Without check #1, interactive shell |
| 5207 | * ignores even bare <newline>, |
| 5208 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5209 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5210 | * ps2> _ <=== wrong, should be ps1 |
| 5211 | * Without check #2, "cmd & <newline>" |
| 5212 | * is similarly mistreated. |
| 5213 | * (BTW, this makes "cmd & cmd" |
| 5214 | * and "cmd && cmd" non-orthogonal. |
| 5215 | * Really, ask yourself, why |
| 5216 | * "cmd && <newline>" doesn't start |
| 5217 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5218 | * The only reason is that it might be |
| 5219 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5220 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5221 | */ |
| 5222 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5223 | if (pi->num_cmds != 0 /* check #1 */ |
| 5224 | && pi->followup != PIPE_BG /* check #2 */ |
| 5225 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5226 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5227 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5228 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5229 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5230 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5231 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5232 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5233 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5234 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5235 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5236 | heredoc_cnt = 0; |
| 5237 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5238 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5239 | 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] | 5240 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5241 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5242 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5243 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5244 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5245 | |
| 5246 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5247 | * } is an ordinary char in this case, even inside { cmd; } |
| 5248 | * Pathological example: { ""}; } should exec "}" cmd |
| 5249 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5250 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5251 | if (ctx.word.length != 0 /* word} */ |
| 5252 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5253 | ) { |
| 5254 | goto ordinary_char; |
| 5255 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5256 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5257 | /* Generally, there should be semicolon: "cmd; }" |
| 5258 | * However, bash allows to omit it if "cmd" is |
| 5259 | * a group. Examples: |
| 5260 | * { { echo 1; } } |
| 5261 | * {(echo 1)} |
| 5262 | * { echo 0 >&2 | { echo 1; } } |
| 5263 | * { while false; do :; done } |
| 5264 | * { case a in b) ;; esac } |
| 5265 | */ |
| 5266 | if (ctx.command->group) |
| 5267 | goto term_group; |
| 5268 | goto ordinary_char; |
| 5269 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5270 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5271 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5272 | goto skip_end_trigger; |
| 5273 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5274 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5275 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5276 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5277 | && (ch != ';' || heredoc_cnt == 0) |
| 5278 | #if ENABLE_HUSH_CASE |
| 5279 | && (ch != ')' |
| 5280 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5281 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5282 | ) |
| 5283 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5284 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5285 | if (heredoc_cnt) { |
| 5286 | /* This is technically valid: |
| 5287 | * { cat <<HERE; }; echo Ok |
| 5288 | * heredoc |
| 5289 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5290 | * HERE |
| 5291 | * but we don't support this. |
| 5292 | * We require heredoc to be in enclosing {}/(), |
| 5293 | * if any. |
| 5294 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5295 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5296 | goto parse_error; |
| 5297 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5298 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5299 | goto parse_error; |
| 5300 | } |
| 5301 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5302 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5303 | 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] | 5304 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5305 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5306 | 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] | 5307 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5308 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5309 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5310 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5311 | if (pstring) |
| 5312 | *pstring = ctx.as_string.data; |
| 5313 | else |
| 5314 | o_free_unsafe(&ctx.as_string); |
| 5315 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5316 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5317 | /* Example: bare "{ }", "()" */ |
| 5318 | G.last_exitcode = 2; /* bash compat */ |
| 5319 | syntax_error_unexpected_ch(ch); |
| 5320 | goto parse_error2; |
| 5321 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5322 | debug_printf_parse("parse_stream return %p: " |
| 5323 | "end_trigger char found\n", |
| 5324 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5325 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5326 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5327 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5328 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5329 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5330 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5331 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5332 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5333 | /* Catch <, > before deciding whether this word is |
| 5334 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5335 | switch (ch) { |
| 5336 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5337 | redir_fd = redirect_opt_num(&ctx.word); |
| 5338 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5339 | goto parse_error; |
| 5340 | } |
| 5341 | redir_style = REDIRECT_OVERWRITE; |
| 5342 | if (next == '>') { |
| 5343 | redir_style = REDIRECT_APPEND; |
| 5344 | ch = i_getch(input); |
| 5345 | nommu_addchr(&ctx.as_string, ch); |
| 5346 | } |
| 5347 | #if 0 |
| 5348 | else if (next == '(') { |
| 5349 | syntax_error(">(process) not supported"); |
| 5350 | goto parse_error; |
| 5351 | } |
| 5352 | #endif |
| 5353 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5354 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5355 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5356 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5357 | redir_fd = redirect_opt_num(&ctx.word); |
| 5358 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5359 | goto parse_error; |
| 5360 | } |
| 5361 | redir_style = REDIRECT_INPUT; |
| 5362 | if (next == '<') { |
| 5363 | redir_style = REDIRECT_HEREDOC; |
| 5364 | heredoc_cnt++; |
| 5365 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5366 | ch = i_getch(input); |
| 5367 | nommu_addchr(&ctx.as_string, ch); |
| 5368 | } else if (next == '>') { |
| 5369 | redir_style = REDIRECT_IO; |
| 5370 | ch = i_getch(input); |
| 5371 | nommu_addchr(&ctx.as_string, ch); |
| 5372 | } |
| 5373 | #if 0 |
| 5374 | else if (next == '(') { |
| 5375 | syntax_error("<(process) not supported"); |
| 5376 | goto parse_error; |
| 5377 | } |
| 5378 | #endif |
| 5379 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5380 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5381 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5382 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5383 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5384 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5385 | /* note: we do not add it to &ctx.as_string */ |
| 5386 | /* TODO: in bash: |
| 5387 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5388 | * cmd "$(cmd2 #comment)" - syntax error |
| 5389 | * cmd "`cmd2 #comment`" - ok |
| 5390 | * We accept both (comment ends where command subst ends, in both cases). |
| 5391 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5392 | while (1) { |
| 5393 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5394 | if (ch == '\n') { |
| 5395 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5396 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5397 | } |
| 5398 | ch = i_getch(input); |
| 5399 | if (ch == EOF) |
| 5400 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5401 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5402 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5403 | } |
| 5404 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5405 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5406 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5407 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5408 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5409 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5410 | && !ctx.pending_redirect |
| 5411 | ) { |
| 5412 | /* ch is a special char and thus this word |
| 5413 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5414 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5415 | 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] | 5416 | } |
| 5417 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5418 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5419 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5420 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5421 | case SPECIAL_VAR_SYMBOL: |
| 5422 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5423 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5424 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5425 | /* fall through */ |
| 5426 | case '#': |
| 5427 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5428 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5429 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5430 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5431 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5432 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5433 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5434 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5435 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5436 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5437 | case '"': |
| 5438 | ctx.word.has_quoted_part = 1; |
| 5439 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5440 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5441 | insert_empty_quoted_str_marker: |
| 5442 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5443 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5444 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5445 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5446 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5447 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5448 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
| 5449 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5450 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5451 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5452 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5453 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5454 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5455 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5456 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5457 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5458 | o_addchr(&ctx.word, '`'); |
| 5459 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5460 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5461 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5462 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5463 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5464 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5465 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5466 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5467 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5468 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5469 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5470 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5471 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5472 | #if ENABLE_HUSH_CASE |
| 5473 | case_semi: |
| 5474 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5475 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5476 | goto parse_error; |
| 5477 | } |
| 5478 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5479 | #if ENABLE_HUSH_CASE |
| 5480 | /* Eat multiple semicolons, detect |
| 5481 | * whether it means something special */ |
| 5482 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5483 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5484 | if (ch != ';') |
| 5485 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5486 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5487 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5488 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5489 | ctx.ctx_dsemicolon = 1; |
| 5490 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5491 | break; |
| 5492 | } |
| 5493 | } |
| 5494 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5495 | new_cmd: |
| 5496 | /* We just finished a cmd. New one may start |
| 5497 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5498 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5499 | 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] | 5500 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5501 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5502 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5503 | goto parse_error; |
| 5504 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5505 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5506 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5507 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5508 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5509 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5510 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5511 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5512 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5513 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5514 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5515 | goto parse_error; |
| 5516 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5517 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5518 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5519 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5520 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5521 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5522 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5523 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5524 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5525 | } else { |
| 5526 | /* we could pick up a file descriptor choice here |
| 5527 | * with redirect_opt_num(), but bash doesn't do it. |
| 5528 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5529 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5530 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5531 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5532 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5533 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5534 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5535 | if (ctx.ctx_res_w == RES_MATCH |
| 5536 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5537 | && ctx.word.length == 0 /* not word(... */ |
| 5538 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5539 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5540 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5541 | } |
| 5542 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5543 | case '{': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5544 | if (parse_group(&ctx, input, ch) != 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5545 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5546 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5547 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5548 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5549 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5550 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5551 | goto case_semi; |
| 5552 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5553 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5554 | /* proper use of this character is caught by end_trigger: |
| 5555 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5556 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5557 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5558 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5559 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5560 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5561 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5562 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5563 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5564 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5565 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5566 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5567 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5568 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5569 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5570 | struct parse_context *pctx; |
| 5571 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5572 | |
| 5573 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5574 | * Sample for finding leaks on syntax error recovery path. |
| 5575 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5576 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5577 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5578 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5579 | * 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] | 5580 | */ |
| 5581 | pctx = &ctx; |
| 5582 | do { |
| 5583 | /* Update pipe/command counts, |
| 5584 | * otherwise freeing may miss some */ |
| 5585 | done_pipe(pctx, PIPE_SEQ); |
| 5586 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5587 | pctx->list_head, pctx); |
| 5588 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5589 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5590 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5591 | #if !BB_MMU |
| 5592 | o_free_unsafe(&pctx->as_string); |
| 5593 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5594 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5595 | if (pctx != &ctx) { |
| 5596 | free(pctx); |
| 5597 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5598 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5599 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5600 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5601 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5602 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5603 | if (pstring) |
| 5604 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5605 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5606 | debug_leave(); |
| 5607 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5608 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5611 | |
| 5612 | /*** Execution routines ***/ |
| 5613 | |
| 5614 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5615 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5616 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5617 | expand_string_to_string(str) |
| 5618 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5619 | 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] | 5620 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5621 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5622 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5623 | |
| 5624 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5625 | * all variable references within and returns a pointer to |
| 5626 | * a list of expanded strings, possibly with larger number |
| 5627 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5628 | * This new list is allocated as a single malloc block. |
| 5629 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5630 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5631 | * Caller can deallocate entire list by single free(list). */ |
| 5632 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5633 | /* A horde of its helpers come first: */ |
| 5634 | |
| 5635 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5636 | { |
| 5637 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5638 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5639 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5640 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5641 | if (c == '{' || c == '}') { |
| 5642 | /* { -> \{, } -> \} */ |
| 5643 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5644 | /* And now we want to add { or } and continue: |
| 5645 | * o_addchr(o, c); |
| 5646 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5647 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5648 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5649 | } |
| 5650 | #endif |
| 5651 | o_addchr(o, c); |
| 5652 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5653 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5654 | o_addchr(o, '\\'); |
| 5655 | if (len) { |
| 5656 | len--; |
| 5657 | o_addchr(o, '\\'); |
| 5658 | o_addchr(o, *str++); |
| 5659 | } |
| 5660 | } |
| 5661 | } |
| 5662 | } |
| 5663 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5664 | /* Store given string, finalizing the word and starting new one whenever |
| 5665 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5666 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5667 | * Return in *ended_with_ifs: |
| 5668 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5669 | */ |
| 5670 | static int expand_on_ifs(int *ended_with_ifs, o_string *output, int n, const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5671 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5672 | int last_is_ifs = 0; |
| 5673 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5674 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5675 | int word_len; |
| 5676 | |
| 5677 | if (!*str) /* EOL - do not finalize word */ |
| 5678 | break; |
| 5679 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5680 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5681 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5682 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5683 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5684 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5685 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5686 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5687 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5688 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5689 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5690 | /*/ Why can't we do it easier? */ |
| 5691 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5692 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5693 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5694 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5695 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5696 | if (!*str) /* EOL - do not finalize word */ |
| 5697 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5698 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5699 | |
| 5700 | /* We know str here points to at least one IFS char */ |
| 5701 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5702 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5703 | if (!*str) /* EOL - do not finalize word */ |
| 5704 | break; |
| 5705 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5706 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 5707 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 5708 | ) { |
| 5709 | /* This is a non-whitespace $IFS char */ |
| 5710 | /* Skip it and IFS whitespace chars, start new word */ |
| 5711 | str++; |
| 5712 | str += strspn(str, G.ifs_whitespace); |
| 5713 | goto new_word; |
| 5714 | } |
| 5715 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5716 | /* Start new word... but not always! */ |
| 5717 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5718 | if (output->has_quoted_part |
| 5719 | /* Case "v=' a'; echo $v": |
| 5720 | * here nothing precedes the space in $v expansion, |
| 5721 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5722 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5723 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5724 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5725 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5726 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5727 | o_addchr(output, '\0'); |
| 5728 | debug_print_list("expand_on_ifs", output, n); |
| 5729 | n = o_save_ptr(output, n); |
| 5730 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5731 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5732 | |
| 5733 | if (ended_with_ifs) |
| 5734 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5735 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5736 | return n; |
| 5737 | } |
| 5738 | |
| 5739 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5740 | * they are in double quotes, with the exception that they are not :). |
| 5741 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5742 | * |
| 5743 | * Returns malloced string. |
| 5744 | * As an optimization, we return NULL if expansion is not needed. |
| 5745 | */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5746 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5747 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5748 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5749 | encode_then_expand_string(str) |
| 5750 | #endif |
| 5751 | static char *encode_then_expand_string(const char *str, int process_bkslash, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5752 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5753 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 5754 | enum { do_unbackslash = 1 }; |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5755 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5756 | char *exp_str; |
| 5757 | struct in_str input; |
| 5758 | o_string dest = NULL_O_STRING; |
| 5759 | |
| 5760 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5761 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5762 | #if ENABLE_HUSH_TICK |
| 5763 | && !strchr(str, '`') |
| 5764 | #endif |
| 5765 | ) { |
| 5766 | return NULL; |
| 5767 | } |
| 5768 | |
| 5769 | /* We need to expand. Example: |
| 5770 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5771 | */ |
| 5772 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5773 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5774 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5775 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5776 | exp_str = expand_string_to_string(dest.data, |
| 5777 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 5778 | do_unbackslash |
| 5779 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5780 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5781 | o_free_unsafe(&dest); |
| 5782 | return exp_str; |
| 5783 | } |
| 5784 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5785 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5786 | 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] | 5787 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5788 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5789 | arith_t res; |
| 5790 | char *exp_str; |
| 5791 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5792 | math_state.lookupvar = get_local_var_value; |
| 5793 | math_state.setvar = set_local_var_from_halves; |
| 5794 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5795 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5796 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5797 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5798 | if (errmsg_p) |
| 5799 | *errmsg_p = math_state.errmsg; |
| 5800 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5801 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5802 | return res; |
| 5803 | } |
| 5804 | #endif |
| 5805 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5806 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5807 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5808 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5809 | { |
| 5810 | while (1) { |
| 5811 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5812 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5813 | if (end) { |
| 5814 | *size = end - val; |
| 5815 | return val; |
| 5816 | } |
| 5817 | if (*val == '\0') |
| 5818 | return NULL; |
| 5819 | /* Optimization: if "*pat" did not match the start of "string", |
| 5820 | * we know that "tring", "ring" etc will not match too: |
| 5821 | */ |
| 5822 | if (pattern[0] == '*') |
| 5823 | return NULL; |
| 5824 | val++; |
| 5825 | } |
| 5826 | } |
| 5827 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5828 | { |
| 5829 | char *result = NULL; |
| 5830 | unsigned res_len = 0; |
| 5831 | unsigned repl_len = strlen(repl); |
| 5832 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5833 | /* Null pattern never matches, including if "var" is empty */ |
| 5834 | if (!pattern[0]) |
| 5835 | return result; /* NULL, no replaces happened */ |
| 5836 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5837 | while (1) { |
| 5838 | int size; |
| 5839 | char *s = strstr_pattern(val, pattern, &size); |
| 5840 | if (!s) |
| 5841 | break; |
| 5842 | |
| 5843 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5844 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5845 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5846 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5847 | |
| 5848 | val = s + size; |
| 5849 | if (exp_op == '/') |
| 5850 | break; |
| 5851 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5852 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5853 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5854 | strcpy(result + res_len, val); |
| 5855 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5856 | } |
| 5857 | debug_printf_varexp("result:'%s'\n", result); |
| 5858 | return result; |
| 5859 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5860 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5861 | |
| 5862 | /* Helper: |
| 5863 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5864 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5865 | static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5866 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5867 | const char *val; |
| 5868 | char *to_be_freed; |
| 5869 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5870 | char *var; |
| 5871 | char first_char; |
| 5872 | char exp_op; |
| 5873 | char exp_save = exp_save; /* for compiler */ |
| 5874 | char *exp_saveptr; /* points to expansion operator */ |
| 5875 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5876 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5877 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5878 | val = NULL; |
| 5879 | to_be_freed = NULL; |
| 5880 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5881 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5882 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5883 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5884 | arg0 = arg[0]; |
| 5885 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5886 | exp_op = 0; |
| 5887 | |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5888 | if (first_char == '#' && arg[1] /* ${#...} but not ${#} */ |
| 5889 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 5890 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 5891 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5892 | ) { |
| 5893 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5894 | var++; |
| 5895 | exp_op = 'L'; |
| 5896 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5897 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5898 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5899 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5900 | ) { |
| 5901 | /* ${?:0}, ${#[:]%0} etc */ |
| 5902 | exp_saveptr = var + 1; |
| 5903 | } else { |
| 5904 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5905 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5906 | } |
| 5907 | exp_op = exp_save = *exp_saveptr; |
| 5908 | if (exp_op) { |
| 5909 | exp_word = exp_saveptr + 1; |
| 5910 | if (exp_op == ':') { |
| 5911 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5912 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5913 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5914 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5915 | ) { |
| 5916 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5917 | exp_op = ':'; |
| 5918 | exp_word--; |
| 5919 | } |
| 5920 | } |
| 5921 | *exp_saveptr = '\0'; |
| 5922 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5923 | } |
| 5924 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5925 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5926 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5927 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5928 | int n = xatoi_positive(var); |
| 5929 | if (n < G.global_argc) |
| 5930 | val = G.global_argv[n]; |
| 5931 | /* else val remains NULL: $N with too big N */ |
| 5932 | } else { |
| 5933 | switch (var[0]) { |
| 5934 | case '$': /* pid */ |
| 5935 | val = utoa(G.root_pid); |
| 5936 | break; |
| 5937 | case '!': /* bg pid */ |
| 5938 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5939 | break; |
| 5940 | case '?': /* exitcode */ |
| 5941 | val = utoa(G.last_exitcode); |
| 5942 | break; |
| 5943 | case '#': /* argc */ |
| 5944 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5945 | break; |
| 5946 | default: |
| 5947 | val = get_local_var_value(var); |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | /* Handle any expansions */ |
| 5952 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5953 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5954 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5955 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5956 | debug_printf_expand("%s\n", val); |
| 5957 | } else if (exp_op) { |
| 5958 | if (exp_op == '%' || exp_op == '#') { |
| 5959 | /* Standard-mandated substring removal ops: |
| 5960 | * ${parameter%word} - remove smallest suffix pattern |
| 5961 | * ${parameter%%word} - remove largest suffix pattern |
| 5962 | * ${parameter#word} - remove smallest prefix pattern |
| 5963 | * ${parameter##word} - remove largest prefix pattern |
| 5964 | * |
| 5965 | * Word is expanded to produce a glob pattern. |
| 5966 | * Then var's value is matched to it and matching part removed. |
| 5967 | */ |
| 5968 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5969 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5970 | char *exp_exp_word; |
| 5971 | char *loc; |
| 5972 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5973 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5974 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5975 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 5976 | /* |
| 5977 | * process_bkslash:1 unbackslash:1 breaks this: |
| 5978 | * a='a\\'; echo ${a%\\\\} # correct output is: a |
| 5979 | * process_bkslash:1 unbackslash:0 breaks this: |
| 5980 | * a='a}'; echo ${a%\}} # correct output is: a |
| 5981 | */ |
| 5982 | exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5983 | if (exp_exp_word) |
| 5984 | exp_word = exp_exp_word; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5985 | debug_printf_expand("expand: exp_exp_word:'%s'\n", exp_word); |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5986 | /* HACK ALERT. We depend here on the fact that |
| 5987 | * G.global_argv and results of utoa and get_local_var_value |
| 5988 | * are actually in writable memory: |
| 5989 | * scan_and_match momentarily stores NULs there. */ |
| 5990 | t = (char*)val; |
| 5991 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5992 | 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] | 5993 | free(exp_exp_word); |
| 5994 | if (loc) { /* match was found */ |
| 5995 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5996 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5997 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5998 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5999 | } |
| 6000 | } |
| 6001 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6002 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6003 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6004 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6005 | * Note that in encoded form it has TWO parts: |
| 6006 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6007 | * and if // is used, it is encoded as \: |
| 6008 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6009 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6010 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6011 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6012 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6013 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6014 | * >az >bz |
| 6015 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6016 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6017 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6018 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6019 | * (note that a*z _pattern_ is never globbed!) |
| 6020 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6021 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6022 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6023 | if (!pattern) |
| 6024 | pattern = xstrdup(exp_word); |
| 6025 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6026 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6027 | exp_word = p; |
| 6028 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6029 | *p = '\0'; |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6030 | repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6031 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6032 | /* HACK ALERT. We depend here on the fact that |
| 6033 | * G.global_argv and results of utoa and get_local_var_value |
| 6034 | * are actually in writable memory: |
| 6035 | * replace_pattern momentarily stores NULs there. */ |
| 6036 | t = (char*)val; |
| 6037 | to_be_freed = replace_pattern(t, |
| 6038 | pattern, |
| 6039 | (repl ? repl : exp_word), |
| 6040 | exp_op); |
| 6041 | if (to_be_freed) /* at least one replace happened */ |
| 6042 | val = to_be_freed; |
| 6043 | free(pattern); |
| 6044 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6045 | } else { |
| 6046 | /* Empty variable always gives nothing */ |
| 6047 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6048 | /* Just skip "replace" part */ |
| 6049 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6050 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6051 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6052 | } |
| 6053 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6054 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6055 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6056 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6057 | /* It's ${var:N[:M]} bashism. |
| 6058 | * Note that in encoded form it has TWO parts: |
| 6059 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6060 | */ |
| 6061 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6062 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6063 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6064 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6065 | if (errmsg) |
| 6066 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6067 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6068 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6069 | exp_word = p; |
| 6070 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6071 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6072 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6073 | if (errmsg) |
| 6074 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6075 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6076 | if (beg < 0) { |
| 6077 | /* negative beg counts from the end */ |
| 6078 | beg = (arith_t)strlen(val) + beg; |
| 6079 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6080 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6081 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6082 | debug_printf_varexp("from val:'%s'\n", val); |
| 6083 | if (len < 0) { |
| 6084 | /* in bash, len=-n means strlen()-n */ |
| 6085 | len = (arith_t)strlen(val) - beg + len; |
| 6086 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6087 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6088 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6089 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6090 | arith_err: |
| 6091 | val = NULL; |
| 6092 | } else { |
| 6093 | /* Paranoia. What if user entered 9999999999999 |
| 6094 | * which fits in arith_t but not int? */ |
| 6095 | if (len >= INT_MAX) |
| 6096 | len = INT_MAX; |
| 6097 | val = to_be_freed = xstrndup(val + beg, len); |
| 6098 | } |
| 6099 | debug_printf_varexp("val:'%s'\n", val); |
| 6100 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6101 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6102 | val = NULL; |
| 6103 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6104 | } else { /* one of "-=+?" */ |
| 6105 | /* Standard-mandated substitution ops: |
| 6106 | * ${var?word} - indicate error if unset |
| 6107 | * If var is unset, word (or a message indicating it is unset |
| 6108 | * if word is null) is written to standard error |
| 6109 | * and the shell exits with a non-zero exit status. |
| 6110 | * Otherwise, the value of var is substituted. |
| 6111 | * ${var-word} - use default value |
| 6112 | * If var is unset, word is substituted. |
| 6113 | * ${var=word} - assign and use default value |
| 6114 | * If var is unset, word is assigned to var. |
| 6115 | * In all cases, final value of var is substituted. |
| 6116 | * ${var+word} - use alternative value |
| 6117 | * If var is unset, null is substituted. |
| 6118 | * Otherwise, word is substituted. |
| 6119 | * |
| 6120 | * Word is subjected to tilde expansion, parameter expansion, |
| 6121 | * command substitution, and arithmetic expansion. |
| 6122 | * If word is not needed, it is not expanded. |
| 6123 | * |
| 6124 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6125 | * but also treat null var as if it is unset. |
| 6126 | */ |
| 6127 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6128 | if (exp_op == '+') |
| 6129 | use_word = !use_word; |
| 6130 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6131 | (exp_save == ':') ? "true" : "false", use_word); |
| 6132 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6133 | to_be_freed = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6134 | if (to_be_freed) |
| 6135 | exp_word = to_be_freed; |
| 6136 | if (exp_op == '?') { |
| 6137 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6138 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6139 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 6140 | exp_word[0] |
| 6141 | ? exp_word |
| 6142 | : "parameter null or not set" |
| 6143 | /* ash has more specific messages, a-la: */ |
| 6144 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6145 | ); |
| 6146 | //TODO: how interactive bash aborts expansion mid-command? |
| 6147 | } else { |
| 6148 | val = exp_word; |
| 6149 | } |
| 6150 | |
| 6151 | if (exp_op == '=') { |
| 6152 | /* ${var=[word]} or ${var:=[word]} */ |
| 6153 | if (isdigit(var[0]) || var[0] == '#') { |
| 6154 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6155 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6156 | val = NULL; |
| 6157 | } else { |
| 6158 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 6159 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6160 | } |
| 6161 | } |
| 6162 | } |
| 6163 | } /* one of "-=+?" */ |
| 6164 | |
| 6165 | *exp_saveptr = exp_save; |
| 6166 | } /* if (exp_op) */ |
| 6167 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6168 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6169 | |
| 6170 | *pp = p; |
| 6171 | *to_be_freed_pp = to_be_freed; |
| 6172 | return val; |
| 6173 | } |
| 6174 | |
| 6175 | /* Expand all variable references in given string, adding words to list[] |
| 6176 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6177 | * to be filled). This routine is extremely tricky: has to deal with |
| 6178 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6179 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6180 | 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] | 6181 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6182 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6183 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6184 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6185 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6186 | int ended_in_ifs = 0; /* did last unquoted expansion end with IFS chars? */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6187 | char *p; |
| 6188 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6189 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6190 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6191 | debug_print_list("expand_vars_to_list", output, n); |
| 6192 | n = o_save_ptr(output, n); |
| 6193 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6194 | |
| 6195 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6196 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6197 | char *to_be_freed = NULL; |
| 6198 | const char *val = NULL; |
| 6199 | #if ENABLE_HUSH_TICK |
| 6200 | o_string subst_result = NULL_O_STRING; |
| 6201 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6202 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6203 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6204 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6205 | |
| 6206 | if (ended_in_ifs) { |
| 6207 | o_addchr(output, '\0'); |
| 6208 | n = o_save_ptr(output, n); |
| 6209 | ended_in_ifs = 0; |
| 6210 | } |
| 6211 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6212 | o_addblock(output, arg, p - arg); |
| 6213 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6214 | arg = ++p; |
| 6215 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6216 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6217 | /* Fetch special var name (if it is indeed one of them) |
| 6218 | * and quote bit, force the bit on if singleword expansion - |
| 6219 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6220 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6221 | |
| 6222 | /* Is this variable quoted and thus expansion can't be null? |
| 6223 | * "$@" is special. Even if quoted, it can still |
| 6224 | * expand to nothing (not even an empty string), |
| 6225 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6226 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6227 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6228 | |
| 6229 | switch (first_ch & 0x7f) { |
| 6230 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6231 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6232 | case '@': { |
| 6233 | int i; |
| 6234 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6235 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6236 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6237 | 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] | 6238 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6239 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6240 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6241 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6242 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6243 | /* this argv[] is not empty and not last: |
| 6244 | * put terminating NUL, start new word */ |
| 6245 | o_addchr(output, '\0'); |
| 6246 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6247 | n = o_save_ptr(output, n); |
| 6248 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6249 | } |
| 6250 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6251 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6252 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6253 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6254 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6255 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6256 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6257 | while (1) { |
| 6258 | o_addQstr(output, G.global_argv[i]); |
| 6259 | if (++i >= G.global_argc) |
| 6260 | break; |
| 6261 | o_addchr(output, '\0'); |
| 6262 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6263 | n = o_save_ptr(output, n); |
| 6264 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6265 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6266 | while (1) { |
| 6267 | o_addQstr(output, G.global_argv[i]); |
| 6268 | if (!G.global_argv[++i]) |
| 6269 | break; |
| 6270 | if (G.ifs[0]) |
| 6271 | o_addchr(output, G.ifs[0]); |
| 6272 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6273 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6274 | } |
| 6275 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6276 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6277 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 6278 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6279 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6280 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6281 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6282 | break; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6283 | case SPECIAL_VAR_QUOTED_SVS: |
| 6284 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
| 6285 | arg++; |
| 6286 | val = SPECIAL_VAR_SYMBOL_STR; |
| 6287 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6288 | #if ENABLE_HUSH_TICK |
| 6289 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6290 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6291 | arg++; |
| 6292 | /* Can't just stuff it into output o_string, |
| 6293 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6294 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6295 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6296 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6297 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6298 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 6299 | val = subst_result.data; |
| 6300 | goto store_val; |
| 6301 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6302 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6303 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 6304 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6305 | |
| 6306 | arg++; /* skip '+' */ |
| 6307 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6308 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6309 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6310 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6311 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6312 | val = arith_buf; |
| 6313 | break; |
| 6314 | } |
| 6315 | #endif |
| 6316 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6317 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6318 | IF_HUSH_TICK(store_val:) |
| 6319 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6320 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6321 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6322 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6323 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6324 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6325 | } |
| 6326 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6327 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6328 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6329 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6330 | } |
| 6331 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6332 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6333 | |
| 6334 | if (val && val[0]) { |
| 6335 | o_addQstr(output, val); |
| 6336 | } |
| 6337 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6338 | |
| 6339 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6340 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6341 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6342 | *p = SPECIAL_VAR_SYMBOL; |
| 6343 | |
| 6344 | #if ENABLE_HUSH_TICK |
| 6345 | o_free(&subst_result); |
| 6346 | #endif |
| 6347 | arg = ++p; |
| 6348 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6349 | |
| 6350 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6351 | if (ended_in_ifs) { |
| 6352 | o_addchr(output, '\0'); |
| 6353 | n = o_save_ptr(output, n); |
| 6354 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6355 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6356 | /* this part is literal, and it was already pre-quoted |
| 6357 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6358 | o_addstr_with_NUL(output, arg); |
| 6359 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6360 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6361 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6362 | ) { |
| 6363 | n--; |
| 6364 | /* allow to reuse list[n] later without re-growth */ |
| 6365 | output->has_empty_slot = 1; |
| 6366 | } else { |
| 6367 | o_addchr(output, '\0'); |
| 6368 | } |
| 6369 | |
| 6370 | return n; |
| 6371 | } |
| 6372 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6373 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6374 | { |
| 6375 | int n; |
| 6376 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6377 | o_string output = NULL_O_STRING; |
| 6378 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6379 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6380 | |
| 6381 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6382 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6383 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6384 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6385 | } |
| 6386 | debug_print_list("expand_variables", &output, n); |
| 6387 | |
| 6388 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6389 | list = o_finalize_list(&output, n); |
| 6390 | debug_print_strings("expand_variables[1]", list); |
| 6391 | return list; |
| 6392 | } |
| 6393 | |
| 6394 | static char **expand_strvec_to_strvec(char **argv) |
| 6395 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6396 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6397 | } |
| 6398 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6399 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6400 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6401 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6402 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6403 | } |
| 6404 | #endif |
| 6405 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6406 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6407 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6408 | * |
| 6409 | * NB: should NOT do globbing! |
| 6410 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6411 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6412 | 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] | 6413 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6414 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6415 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6416 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6417 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6418 | char *argv[2], **list; |
| 6419 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6420 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6421 | /* This is generally an optimization, but it also |
| 6422 | * handles "", which otherwise trips over !list[0] check below. |
| 6423 | * (is this ever happens that we actually get str="" here?) |
| 6424 | */ |
| 6425 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6426 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6427 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6428 | return xstrdup(str); |
| 6429 | } |
| 6430 | |
| 6431 | argv[0] = (char*)str; |
| 6432 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6433 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6434 | if (HUSH_DEBUG) |
| 6435 | if (!list[0] || list[1]) |
| 6436 | bb_error_msg_and_die("BUG in varexp2"); |
| 6437 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6438 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6439 | if (do_unbackslash) |
| 6440 | unbackslash((char*)list); |
| 6441 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6442 | return (char*)list; |
| 6443 | } |
| 6444 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6445 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6446 | static char* expand_strvec_to_string(char **argv) |
| 6447 | { |
| 6448 | char **list; |
| 6449 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6450 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6451 | /* Convert all NULs to spaces */ |
| 6452 | if (list[0]) { |
| 6453 | int n = 1; |
| 6454 | while (list[n]) { |
| 6455 | if (HUSH_DEBUG) |
| 6456 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6457 | bb_error_msg_and_die("BUG in varexp3"); |
| 6458 | /* bash uses ' ' regardless of $IFS contents */ |
| 6459 | list[n][-1] = ' '; |
| 6460 | n++; |
| 6461 | } |
| 6462 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6463 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6464 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6465 | return (char*)list; |
| 6466 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6467 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6468 | |
| 6469 | static char **expand_assignments(char **argv, int count) |
| 6470 | { |
| 6471 | int i; |
| 6472 | char **p; |
| 6473 | |
| 6474 | G.expanded_assignments = p = NULL; |
| 6475 | /* Expand assignments into one string each */ |
| 6476 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6477 | p = add_string_to_strings(p, |
| 6478 | expand_string_to_string(argv[i], |
| 6479 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6480 | /*unbackslash:*/ 1 |
| 6481 | ) |
| 6482 | ); |
| 6483 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6484 | } |
| 6485 | G.expanded_assignments = NULL; |
| 6486 | return p; |
| 6487 | } |
| 6488 | |
| 6489 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6490 | static void switch_off_special_sigs(unsigned mask) |
| 6491 | { |
| 6492 | unsigned sig = 0; |
| 6493 | while ((mask >>= 1) != 0) { |
| 6494 | sig++; |
| 6495 | if (!(mask & 1)) |
| 6496 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6497 | #if ENABLE_HUSH_TRAP |
| 6498 | if (G_traps) { |
| 6499 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6500 | /* trap is '', has to remain SIG_IGN */ |
| 6501 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6502 | free(G_traps[sig]); |
| 6503 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6504 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6505 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6506 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6507 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6508 | } |
| 6509 | } |
| 6510 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6511 | #if BB_MMU |
| 6512 | /* never called */ |
| 6513 | void re_execute_shell(char ***to_free, const char *s, |
| 6514 | char *g_argv0, char **g_argv, |
| 6515 | char **builtin_argv) NORETURN; |
| 6516 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6517 | static void reset_traps_to_defaults(void) |
| 6518 | { |
| 6519 | /* This function is always called in a child shell |
| 6520 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6521 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6522 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6523 | unsigned mask; |
| 6524 | |
| 6525 | /* Child shells are not interactive. |
| 6526 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6527 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6528 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6529 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6530 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6531 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6532 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6533 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6534 | /* Switch off special sigs */ |
| 6535 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6536 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6537 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6538 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6539 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6540 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6541 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6542 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6543 | # if ENABLE_HUSH_TRAP |
| 6544 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6545 | return; |
| 6546 | |
| 6547 | /* Reset all sigs to default except ones with empty traps */ |
| 6548 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6549 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6550 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6551 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6552 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6553 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6554 | free(G_traps[sig]); |
| 6555 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6556 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6557 | if (sig == 0) |
| 6558 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6559 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6560 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6561 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6562 | } |
| 6563 | |
| 6564 | #else /* !BB_MMU */ |
| 6565 | |
| 6566 | static void re_execute_shell(char ***to_free, const char *s, |
| 6567 | char *g_argv0, char **g_argv, |
| 6568 | char **builtin_argv) NORETURN; |
| 6569 | static void re_execute_shell(char ***to_free, const char *s, |
| 6570 | char *g_argv0, char **g_argv, |
| 6571 | char **builtin_argv) |
| 6572 | { |
| 6573 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6574 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6575 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6576 | char *heredoc_argv[4]; |
| 6577 | struct variable *cur; |
| 6578 | # if ENABLE_HUSH_FUNCTIONS |
| 6579 | struct function *funcp; |
| 6580 | # endif |
| 6581 | char **argv, **pp; |
| 6582 | unsigned cnt; |
| 6583 | unsigned long long empty_trap_mask; |
| 6584 | |
| 6585 | if (!g_argv0) { /* heredoc */ |
| 6586 | argv = heredoc_argv; |
| 6587 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6588 | argv[1] = (char *) "-<"; |
| 6589 | argv[2] = (char *) s; |
| 6590 | argv[3] = NULL; |
| 6591 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6592 | goto do_exec; |
| 6593 | } |
| 6594 | |
| 6595 | cnt = 0; |
| 6596 | pp = builtin_argv; |
| 6597 | if (pp) while (*pp++) |
| 6598 | cnt++; |
| 6599 | |
| 6600 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6601 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6602 | int sig; |
| 6603 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6604 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6605 | empty_trap_mask |= 1LL << sig; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6610 | , (unsigned) G.root_pid |
| 6611 | , (unsigned) G.root_ppid |
| 6612 | , (unsigned) G.last_bg_pid |
| 6613 | , (unsigned) G.last_exitcode |
| 6614 | , cnt |
| 6615 | , empty_trap_mask |
| 6616 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6617 | ); |
| 6618 | # undef NOMMU_HACK_FMT |
| 6619 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6620 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6621 | */ |
| 6622 | cnt += 6; |
| 6623 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6624 | if (!cur->flg_export || cur->flg_read_only) |
| 6625 | cnt += 2; |
| 6626 | } |
| 6627 | # if ENABLE_HUSH_FUNCTIONS |
| 6628 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6629 | cnt += 3; |
| 6630 | # endif |
| 6631 | pp = g_argv; |
| 6632 | while (*pp++) |
| 6633 | cnt++; |
| 6634 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6635 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6636 | *pp++ = param_buf; |
| 6637 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6638 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6639 | continue; |
| 6640 | if (cur->flg_read_only) { |
| 6641 | *pp++ = (char *) "-R"; |
| 6642 | *pp++ = cur->varstr; |
| 6643 | } else if (!cur->flg_export) { |
| 6644 | *pp++ = (char *) "-V"; |
| 6645 | *pp++ = cur->varstr; |
| 6646 | } |
| 6647 | } |
| 6648 | # if ENABLE_HUSH_FUNCTIONS |
| 6649 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6650 | *pp++ = (char *) "-F"; |
| 6651 | *pp++ = funcp->name; |
| 6652 | *pp++ = funcp->body_as_string; |
| 6653 | } |
| 6654 | # endif |
| 6655 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6656 | * |
| 6657 | * However, POSIX says that subshells reset signals with traps |
| 6658 | * to SIG_DFL. |
| 6659 | * I tested bash-3.2 and it not only does that with true subshells |
| 6660 | * of the form ( list ), but with any forked children shells. |
| 6661 | * I set trap "echo W" WINCH; and then tried: |
| 6662 | * |
| 6663 | * { echo 1; sleep 20; echo 2; } & |
| 6664 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6665 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6666 | * |
| 6667 | * In all these cases sending SIGWINCH to the child shell |
| 6668 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6669 | * _inside_ group (just before echo 1), it works. |
| 6670 | * |
| 6671 | * 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] | 6672 | */ |
| 6673 | *pp++ = (char *) "-c"; |
| 6674 | *pp++ = (char *) s; |
| 6675 | if (builtin_argv) { |
| 6676 | while (*++builtin_argv) |
| 6677 | *pp++ = *builtin_argv; |
| 6678 | *pp++ = (char *) ""; |
| 6679 | } |
| 6680 | *pp++ = g_argv0; |
| 6681 | while (*g_argv) |
| 6682 | *pp++ = *g_argv++; |
| 6683 | /* *pp = NULL; - is already there */ |
| 6684 | pp = environ; |
| 6685 | |
| 6686 | do_exec: |
| 6687 | 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] | 6688 | /* Don't propagate SIG_IGN to the child */ |
| 6689 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6690 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6691 | execve(bb_busybox_exec_path, argv, pp); |
| 6692 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6693 | if (argv[0][0] == '/') |
| 6694 | execve(argv[0], argv, pp); |
| 6695 | xfunc_error_retval = 127; |
| 6696 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6697 | } |
| 6698 | #endif /* !BB_MMU */ |
| 6699 | |
| 6700 | |
| 6701 | static int run_and_free_list(struct pipe *pi); |
| 6702 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6703 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6704 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6705 | * end_trigger controls how often we stop parsing |
| 6706 | * NUL: parse all, execute, return |
| 6707 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6708 | */ |
| 6709 | 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] | 6710 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6711 | /* Why we need empty flag? |
| 6712 | * An obscure corner case "false; ``; echo $?": |
| 6713 | * empty command in `` should still set $? to 0. |
| 6714 | * But we can't just set $? to 0 at the start, |
| 6715 | * this breaks "false; echo `echo $?`" case. |
| 6716 | */ |
| 6717 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6718 | while (1) { |
| 6719 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6720 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6721 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 6722 | if (end_trigger == ';') { |
| 6723 | G.promptmode = 0; /* PS1 */ |
| 6724 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 6725 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6726 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6727 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6728 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6729 | /* If we are in "big" script |
| 6730 | * (not in `cmd` or something similar)... |
| 6731 | */ |
| 6732 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6733 | /* Discard cached input (rest of line) */ |
| 6734 | int ch = inp->last_char; |
| 6735 | while (ch != EOF && ch != '\n') { |
| 6736 | //bb_error_msg("Discarded:'%c'", ch); |
| 6737 | ch = i_getch(inp); |
| 6738 | } |
| 6739 | /* Force prompt */ |
| 6740 | inp->p = NULL; |
| 6741 | /* This stream isn't empty */ |
| 6742 | empty = 0; |
| 6743 | continue; |
| 6744 | } |
| 6745 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6746 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6747 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6748 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6749 | debug_print_tree(pipe_list, 0); |
| 6750 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6751 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6752 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6753 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6754 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6755 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6758 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6759 | { |
| 6760 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6761 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 6762 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6763 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6764 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6765 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6768 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6769 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6770 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6771 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 6772 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6773 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 6774 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6775 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6776 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6779 | #if ENABLE_HUSH_TICK |
| 6780 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6781 | { |
| 6782 | pid_t pid; |
| 6783 | int channel[2]; |
| 6784 | # if !BB_MMU |
| 6785 | char **to_free = NULL; |
| 6786 | # endif |
| 6787 | |
| 6788 | xpipe(channel); |
| 6789 | pid = BB_MMU ? xfork() : xvfork(); |
| 6790 | if (pid == 0) { /* child */ |
| 6791 | disable_restore_tty_pgrp_on_exit(); |
| 6792 | /* Process substitution is not considered to be usual |
| 6793 | * 'command execution'. |
| 6794 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6795 | */ |
| 6796 | bb_signals(0 |
| 6797 | + (1 << SIGTSTP) |
| 6798 | + (1 << SIGTTIN) |
| 6799 | + (1 << SIGTTOU) |
| 6800 | , SIG_IGN); |
| 6801 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6802 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6803 | xmove_fd(channel[1], 1); |
| 6804 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6805 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6806 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6807 | /* Awful hack for `trap` or $(trap). |
| 6808 | * |
| 6809 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6810 | * contains an example where "trap" is executed in a subshell: |
| 6811 | * |
| 6812 | * save_traps=$(trap) |
| 6813 | * ... |
| 6814 | * eval "$save_traps" |
| 6815 | * |
| 6816 | * Standard does not say that "trap" in subshell shall print |
| 6817 | * parent shell's traps. It only says that its output |
| 6818 | * must have suitable form, but then, in the above example |
| 6819 | * (which is not supposed to be normative), it implies that. |
| 6820 | * |
| 6821 | * bash (and probably other shell) does implement it |
| 6822 | * (traps are reset to defaults, but "trap" still shows them), |
| 6823 | * but as a result, "trap" logic is hopelessly messed up: |
| 6824 | * |
| 6825 | * # trap |
| 6826 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6827 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6828 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6829 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6830 | * trap -- 'echo Ho' SIGWINCH |
| 6831 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6832 | * trap -- 'echo Ho' SIGWINCH |
| 6833 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6834 | * trap -- 'echo Ho' SIGWINCH |
| 6835 | * |
| 6836 | * The rules when to forget and when to not forget traps |
| 6837 | * get really complex and nonsensical. |
| 6838 | * |
| 6839 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6840 | */ |
| 6841 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6842 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6843 | && skip_whitespace(s + 4)[0] == '\0' |
| 6844 | ) { |
| 6845 | static const char *const argv[] = { NULL, NULL }; |
| 6846 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6847 | fflush_all(); /* important */ |
| 6848 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6849 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6850 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6851 | # if BB_MMU |
| 6852 | reset_traps_to_defaults(); |
| 6853 | parse_and_run_string(s); |
| 6854 | _exit(G.last_exitcode); |
| 6855 | # else |
| 6856 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6857 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6858 | * huge=`cat BIG` # was blocking here forever |
| 6859 | * echo OK |
| 6860 | */ |
| 6861 | re_execute_shell(&to_free, |
| 6862 | s, |
| 6863 | G.global_argv[0], |
| 6864 | G.global_argv + 1, |
| 6865 | NULL); |
| 6866 | # endif |
| 6867 | } |
| 6868 | |
| 6869 | /* parent */ |
| 6870 | *pid_p = pid; |
| 6871 | # if ENABLE_HUSH_FAST |
| 6872 | G.count_SIGCHLD++; |
| 6873 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6874 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6875 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6876 | # endif |
| 6877 | enable_restore_tty_pgrp_on_exit(); |
| 6878 | # if !BB_MMU |
| 6879 | free(to_free); |
| 6880 | # endif |
| 6881 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6882 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | /* Return code is exit status of the process that is run. */ |
| 6886 | static int process_command_subs(o_string *dest, const char *s) |
| 6887 | { |
| 6888 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6889 | pid_t pid; |
| 6890 | int status, ch, eol_cnt; |
| 6891 | |
| 6892 | fp = generate_stream_from_string(s, &pid); |
| 6893 | |
| 6894 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6895 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6896 | while ((ch = getc(fp)) != EOF) { |
| 6897 | if (ch == '\0') |
| 6898 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6899 | if (ch == '\n') { |
| 6900 | eol_cnt++; |
| 6901 | continue; |
| 6902 | } |
| 6903 | while (eol_cnt) { |
| 6904 | o_addchr(dest, '\n'); |
| 6905 | eol_cnt--; |
| 6906 | } |
| 6907 | o_addQchr(dest, ch); |
| 6908 | } |
| 6909 | |
| 6910 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6911 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6912 | /* We need to extract exitcode. Test case |
| 6913 | * "true; echo `sleep 1; false` $?" |
| 6914 | * should print 1 */ |
| 6915 | safe_waitpid(pid, &status, 0); |
| 6916 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6917 | return WEXITSTATUS(status); |
| 6918 | } |
| 6919 | #endif /* ENABLE_HUSH_TICK */ |
| 6920 | |
| 6921 | |
| 6922 | static void setup_heredoc(struct redir_struct *redir) |
| 6923 | { |
| 6924 | struct fd_pair pair; |
| 6925 | pid_t pid; |
| 6926 | int len, written; |
| 6927 | /* the _body_ of heredoc (misleading field name) */ |
| 6928 | const char *heredoc = redir->rd_filename; |
| 6929 | char *expanded; |
| 6930 | #if !BB_MMU |
| 6931 | char **to_free; |
| 6932 | #endif |
| 6933 | |
| 6934 | expanded = NULL; |
| 6935 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6936 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6937 | if (expanded) |
| 6938 | heredoc = expanded; |
| 6939 | } |
| 6940 | len = strlen(heredoc); |
| 6941 | |
| 6942 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6943 | xpiped_pair(pair); |
| 6944 | xmove_fd(pair.rd, redir->rd_fd); |
| 6945 | |
| 6946 | /* Try writing without forking. Newer kernels have |
| 6947 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6948 | ndelay_on(pair.wr); |
| 6949 | while (1) { |
| 6950 | written = write(pair.wr, heredoc, len); |
| 6951 | if (written <= 0) |
| 6952 | break; |
| 6953 | len -= written; |
| 6954 | if (len == 0) { |
| 6955 | close(pair.wr); |
| 6956 | free(expanded); |
| 6957 | return; |
| 6958 | } |
| 6959 | heredoc += written; |
| 6960 | } |
| 6961 | ndelay_off(pair.wr); |
| 6962 | |
| 6963 | /* Okay, pipe buffer was not big enough */ |
| 6964 | /* Note: we must not create a stray child (bastard? :) |
| 6965 | * for the unsuspecting parent process. Child creates a grandchild |
| 6966 | * and exits before parent execs the process which consumes heredoc |
| 6967 | * (that exec happens after we return from this function) */ |
| 6968 | #if !BB_MMU |
| 6969 | to_free = NULL; |
| 6970 | #endif |
| 6971 | pid = xvfork(); |
| 6972 | if (pid == 0) { |
| 6973 | /* child */ |
| 6974 | disable_restore_tty_pgrp_on_exit(); |
| 6975 | pid = BB_MMU ? xfork() : xvfork(); |
| 6976 | if (pid != 0) |
| 6977 | _exit(0); |
| 6978 | /* grandchild */ |
| 6979 | close(redir->rd_fd); /* read side of the pipe */ |
| 6980 | #if BB_MMU |
| 6981 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6982 | _exit(0); |
| 6983 | #else |
| 6984 | /* Delegate blocking writes to another process */ |
| 6985 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6986 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6987 | #endif |
| 6988 | } |
| 6989 | /* parent */ |
| 6990 | #if ENABLE_HUSH_FAST |
| 6991 | G.count_SIGCHLD++; |
| 6992 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6993 | #endif |
| 6994 | enable_restore_tty_pgrp_on_exit(); |
| 6995 | #if !BB_MMU |
| 6996 | free(to_free); |
| 6997 | #endif |
| 6998 | close(pair.wr); |
| 6999 | free(expanded); |
| 7000 | wait(NULL); /* wait till child has died */ |
| 7001 | } |
| 7002 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7003 | struct squirrel { |
| 7004 | int orig_fd; |
| 7005 | int moved_to; |
| 7006 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7007 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7008 | }; |
| 7009 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7010 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7011 | { |
| 7012 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7013 | sq[i].orig_fd = orig; |
| 7014 | sq[i].moved_to = moved; |
| 7015 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7016 | return sq; |
| 7017 | } |
| 7018 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7019 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7020 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7021 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7022 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7023 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7024 | i = 0; |
| 7025 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7026 | /* If we collide with an already moved fd... */ |
| 7027 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7028 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7029 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7030 | if (sq[i].moved_to < 0) /* what? */ |
| 7031 | xfunc_die(); |
| 7032 | return sq; |
| 7033 | } |
| 7034 | if (fd == sq[i].orig_fd) { |
| 7035 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7036 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7037 | return sq; |
| 7038 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7039 | } |
| 7040 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7041 | /* 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] | 7042 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7043 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7044 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7045 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7046 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7047 | } |
| 7048 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7049 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7050 | { |
| 7051 | int i; |
| 7052 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7053 | i = 0; |
| 7054 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7055 | /* If we collide with an already moved fd... */ |
| 7056 | if (fd == sq[i].orig_fd) { |
| 7057 | /* Examples: |
| 7058 | * "echo 3>FILE 3>&- 3>FILE" |
| 7059 | * "echo 3>&- 3>FILE" |
| 7060 | * No need for last redirect to insert |
| 7061 | * another "need to close 3" indicator. |
| 7062 | */ |
| 7063 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7064 | return sq; |
| 7065 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7066 | } |
| 7067 | |
| 7068 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7069 | return append_squirrel(sq, i, fd, -1); |
| 7070 | } |
| 7071 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7072 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7073 | * Move all conflicting internally used fds, |
| 7074 | * and remember them so that we can restore them later. |
| 7075 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7076 | 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] | 7077 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7078 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7079 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7080 | |
| 7081 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7082 | if (fd == G.interactive_fd) { |
| 7083 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7084 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7085 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7086 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7087 | } |
| 7088 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7089 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 7090 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 7091 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7092 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 7093 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7094 | * "fileno(fd) = new_fd" can't be done. |
| 7095 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7096 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7097 | return 0; |
| 7098 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7099 | /* If this one of script's fds? */ |
| 7100 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 7101 | return 1; /* yes. "we closed fd" */ |
| 7102 | |
| 7103 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7104 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7105 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7106 | } |
| 7107 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7108 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7109 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7110 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7111 | int i; |
| 7112 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7113 | if (sq[i].moved_to >= 0) { |
| 7114 | /* We simply die on error */ |
| 7115 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7116 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7117 | } else { |
| 7118 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7119 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7120 | close(sq[i].orig_fd); |
| 7121 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7122 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7123 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7124 | } |
| 7125 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7126 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7127 | |
| 7128 | restore_redirected_FILEs(); |
| 7129 | } |
| 7130 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7131 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7132 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7133 | { |
| 7134 | if (G_interactive_fd) |
| 7135 | close(G_interactive_fd); |
| 7136 | close_all_FILE_list(); |
| 7137 | } |
| 7138 | #endif |
| 7139 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7140 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7141 | { |
| 7142 | int i; |
| 7143 | |
| 7144 | #if ENABLE_HUSH_INTERACTIVE |
| 7145 | if (fd == G.interactive_fd) |
| 7146 | return 1; |
| 7147 | #endif |
| 7148 | /* If this one of script's fds? */ |
| 7149 | if (fd_in_FILEs(fd)) |
| 7150 | return 1; |
| 7151 | |
| 7152 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7153 | if (fd == sq[i].moved_to) |
| 7154 | return 1; |
| 7155 | } |
| 7156 | return 0; |
| 7157 | } |
| 7158 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7159 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7160 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7161 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7162 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7163 | struct redir_struct *redir; |
| 7164 | |
| 7165 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7166 | int newfd; |
| 7167 | int closed; |
| 7168 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7169 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7170 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7171 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7172 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7173 | * of the heredoc */ |
| 7174 | debug_printf_parse("set heredoc '%s'\n", |
| 7175 | redir->rd_filename); |
| 7176 | setup_heredoc(redir); |
| 7177 | continue; |
| 7178 | } |
| 7179 | |
| 7180 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7181 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7182 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7183 | int mode; |
| 7184 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7185 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7186 | /* |
| 7187 | * Examples: |
| 7188 | * "cmd >" (no filename) |
| 7189 | * "cmd > <file" (2nd redirect starts too early) |
| 7190 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7191 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7192 | continue; |
| 7193 | } |
| 7194 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7195 | p = expand_string_to_string(redir->rd_filename, |
| 7196 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7197 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7198 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7199 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7200 | /* Error message from open_or_warn can be lost |
| 7201 | * if stderr has been redirected, but bash |
| 7202 | * and ash both lose it as well |
| 7203 | * (though zsh doesn't!) |
| 7204 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7205 | return 1; |
| 7206 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7207 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7208 | /* open() gave us precisely the fd we wanted. |
| 7209 | * This means that this fd was not busy |
| 7210 | * (not opened to anywhere). |
| 7211 | * Remember to close it on restore: |
| 7212 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7213 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7214 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7215 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7216 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7217 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7218 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7219 | } |
| 7220 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7221 | if (newfd == redir->rd_fd) |
| 7222 | continue; |
| 7223 | |
| 7224 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7225 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7226 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7227 | |
| 7228 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7229 | if (newfd == REDIRFD_CLOSE) { |
| 7230 | /* "N>&-" means "close me" */ |
| 7231 | if (!closed) { |
| 7232 | /* ^^^ optimization: saving may already |
| 7233 | * have closed it. If not... */ |
| 7234 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7235 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7236 | /* Sometimes we do another close on restore, getting EBADF. |
| 7237 | * Consider "echo 3>FILE 3>&-" |
| 7238 | * first redirect remembers "need to close 3", |
| 7239 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7240 | */ |
| 7241 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7242 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7243 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7244 | //errno = EBADF; |
| 7245 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7246 | newfd = -1; /* same effect as code above */ |
| 7247 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7248 | xdup2(newfd, redir->rd_fd); |
| 7249 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7250 | /* "rd_fd > FILE" */ |
| 7251 | close(newfd); |
| 7252 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7253 | } |
| 7254 | } |
| 7255 | return 0; |
| 7256 | } |
| 7257 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7258 | static char *find_in_path(const char *arg) |
| 7259 | { |
| 7260 | char *ret = NULL; |
| 7261 | const char *PATH = get_local_var_value("PATH"); |
| 7262 | |
| 7263 | if (!PATH) |
| 7264 | return NULL; |
| 7265 | |
| 7266 | while (1) { |
| 7267 | const char *end = strchrnul(PATH, ':'); |
| 7268 | int sz = end - PATH; /* must be int! */ |
| 7269 | |
| 7270 | free(ret); |
| 7271 | if (sz != 0) { |
| 7272 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7273 | } else { |
| 7274 | /* We have xxx::yyyy in $PATH, |
| 7275 | * it means "use current dir" */ |
| 7276 | ret = xstrdup(arg); |
| 7277 | } |
| 7278 | if (access(ret, F_OK) == 0) |
| 7279 | break; |
| 7280 | |
| 7281 | if (*end == '\0') { |
| 7282 | free(ret); |
| 7283 | return NULL; |
| 7284 | } |
| 7285 | PATH = end + 1; |
| 7286 | } |
| 7287 | |
| 7288 | return ret; |
| 7289 | } |
| 7290 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7291 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7292 | const struct built_in_command *x, |
| 7293 | const struct built_in_command *end) |
| 7294 | { |
| 7295 | while (x != end) { |
| 7296 | if (strcmp(name, x->b_cmd) != 0) { |
| 7297 | x++; |
| 7298 | continue; |
| 7299 | } |
| 7300 | debug_printf_exec("found builtin '%s'\n", name); |
| 7301 | return x; |
| 7302 | } |
| 7303 | return NULL; |
| 7304 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7305 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7306 | { |
| 7307 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7308 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7309 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7310 | { |
| 7311 | const struct built_in_command *x = find_builtin1(name); |
| 7312 | if (x) |
| 7313 | return x; |
| 7314 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7315 | } |
| 7316 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame^] | 7317 | static void remove_nested_vars(void) |
| 7318 | { |
| 7319 | struct variable *cur; |
| 7320 | struct variable **cur_pp; |
| 7321 | |
| 7322 | cur_pp = &G.top_var; |
| 7323 | while ((cur = *cur_pp) != NULL) { |
| 7324 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7325 | cur_pp = &cur->next; |
| 7326 | continue; |
| 7327 | } |
| 7328 | /* Unexport */ |
| 7329 | if (cur->flg_export) { |
| 7330 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7331 | bb_unsetenv(cur->varstr); |
| 7332 | } |
| 7333 | /* Remove from global list */ |
| 7334 | *cur_pp = cur->next; |
| 7335 | /* Free */ |
| 7336 | if (!cur->max_len) { |
| 7337 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7338 | free(cur->varstr); |
| 7339 | } |
| 7340 | free(cur); |
| 7341 | } |
| 7342 | } |
| 7343 | |
| 7344 | static void enter_var_nest_level(void) |
| 7345 | { |
| 7346 | G.var_nest_level++; |
| 7347 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7348 | |
| 7349 | /* Try: f() { echo -n .; f; }; f |
| 7350 | * struct variable::var_nest_level is uint16_t, |
| 7351 | * thus limiting recursion to < 2^16. |
| 7352 | * In any case, with 8 Mbyte stack SEGV happens |
| 7353 | * not too long after 2^16 recursions anyway. |
| 7354 | */ |
| 7355 | if (G.var_nest_level > 0xff00) |
| 7356 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7357 | } |
| 7358 | |
| 7359 | static void leave_var_nest_level(void) |
| 7360 | { |
| 7361 | G.var_nest_level--; |
| 7362 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7363 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7364 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7365 | |
| 7366 | remove_nested_vars(); |
| 7367 | } |
| 7368 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7369 | #if ENABLE_HUSH_FUNCTIONS |
| 7370 | static struct function **find_function_slot(const char *name) |
| 7371 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7372 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7373 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7374 | |
| 7375 | while ((funcp = *funcpp) != NULL) { |
| 7376 | if (strcmp(name, funcp->name) == 0) { |
| 7377 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7378 | break; |
| 7379 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7380 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7381 | } |
| 7382 | return funcpp; |
| 7383 | } |
| 7384 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7385 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7386 | { |
| 7387 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7388 | return funcp; |
| 7389 | } |
| 7390 | |
| 7391 | /* Note: takes ownership on name ptr */ |
| 7392 | static struct function *new_function(char *name) |
| 7393 | { |
| 7394 | struct function **funcpp = find_function_slot(name); |
| 7395 | struct function *funcp = *funcpp; |
| 7396 | |
| 7397 | if (funcp != NULL) { |
| 7398 | struct command *cmd = funcp->parent_cmd; |
| 7399 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7400 | if (!cmd) { |
| 7401 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7402 | free(funcp->name); |
| 7403 | /* Note: if !funcp->body, do not free body_as_string! |
| 7404 | * This is a special case of "-F name body" function: |
| 7405 | * body_as_string was not malloced! */ |
| 7406 | if (funcp->body) { |
| 7407 | free_pipe_list(funcp->body); |
| 7408 | # if !BB_MMU |
| 7409 | free(funcp->body_as_string); |
| 7410 | # endif |
| 7411 | } |
| 7412 | } else { |
| 7413 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7414 | cmd->argv[0] = funcp->name; |
| 7415 | cmd->group = funcp->body; |
| 7416 | # if !BB_MMU |
| 7417 | cmd->group_as_string = funcp->body_as_string; |
| 7418 | # endif |
| 7419 | } |
| 7420 | } else { |
| 7421 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7422 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7423 | /*funcp->next = NULL;*/ |
| 7424 | } |
| 7425 | |
| 7426 | funcp->name = name; |
| 7427 | return funcp; |
| 7428 | } |
| 7429 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7430 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7431 | static void unset_func(const char *name) |
| 7432 | { |
| 7433 | struct function **funcpp = find_function_slot(name); |
| 7434 | struct function *funcp = *funcpp; |
| 7435 | |
| 7436 | if (funcp != NULL) { |
| 7437 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7438 | *funcpp = funcp->next; |
| 7439 | /* funcp is unlinked now, deleting it. |
| 7440 | * Note: if !funcp->body, the function was created by |
| 7441 | * "-F name body", do not free ->body_as_string |
| 7442 | * and ->name as they were not malloced. */ |
| 7443 | if (funcp->body) { |
| 7444 | free_pipe_list(funcp->body); |
| 7445 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7446 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7447 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7448 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7449 | } |
| 7450 | free(funcp); |
| 7451 | } |
| 7452 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7453 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7454 | |
| 7455 | # if BB_MMU |
| 7456 | #define exec_function(to_free, funcp, argv) \ |
| 7457 | exec_function(funcp, argv) |
| 7458 | # endif |
| 7459 | static void exec_function(char ***to_free, |
| 7460 | const struct function *funcp, |
| 7461 | char **argv) NORETURN; |
| 7462 | static void exec_function(char ***to_free, |
| 7463 | const struct function *funcp, |
| 7464 | char **argv) |
| 7465 | { |
| 7466 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7467 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7468 | |
| 7469 | argv[0] = G.global_argv[0]; |
| 7470 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7471 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7472 | |
| 7473 | // Example when we are here: "cmd | func" |
| 7474 | // func will run with saved-redirect fds open. |
| 7475 | // $ f() { echo /proc/self/fd/*; } |
| 7476 | // $ true | f |
| 7477 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7478 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7479 | // Same in script: |
| 7480 | // $ . ./SCRIPT |
| 7481 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7482 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7483 | // They are CLOEXEC so external programs won't see them, but |
| 7484 | // for "more correctness" we might want to close those extra fds here: |
| 7485 | //? close_saved_fds_and_FILE_fds(); |
| 7486 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7487 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7488 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7489 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7490 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7491 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7492 | /* On MMU, funcp->body is always non-NULL */ |
| 7493 | n = run_list(funcp->body); |
| 7494 | fflush_all(); |
| 7495 | _exit(n); |
| 7496 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7497 | //? close_saved_fds_and_FILE_fds(); |
| 7498 | |
| 7499 | //TODO: check whether "true | func_with_return" works |
| 7500 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7501 | re_execute_shell(to_free, |
| 7502 | funcp->body_as_string, |
| 7503 | G.global_argv[0], |
| 7504 | argv + 1, |
| 7505 | NULL); |
| 7506 | # endif |
| 7507 | } |
| 7508 | |
| 7509 | static int run_function(const struct function *funcp, char **argv) |
| 7510 | { |
| 7511 | int rc; |
| 7512 | save_arg_t sv; |
| 7513 | smallint sv_flg; |
| 7514 | |
| 7515 | save_and_replace_G_args(&sv, argv); |
| 7516 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7517 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7518 | sv_flg = G_flag_return_in_progress; |
| 7519 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7520 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7521 | /* Make "local" variables properly shadow previous ones */ |
| 7522 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7523 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7524 | |
| 7525 | /* On MMU, funcp->body is always non-NULL */ |
| 7526 | # if !BB_MMU |
| 7527 | if (!funcp->body) { |
| 7528 | /* Function defined by -F */ |
| 7529 | parse_and_run_string(funcp->body_as_string); |
| 7530 | rc = G.last_exitcode; |
| 7531 | } else |
| 7532 | # endif |
| 7533 | { |
| 7534 | rc = run_list(funcp->body); |
| 7535 | } |
| 7536 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7537 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7538 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7539 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7540 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7541 | |
| 7542 | restore_G_args(&sv, argv); |
| 7543 | |
| 7544 | return rc; |
| 7545 | } |
| 7546 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7547 | |
| 7548 | |
| 7549 | #if BB_MMU |
| 7550 | #define exec_builtin(to_free, x, argv) \ |
| 7551 | exec_builtin(x, argv) |
| 7552 | #else |
| 7553 | #define exec_builtin(to_free, x, argv) \ |
| 7554 | exec_builtin(to_free, argv) |
| 7555 | #endif |
| 7556 | static void exec_builtin(char ***to_free, |
| 7557 | const struct built_in_command *x, |
| 7558 | char **argv) NORETURN; |
| 7559 | static void exec_builtin(char ***to_free, |
| 7560 | const struct built_in_command *x, |
| 7561 | char **argv) |
| 7562 | { |
| 7563 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7564 | int rcode; |
| 7565 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7566 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7567 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7568 | fflush_all(); |
| 7569 | _exit(rcode); |
| 7570 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7571 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7572 | /* On NOMMU, we must never block! |
| 7573 | * Example: { sleep 99 | read line; } & echo Ok |
| 7574 | */ |
| 7575 | re_execute_shell(to_free, |
| 7576 | argv[0], |
| 7577 | G.global_argv[0], |
| 7578 | G.global_argv + 1, |
| 7579 | argv); |
| 7580 | #endif |
| 7581 | } |
| 7582 | |
| 7583 | |
| 7584 | static void execvp_or_die(char **argv) NORETURN; |
| 7585 | static void execvp_or_die(char **argv) |
| 7586 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7587 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7588 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7589 | /* Don't propagate SIG_IGN to the child */ |
| 7590 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7591 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7592 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7593 | e = 2; |
| 7594 | if (errno == EACCES) e = 126; |
| 7595 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7596 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7597 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | #if ENABLE_HUSH_MODE_X |
| 7601 | static void dump_cmd_in_x_mode(char **argv) |
| 7602 | { |
| 7603 | if (G_x_mode && argv) { |
| 7604 | /* We want to output the line in one write op */ |
| 7605 | char *buf, *p; |
| 7606 | int len; |
| 7607 | int n; |
| 7608 | |
| 7609 | len = 3; |
| 7610 | n = 0; |
| 7611 | while (argv[n]) |
| 7612 | len += strlen(argv[n++]) + 1; |
| 7613 | buf = xmalloc(len); |
| 7614 | buf[0] = '+'; |
| 7615 | p = buf + 1; |
| 7616 | n = 0; |
| 7617 | while (argv[n]) |
| 7618 | p += sprintf(p, " %s", argv[n++]); |
| 7619 | *p++ = '\n'; |
| 7620 | *p = '\0'; |
| 7621 | fputs(buf, stderr); |
| 7622 | free(buf); |
| 7623 | } |
| 7624 | } |
| 7625 | #else |
| 7626 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7627 | #endif |
| 7628 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7629 | #if ENABLE_HUSH_COMMAND |
| 7630 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7631 | { |
| 7632 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7633 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7634 | if (!opt_vV) |
| 7635 | return; |
| 7636 | |
| 7637 | to_free = NULL; |
| 7638 | if (!explanation) { |
| 7639 | char *path = getenv("PATH"); |
| 7640 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7641 | if (!explanation) |
| 7642 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7643 | if (opt_vV != 'V') |
| 7644 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7645 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7646 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7647 | free(to_free); |
| 7648 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7649 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7650 | } |
| 7651 | #else |
| 7652 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7653 | #endif |
| 7654 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7655 | #if BB_MMU |
| 7656 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7657 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7658 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7659 | pseudo_exec(command, argv_expanded) |
| 7660 | #endif |
| 7661 | |
| 7662 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7663 | * Never returns. |
| 7664 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7665 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7666 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7667 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7668 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7669 | char **argv, int assignment_cnt, |
| 7670 | char **argv_expanded) NORETURN; |
| 7671 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7672 | char **argv, int assignment_cnt, |
| 7673 | char **argv_expanded) |
| 7674 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7675 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7676 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7677 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7678 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 7679 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7680 | |
| 7681 | new_env = expand_assignments(argv, assignment_cnt); |
| 7682 | dump_cmd_in_x_mode(new_env); |
| 7683 | |
| 7684 | if (!argv[assignment_cnt]) { |
| 7685 | /* Case when we are here: ... | var=val | ... |
| 7686 | * (note that we do not exit early, i.e., do not optimize out |
| 7687 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7688 | */ |
| 7689 | free_strings(new_env); |
| 7690 | _exit(EXIT_SUCCESS); |
| 7691 | } |
| 7692 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7693 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7694 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7695 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7696 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7697 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7698 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7699 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7700 | set_vars_and_save_old(new_env); |
| 7701 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7702 | |
| 7703 | if (argv_expanded) { |
| 7704 | argv = argv_expanded; |
| 7705 | } else { |
| 7706 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7707 | #if !BB_MMU |
| 7708 | nommu_save->argv = argv; |
| 7709 | #endif |
| 7710 | } |
| 7711 | dump_cmd_in_x_mode(argv); |
| 7712 | |
| 7713 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7714 | if (strchr(argv[0], '/') != NULL) |
| 7715 | goto skip; |
| 7716 | #endif |
| 7717 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7718 | #if ENABLE_HUSH_FUNCTIONS |
| 7719 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7720 | funcp = find_function(argv[0]); |
| 7721 | if (funcp) |
| 7722 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7723 | #endif |
| 7724 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7725 | #if ENABLE_HUSH_COMMAND |
| 7726 | /* "command BAR": run BAR without looking it up among functions |
| 7727 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 7728 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 7729 | */ |
| 7730 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 7731 | char *p; |
| 7732 | |
| 7733 | argv++; |
| 7734 | p = *argv; |
| 7735 | if (p[0] != '-' || !p[1]) |
| 7736 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 7737 | |
| 7738 | for (;;) { |
| 7739 | p++; |
| 7740 | switch (*p) { |
| 7741 | case '\0': |
| 7742 | argv++; |
| 7743 | p = *argv; |
| 7744 | if (p[0] != '-' || !p[1]) |
| 7745 | goto after_opts; |
| 7746 | continue; /* next arg is also -opts, process it too */ |
| 7747 | case 'v': |
| 7748 | case 'V': |
| 7749 | opt_vV = *p; |
| 7750 | continue; |
| 7751 | default: |
| 7752 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 7753 | } |
| 7754 | } |
| 7755 | } |
| 7756 | after_opts: |
| 7757 | # if ENABLE_HUSH_FUNCTIONS |
| 7758 | if (opt_vV && find_function(argv[0])) |
| 7759 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 7760 | # endif |
| 7761 | #endif |
| 7762 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7763 | /* Check if the command matches any of the builtins. |
| 7764 | * Depending on context, this might be redundant. But it's |
| 7765 | * easier to waste a few CPU cycles than it is to figure out |
| 7766 | * if this is one of those cases. |
| 7767 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7768 | /* Why "BB_MMU ? :" difference in logic? - |
| 7769 | * On NOMMU, it is more expensive to re-execute shell |
| 7770 | * just in order to run echo or test builtin. |
| 7771 | * It's better to skip it here and run corresponding |
| 7772 | * non-builtin later. */ |
| 7773 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7774 | if (x) { |
| 7775 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 7776 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7777 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7778 | |
| 7779 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7780 | /* Check if the command matches any busybox applets */ |
| 7781 | { |
| 7782 | int a = find_applet_by_name(argv[0]); |
| 7783 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7784 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7785 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7786 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7787 | /* Do not leak open fds from opened script files etc. |
| 7788 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7789 | * should not show tty fd open. |
| 7790 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7791 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7792 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7793 | //This casuses test failures in |
| 7794 | //redir_children_should_not_see_saved_fd_2.tests |
| 7795 | //redir_children_should_not_see_saved_fd_3.tests |
| 7796 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7797 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7798 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7799 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7800 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7801 | } |
| 7802 | # endif |
| 7803 | /* Re-exec ourselves */ |
| 7804 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7805 | /* Don't propagate SIG_IGN to the child */ |
| 7806 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7807 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7808 | execv(bb_busybox_exec_path, argv); |
| 7809 | /* If they called chroot or otherwise made the binary no longer |
| 7810 | * executable, fall through */ |
| 7811 | } |
| 7812 | } |
| 7813 | #endif |
| 7814 | |
| 7815 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7816 | skip: |
| 7817 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7818 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7819 | execvp_or_die(argv); |
| 7820 | } |
| 7821 | |
| 7822 | /* Called after [v]fork() in run_pipe |
| 7823 | */ |
| 7824 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7825 | struct command *command, |
| 7826 | char **argv_expanded) NORETURN; |
| 7827 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7828 | struct command *command, |
| 7829 | char **argv_expanded) |
| 7830 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 7831 | #if ENABLE_HUSH_FUNCTIONS |
| 7832 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7833 | /* Ignore funcdefs in pipes: |
| 7834 | * true | f() { cmd } |
| 7835 | */ |
| 7836 | _exit(0); |
| 7837 | } |
| 7838 | #endif |
| 7839 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7840 | if (command->argv) { |
| 7841 | pseudo_exec_argv(nommu_save, command->argv, |
| 7842 | command->assignment_cnt, argv_expanded); |
| 7843 | } |
| 7844 | |
| 7845 | if (command->group) { |
| 7846 | /* Cases when we are here: |
| 7847 | * ( list ) |
| 7848 | * { list } & |
| 7849 | * ... | ( list ) | ... |
| 7850 | * ... | { list } | ... |
| 7851 | */ |
| 7852 | #if BB_MMU |
| 7853 | int rcode; |
| 7854 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7855 | reset_traps_to_defaults(); |
| 7856 | rcode = run_list(command->group); |
| 7857 | /* OK to leak memory by not calling free_pipe_list, |
| 7858 | * since this process is about to exit */ |
| 7859 | _exit(rcode); |
| 7860 | #else |
| 7861 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7862 | command->group_as_string, |
| 7863 | G.global_argv[0], |
| 7864 | G.global_argv + 1, |
| 7865 | NULL); |
| 7866 | #endif |
| 7867 | } |
| 7868 | |
| 7869 | /* Case when we are here: ... | >file */ |
| 7870 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7871 | _exit(EXIT_SUCCESS); |
| 7872 | } |
| 7873 | |
| 7874 | #if ENABLE_HUSH_JOB |
| 7875 | static const char *get_cmdtext(struct pipe *pi) |
| 7876 | { |
| 7877 | char **argv; |
| 7878 | char *p; |
| 7879 | int len; |
| 7880 | |
| 7881 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7882 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7883 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7884 | if (pi->cmdtext) |
| 7885 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7886 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7887 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7888 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7889 | pi->cmdtext = xzalloc(1); |
| 7890 | return pi->cmdtext; |
| 7891 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7892 | len = 0; |
| 7893 | do { |
| 7894 | len += strlen(*argv) + 1; |
| 7895 | } while (*++argv); |
| 7896 | p = xmalloc(len); |
| 7897 | pi->cmdtext = p; |
| 7898 | argv = pi->cmds[0].argv; |
| 7899 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7900 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7901 | *p++ = ' '; |
| 7902 | } while (*++argv); |
| 7903 | p[-1] = '\0'; |
| 7904 | return pi->cmdtext; |
| 7905 | } |
| 7906 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7907 | static void remove_job_from_table(struct pipe *pi) |
| 7908 | { |
| 7909 | struct pipe *prev_pipe; |
| 7910 | |
| 7911 | if (pi == G.job_list) { |
| 7912 | G.job_list = pi->next; |
| 7913 | } else { |
| 7914 | prev_pipe = G.job_list; |
| 7915 | while (prev_pipe->next != pi) |
| 7916 | prev_pipe = prev_pipe->next; |
| 7917 | prev_pipe->next = pi->next; |
| 7918 | } |
| 7919 | G.last_jobid = 0; |
| 7920 | if (G.job_list) |
| 7921 | G.last_jobid = G.job_list->jobid; |
| 7922 | } |
| 7923 | |
| 7924 | static void delete_finished_job(struct pipe *pi) |
| 7925 | { |
| 7926 | remove_job_from_table(pi); |
| 7927 | free_pipe(pi); |
| 7928 | } |
| 7929 | |
| 7930 | static void clean_up_last_dead_job(void) |
| 7931 | { |
| 7932 | if (G.job_list && !G.job_list->alive_cmds) |
| 7933 | delete_finished_job(G.job_list); |
| 7934 | } |
| 7935 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7936 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7937 | { |
| 7938 | struct pipe *job, **jobp; |
| 7939 | int i; |
| 7940 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7941 | clean_up_last_dead_job(); |
| 7942 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7943 | /* Find the end of the list, and find next job ID to use */ |
| 7944 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7945 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7946 | while ((job = *jobp) != NULL) { |
| 7947 | if (job->jobid > i) |
| 7948 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7949 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7950 | } |
| 7951 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7952 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7953 | /* Create a new job struct at the end */ |
| 7954 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7955 | job->next = NULL; |
| 7956 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7957 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7958 | for (i = 0; i < pi->num_cmds; i++) { |
| 7959 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7960 | /* all other fields are not used and stay zero */ |
| 7961 | } |
| 7962 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7963 | |
| 7964 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7965 | 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] | 7966 | G.last_jobid = job->jobid; |
| 7967 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7968 | #endif /* JOB */ |
| 7969 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7970 | static int job_exited_or_stopped(struct pipe *pi) |
| 7971 | { |
| 7972 | int rcode, i; |
| 7973 | |
| 7974 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7975 | return -1; |
| 7976 | |
| 7977 | /* All processes in fg pipe have exited or stopped */ |
| 7978 | rcode = 0; |
| 7979 | i = pi->num_cmds; |
| 7980 | while (--i >= 0) { |
| 7981 | rcode = pi->cmds[i].cmd_exitcode; |
| 7982 | /* usually last process gives overall exitstatus, |
| 7983 | * but with "set -o pipefail", last *failed* process does */ |
| 7984 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7985 | break; |
| 7986 | } |
| 7987 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7988 | return rcode; |
| 7989 | } |
| 7990 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7991 | 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] | 7992 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7993 | #if ENABLE_HUSH_JOB |
| 7994 | struct pipe *pi; |
| 7995 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7996 | int i, dead; |
| 7997 | |
| 7998 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7999 | |
| 8000 | #if DEBUG_JOBS |
| 8001 | if (WIFSTOPPED(status)) |
| 8002 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8003 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8004 | if (WIFSIGNALED(status)) |
| 8005 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8006 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8007 | if (WIFEXITED(status)) |
| 8008 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8009 | childpid, WEXITSTATUS(status)); |
| 8010 | #endif |
| 8011 | /* Were we asked to wait for a fg pipe? */ |
| 8012 | if (fg_pipe) { |
| 8013 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8014 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8015 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8016 | int rcode; |
| 8017 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8018 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8019 | if (fg_pipe->cmds[i].pid != childpid) |
| 8020 | continue; |
| 8021 | if (dead) { |
| 8022 | int ex; |
| 8023 | fg_pipe->cmds[i].pid = 0; |
| 8024 | fg_pipe->alive_cmds--; |
| 8025 | ex = WEXITSTATUS(status); |
| 8026 | /* bash prints killer signal's name for *last* |
| 8027 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8028 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8029 | */ |
| 8030 | if (WIFSIGNALED(status)) { |
| 8031 | int sig = WTERMSIG(status); |
| 8032 | if (i == fg_pipe->num_cmds-1) |
| 8033 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 8034 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 8035 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8036 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8037 | * Maybe we need to use sig | 128? */ |
| 8038 | ex = sig + 128; |
| 8039 | } |
| 8040 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8041 | } else { |
| 8042 | fg_pipe->stopped_cmds++; |
| 8043 | } |
| 8044 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8045 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8046 | rcode = job_exited_or_stopped(fg_pipe); |
| 8047 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8048 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8049 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8050 | * and "killall -STOP cat" */ |
| 8051 | if (G_interactive_fd) { |
| 8052 | #if ENABLE_HUSH_JOB |
| 8053 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8054 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8055 | #endif |
| 8056 | return rcode; |
| 8057 | } |
| 8058 | if (fg_pipe->alive_cmds == 0) |
| 8059 | return rcode; |
| 8060 | } |
| 8061 | /* There are still running processes in the fg_pipe */ |
| 8062 | return -1; |
| 8063 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8064 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8065 | } |
| 8066 | |
| 8067 | #if ENABLE_HUSH_JOB |
| 8068 | /* We were asked to wait for bg or orphaned children */ |
| 8069 | /* No need to remember exitcode in this case */ |
| 8070 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8071 | for (i = 0; i < pi->num_cmds; i++) { |
| 8072 | if (pi->cmds[i].pid == childpid) |
| 8073 | goto found_pi_and_prognum; |
| 8074 | } |
| 8075 | } |
| 8076 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8077 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8078 | return -1; /* this wasn't a process from fg_pipe */ |
| 8079 | |
| 8080 | found_pi_and_prognum: |
| 8081 | if (dead) { |
| 8082 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8083 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8084 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8085 | rcode = 128 + WTERMSIG(status); |
| 8086 | pi->cmds[i].cmd_exitcode = rcode; |
| 8087 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8088 | G.last_bg_pid_exitcode = rcode; |
| 8089 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8090 | pi->alive_cmds--; |
| 8091 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8092 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8093 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8094 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8095 | delete_finished_job(pi); |
| 8096 | } else { |
| 8097 | /* |
| 8098 | * bash deletes finished jobs from job table only in interactive mode, |
| 8099 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8100 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8101 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8102 | * We only retain one "dead" job, if it's the single job on the list. |
| 8103 | * This covers most of real-world scenarios where this is useful. |
| 8104 | */ |
| 8105 | if (pi != G.job_list) |
| 8106 | delete_finished_job(pi); |
| 8107 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8108 | } |
| 8109 | } else { |
| 8110 | /* child stopped */ |
| 8111 | pi->stopped_cmds++; |
| 8112 | } |
| 8113 | #endif |
| 8114 | return -1; /* this wasn't a process from fg_pipe */ |
| 8115 | } |
| 8116 | |
| 8117 | /* Check to see if any processes have exited -- if they have, |
| 8118 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8119 | * |
| 8120 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8121 | * Return its exitcode or zero if stopped. |
| 8122 | * |
| 8123 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8124 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8125 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8126 | * or 0 if no children changed status. |
| 8127 | * |
| 8128 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8129 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8130 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8131 | */ |
| 8132 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8133 | { |
| 8134 | int attributes; |
| 8135 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8136 | int rcode = 0; |
| 8137 | |
| 8138 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8139 | |
| 8140 | attributes = WUNTRACED; |
| 8141 | if (fg_pipe == NULL) |
| 8142 | attributes |= WNOHANG; |
| 8143 | |
| 8144 | errno = 0; |
| 8145 | #if ENABLE_HUSH_FAST |
| 8146 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8147 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8148 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8149 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8150 | /* Avoid doing waitpid syscall if possible */ |
| 8151 | if (!G.we_have_children) { |
| 8152 | errno = ECHILD; |
| 8153 | return -1; |
| 8154 | } |
| 8155 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8156 | /* We have children, but they did not exit |
| 8157 | * or stop yet (we saw no SIGCHLD) */ |
| 8158 | return 0; |
| 8159 | } |
| 8160 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8161 | } |
| 8162 | #endif |
| 8163 | |
| 8164 | /* Do we do this right? |
| 8165 | * bash-3.00# sleep 20 | false |
| 8166 | * <ctrl-Z pressed> |
| 8167 | * [3]+ Stopped sleep 20 | false |
| 8168 | * bash-3.00# echo $? |
| 8169 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8170 | * [hush 1.14.0: yes we do it right] |
| 8171 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8172 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8173 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8174 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8175 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8176 | i = G.count_SIGCHLD; |
| 8177 | #endif |
| 8178 | childpid = waitpid(-1, &status, attributes); |
| 8179 | if (childpid <= 0) { |
| 8180 | if (childpid && errno != ECHILD) |
| 8181 | bb_perror_msg("waitpid"); |
| 8182 | #if ENABLE_HUSH_FAST |
| 8183 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8184 | G.we_have_children = (childpid == 0); |
| 8185 | G.handled_SIGCHLD = i; |
| 8186 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8187 | } |
| 8188 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8189 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8190 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8191 | break; |
| 8192 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8193 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8194 | if (rcode >= 0) { |
| 8195 | /* fg_pipe exited or stopped */ |
| 8196 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8197 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8198 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8199 | 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] | 8200 | rcode = WEXITSTATUS(status); |
| 8201 | if (WIFSIGNALED(status)) |
| 8202 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8203 | if (WIFSTOPPED(status)) |
| 8204 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8205 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8206 | rcode++; |
| 8207 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8208 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8209 | /* This wasn't one of our processes, or */ |
| 8210 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8211 | } /* while (waitpid succeeds)... */ |
| 8212 | |
| 8213 | return rcode; |
| 8214 | } |
| 8215 | |
| 8216 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8217 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8218 | { |
| 8219 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8220 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8221 | if (G_saved_tty_pgrp) { |
| 8222 | /* Job finished, move the shell to the foreground */ |
| 8223 | p = getpgrp(); /* our process group id */ |
| 8224 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8225 | tcsetpgrp(G_interactive_fd, p); |
| 8226 | } |
| 8227 | return rcode; |
| 8228 | } |
| 8229 | #endif |
| 8230 | |
| 8231 | /* Start all the jobs, but don't wait for anything to finish. |
| 8232 | * See checkjobs(). |
| 8233 | * |
| 8234 | * Return code is normally -1, when the caller has to wait for children |
| 8235 | * to finish to determine the exit status of the pipe. If the pipe |
| 8236 | * is a simple builtin command, however, the action is done by the |
| 8237 | * time run_pipe returns, and the exit code is provided as the |
| 8238 | * return value. |
| 8239 | * |
| 8240 | * Returns -1 only if started some children. IOW: we have to |
| 8241 | * mask out retvals of builtins etc with 0xff! |
| 8242 | * |
| 8243 | * The only case when we do not need to [v]fork is when the pipe |
| 8244 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8245 | * cmd ; ... { list } ; ... |
| 8246 | * cmd && ... { list } && ... |
| 8247 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8248 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8249 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8250 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8251 | * |
| 8252 | * Cases when we must fork: |
| 8253 | * non-single: cmd | cmd |
| 8254 | * backgrounded: cmd & { list } & |
| 8255 | * subshell: ( list ) [&] |
| 8256 | */ |
| 8257 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8258 | #define redirect_and_varexp_helper(old_vars_p, command, squirrel, argv_expanded) \ |
| 8259 | redirect_and_varexp_helper(old_vars_p, command, squirrel) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8260 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8261 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8262 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8263 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8264 | char **argv_expanded) |
| 8265 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8266 | /* Assignments occur before redirects. Try: |
| 8267 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8268 | */ |
| 8269 | |
| 8270 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8271 | dump_cmd_in_x_mode(new_env); |
| 8272 | dump_cmd_in_x_mode(argv_expanded); |
| 8273 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8274 | set_vars_and_save_old(new_env); |
| 8275 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8276 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8277 | * This is perfect for work that comes after exec(). |
| 8278 | * Is it really safe for inline use? Experimentally, |
| 8279 | * things seem to work. */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8280 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8281 | } |
| 8282 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8283 | { |
| 8284 | static const char *const null_ptr = NULL; |
| 8285 | |
| 8286 | int cmd_no; |
| 8287 | int next_infd; |
| 8288 | struct command *command; |
| 8289 | char **argv_expanded; |
| 8290 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8291 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8292 | int rcode; |
| 8293 | |
| 8294 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8295 | debug_enter(); |
| 8296 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8297 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8298 | * Result should be 3 lines: q w e, qwe, q w e |
| 8299 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8300 | if (G.ifs_whitespace != G.ifs) |
| 8301 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8302 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8303 | if (G.ifs) { |
| 8304 | char *p; |
| 8305 | G.ifs_whitespace = (char*)G.ifs; |
| 8306 | p = skip_whitespace(G.ifs); |
| 8307 | if (*p) { |
| 8308 | /* Not all $IFS is whitespace */ |
| 8309 | char *d; |
| 8310 | int len = p - G.ifs; |
| 8311 | p = skip_non_whitespace(p); |
| 8312 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 8313 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 8314 | while (*p) { |
| 8315 | if (isspace(*p)) |
| 8316 | *d++ = *p; |
| 8317 | p++; |
| 8318 | } |
| 8319 | *d = '\0'; |
| 8320 | } |
| 8321 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8322 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8323 | G.ifs_whitespace = (char*)G.ifs; |
| 8324 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8325 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8326 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8327 | pi->stopped_cmds = 0; |
| 8328 | command = &pi->cmds[0]; |
| 8329 | argv_expanded = NULL; |
| 8330 | |
| 8331 | if (pi->num_cmds != 1 |
| 8332 | || pi->followup == PIPE_BG |
| 8333 | || command->cmd_type == CMD_SUBSHELL |
| 8334 | ) { |
| 8335 | goto must_fork; |
| 8336 | } |
| 8337 | |
| 8338 | pi->alive_cmds = 1; |
| 8339 | |
| 8340 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8341 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8342 | |
| 8343 | if (command->group) { |
| 8344 | #if ENABLE_HUSH_FUNCTIONS |
| 8345 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8346 | /* "executing" func () { list } */ |
| 8347 | struct function *funcp; |
| 8348 | |
| 8349 | funcp = new_function(command->argv[0]); |
| 8350 | /* funcp->name is already set to argv[0] */ |
| 8351 | funcp->body = command->group; |
| 8352 | # if !BB_MMU |
| 8353 | funcp->body_as_string = command->group_as_string; |
| 8354 | command->group_as_string = NULL; |
| 8355 | # endif |
| 8356 | command->group = NULL; |
| 8357 | command->argv[0] = NULL; |
| 8358 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8359 | funcp->parent_cmd = command; |
| 8360 | command->child_func = funcp; |
| 8361 | |
| 8362 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8363 | debug_leave(); |
| 8364 | return EXIT_SUCCESS; |
| 8365 | } |
| 8366 | #endif |
| 8367 | /* { list } */ |
| 8368 | debug_printf("non-subshell group\n"); |
| 8369 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8370 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8371 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8372 | //FIXME: we need to pass squirrel down into run_list() |
| 8373 | //for SH_STANDALONE case, or else this construct: |
| 8374 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8375 | //has no way of closing saved fd#1 for "find", |
| 8376 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8377 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8378 | rcode = run_list(command->group) & 0xff; |
| 8379 | } |
| 8380 | restore_redirects(squirrel); |
| 8381 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8382 | debug_leave(); |
| 8383 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8384 | return rcode; |
| 8385 | } |
| 8386 | |
| 8387 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8388 | { |
| 8389 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8390 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8391 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8392 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8393 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8394 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8395 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8396 | if (G.lineno_var) |
| 8397 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8398 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8399 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8400 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8401 | /* Assignments, but no command. |
| 8402 | * Ensure redirects take effect (that is, create files). |
| 8403 | * Try "a=t >file" |
| 8404 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8405 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8406 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8407 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8408 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8409 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8410 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8411 | /* Set shell variables */ |
| 8412 | if (G_x_mode) |
| 8413 | bb_putchar_stderr('+'); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8414 | i = 0; |
| 8415 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8416 | char *p = expand_string_to_string(argv[i], |
| 8417 | EXP_FLAG_ESC_GLOB_CHARS, |
| 8418 | /*unbackslash:*/ 1 |
| 8419 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8420 | if (G_x_mode) |
| 8421 | fprintf(stderr, " %s", p); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8422 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8423 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8424 | /* assignment to readonly var / putenv error? */ |
| 8425 | rcode = 1; |
| 8426 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8427 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8428 | } |
| 8429 | if (G_x_mode) |
| 8430 | bb_putchar_stderr('\n'); |
| 8431 | /* Redirect error sets $? to 1. Otherwise, |
| 8432 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8433 | * Else, clear $?: |
| 8434 | * false; q=`exit 2`; echo $? - should print 2 |
| 8435 | * false; x=1; echo $? - should print 0 |
| 8436 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 8437 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8438 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8439 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8440 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8441 | debug_leave(); |
| 8442 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8443 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8444 | } |
| 8445 | |
| 8446 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 8447 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8448 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8449 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8450 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8451 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8452 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8453 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8454 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8455 | if (!argv_expanded[0]) { |
| 8456 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8457 | /* `false` still has to set exitcode 1 */ |
| 8458 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8459 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8460 | } |
| 8461 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8462 | old_vars = NULL; |
| 8463 | sv_shadowed = G.shadowed_vars_pp; |
| 8464 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8465 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8466 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 8467 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 8468 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8469 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8470 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8471 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8472 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8473 | /* |
| 8474 | * Variable assignments are executed, but then "forgotten": |
| 8475 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 8476 | * sleeps, but prints nothing. |
| 8477 | */ |
| 8478 | enter_var_nest_level(); |
| 8479 | G.shadowed_vars_pp = &old_vars; |
| 8480 | rcode = redirect_and_varexp_helper(command, /*squirrel:*/ NULL, argv_expanded); |
| 8481 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8482 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8483 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8484 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8485 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8486 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8487 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8488 | * a=b true; env | grep ^a= |
| 8489 | */ |
| 8490 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8491 | /* Collect all variables "shadowed" by helper |
| 8492 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 8493 | * into old_vars list: |
| 8494 | */ |
| 8495 | G.shadowed_vars_pp = &old_vars; |
| 8496 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8497 | if (rcode == 0) { |
| 8498 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8499 | /* Do not collect *to old_vars list* vars shadowed |
| 8500 | * by e.g. "local VAR" builtin (collect them |
| 8501 | * in the previously nested list instead): |
| 8502 | * don't want them to be restored immediately |
| 8503 | * after "local" completes. |
| 8504 | */ |
| 8505 | G.shadowed_vars_pp = sv_shadowed; |
| 8506 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8507 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8508 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8509 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8510 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8511 | fflush_all(); |
| 8512 | } |
| 8513 | #if ENABLE_HUSH_FUNCTIONS |
| 8514 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8515 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8516 | funcp->name, argv_expanded[1]); |
| 8517 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8518 | /* |
| 8519 | * But do collect *to old_vars list* vars shadowed |
| 8520 | * within function execution. To that end, restore |
| 8521 | * this pointer _after_ function run: |
| 8522 | */ |
| 8523 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8524 | } |
| 8525 | #endif |
| 8526 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8527 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8528 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8529 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8530 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 8531 | goto must_fork; |
| 8532 | |
| 8533 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8534 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 8535 | G.shadowed_vars_pp = &old_vars; |
| 8536 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 8537 | G.shadowed_vars_pp = sv_shadowed; |
| 8538 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8539 | if (rcode == 0) { |
| 8540 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8541 | argv_expanded[0], argv_expanded[1]); |
| 8542 | /* |
| 8543 | * Note: signals (^C) can't interrupt here. |
| 8544 | * We remember them and they will be acted upon |
| 8545 | * after applet returns. |
| 8546 | * This makes applets which can run for a long time |
| 8547 | * and/or wait for user input ineligible for NOFORK: |
| 8548 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8549 | */ |
| 8550 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8551 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 8552 | } else |
| 8553 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8554 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8555 | restore_redirects(squirrel); |
| 8556 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8557 | leave_var_nest_level(); |
| 8558 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8559 | |
| 8560 | /* |
| 8561 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8562 | * with FEATURE_SH_NOFORK=y. |
| 8563 | */ |
| 8564 | if (!funcp) { |
| 8565 | /* It was builtin or nofork. |
| 8566 | * if this would be a real fork/execed program, |
| 8567 | * it should have died if a fatal sig was received. |
| 8568 | * But OTOH, there was no separate process, |
| 8569 | * the sig was sent to _shell_, not to non-existing |
| 8570 | * child. |
| 8571 | * Let's just handle ^C only, this one is obvious: |
| 8572 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8573 | * during builtin/nofork. |
| 8574 | */ |
| 8575 | if (sigismember(&G.pending_set, SIGINT)) |
| 8576 | rcode = 128 + SIGINT; |
| 8577 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8578 | free(argv_expanded); |
| 8579 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8580 | debug_leave(); |
| 8581 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8582 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8583 | } |
| 8584 | |
| 8585 | must_fork: |
| 8586 | /* NB: argv_expanded may already be created, and that |
| 8587 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8588 | * use argv_expanded if it's non-NULL */ |
| 8589 | |
| 8590 | /* Going to fork a child per each pipe member */ |
| 8591 | pi->alive_cmds = 0; |
| 8592 | next_infd = 0; |
| 8593 | |
| 8594 | cmd_no = 0; |
| 8595 | while (cmd_no < pi->num_cmds) { |
| 8596 | struct fd_pair pipefds; |
| 8597 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8598 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8599 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8600 | nommu_save.old_vars = NULL; |
| 8601 | nommu_save.argv = NULL; |
| 8602 | nommu_save.argv_from_re_execing = NULL; |
| 8603 | #endif |
| 8604 | command = &pi->cmds[cmd_no]; |
| 8605 | cmd_no++; |
| 8606 | if (command->argv) { |
| 8607 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8608 | command->argv[0], command->argv[1]); |
| 8609 | } else { |
| 8610 | debug_printf_exec(": pipe member with no argv\n"); |
| 8611 | } |
| 8612 | |
| 8613 | /* pipes are inserted between pairs of commands */ |
| 8614 | pipefds.rd = 0; |
| 8615 | pipefds.wr = 1; |
| 8616 | if (cmd_no < pi->num_cmds) |
| 8617 | xpiped_pair(pipefds); |
| 8618 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8619 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8620 | if (G.lineno_var) |
| 8621 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8622 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8623 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8624 | command->pid = BB_MMU ? fork() : vfork(); |
| 8625 | if (!command->pid) { /* child */ |
| 8626 | #if ENABLE_HUSH_JOB |
| 8627 | disable_restore_tty_pgrp_on_exit(); |
| 8628 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8629 | |
| 8630 | /* Every child adds itself to new process group |
| 8631 | * with pgid == pid_of_first_child_in_pipe */ |
| 8632 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8633 | pid_t pgrp; |
| 8634 | pgrp = pi->pgrp; |
| 8635 | if (pgrp < 0) /* true for 1st process only */ |
| 8636 | pgrp = getpid(); |
| 8637 | if (setpgid(0, pgrp) == 0 |
| 8638 | && pi->followup != PIPE_BG |
| 8639 | && G_saved_tty_pgrp /* we have ctty */ |
| 8640 | ) { |
| 8641 | /* We do it in *every* child, not just first, |
| 8642 | * to avoid races */ |
| 8643 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8644 | } |
| 8645 | } |
| 8646 | #endif |
| 8647 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8648 | /* 1st cmd in backgrounded pipe |
| 8649 | * should have its stdin /dev/null'ed */ |
| 8650 | close(0); |
| 8651 | if (open(bb_dev_null, O_RDONLY)) |
| 8652 | xopen("/", O_RDONLY); |
| 8653 | } else { |
| 8654 | xmove_fd(next_infd, 0); |
| 8655 | } |
| 8656 | xmove_fd(pipefds.wr, 1); |
| 8657 | if (pipefds.rd > 1) |
| 8658 | close(pipefds.rd); |
| 8659 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8660 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8661 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8662 | * of cmd1 goes into pipe. |
| 8663 | */ |
| 8664 | if (setup_redirects(command, NULL)) { |
| 8665 | /* Happens when redir file can't be opened: |
| 8666 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8667 | * FOO |
| 8668 | * hush: can't open '/qwe/rty': No such file or directory |
| 8669 | * BAZ |
| 8670 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8671 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8672 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8673 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8674 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8675 | /* Stores to nommu_save list of env vars putenv'ed |
| 8676 | * (NOMMU, on MMU we don't need that) */ |
| 8677 | /* cast away volatility... */ |
| 8678 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8679 | /* pseudo_exec() does not return */ |
| 8680 | } |
| 8681 | |
| 8682 | /* parent or error */ |
| 8683 | #if ENABLE_HUSH_FAST |
| 8684 | G.count_SIGCHLD++; |
| 8685 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8686 | #endif |
| 8687 | enable_restore_tty_pgrp_on_exit(); |
| 8688 | #if !BB_MMU |
| 8689 | /* Clean up after vforked child */ |
| 8690 | free(nommu_save.argv); |
| 8691 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8692 | G.var_nest_level = sv_var_nest_level; |
| 8693 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8694 | add_vars(nommu_save.old_vars); |
| 8695 | #endif |
| 8696 | free(argv_expanded); |
| 8697 | argv_expanded = NULL; |
| 8698 | if (command->pid < 0) { /* [v]fork failed */ |
| 8699 | /* Clearly indicate, was it fork or vfork */ |
| 8700 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8701 | } else { |
| 8702 | pi->alive_cmds++; |
| 8703 | #if ENABLE_HUSH_JOB |
| 8704 | /* Second and next children need to know pid of first one */ |
| 8705 | if (pi->pgrp < 0) |
| 8706 | pi->pgrp = command->pid; |
| 8707 | #endif |
| 8708 | } |
| 8709 | |
| 8710 | if (cmd_no > 1) |
| 8711 | close(next_infd); |
| 8712 | if (cmd_no < pi->num_cmds) |
| 8713 | close(pipefds.wr); |
| 8714 | /* Pass read (output) pipe end to next iteration */ |
| 8715 | next_infd = pipefds.rd; |
| 8716 | } |
| 8717 | |
| 8718 | if (!pi->alive_cmds) { |
| 8719 | debug_leave(); |
| 8720 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8721 | return 1; |
| 8722 | } |
| 8723 | |
| 8724 | debug_leave(); |
| 8725 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8726 | return -1; |
| 8727 | } |
| 8728 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8729 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8730 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8731 | static int run_list(struct pipe *pi) |
| 8732 | { |
| 8733 | #if ENABLE_HUSH_CASE |
| 8734 | char *case_word = NULL; |
| 8735 | #endif |
| 8736 | #if ENABLE_HUSH_LOOPS |
| 8737 | struct pipe *loop_top = NULL; |
| 8738 | char **for_lcur = NULL; |
| 8739 | char **for_list = NULL; |
| 8740 | #endif |
| 8741 | smallint last_followup; |
| 8742 | smalluint rcode; |
| 8743 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8744 | smalluint cond_code = 0; |
| 8745 | #else |
| 8746 | enum { cond_code = 0 }; |
| 8747 | #endif |
| 8748 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8749 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8750 | smallint last_rword; /* ditto */ |
| 8751 | #endif |
| 8752 | |
| 8753 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8754 | debug_enter(); |
| 8755 | |
| 8756 | #if ENABLE_HUSH_LOOPS |
| 8757 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8758 | { |
| 8759 | struct pipe *cpipe; |
| 8760 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8761 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8762 | continue; |
| 8763 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8764 | if (cpipe->next == NULL) { |
| 8765 | syntax_error("malformed for"); |
| 8766 | debug_leave(); |
| 8767 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8768 | return 1; |
| 8769 | } |
| 8770 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8771 | if (cpipe->next->res_word == RES_DO) |
| 8772 | continue; |
| 8773 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8774 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8775 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8776 | ) { |
| 8777 | syntax_error("malformed for"); |
| 8778 | debug_leave(); |
| 8779 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8780 | return 1; |
| 8781 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8782 | } |
| 8783 | } |
| 8784 | #endif |
| 8785 | |
| 8786 | /* Past this point, all code paths should jump to ret: label |
| 8787 | * in order to return, no direct "return" statements please. |
| 8788 | * This helps to ensure that no memory is leaked. */ |
| 8789 | |
| 8790 | #if ENABLE_HUSH_JOB |
| 8791 | G.run_list_level++; |
| 8792 | #endif |
| 8793 | |
| 8794 | #if HAS_KEYWORDS |
| 8795 | rword = RES_NONE; |
| 8796 | last_rword = RES_XXXX; |
| 8797 | #endif |
| 8798 | last_followup = PIPE_SEQ; |
| 8799 | rcode = G.last_exitcode; |
| 8800 | |
| 8801 | /* Go through list of pipes, (maybe) executing them. */ |
| 8802 | 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] | 8803 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8804 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8805 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8806 | if (G.flag_SIGINT) |
| 8807 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8808 | if (G_flag_return_in_progress == 1) |
| 8809 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8810 | |
| 8811 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8812 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8813 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8814 | |
| 8815 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8816 | if ( |
| 8817 | #if ENABLE_HUSH_IF |
| 8818 | rword == RES_IF || rword == RES_ELIF || |
| 8819 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8820 | pi->followup != PIPE_SEQ |
| 8821 | ) { |
| 8822 | G.errexit_depth++; |
| 8823 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8824 | #if ENABLE_HUSH_LOOPS |
| 8825 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8826 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8827 | ) { |
| 8828 | /* start of a loop: remember where loop starts */ |
| 8829 | loop_top = pi; |
| 8830 | G.depth_of_loop++; |
| 8831 | } |
| 8832 | #endif |
| 8833 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8834 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8835 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8836 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8837 | ) { |
| 8838 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8839 | * and we should not execute CMD */ |
| 8840 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8841 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8842 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8843 | } |
| 8844 | } |
| 8845 | last_followup = pi->followup; |
| 8846 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8847 | #if ENABLE_HUSH_IF |
| 8848 | if (cond_code) { |
| 8849 | if (rword == RES_THEN) { |
| 8850 | /* if false; then ... fi has exitcode 0! */ |
| 8851 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8852 | /* "if <false> THEN cmd": skip cmd */ |
| 8853 | continue; |
| 8854 | } |
| 8855 | } else { |
| 8856 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8857 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8858 | * skip cmd and all following ones */ |
| 8859 | break; |
| 8860 | } |
| 8861 | } |
| 8862 | #endif |
| 8863 | #if ENABLE_HUSH_LOOPS |
| 8864 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 8865 | if (!for_lcur) { |
| 8866 | /* first loop through for */ |
| 8867 | |
| 8868 | static const char encoded_dollar_at[] ALIGN1 = { |
| 8869 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 8870 | }; /* encoded representation of "$@" */ |
| 8871 | static const char *const encoded_dollar_at_argv[] = { |
| 8872 | encoded_dollar_at, NULL |
| 8873 | }; /* argv list with one element: "$@" */ |
| 8874 | char **vals; |
| 8875 | |
| 8876 | vals = (char**)encoded_dollar_at_argv; |
| 8877 | if (pi->next->res_word == RES_IN) { |
| 8878 | /* if no variable values after "in" we skip "for" */ |
| 8879 | if (!pi->next->cmds[0].argv) { |
| 8880 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8881 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 8882 | break; |
| 8883 | } |
| 8884 | vals = pi->next->cmds[0].argv; |
| 8885 | } /* else: "for var; do..." -> assume "$@" list */ |
| 8886 | /* create list of variable values */ |
| 8887 | debug_print_strings("for_list made from", vals); |
| 8888 | for_list = expand_strvec_to_strvec(vals); |
| 8889 | for_lcur = for_list; |
| 8890 | debug_print_strings("for_list", for_list); |
| 8891 | } |
| 8892 | if (!*for_lcur) { |
| 8893 | /* "for" loop is over, clean up */ |
| 8894 | free(for_list); |
| 8895 | for_list = NULL; |
| 8896 | for_lcur = NULL; |
| 8897 | break; |
| 8898 | } |
| 8899 | /* Insert next value from for_lcur */ |
| 8900 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8901 | 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] | 8902 | continue; |
| 8903 | } |
| 8904 | if (rword == RES_IN) { |
| 8905 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 8906 | } |
| 8907 | if (rword == RES_DONE) { |
| 8908 | continue; /* "done" has no cmds too */ |
| 8909 | } |
| 8910 | #endif |
| 8911 | #if ENABLE_HUSH_CASE |
| 8912 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8913 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8914 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 8915 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 8916 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 8917 | //unbackslash(case_word); |
| 8918 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8919 | continue; |
| 8920 | } |
| 8921 | if (rword == RES_MATCH) { |
| 8922 | char **argv; |
| 8923 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8924 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8925 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 8926 | break; |
| 8927 | /* all prev words didn't match, does this one match? */ |
| 8928 | argv = pi->cmds->argv; |
| 8929 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8930 | char *pattern; |
| 8931 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 8932 | pattern = expand_string_to_string(*argv, |
| 8933 | EXP_FLAG_ESC_GLOB_CHARS, |
| 8934 | /*unbackslash:*/ 0 |
| 8935 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8936 | /* TODO: which FNM_xxx flags to use? */ |
| 8937 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8938 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 8939 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8940 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8941 | if (cond_code == 0) { |
| 8942 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8943 | free(case_word); |
| 8944 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8945 | break; |
| 8946 | } |
| 8947 | argv++; |
| 8948 | } |
| 8949 | continue; |
| 8950 | } |
| 8951 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8952 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8953 | if (cond_code != 0) |
| 8954 | continue; /* not matched yet, skip this pipe */ |
| 8955 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8956 | if (rword == RES_ESAC) { |
| 8957 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 8958 | if (case_word) { |
| 8959 | /* "case" did not match anything: still set $? (to 0) */ |
| 8960 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8961 | } |
| 8962 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8963 | #endif |
| 8964 | /* Just pressing <enter> in shell should check for jobs. |
| 8965 | * OTOH, in non-interactive shell this is useless |
| 8966 | * and only leads to extra job checks */ |
| 8967 | if (pi->num_cmds == 0) { |
| 8968 | if (G_interactive_fd) |
| 8969 | goto check_jobs_and_continue; |
| 8970 | continue; |
| 8971 | } |
| 8972 | |
| 8973 | /* After analyzing all keywords and conditions, we decided |
| 8974 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8975 | * after run_pipe to collect any background children, |
| 8976 | * even if list execution is to be stopped. */ |
| 8977 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8978 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8979 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8980 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8981 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8982 | if (r != -1) { |
| 8983 | /* We ran a builtin, function, or group. |
| 8984 | * rcode is already known |
| 8985 | * and we don't need to wait for anything. */ |
| 8986 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8987 | G.last_exitcode = rcode; |
| 8988 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8989 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8990 | /* Was it "break" or "continue"? */ |
| 8991 | if (G.flag_break_continue) { |
| 8992 | smallint fbc = G.flag_break_continue; |
| 8993 | /* We might fall into outer *loop*, |
| 8994 | * don't want to break it too */ |
| 8995 | if (loop_top) { |
| 8996 | G.depth_break_continue--; |
| 8997 | if (G.depth_break_continue == 0) |
| 8998 | G.flag_break_continue = 0; |
| 8999 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9000 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9001 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9002 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9003 | break; |
| 9004 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9005 | /* "continue": simulate end of loop */ |
| 9006 | rword = RES_DONE; |
| 9007 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9008 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9009 | #endif |
| 9010 | if (G_flag_return_in_progress == 1) { |
| 9011 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9012 | break; |
| 9013 | } |
| 9014 | } else if (pi->followup == PIPE_BG) { |
| 9015 | /* What does bash do with attempts to background builtins? */ |
| 9016 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9017 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9018 | * I'm NOT treating inner &'s as jobs */ |
| 9019 | #if ENABLE_HUSH_JOB |
| 9020 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9021 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9022 | #endif |
| 9023 | /* Last command's pid goes to $! */ |
| 9024 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9025 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9026 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9027 | /* 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] | 9028 | rcode = EXIT_SUCCESS; |
| 9029 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9030 | } else { |
| 9031 | #if ENABLE_HUSH_JOB |
| 9032 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9033 | /* Waits for completion, then fg's main shell */ |
| 9034 | rcode = checkjobs_and_fg_shell(pi); |
| 9035 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9036 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9037 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9038 | #endif |
| 9039 | /* This one just waits for completion */ |
| 9040 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 9041 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 9042 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9043 | G.last_exitcode = rcode; |
| 9044 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9045 | } |
| 9046 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9047 | /* Handle "set -e" */ |
| 9048 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 9049 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 9050 | if (G.errexit_depth == 0) |
| 9051 | hush_exit(rcode); |
| 9052 | } |
| 9053 | G.errexit_depth = sv_errexit_depth; |
| 9054 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9055 | /* Analyze how result affects subsequent commands */ |
| 9056 | #if ENABLE_HUSH_IF |
| 9057 | if (rword == RES_IF || rword == RES_ELIF) |
| 9058 | cond_code = rcode; |
| 9059 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9060 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9061 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9062 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9063 | #if ENABLE_HUSH_LOOPS |
| 9064 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9065 | if (pi->next |
| 9066 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9067 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9068 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9069 | if (rword == RES_WHILE) { |
| 9070 | if (rcode) { |
| 9071 | /* "while false; do...done" - exitcode 0 */ |
| 9072 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9073 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9074 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9075 | } |
| 9076 | } |
| 9077 | if (rword == RES_UNTIL) { |
| 9078 | if (!rcode) { |
| 9079 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9080 | break; |
| 9081 | } |
| 9082 | } |
| 9083 | } |
| 9084 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9085 | } /* for (pi) */ |
| 9086 | |
| 9087 | #if ENABLE_HUSH_JOB |
| 9088 | G.run_list_level--; |
| 9089 | #endif |
| 9090 | #if ENABLE_HUSH_LOOPS |
| 9091 | if (loop_top) |
| 9092 | G.depth_of_loop--; |
| 9093 | free(for_list); |
| 9094 | #endif |
| 9095 | #if ENABLE_HUSH_CASE |
| 9096 | free(case_word); |
| 9097 | #endif |
| 9098 | debug_leave(); |
| 9099 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9100 | return rcode; |
| 9101 | } |
| 9102 | |
| 9103 | /* Select which version we will use */ |
| 9104 | static int run_and_free_list(struct pipe *pi) |
| 9105 | { |
| 9106 | int rcode = 0; |
| 9107 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9108 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9109 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9110 | rcode = run_list(pi); |
| 9111 | } |
| 9112 | /* free_pipe_list has the side effect of clearing memory. |
| 9113 | * In the long run that function can be merged with run_list, |
| 9114 | * but doing that now would hobble the debugging effort. */ |
| 9115 | free_pipe_list(pi); |
| 9116 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9117 | return rcode; |
| 9118 | } |
| 9119 | |
| 9120 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9121 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9122 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9123 | sighandler_t old_handler; |
| 9124 | unsigned sig = 0; |
| 9125 | while ((mask >>= 1) != 0) { |
| 9126 | sig++; |
| 9127 | if (!(mask & 1)) |
| 9128 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9129 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9130 | /* POSIX allows shell to re-enable SIGCHLD |
| 9131 | * even if it was SIG_IGN on entry. |
| 9132 | * Therefore we skip IGN check for it: |
| 9133 | */ |
| 9134 | if (sig == SIGCHLD) |
| 9135 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9136 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9137 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9138 | */ |
| 9139 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9140 | if (old_handler == SIG_IGN) { |
| 9141 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9142 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9143 | #if ENABLE_HUSH_TRAP |
| 9144 | if (!G_traps) |
| 9145 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9146 | free(G_traps[sig]); |
| 9147 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9148 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9149 | } |
| 9150 | } |
| 9151 | } |
| 9152 | |
| 9153 | /* Called a few times only (or even once if "sh -c") */ |
| 9154 | static void install_special_sighandlers(void) |
| 9155 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9156 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9157 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9158 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9159 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9160 | if (G_interactive_fd) { |
| 9161 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9162 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9163 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9164 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9165 | /* Careful, do not re-install handlers we already installed */ |
| 9166 | if (G.special_sig_mask != mask) { |
| 9167 | unsigned diff = mask & ~G.special_sig_mask; |
| 9168 | G.special_sig_mask = mask; |
| 9169 | install_sighandlers(diff); |
| 9170 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
| 9173 | #if ENABLE_HUSH_JOB |
| 9174 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9175 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9176 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9177 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9178 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9179 | |
| 9180 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9181 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9182 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9183 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9184 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9185 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9186 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9187 | + (1 << SIGABRT) |
| 9188 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9189 | + (1 << SIGPIPE) |
| 9190 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9191 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9192 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9193 | * we never want to restore pgrp on exit, and this fn is not called |
| 9194 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9195 | /*+ (1 << SIGHUP )*/ |
| 9196 | /*+ (1 << SIGTERM)*/ |
| 9197 | /*+ (1 << SIGINT )*/ |
| 9198 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9199 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9200 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9201 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9202 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9203 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9204 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9205 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9206 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9207 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9208 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9209 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9210 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9211 | break; |
| 9212 | case 'x': |
| 9213 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9214 | break; |
| 9215 | case 'o': |
| 9216 | if (!o_opt) { |
| 9217 | /* "set -+o" without parameter. |
| 9218 | * in bash, set -o produces this output: |
| 9219 | * pipefail off |
| 9220 | * and set +o: |
| 9221 | * set +o pipefail |
| 9222 | * We always use the second form. |
| 9223 | */ |
| 9224 | const char *p = o_opt_strings; |
| 9225 | idx = 0; |
| 9226 | while (*p) { |
| 9227 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9228 | idx++; |
| 9229 | p += strlen(p) + 1; |
| 9230 | } |
| 9231 | break; |
| 9232 | } |
| 9233 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9234 | if (idx >= 0) { |
| 9235 | G.o_opt[idx] = state; |
| 9236 | break; |
| 9237 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9238 | case 'e': |
| 9239 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9240 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9241 | default: |
| 9242 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9243 | } |
| 9244 | return EXIT_SUCCESS; |
| 9245 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9246 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9247 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9248 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9249 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9250 | enum { |
| 9251 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9252 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9253 | }; |
| 9254 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9255 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9256 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9257 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9258 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9259 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9260 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9261 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9262 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9263 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9264 | #if ENABLE_HUSH_FAST |
| 9265 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9266 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9267 | #if !BB_MMU |
| 9268 | G.argv0_for_re_execing = argv[0]; |
| 9269 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9270 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9271 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9272 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9273 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9274 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9275 | shell_ver->flg_export = 1; |
| 9276 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9277 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9278 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9279 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9280 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9281 | /* Create shell local variables from the values |
| 9282 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9283 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9284 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9285 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9286 | if (e) while (*e) { |
| 9287 | char *value = strchr(*e, '='); |
| 9288 | if (value) { /* paranoia */ |
| 9289 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9290 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9291 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9292 | cur_var->max_len = strlen(*e); |
| 9293 | cur_var->flg_export = 1; |
| 9294 | } |
| 9295 | e++; |
| 9296 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9297 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9298 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9299 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9300 | |
| 9301 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9302 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9303 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9304 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9305 | /* Set (but not export) PS1/2 unless already set */ |
| 9306 | if (!get_local_var_value("PS1")) |
| 9307 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9308 | if (!get_local_var_value("PS2")) |
| 9309 | set_local_var_from_halves("PS2", "> "); |
| 9310 | #endif |
| 9311 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9312 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9313 | /* Set (but not export) HOSTNAME unless already set */ |
| 9314 | if (!get_local_var_value("HOSTNAME")) { |
| 9315 | struct utsname uts; |
| 9316 | uname(&uts); |
| 9317 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9318 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9319 | /* bash also exports SHLVL and _, |
| 9320 | * and sets (but doesn't export) the following variables: |
| 9321 | * BASH=/bin/bash |
| 9322 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9323 | * BASH_VERSION='3.2.0(1)-release' |
| 9324 | * HOSTTYPE=i386 |
| 9325 | * MACHTYPE=i386-pc-linux-gnu |
| 9326 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9327 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9328 | * EUID=<NNNNN> |
| 9329 | * UID=<NNNNN> |
| 9330 | * GROUPS=() |
| 9331 | * LINES=<NNN> |
| 9332 | * COLUMNS=<NNN> |
| 9333 | * BASH_ARGC=() |
| 9334 | * BASH_ARGV=() |
| 9335 | * BASH_LINENO=() |
| 9336 | * BASH_SOURCE=() |
| 9337 | * DIRSTACK=() |
| 9338 | * PIPESTATUS=([0]="0") |
| 9339 | * HISTFILE=/<xxx>/.bash_history |
| 9340 | * HISTFILESIZE=500 |
| 9341 | * HISTSIZE=500 |
| 9342 | * MAILCHECK=60 |
| 9343 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9344 | * SHELL=/bin/bash |
| 9345 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9346 | * TERM=dumb |
| 9347 | * OPTERR=1 |
| 9348 | * OPTIND=1 |
| 9349 | * IFS=$' \t\n' |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9350 | * PS4='+ ' |
| 9351 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9352 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9353 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9354 | #if ENABLE_HUSH_LINENO_VAR |
| 9355 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9356 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9357 | set_local_var(p, /*flags*/ 0); |
| 9358 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9359 | } |
| 9360 | #endif |
| 9361 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9362 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9363 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9364 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9365 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9366 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9367 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9368 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9369 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9370 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9371 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9372 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9373 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9374 | * 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] | 9375 | * in order to intercept (more) signals. |
| 9376 | */ |
| 9377 | |
| 9378 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9379 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9380 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9381 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9382 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9383 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9384 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9385 | "<:$:R:V:" |
| 9386 | # if ENABLE_HUSH_FUNCTIONS |
| 9387 | "F:" |
| 9388 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9389 | #endif |
| 9390 | ); |
| 9391 | if (opt <= 0) |
| 9392 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9393 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9394 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9395 | /* Possibilities: |
| 9396 | * sh ... -c 'script' |
| 9397 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9398 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9399 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9400 | * "" needs to be replaced with NULL |
| 9401 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9402 | * Note: the form without ARG0 never happens: |
| 9403 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9404 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9405 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9406 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9407 | G.root_ppid = getppid(); |
| 9408 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9409 | G.global_argv = argv + optind; |
| 9410 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9411 | if (builtin_argc) { |
| 9412 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9413 | const struct built_in_command *x; |
| 9414 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9415 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9416 | x = find_builtin(optarg); |
| 9417 | if (x) { /* paranoia */ |
| 9418 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9419 | G.global_argv += builtin_argc; |
| 9420 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9421 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9422 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9423 | } |
| 9424 | goto final_return; |
| 9425 | } |
| 9426 | if (!G.global_argv[0]) { |
| 9427 | /* -c 'script' (no params): prevent empty $0 */ |
| 9428 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9429 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9430 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9431 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9432 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9433 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9434 | goto final_return; |
| 9435 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9436 | /* Well, we cannot just declare interactiveness, |
| 9437 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9438 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9439 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9440 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9441 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9442 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9443 | case 'l': |
| 9444 | flags |= OPT_login; |
| 9445 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9446 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9447 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9448 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9449 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9450 | case '$': { |
| 9451 | unsigned long long empty_trap_mask; |
| 9452 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9453 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9454 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9455 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9456 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9457 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9458 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9459 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9460 | optarg++; |
| 9461 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9462 | optarg++; |
| 9463 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9464 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9465 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9466 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9467 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9468 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9469 | for (sig = 1; sig < NSIG; sig++) { |
| 9470 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9471 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9472 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9473 | } |
| 9474 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9475 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9476 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9477 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9478 | optarg++; |
| 9479 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9480 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 9481 | # if ENABLE_HUSH_FUNCTIONS |
| 9482 | /* nommu uses re-exec trick for "... | func | ...", |
| 9483 | * should allow "return". |
| 9484 | * This accidentally allows returns in subshells. |
| 9485 | */ |
| 9486 | G_flag_return_in_progress = -1; |
| 9487 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9488 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9489 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9490 | case 'R': |
| 9491 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9492 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9493 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9494 | # if ENABLE_HUSH_FUNCTIONS |
| 9495 | case 'F': { |
| 9496 | struct function *funcp = new_function(optarg); |
| 9497 | /* funcp->name is already set to optarg */ |
| 9498 | /* funcp->body is set to NULL. It's a special case. */ |
| 9499 | funcp->body_as_string = argv[optind]; |
| 9500 | optind++; |
| 9501 | break; |
| 9502 | } |
| 9503 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9504 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9505 | case 'n': |
| 9506 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9507 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9508 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9509 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9510 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9511 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9512 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9513 | " or: sh -c command [args]...\n\n"); |
| 9514 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9515 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9516 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9517 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9518 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9519 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9520 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9521 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9522 | G.global_argc = argc - (optind - 1); |
| 9523 | G.global_argv = argv + (optind - 1); |
| 9524 | G.global_argv[0] = argv[0]; |
| 9525 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9526 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9527 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9528 | G.root_ppid = getppid(); |
| 9529 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9530 | |
| 9531 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9532 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9533 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9534 | debug_printf("sourcing /etc/profile\n"); |
| 9535 | input = fopen_for_read("/etc/profile"); |
| 9536 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9537 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9538 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9539 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9540 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9541 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9542 | /* bash: after sourcing /etc/profile, |
| 9543 | * tries to source (in the given order): |
| 9544 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9545 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9546 | * bash also sources ~/.bash_logout on exit. |
| 9547 | * If called as sh, skips .bash_XXX files. |
| 9548 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9549 | } |
| 9550 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9551 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 9552 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9553 | FILE *input; |
| 9554 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9555 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9556 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9557 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9558 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9559 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9560 | G.global_argc--; |
| 9561 | G.global_argv++; |
| 9562 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9563 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9564 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9565 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9566 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9567 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9568 | parse_and_run_file(input); |
| 9569 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9570 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9571 | #endif |
| 9572 | goto final_return; |
| 9573 | } |
| 9574 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9575 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9576 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9577 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9578 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9579 | /* A shell is interactive if the '-i' flag was given, |
| 9580 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9581 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9582 | * no arguments remaining or the -s flag given |
| 9583 | * standard input is a terminal |
| 9584 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9585 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9586 | */ |
| 9587 | #if ENABLE_HUSH_JOB |
| 9588 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9589 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9590 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9591 | if (G_saved_tty_pgrp < 0) |
| 9592 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9593 | |
| 9594 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9595 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9596 | if (G_interactive_fd < 0) { |
| 9597 | /* try to dup to any fd */ |
| 9598 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9599 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9600 | /* give up */ |
| 9601 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9602 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9603 | } |
| 9604 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9605 | // TODO: track & disallow any attempts of user |
| 9606 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9607 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9608 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9609 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9610 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9611 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9612 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9613 | /* If we were run as 'hush &', sleep until we are |
| 9614 | * in the foreground (tty pgrp == our pgrp). |
| 9615 | * If we get started under a job aware app (like bash), |
| 9616 | * make sure we are now in charge so we don't fight over |
| 9617 | * who gets the foreground */ |
| 9618 | while (1) { |
| 9619 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9620 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9621 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9622 | break; |
| 9623 | /* send TTIN to ourself (should stop us) */ |
| 9624 | kill(- shell_pgrp, SIGTTIN); |
| 9625 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9626 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9627 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9628 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9629 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9630 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9631 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9632 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9633 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9634 | /* Put ourselves in our own process group |
| 9635 | * (bash, too, does this only if ctty is available) */ |
| 9636 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9637 | /* Grab control of the terminal */ |
| 9638 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9639 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9640 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9641 | |
| 9642 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9643 | { |
| 9644 | const char *hp = get_local_var_value("HISTFILE"); |
| 9645 | if (!hp) { |
| 9646 | hp = get_local_var_value("HOME"); |
| 9647 | if (hp) |
| 9648 | hp = concat_path_file(hp, ".hush_history"); |
| 9649 | } else { |
| 9650 | hp = xstrdup(hp); |
| 9651 | } |
| 9652 | if (hp) { |
| 9653 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9654 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9655 | } |
| 9656 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9657 | hp = get_local_var_value("HISTFILESIZE"); |
| 9658 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9659 | # endif |
| 9660 | } |
| 9661 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9662 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9663 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9664 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9665 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9666 | /* No job control compiled in, only prompt/line editing */ |
| 9667 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9668 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9669 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9670 | /* try to dup to any fd */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9671 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9672 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9673 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9674 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9675 | } |
| 9676 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9677 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9678 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9679 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9680 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9681 | #else |
| 9682 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9683 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9684 | #endif |
| 9685 | /* bash: |
| 9686 | * if interactive but not a login shell, sources ~/.bashrc |
| 9687 | * (--norc turns this off, --rcfile <file> overrides) |
| 9688 | */ |
| 9689 | |
| 9690 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9691 | /* note: ash and hush share this string */ |
| 9692 | printf("\n\n%s %s\n" |
| 9693 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9694 | "\n", |
| 9695 | bb_banner, |
| 9696 | "hush - the humble shell" |
| 9697 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9698 | } |
| 9699 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9700 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9701 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9702 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9703 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9704 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9705 | |
| 9706 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9707 | /* |
| 9708 | * Built-ins |
| 9709 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9710 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9711 | { |
| 9712 | return 0; |
| 9713 | } |
| 9714 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9715 | #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] | 9716 | 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] | 9717 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9718 | int argc = string_array_len(argv); |
| 9719 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9720 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9721 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9722 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9723 | static int FAST_FUNC builtin_test(char **argv) |
| 9724 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9725 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9726 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9727 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9728 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9729 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9730 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9731 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9732 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9733 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9734 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9735 | static int FAST_FUNC builtin_printf(char **argv) |
| 9736 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9737 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9738 | } |
| 9739 | #endif |
| 9740 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9741 | #if ENABLE_HUSH_HELP |
| 9742 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9743 | { |
| 9744 | const struct built_in_command *x; |
| 9745 | |
| 9746 | printf( |
| 9747 | "Built-in commands:\n" |
| 9748 | "------------------\n"); |
| 9749 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9750 | if (x->b_descr) |
| 9751 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9752 | } |
| 9753 | return EXIT_SUCCESS; |
| 9754 | } |
| 9755 | #endif |
| 9756 | |
| 9757 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9758 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9759 | { |
| 9760 | show_history(G.line_input_state); |
| 9761 | return EXIT_SUCCESS; |
| 9762 | } |
| 9763 | #endif |
| 9764 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9765 | static char **skip_dash_dash(char **argv) |
| 9766 | { |
| 9767 | argv++; |
| 9768 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9769 | argv++; |
| 9770 | return argv; |
| 9771 | } |
| 9772 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9773 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9774 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9775 | const char *newdir; |
| 9776 | |
| 9777 | argv = skip_dash_dash(argv); |
| 9778 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9779 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9780 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9781 | * bash says "bash: cd: HOME not set" and does nothing |
| 9782 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9783 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9784 | const char *home = get_local_var_value("HOME"); |
| 9785 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9786 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9787 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9788 | /* Mimic bash message exactly */ |
| 9789 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9790 | return EXIT_FAILURE; |
| 9791 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9792 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9793 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9794 | * set it again, but do not export. bash does the same. |
| 9795 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9796 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9797 | return EXIT_SUCCESS; |
| 9798 | } |
| 9799 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9800 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9801 | { |
| 9802 | puts(get_cwd(0)); |
| 9803 | return EXIT_SUCCESS; |
| 9804 | } |
| 9805 | |
| 9806 | static int FAST_FUNC builtin_eval(char **argv) |
| 9807 | { |
| 9808 | int rcode = EXIT_SUCCESS; |
| 9809 | |
| 9810 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9811 | if (argv[0]) { |
| 9812 | char *str = NULL; |
| 9813 | |
| 9814 | if (argv[1]) { |
| 9815 | /* "The eval utility shall construct a command by |
| 9816 | * concatenating arguments together, separating |
| 9817 | * each with a <space> character." |
| 9818 | */ |
| 9819 | char *p; |
| 9820 | unsigned len = 0; |
| 9821 | char **pp = argv; |
| 9822 | do |
| 9823 | len += strlen(*pp) + 1; |
| 9824 | while (*++pp); |
| 9825 | str = p = xmalloc(len); |
| 9826 | pp = argv; |
| 9827 | do { |
| 9828 | p = stpcpy(p, *pp); |
| 9829 | *p++ = ' '; |
| 9830 | } while (*++pp); |
| 9831 | p[-1] = '\0'; |
| 9832 | } |
| 9833 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9834 | /* bash: |
| 9835 | * eval "echo Hi; done" ("done" is syntax error): |
| 9836 | * "echo Hi" will not execute too. |
| 9837 | */ |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9838 | parse_and_run_string(str ? str : argv[0]); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9839 | free(str); |
| 9840 | rcode = G.last_exitcode; |
| 9841 | } |
| 9842 | return rcode; |
| 9843 | } |
| 9844 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9845 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9846 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9847 | argv = skip_dash_dash(argv); |
| 9848 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9849 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9850 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9851 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9852 | * tty pgrp then, only top-level shell process does that */ |
| 9853 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9854 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9855 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9856 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9857 | * open here. However, they are all CLOEXEC, and execv below |
| 9858 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9859 | * it should show no extra open fds in the "ls" process. |
| 9860 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 9861 | */ |
| 9862 | //close_saved_fds_and_FILE_fds(); |
| 9863 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 9864 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9865 | * 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] | 9866 | * and tcsetpgrp, and this is inherently racy. |
| 9867 | */ |
| 9868 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9869 | } |
| 9870 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9871 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9872 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 9873 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9874 | |
| 9875 | /* interactive bash: |
| 9876 | * # trap "echo EEE" EXIT |
| 9877 | * # exit |
| 9878 | * exit |
| 9879 | * There are stopped jobs. |
| 9880 | * (if there are _stopped_ jobs, running ones don't count) |
| 9881 | * # exit |
| 9882 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 9883 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9884 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 9885 | * 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] | 9886 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 9887 | |
| 9888 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9889 | argv = skip_dash_dash(argv); |
| 9890 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9891 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9892 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 9893 | xfunc_error_retval = 255; |
| 9894 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9895 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9898 | #if ENABLE_HUSH_TYPE |
| 9899 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 9900 | static int FAST_FUNC builtin_type(char **argv) |
| 9901 | { |
| 9902 | int ret = EXIT_SUCCESS; |
| 9903 | |
| 9904 | while (*++argv) { |
| 9905 | const char *type; |
| 9906 | char *path = NULL; |
| 9907 | |
| 9908 | if (0) {} /* make conditional compile easier below */ |
| 9909 | /*else if (find_alias(*argv)) |
| 9910 | type = "an alias";*/ |
| 9911 | #if ENABLE_HUSH_FUNCTIONS |
| 9912 | else if (find_function(*argv)) |
| 9913 | type = "a function"; |
| 9914 | #endif |
| 9915 | else if (find_builtin(*argv)) |
| 9916 | type = "a shell builtin"; |
| 9917 | else if ((path = find_in_path(*argv)) != NULL) |
| 9918 | type = path; |
| 9919 | else { |
| 9920 | bb_error_msg("type: %s: not found", *argv); |
| 9921 | ret = EXIT_FAILURE; |
| 9922 | continue; |
| 9923 | } |
| 9924 | |
| 9925 | printf("%s is %s\n", *argv, type); |
| 9926 | free(path); |
| 9927 | } |
| 9928 | |
| 9929 | return ret; |
| 9930 | } |
| 9931 | #endif |
| 9932 | |
| 9933 | #if ENABLE_HUSH_READ |
| 9934 | /* Interruptibility of read builtin in bash |
| 9935 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9936 | * |
| 9937 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9938 | * |
| 9939 | * SIGINT: |
| 9940 | * - terminates non-interactive shell; |
| 9941 | * - interrupts read in interactive shell; |
| 9942 | * if it has non-empty trap: |
| 9943 | * - executes trap and returns to command prompt in interactive shell; |
| 9944 | * - executes trap and returns to read in non-interactive shell; |
| 9945 | * SIGTERM: |
| 9946 | * - is ignored (does not interrupt) read in interactive shell; |
| 9947 | * - terminates non-interactive shell; |
| 9948 | * if it has non-empty trap: |
| 9949 | * - executes trap and returns to read; |
| 9950 | * SIGHUP: |
| 9951 | * - terminates shell (regardless of interactivity); |
| 9952 | * if it has non-empty trap: |
| 9953 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9954 | * SIGCHLD from children: |
| 9955 | * - does not interrupt read regardless of interactivity: |
| 9956 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9957 | */ |
| 9958 | static int FAST_FUNC builtin_read(char **argv) |
| 9959 | { |
| 9960 | const char *r; |
| 9961 | char *opt_n = NULL; |
| 9962 | char *opt_p = NULL; |
| 9963 | char *opt_t = NULL; |
| 9964 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9965 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9966 | const char *ifs; |
| 9967 | int read_flags; |
| 9968 | |
| 9969 | /* "!": do not abort on errors. |
| 9970 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9971 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9972 | read_flags = getopt32(argv, |
| 9973 | #if BASH_READ_D |
| 9974 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 9975 | #else |
| 9976 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 9977 | #endif |
| 9978 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9979 | if (read_flags == (uint32_t)-1) |
| 9980 | return EXIT_FAILURE; |
| 9981 | argv += optind; |
| 9982 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9983 | |
| 9984 | again: |
| 9985 | r = shell_builtin_read(set_local_var_from_halves, |
| 9986 | argv, |
| 9987 | ifs, |
| 9988 | read_flags, |
| 9989 | opt_n, |
| 9990 | opt_p, |
| 9991 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9992 | opt_u, |
| 9993 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9994 | ); |
| 9995 | |
| 9996 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9997 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9998 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9999 | goto again; |
| 10000 | } |
| 10001 | |
| 10002 | if ((uintptr_t)r > 1) { |
| 10003 | bb_error_msg("%s", r); |
| 10004 | r = (char*)(uintptr_t)1; |
| 10005 | } |
| 10006 | |
| 10007 | return (uintptr_t)r; |
| 10008 | } |
| 10009 | #endif |
| 10010 | |
| 10011 | #if ENABLE_HUSH_UMASK |
| 10012 | static int FAST_FUNC builtin_umask(char **argv) |
| 10013 | { |
| 10014 | int rc; |
| 10015 | mode_t mask; |
| 10016 | |
| 10017 | rc = 1; |
| 10018 | mask = umask(0); |
| 10019 | argv = skip_dash_dash(argv); |
| 10020 | if (argv[0]) { |
| 10021 | mode_t old_mask = mask; |
| 10022 | |
| 10023 | /* numeric umasks are taken as-is */ |
| 10024 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 10025 | if (!isdigit(argv[0][0])) |
| 10026 | mask ^= 0777; |
| 10027 | mask = bb_parse_mode(argv[0], mask); |
| 10028 | if (!isdigit(argv[0][0])) |
| 10029 | mask ^= 0777; |
| 10030 | if ((unsigned)mask > 0777) { |
| 10031 | mask = old_mask; |
| 10032 | /* bash messages: |
| 10033 | * bash: umask: 'q': invalid symbolic mode operator |
| 10034 | * bash: umask: 999: octal number out of range |
| 10035 | */ |
| 10036 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 10037 | rc = 0; |
| 10038 | } |
| 10039 | } else { |
| 10040 | /* Mimic bash */ |
| 10041 | printf("%04o\n", (unsigned) mask); |
| 10042 | /* fall through and restore mask which we set to 0 */ |
| 10043 | } |
| 10044 | umask(mask); |
| 10045 | |
| 10046 | return !rc; /* rc != 0 - success */ |
| 10047 | } |
| 10048 | #endif |
| 10049 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10050 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10051 | static void print_escaped(const char *s) |
| 10052 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10053 | if (*s == '\'') |
| 10054 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10055 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10056 | const char *p = strchrnul(s, '\''); |
| 10057 | /* print 'xxxx', possibly just '' */ |
| 10058 | printf("'%.*s'", (int)(p - s), s); |
| 10059 | if (*p == '\0') |
| 10060 | break; |
| 10061 | s = p; |
| 10062 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10063 | /* s points to '; print "'''...'''" */ |
| 10064 | putchar('"'); |
| 10065 | do putchar('\''); while (*++s == '\''); |
| 10066 | putchar('"'); |
| 10067 | } while (*s); |
| 10068 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10069 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10070 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10071 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10072 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10073 | { |
| 10074 | do { |
| 10075 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10076 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10077 | |
| 10078 | /* So far we do not check that name is valid (TODO?) */ |
| 10079 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10080 | if (*name_end == '\0') { |
| 10081 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10082 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10083 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10084 | var = vpp ? *vpp : NULL; |
| 10085 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10086 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10087 | /* export -n NAME (without =VALUE) */ |
| 10088 | if (var) { |
| 10089 | var->flg_export = 0; |
| 10090 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10091 | unsetenv(name); |
| 10092 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10093 | continue; |
| 10094 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10095 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10096 | /* export NAME (without =VALUE) */ |
| 10097 | if (var) { |
| 10098 | var->flg_export = 1; |
| 10099 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10100 | putenv(var->varstr); |
| 10101 | continue; |
| 10102 | } |
| 10103 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10104 | if (flags & SETFLAG_MAKE_RO) { |
| 10105 | /* readonly NAME (without =VALUE) */ |
| 10106 | if (var) { |
| 10107 | var->flg_read_only = 1; |
| 10108 | continue; |
| 10109 | } |
| 10110 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10111 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10112 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10113 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10114 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10115 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10116 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10117 | continue; |
| 10118 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10119 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10120 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10121 | /* Exporting non-existing variable. |
| 10122 | * bash does not put it in environment, |
| 10123 | * but remembers that it is exported, |
| 10124 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10125 | * We just set it to "" and export. |
| 10126 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10127 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10128 | * bash sets the value to "". |
| 10129 | */ |
| 10130 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10131 | * bash remembers NAME and disallows its creation |
| 10132 | * in the future. |
| 10133 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10134 | name = xasprintf("%s=", name); |
| 10135 | } else { |
| 10136 | /* (Un)exporting/making local NAME=VALUE */ |
| 10137 | name = xstrdup(name); |
| 10138 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10139 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10140 | if (set_local_var(name, flags)) |
| 10141 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10142 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10143 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10144 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10145 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10146 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10147 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10148 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10149 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10150 | unsigned opt_unexport; |
| 10151 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10152 | #if ENABLE_HUSH_EXPORT_N |
| 10153 | /* "!": do not abort on errors */ |
| 10154 | opt_unexport = getopt32(argv, "!n"); |
| 10155 | if (opt_unexport == (uint32_t)-1) |
| 10156 | return EXIT_FAILURE; |
| 10157 | argv += optind; |
| 10158 | #else |
| 10159 | opt_unexport = 0; |
| 10160 | argv++; |
| 10161 | #endif |
| 10162 | |
| 10163 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10164 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10165 | if (e) { |
| 10166 | while (*e) { |
| 10167 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10168 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10169 | #else |
| 10170 | /* ash emits: export VAR='VAL' |
| 10171 | * bash: declare -x VAR="VAL" |
| 10172 | * we follow ash example */ |
| 10173 | const char *s = *e++; |
| 10174 | const char *p = strchr(s, '='); |
| 10175 | |
| 10176 | if (!p) /* wtf? take next variable */ |
| 10177 | continue; |
| 10178 | /* export var= */ |
| 10179 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10180 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10181 | putchar('\n'); |
| 10182 | #endif |
| 10183 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10184 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10185 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10186 | return EXIT_SUCCESS; |
| 10187 | } |
| 10188 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10189 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10190 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10191 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10192 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10193 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10194 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10195 | { |
| 10196 | if (G.func_nest_level == 0) { |
| 10197 | bb_error_msg("%s: not in a function", argv[0]); |
| 10198 | return EXIT_FAILURE; /* bash compat */ |
| 10199 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10200 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10201 | /* Since all builtins run in a nested variable level, |
| 10202 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10203 | * after builtin returns. |
| 10204 | */ |
| 10205 | 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] | 10206 | } |
| 10207 | #endif |
| 10208 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10209 | #if ENABLE_HUSH_READONLY |
| 10210 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10211 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10212 | argv++; |
| 10213 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10214 | /* bash: readonly [-p]: list all readonly VARs |
| 10215 | * (-p has no effect in bash) |
| 10216 | */ |
| 10217 | struct variable *e; |
| 10218 | for (e = G.top_var; e; e = e->next) { |
| 10219 | if (e->flg_read_only) { |
| 10220 | //TODO: quote value: readonly VAR='VAL' |
| 10221 | printf("readonly %s\n", e->varstr); |
| 10222 | } |
| 10223 | } |
| 10224 | return EXIT_SUCCESS; |
| 10225 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10226 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10227 | } |
| 10228 | #endif |
| 10229 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10230 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10231 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10232 | static int FAST_FUNC builtin_unset(char **argv) |
| 10233 | { |
| 10234 | int ret; |
| 10235 | unsigned opts; |
| 10236 | |
| 10237 | /* "!": do not abort on errors */ |
| 10238 | /* "+": stop at 1st non-option */ |
| 10239 | opts = getopt32(argv, "!+vf"); |
| 10240 | if (opts == (unsigned)-1) |
| 10241 | return EXIT_FAILURE; |
| 10242 | if (opts == 3) { |
| 10243 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10244 | return EXIT_FAILURE; |
| 10245 | } |
| 10246 | argv += optind; |
| 10247 | |
| 10248 | ret = EXIT_SUCCESS; |
| 10249 | while (*argv) { |
| 10250 | if (!(opts & 2)) { /* not -f */ |
| 10251 | if (unset_local_var(*argv)) { |
| 10252 | /* unset <nonexistent_var> doesn't fail. |
| 10253 | * Error is when one tries to unset RO var. |
| 10254 | * Message was printed by unset_local_var. */ |
| 10255 | ret = EXIT_FAILURE; |
| 10256 | } |
| 10257 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10258 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10259 | else { |
| 10260 | unset_func(*argv); |
| 10261 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10262 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10263 | argv++; |
| 10264 | } |
| 10265 | return ret; |
| 10266 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10267 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10268 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10269 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10270 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10271 | * built-in 'set' handler |
| 10272 | * SUSv3 says: |
| 10273 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10274 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10275 | * set -- [argument...] |
| 10276 | * set -o |
| 10277 | * set +o |
| 10278 | * Implementations shall support the options in both their hyphen and |
| 10279 | * plus-sign forms. These options can also be specified as options to sh. |
| 10280 | * Examples: |
| 10281 | * Write out all variables and their values: set |
| 10282 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10283 | * Turn on the -x and -v options: set -xv |
| 10284 | * Unset all positional parameters: set -- |
| 10285 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10286 | * Set the positional parameters to the expansion of x, even if x expands |
| 10287 | * with a leading '-' or '+': set -- $x |
| 10288 | * |
| 10289 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10290 | */ |
| 10291 | static int FAST_FUNC builtin_set(char **argv) |
| 10292 | { |
| 10293 | int n; |
| 10294 | char **pp, **g_argv; |
| 10295 | char *arg = *++argv; |
| 10296 | |
| 10297 | if (arg == NULL) { |
| 10298 | struct variable *e; |
| 10299 | for (e = G.top_var; e; e = e->next) |
| 10300 | puts(e->varstr); |
| 10301 | return EXIT_SUCCESS; |
| 10302 | } |
| 10303 | |
| 10304 | do { |
| 10305 | if (strcmp(arg, "--") == 0) { |
| 10306 | ++argv; |
| 10307 | goto set_argv; |
| 10308 | } |
| 10309 | if (arg[0] != '+' && arg[0] != '-') |
| 10310 | break; |
| 10311 | for (n = 1; arg[n]; ++n) { |
| 10312 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10313 | goto error; |
| 10314 | if (arg[n] == 'o' && argv[1]) |
| 10315 | argv++; |
| 10316 | } |
| 10317 | } while ((arg = *++argv) != NULL); |
| 10318 | /* Now argv[0] is 1st argument */ |
| 10319 | |
| 10320 | if (arg == NULL) |
| 10321 | return EXIT_SUCCESS; |
| 10322 | set_argv: |
| 10323 | |
| 10324 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10325 | g_argv = G.global_argv; |
| 10326 | if (G.global_args_malloced) { |
| 10327 | pp = g_argv; |
| 10328 | while (*++pp) |
| 10329 | free(*pp); |
| 10330 | g_argv[1] = NULL; |
| 10331 | } else { |
| 10332 | G.global_args_malloced = 1; |
| 10333 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10334 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10335 | g_argv = pp; |
| 10336 | } |
| 10337 | /* This realloc's G.global_argv */ |
| 10338 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10339 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10340 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10341 | |
| 10342 | return EXIT_SUCCESS; |
| 10343 | |
| 10344 | /* Nothing known, so abort */ |
| 10345 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10346 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10347 | return EXIT_FAILURE; |
| 10348 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10349 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10350 | |
| 10351 | static int FAST_FUNC builtin_shift(char **argv) |
| 10352 | { |
| 10353 | int n = 1; |
| 10354 | argv = skip_dash_dash(argv); |
| 10355 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10356 | n = bb_strtou(argv[0], NULL, 10); |
| 10357 | if (errno || n < 0) { |
| 10358 | /* shared string with ash.c */ |
| 10359 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10360 | /* |
| 10361 | * ash aborts in this case. |
| 10362 | * bash prints error message and set $? to 1. |
| 10363 | * Interestingly, for "shift 99999" bash does not |
| 10364 | * print error message, but does set $? to 1 |
| 10365 | * (and does no shifting at all). |
| 10366 | */ |
| 10367 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10368 | } |
| 10369 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10370 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10371 | int m = 1; |
| 10372 | while (m <= n) |
| 10373 | free(G.global_argv[m++]); |
| 10374 | } |
| 10375 | G.global_argc -= n; |
| 10376 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10377 | G.global_argc * sizeof(G.global_argv[0])); |
| 10378 | return EXIT_SUCCESS; |
| 10379 | } |
| 10380 | return EXIT_FAILURE; |
| 10381 | } |
| 10382 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10383 | #if ENABLE_HUSH_GETOPTS |
| 10384 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10385 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10386 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10387 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10388 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10389 | If a required argument is not found, and getopts is not silent, |
| 10390 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10391 | diagnostic message is printed. If getopts is silent, then a |
| 10392 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10393 | character found. |
| 10394 | |
| 10395 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10396 | |
| 10397 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10398 | */ |
| 10399 | char cbuf[2]; |
| 10400 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10401 | int c, n, exitcode, my_opterr; |
| 10402 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10403 | |
| 10404 | optstring = *++argv; |
| 10405 | if (!optstring || !(var = *++argv)) { |
| 10406 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10407 | return EXIT_FAILURE; |
| 10408 | } |
| 10409 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10410 | if (argv[1]) |
| 10411 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10412 | else |
| 10413 | argv = G.global_argv; |
| 10414 | cbuf[1] = '\0'; |
| 10415 | |
| 10416 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10417 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10418 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10419 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10420 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10421 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10422 | |
| 10423 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10424 | /*if (optstring[0] != '+')*/ { |
| 10425 | char *s = alloca(strlen(optstring) + 2); |
| 10426 | sprintf(s, "+%s", optstring); |
| 10427 | optstring = s; |
| 10428 | } |
| 10429 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10430 | /* Naively, now we should just |
| 10431 | * cp = get_local_var_value("OPTIND"); |
| 10432 | * optind = cp ? atoi(cp) : 0; |
| 10433 | * optarg = NULL; |
| 10434 | * opterr = my_opterr; |
| 10435 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10436 | * and be done? Not so fast... |
| 10437 | * Unlike normal getopt() usage in C programs, here |
| 10438 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10439 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10440 | * invocations of "getopts", there will be calls to shell builtins |
| 10441 | * which use getopt() internally. Example: |
| 10442 | * while getopts "abc" RES -a -bc -abc de; do |
| 10443 | * unset -ff func |
| 10444 | * done |
| 10445 | * This would not work correctly: getopt() call inside "unset" |
| 10446 | * modifies internal libc state which is tracking position in |
| 10447 | * multi-option strings ("-abc"). At best, it can skip options |
| 10448 | * or return the same option infinitely. With glibc implementation |
| 10449 | * of getopt(), it would use outright invalid pointers and return |
| 10450 | * garbage even _without_ "unset" mangling internal state. |
| 10451 | * |
| 10452 | * We resort to resetting getopt() state and calling it N times, |
| 10453 | * until we get Nth result (or failure). |
| 10454 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10455 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10456 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10457 | count = 0; |
| 10458 | n = string_array_len(argv); |
| 10459 | do { |
| 10460 | optarg = NULL; |
| 10461 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10462 | c = getopt(n, argv, optstring); |
| 10463 | if (c < 0) |
| 10464 | break; |
| 10465 | count++; |
| 10466 | } while (count <= G.getopt_count); |
| 10467 | |
| 10468 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10469 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10470 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10471 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10472 | |
| 10473 | /* Set OPTARG */ |
| 10474 | /* Always set or unset, never left as-is, even on exit/error: |
| 10475 | * "If no option was found, or if the option that was found |
| 10476 | * does not have an option-argument, OPTARG shall be unset." |
| 10477 | */ |
| 10478 | cp = optarg; |
| 10479 | if (c == '?') { |
| 10480 | /* If ":optstring" and unknown option is seen, |
| 10481 | * it is stored to OPTARG. |
| 10482 | */ |
| 10483 | if (optstring[1] == ':') { |
| 10484 | cbuf[0] = optopt; |
| 10485 | cp = cbuf; |
| 10486 | } |
| 10487 | } |
| 10488 | if (cp) |
| 10489 | set_local_var_from_halves("OPTARG", cp); |
| 10490 | else |
| 10491 | unset_local_var("OPTARG"); |
| 10492 | |
| 10493 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10494 | exitcode = EXIT_SUCCESS; |
| 10495 | if (c < 0) { /* -1: end of options */ |
| 10496 | exitcode = EXIT_FAILURE; |
| 10497 | c = '?'; |
| 10498 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10499 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10500 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10501 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10502 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10503 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10504 | return exitcode; |
| 10505 | } |
| 10506 | #endif |
| 10507 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10508 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10509 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10510 | char *arg_path, *filename; |
| 10511 | FILE *input; |
| 10512 | save_arg_t sv; |
| 10513 | char *args_need_save; |
| 10514 | #if ENABLE_HUSH_FUNCTIONS |
| 10515 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10516 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10517 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10518 | argv = skip_dash_dash(argv); |
| 10519 | filename = argv[0]; |
| 10520 | if (!filename) { |
| 10521 | /* bash says: "bash: .: filename argument required" */ |
| 10522 | return 2; /* bash compat */ |
| 10523 | } |
| 10524 | arg_path = NULL; |
| 10525 | if (!strchr(filename, '/')) { |
| 10526 | arg_path = find_in_path(filename); |
| 10527 | if (arg_path) |
| 10528 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10529 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10530 | errno = ENOENT; |
| 10531 | bb_simple_perror_msg(filename); |
| 10532 | return EXIT_FAILURE; |
| 10533 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10534 | } |
| 10535 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10536 | free(arg_path); |
| 10537 | if (!input) { |
| 10538 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10539 | /* POSIX: non-interactive shell should abort here, |
| 10540 | * not merely fail. So far no one complained :) |
| 10541 | */ |
| 10542 | return EXIT_FAILURE; |
| 10543 | } |
| 10544 | |
| 10545 | #if ENABLE_HUSH_FUNCTIONS |
| 10546 | sv_flg = G_flag_return_in_progress; |
| 10547 | /* "we are inside sourced file, ok to use return" */ |
| 10548 | G_flag_return_in_progress = -1; |
| 10549 | #endif |
| 10550 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10551 | if (args_need_save) |
| 10552 | save_and_replace_G_args(&sv, argv); |
| 10553 | |
| 10554 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10555 | G.last_exitcode = 0; |
| 10556 | parse_and_run_file(input); |
| 10557 | fclose_and_forget(input); |
| 10558 | |
| 10559 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10560 | restore_G_args(&sv, argv); |
| 10561 | #if ENABLE_HUSH_FUNCTIONS |
| 10562 | G_flag_return_in_progress = sv_flg; |
| 10563 | #endif |
| 10564 | |
| 10565 | return G.last_exitcode; |
| 10566 | } |
| 10567 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10568 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10569 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10570 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10571 | int sig; |
| 10572 | char *new_cmd; |
| 10573 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10574 | if (!G_traps) |
| 10575 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10576 | |
| 10577 | argv++; |
| 10578 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10579 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10580 | /* No args: print all trapped */ |
| 10581 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10582 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10583 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10584 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10585 | /* note: bash adds "SIG", but only if invoked |
| 10586 | * as "bash". If called as "sh", or if set -o posix, |
| 10587 | * then it prints short signal names. |
| 10588 | * We are printing short names: */ |
| 10589 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10590 | } |
| 10591 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10592 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10593 | return EXIT_SUCCESS; |
| 10594 | } |
| 10595 | |
| 10596 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10597 | /* If first arg is a number: reset all specified signals */ |
| 10598 | sig = bb_strtou(*argv, NULL, 10); |
| 10599 | if (errno == 0) { |
| 10600 | int ret; |
| 10601 | process_sig_list: |
| 10602 | ret = EXIT_SUCCESS; |
| 10603 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10604 | sighandler_t handler; |
| 10605 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10606 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10607 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10608 | ret = EXIT_FAILURE; |
| 10609 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10610 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10611 | continue; |
| 10612 | } |
| 10613 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10614 | free(G_traps[sig]); |
| 10615 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10616 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10617 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10618 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10619 | |
| 10620 | /* There is no signal for 0 (EXIT) */ |
| 10621 | if (sig == 0) |
| 10622 | continue; |
| 10623 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10624 | if (new_cmd) |
| 10625 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10626 | else |
| 10627 | /* We are removing trap handler */ |
| 10628 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10629 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10630 | } |
| 10631 | return ret; |
| 10632 | } |
| 10633 | |
| 10634 | if (!argv[1]) { /* no second arg */ |
| 10635 | bb_error_msg("trap: invalid arguments"); |
| 10636 | return EXIT_FAILURE; |
| 10637 | } |
| 10638 | |
| 10639 | /* First arg is "-": reset all specified to default */ |
| 10640 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10641 | /* Everything else: set arg as signal handler |
| 10642 | * (includes "" case, which ignores signal) */ |
| 10643 | if (argv[0][0] == '-') { |
| 10644 | if (argv[0][1] == '\0') { /* "-" */ |
| 10645 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10646 | goto reset_traps; |
| 10647 | } |
| 10648 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10649 | argv++; |
| 10650 | } |
| 10651 | /* else: "-something", no special meaning */ |
| 10652 | } |
| 10653 | new_cmd = *argv; |
| 10654 | reset_traps: |
| 10655 | argv++; |
| 10656 | goto process_sig_list; |
| 10657 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10658 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10659 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10660 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10661 | static struct pipe *parse_jobspec(const char *str) |
| 10662 | { |
| 10663 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10664 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10665 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10666 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10667 | if (str[0] != '%' |
| 10668 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10669 | ) { |
| 10670 | bb_error_msg("bad argument '%s'", str); |
| 10671 | return NULL; |
| 10672 | } |
| 10673 | /* It is "%%", "%+" or "%" - current job */ |
| 10674 | jobnum = G.last_jobid; |
| 10675 | if (jobnum == 0) { |
| 10676 | bb_error_msg("no current job"); |
| 10677 | return NULL; |
| 10678 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10679 | } |
| 10680 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10681 | if (pi->jobid == jobnum) { |
| 10682 | return pi; |
| 10683 | } |
| 10684 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10685 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10686 | return NULL; |
| 10687 | } |
| 10688 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10689 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10690 | { |
| 10691 | struct pipe *job; |
| 10692 | const char *status_string; |
| 10693 | |
| 10694 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10695 | for (job = G.job_list; job; job = job->next) { |
| 10696 | if (job->alive_cmds == job->stopped_cmds) |
| 10697 | status_string = "Stopped"; |
| 10698 | else |
| 10699 | status_string = "Running"; |
| 10700 | |
| 10701 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10702 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10703 | |
| 10704 | clean_up_last_dead_job(); |
| 10705 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10706 | return EXIT_SUCCESS; |
| 10707 | } |
| 10708 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10709 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10710 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10711 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10712 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10713 | struct pipe *pi; |
| 10714 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10715 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10716 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10717 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10718 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10719 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10720 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10721 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10722 | goto found; |
| 10723 | } |
| 10724 | } |
| 10725 | bb_error_msg("%s: no current job", argv[0]); |
| 10726 | return EXIT_FAILURE; |
| 10727 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10728 | |
| 10729 | pi = parse_jobspec(argv[1]); |
| 10730 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10731 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10732 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10733 | /* TODO: bash prints a string representation |
| 10734 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10735 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10736 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10737 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10738 | } |
| 10739 | |
| 10740 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10741 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10742 | for (i = 0; i < pi->num_cmds; i++) { |
| 10743 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10744 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10745 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10746 | |
| 10747 | i = kill(- pi->pgrp, SIGCONT); |
| 10748 | if (i < 0) { |
| 10749 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10750 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10751 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10752 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10753 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10754 | } |
| 10755 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10756 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10757 | 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] | 10758 | return checkjobs_and_fg_shell(pi); |
| 10759 | } |
| 10760 | return EXIT_SUCCESS; |
| 10761 | } |
| 10762 | #endif |
| 10763 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10764 | #if ENABLE_HUSH_KILL |
| 10765 | static int FAST_FUNC builtin_kill(char **argv) |
| 10766 | { |
| 10767 | int ret = 0; |
| 10768 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10769 | # if ENABLE_HUSH_JOB |
| 10770 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10771 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10772 | |
| 10773 | do { |
| 10774 | struct pipe *pi; |
| 10775 | char *dst; |
| 10776 | int j, n; |
| 10777 | |
| 10778 | if (argv[i][0] != '%') |
| 10779 | continue; |
| 10780 | /* |
| 10781 | * "kill %N" - job kill |
| 10782 | * Converting to pgrp / pid kill |
| 10783 | */ |
| 10784 | pi = parse_jobspec(argv[i]); |
| 10785 | if (!pi) { |
| 10786 | /* Eat bad jobspec */ |
| 10787 | j = i; |
| 10788 | do { |
| 10789 | j++; |
| 10790 | argv[j - 1] = argv[j]; |
| 10791 | } while (argv[j]); |
| 10792 | ret = 1; |
| 10793 | i--; |
| 10794 | continue; |
| 10795 | } |
| 10796 | /* |
| 10797 | * In jobs started under job control, we signal |
| 10798 | * entire process group by kill -PGRP_ID. |
| 10799 | * This happens, f.e., in interactive shell. |
| 10800 | * |
| 10801 | * Otherwise, we signal each child via |
| 10802 | * kill PID1 PID2 PID3. |
| 10803 | * Testcases: |
| 10804 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10805 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10806 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10807 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10808 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10809 | dst = alloca(n * sizeof(int)*4); |
| 10810 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10811 | if (G_interactive_fd) |
| 10812 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10813 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10814 | struct command *cmd = &pi->cmds[j]; |
| 10815 | /* Skip exited members of the job */ |
| 10816 | if (cmd->pid == 0) |
| 10817 | continue; |
| 10818 | /* |
| 10819 | * kill_main has matching code to expect |
| 10820 | * leading space. Needed to not confuse |
| 10821 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10822 | */ |
| 10823 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10824 | } |
| 10825 | *dst = '\0'; |
| 10826 | } while (argv[++i]); |
| 10827 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10828 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10829 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10830 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10831 | ret = run_applet_main(argv, kill_main); |
| 10832 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10833 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10834 | return ret; |
| 10835 | } |
| 10836 | #endif |
| 10837 | |
| 10838 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10839 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10840 | #if !ENABLE_HUSH_JOB |
| 10841 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10842 | #endif |
| 10843 | 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] | 10844 | { |
| 10845 | int ret = 0; |
| 10846 | for (;;) { |
| 10847 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10848 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10849 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10850 | if (!sigisemptyset(&G.pending_set)) |
| 10851 | goto check_sig; |
| 10852 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10853 | /* waitpid is not interruptible by SA_RESTARTed |
| 10854 | * signals which we use. Thus, this ugly dance: |
| 10855 | */ |
| 10856 | |
| 10857 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10858 | * pending signal mask before we call waitpid. |
| 10859 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10860 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10861 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10862 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 10863 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10864 | |
| 10865 | if (!sigisemptyset(&G.pending_set)) { |
| 10866 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10867 | goto restore; |
| 10868 | } |
| 10869 | |
| 10870 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10871 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10872 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10873 | debug_printf_exec("checkjobs:%d\n", ret); |
| 10874 | #if ENABLE_HUSH_JOB |
| 10875 | if (waitfor_pipe) { |
| 10876 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 10877 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 10878 | if (rcode >= 0) { |
| 10879 | ret = rcode; |
| 10880 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10881 | break; |
| 10882 | } |
| 10883 | } |
| 10884 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10885 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 10886 | /* if ret == 0, no children changed state */ |
| 10887 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10888 | if (errno == ECHILD || ret) { |
| 10889 | ret--; |
| 10890 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10891 | ret = 0; |
| 10892 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10893 | break; |
| 10894 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10895 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10896 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 10897 | /* Note: sigsuspend invokes signal handler */ |
| 10898 | sigsuspend(&oldset); |
| 10899 | restore: |
| 10900 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10901 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10902 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10903 | sig = check_and_run_traps(); |
| 10904 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 10905 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10906 | ret = 128 + sig; |
| 10907 | break; |
| 10908 | } |
| 10909 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 10910 | } |
| 10911 | return ret; |
| 10912 | } |
| 10913 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10914 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10915 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10916 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10917 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10918 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10919 | argv = skip_dash_dash(argv); |
| 10920 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 10921 | /* Don't care about wait results */ |
| 10922 | /* Note 1: must wait until there are no more children */ |
| 10923 | /* Note 2: must be interruptible */ |
| 10924 | /* Examples: |
| 10925 | * $ sleep 3 & sleep 6 & wait |
| 10926 | * [1] 30934 sleep 3 |
| 10927 | * [2] 30935 sleep 6 |
| 10928 | * [1] Done sleep 3 |
| 10929 | * [2] Done sleep 6 |
| 10930 | * $ sleep 3 & sleep 6 & wait |
| 10931 | * [1] 30936 sleep 3 |
| 10932 | * [2] 30937 sleep 6 |
| 10933 | * [1] Done sleep 3 |
| 10934 | * ^C <-- after ~4 sec from keyboard |
| 10935 | * $ |
| 10936 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10937 | 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] | 10938 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10939 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10940 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10941 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10942 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10943 | #if ENABLE_HUSH_JOB |
| 10944 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10945 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10946 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10947 | wait_pipe = parse_jobspec(*argv); |
| 10948 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10949 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10950 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10951 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10952 | } else { |
| 10953 | /* waiting on "last dead job" removes it */ |
| 10954 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 10955 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10956 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10957 | /* else: parse_jobspec() already emitted error msg */ |
| 10958 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10959 | } |
| 10960 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10961 | /* mimic bash message */ |
| 10962 | 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] | 10963 | ret = EXIT_FAILURE; |
| 10964 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10965 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10966 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10967 | /* Do we have such child? */ |
| 10968 | ret = waitpid(pid, &status, WNOHANG); |
| 10969 | if (ret < 0) { |
| 10970 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10971 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10972 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 10973 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10974 | /* "wait $!" but last bg task has already exited. Try: |
| 10975 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 10976 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10977 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10978 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10979 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10980 | } else { |
| 10981 | /* Example: "wait 1". mimic bash message */ |
| 10982 | 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] | 10983 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10984 | } else { |
| 10985 | /* ??? */ |
| 10986 | bb_perror_msg("wait %s", *argv); |
| 10987 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10988 | continue; /* bash checks all argv[] */ |
| 10989 | } |
| 10990 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10991 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10992 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10993 | } else { |
| 10994 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10995 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 10996 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10997 | if (WIFSIGNALED(status)) |
| 10998 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10999 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11000 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11001 | |
| 11002 | return ret; |
| 11003 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11004 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11005 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11006 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 11007 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 11008 | { |
| 11009 | if (argv[1]) { |
| 11010 | def = bb_strtou(argv[1], NULL, 10); |
| 11011 | if (errno || def < def_min || argv[2]) { |
| 11012 | bb_error_msg("%s: bad arguments", argv[0]); |
| 11013 | def = UINT_MAX; |
| 11014 | } |
| 11015 | } |
| 11016 | return def; |
| 11017 | } |
| 11018 | #endif |
| 11019 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11020 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11021 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11022 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11023 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11024 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11025 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11026 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 11027 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 11028 | return EXIT_SUCCESS; /* bash compat */ |
| 11029 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11030 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11031 | |
| 11032 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 11033 | if (depth == UINT_MAX) |
| 11034 | G.flag_break_continue = BC_BREAK; |
| 11035 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11036 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11037 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11038 | return EXIT_SUCCESS; |
| 11039 | } |
| 11040 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11041 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11042 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11043 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 11044 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11045 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11046 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11047 | |
| 11048 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11049 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11050 | { |
| 11051 | int rc; |
| 11052 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11053 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11054 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 11055 | return EXIT_FAILURE; /* bash compat */ |
| 11056 | } |
| 11057 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11058 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11059 | |
| 11060 | /* bash: |
| 11061 | * out of range: wraps around at 256, does not error out |
| 11062 | * non-numeric param: |
| 11063 | * f() { false; return qwe; }; f; echo $? |
| 11064 | * bash: return: qwe: numeric argument required <== we do this |
| 11065 | * 255 <== we also do this |
| 11066 | */ |
| 11067 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 11068 | return rc; |
| 11069 | } |
| 11070 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11071 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11072 | #if ENABLE_HUSH_TIMES |
| 11073 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11074 | { |
| 11075 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11076 | ' ', offsetof(struct tms, tms_utime), |
| 11077 | '\n', offsetof(struct tms, tms_stime), |
| 11078 | ' ', offsetof(struct tms, tms_cutime), |
| 11079 | '\n', offsetof(struct tms, tms_cstime), |
| 11080 | 0 |
| 11081 | }; |
| 11082 | const uint8_t *p; |
| 11083 | unsigned clk_tck; |
| 11084 | struct tms buf; |
| 11085 | |
| 11086 | clk_tck = bb_clk_tck(); |
| 11087 | |
| 11088 | times(&buf); |
| 11089 | p = times_tbl; |
| 11090 | do { |
| 11091 | unsigned sec, frac; |
| 11092 | unsigned long t; |
| 11093 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11094 | sec = t / clk_tck; |
| 11095 | frac = t % clk_tck; |
| 11096 | printf("%um%u.%03us%c", |
| 11097 | sec / 60, sec % 60, |
| 11098 | (frac * 1000) / clk_tck, |
| 11099 | p[0]); |
| 11100 | p += 2; |
| 11101 | } while (*p); |
| 11102 | |
| 11103 | return EXIT_SUCCESS; |
| 11104 | } |
| 11105 | #endif |
| 11106 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11107 | #if ENABLE_HUSH_MEMLEAK |
| 11108 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11109 | { |
| 11110 | void *p; |
| 11111 | unsigned long l; |
| 11112 | |
| 11113 | # ifdef M_TRIM_THRESHOLD |
| 11114 | /* Optional. Reduces probability of false positives */ |
| 11115 | malloc_trim(0); |
| 11116 | # endif |
| 11117 | /* Crude attempt to find where "free memory" starts, |
| 11118 | * sans fragmentation. */ |
| 11119 | p = malloc(240); |
| 11120 | l = (unsigned long)p; |
| 11121 | free(p); |
| 11122 | p = malloc(3400); |
| 11123 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11124 | free(p); |
| 11125 | |
| 11126 | |
| 11127 | # if 0 /* debug */ |
| 11128 | { |
| 11129 | struct mallinfo mi = mallinfo(); |
| 11130 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11131 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11132 | } |
| 11133 | # endif |
| 11134 | |
| 11135 | if (!G.memleak_value) |
| 11136 | G.memleak_value = l; |
| 11137 | |
| 11138 | l -= G.memleak_value; |
| 11139 | if ((long)l < 0) |
| 11140 | l = 0; |
| 11141 | l /= 1024; |
| 11142 | if (l > 127) |
| 11143 | l = 127; |
| 11144 | |
| 11145 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11146 | return l; |
| 11147 | } |
| 11148 | #endif |