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: */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [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 */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 444 | #define debug_printf_parse(...) do {} while (0) |
| 445 | #define debug_printf_heredoc(...) do {} while (0) |
| 446 | #define debug_print_tree(a, b) do {} while (0) |
| 447 | #define debug_printf_exec(...) do {} while (0) |
| 448 | #define debug_printf_env(...) do {} while (0) |
| 449 | #define debug_printf_jobs(...) do {} while (0) |
| 450 | #define debug_printf_expand(...) do {} while (0) |
| 451 | #define debug_printf_varexp(...) do {} while (0) |
| 452 | #define debug_printf_glob(...) do {} while (0) |
| 453 | #define debug_printf_redir(...) do {} while (0) |
| 454 | #define debug_printf_list(...) do {} while (0) |
| 455 | #define debug_printf_subst(...) do {} while (0) |
| 456 | #define debug_printf_prompt(...) do {} while (0) |
| 457 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 459 | #define ERR_PTR ((void*)(long)1) |
| 460 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 461 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 462 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 463 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 464 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 465 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 466 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 467 | /* Support / and // replace ops */ |
| 468 | /* Note that // is stored as \ in "encoded" string representation */ |
| 469 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 470 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 471 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 472 | #else |
| 473 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 474 | # define VAR_SUBST_OPS "%#:-=+?" |
| 475 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 476 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 477 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 478 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 479 | #define SPECIAL_VAR_SYMBOL 3 |
| 480 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 481 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 482 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 483 | struct variable; |
| 484 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 485 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 486 | |
| 487 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 488 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 489 | */ |
| 490 | #if !BB_MMU |
| 491 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 492 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 493 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 494 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 495 | } nommu_save_t; |
| 496 | #endif |
| 497 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 498 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 499 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 500 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 501 | RES_IF , |
| 502 | RES_THEN , |
| 503 | RES_ELIF , |
| 504 | RES_ELSE , |
| 505 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 506 | #endif |
| 507 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 508 | RES_FOR , |
| 509 | RES_WHILE , |
| 510 | RES_UNTIL , |
| 511 | RES_DO , |
| 512 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 513 | #endif |
| 514 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 515 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 516 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 517 | #if ENABLE_HUSH_CASE |
| 518 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 519 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 520 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 521 | RES_MATCH , /* "word)" */ |
| 522 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 523 | RES_ESAC , |
| 524 | #endif |
| 525 | RES_XXXX , |
| 526 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 527 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 528 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 529 | typedef struct o_string { |
| 530 | char *data; |
| 531 | int length; /* position where data is appended */ |
| 532 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 533 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 534 | /* At least some part of the string was inside '' or "", |
| 535 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 536 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 537 | smallint has_empty_slot; |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 538 | smallint ended_in_ifs; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 539 | } o_string; |
| 540 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 541 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 542 | EXP_FLAG_GLOB = 0x2, |
| 543 | /* Protect newly added chars against globbing |
| 544 | * by prepending \ to *, ?, [, \ */ |
| 545 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 546 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 547 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 548 | #define NULL_O_STRING { NULL } |
| 549 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 550 | #ifndef debug_printf_parse |
| 551 | static const char *const assignment_flag[] = { |
| 552 | "MAYBE_ASSIGNMENT", |
| 553 | "DEFINITELY_ASSIGNMENT", |
| 554 | "NOT_ASSIGNMENT", |
| 555 | "WORD_IS_KEYWORD", |
| 556 | }; |
| 557 | #endif |
| 558 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 559 | typedef struct in_str { |
| 560 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 561 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 562 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 563 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 564 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 565 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 566 | /* The descrip member of this structure is only used to make |
| 567 | * debugging output pretty */ |
| 568 | static const struct { |
| 569 | int mode; |
| 570 | signed char default_fd; |
| 571 | char descrip[3]; |
| 572 | } redir_table[] = { |
| 573 | { O_RDONLY, 0, "<" }, |
| 574 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 575 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 576 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 577 | { O_RDONLY, 0, "<<" }, |
| 578 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 579 | /* { O_RDONLY, 77, "<<" }, */ |
| 580 | }; |
| 581 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 582 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 583 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 584 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 585 | int rd_fd; /* fd to redirect */ |
| 586 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 587 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 588 | smallint rd_type; /* (enum redir_type) */ |
| 589 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 590 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 591 | * bit 0: do we need to trim leading tabs? |
| 592 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 593 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 594 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 595 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 596 | REDIRECT_INPUT = 0, |
| 597 | REDIRECT_OVERWRITE = 1, |
| 598 | REDIRECT_APPEND = 2, |
| 599 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 600 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 601 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 602 | |
| 603 | REDIRFD_CLOSE = -3, |
| 604 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 605 | REDIRFD_TO_FILE = -1, |
| 606 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 607 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 608 | HEREDOC_SKIPTABS = 1, |
| 609 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 610 | } redir_type; |
| 611 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 612 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 613 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 614 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 615 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 616 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 617 | unsigned lineno; |
| 618 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 619 | smallint cmd_type; /* CMD_xxx */ |
| 620 | #define CMD_NORMAL 0 |
| 621 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 622 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 623 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 624 | * "export v=t*" |
| 625 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 626 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 627 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 628 | #if ENABLE_HUSH_FUNCTIONS |
| 629 | # define CMD_FUNCDEF 3 |
| 630 | #endif |
| 631 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 632 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 633 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 634 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 635 | #if !BB_MMU |
| 636 | char *group_as_string; |
| 637 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 638 | #if ENABLE_HUSH_FUNCTIONS |
| 639 | struct function *child_func; |
| 640 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 641 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 642 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 643 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 644 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 645 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 646 | * we put those fields back into cmd->xxx |
| 647 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 648 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 649 | * Without this trick, loop would execute a;b;b;b;... |
| 650 | * instead of correct sequence a;b;a;b;... |
| 651 | * When command is freed, it severs the link |
| 652 | * (sets ->child_func->parent_cmd to NULL). |
| 653 | */ |
| 654 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 655 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 656 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 657 | * and on execution these are substituted with their values. |
| 658 | * Substitution can make _several_ words out of one argv[n]! |
| 659 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 660 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 661 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 662 | struct redir_struct *redirects; /* I/O redirections */ |
| 663 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 664 | /* Is there anything in this command at all? */ |
| 665 | #define IS_NULL_CMD(cmd) \ |
| 666 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 667 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 668 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 669 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 670 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 671 | int alive_cmds; /* number of commands running (not exited) */ |
| 672 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 673 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 674 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 675 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 676 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 677 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 678 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 679 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 680 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 681 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 682 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 683 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 684 | PIPE_SEQ = 0, |
| 685 | PIPE_AND = 1, |
| 686 | PIPE_OR = 2, |
| 687 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 688 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 689 | /* Is there anything in this pipe at all? */ |
| 690 | #define IS_NULL_PIPE(pi) \ |
| 691 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 692 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 693 | /* This holds pointers to the various results of parsing */ |
| 694 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 695 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 696 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 697 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 698 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 699 | /* last command in pipe (being constructed right now) */ |
| 700 | struct command *command; |
| 701 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 702 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 703 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 704 | #if !BB_MMU |
| 705 | o_string as_string; |
| 706 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 707 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 708 | #if HAS_KEYWORDS |
| 709 | smallint ctx_res_w; |
| 710 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 711 | #if ENABLE_HUSH_CASE |
| 712 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 713 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 714 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 715 | int old_flag; |
| 716 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 717 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 718 | * when we see "if" or "then", we malloc and copy current context, |
| 719 | * and make ->stack point to it. then we parse pipeN. |
| 720 | * when closing "then" / fi" / whatever is found, |
| 721 | * we move list_head into ->stack->command->group, |
| 722 | * copy ->stack into current context, and delete ->stack. |
| 723 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 724 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 725 | struct parse_context *stack; |
| 726 | #endif |
| 727 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 728 | enum { |
| 729 | MAYBE_ASSIGNMENT = 0, |
| 730 | DEFINITELY_ASSIGNMENT = 1, |
| 731 | NOT_ASSIGNMENT = 2, |
| 732 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 733 | WORD_IS_KEYWORD = 3, |
| 734 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 735 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 736 | /* On program start, environ points to initial environment. |
| 737 | * putenv adds new pointers into it, unsetenv removes them. |
| 738 | * Neither of these (de)allocates the strings. |
| 739 | * setenv allocates new strings in malloc space and does putenv, |
| 740 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 741 | #define setenv(...) setenv_is_leaky_dont_use() |
| 742 | struct variable { |
| 743 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 744 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 745 | 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] | 746 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 747 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 748 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 749 | }; |
| 750 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 751 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 752 | BC_BREAK = 1, |
| 753 | BC_CONTINUE = 2, |
| 754 | }; |
| 755 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 756 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 757 | struct function { |
| 758 | struct function *next; |
| 759 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 760 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 761 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 762 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 763 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 764 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 765 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 766 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 767 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 768 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 769 | /* set -/+o OPT support. (TODO: make it optional) |
| 770 | * bash supports the following opts: |
| 771 | * allexport off |
| 772 | * braceexpand on |
| 773 | * emacs on |
| 774 | * errexit off |
| 775 | * errtrace off |
| 776 | * functrace off |
| 777 | * hashall on |
| 778 | * histexpand off |
| 779 | * history on |
| 780 | * ignoreeof off |
| 781 | * interactive-comments on |
| 782 | * keyword off |
| 783 | * monitor on |
| 784 | * noclobber off |
| 785 | * noexec off |
| 786 | * noglob off |
| 787 | * nolog off |
| 788 | * notify off |
| 789 | * nounset off |
| 790 | * onecmd off |
| 791 | * physical off |
| 792 | * pipefail off |
| 793 | * posix off |
| 794 | * privileged off |
| 795 | * verbose off |
| 796 | * vi off |
| 797 | * xtrace off |
| 798 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 799 | static const char o_opt_strings[] ALIGN1 = |
| 800 | "pipefail\0" |
| 801 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 802 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 803 | #if ENABLE_HUSH_MODE_X |
| 804 | "xtrace\0" |
| 805 | #endif |
| 806 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 807 | enum { |
| 808 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 809 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 810 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 811 | #if ENABLE_HUSH_MODE_X |
| 812 | OPT_O_XTRACE, |
| 813 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 814 | NUM_OPT_O |
| 815 | }; |
| 816 | |
| 817 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 818 | struct FILE_list { |
| 819 | struct FILE_list *next; |
| 820 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 821 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 822 | }; |
| 823 | |
| 824 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 825 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 826 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 827 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 828 | /* interactive_fd != 0 means we are an interactive shell. |
| 829 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 830 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 831 | * job control still works, but terminal signals |
| 832 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 833 | * process groups can only be created with "cmd &". |
| 834 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 835 | * to give tty to the foreground process group, |
| 836 | * and will take it back when the group is stopped (^Z) |
| 837 | * or killed (^C). |
| 838 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 839 | #if ENABLE_HUSH_INTERACTIVE |
| 840 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 841 | * _AND_ if we decided to act interactively */ |
| 842 | int interactive_fd; |
| 843 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 844 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 845 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 846 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 847 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 848 | #endif |
| 849 | #if ENABLE_FEATURE_EDITING |
| 850 | line_input_t *line_input_state; |
| 851 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 852 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 853 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 854 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 855 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 856 | random_t random_gen; |
| 857 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 858 | #if ENABLE_HUSH_JOB |
| 859 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 860 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 861 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 862 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 863 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 864 | #else |
| 865 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 866 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 867 | /* How deeply are we in context where "set -e" is ignored */ |
| 868 | int errexit_depth; |
| 869 | /* "set -e" rules (do we follow them correctly?): |
| 870 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 871 | * Shell does not exit if failed command is part of condition in |
| 872 | * if/while, part of && or || list except the last command, any command |
| 873 | * in a pipe but the last, or if the command's return value is being |
| 874 | * inverted with !. If a compound command other than a subshell returns a |
| 875 | * non-zero status because a command failed while -e was being ignored, the |
| 876 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 877 | * exits [ERR is a bashism]. |
| 878 | * |
| 879 | * If a compound command or function executes in a context where -e is |
| 880 | * ignored, none of the commands executed within are affected by the -e |
| 881 | * setting. If a compound command or function sets -e while executing in a |
| 882 | * context where -e is ignored, that setting does not have any effect until |
| 883 | * the compound command or the command containing the function call completes. |
| 884 | */ |
| 885 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 886 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 887 | #if ENABLE_HUSH_MODE_X |
| 888 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 889 | #else |
| 890 | # define G_x_mode 0 |
| 891 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 892 | #if ENABLE_HUSH_INTERACTIVE |
| 893 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 894 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 895 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 896 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 897 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 898 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 899 | #if ENABLE_HUSH_FUNCTIONS |
| 900 | /* 0: outside of a function (or sourced file) |
| 901 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 902 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 903 | */ |
| 904 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 905 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 906 | #else |
| 907 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 908 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 909 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 910 | /* These support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 911 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 912 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 913 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 914 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 915 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 916 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 917 | # define G_global_args_malloced (G.global_args_malloced) |
| 918 | #else |
| 919 | # define G_global_args_malloced 0 |
| 920 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 921 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 922 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 923 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 924 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 925 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 926 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 927 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 928 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 929 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 930 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 931 | #if ENABLE_HUSH_GETOPTS |
| 932 | unsigned getopt_count; |
| 933 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 934 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 935 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 936 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 937 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 938 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 939 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 940 | unsigned var_nest_level; |
| 941 | #if ENABLE_HUSH_FUNCTIONS |
| 942 | # if ENABLE_HUSH_LOCAL |
| 943 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 944 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 945 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 946 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 947 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 948 | #if ENABLE_HUSH_FAST |
| 949 | unsigned count_SIGCHLD; |
| 950 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 951 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 952 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 953 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 954 | unsigned lineno; |
| 955 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 956 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 957 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 958 | /* Which signals have non-DFL handler (even with no traps set)? |
| 959 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 960 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 961 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 962 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 963 | * Other than these two times, never modified. |
| 964 | */ |
| 965 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 966 | #if ENABLE_HUSH_JOB |
| 967 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 968 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 969 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 970 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 971 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 972 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 973 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 974 | # define G_traps G.traps |
| 975 | #else |
| 976 | # define G_traps ((char**)NULL) |
| 977 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 978 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 979 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 980 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 981 | #endif |
| 982 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 983 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 984 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 985 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 986 | #if ENABLE_FEATURE_EDITING |
| 987 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 988 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 989 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 990 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 991 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 992 | * (too many defines). Also, I actually prefer to see when a variable |
| 993 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 994 | #define INIT_G() do { \ |
| 995 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 996 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 997 | sigfillset(&G.sa.sa_mask); \ |
| 998 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 999 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1000 | |
| 1001 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1002 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1003 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1004 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1005 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1006 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1007 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1008 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1009 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1010 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1011 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1012 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1013 | #if ENABLE_HUSH_READONLY |
| 1014 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1015 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1016 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1017 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1018 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1019 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1020 | #if ENABLE_HUSH_GETOPTS |
| 1021 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1022 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1023 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1024 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1025 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1026 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1027 | static int builtin_history(char **argv) FAST_FUNC; |
| 1028 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1029 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1030 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1031 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1032 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1033 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1034 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1035 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1036 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1037 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1038 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1039 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1040 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1041 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1042 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1043 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1044 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1045 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1046 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1047 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1048 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1049 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1050 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1051 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1052 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1053 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1054 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1055 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1056 | #if ENABLE_HUSH_TIMES |
| 1057 | static int builtin_times(char **argv) FAST_FUNC; |
| 1058 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1059 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1060 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1061 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1062 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1063 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1064 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1065 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1066 | #if ENABLE_HUSH_KILL |
| 1067 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1068 | #endif |
| 1069 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1070 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1071 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1072 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1073 | static int builtin_break(char **argv) FAST_FUNC; |
| 1074 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1075 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1076 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1077 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1078 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* Table of built-in functions. They can be forked or not, depending on |
| 1081 | * context: within pipes, they fork. As simple commands, they do not. |
| 1082 | * When used in non-forking context, they can change global variables |
| 1083 | * in the parent shell process. If forked, of course they cannot. |
| 1084 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1085 | * still be set at the end. */ |
| 1086 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1087 | const char *b_cmd; |
| 1088 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1089 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1090 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1091 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1092 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1093 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | }; |
| 1096 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1097 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1098 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1099 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1100 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1101 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1102 | #endif |
| 1103 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1104 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1105 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1106 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1107 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1108 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1109 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1110 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1111 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1112 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1113 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1114 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1115 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1116 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1117 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1118 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1119 | #if ENABLE_HUSH_GETOPTS |
| 1120 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1121 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1122 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1123 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1124 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1125 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1126 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1127 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1128 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1129 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1130 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1131 | #if ENABLE_HUSH_KILL |
| 1132 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1133 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1134 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1135 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1136 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1137 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1138 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1139 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1140 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1141 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1142 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1143 | #if ENABLE_HUSH_READONLY |
| 1144 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1145 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1146 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1147 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1148 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1149 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1150 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1151 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1152 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1153 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1154 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1155 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1156 | #if ENABLE_HUSH_TIMES |
| 1157 | BLTIN("times" , builtin_times , NULL), |
| 1158 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1159 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1160 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1161 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1162 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1163 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1164 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1165 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1166 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1167 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1168 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1169 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1170 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1171 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1172 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1173 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1174 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1175 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1176 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1177 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1178 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1179 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1180 | * (it's cheaper to run an external program in this case): |
| 1181 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1182 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1183 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1184 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1185 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1186 | #if BASH_TEST2 |
| 1187 | BLTIN("[[" , builtin_test , NULL), |
| 1188 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1189 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1190 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1191 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1192 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1193 | BLTIN("printf" , builtin_printf , NULL), |
| 1194 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1195 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1196 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1197 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1198 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1201 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1202 | /* Debug printouts. |
| 1203 | */ |
| 1204 | #if HUSH_DEBUG |
| 1205 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1206 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1207 | # define debug_enter() (G.debug_indent++) |
| 1208 | # define debug_leave() (G.debug_indent--) |
| 1209 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1210 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1211 | # define debug_enter() ((void)0) |
| 1212 | # define debug_leave() ((void)0) |
| 1213 | #endif |
| 1214 | |
| 1215 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1216 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1217 | #endif |
| 1218 | |
| 1219 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1220 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1221 | #endif |
| 1222 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 1223 | #ifndef debug_printf_heredoc |
| 1224 | # define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1225 | #endif |
| 1226 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1227 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1228 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1229 | #endif |
| 1230 | |
| 1231 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1232 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | |
| 1235 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1236 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1237 | # define DEBUG_JOBS 1 |
| 1238 | #else |
| 1239 | # define DEBUG_JOBS 0 |
| 1240 | #endif |
| 1241 | |
| 1242 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1243 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1244 | # define DEBUG_EXPAND 1 |
| 1245 | #else |
| 1246 | # define DEBUG_EXPAND 0 |
| 1247 | #endif |
| 1248 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1249 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1250 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1251 | #endif |
| 1252 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1253 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1254 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1255 | # define DEBUG_GLOB 1 |
| 1256 | #else |
| 1257 | # define DEBUG_GLOB 0 |
| 1258 | #endif |
| 1259 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1260 | #ifndef debug_printf_redir |
| 1261 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1262 | #endif |
| 1263 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1264 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1265 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1266 | #endif |
| 1267 | |
| 1268 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1269 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1270 | #endif |
| 1271 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1272 | #ifndef debug_printf_prompt |
| 1273 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1274 | #endif |
| 1275 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1276 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1277 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1278 | # define DEBUG_CLEAN 1 |
| 1279 | #else |
| 1280 | # define DEBUG_CLEAN 0 |
| 1281 | #endif |
| 1282 | |
| 1283 | #if DEBUG_EXPAND |
| 1284 | static void debug_print_strings(const char *prefix, char **vv) |
| 1285 | { |
| 1286 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1287 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1288 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1289 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1290 | } |
| 1291 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1292 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | |
| 1295 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1296 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1297 | */ |
| 1298 | #if LEAK_HUNTING |
| 1299 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1300 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1301 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1302 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1303 | return ptr; |
| 1304 | } |
| 1305 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1306 | { |
| 1307 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1308 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1309 | return ptr; |
| 1310 | } |
| 1311 | static char *xxstrdup(int lineno, const char *str) |
| 1312 | { |
| 1313 | char *ptr = xstrdup(str); |
| 1314 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1315 | return ptr; |
| 1316 | } |
| 1317 | static void xxfree(void *ptr) |
| 1318 | { |
| 1319 | fdprintf(2, "free %p\n", ptr); |
| 1320 | free(ptr); |
| 1321 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1322 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1323 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1324 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1325 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | |
| 1328 | |
| 1329 | /* Syntax and runtime errors. They always abort scripts. |
| 1330 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1331 | * and return to the prompt. |
| 1332 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1333 | */ |
| 1334 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1335 | # 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] | 1336 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1337 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1338 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1339 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1340 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1343 | static void die_if_script(void) |
| 1344 | { |
| 1345 | if (!G_interactive_fd) { |
| 1346 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1347 | xfunc_error_retval = G.last_exitcode; |
| 1348 | xfunc_die(); |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1353 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1354 | va_list p; |
| 1355 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1356 | #if HUSH_DEBUG >= 2 |
| 1357 | bb_error_msg("hush.c:%u", lineno); |
| 1358 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1359 | va_start(p, fmt); |
| 1360 | bb_verror_msg(fmt, p, NULL); |
| 1361 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1362 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1363 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1364 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1365 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1366 | { |
| 1367 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1368 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1369 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1370 | bb_error_msg("syntax error"); |
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_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1375 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1376 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1377 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1380 | 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] | 1381 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1382 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1383 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1384 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1387 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1388 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1389 | char msg[2] = { ch, '\0' }; |
| 1390 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1393 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1394 | { |
| 1395 | char msg[2]; |
| 1396 | msg[0] = ch; |
| 1397 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1398 | #if HUSH_DEBUG >= 2 |
| 1399 | bb_error_msg("hush.c:%u", lineno); |
| 1400 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1401 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1402 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1405 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1406 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1407 | # undef syntax_error |
| 1408 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1409 | # undef syntax_error_unterm_ch |
| 1410 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1411 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1412 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1413 | # 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] | 1414 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1415 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1416 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1417 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1418 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1419 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1420 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1421 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1422 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1423 | static void cmdedit_update_prompt(void); |
| 1424 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1425 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1426 | #endif |
| 1427 | |
| 1428 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1429 | /* Utility functions |
| 1430 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1431 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1432 | static char *unbackslash(char *src) |
| 1433 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1434 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1435 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1436 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1437 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1438 | if (*src != '\0') { |
| 1439 | /* \x -> x */ |
| 1440 | *dst++ = *src++; |
| 1441 | continue; |
| 1442 | } |
| 1443 | /* else: "\<nul>". Do not delete this backslash. |
| 1444 | * Testcase: eval 'echo ok\' |
| 1445 | */ |
| 1446 | *dst++ = '\\'; |
| 1447 | /* fallthrough */ |
| 1448 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1449 | if ((*dst++ = *src++) == '\0') |
| 1450 | break; |
| 1451 | } |
| 1452 | return dst; |
| 1453 | } |
| 1454 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1455 | 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] | 1456 | { |
| 1457 | int i; |
| 1458 | unsigned count1; |
| 1459 | unsigned count2; |
| 1460 | char **v; |
| 1461 | |
| 1462 | v = strings; |
| 1463 | count1 = 0; |
| 1464 | if (v) { |
| 1465 | while (*v) { |
| 1466 | count1++; |
| 1467 | v++; |
| 1468 | } |
| 1469 | } |
| 1470 | count2 = 0; |
| 1471 | v = add; |
| 1472 | while (*v) { |
| 1473 | count2++; |
| 1474 | v++; |
| 1475 | } |
| 1476 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1477 | v[count1 + count2] = NULL; |
| 1478 | i = count2; |
| 1479 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1480 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1481 | return v; |
| 1482 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1483 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1484 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1485 | { |
| 1486 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1487 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1488 | return ptr; |
| 1489 | } |
| 1490 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1491 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1492 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1493 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1494 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1495 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1496 | { |
| 1497 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1498 | v[0] = add; |
| 1499 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1500 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1501 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1502 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1503 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1504 | { |
| 1505 | char **ptr = add_string_to_strings(strings, add); |
| 1506 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1507 | return ptr; |
| 1508 | } |
| 1509 | #define add_string_to_strings(strings, add) \ |
| 1510 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1511 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1512 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1513 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1514 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1515 | char **v; |
| 1516 | |
| 1517 | if (!strings) |
| 1518 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1519 | v = strings; |
| 1520 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1521 | free(*v); |
| 1522 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1523 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1524 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1527 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1528 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1529 | int newfd; |
| 1530 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1531 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1532 | if (newfd >= 0) { |
| 1533 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1534 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1535 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1536 | if (errno == EBUSY) |
| 1537 | goto repeat; |
| 1538 | if (errno == EINTR) |
| 1539 | goto repeat; |
| 1540 | } |
| 1541 | return newfd; |
| 1542 | } |
| 1543 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1544 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1545 | { |
| 1546 | int newfd; |
| 1547 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1548 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1549 | if (newfd < 0) { |
| 1550 | if (errno == EBUSY) |
| 1551 | goto repeat; |
| 1552 | if (errno == EINTR) |
| 1553 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1554 | /* fd was not open? */ |
| 1555 | if (errno == EBADF) |
| 1556 | return fd; |
| 1557 | xfunc_die(); |
| 1558 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1559 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1560 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1561 | close(fd); |
| 1562 | return newfd; |
| 1563 | } |
| 1564 | |
| 1565 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1566 | /* Manipulating the list of open FILEs */ |
| 1567 | static FILE *remember_FILE(FILE *fp) |
| 1568 | { |
| 1569 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1570 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1571 | n->next = G.FILE_list; |
| 1572 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1573 | n->fp = fp; |
| 1574 | n->fd = fileno(fp); |
| 1575 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1576 | } |
| 1577 | return fp; |
| 1578 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1579 | static void fclose_and_forget(FILE *fp) |
| 1580 | { |
| 1581 | struct FILE_list **pp = &G.FILE_list; |
| 1582 | while (*pp) { |
| 1583 | struct FILE_list *cur = *pp; |
| 1584 | if (cur->fp == fp) { |
| 1585 | *pp = cur->next; |
| 1586 | free(cur); |
| 1587 | break; |
| 1588 | } |
| 1589 | pp = &cur->next; |
| 1590 | } |
| 1591 | fclose(fp); |
| 1592 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1593 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1594 | { |
| 1595 | struct FILE_list *fl = G.FILE_list; |
| 1596 | while (fl) { |
| 1597 | if (fd == fl->fd) { |
| 1598 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1599 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1600 | 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] | 1601 | return 1; |
| 1602 | } |
| 1603 | fl = fl->next; |
| 1604 | } |
| 1605 | return 0; |
| 1606 | } |
| 1607 | static void restore_redirected_FILEs(void) |
| 1608 | { |
| 1609 | struct FILE_list *fl = G.FILE_list; |
| 1610 | while (fl) { |
| 1611 | int should_be = fileno(fl->fp); |
| 1612 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1613 | 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] | 1614 | xmove_fd(fl->fd, should_be); |
| 1615 | fl->fd = should_be; |
| 1616 | } |
| 1617 | fl = fl->next; |
| 1618 | } |
| 1619 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1620 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1621 | static void close_all_FILE_list(void) |
| 1622 | { |
| 1623 | struct FILE_list *fl = G.FILE_list; |
| 1624 | while (fl) { |
| 1625 | /* fclose would also free FILE object. |
| 1626 | * It is disastrous if we share memory with a vforked parent. |
| 1627 | * I'm not sure we never come here after vfork. |
| 1628 | * Therefore just close fd, nothing more. |
| 1629 | */ |
| 1630 | /*fclose(fl->fp); - unsafe */ |
| 1631 | close(fl->fd); |
| 1632 | fl = fl->next; |
| 1633 | } |
| 1634 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1635 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1636 | static int fd_in_FILEs(int fd) |
| 1637 | { |
| 1638 | struct FILE_list *fl = G.FILE_list; |
| 1639 | while (fl) { |
| 1640 | if (fl->fd == fd) |
| 1641 | return 1; |
| 1642 | fl = fl->next; |
| 1643 | } |
| 1644 | return 0; |
| 1645 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1646 | |
| 1647 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1648 | /* Helpers for setting new $n and restoring them back |
| 1649 | */ |
| 1650 | typedef struct save_arg_t { |
| 1651 | char *sv_argv0; |
| 1652 | char **sv_g_argv; |
| 1653 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1654 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1655 | } save_arg_t; |
| 1656 | |
| 1657 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1658 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1659 | sv->sv_argv0 = argv[0]; |
| 1660 | sv->sv_g_argv = G.global_argv; |
| 1661 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1662 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1663 | |
| 1664 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1665 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1666 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1667 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1668 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1672 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1673 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1674 | if (G.global_args_malloced) { |
| 1675 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1676 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1677 | while (*++pp) /* note: does not free $0 */ |
| 1678 | free(*pp); |
| 1679 | free(G.global_argv); |
| 1680 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1681 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1682 | argv[0] = sv->sv_argv0; |
| 1683 | G.global_argv = sv->sv_g_argv; |
| 1684 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1685 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1689 | /* Basic theory of signal handling in shell |
| 1690 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1691 | * This does not describe what hush does, rather, it is current understanding |
| 1692 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1693 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1694 | * |
| 1695 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1696 | * is finished or backgrounded. It is the same in interactive and |
| 1697 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1698 | * 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] | 1699 | * ^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] | 1700 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1701 | * pipe. |
| 1702 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1703 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1704 | * or by SIGINT in interactive shell. |
| 1705 | * |
| 1706 | * Trap handlers will execute even within trap handlers. (right?) |
| 1707 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1708 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1709 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1710 | * |
| 1711 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1712 | * have SIGINT, SIGQUIT set to SIG_IGN. |
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 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1715 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1716 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1717 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1718 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1719 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1720 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1721 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1722 | * |
| 1723 | * SIGQUIT: ignore |
| 1724 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1725 | * SIGHUP (interactive): |
| 1726 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1727 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1728 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1729 | * that all pipe members are stopped. Try this in bash: |
| 1730 | * while :; do :; done - ^Z does not background it |
| 1731 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1732 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1733 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1734 | * to interactive shell while shell is waiting for a pipe, |
| 1735 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1736 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1737 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1738 | * Example 2: this does not wait and does not execute ls: |
| 1739 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1740 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1741 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1742 | * Example 4: this does not wait and does not execute ls: |
| 1743 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1744 | * |
| 1745 | * (What happens to signals which are IGN on shell start?) |
| 1746 | * (What happens with signal mask on shell start?) |
| 1747 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1748 | * Old implementation |
| 1749 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1750 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1751 | * We block all signals which we don't want to take action immediately, |
| 1752 | * i.e. we block all signals which need to have special handling as described |
| 1753 | * above, and all signals which have traps set. |
| 1754 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1755 | * and act on them. |
| 1756 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1757 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1758 | * sigset_t blocked_set: current blocked signal set |
| 1759 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1760 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1761 | * 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] | 1762 | * "trap 'cmd' SIGxxx": |
| 1763 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1764 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1765 | * unblock signals with special interactive handling |
| 1766 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1767 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1768 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1769 | * 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] | 1770 | * 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] | 1771 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1772 | * 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] | 1773 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1774 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1775 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1776 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1777 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1778 | * 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] | 1779 | * 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] | 1780 | * |
| 1781 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1782 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1783 | * masked signals are not visible! |
| 1784 | * |
| 1785 | * New implementation |
| 1786 | * ================== |
| 1787 | * We record each signal we are interested in by installing signal handler |
| 1788 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1789 | * We are interested in: signals which need to have special handling |
| 1790 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1791 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1792 | * After each pipe execution, we extract any pending signals |
| 1793 | * and act on them. |
| 1794 | * |
| 1795 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1796 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1797 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1798 | * sigset_t pending_set: set of sigs we received. |
| 1799 | * |
| 1800 | * "trap - SIGxxx": |
| 1801 | * if sig is in special_sig_mask, set handler back to: |
| 1802 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1803 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1804 | * else: set handler back to SIG_DFL |
| 1805 | * "trap 'cmd' SIGxxx": |
| 1806 | * set handler to record_pending_signo. |
| 1807 | * "trap '' SIGxxx": |
| 1808 | * set handler to SIG_IGN. |
| 1809 | * after [v]fork, if we plan to be a shell: |
| 1810 | * set signals with special interactive handling to SIG_DFL |
| 1811 | * (because child shell is not interactive), |
| 1812 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1813 | * after [v]fork, if we plan to exec: |
| 1814 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1815 | * |
| 1816 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1817 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1818 | * |
| 1819 | * Note (compat): |
| 1820 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1821 | * are set to the default actions". bash interprets it so that traps which |
| 1822 | * 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] | 1823 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1824 | enum { |
| 1825 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1826 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1827 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1828 | | (1 << SIGHUP) |
| 1829 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1830 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1831 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1832 | | (1 << SIGTTIN) |
| 1833 | | (1 << SIGTTOU) |
| 1834 | | (1 << SIGTSTP) |
| 1835 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1836 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1837 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1838 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1839 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1840 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1841 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1842 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1843 | if (sig == SIGCHLD) { |
| 1844 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1845 | //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] | 1846 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1847 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1848 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1849 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1850 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1851 | { |
| 1852 | struct sigaction old_sa; |
| 1853 | |
| 1854 | /* We could use signal() to install handlers... almost: |
| 1855 | * except that we need to mask ALL signals while handlers run. |
| 1856 | * I saw signal nesting in strace, race window isn't small. |
| 1857 | * SA_RESTART is also needed, but in Linux, signal() |
| 1858 | * sets SA_RESTART too. |
| 1859 | */ |
| 1860 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1861 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1862 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1863 | G.sa.sa_handler = handler; |
| 1864 | sigaction(sig, &G.sa, &old_sa); |
| 1865 | return old_sa.sa_handler; |
| 1866 | } |
| 1867 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1868 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1869 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1870 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1871 | static void restore_ttypgrp_and__exit(void) |
| 1872 | { |
| 1873 | /* xfunc has failed! die die die */ |
| 1874 | /* no EXIT traps, this is an escape hatch! */ |
| 1875 | G.exiting = 1; |
| 1876 | hush_exit(xfunc_error_retval); |
| 1877 | } |
| 1878 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1879 | #if ENABLE_HUSH_JOB |
| 1880 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1881 | /* Needed only on some libc: |
| 1882 | * It was observed that on exit(), fgetc'ed buffered data |
| 1883 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1884 | * With the net effect that even after fork(), not vfork(), |
| 1885 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1886 | * noexec_applet_here |
| 1887 | * echo END_OF_SCRIPT |
| 1888 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1889 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1890 | * 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] | 1891 | * and in `cmd` handling. |
| 1892 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1893 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1894 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1895 | static void fflush_and__exit(void) |
| 1896 | { |
| 1897 | fflush_all(); |
| 1898 | _exit(xfunc_error_retval); |
| 1899 | } |
| 1900 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1901 | /* 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] | 1902 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1903 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1904 | # 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] | 1905 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1906 | /* Restores tty foreground process group, and exits. |
| 1907 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1908 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1909 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1910 | * We also call it if xfunc is exiting. |
| 1911 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1912 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1913 | static void sigexit(int sig) |
| 1914 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1915 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1916 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1917 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1918 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1919 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1920 | */ |
| 1921 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1922 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1923 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1924 | |
| 1925 | /* Not a signal, just exit */ |
| 1926 | if (sig <= 0) |
| 1927 | _exit(- sig); |
| 1928 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1929 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1930 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1931 | #else |
| 1932 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1933 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1934 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1935 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1936 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1937 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1938 | static sighandler_t pick_sighandler(unsigned sig) |
| 1939 | { |
| 1940 | sighandler_t handler = SIG_DFL; |
| 1941 | if (sig < sizeof(unsigned)*8) { |
| 1942 | unsigned sigmask = (1 << sig); |
| 1943 | |
| 1944 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1945 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1946 | if (G_fatal_sig_mask & sigmask) |
| 1947 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1948 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1949 | #endif |
| 1950 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1951 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1952 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1953 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1954 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1955 | * in an endless loop when we try to do some |
| 1956 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1957 | */ |
| 1958 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1959 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1960 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1961 | } |
| 1962 | return handler; |
| 1963 | } |
| 1964 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1965 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1966 | static void hush_exit(int exitcode) |
| 1967 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1968 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1969 | save_history(G.line_input_state); |
| 1970 | #endif |
| 1971 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1972 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1973 | 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] | 1974 | char *argv[3]; |
| 1975 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1976 | 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] | 1977 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1978 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1979 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1980 | * "trap" will still show it, if executed |
| 1981 | * in the handler */ |
| 1982 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1983 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1984 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1985 | #if ENABLE_FEATURE_CLEAN_UP |
| 1986 | { |
| 1987 | struct variable *cur_var; |
| 1988 | if (G.cwd != bb_msg_unknown) |
| 1989 | free((char*)G.cwd); |
| 1990 | cur_var = G.top_var; |
| 1991 | while (cur_var) { |
| 1992 | struct variable *tmp = cur_var; |
| 1993 | if (!cur_var->max_len) |
| 1994 | free(cur_var->varstr); |
| 1995 | cur_var = cur_var->next; |
| 1996 | free(tmp); |
| 1997 | } |
| 1998 | } |
| 1999 | #endif |
| 2000 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2001 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2002 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2003 | sigexit(- (exitcode & 0xff)); |
| 2004 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2005 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2006 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2009 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2010 | //TODO: return a mask of ALL handled sigs? |
| 2011 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2012 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2013 | int last_sig = 0; |
| 2014 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2015 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2016 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2017 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2018 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2019 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2020 | sig = 0; |
| 2021 | do { |
| 2022 | sig++; |
| 2023 | if (sigismember(&G.pending_set, sig)) { |
| 2024 | sigdelset(&G.pending_set, sig); |
| 2025 | goto got_sig; |
| 2026 | } |
| 2027 | } while (sig < NSIG); |
| 2028 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2029 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2030 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2031 | 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] | 2032 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2033 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2034 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2035 | char *argv[3]; |
| 2036 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2037 | argv[1] = xstrdup(G_traps[sig]); |
| 2038 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2039 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2040 | save_rcode = G.last_exitcode; |
| 2041 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2042 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2043 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2044 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2045 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2046 | } /* else: "" trap, ignoring signal */ |
| 2047 | continue; |
| 2048 | } |
| 2049 | /* not a trap: special action */ |
| 2050 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2051 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2052 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2053 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2054 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2055 | break; |
| 2056 | #if ENABLE_HUSH_JOB |
| 2057 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2058 | //TODO: why are we doing this? ash and dash don't do this, |
| 2059 | //they have no handler for SIGHUP at all, |
| 2060 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2061 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2062 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2063 | /* bash is observed to signal whole process groups, |
| 2064 | * not individual processes */ |
| 2065 | for (job = G.job_list; job; job = job->next) { |
| 2066 | if (job->pgrp <= 0) |
| 2067 | continue; |
| 2068 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2069 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2070 | kill(- job->pgrp, SIGCONT); |
| 2071 | } |
| 2072 | sigexit(SIGHUP); |
| 2073 | } |
| 2074 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2075 | #if ENABLE_HUSH_FAST |
| 2076 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2077 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2078 | G.count_SIGCHLD++; |
| 2079 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2080 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2081 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2082 | * This simplifies wait builtin a bit. |
| 2083 | */ |
| 2084 | break; |
| 2085 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2086 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2087 | 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] | 2088 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2089 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2090 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2091 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2092 | * in interactive shell, because TERM is ignored. |
| 2093 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | return last_sig; |
| 2098 | } |
| 2099 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2100 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2101 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2102 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2103 | if (force || G.cwd == NULL) { |
| 2104 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2105 | * we must not try to free(bb_msg_unknown) */ |
| 2106 | if (G.cwd == bb_msg_unknown) |
| 2107 | G.cwd = NULL; |
| 2108 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2109 | if (!G.cwd) |
| 2110 | G.cwd = bb_msg_unknown; |
| 2111 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2112 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2115 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2116 | /* |
| 2117 | * Shell and environment variable support |
| 2118 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2119 | 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] | 2120 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2121 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2122 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2123 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2124 | pp = &G.top_var; |
| 2125 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2126 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2127 | return pp; |
| 2128 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2129 | } |
| 2130 | return NULL; |
| 2131 | } |
| 2132 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2133 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2134 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2135 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2136 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2137 | |
| 2138 | if (G.expanded_assignments) { |
| 2139 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2140 | while (*cpp) { |
| 2141 | char *cp = *cpp; |
| 2142 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2143 | return cp + len + 1; |
| 2144 | cpp++; |
| 2145 | } |
| 2146 | } |
| 2147 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2148 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2149 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2150 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2151 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2152 | if (strcmp(name, "PPID") == 0) |
| 2153 | return utoa(G.root_ppid); |
| 2154 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2155 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2156 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2157 | return utoa(next_random(&G.random_gen)); |
| 2158 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2159 | return NULL; |
| 2160 | } |
| 2161 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2162 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2163 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2164 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2165 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2166 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2167 | cmdedit_update_prompt(); |
| 2168 | return; |
| 2169 | } |
| 2170 | |
| 2171 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2172 | && name_len == 6 |
| 2173 | ) { |
| 2174 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2175 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2176 | G.lineno_var = NULL; |
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 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2181 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2182 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2183 | return; |
| 2184 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2185 | #endif |
| 2186 | } |
| 2187 | } |
| 2188 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2189 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2190 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2191 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2192 | #define SETFLAG_EXPORT (1 << 0) |
| 2193 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2194 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2195 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2196 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2197 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2198 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2199 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2200 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2201 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2202 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2203 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2204 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2205 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2206 | if (HUSH_DEBUG && !eq_sign) |
| 2207 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2208 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2209 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2210 | cur_pp = &G.top_var; |
| 2211 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2212 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2213 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2214 | continue; |
| 2215 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2216 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2217 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2218 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2219 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2220 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2221 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2222 | //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] | 2223 | return -1; |
| 2224 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2225 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2226 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2227 | *eq_sign = '\0'; |
| 2228 | unsetenv(str); |
| 2229 | *eq_sign = '='; |
| 2230 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2231 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2232 | /* bash 3.2.33(1) and exported vars: |
| 2233 | * # export z=z |
| 2234 | * # f() { local z=a; env | grep ^z; } |
| 2235 | * # f |
| 2236 | * z=a |
| 2237 | * # env | grep ^z |
| 2238 | * z=z |
| 2239 | */ |
| 2240 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2241 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2242 | /* New variable is local ("local VAR=VAL" or |
| 2243 | * "VAR=VAL cmd") |
| 2244 | * and existing one is global, or local |
| 2245 | * on a lower level that new one. |
| 2246 | * Remove it from global variable list: |
| 2247 | */ |
| 2248 | *cur_pp = cur->next; |
| 2249 | if (G.shadowed_vars_pp) { |
| 2250 | /* Save in "shadowed" list */ |
| 2251 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2252 | cur->flg_export ? "exported " : "", |
| 2253 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2254 | ); |
| 2255 | cur->next = *G.shadowed_vars_pp; |
| 2256 | *G.shadowed_vars_pp = cur; |
| 2257 | } else { |
| 2258 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2259 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2260 | cur->flg_export ? "exported " : "", |
| 2261 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2262 | ); |
| 2263 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2264 | free_me = cur->varstr; /* then free it later */ |
| 2265 | free(cur); |
| 2266 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2267 | break; |
| 2268 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2269 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2270 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2271 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2272 | free_and_exp: |
| 2273 | free(str); |
| 2274 | goto exp; |
| 2275 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2276 | |
| 2277 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2278 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2279 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2280 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2281 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2282 | strcpy(cur->varstr, str); |
| 2283 | goto free_and_exp; |
| 2284 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2285 | /* Can't reuse */ |
| 2286 | cur->max_len = 0; |
| 2287 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2288 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2289 | /* max_len == 0 signifies "malloced" var, which we can |
| 2290 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2291 | * if cur->flg_export is 1, it is in the environment. |
| 2292 | * We should either unsetenv+free, or wait until putenv, |
| 2293 | * then putenv(new)+free(old). |
| 2294 | */ |
| 2295 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2296 | goto set_str_and_exp; |
| 2297 | } |
| 2298 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2299 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2300 | 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] | 2301 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2302 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2303 | cur->next = *cur_pp; |
| 2304 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2305 | |
| 2306 | set_str_and_exp: |
| 2307 | cur->varstr = str; |
| 2308 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2309 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2310 | if (flags & SETFLAG_MAKE_RO) { |
| 2311 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2312 | } |
| 2313 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2314 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2315 | cur->flg_export = 1; |
| 2316 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2317 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2318 | cur->flg_export = 0; |
| 2319 | /* unsetenv was already done */ |
| 2320 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2321 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2322 | 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] | 2323 | i = putenv(cur->varstr); |
| 2324 | /* only now we can free old exported malloced string */ |
| 2325 | free(free_me); |
| 2326 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2327 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2328 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2329 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2330 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2331 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2332 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2333 | return 0; |
| 2334 | } |
| 2335 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2336 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2337 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2338 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2339 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
Denys Vlasenko | 35a017c | 2018-06-26 18:27:54 +0200 | [diff] [blame] | 2342 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2343 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2344 | { |
| 2345 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2346 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2347 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2348 | cur_pp = &G.top_var; |
| 2349 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2350 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2351 | && cur->varstr[name_len] == '=' |
| 2352 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2353 | if (cur->flg_read_only) { |
| 2354 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2355 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2356 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2357 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2358 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2359 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2360 | bb_unsetenv(cur->varstr); |
| 2361 | if (!cur->max_len) |
| 2362 | free(cur->varstr); |
| 2363 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2364 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2365 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2366 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2367 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2368 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2369 | |
| 2370 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2371 | handle_changed_special_names(name, name_len); |
| 2372 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2373 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2376 | static int unset_local_var(const char *name) |
| 2377 | { |
| 2378 | return unset_local_var_len(name, strlen(name)); |
| 2379 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2380 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2381 | |
Denys Vlasenko | b2b14cb | 2018-06-26 18:09:22 +0200 | [diff] [blame] | 2382 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ || ENABLE_HUSH_GETOPTS \ |
| 2383 | || (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2384 | 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] | 2385 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2386 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2387 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2388 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2389 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2390 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2391 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2392 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2393 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2394 | */ |
| 2395 | static void add_vars(struct variable *var) |
| 2396 | { |
| 2397 | struct variable *next; |
| 2398 | |
| 2399 | while (var) { |
| 2400 | next = var->next; |
| 2401 | var->next = G.top_var; |
| 2402 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2403 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2404 | 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] | 2405 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2406 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2407 | 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] | 2408 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2409 | var = next; |
| 2410 | } |
| 2411 | } |
| 2412 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2413 | /* We put strings[i] into variable table and possibly putenv them. |
| 2414 | * If variable is read only, we can free the strings[i] |
| 2415 | * which attempts to overwrite it. |
| 2416 | * The strings[] vector itself is freed. |
| 2417 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2418 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2419 | { |
| 2420 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2421 | |
| 2422 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2423 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2424 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2425 | s = strings; |
| 2426 | while (*s) { |
| 2427 | struct variable *var_p; |
| 2428 | struct variable **var_pp; |
| 2429 | char *eq; |
| 2430 | |
| 2431 | eq = strchr(*s, '='); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2432 | if (HUSH_DEBUG && !eq) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2433 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2434 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
| 2435 | if (var_pp) { |
| 2436 | var_p = *var_pp; |
| 2437 | if (var_p->flg_read_only) { |
| 2438 | char **p; |
| 2439 | bb_error_msg("%s: readonly variable", *s); |
| 2440 | /* |
| 2441 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2442 | * If VAR is readonly, leaving it in the list |
| 2443 | * after asssignment error (msg above) |
| 2444 | * causes doubled error message later, on unset. |
| 2445 | */ |
| 2446 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2447 | free(*s); |
| 2448 | p = s; |
| 2449 | do { *p = p[1]; p++; } while (*p); |
| 2450 | goto next; |
| 2451 | } |
| 2452 | /* below, set_local_var() with nest level will |
| 2453 | * "shadow" (remove) this variable from |
| 2454 | * global linked list. |
| 2455 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2456 | } |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2457 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
| 2458 | set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2459 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2460 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2461 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2462 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | |
| 2466 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2467 | * Unicode helper |
| 2468 | */ |
| 2469 | static void reinit_unicode_for_hush(void) |
| 2470 | { |
| 2471 | /* Unicode support should be activated even if LANG is set |
| 2472 | * _during_ shell execution, not only if it was set when |
| 2473 | * shell was started. Therefore, re-check LANG every time: |
| 2474 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2475 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2476 | || ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 4c201c0 | 2018-07-17 15:04:17 +0200 | [diff] [blame] | 2477 | ) { |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2478 | const char *s = get_local_var_value("LC_ALL"); |
| 2479 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2480 | if (!s) s = get_local_var_value("LANG"); |
| 2481 | reinit_unicode(s); |
| 2482 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2483 | } |
| 2484 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2485 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2486 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2487 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2488 | |
| 2489 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2490 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2491 | * echo $P\ |
| 2492 | * \ |
| 2493 | * AT\ |
| 2494 | * H\ |
| 2495 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2496 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2497 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2498 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2499 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2500 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2501 | G.PS1 = get_local_var_value("PS1"); |
| 2502 | if (G.PS1 == NULL) |
| 2503 | G.PS1 = ""; |
| 2504 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2505 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2506 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2507 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2508 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2509 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2510 | { |
| 2511 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2512 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2513 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2514 | |
| 2515 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2516 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2517 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2518 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2519 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2520 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2521 | /* bash uses $PWD value, even if it is set by user. |
| 2522 | * It uses current dir only if PWD is unset. |
| 2523 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2524 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2525 | } |
| 2526 | prompt_str = G.PS1; |
| 2527 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2528 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2529 | return prompt_str; |
| 2530 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2531 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2532 | { |
| 2533 | int r; |
| 2534 | const char *prompt_str; |
| 2535 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2536 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2537 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2538 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2539 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2540 | if (G.flag_SIGINT) { |
| 2541 | /* There was ^C'ed, make it look prettier: */ |
| 2542 | bb_putchar('\n'); |
| 2543 | G.flag_SIGINT = 0; |
| 2544 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2545 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2546 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2547 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2548 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2549 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2550 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2551 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2552 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2553 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2554 | if (r != 0 && !G.flag_SIGINT) |
| 2555 | break; |
| 2556 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2557 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2558 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2559 | G.last_exitcode = 128 + SIGINT; |
| 2560 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2561 | if (r < 0) { |
| 2562 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2563 | i->p = NULL; |
| 2564 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2565 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2566 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2567 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2568 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2569 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2570 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2571 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2572 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2573 | /* Why check_and_run_traps here? Try this interactively: |
| 2574 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2575 | * $ <[enter], repeatedly...> |
| 2576 | * Without check_and_run_traps, handler never runs. |
| 2577 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2578 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2579 | fputs(prompt_str, stdout); |
| 2580 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2581 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2582 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2583 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2584 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2585 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2586 | * it generates SIGINT as usual. |
| 2587 | */ |
| 2588 | check_and_run_traps(); |
| 2589 | if (G.flag_SIGINT) |
| 2590 | G.last_exitcode = 128 + SIGINT; |
| 2591 | if (r != '\0') |
| 2592 | break; |
| 2593 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2594 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2595 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2596 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2597 | /* This is the magic location that prints prompts |
| 2598 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2599 | static int fgetc_interactive(struct in_str *i) |
| 2600 | { |
| 2601 | int ch; |
| 2602 | /* If it's interactive stdin, get new line. */ |
| 2603 | if (G_interactive_fd && i->file == stdin) { |
| 2604 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2605 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2606 | G.promptmode = 1; /* PS2 */ |
| 2607 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2608 | } else { |
| 2609 | /* Not stdin: script file, sourced file, etc */ |
| 2610 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2611 | } |
| 2612 | return ch; |
| 2613 | } |
| 2614 | #else |
| 2615 | static inline int fgetc_interactive(struct in_str *i) |
| 2616 | { |
| 2617 | int ch; |
| 2618 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2619 | return ch; |
| 2620 | } |
| 2621 | #endif /* INTERACTIVE */ |
| 2622 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2623 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2624 | { |
| 2625 | int ch; |
| 2626 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2627 | if (!i->file) { |
| 2628 | /* string-based in_str */ |
| 2629 | ch = (unsigned char)*i->p; |
| 2630 | if (ch != '\0') { |
| 2631 | i->p++; |
| 2632 | i->last_char = ch; |
| 2633 | return ch; |
| 2634 | } |
| 2635 | return EOF; |
| 2636 | } |
| 2637 | |
| 2638 | /* FILE-based in_str */ |
| 2639 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2640 | #if ENABLE_FEATURE_EDITING |
| 2641 | /* This can be stdin, check line editing char[] buffer */ |
| 2642 | if (i->p && *i->p != '\0') { |
| 2643 | ch = (unsigned char)*i->p++; |
| 2644 | goto out; |
| 2645 | } |
| 2646 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2647 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2648 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2649 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2650 | int ch2 = i->peek_buf[1]; |
| 2651 | i->peek_buf[0] = ch2; |
| 2652 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2653 | goto out; |
| 2654 | i->peek_buf[1] = 0; |
| 2655 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2656 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2657 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2658 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2659 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2660 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2661 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2662 | #if ENABLE_HUSH_LINENO_VAR |
| 2663 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2664 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2665 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2666 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2667 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2668 | return ch; |
| 2669 | } |
| 2670 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2671 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2672 | { |
| 2673 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2674 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2675 | if (!i->file) { |
| 2676 | /* string-based in_str */ |
| 2677 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2678 | return (unsigned char)*i->p; |
| 2679 | } |
| 2680 | |
| 2681 | /* FILE-based in_str */ |
| 2682 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2683 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2684 | /* This can be stdin, check line editing char[] buffer */ |
| 2685 | if (i->p && *i->p != '\0') |
| 2686 | return (unsigned char)*i->p; |
| 2687 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2688 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2689 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2690 | if (ch != 0) |
| 2691 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2692 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2693 | /* Need to get a new char */ |
| 2694 | ch = fgetc_interactive(i); |
| 2695 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2696 | |
| 2697 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2698 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2699 | if (i->p) { |
| 2700 | i->p -= 1; |
| 2701 | return ch; |
| 2702 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2703 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2704 | i->peek_buf[0] = ch; |
| 2705 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2706 | return ch; |
| 2707 | } |
| 2708 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2709 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2710 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2711 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2712 | */ |
| 2713 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2714 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2715 | int ch; |
| 2716 | |
| 2717 | /* There are two cases when i->p[] buffer exists. |
| 2718 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2719 | * (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] | 2720 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2721 | * the peek2 result is in i->p[1]. |
| 2722 | */ |
| 2723 | if (i->p) |
| 2724 | return (unsigned char)i->p[1]; |
| 2725 | |
| 2726 | /* Now we know it is a file-based in_str. */ |
| 2727 | |
| 2728 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2729 | /* Is there 2nd char? */ |
| 2730 | ch = i->peek_buf[1]; |
| 2731 | if (ch == 0) { |
| 2732 | /* We did not read it yet, get it now */ |
| 2733 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2734 | i->peek_buf[1] = ch; |
| 2735 | } |
| 2736 | |
| 2737 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2738 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2739 | } |
| 2740 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2741 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2742 | { |
| 2743 | for (;;) { |
| 2744 | int ch, ch2; |
| 2745 | |
| 2746 | ch = i_getch(input); |
| 2747 | if (ch != '\\') |
| 2748 | return ch; |
| 2749 | ch2 = i_peek(input); |
| 2750 | if (ch2 != '\n') |
| 2751 | return ch; |
| 2752 | /* backslash+newline, skip it */ |
| 2753 | i_getch(input); |
| 2754 | } |
| 2755 | } |
| 2756 | |
| 2757 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2758 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2759 | */ |
| 2760 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2761 | { |
| 2762 | for (;;) { |
| 2763 | int ch, ch2; |
| 2764 | |
| 2765 | ch = i_peek(input); |
| 2766 | if (ch != '\\') |
| 2767 | return ch; |
| 2768 | ch2 = i_peek2(input); |
| 2769 | if (ch2 != '\n') |
| 2770 | return ch; |
| 2771 | /* backslash+newline, skip it */ |
| 2772 | i_getch(input); |
| 2773 | i_getch(input); |
| 2774 | } |
| 2775 | } |
| 2776 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2777 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2778 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2779 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2780 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2781 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2785 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2786 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2787 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2788 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2792 | /* |
| 2793 | * o_string support |
| 2794 | */ |
| 2795 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2796 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2797 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2798 | { |
| 2799 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2800 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2801 | if (o->data) |
| 2802 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2805 | static void o_free_and_set_NULL(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2806 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2807 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2808 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2811 | static ALWAYS_INLINE void o_free(o_string *o) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2812 | { |
| 2813 | free(o->data); |
| 2814 | } |
| 2815 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2816 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2817 | { |
| 2818 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2819 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2820 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2821 | } |
| 2822 | } |
| 2823 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2824 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2825 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2826 | 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] | 2827 | if (o->length < o->maxlen) { |
| 2828 | /* likely. avoid o_grow_by() call */ |
| 2829 | add: |
| 2830 | o->data[o->length] = ch; |
| 2831 | o->length++; |
| 2832 | o->data[o->length] = '\0'; |
| 2833 | return; |
| 2834 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2835 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2836 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2839 | #if 0 |
| 2840 | /* Valid only if we know o_string is not empty */ |
| 2841 | static void o_delchr(o_string *o) |
| 2842 | { |
| 2843 | o->length--; |
| 2844 | o->data[o->length] = '\0'; |
| 2845 | } |
| 2846 | #endif |
| 2847 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2848 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2849 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2850 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2851 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2852 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2855 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2856 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2857 | o_addblock(o, str, strlen(str)); |
| 2858 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2859 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2860 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2861 | static void nommu_addchr(o_string *o, int ch) |
| 2862 | { |
| 2863 | if (o) |
| 2864 | o_addchr(o, ch); |
| 2865 | } |
| 2866 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2867 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2868 | #endif |
| 2869 | |
| 2870 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2871 | { |
| 2872 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2875 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2876 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2877 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2878 | * Apparently, on unquoted $v bash still does globbing |
| 2879 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2880 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2881 | * quoting mechanisms on $v expansion side: one protects |
| 2882 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2883 | * We have only second one. |
| 2884 | */ |
| 2885 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2886 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2887 | # define MAYBE_BRACES "{}" |
| 2888 | #else |
| 2889 | # define MAYBE_BRACES "" |
| 2890 | #endif |
| 2891 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2892 | /* My analysis of quoting semantics tells me that state information |
| 2893 | * is associated with a destination, not a source. |
| 2894 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2895 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2896 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2897 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2898 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2899 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2900 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2901 | o_grow_by(o, sz); |
| 2902 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2903 | o->data[o->length] = '\\'; |
| 2904 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2905 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2906 | o->data[o->length] = ch; |
| 2907 | o->length++; |
| 2908 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2911 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2912 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2913 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2914 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2915 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2916 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2917 | sz++; |
| 2918 | o->data[o->length] = '\\'; |
| 2919 | o->length++; |
| 2920 | } |
| 2921 | o_grow_by(o, sz); |
| 2922 | o->data[o->length] = ch; |
| 2923 | o->length++; |
| 2924 | o->data[o->length] = '\0'; |
| 2925 | } |
| 2926 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2927 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2928 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2929 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2930 | char ch; |
| 2931 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2932 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2933 | if (ordinary_cnt > len) /* paranoia */ |
| 2934 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2935 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2936 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2937 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2938 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2939 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2940 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2941 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2942 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2943 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2944 | sz++; |
| 2945 | o->data[o->length] = '\\'; |
| 2946 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2947 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2948 | o_grow_by(o, sz); |
| 2949 | o->data[o->length] = ch; |
| 2950 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2951 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2952 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2955 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2956 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2957 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2958 | o_addblock(o, str, len); |
| 2959 | return; |
| 2960 | } |
| 2961 | o_addqblock(o, str, len); |
| 2962 | } |
| 2963 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2964 | static void o_addQstr(o_string *o, const char *str) |
| 2965 | { |
| 2966 | o_addQblock(o, str, strlen(str)); |
| 2967 | } |
| 2968 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2969 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2970 | * 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] | 2971 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2972 | * list[i] contains an INDEX (int!) into this string data. |
| 2973 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2974 | * but list[i]'s need not be modified. |
| 2975 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2976 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2977 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2978 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2979 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2980 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2981 | { |
| 2982 | char **list = (char**)o->data; |
| 2983 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2984 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2985 | |
| 2986 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2987 | 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] | 2988 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2989 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2990 | o->has_quoted_part, |
| 2991 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2992 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2993 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2994 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2995 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2996 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2997 | i++; |
| 2998 | } |
| 2999 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3000 | 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] | 3001 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3002 | 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] | 3003 | } |
| 3004 | } |
| 3005 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3006 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3007 | #endif |
| 3008 | |
| 3009 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3010 | * in list[n] so that it points past last stored byte so far. |
| 3011 | * It returns n+1. */ |
| 3012 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3013 | { |
| 3014 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3015 | int string_start; |
| 3016 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3017 | |
| 3018 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3019 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3020 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3021 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3022 | 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] | 3023 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3024 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3025 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3026 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3027 | memmove(list + n + 0x10, list + n, string_len); |
| 3028 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3029 | } else { |
| 3030 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3031 | n, string_len, string_start); |
| 3032 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3033 | } else { |
| 3034 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3035 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3036 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3037 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3038 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3039 | o->has_empty_slot = 0; |
| 3040 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3041 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3042 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3043 | return n + 1; |
| 3044 | } |
| 3045 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3046 | /* "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] | 3047 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3048 | { |
| 3049 | char **list = (char**)o->data; |
| 3050 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3051 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3052 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3055 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3056 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3057 | * first, it processes even {a} (no commas), second, |
| 3058 | * 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] | 3059 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3060 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3061 | |
| 3062 | /* Helper */ |
| 3063 | static int glob_needed(const char *s) |
| 3064 | { |
| 3065 | while (*s) { |
| 3066 | if (*s == '\\') { |
| 3067 | if (!s[1]) |
| 3068 | return 0; |
| 3069 | s += 2; |
| 3070 | continue; |
| 3071 | } |
| 3072 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3073 | return 1; |
| 3074 | s++; |
| 3075 | } |
| 3076 | return 0; |
| 3077 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3078 | /* Return pointer to next closing brace or to comma */ |
| 3079 | static const char *next_brace_sub(const char *cp) |
| 3080 | { |
| 3081 | unsigned depth = 0; |
| 3082 | cp++; |
| 3083 | while (*cp != '\0') { |
| 3084 | if (*cp == '\\') { |
| 3085 | if (*++cp == '\0') |
| 3086 | break; |
| 3087 | cp++; |
| 3088 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3089 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3090 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3091 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3092 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3093 | depth++; |
| 3094 | } |
| 3095 | |
| 3096 | return *cp != '\0' ? cp : NULL; |
| 3097 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3098 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3099 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3100 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3101 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3102 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3103 | const char *next; |
| 3104 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3105 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3106 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3107 | |
| 3108 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3109 | |
| 3110 | begin = pattern; |
| 3111 | while (1) { |
| 3112 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3113 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3114 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3115 | /* Find the first sub-pattern and at the same time |
| 3116 | * find the rest after the closing brace */ |
| 3117 | next = next_brace_sub(begin); |
| 3118 | if (next == NULL) { |
| 3119 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3120 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3121 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3122 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3123 | /* "{abc}" with no commas - illegal |
| 3124 | * brace expr, disregard and skip it */ |
| 3125 | begin = next + 1; |
| 3126 | continue; |
| 3127 | } |
| 3128 | break; |
| 3129 | } |
| 3130 | if (*begin == '\\' && begin[1] != '\0') |
| 3131 | begin++; |
| 3132 | begin++; |
| 3133 | } |
| 3134 | debug_printf_glob("begin:%s\n", begin); |
| 3135 | debug_printf_glob("next:%s\n", next); |
| 3136 | |
| 3137 | /* Now find the end of the whole brace expression */ |
| 3138 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3139 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3140 | rest = next_brace_sub(rest); |
| 3141 | if (rest == NULL) { |
| 3142 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3143 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3144 | } |
| 3145 | debug_printf_glob("rest:%s\n", rest); |
| 3146 | } |
| 3147 | rest_len = strlen(++rest) + 1; |
| 3148 | |
| 3149 | /* We are sure the brace expression is well-formed */ |
| 3150 | |
| 3151 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3152 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3153 | |
| 3154 | /* We have a brace expression. BEGIN points to the opening {, |
| 3155 | * NEXT points past the terminator of the first element, and REST |
| 3156 | * points past the final }. We will accumulate result names from |
| 3157 | * recursive runs for each brace alternative in the buffer using |
| 3158 | * GLOB_APPEND. */ |
| 3159 | |
| 3160 | p = begin + 1; |
| 3161 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3162 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3163 | memcpy( |
| 3164 | mempcpy( |
| 3165 | mempcpy(new_pattern_buf, |
| 3166 | /* We know the prefix for all sub-patterns */ |
| 3167 | pattern, begin - pattern), |
| 3168 | p, next - p), |
| 3169 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3170 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3171 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3172 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3173 | */ |
| 3174 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3175 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3176 | /* We saw the last entry */ |
| 3177 | break; |
| 3178 | } |
| 3179 | p = next + 1; |
| 3180 | next = next_brace_sub(next); |
| 3181 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3182 | free(new_pattern_buf); |
| 3183 | return n; |
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 | simple_glob: |
| 3186 | { |
| 3187 | int gr; |
| 3188 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3189 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3190 | memset(&globdata, 0, sizeof(globdata)); |
| 3191 | gr = glob(pattern, 0, NULL, &globdata); |
| 3192 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3193 | if (gr != 0) { |
| 3194 | if (gr == GLOB_NOMATCH) { |
| 3195 | globfree(&globdata); |
| 3196 | /* NB: garbles parameter */ |
| 3197 | unbackslash(pattern); |
| 3198 | o_addstr_with_NUL(o, pattern); |
| 3199 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3200 | return o_save_ptr_helper(o, n); |
| 3201 | } |
| 3202 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3203 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3204 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3205 | * but we didn't specify it. Paranoia again. */ |
| 3206 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3207 | } |
| 3208 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3209 | char **argv = globdata.gl_pathv; |
| 3210 | while (1) { |
| 3211 | o_addstr_with_NUL(o, *argv); |
| 3212 | n = o_save_ptr_helper(o, n); |
| 3213 | argv++; |
| 3214 | if (!*argv) |
| 3215 | break; |
| 3216 | } |
| 3217 | } |
| 3218 | globfree(&globdata); |
| 3219 | } |
| 3220 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3221 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3222 | /* Performs globbing on last list[], |
| 3223 | * saving each result as a new list[]. |
| 3224 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3225 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3226 | { |
| 3227 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3228 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3229 | 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] | 3230 | if (!o->data) |
| 3231 | return o_save_ptr_helper(o, n); |
| 3232 | pattern = o->data + o_get_last_ptr(o, n); |
| 3233 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3234 | if (!glob_needed(pattern)) { |
| 3235 | /* unbackslash last string in o in place, fix length */ |
| 3236 | o->length = unbackslash(pattern) - o->data; |
| 3237 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3238 | return o_save_ptr_helper(o, n); |
| 3239 | } |
| 3240 | |
| 3241 | copy = xstrdup(pattern); |
| 3242 | /* "forget" pattern in o */ |
| 3243 | o->length = pattern - o->data; |
| 3244 | n = glob_brace(copy, o, n); |
| 3245 | free(copy); |
| 3246 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3247 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3248 | return n; |
| 3249 | } |
| 3250 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3251 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3252 | |
| 3253 | /* Helper */ |
| 3254 | static int glob_needed(const char *s) |
| 3255 | { |
| 3256 | while (*s) { |
| 3257 | if (*s == '\\') { |
| 3258 | if (!s[1]) |
| 3259 | return 0; |
| 3260 | s += 2; |
| 3261 | continue; |
| 3262 | } |
| 3263 | if (*s == '*' || *s == '[' || *s == '?') |
| 3264 | return 1; |
| 3265 | s++; |
| 3266 | } |
| 3267 | return 0; |
| 3268 | } |
| 3269 | /* Performs globbing on last list[], |
| 3270 | * saving each result as a new list[]. |
| 3271 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3272 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3273 | { |
| 3274 | glob_t globdata; |
| 3275 | int gr; |
| 3276 | char *pattern; |
| 3277 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3278 | 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] | 3279 | if (!o->data) |
| 3280 | return o_save_ptr_helper(o, n); |
| 3281 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3282 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3283 | if (!glob_needed(pattern)) { |
| 3284 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3285 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3286 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3287 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3288 | return o_save_ptr_helper(o, n); |
| 3289 | } |
| 3290 | |
| 3291 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3292 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3293 | * If we glob "*.\*" and don't find anything, we need |
| 3294 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3295 | * will return "*.\*"! |
| 3296 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3297 | gr = glob(pattern, 0, NULL, &globdata); |
| 3298 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3299 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3300 | if (gr == GLOB_NOMATCH) { |
| 3301 | globfree(&globdata); |
| 3302 | goto literal; |
| 3303 | } |
| 3304 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3305 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3306 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3307 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3308 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3309 | } |
| 3310 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3311 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3312 | /* "forget" pattern in o */ |
| 3313 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3314 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3315 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3316 | n = o_save_ptr_helper(o, n); |
| 3317 | argv++; |
| 3318 | if (!*argv) |
| 3319 | break; |
| 3320 | } |
| 3321 | } |
| 3322 | globfree(&globdata); |
| 3323 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3324 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3325 | return n; |
| 3326 | } |
| 3327 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3328 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3329 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3330 | /* 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] | 3331 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3332 | static int o_save_ptr(o_string *o, int n) |
| 3333 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3334 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3335 | /* If o->has_empty_slot, list[n] was already globbed |
| 3336 | * (if it was requested back then when it was filled) |
| 3337 | * so don't do that again! */ |
| 3338 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3339 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3340 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3341 | return o_save_ptr_helper(o, n); |
| 3342 | } |
| 3343 | |
| 3344 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3345 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3346 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3347 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3348 | int string_start; |
| 3349 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3350 | if (DEBUG_EXPAND) |
| 3351 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3352 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3353 | list = (char**)o->data; |
| 3354 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3355 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3356 | while (n) { |
| 3357 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3358 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3359 | } |
| 3360 | return list; |
| 3361 | } |
| 3362 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3363 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3364 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3365 | /* Returns pi->next - next pipe in the list */ |
| 3366 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3367 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3368 | struct pipe *next; |
| 3369 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3370 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3371 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3372 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3373 | struct command *command; |
| 3374 | struct redir_struct *r, *rnext; |
| 3375 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3376 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3377 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3378 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3379 | if (DEBUG_CLEAN) { |
| 3380 | int a; |
| 3381 | char **p; |
| 3382 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3383 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3384 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3385 | } |
| 3386 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3387 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3388 | } |
| 3389 | /* not "else if": on syntax error, we may have both! */ |
| 3390 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3391 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3392 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3393 | free_pipe_list(command->group); |
| 3394 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3395 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3396 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3397 | /* else is crucial here. |
| 3398 | * If group != NULL, child_func is meaningless */ |
| 3399 | #if ENABLE_HUSH_FUNCTIONS |
| 3400 | else if (command->child_func) { |
| 3401 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3402 | command->child_func->parent_cmd = NULL; |
| 3403 | } |
| 3404 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3405 | #if !BB_MMU |
| 3406 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3407 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3408 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3409 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3410 | debug_printf_clean(" redirect %d%s", |
| 3411 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3412 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3413 | if (r->rd_filename) { |
| 3414 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3415 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3416 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3417 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3418 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3419 | rnext = r->next; |
| 3420 | free(r); |
| 3421 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3422 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3423 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3424 | 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] | 3425 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3426 | #if ENABLE_HUSH_JOB |
| 3427 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3428 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3429 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3430 | |
| 3431 | next = pi->next; |
| 3432 | free(pi); |
| 3433 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3434 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3435 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3436 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3437 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3438 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3439 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3440 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3441 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3442 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3443 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3444 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3448 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3449 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3450 | #ifndef debug_print_tree |
| 3451 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3452 | { |
| 3453 | static const char *const PIPE[] = { |
| 3454 | [PIPE_SEQ] = "SEQ", |
| 3455 | [PIPE_AND] = "AND", |
| 3456 | [PIPE_OR ] = "OR" , |
| 3457 | [PIPE_BG ] = "BG" , |
| 3458 | }; |
| 3459 | static const char *RES[] = { |
| 3460 | [RES_NONE ] = "NONE" , |
| 3461 | # if ENABLE_HUSH_IF |
| 3462 | [RES_IF ] = "IF" , |
| 3463 | [RES_THEN ] = "THEN" , |
| 3464 | [RES_ELIF ] = "ELIF" , |
| 3465 | [RES_ELSE ] = "ELSE" , |
| 3466 | [RES_FI ] = "FI" , |
| 3467 | # endif |
| 3468 | # if ENABLE_HUSH_LOOPS |
| 3469 | [RES_FOR ] = "FOR" , |
| 3470 | [RES_WHILE] = "WHILE", |
| 3471 | [RES_UNTIL] = "UNTIL", |
| 3472 | [RES_DO ] = "DO" , |
| 3473 | [RES_DONE ] = "DONE" , |
| 3474 | # endif |
| 3475 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3476 | [RES_IN ] = "IN" , |
| 3477 | # endif |
| 3478 | # if ENABLE_HUSH_CASE |
| 3479 | [RES_CASE ] = "CASE" , |
| 3480 | [RES_CASE_IN ] = "CASE_IN" , |
| 3481 | [RES_MATCH] = "MATCH", |
| 3482 | [RES_CASE_BODY] = "CASE_BODY", |
| 3483 | [RES_ESAC ] = "ESAC" , |
| 3484 | # endif |
| 3485 | [RES_XXXX ] = "XXXX" , |
| 3486 | [RES_SNTX ] = "SNTX" , |
| 3487 | }; |
| 3488 | static const char *const CMDTYPE[] = { |
| 3489 | "{}", |
| 3490 | "()", |
| 3491 | "[noglob]", |
| 3492 | # if ENABLE_HUSH_FUNCTIONS |
| 3493 | "func()", |
| 3494 | # endif |
| 3495 | }; |
| 3496 | |
| 3497 | int pin, prn; |
| 3498 | |
| 3499 | pin = 0; |
| 3500 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3501 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3502 | lvl*2, "", |
| 3503 | pin, |
| 3504 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3505 | RES[pi->res_word], |
| 3506 | pi->followup, PIPE[pi->followup] |
| 3507 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3508 | prn = 0; |
| 3509 | while (prn < pi->num_cmds) { |
| 3510 | struct command *command = &pi->cmds[prn]; |
| 3511 | char **argv = command->argv; |
| 3512 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3513 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3514 | lvl*2, "", prn, |
| 3515 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3516 | #if ENABLE_HUSH_LINENO_VAR |
| 3517 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3518 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3519 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3520 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3521 | CMDTYPE[command->cmd_type], |
| 3522 | argv |
| 3523 | # if !BB_MMU |
| 3524 | , " group_as_string:", command->group_as_string |
| 3525 | # else |
| 3526 | , "", "" |
| 3527 | # endif |
| 3528 | ); |
| 3529 | debug_print_tree(command->group, lvl+1); |
| 3530 | prn++; |
| 3531 | continue; |
| 3532 | } |
| 3533 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3534 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3535 | argv++; |
| 3536 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3537 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3538 | prn++; |
| 3539 | } |
| 3540 | pi = pi->next; |
| 3541 | pin++; |
| 3542 | } |
| 3543 | } |
| 3544 | #endif /* debug_print_tree */ |
| 3545 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3546 | static struct pipe *new_pipe(void) |
| 3547 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3548 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3549 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3550 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3551 | return pi; |
| 3552 | } |
| 3553 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3554 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3555 | * if ctx->command is NULL. |
| 3556 | * No errors possible here. |
| 3557 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3558 | static int done_command(struct parse_context *ctx) |
| 3559 | { |
| 3560 | /* The command is really already in the pipe structure, so |
| 3561 | * advance the pipe counter and make a new, null command. */ |
| 3562 | struct pipe *pi = ctx->pipe; |
| 3563 | struct command *command = ctx->command; |
| 3564 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3565 | #if 0 /* Instead we emit error message at run time */ |
| 3566 | if (ctx->pending_redirect) { |
| 3567 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3568 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3569 | ctx->pending_redirect = NULL; |
| 3570 | } |
| 3571 | #endif |
| 3572 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3573 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3574 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3575 | 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] | 3576 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3577 | } |
| 3578 | pi->num_cmds++; |
| 3579 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3580 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3581 | } else { |
| 3582 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3583 | } |
| 3584 | |
| 3585 | /* Only real trickiness here is that the uncommitted |
| 3586 | * command structure is not counted in pi->num_cmds. */ |
| 3587 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3588 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3589 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3590 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3591 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3592 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3593 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3594 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3595 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3596 | } |
| 3597 | |
| 3598 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3599 | { |
| 3600 | int not_null; |
| 3601 | |
| 3602 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3603 | /* Close previous command */ |
| 3604 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3605 | #if HAS_KEYWORDS |
| 3606 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3607 | ctx->ctx_inverted = 0; |
| 3608 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3609 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3610 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3611 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3612 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3613 | * in a backgrounded subshell. |
| 3614 | */ |
| 3615 | struct pipe *pi; |
| 3616 | struct command *command; |
| 3617 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3618 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3619 | pi = ctx->list_head; |
| 3620 | while (pi != ctx->pipe) { |
| 3621 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3622 | goto no_conv; |
| 3623 | pi = pi->next; |
| 3624 | } |
| 3625 | |
| 3626 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3627 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3628 | pi = xzalloc(sizeof(*pi)); |
| 3629 | pi->followup = PIPE_BG; |
| 3630 | pi->num_cmds = 1; |
| 3631 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3632 | command = &pi->cmds[0]; |
| 3633 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3634 | command->cmd_type = CMD_NORMAL; |
| 3635 | command->group = ctx->list_head; |
| 3636 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3637 | command->group_as_string = xstrndup( |
| 3638 | ctx->as_string.data, |
| 3639 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3640 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3641 | #endif |
| 3642 | /* Replace all pipes in ctx with one newly created */ |
| 3643 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3644 | } else { |
| 3645 | no_conv: |
| 3646 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3647 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3648 | |
| 3649 | /* Without this check, even just <enter> on command line generates |
| 3650 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3651 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3652 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3653 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3654 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3655 | #endif |
| 3656 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3657 | || ctx->ctx_res_w == RES_DONE |
| 3658 | || ctx->ctx_res_w == RES_FOR |
| 3659 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3660 | #endif |
| 3661 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3662 | || ctx->ctx_res_w == RES_ESAC |
| 3663 | #endif |
| 3664 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3665 | struct pipe *new_p; |
| 3666 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3667 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3668 | not_null, ctx->ctx_res_w); |
| 3669 | new_p = new_pipe(); |
| 3670 | ctx->pipe->next = new_p; |
| 3671 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3672 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3673 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3674 | * This is used to control execution. |
| 3675 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3676 | * cases where variable or value happens to match a keyword): |
| 3677 | */ |
| 3678 | #if ENABLE_HUSH_LOOPS |
| 3679 | if (ctx->ctx_res_w == RES_FOR |
| 3680 | || ctx->ctx_res_w == RES_IN) |
| 3681 | ctx->ctx_res_w = RES_NONE; |
| 3682 | #endif |
| 3683 | #if ENABLE_HUSH_CASE |
| 3684 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3685 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3686 | if (ctx->ctx_res_w == RES_CASE) |
| 3687 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3688 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3689 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3690 | /* Create the memory for command, roughly: |
| 3691 | * ctx->pipe->cmds = new struct command; |
| 3692 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3693 | */ |
| 3694 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3695 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3696 | } |
| 3697 | debug_printf_parse("done_pipe return\n"); |
| 3698 | } |
| 3699 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3700 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3701 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3702 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3703 | if (MAYBE_ASSIGNMENT != 0) |
| 3704 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3705 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3706 | /* Create the memory for command, roughly: |
| 3707 | * ctx->pipe->cmds = new struct command; |
| 3708 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3709 | */ |
| 3710 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3713 | /* If a reserved word is found and processed, parse context is modified |
| 3714 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3715 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3716 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3717 | struct reserved_combo { |
| 3718 | char literal[6]; |
| 3719 | unsigned char res; |
| 3720 | unsigned char assignment_flag; |
| 3721 | int flag; |
| 3722 | }; |
| 3723 | enum { |
| 3724 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3725 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3726 | FLAG_IF = (1 << RES_IF ), |
| 3727 | FLAG_THEN = (1 << RES_THEN ), |
| 3728 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3729 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3730 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3731 | # endif |
| 3732 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3733 | FLAG_FOR = (1 << RES_FOR ), |
| 3734 | FLAG_WHILE = (1 << RES_WHILE), |
| 3735 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3736 | FLAG_DO = (1 << RES_DO ), |
| 3737 | FLAG_DONE = (1 << RES_DONE ), |
| 3738 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3739 | # endif |
| 3740 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3741 | FLAG_MATCH = (1 << RES_MATCH), |
| 3742 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3743 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3744 | FLAG_START = (1 << RES_XXXX ), |
| 3745 | }; |
| 3746 | |
| 3747 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3748 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3749 | /* Mostly a list of accepted follow-up reserved words. |
| 3750 | * FLAG_END means we are done with the sequence, and are ready |
| 3751 | * to turn the compound list into a command. |
| 3752 | * FLAG_START means the word must start a new compound list. |
| 3753 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3754 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3755 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3756 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3757 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3758 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3759 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3760 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3761 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3762 | # endif |
| 3763 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3764 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3765 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3766 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3767 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3768 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3769 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3770 | # endif |
| 3771 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3772 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3773 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3774 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3775 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3776 | const struct reserved_combo *r; |
| 3777 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3778 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3779 | if (strcmp(word->data, r->literal) == 0) |
| 3780 | return r; |
| 3781 | } |
| 3782 | return NULL; |
| 3783 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3784 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3785 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3786 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3787 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3788 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3789 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3790 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3791 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3792 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3793 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3794 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3795 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3796 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3797 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3798 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3799 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3800 | |
| 3801 | 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] | 3802 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3803 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3804 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3805 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3806 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3807 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3808 | if (r->flag == 0) { /* '!' */ |
| 3809 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3810 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3811 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3812 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3813 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3814 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3815 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3816 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3817 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3818 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3819 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3820 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3821 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3822 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3823 | } 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] | 3824 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3825 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3826 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3827 | } else { |
| 3828 | /* "{...} fi" is ok. "{...} if" is not |
| 3829 | * Example: |
| 3830 | * if { echo foo; } then { echo bar; } fi */ |
| 3831 | if (ctx->command->group) |
| 3832 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3833 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3834 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3835 | ctx->ctx_res_w = r->res; |
| 3836 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3837 | ctx->is_assignment = r->assignment_flag; |
| 3838 | 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] | 3839 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3840 | if (ctx->old_flag & FLAG_END) { |
| 3841 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3842 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3843 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3844 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3845 | old = ctx->stack; |
| 3846 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3847 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3848 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3849 | /* At this point, the compound command's string is in |
| 3850 | * ctx->as_string... except for the leading keyword! |
| 3851 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3852 | * ctx->as_string will contain "true; then echo a; fi", |
| 3853 | * with "if " remaining in old->as_string! |
| 3854 | */ |
| 3855 | { |
| 3856 | char *str; |
| 3857 | int len = old->as_string.length; |
| 3858 | /* Concatenate halves */ |
| 3859 | o_addstr(&old->as_string, ctx->as_string.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 3860 | o_free(&ctx->as_string); |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3861 | /* Find where leading keyword starts in first half */ |
| 3862 | str = old->as_string.data + len; |
| 3863 | if (str > old->as_string.data) |
| 3864 | str--; /* skip whitespace after keyword */ |
| 3865 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3866 | str--; |
| 3867 | /* Ugh, we're done with this horrid hack */ |
| 3868 | old->command->group_as_string = xstrdup(str); |
| 3869 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3870 | old->command->group_as_string); |
| 3871 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3872 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3873 | *ctx = *old; /* physical copy */ |
| 3874 | free(old); |
| 3875 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3876 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3877 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3878 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3879 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3880 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3881 | * Normal return is 0. Syntax errors return 1. |
| 3882 | * Note: on return, word is reset, but not o_free'd! |
| 3883 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3884 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3885 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3886 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3887 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3888 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 3889 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3890 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3891 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3892 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3893 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3894 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3895 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3896 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3897 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3898 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3899 | * If the redirection operator is "<<" or "<<-", the word |
| 3900 | * that follows the redirection operator shall be |
| 3901 | * subjected to quote removal; it is unspecified whether |
| 3902 | * any of the other expansions occur. For the other |
| 3903 | * redirection operators, the word that follows the |
| 3904 | * redirection operator shall be subjected to tilde |
| 3905 | * expansion, parameter expansion, command substitution, |
| 3906 | * arithmetic expansion, and quote removal. |
| 3907 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3908 | * on the word by a non-interactive shell; an interactive |
| 3909 | * shell may perform it, but shall do so only when |
| 3910 | * the expansion would result in one word." |
| 3911 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3912 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3913 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3914 | // as written: |
| 3915 | // <<EOF$t |
| 3916 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3917 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3918 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3919 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3920 | /* Cater for >\file case: |
| 3921 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3922 | * Same with heredocs: |
| 3923 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3924 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3925 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3926 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3927 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3928 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3929 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3930 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3931 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3932 | 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] | 3933 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3934 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3935 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3936 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3937 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3938 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3939 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3940 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3941 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3942 | ctx->ctx_dsemicolon = 0; |
| 3943 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3944 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3945 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3946 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3947 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3948 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3949 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3950 | # if ENABLE_HUSH_CASE |
| 3951 | && ctx->ctx_res_w != RES_CASE |
| 3952 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3953 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3954 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3955 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3956 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3957 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3958 | # if ENABLE_HUSH_LINENO_VAR |
| 3959 | /* Case: |
| 3960 | * "while ...; do |
| 3961 | * cmd ..." |
| 3962 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3963 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3964 | */ |
| 3965 | if (0 |
| 3966 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3967 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3968 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3969 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3970 | ) { |
| 3971 | done_pipe(ctx, PIPE_SEQ); |
| 3972 | } |
| 3973 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3974 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3975 | debug_printf_parse("done_word return %d\n", |
| 3976 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3977 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3978 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3979 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 3980 | if (0 |
| 3981 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3982 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3983 | # endif |
| 3984 | /* In bash, local/export/readonly are special, args |
| 3985 | * are assignments and therefore expansion of them |
| 3986 | * should be "one-word" expansion: |
| 3987 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 3988 | * compare with: |
| 3989 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 3990 | * ls: cannot access i=a: No such file or directory |
| 3991 | * ls: cannot access b: No such file or directory |
| 3992 | * Note: bash 3.2.33(1) does this only if export word |
| 3993 | * itself is not quoted: |
| 3994 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 3995 | * aaa bbb |
| 3996 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 3997 | * aaa |
| 3998 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3999 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 4000 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 4001 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4002 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4003 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4004 | } |
| 4005 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4006 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4007 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4008 | #endif /* HAS_KEYWORDS */ |
| 4009 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4010 | if (command->group) { |
| 4011 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4012 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4013 | debug_printf_parse("done_word return 1: syntax error, " |
| 4014 | "groups and arglists don't mix\n"); |
| 4015 | return 1; |
| 4016 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4017 | |
| 4018 | /* If this word wasn't an assignment, next ones definitely |
| 4019 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4020 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4021 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4022 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4023 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4024 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4025 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4026 | command->assignment_cnt++; |
| 4027 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4028 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4029 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4030 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4031 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4032 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4033 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4034 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4035 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4036 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4037 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4038 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4039 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4040 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4041 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4042 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4043 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4044 | return 1; |
| 4045 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4046 | /* Force FOR to have just one word (variable name) */ |
| 4047 | /* NB: basically, this makes hush see "for v in ..." |
| 4048 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4049 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4050 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4051 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4052 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4053 | #if ENABLE_HUSH_CASE |
| 4054 | /* Force CASE to have just one word */ |
| 4055 | if (ctx->ctx_res_w == RES_CASE) { |
| 4056 | done_pipe(ctx, PIPE_SEQ); |
| 4057 | } |
| 4058 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4059 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4060 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4061 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4062 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4066 | |
| 4067 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4068 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4069 | * Return: |
| 4070 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4071 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4072 | * REDIRFD_TO_FILE if no & was seen, |
| 4073 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4074 | */ |
| 4075 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4076 | #define parse_redir_right_fd(as_string, input) \ |
| 4077 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4078 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4079 | 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] | 4080 | { |
| 4081 | int ch, d, ok; |
| 4082 | |
| 4083 | ch = i_peek(input); |
| 4084 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4085 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4086 | |
| 4087 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4088 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4089 | ch = i_peek(input); |
| 4090 | if (ch == '-') { |
| 4091 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4092 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4093 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4094 | } |
| 4095 | d = 0; |
| 4096 | ok = 0; |
| 4097 | while (ch != EOF && isdigit(ch)) { |
| 4098 | d = d*10 + (ch-'0'); |
| 4099 | ok = 1; |
| 4100 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4101 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4102 | ch = i_peek(input); |
| 4103 | } |
| 4104 | if (ok) return d; |
| 4105 | |
| 4106 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4107 | |
| 4108 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4109 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4112 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4113 | */ |
| 4114 | static int parse_redirect(struct parse_context *ctx, |
| 4115 | int fd, |
| 4116 | redir_type style, |
| 4117 | struct in_str *input) |
| 4118 | { |
| 4119 | struct command *command = ctx->command; |
| 4120 | struct redir_struct *redir; |
| 4121 | struct redir_struct **redirp; |
| 4122 | int dup_num; |
| 4123 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4124 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4125 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4126 | /* Check for a '>&1' type redirect */ |
| 4127 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4128 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4129 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4130 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4131 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4132 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4133 | if (dup_num) { /* <<-... */ |
| 4134 | ch = i_getch(input); |
| 4135 | nommu_addchr(&ctx->as_string, ch); |
| 4136 | ch = i_peek(input); |
| 4137 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4138 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4139 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4140 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4141 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4142 | if (ch == '|') { |
| 4143 | /* >|FILE redirect ("clobbering" >). |
| 4144 | * Since we do not support "set -o noclobber" yet, |
| 4145 | * >| and > are the same for now. Just eat |. |
| 4146 | */ |
| 4147 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4148 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | /* Create a new redir_struct and append it to the linked list */ |
| 4153 | redirp = &command->redirects; |
| 4154 | while ((redir = *redirp) != NULL) { |
| 4155 | redirp = &(redir->next); |
| 4156 | } |
| 4157 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4158 | /* redir->next = NULL; */ |
| 4159 | /* redir->rd_filename = NULL; */ |
| 4160 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4161 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4162 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4163 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4164 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4165 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4166 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4167 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4168 | /* Erik had a check here that the file descriptor in question |
| 4169 | * is legit; I postpone that to "run time" |
| 4170 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4171 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4172 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4173 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4174 | #if 0 /* Instead we emit error message at run time */ |
| 4175 | if (ctx->pending_redirect) { |
| 4176 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4177 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4178 | } |
| 4179 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4180 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4181 | * end of the next parsed word. */ |
| 4182 | ctx->pending_redirect = redir; |
| 4183 | } |
| 4184 | return 0; |
| 4185 | } |
| 4186 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4187 | /* If a redirect is immediately preceded by a number, that number is |
| 4188 | * supposed to tell which file descriptor to redirect. This routine |
| 4189 | * looks for such preceding numbers. In an ideal world this routine |
| 4190 | * needs to handle all the following classes of redirects... |
| 4191 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4192 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4193 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4194 | * 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] | 4195 | * |
| 4196 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4197 | * "2.7 Redirection |
| 4198 | * ... If n is quoted, the number shall not be recognized as part of |
| 4199 | * the redirection expression. For example: |
| 4200 | * echo \2>a |
| 4201 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4202 | * 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] | 4203 | * |
| 4204 | * A -1 return means no valid number was found, |
| 4205 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4206 | */ |
| 4207 | static int redirect_opt_num(o_string *o) |
| 4208 | { |
| 4209 | int num; |
| 4210 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4211 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4212 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4213 | num = bb_strtou(o->data, NULL, 10); |
| 4214 | if (errno || num < 0) |
| 4215 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4216 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4217 | return num; |
| 4218 | } |
| 4219 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4220 | #if BB_MMU |
| 4221 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4222 | fetch_till_str(input, word, skip_tabs) |
| 4223 | #endif |
| 4224 | static char *fetch_till_str(o_string *as_string, |
| 4225 | struct in_str *input, |
| 4226 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4227 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4228 | { |
| 4229 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4230 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4231 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4232 | int ch; |
| 4233 | |
Denys Vlasenko | d73cdbf | 2018-07-23 15:43:57 +0200 | [diff] [blame] | 4234 | /* Starting with "" is necessary for this case: |
| 4235 | * cat <<EOF |
| 4236 | * |
| 4237 | * xxx |
| 4238 | * EOF |
| 4239 | */ |
| 4240 | heredoc.data = xzalloc(1); /* start as "", not as NULL */ |
| 4241 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4242 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4243 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4244 | while (1) { |
| 4245 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4246 | if (ch != EOF) |
| 4247 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4248 | if (ch == '\n' || ch == EOF) { |
| 4249 | check_heredoc_end: |
| 4250 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4251 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4252 | heredoc.data[past_EOL] = '\0'; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 4253 | debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4254 | return heredoc.data; |
| 4255 | } |
| 4256 | if (ch == '\n') { |
| 4257 | /* This is a new line. |
| 4258 | * Remember position and backslash-escaping status. |
| 4259 | */ |
| 4260 | o_addchr(&heredoc, ch); |
| 4261 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4262 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4263 | past_EOL = heredoc.length; |
| 4264 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4265 | do { |
| 4266 | ch = i_getch(input); |
| 4267 | if (ch != EOF) |
| 4268 | nommu_addchr(as_string, ch); |
| 4269 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4270 | /* If this immediately ended the line, |
| 4271 | * go back to end-of-line checks. |
| 4272 | */ |
| 4273 | if (ch == '\n') |
| 4274 | goto check_heredoc_end; |
| 4275 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4276 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4277 | } |
| 4278 | if (ch == EOF) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4279 | o_free(&heredoc); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4280 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4281 | } |
| 4282 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4283 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4284 | if (prev == '\\' && ch == '\\') |
| 4285 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4286 | prev = 0; /* not \ */ |
| 4287 | else |
| 4288 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4292 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4293 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4294 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4295 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4296 | { |
| 4297 | struct pipe *pi = ctx->list_head; |
| 4298 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4299 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4300 | int i; |
| 4301 | struct command *cmd = pi->cmds; |
| 4302 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 4303 | debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4304 | pi->num_cmds, |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 4305 | cmd->argv ? cmd->argv[0] : "NONE" |
| 4306 | ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4307 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4308 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4309 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 4310 | debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4311 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4312 | while (redir) { |
| 4313 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4314 | char *p; |
| 4315 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4316 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4317 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4318 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4319 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4320 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4321 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4322 | return 1; |
| 4323 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4324 | free(redir->rd_filename); |
| 4325 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4326 | heredoc_cnt--; |
| 4327 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4328 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4329 | } |
| 4330 | cmd++; |
| 4331 | } |
| 4332 | pi = pi->next; |
| 4333 | } |
| 4334 | /* Should be 0. If it isn't, it's a parse error */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 4335 | if (HUSH_DEBUG && heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4336 | bb_error_msg_and_die("heredoc BUG 2"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4337 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4341 | static int run_list(struct pipe *pi); |
| 4342 | #if BB_MMU |
| 4343 | #define parse_stream(pstring, input, end_trigger) \ |
| 4344 | parse_stream(input, end_trigger) |
| 4345 | #endif |
| 4346 | static struct pipe *parse_stream(char **pstring, |
| 4347 | struct in_str *input, |
| 4348 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4349 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4350 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4351 | static int parse_group(struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4352 | struct in_str *input, int ch) |
| 4353 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4354 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4355 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4356 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4357 | #if BB_MMU |
| 4358 | # define as_string NULL |
| 4359 | #else |
| 4360 | char *as_string = NULL; |
| 4361 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4362 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4363 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4364 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4365 | |
| 4366 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4367 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4368 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4369 | if (ctx->word.length) |
| 4370 | if (done_word(ctx)) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4371 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4372 | if (!command->argv) |
| 4373 | goto skip; /* (... */ |
| 4374 | if (command->argv[1]) { /* word word ... (... */ |
| 4375 | syntax_error_unexpected_ch('('); |
| 4376 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4377 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4378 | /* it is "word(..." or "word (..." */ |
| 4379 | do |
| 4380 | ch = i_getch(input); |
| 4381 | while (ch == ' ' || ch == '\t'); |
| 4382 | if (ch != ')') { |
| 4383 | syntax_error_unexpected_ch(ch); |
| 4384 | return 1; |
| 4385 | } |
| 4386 | nommu_addchr(&ctx->as_string, ch); |
| 4387 | do |
| 4388 | ch = i_getch(input); |
| 4389 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4390 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4391 | syntax_error_unexpected_ch(ch); |
| 4392 | return 1; |
| 4393 | } |
| 4394 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4395 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4396 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4397 | } |
| 4398 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4399 | |
| 4400 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4401 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4402 | || ctx->word.length /* word{... */ |
| 4403 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4404 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4405 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4406 | debug_printf_parse("parse_group return 1: " |
| 4407 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4408 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4409 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4410 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4411 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4412 | IF_HUSH_FUNCTIONS(skip:) |
| 4413 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4414 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4415 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4416 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4417 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4418 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4419 | } else { |
| 4420 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4421 | ch = i_peek(input); |
| 4422 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4423 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4424 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4425 | syntax_error_unexpected_ch(ch); |
| 4426 | return 1; |
| 4427 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4428 | if (ch != '(') { |
| 4429 | ch = i_getch(input); |
| 4430 | nommu_addchr(&ctx->as_string, ch); |
| 4431 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4432 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4433 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4434 | pipe_list = parse_stream(&as_string, input, endch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4435 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4436 | if (as_string) |
| 4437 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4438 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4439 | |
| 4440 | /* empty ()/{} or parse error? */ |
| 4441 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4442 | /* parse_stream already emitted error msg */ |
| 4443 | if (!BB_MMU) |
| 4444 | free(as_string); |
| 4445 | debug_printf_parse("parse_group return 1: " |
| 4446 | "parse_stream returned %p\n", pipe_list); |
| 4447 | return 1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4448 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4449 | #if !BB_MMU |
| 4450 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4451 | command->group_as_string = as_string; |
| 4452 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4453 | command->group_as_string); |
| 4454 | #endif |
| 4455 | |
| 4456 | #if ENABLE_HUSH_FUNCTIONS |
| 4457 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4458 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4459 | struct command *cmd2; |
| 4460 | |
| 4461 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4462 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4463 | cmd2->group = pipe_list; |
| 4464 | # if !BB_MMU |
| 4465 | //UNTESTED! |
| 4466 | cmd2->group_as_string = command->group_as_string; |
| 4467 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4468 | # endif |
| 4469 | |
| 4470 | pipe_list = new_pipe(); |
| 4471 | pipe_list->cmds = cmd2; |
| 4472 | pipe_list->num_cmds = 1; |
| 4473 | } |
| 4474 | #endif |
| 4475 | |
| 4476 | command->group = pipe_list; |
| 4477 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4478 | debug_printf_parse("parse_group return 0\n"); |
| 4479 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4480 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4481 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4484 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4485 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4486 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4487 | 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] | 4488 | { |
| 4489 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4490 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4491 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4492 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4493 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4494 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4495 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4496 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4497 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4500 | static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input) |
| 4501 | { |
| 4502 | while (1) { |
| 4503 | int ch = i_getch(input); |
| 4504 | if (ch == EOF) { |
| 4505 | syntax_error_unterm_ch('\''); |
| 4506 | return 0; |
| 4507 | } |
| 4508 | if (ch == '\'') |
| 4509 | return 1; |
| 4510 | o_addqchr(dest, ch); |
| 4511 | } |
| 4512 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4513 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4514 | static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4515 | 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] | 4516 | { |
| 4517 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4518 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4519 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4520 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4521 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4522 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4523 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4524 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4525 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4526 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4527 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4528 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4529 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4530 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4531 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4532 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4533 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4534 | continue; |
| 4535 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4536 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
| 4539 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4540 | * \` quoting. |
| 4541 | * "Within the backquoted style of command substitution, backslash |
| 4542 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4543 | * The search for the matching backquote shall be satisfied by the first |
| 4544 | * backquote found without a preceding backslash; during this search, |
| 4545 | * if a non-escaped backquote is encountered within a shell comment, |
| 4546 | * a here-document, an embedded command substitution of the $(command) |
| 4547 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4548 | * or double-quoted string that begins, but does not end, within the |
| 4549 | * "`...`" sequence produces undefined results." |
| 4550 | * Example Output |
| 4551 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4552 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4553 | 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] | 4554 | { |
| 4555 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4556 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4557 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4558 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4559 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4560 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4561 | ch = i_getch(input); |
| 4562 | if (ch != '`' |
| 4563 | && ch != '$' |
| 4564 | && ch != '\\' |
| 4565 | && (!in_dquote || ch != '"') |
| 4566 | ) { |
| 4567 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4568 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4569 | } |
| 4570 | if (ch == EOF) { |
| 4571 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4572 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4573 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4574 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4575 | } |
| 4576 | } |
| 4577 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4578 | * quoting and nested ()s. |
| 4579 | * "With the $(command) style of command substitution, all characters |
| 4580 | * following the open parenthesis to the matching closing parenthesis |
| 4581 | * constitute the command. Any valid shell script can be used for command, |
| 4582 | * except a script consisting solely of redirections which produces |
| 4583 | * unspecified results." |
| 4584 | * Example Output |
| 4585 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4586 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4587 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4588 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4589 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4590 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4591 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4592 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4593 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4594 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4595 | 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] | 4596 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4597 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4598 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4599 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4600 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4601 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4602 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4603 | |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4604 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4605 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4606 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4607 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4608 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4609 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4610 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4611 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4612 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4613 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4614 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4615 | if (ch == end_ch |
| 4616 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4617 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4618 | # endif |
| 4619 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4620 | if (!dbl) |
| 4621 | break; |
| 4622 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4623 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4624 | i_getch(input); /* eat second ')' */ |
| 4625 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4626 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4627 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4628 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4629 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4630 | if (ch == '(' || ch == '{') { |
| 4631 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4632 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4633 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4634 | o_addchr(dest, ch); |
| 4635 | continue; |
| 4636 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4637 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4638 | if (!add_till_single_quote(dest, input)) |
| 4639 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4640 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4641 | continue; |
| 4642 | } |
| 4643 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4644 | if (!add_till_double_quote(dest, input)) |
| 4645 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4646 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4647 | continue; |
| 4648 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4649 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4650 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4651 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4652 | o_addchr(dest, ch); |
| 4653 | continue; |
| 4654 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4655 | if (ch == '\\') { |
| 4656 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4657 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4658 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4659 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4660 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4661 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4662 | #if 0 |
| 4663 | if (ch == '\n') { |
| 4664 | /* "backslash+newline", ignore both */ |
| 4665 | o_delchr(dest); /* undo insertion of '\' */ |
| 4666 | continue; |
| 4667 | } |
| 4668 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4669 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4670 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4671 | continue; |
| 4672 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4673 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4674 | 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] | 4675 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4676 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4677 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4678 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4679 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4680 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4681 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4682 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4683 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4684 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4685 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4686 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4687 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4688 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4689 | 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] | 4690 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4691 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4692 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4693 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4694 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4695 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4696 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4697 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4698 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4699 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4700 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4701 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4702 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4703 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4704 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4705 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4706 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4707 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4708 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4709 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4710 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4711 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4712 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4713 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4714 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4715 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4716 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4717 | o_addchr(dest, ch | quote_mask); |
| 4718 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4719 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4720 | case '$': /* pid */ |
| 4721 | case '!': /* last bg pid */ |
| 4722 | case '?': /* last exit code */ |
| 4723 | case '#': /* number of args */ |
| 4724 | case '*': /* args */ |
| 4725 | case '@': /* args */ |
| 4726 | goto make_one_char_var; |
| 4727 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4728 | char len_single_ch; |
| 4729 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4730 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4731 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4732 | ch = i_getch(input); /* eat '{' */ |
| 4733 | nommu_addchr(as_string, ch); |
| 4734 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4735 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4736 | /* It should be ${?}, or ${#var}, |
| 4737 | * or even ${?+subst} - operator acting on a special variable, |
| 4738 | * or the beginning of variable name. |
| 4739 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4740 | if (ch == EOF |
| 4741 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4742 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4743 | bad_dollar_syntax: |
| 4744 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4745 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4746 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4747 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4748 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4749 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4750 | ch |= quote_mask; |
| 4751 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4752 | /* 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] | 4753 | * However, this regresses some of our testsuite cases |
| 4754 | * which check invalid constructs like ${%}. |
| 4755 | * Oh well... let's check that the var name part is fine... */ |
| 4756 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4757 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4758 | unsigned pos; |
| 4759 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4760 | o_addchr(dest, ch); |
| 4761 | debug_printf_parse(": '%c'\n", ch); |
| 4762 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4763 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4764 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4765 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4766 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4767 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4768 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4769 | unsigned end_ch; |
| 4770 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4771 | /* handle parameter expansions |
| 4772 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4773 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4774 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4775 | if (len_single_ch != '#' |
| 4776 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4777 | || i_peek(input) != '}' |
| 4778 | ) { |
| 4779 | goto bad_dollar_syntax; |
| 4780 | } |
| 4781 | /* else: it's "length of C" ${#C} op, |
| 4782 | * where C is a single char |
| 4783 | * special var name, e.g. ${#!}. |
| 4784 | */ |
| 4785 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4786 | /* Eat everything until closing '}' (or ':') */ |
| 4787 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4788 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4789 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4790 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4791 | ) { |
| 4792 | /* It's ${var:N[:M]} thing */ |
| 4793 | end_ch = '}' * 0x100 + ':'; |
| 4794 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4795 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4796 | && ch == '/' |
| 4797 | ) { |
| 4798 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4799 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4800 | i_getch(input); |
| 4801 | nommu_addchr(as_string, '/'); |
| 4802 | ch = '\\'; |
| 4803 | } |
| 4804 | end_ch = '}' * 0x100 + '/'; |
| 4805 | } |
| 4806 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4807 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4808 | if (!BB_MMU) |
| 4809 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4810 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4811 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4812 | if (last_ch == 0) /* error? */ |
| 4813 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4814 | #else |
| 4815 | #error Simple code to only allow ${var} is not implemented |
| 4816 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4817 | if (as_string) { |
| 4818 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4819 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4820 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4821 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4822 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4823 | && (end_ch & 0xff00) |
| 4824 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4825 | /* close the first block: */ |
| 4826 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4827 | /* while parsing N from ${var:N[:M]} |
| 4828 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4829 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4830 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4831 | end_ch = '}'; |
| 4832 | goto again; |
| 4833 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4834 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4835 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4836 | /* it's ${var:N} - emulate :999999999 */ |
| 4837 | o_addstr(dest, "999999999"); |
| 4838 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4839 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4840 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4841 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4842 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4843 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4844 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4845 | break; |
| 4846 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4847 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4848 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4849 | unsigned pos; |
| 4850 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4851 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4852 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4853 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4854 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4855 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4856 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4857 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4858 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4859 | if (!BB_MMU) |
| 4860 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4861 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4862 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4863 | if (as_string) { |
| 4864 | o_addstr(as_string, dest->data + pos); |
| 4865 | o_addchr(as_string, ')'); |
| 4866 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4867 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4868 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4869 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4870 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4871 | # endif |
| 4872 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4873 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4874 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4875 | if (!BB_MMU) |
| 4876 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4877 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4878 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4879 | if (as_string) { |
| 4880 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4881 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4882 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4883 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4884 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4887 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4888 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4889 | goto make_var; |
| 4890 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4891 | /* TODO: $_ and $-: */ |
| 4892 | /* $_ Shell or shell script name; or last argument of last command |
| 4893 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4894 | * 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] | 4895 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4896 | ch = i_getch(input); |
| 4897 | nommu_addchr(as_string, ch); |
| 4898 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4899 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4900 | ch = '_'; |
| 4901 | goto make_var1; |
| 4902 | } |
| 4903 | /* else: it's $_ */ |
| 4904 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4905 | default: |
| 4906 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4907 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4908 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4909 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4910 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4913 | #if BB_MMU |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4914 | #define encode_string(as_string, dest, input, dquote_end) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4915 | encode_string(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4916 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4917 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4918 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4919 | o_string *dest, |
| 4920 | struct in_str *input, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4921 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4922 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4923 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4924 | int next; |
| 4925 | |
| 4926 | again: |
| 4927 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4928 | if (ch != EOF) |
| 4929 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4930 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4931 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4932 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4933 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4934 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4935 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4936 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4937 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4938 | } |
| 4939 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4940 | if (ch != '\n') { |
| 4941 | next = i_peek(input); |
| 4942 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4943 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4944 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4945 | if (ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4946 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 4947 | /* Testcase: in interactive shell a file with |
| 4948 | * echo "unterminated string\<eof> |
| 4949 | * is sourced. |
| 4950 | */ |
| 4951 | syntax_error_unterm_ch('"'); |
| 4952 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4953 | } |
| 4954 | /* bash: |
| 4955 | * "The backslash retains its special meaning [in "..."] |
| 4956 | * only when followed by one of the following characters: |
| 4957 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4958 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4959 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4960 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4961 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4962 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4963 | ch = i_getch(input); /* eat next */ |
| 4964 | if (ch == '\n') |
| 4965 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4966 | } /* else: ch remains == '\\', and we double it below: */ |
| 4967 | 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] | 4968 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4969 | goto again; |
| 4970 | } |
| 4971 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4972 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4973 | debug_printf_parse("encode_string return 0: " |
| 4974 | "parse_dollar returned 0 (error)\n"); |
| 4975 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4976 | } |
| 4977 | goto again; |
| 4978 | } |
| 4979 | #if ENABLE_HUSH_TICK |
| 4980 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4981 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4982 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4983 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4984 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4985 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4986 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4987 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4988 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4989 | } |
| 4990 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4991 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4992 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4993 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4996 | /* |
| 4997 | * Scan input until EOF or end_trigger char. |
| 4998 | * Return a list of pipes to execute, or NULL on EOF |
| 4999 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5000 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5001 | * reset parsing machinery and start parsing anew, |
| 5002 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5003 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5004 | static struct pipe *parse_stream(char **pstring, |
| 5005 | struct in_str *input, |
| 5006 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5007 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5008 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5009 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5010 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5011 | /* 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] | 5012 | * 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] | 5013 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5014 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5015 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5016 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5017 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5018 | initialize_context(&ctx); |
| 5019 | |
| 5020 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5021 | * Preventing this: |
| 5022 | */ |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5023 | ctx.word.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5024 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5025 | /* We used to separate words on $IFS here. This was wrong. |
| 5026 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5027 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5028 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5029 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5030 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5031 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5032 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5033 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5034 | int ch; |
| 5035 | int next; |
| 5036 | int redir_fd; |
| 5037 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5038 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5039 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5040 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5041 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5042 | if (ch == EOF) { |
| 5043 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5044 | |
| 5045 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5046 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5047 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5048 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5049 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5050 | syntax_error_unterm_ch('('); |
| 5051 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5052 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5053 | if (end_trigger == '}') { |
| 5054 | syntax_error_unterm_ch('{'); |
| 5055 | goto parse_error; |
| 5056 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5057 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5058 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5059 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5060 | } |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5061 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5062 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5063 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5064 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5065 | /* (this makes bare "&" cmd a no-op. |
| 5066 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5067 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5068 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5069 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5070 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5071 | pi = NULL; |
| 5072 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5073 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5074 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5075 | if (pstring) |
| 5076 | *pstring = ctx.as_string.data; |
| 5077 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5078 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5079 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5080 | debug_leave(); |
| 5081 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5082 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5083 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5084 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5085 | /* Handle "'" and "\" first, as they won't play nice with |
| 5086 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5087 | * echo z\\ |
| 5088 | * and |
| 5089 | * echo '\ |
| 5090 | * ' |
| 5091 | * would break. |
| 5092 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5093 | if (ch == '\\') { |
| 5094 | ch = i_getch(input); |
| 5095 | if (ch == '\n') |
| 5096 | continue; /* drop \<newline>, get next char */ |
| 5097 | nommu_addchr(&ctx.as_string, '\\'); |
| 5098 | o_addchr(&ctx.word, '\\'); |
| 5099 | if (ch == EOF) { |
| 5100 | /* Testcase: eval 'echo Ok\' */ |
| 5101 | /* bash-4.3.43 was removing backslash, |
| 5102 | * but 4.4.19 retains it, most other shells too |
| 5103 | */ |
| 5104 | continue; /* get next char */ |
| 5105 | } |
| 5106 | /* Example: echo Hello \2>file |
| 5107 | * we need to know that word 2 is quoted |
| 5108 | */ |
| 5109 | ctx.word.has_quoted_part = 1; |
| 5110 | nommu_addchr(&ctx.as_string, ch); |
| 5111 | o_addchr(&ctx.word, ch); |
| 5112 | continue; /* get next char */ |
| 5113 | } |
| 5114 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5115 | if (ch == '\'') { |
| 5116 | ctx.word.has_quoted_part = 1; |
| 5117 | next = i_getch(input); |
| 5118 | if (next == '\'' && !ctx.pending_redirect) |
| 5119 | goto insert_empty_quoted_str_marker; |
| 5120 | |
| 5121 | ch = next; |
| 5122 | while (1) { |
| 5123 | if (ch == EOF) { |
| 5124 | syntax_error_unterm_ch('\''); |
| 5125 | goto parse_error; |
| 5126 | } |
| 5127 | nommu_addchr(&ctx.as_string, ch); |
| 5128 | if (ch == '\'') |
| 5129 | break; |
| 5130 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5131 | /* Convert raw ^C to corresponding special variable reference */ |
| 5132 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5133 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5134 | } |
| 5135 | o_addqchr(&ctx.word, ch); |
| 5136 | ch = i_getch(input); |
| 5137 | } |
| 5138 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5139 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5140 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5141 | next = '\0'; |
| 5142 | if (ch != '\n') |
| 5143 | next = i_peek_and_eat_bkslash_nl(input); |
| 5144 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5145 | is_special = "{}<>;&|()#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5146 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5147 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5148 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5149 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5150 | || ctx.word.length /* word{... - non-special */ |
| 5151 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5152 | || (next != ';' /* }; - special */ |
| 5153 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5154 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5155 | && next != '&' /* }& and }&& ... - special */ |
| 5156 | && next != '|' /* }|| ... - special */ |
| 5157 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5158 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5159 | ) { |
| 5160 | /* They are not special, skip "{}" */ |
| 5161 | is_special += 2; |
| 5162 | } |
| 5163 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5164 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5165 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5166 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5167 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5168 | o_addQchr(&ctx.word, ch); |
| 5169 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5170 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5171 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5172 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5173 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5174 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5175 | 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] | 5176 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5177 | continue; |
| 5178 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5179 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5180 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5181 | #if ENABLE_HUSH_LINENO_VAR |
| 5182 | /* Case: |
| 5183 | * "while ...; do<whitespace><newline> |
| 5184 | * cmd ..." |
| 5185 | * would think that "cmd" starts in <whitespace> - |
| 5186 | * i.e., at the previous line. |
| 5187 | * We need to skip all whitespace before newlines. |
| 5188 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5189 | while (ch != '\n') { |
| 5190 | next = i_peek(input); |
| 5191 | if (next != ' ' && next != '\t' && next != '\n') |
| 5192 | break; /* next char is not ws */ |
| 5193 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5194 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5195 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5196 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5197 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5198 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5199 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5200 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5201 | /* Is this a case when newline is simply ignored? |
| 5202 | * Some examples: |
| 5203 | * "cmd | <newline> cmd ..." |
| 5204 | * "case ... in <newline> word) ..." |
| 5205 | */ |
| 5206 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 5207 | && ctx.word.length == 0 |
| 5208 | && !ctx.word.has_quoted_part |
| 5209 | && heredoc_cnt == 0 |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5210 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5211 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5212 | * Without check #1, interactive shell |
| 5213 | * ignores even bare <newline>, |
| 5214 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5215 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5216 | * ps2> _ <=== wrong, should be ps1 |
| 5217 | * Without check #2, "cmd & <newline>" |
| 5218 | * is similarly mistreated. |
| 5219 | * (BTW, this makes "cmd & cmd" |
| 5220 | * and "cmd && cmd" non-orthogonal. |
| 5221 | * Really, ask yourself, why |
| 5222 | * "cmd && <newline>" doesn't start |
| 5223 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5224 | * The only reason is that it might be |
| 5225 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5226 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5227 | */ |
| 5228 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5229 | if (pi->num_cmds != 0 /* check #1 */ |
| 5230 | && pi->followup != PIPE_BG /* check #2 */ |
| 5231 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5232 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5233 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5234 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5235 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5236 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 5237 | debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5238 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5239 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5240 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5241 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5242 | heredoc_cnt = 0; |
| 5243 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5244 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5245 | 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] | 5246 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5247 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5248 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5249 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5250 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5251 | |
| 5252 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5253 | * } is an ordinary char in this case, even inside { cmd; } |
| 5254 | * Pathological example: { ""}; } should exec "}" cmd |
| 5255 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5256 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5257 | if (ctx.word.length != 0 /* word} */ |
| 5258 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5259 | ) { |
| 5260 | goto ordinary_char; |
| 5261 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5262 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5263 | /* Generally, there should be semicolon: "cmd; }" |
| 5264 | * However, bash allows to omit it if "cmd" is |
| 5265 | * a group. Examples: |
| 5266 | * { { echo 1; } } |
| 5267 | * {(echo 1)} |
| 5268 | * { echo 0 >&2 | { echo 1; } } |
| 5269 | * { while false; do :; done } |
| 5270 | * { case a in b) ;; esac } |
| 5271 | */ |
| 5272 | if (ctx.command->group) |
| 5273 | goto term_group; |
| 5274 | goto ordinary_char; |
| 5275 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5276 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5277 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5278 | goto skip_end_trigger; |
| 5279 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5280 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5281 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5282 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5283 | && (ch != ';' || heredoc_cnt == 0) |
| 5284 | #if ENABLE_HUSH_CASE |
| 5285 | && (ch != ')' |
| 5286 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5287 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5288 | ) |
| 5289 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5290 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5291 | if (heredoc_cnt) { |
| 5292 | /* This is technically valid: |
| 5293 | * { cat <<HERE; }; echo Ok |
| 5294 | * heredoc |
| 5295 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5296 | * HERE |
| 5297 | * but we don't support this. |
| 5298 | * We require heredoc to be in enclosing {}/(), |
| 5299 | * if any. |
| 5300 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5301 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5302 | goto parse_error; |
| 5303 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5304 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5305 | goto parse_error; |
| 5306 | } |
| 5307 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5308 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5309 | 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] | 5310 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5311 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5312 | 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] | 5313 | ) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5314 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5315 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5316 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5317 | if (pstring) |
| 5318 | *pstring = ctx.as_string.data; |
| 5319 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5320 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5321 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5322 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5323 | /* Example: bare "{ }", "()" */ |
| 5324 | G.last_exitcode = 2; /* bash compat */ |
| 5325 | syntax_error_unexpected_ch(ch); |
| 5326 | goto parse_error2; |
| 5327 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5328 | debug_printf_parse("parse_stream return %p: " |
| 5329 | "end_trigger char found\n", |
| 5330 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5331 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5332 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5333 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5334 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5335 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5336 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5337 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5338 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5339 | /* Catch <, > before deciding whether this word is |
| 5340 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5341 | switch (ch) { |
| 5342 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5343 | redir_fd = redirect_opt_num(&ctx.word); |
| 5344 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5345 | goto parse_error; |
| 5346 | } |
| 5347 | redir_style = REDIRECT_OVERWRITE; |
| 5348 | if (next == '>') { |
| 5349 | redir_style = REDIRECT_APPEND; |
| 5350 | ch = i_getch(input); |
| 5351 | nommu_addchr(&ctx.as_string, ch); |
| 5352 | } |
| 5353 | #if 0 |
| 5354 | else if (next == '(') { |
| 5355 | syntax_error(">(process) not supported"); |
| 5356 | goto parse_error; |
| 5357 | } |
| 5358 | #endif |
| 5359 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5360 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5361 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5362 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5363 | redir_fd = redirect_opt_num(&ctx.word); |
| 5364 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5365 | goto parse_error; |
| 5366 | } |
| 5367 | redir_style = REDIRECT_INPUT; |
| 5368 | if (next == '<') { |
| 5369 | redir_style = REDIRECT_HEREDOC; |
| 5370 | heredoc_cnt++; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame^] | 5371 | debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5372 | ch = i_getch(input); |
| 5373 | nommu_addchr(&ctx.as_string, ch); |
| 5374 | } else if (next == '>') { |
| 5375 | redir_style = REDIRECT_IO; |
| 5376 | ch = i_getch(input); |
| 5377 | nommu_addchr(&ctx.as_string, ch); |
| 5378 | } |
| 5379 | #if 0 |
| 5380 | else if (next == '(') { |
| 5381 | syntax_error("<(process) not supported"); |
| 5382 | goto parse_error; |
| 5383 | } |
| 5384 | #endif |
| 5385 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5386 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5387 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5388 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5389 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5390 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5391 | /* note: we do not add it to &ctx.as_string */ |
| 5392 | /* TODO: in bash: |
| 5393 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5394 | * cmd "$(cmd2 #comment)" - syntax error |
| 5395 | * cmd "`cmd2 #comment`" - ok |
| 5396 | * We accept both (comment ends where command subst ends, in both cases). |
| 5397 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5398 | while (1) { |
| 5399 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5400 | if (ch == '\n') { |
| 5401 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5402 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5403 | } |
| 5404 | ch = i_getch(input); |
| 5405 | if (ch == EOF) |
| 5406 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5407 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5408 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5409 | } |
| 5410 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5411 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5412 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5413 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5414 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5415 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5416 | && !ctx.pending_redirect |
| 5417 | ) { |
| 5418 | /* ch is a special char and thus this word |
| 5419 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5420 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5421 | 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] | 5422 | } |
| 5423 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5424 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5425 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5426 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5427 | case SPECIAL_VAR_SYMBOL: |
| 5428 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5429 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5430 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5431 | /* fall through */ |
| 5432 | case '#': |
| 5433 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5434 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5435 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5436 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5437 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5438 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5439 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5440 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5441 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5442 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5443 | case '"': |
| 5444 | ctx.word.has_quoted_part = 1; |
| 5445 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5446 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5447 | insert_empty_quoted_str_marker: |
| 5448 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5449 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5450 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5451 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5452 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5453 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5454 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5455 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"')) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5456 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5457 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5458 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5459 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5460 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5461 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5462 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5463 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5464 | o_addchr(&ctx.word, '`'); |
| 5465 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5466 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5467 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5468 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5469 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5470 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5471 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5472 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5473 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5474 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5475 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5476 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5477 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5478 | #if ENABLE_HUSH_CASE |
| 5479 | case_semi: |
| 5480 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5481 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5482 | goto parse_error; |
| 5483 | } |
| 5484 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5485 | #if ENABLE_HUSH_CASE |
| 5486 | /* Eat multiple semicolons, detect |
| 5487 | * whether it means something special */ |
| 5488 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5489 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5490 | if (ch != ';') |
| 5491 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5492 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5493 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5494 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5495 | ctx.ctx_dsemicolon = 1; |
| 5496 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5497 | break; |
| 5498 | } |
| 5499 | } |
| 5500 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5501 | new_cmd: |
| 5502 | /* We just finished a cmd. New one may start |
| 5503 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5504 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5505 | 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] | 5506 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5507 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5508 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5509 | goto parse_error; |
| 5510 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5511 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5512 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5513 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5514 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5515 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5516 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5517 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5518 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5519 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5520 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5521 | goto parse_error; |
| 5522 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5523 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5524 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5525 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5526 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5527 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5528 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5529 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5530 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5531 | } else { |
| 5532 | /* we could pick up a file descriptor choice here |
| 5533 | * with redirect_opt_num(), but bash doesn't do it. |
| 5534 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5535 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5536 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5537 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5538 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5539 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5540 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5541 | if (ctx.ctx_res_w == RES_MATCH |
| 5542 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5543 | && ctx.word.length == 0 /* not word(... */ |
| 5544 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5545 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5546 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5547 | } |
| 5548 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5549 | case '{': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5550 | if (parse_group(&ctx, input, ch) != 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5551 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5552 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5553 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5554 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5555 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5556 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5557 | goto case_semi; |
| 5558 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5559 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5560 | /* proper use of this character is caught by end_trigger: |
| 5561 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5562 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5563 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5564 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5565 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5566 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5567 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5568 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5569 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5570 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5571 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5572 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5573 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5574 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5575 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5576 | struct parse_context *pctx; |
| 5577 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5578 | |
| 5579 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5580 | * Sample for finding leaks on syntax error recovery path. |
| 5581 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5582 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5583 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5584 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5585 | * 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] | 5586 | */ |
| 5587 | pctx = &ctx; |
| 5588 | do { |
| 5589 | /* Update pipe/command counts, |
| 5590 | * otherwise freeing may miss some */ |
| 5591 | done_pipe(pctx, PIPE_SEQ); |
| 5592 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5593 | pctx->list_head, pctx); |
| 5594 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5595 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5596 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5597 | #if !BB_MMU |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5598 | o_free(&pctx->as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5599 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5600 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5601 | if (pctx != &ctx) { |
| 5602 | free(pctx); |
| 5603 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5604 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5605 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5606 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5607 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5608 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5609 | if (pstring) |
| 5610 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5611 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5612 | debug_leave(); |
| 5613 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5614 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5617 | |
| 5618 | /*** Execution routines ***/ |
| 5619 | |
| 5620 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5621 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5622 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5623 | expand_string_to_string(str) |
| 5624 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5625 | 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] | 5626 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5627 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5628 | #endif |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5629 | static int expand_vars_to_list(o_string *output, int n, char *arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5630 | |
| 5631 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5632 | * all variable references within and returns a pointer to |
| 5633 | * a list of expanded strings, possibly with larger number |
| 5634 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5635 | * This new list is allocated as a single malloc block. |
| 5636 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5637 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5638 | * Caller can deallocate entire list by single free(list). */ |
| 5639 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5640 | /* A horde of its helpers come first: */ |
| 5641 | |
| 5642 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5643 | { |
| 5644 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5645 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5646 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5647 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5648 | if (c == '{' || c == '}') { |
| 5649 | /* { -> \{, } -> \} */ |
| 5650 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5651 | /* And now we want to add { or } and continue: |
| 5652 | * o_addchr(o, c); |
| 5653 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5654 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5655 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5656 | } |
| 5657 | #endif |
| 5658 | o_addchr(o, c); |
| 5659 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5660 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5661 | o_addchr(o, '\\'); |
| 5662 | if (len) { |
| 5663 | len--; |
| 5664 | o_addchr(o, '\\'); |
| 5665 | o_addchr(o, *str++); |
| 5666 | } |
| 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5671 | /* Store given string, finalizing the word and starting new one whenever |
| 5672 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5673 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5674 | * Return in output->ended_in_ifs: |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5675 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5676 | */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5677 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5678 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5679 | int last_is_ifs = 0; |
| 5680 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5681 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5682 | int word_len; |
| 5683 | |
| 5684 | if (!*str) /* EOL - do not finalize word */ |
| 5685 | break; |
| 5686 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5687 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5688 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5689 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5690 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5691 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5692 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5693 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5694 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5695 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5696 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5697 | /*/ Why can't we do it easier? */ |
| 5698 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5699 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5700 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5701 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5702 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5703 | if (!*str) /* EOL - do not finalize word */ |
| 5704 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5705 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5706 | |
| 5707 | /* We know str here points to at least one IFS char */ |
| 5708 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5709 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5710 | if (!*str) /* EOL - do not finalize word */ |
| 5711 | break; |
| 5712 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5713 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 5714 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 5715 | ) { |
| 5716 | /* This is a non-whitespace $IFS char */ |
| 5717 | /* Skip it and IFS whitespace chars, start new word */ |
| 5718 | str++; |
| 5719 | str += strspn(str, G.ifs_whitespace); |
| 5720 | goto new_word; |
| 5721 | } |
| 5722 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5723 | /* Start new word... but not always! */ |
| 5724 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5725 | if (output->has_quoted_part |
| 5726 | /* Case "v=' a'; echo $v": |
| 5727 | * here nothing precedes the space in $v expansion, |
| 5728 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5729 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5730 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5731 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5732 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5733 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5734 | o_addchr(output, '\0'); |
| 5735 | debug_print_list("expand_on_ifs", output, n); |
| 5736 | n = o_save_ptr(output, n); |
| 5737 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5738 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5739 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5740 | output->ended_in_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5741 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5742 | return n; |
| 5743 | } |
| 5744 | |
| 5745 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5746 | * they are in double quotes, with the exception that they are not :). |
| 5747 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5748 | * |
| 5749 | * Returns malloced string. |
| 5750 | * As an optimization, we return NULL if expansion is not needed. |
| 5751 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5752 | static char *encode_then_expand_string(const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5753 | { |
| 5754 | char *exp_str; |
| 5755 | struct in_str input; |
| 5756 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5757 | const char *cp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5758 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5759 | cp = str; |
| 5760 | for (;;) { |
| 5761 | if (!*cp) return NULL; /* string has no special chars */ |
| 5762 | if (*cp == '$') break; |
| 5763 | if (*cp == '\\') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5764 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5765 | if (*cp == '`') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5766 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5767 | cp++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5768 | } |
| 5769 | |
| 5770 | /* We need to expand. Example: |
| 5771 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5772 | */ |
| 5773 | setup_string_in_str(&input, str); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5774 | encode_string(NULL, &dest, &input, EOF); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5775 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5776 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5777 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5778 | EXP_FLAG_ESC_GLOB_CHARS, |
| 5779 | /*unbackslash:*/ 1 |
| 5780 | ); |
| 5781 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5782 | o_free(&dest); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5783 | return exp_str; |
| 5784 | } |
| 5785 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5786 | /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}. |
| 5787 | * These can contain single- and double-quoted strings, |
| 5788 | * and treated as if the ARG string is initially unquoted. IOW: |
| 5789 | * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be |
| 5790 | * a dquoted string: "${var#"zz"}"), the difference only comes later |
| 5791 | * (word splitting and globbing of the ${var...} result). |
| 5792 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5793 | #if !BASH_PATTERN_SUBST |
| 5794 | #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ |
| 5795 | encode_then_expand_vararg(str, handle_squotes) |
| 5796 | #endif |
| 5797 | static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash) |
| 5798 | { |
| 5799 | #if !BASH_PATTERN_SUBST |
| 5800 | const int do_unbackslash = 0; |
| 5801 | #endif |
| 5802 | char *exp_str; |
| 5803 | struct in_str input; |
| 5804 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5805 | const char *cp; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5806 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5807 | cp = str; |
| 5808 | for (;;) { |
| 5809 | if (!*cp) return NULL; /* string has no special chars */ |
| 5810 | if (*cp == '$') break; |
| 5811 | if (*cp == '\\') break; |
| 5812 | if (*cp == '\'') break; |
| 5813 | if (*cp == '"') break; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5814 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5815 | if (*cp == '`') break; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5816 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5817 | cp++; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5818 | } |
| 5819 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5820 | setup_string_in_str(&input, str); |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5821 | dest.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5822 | exp_str = NULL; |
| 5823 | |
| 5824 | for (;;) { |
| 5825 | int ch; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5826 | |
| 5827 | ch = i_getch(&input); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5828 | debug_printf_parse("%s: ch=%c (%d) escape=%d\n", |
| 5829 | __func__, ch, ch, !!dest.o_expflags); |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5830 | |
| 5831 | if (!dest.o_expflags) { |
| 5832 | if (ch == EOF) |
| 5833 | break; |
| 5834 | if (handle_squotes && ch == '\'') { |
| 5835 | if (!add_till_single_quote_dquoted(&dest, &input)) |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5836 | goto ret; /* error */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5837 | continue; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5838 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5839 | } |
| 5840 | if (ch == EOF) { |
| 5841 | syntax_error_unterm_ch('"'); |
| 5842 | goto ret; /* error */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5843 | } |
| 5844 | if (ch == '"') { |
| 5845 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
| 5846 | continue; |
| 5847 | } |
| 5848 | if (ch == '\\') { |
| 5849 | ch = i_getch(&input); |
| 5850 | if (ch == EOF) { |
| 5851 | //example? error message? syntax_error_unterm_ch('"'); |
| 5852 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 5853 | goto ret; |
| 5854 | } |
| 5855 | o_addqchr(&dest, ch); |
| 5856 | continue; |
| 5857 | } |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5858 | if (ch == '$') { |
| 5859 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) { |
| 5860 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 5861 | goto ret; |
| 5862 | } |
| 5863 | continue; |
| 5864 | } |
| 5865 | #if ENABLE_HUSH_TICK |
| 5866 | if (ch == '`') { |
| 5867 | //unsigned pos = dest->length; |
| 5868 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5869 | o_addchr(&dest, 0x80 | '`'); |
| 5870 | if (!add_till_backquote(&dest, &input, |
| 5871 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 5872 | ) |
| 5873 | ) { |
| 5874 | goto ret; /* error */ |
| 5875 | } |
| 5876 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5877 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 5878 | continue; |
| 5879 | } |
| 5880 | #endif |
| 5881 | o_addQchr(&dest, ch); |
| 5882 | } /* for (;;) */ |
| 5883 | |
| 5884 | debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data); |
| 5885 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5886 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 5887 | do_unbackslash |
| 5888 | ); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5889 | ret: |
| 5890 | debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5891 | o_free(&dest); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5892 | return exp_str; |
| 5893 | } |
| 5894 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5895 | /* Expanding ARG in ${var+ARG}, ${var-ARG} |
| 5896 | */ |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 5897 | static int encode_then_append_var_plusminus(o_string *output, int n, |
| 5898 | const char *str, int dquoted) |
| 5899 | { |
| 5900 | struct in_str input; |
| 5901 | o_string dest = NULL_O_STRING; |
| 5902 | |
| 5903 | #if 0 //todo? |
| 5904 | const char *cp; |
| 5905 | cp = str; |
| 5906 | for (;;) { |
| 5907 | if (!*cp) return NULL; /* string has no special chars */ |
| 5908 | if (*cp == '$') break; |
| 5909 | if (*cp == '\\') break; |
| 5910 | if (*cp == '\'') break; |
| 5911 | if (*cp == '"') break; |
| 5912 | #if ENABLE_HUSH_TICK |
| 5913 | if (*cp == '`') break; |
| 5914 | #endif |
| 5915 | cp++; |
| 5916 | } |
| 5917 | #endif |
| 5918 | |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 5919 | setup_string_in_str(&input, str); |
| 5920 | |
| 5921 | for (;;) { |
| 5922 | int ch; |
| 5923 | |
| 5924 | ch = i_getch(&input); |
| 5925 | debug_printf_parse("%s: ch=%c (%d) escape=%x\n", |
| 5926 | __func__, ch, ch, dest.o_expflags); |
| 5927 | |
| 5928 | if (!dest.o_expflags) { |
| 5929 | if (ch == EOF) |
| 5930 | break; |
| 5931 | if (!dquoted && strchr(G.ifs, ch)) { |
| 5932 | /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word. |
| 5933 | * do not assume we are at the start of the word (PREFIX above). |
| 5934 | */ |
| 5935 | if (dest.data) { |
| 5936 | n = expand_vars_to_list(output, n, dest.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5937 | o_free_and_set_NULL(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 5938 | o_addchr(output, '\0'); |
| 5939 | n = o_save_ptr(output, n); /* create next word */ |
| 5940 | } else |
| 5941 | if (output->length != o_get_last_ptr(output, n) |
| 5942 | || output->has_quoted_part |
| 5943 | ) { |
| 5944 | /* For these cases: |
| 5945 | * f() { for i; do echo "|$i|"; done; }; x=x |
| 5946 | * f a${x:+ }b # 1st condition |
| 5947 | * |a| |
| 5948 | * |b| |
| 5949 | * f ""${x:+ }b # 2nd condition |
| 5950 | * || |
| 5951 | * |b| |
| 5952 | */ |
| 5953 | o_addchr(output, '\0'); |
| 5954 | n = o_save_ptr(output, n); /* create next word */ |
| 5955 | } |
| 5956 | continue; |
| 5957 | } |
| 5958 | if (!dquoted && ch == '\'') { |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5959 | if (!add_till_single_quote_dquoted(&dest, &input)) |
| 5960 | goto ret; /* error */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 5961 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5962 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 5963 | continue; |
| 5964 | } |
| 5965 | } |
| 5966 | if (ch == EOF) { |
| 5967 | syntax_error_unterm_ch('"'); |
| 5968 | goto ret; /* error */ |
| 5969 | } |
| 5970 | if (ch == '"') { |
| 5971 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 5972 | if (dest.o_expflags) { |
| 5973 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5974 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5975 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 5976 | continue; |
| 5977 | } |
| 5978 | if (ch == '\\') { |
| 5979 | ch = i_getch(&input); |
| 5980 | if (ch == EOF) { |
| 5981 | //example? error message? syntax_error_unterm_ch('"'); |
| 5982 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 5983 | goto ret; |
| 5984 | } |
| 5985 | o_addqchr(&dest, ch); |
| 5986 | continue; |
| 5987 | } |
| 5988 | if (ch == '$') { |
| 5989 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) { |
| 5990 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 5991 | goto ret; |
| 5992 | } |
| 5993 | continue; |
| 5994 | } |
| 5995 | #if ENABLE_HUSH_TICK |
| 5996 | if (ch == '`') { |
| 5997 | //unsigned pos = dest->length; |
| 5998 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5999 | o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`'); |
| 6000 | if (!add_till_backquote(&dest, &input, |
| 6001 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6002 | ) |
| 6003 | ) { |
| 6004 | goto ret; /* error */ |
| 6005 | } |
| 6006 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6007 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6008 | continue; |
| 6009 | } |
| 6010 | #endif |
Denys Vlasenko | f36caa4 | 2018-07-20 19:29:41 +0200 | [diff] [blame] | 6011 | if (dquoted) { |
| 6012 | /* Always glob-protect if in dquotes: |
| 6013 | * x=x; echo "${x:+/bin/c*}" - prints: /bin/c* |
| 6014 | * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c* |
| 6015 | */ |
| 6016 | o_addqchr(&dest, ch); |
| 6017 | } else { |
| 6018 | /* Glob-protect only if char is quoted: |
| 6019 | * x=x; echo ${x:+/bin/c*} - prints many filenames |
| 6020 | * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c* |
| 6021 | */ |
| 6022 | o_addQchr(&dest, ch); |
| 6023 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6024 | } /* for (;;) */ |
| 6025 | |
| 6026 | if (dest.data) { |
| 6027 | n = expand_vars_to_list(output, n, dest.data); |
| 6028 | } |
| 6029 | ret: |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6030 | o_free(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6031 | return n; |
| 6032 | } |
| 6033 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6034 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6035 | 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] | 6036 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6037 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6038 | arith_t res; |
| 6039 | char *exp_str; |
| 6040 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6041 | math_state.lookupvar = get_local_var_value; |
| 6042 | math_state.setvar = set_local_var_from_halves; |
| 6043 | //math_state.endofname = endofname; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6044 | exp_str = encode_then_expand_string(arg); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6045 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6046 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6047 | if (errmsg_p) |
| 6048 | *errmsg_p = math_state.errmsg; |
| 6049 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6050 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6051 | return res; |
| 6052 | } |
| 6053 | #endif |
| 6054 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6055 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6056 | /* ${var/[/]pattern[/repl]} helpers */ |
| 6057 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 6058 | { |
| 6059 | while (1) { |
| 6060 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 6061 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 6062 | if (end) { |
| 6063 | *size = end - val; |
| 6064 | return val; |
| 6065 | } |
| 6066 | if (*val == '\0') |
| 6067 | return NULL; |
| 6068 | /* Optimization: if "*pat" did not match the start of "string", |
| 6069 | * we know that "tring", "ring" etc will not match too: |
| 6070 | */ |
| 6071 | if (pattern[0] == '*') |
| 6072 | return NULL; |
| 6073 | val++; |
| 6074 | } |
| 6075 | } |
| 6076 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 6077 | { |
| 6078 | char *result = NULL; |
| 6079 | unsigned res_len = 0; |
| 6080 | unsigned repl_len = strlen(repl); |
| 6081 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6082 | /* Null pattern never matches, including if "var" is empty */ |
| 6083 | if (!pattern[0]) |
| 6084 | return result; /* NULL, no replaces happened */ |
| 6085 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6086 | while (1) { |
| 6087 | int size; |
| 6088 | char *s = strstr_pattern(val, pattern, &size); |
| 6089 | if (!s) |
| 6090 | break; |
| 6091 | |
| 6092 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6093 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 6094 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6095 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 6096 | |
| 6097 | val = s + size; |
| 6098 | if (exp_op == '/') |
| 6099 | break; |
| 6100 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6101 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6102 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 6103 | strcpy(result + res_len, val); |
| 6104 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 6105 | } |
| 6106 | debug_printf_varexp("result:'%s'\n", result); |
| 6107 | return result; |
| 6108 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6109 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6110 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6111 | static int append_str_maybe_ifs_split(o_string *output, int n, |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6112 | int first_ch, const char *val) |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6113 | { |
| 6114 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 6115 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6116 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6117 | if (val && val[0]) |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6118 | n = expand_on_ifs(output, n, val); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6119 | } else { /* quoted "$VAR" */ |
| 6120 | output->has_quoted_part = 1; |
| 6121 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6122 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6123 | if (val && val[0]) |
| 6124 | o_addQstr(output, val); |
| 6125 | } |
| 6126 | return n; |
| 6127 | } |
| 6128 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6129 | /* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6130 | */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6131 | static NOINLINE int expand_one_var(o_string *output, int n, |
| 6132 | int first_ch, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6133 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6134 | const char *val; |
| 6135 | char *to_be_freed; |
| 6136 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6137 | char *var; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6138 | char exp_op; |
| 6139 | char exp_save = exp_save; /* for compiler */ |
| 6140 | char *exp_saveptr; /* points to expansion operator */ |
| 6141 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6142 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6143 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6144 | val = NULL; |
| 6145 | to_be_freed = NULL; |
| 6146 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6147 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6148 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6149 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6150 | arg0 = arg[0]; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6151 | arg[0] = (arg0 & 0x7f); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6152 | exp_op = 0; |
| 6153 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6154 | if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 6155 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 6156 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 6157 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6158 | ) { |
| 6159 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6160 | var++; |
| 6161 | exp_op = 'L'; |
| 6162 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6163 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6164 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6165 | && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6166 | ) { |
| 6167 | /* ${?:0}, ${#[:]%0} etc */ |
| 6168 | exp_saveptr = var + 1; |
| 6169 | } else { |
| 6170 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 6171 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 6172 | } |
| 6173 | exp_op = exp_save = *exp_saveptr; |
| 6174 | if (exp_op) { |
| 6175 | exp_word = exp_saveptr + 1; |
| 6176 | if (exp_op == ':') { |
| 6177 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6178 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6179 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6180 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6181 | ) { |
| 6182 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 6183 | exp_op = ':'; |
| 6184 | exp_word--; |
| 6185 | } |
| 6186 | } |
| 6187 | *exp_saveptr = '\0'; |
| 6188 | } /* else: it's not an expansion op, but bare ${var} */ |
| 6189 | } |
| 6190 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6191 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6192 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 6193 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6194 | int nn = xatoi_positive(var); |
| 6195 | if (nn < G.global_argc) |
| 6196 | val = G.global_argv[nn]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6197 | /* else val remains NULL: $N with too big N */ |
| 6198 | } else { |
| 6199 | switch (var[0]) { |
| 6200 | case '$': /* pid */ |
| 6201 | val = utoa(G.root_pid); |
| 6202 | break; |
| 6203 | case '!': /* bg pid */ |
| 6204 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 6205 | break; |
| 6206 | case '?': /* exitcode */ |
| 6207 | val = utoa(G.last_exitcode); |
| 6208 | break; |
| 6209 | case '#': /* argc */ |
| 6210 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 6211 | break; |
| 6212 | default: |
| 6213 | val = get_local_var_value(var); |
| 6214 | } |
| 6215 | } |
| 6216 | |
| 6217 | /* Handle any expansions */ |
| 6218 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6219 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6220 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6221 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6222 | debug_printf_expand("%s\n", val); |
| 6223 | } else if (exp_op) { |
| 6224 | if (exp_op == '%' || exp_op == '#') { |
| 6225 | /* Standard-mandated substring removal ops: |
| 6226 | * ${parameter%word} - remove smallest suffix pattern |
| 6227 | * ${parameter%%word} - remove largest suffix pattern |
| 6228 | * ${parameter#word} - remove smallest prefix pattern |
| 6229 | * ${parameter##word} - remove largest prefix pattern |
| 6230 | * |
| 6231 | * Word is expanded to produce a glob pattern. |
| 6232 | * Then var's value is matched to it and matching part removed. |
| 6233 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6234 | //FIXME: ${x#...${...}...} |
| 6235 | //should evaluate inner ${...} even if x is "" and no shrinking of it is possible - |
| 6236 | //inner ${...} may have side effects! |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6237 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6238 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6239 | char *exp_exp_word; |
| 6240 | char *loc; |
| 6241 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 6242 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6243 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6244 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6245 | exp_exp_word = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6246 | if (exp_exp_word) |
| 6247 | exp_word = exp_exp_word; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6248 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
| 6249 | /* |
| 6250 | * HACK ALERT. We depend here on the fact that |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6251 | * G.global_argv and results of utoa and get_local_var_value |
| 6252 | * are actually in writable memory: |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6253 | * scan_and_match momentarily stores NULs there. |
| 6254 | */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6255 | t = (char*)val; |
| 6256 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6257 | 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] | 6258 | free(exp_exp_word); |
| 6259 | if (loc) { /* match was found */ |
| 6260 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6261 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6262 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6263 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6264 | } |
| 6265 | } |
| 6266 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6267 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6268 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6269 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6270 | * Note that in encoded form it has TWO parts: |
| 6271 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6272 | * and if // is used, it is encoded as \: |
| 6273 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6274 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6275 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6276 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6277 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6278 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6279 | * >az >bz |
| 6280 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6281 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6282 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6283 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6284 | * (note that a*z _pattern_ is never globbed!) |
| 6285 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6286 | char *pattern, *repl, *t; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6287 | pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6288 | if (!pattern) |
| 6289 | pattern = xstrdup(exp_word); |
| 6290 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6291 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6292 | exp_word = p; |
| 6293 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6294 | *p = '\0'; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6295 | repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6296 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6297 | /* HACK ALERT. We depend here on the fact that |
| 6298 | * G.global_argv and results of utoa and get_local_var_value |
| 6299 | * are actually in writable memory: |
| 6300 | * replace_pattern momentarily stores NULs there. */ |
| 6301 | t = (char*)val; |
| 6302 | to_be_freed = replace_pattern(t, |
| 6303 | pattern, |
| 6304 | (repl ? repl : exp_word), |
| 6305 | exp_op); |
| 6306 | if (to_be_freed) /* at least one replace happened */ |
| 6307 | val = to_be_freed; |
| 6308 | free(pattern); |
| 6309 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6310 | } else { |
| 6311 | /* Empty variable always gives nothing */ |
| 6312 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6313 | /* Just skip "replace" part */ |
| 6314 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6315 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6316 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6317 | } |
| 6318 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6319 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6320 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6321 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6322 | /* It's ${var:N[:M]} bashism. |
| 6323 | * Note that in encoded form it has TWO parts: |
| 6324 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6325 | */ |
| 6326 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6327 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6328 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6329 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6330 | if (errmsg) |
| 6331 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6332 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6333 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6334 | exp_word = p; |
| 6335 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6336 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6337 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6338 | if (errmsg) |
| 6339 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6340 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6341 | if (beg < 0) { |
| 6342 | /* negative beg counts from the end */ |
| 6343 | beg = (arith_t)strlen(val) + beg; |
| 6344 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6345 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6346 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6347 | debug_printf_varexp("from val:'%s'\n", val); |
| 6348 | if (len < 0) { |
| 6349 | /* in bash, len=-n means strlen()-n */ |
| 6350 | len = (arith_t)strlen(val) - beg + len; |
| 6351 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6352 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6353 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6354 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6355 | arith_err: |
| 6356 | val = NULL; |
| 6357 | } else { |
| 6358 | /* Paranoia. What if user entered 9999999999999 |
| 6359 | * which fits in arith_t but not int? */ |
| 6360 | if (len >= INT_MAX) |
| 6361 | len = INT_MAX; |
| 6362 | val = to_be_freed = xstrndup(val + beg, len); |
| 6363 | } |
| 6364 | debug_printf_varexp("val:'%s'\n", val); |
| 6365 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6366 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6367 | val = NULL; |
| 6368 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6369 | } else { /* one of "-=+?" */ |
| 6370 | /* Standard-mandated substitution ops: |
| 6371 | * ${var?word} - indicate error if unset |
| 6372 | * If var is unset, word (or a message indicating it is unset |
| 6373 | * if word is null) is written to standard error |
| 6374 | * and the shell exits with a non-zero exit status. |
| 6375 | * Otherwise, the value of var is substituted. |
| 6376 | * ${var-word} - use default value |
| 6377 | * If var is unset, word is substituted. |
| 6378 | * ${var=word} - assign and use default value |
| 6379 | * If var is unset, word is assigned to var. |
| 6380 | * In all cases, final value of var is substituted. |
| 6381 | * ${var+word} - use alternative value |
| 6382 | * If var is unset, null is substituted. |
| 6383 | * Otherwise, word is substituted. |
| 6384 | * |
| 6385 | * Word is subjected to tilde expansion, parameter expansion, |
| 6386 | * command substitution, and arithmetic expansion. |
| 6387 | * If word is not needed, it is not expanded. |
| 6388 | * |
| 6389 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6390 | * but also treat null var as if it is unset. |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6391 | * |
| 6392 | * Word-splitting and single quote behavior: |
| 6393 | * |
| 6394 | * $ f() { for i; do echo "|$i|"; done; }; |
| 6395 | * |
| 6396 | * $ x=; f ${x:?'x y' z} |
| 6397 | * bash: x: x y z #BUG: does not abort, ${} results in empty expansion |
| 6398 | * $ x=; f "${x:?'x y' z}" |
| 6399 | * bash: x: x y z # dash prints: dash: x: 'x y' z #BUG: does not abort, ${} results in "" |
| 6400 | * |
| 6401 | * $ x=; f ${x:='x y' z} |
| 6402 | * |x| |
| 6403 | * |y| |
| 6404 | * |z| |
| 6405 | * $ x=; f "${x:='x y' z}" |
| 6406 | * |'x y' z| |
| 6407 | * |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6408 | * $ x=x; f ${x:+'x y' z} |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6409 | * |x y| |
| 6410 | * |z| |
| 6411 | * $ x=x; f "${x:+'x y' z}" |
| 6412 | * |'x y' z| |
| 6413 | * |
| 6414 | * $ x=; f ${x:-'x y' z} |
| 6415 | * |x y| |
| 6416 | * |z| |
| 6417 | * $ x=; f "${x:-'x y' z}" |
| 6418 | * |'x y' z| |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6419 | */ |
| 6420 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6421 | if (exp_op == '+') |
| 6422 | use_word = !use_word; |
| 6423 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6424 | (exp_save == ':') ? "true" : "false", use_word); |
| 6425 | if (use_word) { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6426 | if (exp_op == '+' || exp_op == '-') { |
| 6427 | /* ${var+word} - use alternative value */ |
| 6428 | /* ${var-word} - use default value */ |
| 6429 | n = encode_then_append_var_plusminus(output, n, exp_word, |
| 6430 | /*dquoted:*/ (arg0 & 0x80) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6431 | ); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6432 | val = NULL; |
| 6433 | } else { |
| 6434 | /* ${var?word} - indicate error if unset */ |
| 6435 | /* ${var=word} - assign and use default value */ |
| 6436 | to_be_freed = encode_then_expand_vararg(exp_word, |
| 6437 | /*handle_squotes:*/ !(arg0 & 0x80), |
| 6438 | /*unbackslash:*/ 0 |
| 6439 | ); |
| 6440 | if (to_be_freed) |
| 6441 | exp_word = to_be_freed; |
| 6442 | if (exp_op == '?') { |
| 6443 | /* mimic bash message */ |
| 6444 | msg_and_die_if_script("%s: %s", |
| 6445 | var, |
| 6446 | exp_word[0] |
| 6447 | ? exp_word |
| 6448 | : "parameter null or not set" |
| 6449 | /* ash has more specific messages, a-la: */ |
| 6450 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
| 6451 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6452 | //TODO: how interactive bash aborts expansion mid-command? |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6453 | //It aborts the entire line, returns to prompt: |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6454 | // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO |
| 6455 | // bash: x: x y z |
| 6456 | // $ |
| 6457 | // ("echo YO" is not executed, neither the f function call) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6458 | } else { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6459 | val = exp_word; |
| 6460 | } |
| 6461 | if (exp_op == '=') { |
| 6462 | /* ${var=[word]} or ${var:=[word]} */ |
| 6463 | if (isdigit(var[0]) || var[0] == '#') { |
| 6464 | /* mimic bash message */ |
| 6465 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
| 6466 | val = NULL; |
| 6467 | } else { |
| 6468 | char *new_var = xasprintf("%s=%s", var, val); |
| 6469 | set_local_var(new_var, /*flag:*/ 0); |
| 6470 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6471 | } |
| 6472 | } |
| 6473 | } |
| 6474 | } /* one of "-=+?" */ |
| 6475 | |
| 6476 | *exp_saveptr = exp_save; |
| 6477 | } /* if (exp_op) */ |
| 6478 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6479 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6480 | *pp = p; |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6481 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6482 | n = append_str_maybe_ifs_split(output, n, first_ch, val); |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6483 | |
| 6484 | free(to_be_freed); |
| 6485 | return n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6486 | } |
| 6487 | |
| 6488 | /* Expand all variable references in given string, adding words to list[] |
| 6489 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6490 | * to be filled). This routine is extremely tricky: has to deal with |
| 6491 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6492 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6493 | 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] | 6494 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6495 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6496 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6497 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6498 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6499 | char *p; |
| 6500 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6501 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6502 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6503 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6504 | |
| 6505 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6506 | char first_ch; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6507 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6508 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6509 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6510 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6511 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6512 | o_addchr(output, '\0'); |
| 6513 | n = o_save_ptr(output, n); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6514 | output->ended_in_ifs = 0; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6515 | } |
| 6516 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6517 | o_addblock(output, arg, p - arg); |
| 6518 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6519 | arg = ++p; |
| 6520 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6521 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6522 | /* Fetch special var name (if it is indeed one of them) |
| 6523 | * and quote bit, force the bit on if singleword expansion - |
| 6524 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6525 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6526 | |
| 6527 | /* Is this variable quoted and thus expansion can't be null? |
| 6528 | * "$@" is special. Even if quoted, it can still |
| 6529 | * expand to nothing (not even an empty string), |
| 6530 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6531 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6532 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6533 | |
| 6534 | switch (first_ch & 0x7f) { |
| 6535 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6536 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6537 | case '@': { |
| 6538 | int i; |
| 6539 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6540 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6541 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6542 | 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] | 6543 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6544 | while (G.global_argv[i]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6545 | n = expand_on_ifs(output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6546 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6547 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6548 | /* this argv[] is not empty and not last: |
| 6549 | * put terminating NUL, start new word */ |
| 6550 | o_addchr(output, '\0'); |
| 6551 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6552 | n = o_save_ptr(output, n); |
| 6553 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6554 | } |
| 6555 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6556 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6557 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6558 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6559 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6560 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6561 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6562 | while (1) { |
| 6563 | o_addQstr(output, G.global_argv[i]); |
| 6564 | if (++i >= G.global_argc) |
| 6565 | break; |
| 6566 | o_addchr(output, '\0'); |
| 6567 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6568 | n = o_save_ptr(output, n); |
| 6569 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6570 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6571 | while (1) { |
| 6572 | o_addQstr(output, G.global_argv[i]); |
| 6573 | if (!G.global_argv[++i]) |
| 6574 | break; |
| 6575 | if (G.ifs[0]) |
| 6576 | o_addchr(output, G.ifs[0]); |
| 6577 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6578 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6579 | } |
| 6580 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6581 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6582 | case SPECIAL_VAR_SYMBOL: { |
| 6583 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6584 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6585 | output->has_quoted_part = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6586 | cant_be_null = 0x80; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6587 | arg++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6588 | break; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6589 | } |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6590 | case SPECIAL_VAR_QUOTED_SVS: |
| 6591 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6592 | /* "^C variable", represents literal ^C char (possible in scripts) */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6593 | o_addchr(output, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6594 | arg++; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6595 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6596 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6597 | case '`': { |
| 6598 | /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6599 | o_string subst_result = NULL_O_STRING; |
| 6600 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6601 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6602 | arg++; |
| 6603 | /* Can't just stuff it into output o_string, |
| 6604 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6605 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6606 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6607 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6608 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6609 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6610 | n = append_str_maybe_ifs_split(output, n, first_ch, subst_result.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6611 | o_free(&subst_result); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6612 | break; |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6613 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6614 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6615 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6616 | case '+': { |
| 6617 | /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6618 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6619 | |
| 6620 | arg++; /* skip '+' */ |
| 6621 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6622 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6623 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6624 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6625 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6626 | o_addstr(output, arith_buf); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6627 | break; |
| 6628 | } |
| 6629 | #endif |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6630 | default: |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6631 | /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6632 | n = expand_one_var(output, n, first_ch, arg, &p); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6633 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6634 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6635 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6636 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6637 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6638 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6639 | *p = SPECIAL_VAR_SYMBOL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6640 | arg = ++p; |
| 6641 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6642 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6643 | if (*arg) { |
| 6644 | /* handle trailing string */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6645 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6646 | o_addchr(output, '\0'); |
| 6647 | n = o_save_ptr(output, n); |
| 6648 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6649 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6650 | /* this part is literal, and it was already pre-quoted |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6651 | * if needed (much earlier), do not use o_addQstr here! |
| 6652 | */ |
| 6653 | o_addstr(output, arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6654 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6655 | } else |
| 6656 | if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6657 | && !(cant_be_null & 0x80) /* and all vars were not quoted */ |
| 6658 | && !output->has_quoted_part |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6659 | ) { |
| 6660 | n--; |
| 6661 | /* allow to reuse list[n] later without re-growth */ |
| 6662 | output->has_empty_slot = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6663 | } |
| 6664 | |
| 6665 | return n; |
| 6666 | } |
| 6667 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6668 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6669 | { |
| 6670 | int n; |
| 6671 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6672 | o_string output = NULL_O_STRING; |
| 6673 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6674 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6675 | |
| 6676 | n = 0; |
Denys Vlasenko | 57235be | 2018-07-20 14:45:12 +0200 | [diff] [blame] | 6677 | for (;;) { |
| 6678 | /* go to next list[n] */ |
| 6679 | output.ended_in_ifs = 0; |
| 6680 | n = o_save_ptr(&output, n); |
| 6681 | |
| 6682 | if (!*argv) |
| 6683 | break; |
| 6684 | |
| 6685 | /* expand argv[i] */ |
| 6686 | n = expand_vars_to_list(&output, n, *argv++); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6687 | /* if (!output->has_empty_slot) -- need this?? */ |
| 6688 | o_addchr(&output, '\0'); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6689 | } |
| 6690 | debug_print_list("expand_variables", &output, n); |
| 6691 | |
| 6692 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6693 | list = o_finalize_list(&output, n); |
| 6694 | debug_print_strings("expand_variables[1]", list); |
| 6695 | return list; |
| 6696 | } |
| 6697 | |
| 6698 | static char **expand_strvec_to_strvec(char **argv) |
| 6699 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6700 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6701 | } |
| 6702 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6703 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6704 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6705 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6706 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6707 | } |
| 6708 | #endif |
| 6709 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6710 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6711 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6712 | * |
| 6713 | * NB: should NOT do globbing! |
| 6714 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6715 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6716 | 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] | 6717 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6718 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6719 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6720 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6721 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6722 | char *argv[2], **list; |
| 6723 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6724 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6725 | /* This is generally an optimization, but it also |
| 6726 | * handles "", which otherwise trips over !list[0] check below. |
| 6727 | * (is this ever happens that we actually get str="" here?) |
| 6728 | */ |
| 6729 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6730 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6731 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6732 | return xstrdup(str); |
| 6733 | } |
| 6734 | |
| 6735 | argv[0] = (char*)str; |
| 6736 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6737 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 6738 | if (!list[0]) { |
| 6739 | /* Example where it happens: |
| 6740 | * x=; echo ${x:-"$@"} |
| 6741 | */ |
| 6742 | ((char*)list)[0] = '\0'; |
| 6743 | } else { |
| 6744 | if (HUSH_DEBUG) |
| 6745 | if (list[1]) |
| 6746 | bb_error_msg_and_die("BUG in varexp2"); |
| 6747 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6748 | overlapping_strcpy((char*)list, list[0]); |
| 6749 | if (do_unbackslash) |
| 6750 | unbackslash((char*)list); |
| 6751 | } |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6752 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6753 | return (char*)list; |
| 6754 | } |
| 6755 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6756 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6757 | static char* expand_strvec_to_string(char **argv) |
| 6758 | { |
| 6759 | char **list; |
| 6760 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6761 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6762 | /* Convert all NULs to spaces */ |
| 6763 | if (list[0]) { |
| 6764 | int n = 1; |
| 6765 | while (list[n]) { |
| 6766 | if (HUSH_DEBUG) |
| 6767 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6768 | bb_error_msg_and_die("BUG in varexp3"); |
| 6769 | /* bash uses ' ' regardless of $IFS contents */ |
| 6770 | list[n][-1] = ' '; |
| 6771 | n++; |
| 6772 | } |
| 6773 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6774 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6775 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6776 | return (char*)list; |
| 6777 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6778 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6779 | |
| 6780 | static char **expand_assignments(char **argv, int count) |
| 6781 | { |
| 6782 | int i; |
| 6783 | char **p; |
| 6784 | |
| 6785 | G.expanded_assignments = p = NULL; |
| 6786 | /* Expand assignments into one string each */ |
| 6787 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6788 | p = add_string_to_strings(p, |
| 6789 | expand_string_to_string(argv[i], |
| 6790 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6791 | /*unbackslash:*/ 1 |
| 6792 | ) |
| 6793 | ); |
| 6794 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6795 | } |
| 6796 | G.expanded_assignments = NULL; |
| 6797 | return p; |
| 6798 | } |
| 6799 | |
| 6800 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6801 | static void switch_off_special_sigs(unsigned mask) |
| 6802 | { |
| 6803 | unsigned sig = 0; |
| 6804 | while ((mask >>= 1) != 0) { |
| 6805 | sig++; |
| 6806 | if (!(mask & 1)) |
| 6807 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6808 | #if ENABLE_HUSH_TRAP |
| 6809 | if (G_traps) { |
| 6810 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6811 | /* trap is '', has to remain SIG_IGN */ |
| 6812 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6813 | free(G_traps[sig]); |
| 6814 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6815 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6816 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6817 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6818 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6819 | } |
| 6820 | } |
| 6821 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6822 | #if BB_MMU |
| 6823 | /* never called */ |
| 6824 | void re_execute_shell(char ***to_free, const char *s, |
| 6825 | char *g_argv0, char **g_argv, |
| 6826 | char **builtin_argv) NORETURN; |
| 6827 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6828 | static void reset_traps_to_defaults(void) |
| 6829 | { |
| 6830 | /* This function is always called in a child shell |
| 6831 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6832 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6833 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6834 | unsigned mask; |
| 6835 | |
| 6836 | /* Child shells are not interactive. |
| 6837 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6838 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6839 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6840 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6841 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6842 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6843 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6844 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6845 | /* Switch off special sigs */ |
| 6846 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6847 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6848 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6849 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6850 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6851 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6852 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6853 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6854 | # if ENABLE_HUSH_TRAP |
| 6855 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6856 | return; |
| 6857 | |
| 6858 | /* Reset all sigs to default except ones with empty traps */ |
| 6859 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6860 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6861 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6862 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6863 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6864 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6865 | free(G_traps[sig]); |
| 6866 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6867 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6868 | if (sig == 0) |
| 6869 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6870 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6871 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6872 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | #else /* !BB_MMU */ |
| 6876 | |
| 6877 | static void re_execute_shell(char ***to_free, const char *s, |
| 6878 | char *g_argv0, char **g_argv, |
| 6879 | char **builtin_argv) NORETURN; |
| 6880 | static void re_execute_shell(char ***to_free, const char *s, |
| 6881 | char *g_argv0, char **g_argv, |
| 6882 | char **builtin_argv) |
| 6883 | { |
| 6884 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6885 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6886 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6887 | char *heredoc_argv[4]; |
| 6888 | struct variable *cur; |
| 6889 | # if ENABLE_HUSH_FUNCTIONS |
| 6890 | struct function *funcp; |
| 6891 | # endif |
| 6892 | char **argv, **pp; |
| 6893 | unsigned cnt; |
| 6894 | unsigned long long empty_trap_mask; |
| 6895 | |
| 6896 | if (!g_argv0) { /* heredoc */ |
| 6897 | argv = heredoc_argv; |
| 6898 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6899 | argv[1] = (char *) "-<"; |
| 6900 | argv[2] = (char *) s; |
| 6901 | argv[3] = NULL; |
| 6902 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6903 | goto do_exec; |
| 6904 | } |
| 6905 | |
| 6906 | cnt = 0; |
| 6907 | pp = builtin_argv; |
| 6908 | if (pp) while (*pp++) |
| 6909 | cnt++; |
| 6910 | |
| 6911 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6912 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6913 | int sig; |
| 6914 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6915 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6916 | empty_trap_mask |= 1LL << sig; |
| 6917 | } |
| 6918 | } |
| 6919 | |
| 6920 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6921 | , (unsigned) G.root_pid |
| 6922 | , (unsigned) G.root_ppid |
| 6923 | , (unsigned) G.last_bg_pid |
| 6924 | , (unsigned) G.last_exitcode |
| 6925 | , cnt |
| 6926 | , empty_trap_mask |
| 6927 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6928 | ); |
| 6929 | # undef NOMMU_HACK_FMT |
| 6930 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6931 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6932 | */ |
| 6933 | cnt += 6; |
| 6934 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6935 | if (!cur->flg_export || cur->flg_read_only) |
| 6936 | cnt += 2; |
| 6937 | } |
| 6938 | # if ENABLE_HUSH_FUNCTIONS |
| 6939 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6940 | cnt += 3; |
| 6941 | # endif |
| 6942 | pp = g_argv; |
| 6943 | while (*pp++) |
| 6944 | cnt++; |
| 6945 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6946 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6947 | *pp++ = param_buf; |
| 6948 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6949 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6950 | continue; |
| 6951 | if (cur->flg_read_only) { |
| 6952 | *pp++ = (char *) "-R"; |
| 6953 | *pp++ = cur->varstr; |
| 6954 | } else if (!cur->flg_export) { |
| 6955 | *pp++ = (char *) "-V"; |
| 6956 | *pp++ = cur->varstr; |
| 6957 | } |
| 6958 | } |
| 6959 | # if ENABLE_HUSH_FUNCTIONS |
| 6960 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6961 | *pp++ = (char *) "-F"; |
| 6962 | *pp++ = funcp->name; |
| 6963 | *pp++ = funcp->body_as_string; |
| 6964 | } |
| 6965 | # endif |
| 6966 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6967 | * |
| 6968 | * However, POSIX says that subshells reset signals with traps |
| 6969 | * to SIG_DFL. |
| 6970 | * I tested bash-3.2 and it not only does that with true subshells |
| 6971 | * of the form ( list ), but with any forked children shells. |
| 6972 | * I set trap "echo W" WINCH; and then tried: |
| 6973 | * |
| 6974 | * { echo 1; sleep 20; echo 2; } & |
| 6975 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6976 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6977 | * |
| 6978 | * In all these cases sending SIGWINCH to the child shell |
| 6979 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6980 | * _inside_ group (just before echo 1), it works. |
| 6981 | * |
| 6982 | * 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] | 6983 | */ |
| 6984 | *pp++ = (char *) "-c"; |
| 6985 | *pp++ = (char *) s; |
| 6986 | if (builtin_argv) { |
| 6987 | while (*++builtin_argv) |
| 6988 | *pp++ = *builtin_argv; |
| 6989 | *pp++ = (char *) ""; |
| 6990 | } |
| 6991 | *pp++ = g_argv0; |
| 6992 | while (*g_argv) |
| 6993 | *pp++ = *g_argv++; |
| 6994 | /* *pp = NULL; - is already there */ |
| 6995 | pp = environ; |
| 6996 | |
| 6997 | do_exec: |
| 6998 | 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] | 6999 | /* Don't propagate SIG_IGN to the child */ |
| 7000 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7001 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7002 | execve(bb_busybox_exec_path, argv, pp); |
| 7003 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 7004 | if (argv[0][0] == '/') |
| 7005 | execve(argv[0], argv, pp); |
| 7006 | xfunc_error_retval = 127; |
| 7007 | bb_error_msg_and_die("can't re-execute the shell"); |
| 7008 | } |
| 7009 | #endif /* !BB_MMU */ |
| 7010 | |
| 7011 | |
| 7012 | static int run_and_free_list(struct pipe *pi); |
| 7013 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7014 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7015 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 7016 | * end_trigger controls how often we stop parsing |
| 7017 | * NUL: parse all, execute, return |
| 7018 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 7019 | */ |
| 7020 | 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] | 7021 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7022 | /* Why we need empty flag? |
| 7023 | * An obscure corner case "false; ``; echo $?": |
| 7024 | * empty command in `` should still set $? to 0. |
| 7025 | * But we can't just set $? to 0 at the start, |
| 7026 | * this breaks "false; echo `echo $?`" case. |
| 7027 | */ |
| 7028 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7029 | while (1) { |
| 7030 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 7031 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7032 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 7033 | if (end_trigger == ';') { |
| 7034 | G.promptmode = 0; /* PS1 */ |
| 7035 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 7036 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7037 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7038 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 7039 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 7040 | /* If we are in "big" script |
| 7041 | * (not in `cmd` or something similar)... |
| 7042 | */ |
| 7043 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 7044 | /* Discard cached input (rest of line) */ |
| 7045 | int ch = inp->last_char; |
| 7046 | while (ch != EOF && ch != '\n') { |
| 7047 | //bb_error_msg("Discarded:'%c'", ch); |
| 7048 | ch = i_getch(inp); |
| 7049 | } |
| 7050 | /* Force prompt */ |
| 7051 | inp->p = NULL; |
| 7052 | /* This stream isn't empty */ |
| 7053 | empty = 0; |
| 7054 | continue; |
| 7055 | } |
| 7056 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7057 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7058 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7059 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7060 | debug_print_tree(pipe_list, 0); |
| 7061 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7062 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7063 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7064 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 7065 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7066 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7067 | } |
| 7068 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7069 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7070 | { |
| 7071 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7072 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 7073 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7074 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7075 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7076 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7079 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7080 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7081 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7082 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 7083 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7084 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 7085 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7086 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7087 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7090 | #if ENABLE_HUSH_TICK |
| 7091 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 7092 | { |
| 7093 | pid_t pid; |
| 7094 | int channel[2]; |
| 7095 | # if !BB_MMU |
| 7096 | char **to_free = NULL; |
| 7097 | # endif |
| 7098 | |
| 7099 | xpipe(channel); |
| 7100 | pid = BB_MMU ? xfork() : xvfork(); |
| 7101 | if (pid == 0) { /* child */ |
| 7102 | disable_restore_tty_pgrp_on_exit(); |
| 7103 | /* Process substitution is not considered to be usual |
| 7104 | * 'command execution'. |
| 7105 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 7106 | */ |
| 7107 | bb_signals(0 |
| 7108 | + (1 << SIGTSTP) |
| 7109 | + (1 << SIGTTIN) |
| 7110 | + (1 << SIGTTOU) |
| 7111 | , SIG_IGN); |
| 7112 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 7113 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 7114 | xmove_fd(channel[1], 1); |
| 7115 | /* Prevent it from trying to handle ctrl-z etc */ |
| 7116 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7117 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7118 | /* Awful hack for `trap` or $(trap). |
| 7119 | * |
| 7120 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 7121 | * contains an example where "trap" is executed in a subshell: |
| 7122 | * |
| 7123 | * save_traps=$(trap) |
| 7124 | * ... |
| 7125 | * eval "$save_traps" |
| 7126 | * |
| 7127 | * Standard does not say that "trap" in subshell shall print |
| 7128 | * parent shell's traps. It only says that its output |
| 7129 | * must have suitable form, but then, in the above example |
| 7130 | * (which is not supposed to be normative), it implies that. |
| 7131 | * |
| 7132 | * bash (and probably other shell) does implement it |
| 7133 | * (traps are reset to defaults, but "trap" still shows them), |
| 7134 | * but as a result, "trap" logic is hopelessly messed up: |
| 7135 | * |
| 7136 | * # trap |
| 7137 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 7138 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 7139 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 7140 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 7141 | * trap -- 'echo Ho' SIGWINCH |
| 7142 | * # echo `(trap)` <--- in subshell in subshell - output |
| 7143 | * trap -- 'echo Ho' SIGWINCH |
| 7144 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 7145 | * trap -- 'echo Ho' SIGWINCH |
| 7146 | * |
| 7147 | * The rules when to forget and when to not forget traps |
| 7148 | * get really complex and nonsensical. |
| 7149 | * |
| 7150 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 7151 | */ |
| 7152 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 7153 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7154 | && skip_whitespace(s + 4)[0] == '\0' |
| 7155 | ) { |
| 7156 | static const char *const argv[] = { NULL, NULL }; |
| 7157 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 7158 | fflush_all(); /* important */ |
| 7159 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7160 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7161 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7162 | # if BB_MMU |
| 7163 | reset_traps_to_defaults(); |
| 7164 | parse_and_run_string(s); |
| 7165 | _exit(G.last_exitcode); |
| 7166 | # else |
| 7167 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 7168 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 7169 | * huge=`cat BIG` # was blocking here forever |
| 7170 | * echo OK |
| 7171 | */ |
| 7172 | re_execute_shell(&to_free, |
| 7173 | s, |
| 7174 | G.global_argv[0], |
| 7175 | G.global_argv + 1, |
| 7176 | NULL); |
| 7177 | # endif |
| 7178 | } |
| 7179 | |
| 7180 | /* parent */ |
| 7181 | *pid_p = pid; |
| 7182 | # if ENABLE_HUSH_FAST |
| 7183 | G.count_SIGCHLD++; |
| 7184 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 7185 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 7186 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7187 | # endif |
| 7188 | enable_restore_tty_pgrp_on_exit(); |
| 7189 | # if !BB_MMU |
| 7190 | free(to_free); |
| 7191 | # endif |
| 7192 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 7193 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7194 | } |
| 7195 | |
| 7196 | /* Return code is exit status of the process that is run. */ |
| 7197 | static int process_command_subs(o_string *dest, const char *s) |
| 7198 | { |
| 7199 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7200 | pid_t pid; |
| 7201 | int status, ch, eol_cnt; |
| 7202 | |
| 7203 | fp = generate_stream_from_string(s, &pid); |
| 7204 | |
| 7205 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7206 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7207 | while ((ch = getc(fp)) != EOF) { |
| 7208 | if (ch == '\0') |
| 7209 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7210 | if (ch == '\n') { |
| 7211 | eol_cnt++; |
| 7212 | continue; |
| 7213 | } |
| 7214 | while (eol_cnt) { |
| 7215 | o_addchr(dest, '\n'); |
| 7216 | eol_cnt--; |
| 7217 | } |
| 7218 | o_addQchr(dest, ch); |
| 7219 | } |
| 7220 | |
| 7221 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 7222 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7223 | /* We need to extract exitcode. Test case |
| 7224 | * "true; echo `sleep 1; false` $?" |
| 7225 | * should print 1 */ |
| 7226 | safe_waitpid(pid, &status, 0); |
| 7227 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 7228 | return WEXITSTATUS(status); |
| 7229 | } |
| 7230 | #endif /* ENABLE_HUSH_TICK */ |
| 7231 | |
| 7232 | |
| 7233 | static void setup_heredoc(struct redir_struct *redir) |
| 7234 | { |
| 7235 | struct fd_pair pair; |
| 7236 | pid_t pid; |
| 7237 | int len, written; |
| 7238 | /* the _body_ of heredoc (misleading field name) */ |
| 7239 | const char *heredoc = redir->rd_filename; |
| 7240 | char *expanded; |
| 7241 | #if !BB_MMU |
| 7242 | char **to_free; |
| 7243 | #endif |
| 7244 | |
| 7245 | expanded = NULL; |
| 7246 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 7247 | expanded = encode_then_expand_string(heredoc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7248 | if (expanded) |
| 7249 | heredoc = expanded; |
| 7250 | } |
| 7251 | len = strlen(heredoc); |
| 7252 | |
| 7253 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 7254 | xpiped_pair(pair); |
| 7255 | xmove_fd(pair.rd, redir->rd_fd); |
| 7256 | |
| 7257 | /* Try writing without forking. Newer kernels have |
| 7258 | * dynamically growing pipes. Must use non-blocking write! */ |
| 7259 | ndelay_on(pair.wr); |
| 7260 | while (1) { |
| 7261 | written = write(pair.wr, heredoc, len); |
| 7262 | if (written <= 0) |
| 7263 | break; |
| 7264 | len -= written; |
| 7265 | if (len == 0) { |
| 7266 | close(pair.wr); |
| 7267 | free(expanded); |
| 7268 | return; |
| 7269 | } |
| 7270 | heredoc += written; |
| 7271 | } |
| 7272 | ndelay_off(pair.wr); |
| 7273 | |
| 7274 | /* Okay, pipe buffer was not big enough */ |
| 7275 | /* Note: we must not create a stray child (bastard? :) |
| 7276 | * for the unsuspecting parent process. Child creates a grandchild |
| 7277 | * and exits before parent execs the process which consumes heredoc |
| 7278 | * (that exec happens after we return from this function) */ |
| 7279 | #if !BB_MMU |
| 7280 | to_free = NULL; |
| 7281 | #endif |
| 7282 | pid = xvfork(); |
| 7283 | if (pid == 0) { |
| 7284 | /* child */ |
| 7285 | disable_restore_tty_pgrp_on_exit(); |
| 7286 | pid = BB_MMU ? xfork() : xvfork(); |
| 7287 | if (pid != 0) |
| 7288 | _exit(0); |
| 7289 | /* grandchild */ |
| 7290 | close(redir->rd_fd); /* read side of the pipe */ |
| 7291 | #if BB_MMU |
| 7292 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 7293 | _exit(0); |
| 7294 | #else |
| 7295 | /* Delegate blocking writes to another process */ |
| 7296 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 7297 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 7298 | #endif |
| 7299 | } |
| 7300 | /* parent */ |
| 7301 | #if ENABLE_HUSH_FAST |
| 7302 | G.count_SIGCHLD++; |
| 7303 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7304 | #endif |
| 7305 | enable_restore_tty_pgrp_on_exit(); |
| 7306 | #if !BB_MMU |
| 7307 | free(to_free); |
| 7308 | #endif |
| 7309 | close(pair.wr); |
| 7310 | free(expanded); |
| 7311 | wait(NULL); /* wait till child has died */ |
| 7312 | } |
| 7313 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7314 | struct squirrel { |
| 7315 | int orig_fd; |
| 7316 | int moved_to; |
| 7317 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7318 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7319 | }; |
| 7320 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7321 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7322 | { |
| 7323 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7324 | sq[i].orig_fd = orig; |
| 7325 | sq[i].moved_to = moved; |
| 7326 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7327 | return sq; |
| 7328 | } |
| 7329 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7330 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7331 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7332 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7333 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7334 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7335 | i = 0; |
| 7336 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7337 | /* If we collide with an already moved fd... */ |
| 7338 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7339 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7340 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7341 | if (sq[i].moved_to < 0) /* what? */ |
| 7342 | xfunc_die(); |
| 7343 | return sq; |
| 7344 | } |
| 7345 | if (fd == sq[i].orig_fd) { |
| 7346 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7347 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7348 | return sq; |
| 7349 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7350 | } |
| 7351 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7352 | /* 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] | 7353 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7354 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7355 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7356 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7357 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7358 | } |
| 7359 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7360 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7361 | { |
| 7362 | int i; |
| 7363 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7364 | i = 0; |
| 7365 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7366 | /* If we collide with an already moved fd... */ |
| 7367 | if (fd == sq[i].orig_fd) { |
| 7368 | /* Examples: |
| 7369 | * "echo 3>FILE 3>&- 3>FILE" |
| 7370 | * "echo 3>&- 3>FILE" |
| 7371 | * No need for last redirect to insert |
| 7372 | * another "need to close 3" indicator. |
| 7373 | */ |
| 7374 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7375 | return sq; |
| 7376 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7380 | return append_squirrel(sq, i, fd, -1); |
| 7381 | } |
| 7382 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7383 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7384 | * Move all conflicting internally used fds, |
| 7385 | * and remember them so that we can restore them later. |
| 7386 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7387 | 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] | 7388 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7389 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7390 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7391 | |
| 7392 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7393 | if (fd == G.interactive_fd) { |
| 7394 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7395 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7396 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7397 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7398 | } |
| 7399 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7400 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 7401 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 7402 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7403 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 7404 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7405 | * "fileno(fd) = new_fd" can't be done. |
| 7406 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7407 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7408 | return 0; |
| 7409 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7410 | /* If this one of script's fds? */ |
| 7411 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 7412 | return 1; /* yes. "we closed fd" */ |
| 7413 | |
| 7414 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7415 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7416 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7417 | } |
| 7418 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7419 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7420 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7421 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7422 | int i; |
| 7423 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7424 | if (sq[i].moved_to >= 0) { |
| 7425 | /* We simply die on error */ |
| 7426 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7427 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7428 | } else { |
| 7429 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7430 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7431 | close(sq[i].orig_fd); |
| 7432 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7433 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7434 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7435 | } |
| 7436 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7437 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7438 | |
| 7439 | restore_redirected_FILEs(); |
| 7440 | } |
| 7441 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7442 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7443 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7444 | { |
| 7445 | if (G_interactive_fd) |
| 7446 | close(G_interactive_fd); |
| 7447 | close_all_FILE_list(); |
| 7448 | } |
| 7449 | #endif |
| 7450 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7451 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7452 | { |
| 7453 | int i; |
| 7454 | |
| 7455 | #if ENABLE_HUSH_INTERACTIVE |
| 7456 | if (fd == G.interactive_fd) |
| 7457 | return 1; |
| 7458 | #endif |
| 7459 | /* If this one of script's fds? */ |
| 7460 | if (fd_in_FILEs(fd)) |
| 7461 | return 1; |
| 7462 | |
| 7463 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7464 | if (fd == sq[i].moved_to) |
| 7465 | return 1; |
| 7466 | } |
| 7467 | return 0; |
| 7468 | } |
| 7469 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7470 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7471 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7472 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7473 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7474 | struct redir_struct *redir; |
| 7475 | |
| 7476 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7477 | int newfd; |
| 7478 | int closed; |
| 7479 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7480 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7481 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7482 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7483 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7484 | * of the heredoc */ |
| 7485 | debug_printf_parse("set heredoc '%s'\n", |
| 7486 | redir->rd_filename); |
| 7487 | setup_heredoc(redir); |
| 7488 | continue; |
| 7489 | } |
| 7490 | |
| 7491 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7492 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7493 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7494 | int mode; |
| 7495 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7496 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7497 | /* |
| 7498 | * Examples: |
| 7499 | * "cmd >" (no filename) |
| 7500 | * "cmd > <file" (2nd redirect starts too early) |
| 7501 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7502 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7503 | continue; |
| 7504 | } |
| 7505 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7506 | p = expand_string_to_string(redir->rd_filename, |
| 7507 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7508 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7509 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7510 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7511 | /* Error message from open_or_warn can be lost |
| 7512 | * if stderr has been redirected, but bash |
| 7513 | * and ash both lose it as well |
| 7514 | * (though zsh doesn't!) |
| 7515 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7516 | return 1; |
| 7517 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7518 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7519 | /* open() gave us precisely the fd we wanted. |
| 7520 | * This means that this fd was not busy |
| 7521 | * (not opened to anywhere). |
| 7522 | * Remember to close it on restore: |
| 7523 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7524 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7525 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7526 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7527 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7528 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7529 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7530 | } |
| 7531 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7532 | if (newfd == redir->rd_fd) |
| 7533 | continue; |
| 7534 | |
| 7535 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7536 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7537 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7538 | |
| 7539 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7540 | if (newfd == REDIRFD_CLOSE) { |
| 7541 | /* "N>&-" means "close me" */ |
| 7542 | if (!closed) { |
| 7543 | /* ^^^ optimization: saving may already |
| 7544 | * have closed it. If not... */ |
| 7545 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7546 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7547 | /* Sometimes we do another close on restore, getting EBADF. |
| 7548 | * Consider "echo 3>FILE 3>&-" |
| 7549 | * first redirect remembers "need to close 3", |
| 7550 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7551 | */ |
| 7552 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7553 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7554 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7555 | //errno = EBADF; |
| 7556 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7557 | newfd = -1; /* same effect as code above */ |
| 7558 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7559 | xdup2(newfd, redir->rd_fd); |
| 7560 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7561 | /* "rd_fd > FILE" */ |
| 7562 | close(newfd); |
| 7563 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7564 | } |
| 7565 | } |
| 7566 | return 0; |
| 7567 | } |
| 7568 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7569 | static char *find_in_path(const char *arg) |
| 7570 | { |
| 7571 | char *ret = NULL; |
| 7572 | const char *PATH = get_local_var_value("PATH"); |
| 7573 | |
| 7574 | if (!PATH) |
| 7575 | return NULL; |
| 7576 | |
| 7577 | while (1) { |
| 7578 | const char *end = strchrnul(PATH, ':'); |
| 7579 | int sz = end - PATH; /* must be int! */ |
| 7580 | |
| 7581 | free(ret); |
| 7582 | if (sz != 0) { |
| 7583 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7584 | } else { |
| 7585 | /* We have xxx::yyyy in $PATH, |
| 7586 | * it means "use current dir" */ |
| 7587 | ret = xstrdup(arg); |
| 7588 | } |
| 7589 | if (access(ret, F_OK) == 0) |
| 7590 | break; |
| 7591 | |
| 7592 | if (*end == '\0') { |
| 7593 | free(ret); |
| 7594 | return NULL; |
| 7595 | } |
| 7596 | PATH = end + 1; |
| 7597 | } |
| 7598 | |
| 7599 | return ret; |
| 7600 | } |
| 7601 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7602 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7603 | const struct built_in_command *x, |
| 7604 | const struct built_in_command *end) |
| 7605 | { |
| 7606 | while (x != end) { |
| 7607 | if (strcmp(name, x->b_cmd) != 0) { |
| 7608 | x++; |
| 7609 | continue; |
| 7610 | } |
| 7611 | debug_printf_exec("found builtin '%s'\n", name); |
| 7612 | return x; |
| 7613 | } |
| 7614 | return NULL; |
| 7615 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7616 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7617 | { |
| 7618 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7619 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7620 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7621 | { |
| 7622 | const struct built_in_command *x = find_builtin1(name); |
| 7623 | if (x) |
| 7624 | return x; |
| 7625 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7626 | } |
| 7627 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 7628 | static void remove_nested_vars(void) |
| 7629 | { |
| 7630 | struct variable *cur; |
| 7631 | struct variable **cur_pp; |
| 7632 | |
| 7633 | cur_pp = &G.top_var; |
| 7634 | while ((cur = *cur_pp) != NULL) { |
| 7635 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7636 | cur_pp = &cur->next; |
| 7637 | continue; |
| 7638 | } |
| 7639 | /* Unexport */ |
| 7640 | if (cur->flg_export) { |
| 7641 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7642 | bb_unsetenv(cur->varstr); |
| 7643 | } |
| 7644 | /* Remove from global list */ |
| 7645 | *cur_pp = cur->next; |
| 7646 | /* Free */ |
| 7647 | if (!cur->max_len) { |
| 7648 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7649 | free(cur->varstr); |
| 7650 | } |
| 7651 | free(cur); |
| 7652 | } |
| 7653 | } |
| 7654 | |
| 7655 | static void enter_var_nest_level(void) |
| 7656 | { |
| 7657 | G.var_nest_level++; |
| 7658 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7659 | |
| 7660 | /* Try: f() { echo -n .; f; }; f |
| 7661 | * struct variable::var_nest_level is uint16_t, |
| 7662 | * thus limiting recursion to < 2^16. |
| 7663 | * In any case, with 8 Mbyte stack SEGV happens |
| 7664 | * not too long after 2^16 recursions anyway. |
| 7665 | */ |
| 7666 | if (G.var_nest_level > 0xff00) |
| 7667 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7668 | } |
| 7669 | |
| 7670 | static void leave_var_nest_level(void) |
| 7671 | { |
| 7672 | G.var_nest_level--; |
| 7673 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7674 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7675 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7676 | |
| 7677 | remove_nested_vars(); |
| 7678 | } |
| 7679 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7680 | #if ENABLE_HUSH_FUNCTIONS |
| 7681 | static struct function **find_function_slot(const char *name) |
| 7682 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7683 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7684 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7685 | |
| 7686 | while ((funcp = *funcpp) != NULL) { |
| 7687 | if (strcmp(name, funcp->name) == 0) { |
| 7688 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7689 | break; |
| 7690 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7691 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7692 | } |
| 7693 | return funcpp; |
| 7694 | } |
| 7695 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7696 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7697 | { |
| 7698 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7699 | return funcp; |
| 7700 | } |
| 7701 | |
| 7702 | /* Note: takes ownership on name ptr */ |
| 7703 | static struct function *new_function(char *name) |
| 7704 | { |
| 7705 | struct function **funcpp = find_function_slot(name); |
| 7706 | struct function *funcp = *funcpp; |
| 7707 | |
| 7708 | if (funcp != NULL) { |
| 7709 | struct command *cmd = funcp->parent_cmd; |
| 7710 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7711 | if (!cmd) { |
| 7712 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7713 | free(funcp->name); |
| 7714 | /* Note: if !funcp->body, do not free body_as_string! |
| 7715 | * This is a special case of "-F name body" function: |
| 7716 | * body_as_string was not malloced! */ |
| 7717 | if (funcp->body) { |
| 7718 | free_pipe_list(funcp->body); |
| 7719 | # if !BB_MMU |
| 7720 | free(funcp->body_as_string); |
| 7721 | # endif |
| 7722 | } |
| 7723 | } else { |
| 7724 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7725 | cmd->argv[0] = funcp->name; |
| 7726 | cmd->group = funcp->body; |
| 7727 | # if !BB_MMU |
| 7728 | cmd->group_as_string = funcp->body_as_string; |
| 7729 | # endif |
| 7730 | } |
| 7731 | } else { |
| 7732 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7733 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7734 | /*funcp->next = NULL;*/ |
| 7735 | } |
| 7736 | |
| 7737 | funcp->name = name; |
| 7738 | return funcp; |
| 7739 | } |
| 7740 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7741 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7742 | static void unset_func(const char *name) |
| 7743 | { |
| 7744 | struct function **funcpp = find_function_slot(name); |
| 7745 | struct function *funcp = *funcpp; |
| 7746 | |
| 7747 | if (funcp != NULL) { |
| 7748 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7749 | *funcpp = funcp->next; |
| 7750 | /* funcp is unlinked now, deleting it. |
| 7751 | * Note: if !funcp->body, the function was created by |
| 7752 | * "-F name body", do not free ->body_as_string |
| 7753 | * and ->name as they were not malloced. */ |
| 7754 | if (funcp->body) { |
| 7755 | free_pipe_list(funcp->body); |
| 7756 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7757 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7758 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7759 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7760 | } |
| 7761 | free(funcp); |
| 7762 | } |
| 7763 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7764 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7765 | |
| 7766 | # if BB_MMU |
| 7767 | #define exec_function(to_free, funcp, argv) \ |
| 7768 | exec_function(funcp, argv) |
| 7769 | # endif |
| 7770 | static void exec_function(char ***to_free, |
| 7771 | const struct function *funcp, |
| 7772 | char **argv) NORETURN; |
| 7773 | static void exec_function(char ***to_free, |
| 7774 | const struct function *funcp, |
| 7775 | char **argv) |
| 7776 | { |
| 7777 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7778 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7779 | |
| 7780 | argv[0] = G.global_argv[0]; |
| 7781 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7782 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7783 | |
| 7784 | // Example when we are here: "cmd | func" |
| 7785 | // func will run with saved-redirect fds open. |
| 7786 | // $ f() { echo /proc/self/fd/*; } |
| 7787 | // $ true | f |
| 7788 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7789 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7790 | // Same in script: |
| 7791 | // $ . ./SCRIPT |
| 7792 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7793 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7794 | // They are CLOEXEC so external programs won't see them, but |
| 7795 | // for "more correctness" we might want to close those extra fds here: |
| 7796 | //? close_saved_fds_and_FILE_fds(); |
| 7797 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7798 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7799 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7800 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7801 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7802 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7803 | /* On MMU, funcp->body is always non-NULL */ |
| 7804 | n = run_list(funcp->body); |
| 7805 | fflush_all(); |
| 7806 | _exit(n); |
| 7807 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7808 | //? close_saved_fds_and_FILE_fds(); |
| 7809 | |
| 7810 | //TODO: check whether "true | func_with_return" works |
| 7811 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7812 | re_execute_shell(to_free, |
| 7813 | funcp->body_as_string, |
| 7814 | G.global_argv[0], |
| 7815 | argv + 1, |
| 7816 | NULL); |
| 7817 | # endif |
| 7818 | } |
| 7819 | |
| 7820 | static int run_function(const struct function *funcp, char **argv) |
| 7821 | { |
| 7822 | int rc; |
| 7823 | save_arg_t sv; |
| 7824 | smallint sv_flg; |
| 7825 | |
| 7826 | save_and_replace_G_args(&sv, argv); |
| 7827 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7828 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7829 | sv_flg = G_flag_return_in_progress; |
| 7830 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7831 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7832 | /* Make "local" variables properly shadow previous ones */ |
| 7833 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7834 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7835 | |
| 7836 | /* On MMU, funcp->body is always non-NULL */ |
| 7837 | # if !BB_MMU |
| 7838 | if (!funcp->body) { |
| 7839 | /* Function defined by -F */ |
| 7840 | parse_and_run_string(funcp->body_as_string); |
| 7841 | rc = G.last_exitcode; |
| 7842 | } else |
| 7843 | # endif |
| 7844 | { |
| 7845 | rc = run_list(funcp->body); |
| 7846 | } |
| 7847 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7848 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7849 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7850 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7851 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7852 | |
| 7853 | restore_G_args(&sv, argv); |
| 7854 | |
| 7855 | return rc; |
| 7856 | } |
| 7857 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7858 | |
| 7859 | |
| 7860 | #if BB_MMU |
| 7861 | #define exec_builtin(to_free, x, argv) \ |
| 7862 | exec_builtin(x, argv) |
| 7863 | #else |
| 7864 | #define exec_builtin(to_free, x, argv) \ |
| 7865 | exec_builtin(to_free, argv) |
| 7866 | #endif |
| 7867 | static void exec_builtin(char ***to_free, |
| 7868 | const struct built_in_command *x, |
| 7869 | char **argv) NORETURN; |
| 7870 | static void exec_builtin(char ***to_free, |
| 7871 | const struct built_in_command *x, |
| 7872 | char **argv) |
| 7873 | { |
| 7874 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7875 | int rcode; |
| 7876 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7877 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7878 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7879 | fflush_all(); |
| 7880 | _exit(rcode); |
| 7881 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7882 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7883 | /* On NOMMU, we must never block! |
| 7884 | * Example: { sleep 99 | read line; } & echo Ok |
| 7885 | */ |
| 7886 | re_execute_shell(to_free, |
| 7887 | argv[0], |
| 7888 | G.global_argv[0], |
| 7889 | G.global_argv + 1, |
| 7890 | argv); |
| 7891 | #endif |
| 7892 | } |
| 7893 | |
| 7894 | |
| 7895 | static void execvp_or_die(char **argv) NORETURN; |
| 7896 | static void execvp_or_die(char **argv) |
| 7897 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7898 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7899 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7900 | /* Don't propagate SIG_IGN to the child */ |
| 7901 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7902 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7903 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7904 | e = 2; |
| 7905 | if (errno == EACCES) e = 126; |
| 7906 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7907 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7908 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | #if ENABLE_HUSH_MODE_X |
| 7912 | static void dump_cmd_in_x_mode(char **argv) |
| 7913 | { |
| 7914 | if (G_x_mode && argv) { |
| 7915 | /* We want to output the line in one write op */ |
| 7916 | char *buf, *p; |
| 7917 | int len; |
| 7918 | int n; |
| 7919 | |
| 7920 | len = 3; |
| 7921 | n = 0; |
| 7922 | while (argv[n]) |
| 7923 | len += strlen(argv[n++]) + 1; |
| 7924 | buf = xmalloc(len); |
| 7925 | buf[0] = '+'; |
| 7926 | p = buf + 1; |
| 7927 | n = 0; |
| 7928 | while (argv[n]) |
| 7929 | p += sprintf(p, " %s", argv[n++]); |
| 7930 | *p++ = '\n'; |
| 7931 | *p = '\0'; |
| 7932 | fputs(buf, stderr); |
| 7933 | free(buf); |
| 7934 | } |
| 7935 | } |
| 7936 | #else |
| 7937 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7938 | #endif |
| 7939 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7940 | #if ENABLE_HUSH_COMMAND |
| 7941 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7942 | { |
| 7943 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7944 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7945 | if (!opt_vV) |
| 7946 | return; |
| 7947 | |
| 7948 | to_free = NULL; |
| 7949 | if (!explanation) { |
| 7950 | char *path = getenv("PATH"); |
| 7951 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7952 | if (!explanation) |
| 7953 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7954 | if (opt_vV != 'V') |
| 7955 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7956 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7957 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7958 | free(to_free); |
| 7959 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7960 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7961 | } |
| 7962 | #else |
| 7963 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7964 | #endif |
| 7965 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7966 | #if BB_MMU |
| 7967 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7968 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7969 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7970 | pseudo_exec(command, argv_expanded) |
| 7971 | #endif |
| 7972 | |
| 7973 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7974 | * Never returns. |
| 7975 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7976 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7977 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7978 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7979 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7980 | char **argv, int assignment_cnt, |
| 7981 | char **argv_expanded) NORETURN; |
| 7982 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7983 | char **argv, int assignment_cnt, |
| 7984 | char **argv_expanded) |
| 7985 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7986 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7987 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7988 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7989 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 7990 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7991 | |
| 7992 | new_env = expand_assignments(argv, assignment_cnt); |
| 7993 | dump_cmd_in_x_mode(new_env); |
| 7994 | |
| 7995 | if (!argv[assignment_cnt]) { |
| 7996 | /* Case when we are here: ... | var=val | ... |
| 7997 | * (note that we do not exit early, i.e., do not optimize out |
| 7998 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7999 | */ |
| 8000 | free_strings(new_env); |
| 8001 | _exit(EXIT_SUCCESS); |
| 8002 | } |
| 8003 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8004 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8005 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8006 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8007 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8008 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8009 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8010 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8011 | set_vars_and_save_old(new_env); |
| 8012 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8013 | |
| 8014 | if (argv_expanded) { |
| 8015 | argv = argv_expanded; |
| 8016 | } else { |
| 8017 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 8018 | #if !BB_MMU |
| 8019 | nommu_save->argv = argv; |
| 8020 | #endif |
| 8021 | } |
| 8022 | dump_cmd_in_x_mode(argv); |
| 8023 | |
| 8024 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8025 | if (strchr(argv[0], '/') != NULL) |
| 8026 | goto skip; |
| 8027 | #endif |
| 8028 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8029 | #if ENABLE_HUSH_FUNCTIONS |
| 8030 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8031 | funcp = find_function(argv[0]); |
| 8032 | if (funcp) |
| 8033 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8034 | #endif |
| 8035 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8036 | #if ENABLE_HUSH_COMMAND |
| 8037 | /* "command BAR": run BAR without looking it up among functions |
| 8038 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 8039 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 8040 | */ |
| 8041 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 8042 | char *p; |
| 8043 | |
| 8044 | argv++; |
| 8045 | p = *argv; |
| 8046 | if (p[0] != '-' || !p[1]) |
| 8047 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 8048 | |
| 8049 | for (;;) { |
| 8050 | p++; |
| 8051 | switch (*p) { |
| 8052 | case '\0': |
| 8053 | argv++; |
| 8054 | p = *argv; |
| 8055 | if (p[0] != '-' || !p[1]) |
| 8056 | goto after_opts; |
| 8057 | continue; /* next arg is also -opts, process it too */ |
| 8058 | case 'v': |
| 8059 | case 'V': |
| 8060 | opt_vV = *p; |
| 8061 | continue; |
| 8062 | default: |
| 8063 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 8064 | } |
| 8065 | } |
| 8066 | } |
| 8067 | after_opts: |
| 8068 | # if ENABLE_HUSH_FUNCTIONS |
| 8069 | if (opt_vV && find_function(argv[0])) |
| 8070 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 8071 | # endif |
| 8072 | #endif |
| 8073 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8074 | /* Check if the command matches any of the builtins. |
| 8075 | * Depending on context, this might be redundant. But it's |
| 8076 | * easier to waste a few CPU cycles than it is to figure out |
| 8077 | * if this is one of those cases. |
| 8078 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8079 | /* Why "BB_MMU ? :" difference in logic? - |
| 8080 | * On NOMMU, it is more expensive to re-execute shell |
| 8081 | * just in order to run echo or test builtin. |
| 8082 | * It's better to skip it here and run corresponding |
| 8083 | * non-builtin later. */ |
| 8084 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 8085 | if (x) { |
| 8086 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 8087 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8088 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8089 | |
| 8090 | #if ENABLE_FEATURE_SH_STANDALONE |
| 8091 | /* Check if the command matches any busybox applets */ |
| 8092 | { |
| 8093 | int a = find_applet_by_name(argv[0]); |
| 8094 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8095 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8096 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 8097 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8098 | /* Do not leak open fds from opened script files etc. |
| 8099 | * Testcase: interactive "ls -l /proc/self/fd" |
| 8100 | * should not show tty fd open. |
| 8101 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8102 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8103 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 8104 | //This casuses test failures in |
| 8105 | //redir_children_should_not_see_saved_fd_2.tests |
| 8106 | //redir_children_should_not_see_saved_fd_3.tests |
| 8107 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8108 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 8109 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 8110 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8111 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8112 | } |
| 8113 | # endif |
| 8114 | /* Re-exec ourselves */ |
| 8115 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8116 | /* Don't propagate SIG_IGN to the child */ |
| 8117 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8118 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8119 | execv(bb_busybox_exec_path, argv); |
| 8120 | /* If they called chroot or otherwise made the binary no longer |
| 8121 | * executable, fall through */ |
| 8122 | } |
| 8123 | } |
| 8124 | #endif |
| 8125 | |
| 8126 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8127 | skip: |
| 8128 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8129 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8130 | execvp_or_die(argv); |
| 8131 | } |
| 8132 | |
| 8133 | /* Called after [v]fork() in run_pipe |
| 8134 | */ |
| 8135 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8136 | struct command *command, |
| 8137 | char **argv_expanded) NORETURN; |
| 8138 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8139 | struct command *command, |
| 8140 | char **argv_expanded) |
| 8141 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 8142 | #if ENABLE_HUSH_FUNCTIONS |
| 8143 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8144 | /* Ignore funcdefs in pipes: |
| 8145 | * true | f() { cmd } |
| 8146 | */ |
| 8147 | _exit(0); |
| 8148 | } |
| 8149 | #endif |
| 8150 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8151 | if (command->argv) { |
| 8152 | pseudo_exec_argv(nommu_save, command->argv, |
| 8153 | command->assignment_cnt, argv_expanded); |
| 8154 | } |
| 8155 | |
| 8156 | if (command->group) { |
| 8157 | /* Cases when we are here: |
| 8158 | * ( list ) |
| 8159 | * { list } & |
| 8160 | * ... | ( list ) | ... |
| 8161 | * ... | { list } | ... |
| 8162 | */ |
| 8163 | #if BB_MMU |
| 8164 | int rcode; |
| 8165 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 8166 | reset_traps_to_defaults(); |
| 8167 | rcode = run_list(command->group); |
| 8168 | /* OK to leak memory by not calling free_pipe_list, |
| 8169 | * since this process is about to exit */ |
| 8170 | _exit(rcode); |
| 8171 | #else |
| 8172 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 8173 | command->group_as_string, |
| 8174 | G.global_argv[0], |
| 8175 | G.global_argv + 1, |
| 8176 | NULL); |
| 8177 | #endif |
| 8178 | } |
| 8179 | |
| 8180 | /* Case when we are here: ... | >file */ |
| 8181 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 8182 | _exit(EXIT_SUCCESS); |
| 8183 | } |
| 8184 | |
| 8185 | #if ENABLE_HUSH_JOB |
| 8186 | static const char *get_cmdtext(struct pipe *pi) |
| 8187 | { |
| 8188 | char **argv; |
| 8189 | char *p; |
| 8190 | int len; |
| 8191 | |
| 8192 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 8193 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 8194 | * On subsequent bg argv is trashed, but we won't use it */ |
| 8195 | if (pi->cmdtext) |
| 8196 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8197 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8198 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8199 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8200 | pi->cmdtext = xzalloc(1); |
| 8201 | return pi->cmdtext; |
| 8202 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8203 | len = 0; |
| 8204 | do { |
| 8205 | len += strlen(*argv) + 1; |
| 8206 | } while (*++argv); |
| 8207 | p = xmalloc(len); |
| 8208 | pi->cmdtext = p; |
| 8209 | argv = pi->cmds[0].argv; |
| 8210 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8211 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8212 | *p++ = ' '; |
| 8213 | } while (*++argv); |
| 8214 | p[-1] = '\0'; |
| 8215 | return pi->cmdtext; |
| 8216 | } |
| 8217 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8218 | static void remove_job_from_table(struct pipe *pi) |
| 8219 | { |
| 8220 | struct pipe *prev_pipe; |
| 8221 | |
| 8222 | if (pi == G.job_list) { |
| 8223 | G.job_list = pi->next; |
| 8224 | } else { |
| 8225 | prev_pipe = G.job_list; |
| 8226 | while (prev_pipe->next != pi) |
| 8227 | prev_pipe = prev_pipe->next; |
| 8228 | prev_pipe->next = pi->next; |
| 8229 | } |
| 8230 | G.last_jobid = 0; |
| 8231 | if (G.job_list) |
| 8232 | G.last_jobid = G.job_list->jobid; |
| 8233 | } |
| 8234 | |
| 8235 | static void delete_finished_job(struct pipe *pi) |
| 8236 | { |
| 8237 | remove_job_from_table(pi); |
| 8238 | free_pipe(pi); |
| 8239 | } |
| 8240 | |
| 8241 | static void clean_up_last_dead_job(void) |
| 8242 | { |
| 8243 | if (G.job_list && !G.job_list->alive_cmds) |
| 8244 | delete_finished_job(G.job_list); |
| 8245 | } |
| 8246 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8247 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8248 | { |
| 8249 | struct pipe *job, **jobp; |
| 8250 | int i; |
| 8251 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8252 | clean_up_last_dead_job(); |
| 8253 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8254 | /* Find the end of the list, and find next job ID to use */ |
| 8255 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8256 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8257 | while ((job = *jobp) != NULL) { |
| 8258 | if (job->jobid > i) |
| 8259 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8260 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8261 | } |
| 8262 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8263 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8264 | /* Create a new job struct at the end */ |
| 8265 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8266 | job->next = NULL; |
| 8267 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 8268 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 8269 | for (i = 0; i < pi->num_cmds; i++) { |
| 8270 | job->cmds[i].pid = pi->cmds[i].pid; |
| 8271 | /* all other fields are not used and stay zero */ |
| 8272 | } |
| 8273 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 8274 | |
| 8275 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 8276 | 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] | 8277 | G.last_jobid = job->jobid; |
| 8278 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8279 | #endif /* JOB */ |
| 8280 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8281 | static int job_exited_or_stopped(struct pipe *pi) |
| 8282 | { |
| 8283 | int rcode, i; |
| 8284 | |
| 8285 | if (pi->alive_cmds != pi->stopped_cmds) |
| 8286 | return -1; |
| 8287 | |
| 8288 | /* All processes in fg pipe have exited or stopped */ |
| 8289 | rcode = 0; |
| 8290 | i = pi->num_cmds; |
| 8291 | while (--i >= 0) { |
| 8292 | rcode = pi->cmds[i].cmd_exitcode; |
| 8293 | /* usually last process gives overall exitstatus, |
| 8294 | * but with "set -o pipefail", last *failed* process does */ |
| 8295 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 8296 | break; |
| 8297 | } |
| 8298 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8299 | return rcode; |
| 8300 | } |
| 8301 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8302 | 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] | 8303 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8304 | #if ENABLE_HUSH_JOB |
| 8305 | struct pipe *pi; |
| 8306 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8307 | int i, dead; |
| 8308 | |
| 8309 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 8310 | |
| 8311 | #if DEBUG_JOBS |
| 8312 | if (WIFSTOPPED(status)) |
| 8313 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8314 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8315 | if (WIFSIGNALED(status)) |
| 8316 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8317 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8318 | if (WIFEXITED(status)) |
| 8319 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8320 | childpid, WEXITSTATUS(status)); |
| 8321 | #endif |
| 8322 | /* Were we asked to wait for a fg pipe? */ |
| 8323 | if (fg_pipe) { |
| 8324 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8325 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8326 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8327 | int rcode; |
| 8328 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8329 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8330 | if (fg_pipe->cmds[i].pid != childpid) |
| 8331 | continue; |
| 8332 | if (dead) { |
| 8333 | int ex; |
| 8334 | fg_pipe->cmds[i].pid = 0; |
| 8335 | fg_pipe->alive_cmds--; |
| 8336 | ex = WEXITSTATUS(status); |
| 8337 | /* bash prints killer signal's name for *last* |
| 8338 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8339 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8340 | */ |
| 8341 | if (WIFSIGNALED(status)) { |
| 8342 | int sig = WTERMSIG(status); |
| 8343 | if (i == fg_pipe->num_cmds-1) |
| 8344 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 8345 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 8346 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8347 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8348 | * Maybe we need to use sig | 128? */ |
| 8349 | ex = sig + 128; |
| 8350 | } |
| 8351 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8352 | } else { |
| 8353 | fg_pipe->stopped_cmds++; |
| 8354 | } |
| 8355 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8356 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8357 | rcode = job_exited_or_stopped(fg_pipe); |
| 8358 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8359 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8360 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8361 | * and "killall -STOP cat" */ |
| 8362 | if (G_interactive_fd) { |
| 8363 | #if ENABLE_HUSH_JOB |
| 8364 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8365 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8366 | #endif |
| 8367 | return rcode; |
| 8368 | } |
| 8369 | if (fg_pipe->alive_cmds == 0) |
| 8370 | return rcode; |
| 8371 | } |
| 8372 | /* There are still running processes in the fg_pipe */ |
| 8373 | return -1; |
| 8374 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8375 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8376 | } |
| 8377 | |
| 8378 | #if ENABLE_HUSH_JOB |
| 8379 | /* We were asked to wait for bg or orphaned children */ |
| 8380 | /* No need to remember exitcode in this case */ |
| 8381 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8382 | for (i = 0; i < pi->num_cmds; i++) { |
| 8383 | if (pi->cmds[i].pid == childpid) |
| 8384 | goto found_pi_and_prognum; |
| 8385 | } |
| 8386 | } |
| 8387 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8388 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8389 | return -1; /* this wasn't a process from fg_pipe */ |
| 8390 | |
| 8391 | found_pi_and_prognum: |
| 8392 | if (dead) { |
| 8393 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8394 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8395 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8396 | rcode = 128 + WTERMSIG(status); |
| 8397 | pi->cmds[i].cmd_exitcode = rcode; |
| 8398 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8399 | G.last_bg_pid_exitcode = rcode; |
| 8400 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8401 | pi->alive_cmds--; |
| 8402 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8403 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8404 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8405 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8406 | delete_finished_job(pi); |
| 8407 | } else { |
| 8408 | /* |
| 8409 | * bash deletes finished jobs from job table only in interactive mode, |
| 8410 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8411 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8412 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8413 | * We only retain one "dead" job, if it's the single job on the list. |
| 8414 | * This covers most of real-world scenarios where this is useful. |
| 8415 | */ |
| 8416 | if (pi != G.job_list) |
| 8417 | delete_finished_job(pi); |
| 8418 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8419 | } |
| 8420 | } else { |
| 8421 | /* child stopped */ |
| 8422 | pi->stopped_cmds++; |
| 8423 | } |
| 8424 | #endif |
| 8425 | return -1; /* this wasn't a process from fg_pipe */ |
| 8426 | } |
| 8427 | |
| 8428 | /* Check to see if any processes have exited -- if they have, |
| 8429 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8430 | * |
| 8431 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8432 | * Return its exitcode or zero if stopped. |
| 8433 | * |
| 8434 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8435 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8436 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8437 | * or 0 if no children changed status. |
| 8438 | * |
| 8439 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8440 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8441 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8442 | */ |
| 8443 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8444 | { |
| 8445 | int attributes; |
| 8446 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8447 | int rcode = 0; |
| 8448 | |
| 8449 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8450 | |
| 8451 | attributes = WUNTRACED; |
| 8452 | if (fg_pipe == NULL) |
| 8453 | attributes |= WNOHANG; |
| 8454 | |
| 8455 | errno = 0; |
| 8456 | #if ENABLE_HUSH_FAST |
| 8457 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8458 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8459 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8460 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8461 | /* Avoid doing waitpid syscall if possible */ |
| 8462 | if (!G.we_have_children) { |
| 8463 | errno = ECHILD; |
| 8464 | return -1; |
| 8465 | } |
| 8466 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8467 | /* We have children, but they did not exit |
| 8468 | * or stop yet (we saw no SIGCHLD) */ |
| 8469 | return 0; |
| 8470 | } |
| 8471 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8472 | } |
| 8473 | #endif |
| 8474 | |
| 8475 | /* Do we do this right? |
| 8476 | * bash-3.00# sleep 20 | false |
| 8477 | * <ctrl-Z pressed> |
| 8478 | * [3]+ Stopped sleep 20 | false |
| 8479 | * bash-3.00# echo $? |
| 8480 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8481 | * [hush 1.14.0: yes we do it right] |
| 8482 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8483 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8484 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8485 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8486 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8487 | i = G.count_SIGCHLD; |
| 8488 | #endif |
| 8489 | childpid = waitpid(-1, &status, attributes); |
| 8490 | if (childpid <= 0) { |
| 8491 | if (childpid && errno != ECHILD) |
| 8492 | bb_perror_msg("waitpid"); |
| 8493 | #if ENABLE_HUSH_FAST |
| 8494 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8495 | G.we_have_children = (childpid == 0); |
| 8496 | G.handled_SIGCHLD = i; |
| 8497 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8498 | } |
| 8499 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8500 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8501 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8502 | break; |
| 8503 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8504 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8505 | if (rcode >= 0) { |
| 8506 | /* fg_pipe exited or stopped */ |
| 8507 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8508 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8509 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8510 | 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] | 8511 | rcode = WEXITSTATUS(status); |
| 8512 | if (WIFSIGNALED(status)) |
| 8513 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8514 | if (WIFSTOPPED(status)) |
| 8515 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8516 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8517 | rcode++; |
| 8518 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8519 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8520 | /* This wasn't one of our processes, or */ |
| 8521 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8522 | } /* while (waitpid succeeds)... */ |
| 8523 | |
| 8524 | return rcode; |
| 8525 | } |
| 8526 | |
| 8527 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8528 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8529 | { |
| 8530 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8531 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8532 | if (G_saved_tty_pgrp) { |
| 8533 | /* Job finished, move the shell to the foreground */ |
| 8534 | p = getpgrp(); /* our process group id */ |
| 8535 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8536 | tcsetpgrp(G_interactive_fd, p); |
| 8537 | } |
| 8538 | return rcode; |
| 8539 | } |
| 8540 | #endif |
| 8541 | |
| 8542 | /* Start all the jobs, but don't wait for anything to finish. |
| 8543 | * See checkjobs(). |
| 8544 | * |
| 8545 | * Return code is normally -1, when the caller has to wait for children |
| 8546 | * to finish to determine the exit status of the pipe. If the pipe |
| 8547 | * is a simple builtin command, however, the action is done by the |
| 8548 | * time run_pipe returns, and the exit code is provided as the |
| 8549 | * return value. |
| 8550 | * |
| 8551 | * Returns -1 only if started some children. IOW: we have to |
| 8552 | * mask out retvals of builtins etc with 0xff! |
| 8553 | * |
| 8554 | * The only case when we do not need to [v]fork is when the pipe |
| 8555 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8556 | * cmd ; ... { list } ; ... |
| 8557 | * cmd && ... { list } && ... |
| 8558 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8559 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8560 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8561 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8562 | * |
| 8563 | * Cases when we must fork: |
| 8564 | * non-single: cmd | cmd |
| 8565 | * backgrounded: cmd & { list } & |
| 8566 | * subshell: ( list ) [&] |
| 8567 | */ |
| 8568 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | c96bb2c | 2018-06-26 15:43:56 +0200 | [diff] [blame] | 8569 | #define redirect_and_varexp_helper(command, squirrel, argv_expanded) \ |
| 8570 | redirect_and_varexp_helper(command, squirrel) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8571 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8572 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8573 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8574 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8575 | char **argv_expanded) |
| 8576 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8577 | /* Assignments occur before redirects. Try: |
| 8578 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8579 | */ |
| 8580 | |
| 8581 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8582 | dump_cmd_in_x_mode(new_env); |
| 8583 | dump_cmd_in_x_mode(argv_expanded); |
| 8584 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8585 | set_vars_and_save_old(new_env); |
| 8586 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8587 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8588 | * This is perfect for work that comes after exec(). |
| 8589 | * Is it really safe for inline use? Experimentally, |
| 8590 | * things seem to work. */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8591 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8592 | } |
| 8593 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8594 | { |
| 8595 | static const char *const null_ptr = NULL; |
| 8596 | |
| 8597 | int cmd_no; |
| 8598 | int next_infd; |
| 8599 | struct command *command; |
| 8600 | char **argv_expanded; |
| 8601 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8602 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8603 | int rcode; |
| 8604 | |
| 8605 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8606 | debug_enter(); |
| 8607 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8608 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8609 | * Result should be 3 lines: q w e, qwe, q w e |
| 8610 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8611 | if (G.ifs_whitespace != G.ifs) |
| 8612 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8613 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8614 | if (G.ifs) { |
| 8615 | char *p; |
| 8616 | G.ifs_whitespace = (char*)G.ifs; |
| 8617 | p = skip_whitespace(G.ifs); |
| 8618 | if (*p) { |
| 8619 | /* Not all $IFS is whitespace */ |
| 8620 | char *d; |
| 8621 | int len = p - G.ifs; |
| 8622 | p = skip_non_whitespace(p); |
| 8623 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 8624 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 8625 | while (*p) { |
| 8626 | if (isspace(*p)) |
| 8627 | *d++ = *p; |
| 8628 | p++; |
| 8629 | } |
| 8630 | *d = '\0'; |
| 8631 | } |
| 8632 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8633 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8634 | G.ifs_whitespace = (char*)G.ifs; |
| 8635 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8636 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8637 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8638 | pi->stopped_cmds = 0; |
| 8639 | command = &pi->cmds[0]; |
| 8640 | argv_expanded = NULL; |
| 8641 | |
| 8642 | if (pi->num_cmds != 1 |
| 8643 | || pi->followup == PIPE_BG |
| 8644 | || command->cmd_type == CMD_SUBSHELL |
| 8645 | ) { |
| 8646 | goto must_fork; |
| 8647 | } |
| 8648 | |
| 8649 | pi->alive_cmds = 1; |
| 8650 | |
| 8651 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8652 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8653 | |
| 8654 | if (command->group) { |
| 8655 | #if ENABLE_HUSH_FUNCTIONS |
| 8656 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8657 | /* "executing" func () { list } */ |
| 8658 | struct function *funcp; |
| 8659 | |
| 8660 | funcp = new_function(command->argv[0]); |
| 8661 | /* funcp->name is already set to argv[0] */ |
| 8662 | funcp->body = command->group; |
| 8663 | # if !BB_MMU |
| 8664 | funcp->body_as_string = command->group_as_string; |
| 8665 | command->group_as_string = NULL; |
| 8666 | # endif |
| 8667 | command->group = NULL; |
| 8668 | command->argv[0] = NULL; |
| 8669 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8670 | funcp->parent_cmd = command; |
| 8671 | command->child_func = funcp; |
| 8672 | |
| 8673 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8674 | debug_leave(); |
| 8675 | return EXIT_SUCCESS; |
| 8676 | } |
| 8677 | #endif |
| 8678 | /* { list } */ |
| 8679 | debug_printf("non-subshell group\n"); |
| 8680 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8681 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8682 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8683 | //FIXME: we need to pass squirrel down into run_list() |
| 8684 | //for SH_STANDALONE case, or else this construct: |
| 8685 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8686 | //has no way of closing saved fd#1 for "find", |
| 8687 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8688 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8689 | rcode = run_list(command->group) & 0xff; |
| 8690 | } |
| 8691 | restore_redirects(squirrel); |
| 8692 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8693 | debug_leave(); |
| 8694 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8695 | return rcode; |
| 8696 | } |
| 8697 | |
| 8698 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8699 | { |
| 8700 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8701 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8702 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8703 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8704 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8705 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8706 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8707 | if (G.lineno_var) |
| 8708 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8709 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8710 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8711 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8712 | /* Assignments, but no command. |
| 8713 | * Ensure redirects take effect (that is, create files). |
| 8714 | * Try "a=t >file" |
| 8715 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8716 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8717 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8718 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8719 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8720 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8721 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8722 | /* Set shell variables */ |
| 8723 | if (G_x_mode) |
| 8724 | bb_putchar_stderr('+'); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8725 | i = 0; |
| 8726 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8727 | char *p = expand_string_to_string(argv[i], |
| 8728 | EXP_FLAG_ESC_GLOB_CHARS, |
| 8729 | /*unbackslash:*/ 1 |
| 8730 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8731 | if (G_x_mode) |
| 8732 | fprintf(stderr, " %s", p); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8733 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8734 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8735 | /* assignment to readonly var / putenv error? */ |
| 8736 | rcode = 1; |
| 8737 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8738 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8739 | } |
| 8740 | if (G_x_mode) |
| 8741 | bb_putchar_stderr('\n'); |
| 8742 | /* Redirect error sets $? to 1. Otherwise, |
| 8743 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8744 | * Else, clear $?: |
| 8745 | * false; q=`exit 2`; echo $? - should print 2 |
| 8746 | * false; x=1; echo $? - should print 0 |
| 8747 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 8748 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8749 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8750 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8751 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8752 | debug_leave(); |
| 8753 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8754 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8755 | } |
| 8756 | |
| 8757 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 8758 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8759 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8760 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8761 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8762 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8763 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8764 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8765 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8766 | if (!argv_expanded[0]) { |
| 8767 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8768 | /* `false` still has to set exitcode 1 */ |
| 8769 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8770 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8771 | } |
| 8772 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8773 | old_vars = NULL; |
| 8774 | sv_shadowed = G.shadowed_vars_pp; |
| 8775 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8776 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8777 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 8778 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 8779 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8780 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8781 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8782 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8783 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8784 | /* |
| 8785 | * Variable assignments are executed, but then "forgotten": |
| 8786 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 8787 | * sleeps, but prints nothing. |
| 8788 | */ |
| 8789 | enter_var_nest_level(); |
| 8790 | G.shadowed_vars_pp = &old_vars; |
| 8791 | rcode = redirect_and_varexp_helper(command, /*squirrel:*/ NULL, argv_expanded); |
| 8792 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8793 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8794 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8795 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8796 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8797 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8798 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8799 | * a=b true; env | grep ^a= |
| 8800 | */ |
| 8801 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8802 | /* Collect all variables "shadowed" by helper |
| 8803 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 8804 | * into old_vars list: |
| 8805 | */ |
| 8806 | G.shadowed_vars_pp = &old_vars; |
| 8807 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8808 | if (rcode == 0) { |
| 8809 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8810 | /* Do not collect *to old_vars list* vars shadowed |
| 8811 | * by e.g. "local VAR" builtin (collect them |
| 8812 | * in the previously nested list instead): |
| 8813 | * don't want them to be restored immediately |
| 8814 | * after "local" completes. |
| 8815 | */ |
| 8816 | G.shadowed_vars_pp = sv_shadowed; |
| 8817 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8818 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8819 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8820 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8821 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8822 | fflush_all(); |
| 8823 | } |
| 8824 | #if ENABLE_HUSH_FUNCTIONS |
| 8825 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8826 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8827 | funcp->name, argv_expanded[1]); |
| 8828 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8829 | /* |
| 8830 | * But do collect *to old_vars list* vars shadowed |
| 8831 | * within function execution. To that end, restore |
| 8832 | * this pointer _after_ function run: |
| 8833 | */ |
| 8834 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8835 | } |
| 8836 | #endif |
| 8837 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8838 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8839 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8840 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8841 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 8842 | goto must_fork; |
| 8843 | |
| 8844 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8845 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 8846 | G.shadowed_vars_pp = &old_vars; |
| 8847 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 8848 | G.shadowed_vars_pp = sv_shadowed; |
| 8849 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8850 | if (rcode == 0) { |
| 8851 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8852 | argv_expanded[0], argv_expanded[1]); |
| 8853 | /* |
| 8854 | * Note: signals (^C) can't interrupt here. |
| 8855 | * We remember them and they will be acted upon |
| 8856 | * after applet returns. |
| 8857 | * This makes applets which can run for a long time |
| 8858 | * and/or wait for user input ineligible for NOFORK: |
| 8859 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8860 | */ |
| 8861 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8862 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 8863 | } else |
| 8864 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8865 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8866 | restore_redirects(squirrel); |
| 8867 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8868 | leave_var_nest_level(); |
| 8869 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8870 | |
| 8871 | /* |
| 8872 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8873 | * with FEATURE_SH_NOFORK=y. |
| 8874 | */ |
| 8875 | if (!funcp) { |
| 8876 | /* It was builtin or nofork. |
| 8877 | * if this would be a real fork/execed program, |
| 8878 | * it should have died if a fatal sig was received. |
| 8879 | * But OTOH, there was no separate process, |
| 8880 | * the sig was sent to _shell_, not to non-existing |
| 8881 | * child. |
| 8882 | * Let's just handle ^C only, this one is obvious: |
| 8883 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8884 | * during builtin/nofork. |
| 8885 | */ |
| 8886 | if (sigismember(&G.pending_set, SIGINT)) |
| 8887 | rcode = 128 + SIGINT; |
| 8888 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8889 | free(argv_expanded); |
| 8890 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8891 | debug_leave(); |
| 8892 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8893 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8894 | } |
| 8895 | |
| 8896 | must_fork: |
| 8897 | /* NB: argv_expanded may already be created, and that |
| 8898 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8899 | * use argv_expanded if it's non-NULL */ |
| 8900 | |
| 8901 | /* Going to fork a child per each pipe member */ |
| 8902 | pi->alive_cmds = 0; |
| 8903 | next_infd = 0; |
| 8904 | |
| 8905 | cmd_no = 0; |
| 8906 | while (cmd_no < pi->num_cmds) { |
| 8907 | struct fd_pair pipefds; |
| 8908 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8909 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8910 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8911 | nommu_save.old_vars = NULL; |
| 8912 | nommu_save.argv = NULL; |
| 8913 | nommu_save.argv_from_re_execing = NULL; |
| 8914 | #endif |
| 8915 | command = &pi->cmds[cmd_no]; |
| 8916 | cmd_no++; |
| 8917 | if (command->argv) { |
| 8918 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8919 | command->argv[0], command->argv[1]); |
| 8920 | } else { |
| 8921 | debug_printf_exec(": pipe member with no argv\n"); |
| 8922 | } |
| 8923 | |
| 8924 | /* pipes are inserted between pairs of commands */ |
| 8925 | pipefds.rd = 0; |
| 8926 | pipefds.wr = 1; |
| 8927 | if (cmd_no < pi->num_cmds) |
| 8928 | xpiped_pair(pipefds); |
| 8929 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8930 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8931 | if (G.lineno_var) |
| 8932 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8933 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8934 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8935 | command->pid = BB_MMU ? fork() : vfork(); |
| 8936 | if (!command->pid) { /* child */ |
| 8937 | #if ENABLE_HUSH_JOB |
| 8938 | disable_restore_tty_pgrp_on_exit(); |
| 8939 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8940 | |
| 8941 | /* Every child adds itself to new process group |
| 8942 | * with pgid == pid_of_first_child_in_pipe */ |
| 8943 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8944 | pid_t pgrp; |
| 8945 | pgrp = pi->pgrp; |
| 8946 | if (pgrp < 0) /* true for 1st process only */ |
| 8947 | pgrp = getpid(); |
| 8948 | if (setpgid(0, pgrp) == 0 |
| 8949 | && pi->followup != PIPE_BG |
| 8950 | && G_saved_tty_pgrp /* we have ctty */ |
| 8951 | ) { |
| 8952 | /* We do it in *every* child, not just first, |
| 8953 | * to avoid races */ |
| 8954 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8955 | } |
| 8956 | } |
| 8957 | #endif |
| 8958 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8959 | /* 1st cmd in backgrounded pipe |
| 8960 | * should have its stdin /dev/null'ed */ |
| 8961 | close(0); |
| 8962 | if (open(bb_dev_null, O_RDONLY)) |
| 8963 | xopen("/", O_RDONLY); |
| 8964 | } else { |
| 8965 | xmove_fd(next_infd, 0); |
| 8966 | } |
| 8967 | xmove_fd(pipefds.wr, 1); |
| 8968 | if (pipefds.rd > 1) |
| 8969 | close(pipefds.rd); |
| 8970 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8971 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8972 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8973 | * of cmd1 goes into pipe. |
| 8974 | */ |
| 8975 | if (setup_redirects(command, NULL)) { |
| 8976 | /* Happens when redir file can't be opened: |
| 8977 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8978 | * FOO |
| 8979 | * hush: can't open '/qwe/rty': No such file or directory |
| 8980 | * BAZ |
| 8981 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8982 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8983 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8984 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8985 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8986 | /* Stores to nommu_save list of env vars putenv'ed |
| 8987 | * (NOMMU, on MMU we don't need that) */ |
| 8988 | /* cast away volatility... */ |
| 8989 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8990 | /* pseudo_exec() does not return */ |
| 8991 | } |
| 8992 | |
| 8993 | /* parent or error */ |
| 8994 | #if ENABLE_HUSH_FAST |
| 8995 | G.count_SIGCHLD++; |
| 8996 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8997 | #endif |
| 8998 | enable_restore_tty_pgrp_on_exit(); |
| 8999 | #if !BB_MMU |
| 9000 | /* Clean up after vforked child */ |
| 9001 | free(nommu_save.argv); |
| 9002 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9003 | G.var_nest_level = sv_var_nest_level; |
| 9004 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9005 | add_vars(nommu_save.old_vars); |
| 9006 | #endif |
| 9007 | free(argv_expanded); |
| 9008 | argv_expanded = NULL; |
| 9009 | if (command->pid < 0) { /* [v]fork failed */ |
| 9010 | /* Clearly indicate, was it fork or vfork */ |
| 9011 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 9012 | } else { |
| 9013 | pi->alive_cmds++; |
| 9014 | #if ENABLE_HUSH_JOB |
| 9015 | /* Second and next children need to know pid of first one */ |
| 9016 | if (pi->pgrp < 0) |
| 9017 | pi->pgrp = command->pid; |
| 9018 | #endif |
| 9019 | } |
| 9020 | |
| 9021 | if (cmd_no > 1) |
| 9022 | close(next_infd); |
| 9023 | if (cmd_no < pi->num_cmds) |
| 9024 | close(pipefds.wr); |
| 9025 | /* Pass read (output) pipe end to next iteration */ |
| 9026 | next_infd = pipefds.rd; |
| 9027 | } |
| 9028 | |
| 9029 | if (!pi->alive_cmds) { |
| 9030 | debug_leave(); |
| 9031 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 9032 | return 1; |
| 9033 | } |
| 9034 | |
| 9035 | debug_leave(); |
| 9036 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 9037 | return -1; |
| 9038 | } |
| 9039 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9040 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 9041 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 9042 | static int run_list(struct pipe *pi) |
| 9043 | { |
| 9044 | #if ENABLE_HUSH_CASE |
| 9045 | char *case_word = NULL; |
| 9046 | #endif |
| 9047 | #if ENABLE_HUSH_LOOPS |
| 9048 | struct pipe *loop_top = NULL; |
| 9049 | char **for_lcur = NULL; |
| 9050 | char **for_list = NULL; |
| 9051 | #endif |
| 9052 | smallint last_followup; |
| 9053 | smalluint rcode; |
| 9054 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 9055 | smalluint cond_code = 0; |
| 9056 | #else |
| 9057 | enum { cond_code = 0 }; |
| 9058 | #endif |
| 9059 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 9060 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9061 | smallint last_rword; /* ditto */ |
| 9062 | #endif |
| 9063 | |
| 9064 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 9065 | debug_enter(); |
| 9066 | |
| 9067 | #if ENABLE_HUSH_LOOPS |
| 9068 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 9069 | { |
| 9070 | struct pipe *cpipe; |
| 9071 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 9072 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 9073 | continue; |
| 9074 | /* current word is FOR or IN (BOLD in comments below) */ |
| 9075 | if (cpipe->next == NULL) { |
| 9076 | syntax_error("malformed for"); |
| 9077 | debug_leave(); |
| 9078 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9079 | return 1; |
| 9080 | } |
| 9081 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 9082 | if (cpipe->next->res_word == RES_DO) |
| 9083 | continue; |
| 9084 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 9085 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 9086 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 9087 | ) { |
| 9088 | syntax_error("malformed for"); |
| 9089 | debug_leave(); |
| 9090 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9091 | return 1; |
| 9092 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9093 | } |
| 9094 | } |
| 9095 | #endif |
| 9096 | |
| 9097 | /* Past this point, all code paths should jump to ret: label |
| 9098 | * in order to return, no direct "return" statements please. |
| 9099 | * This helps to ensure that no memory is leaked. */ |
| 9100 | |
| 9101 | #if ENABLE_HUSH_JOB |
| 9102 | G.run_list_level++; |
| 9103 | #endif |
| 9104 | |
| 9105 | #if HAS_KEYWORDS |
| 9106 | rword = RES_NONE; |
| 9107 | last_rword = RES_XXXX; |
| 9108 | #endif |
| 9109 | last_followup = PIPE_SEQ; |
| 9110 | rcode = G.last_exitcode; |
| 9111 | |
| 9112 | /* Go through list of pipes, (maybe) executing them. */ |
| 9113 | 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] | 9114 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9115 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9116 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9117 | if (G.flag_SIGINT) |
| 9118 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9119 | if (G_flag_return_in_progress == 1) |
| 9120 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9121 | |
| 9122 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 9123 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 9124 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9125 | |
| 9126 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9127 | if ( |
| 9128 | #if ENABLE_HUSH_IF |
| 9129 | rword == RES_IF || rword == RES_ELIF || |
| 9130 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9131 | pi->followup != PIPE_SEQ |
| 9132 | ) { |
| 9133 | G.errexit_depth++; |
| 9134 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9135 | #if ENABLE_HUSH_LOOPS |
| 9136 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 9137 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 9138 | ) { |
| 9139 | /* start of a loop: remember where loop starts */ |
| 9140 | loop_top = pi; |
| 9141 | G.depth_of_loop++; |
| 9142 | } |
| 9143 | #endif |
| 9144 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 9145 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 9146 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 9147 | || (rcode != 0 && last_followup == PIPE_AND) |
| 9148 | ) { |
| 9149 | /* It is "<true> || CMD" or "<false> && CMD" |
| 9150 | * and we should not execute CMD */ |
| 9151 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 9152 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9153 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9154 | } |
| 9155 | } |
| 9156 | last_followup = pi->followup; |
| 9157 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 9158 | #if ENABLE_HUSH_IF |
| 9159 | if (cond_code) { |
| 9160 | if (rword == RES_THEN) { |
| 9161 | /* if false; then ... fi has exitcode 0! */ |
| 9162 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9163 | /* "if <false> THEN cmd": skip cmd */ |
| 9164 | continue; |
| 9165 | } |
| 9166 | } else { |
| 9167 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 9168 | /* "if <true> then ... ELSE/ELIF cmd": |
| 9169 | * skip cmd and all following ones */ |
| 9170 | break; |
| 9171 | } |
| 9172 | } |
| 9173 | #endif |
| 9174 | #if ENABLE_HUSH_LOOPS |
| 9175 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 9176 | if (!for_lcur) { |
| 9177 | /* first loop through for */ |
| 9178 | |
| 9179 | static const char encoded_dollar_at[] ALIGN1 = { |
| 9180 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 9181 | }; /* encoded representation of "$@" */ |
| 9182 | static const char *const encoded_dollar_at_argv[] = { |
| 9183 | encoded_dollar_at, NULL |
| 9184 | }; /* argv list with one element: "$@" */ |
| 9185 | char **vals; |
| 9186 | |
| 9187 | vals = (char**)encoded_dollar_at_argv; |
| 9188 | if (pi->next->res_word == RES_IN) { |
| 9189 | /* if no variable values after "in" we skip "for" */ |
| 9190 | if (!pi->next->cmds[0].argv) { |
| 9191 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9192 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 9193 | break; |
| 9194 | } |
| 9195 | vals = pi->next->cmds[0].argv; |
| 9196 | } /* else: "for var; do..." -> assume "$@" list */ |
| 9197 | /* create list of variable values */ |
| 9198 | debug_print_strings("for_list made from", vals); |
| 9199 | for_list = expand_strvec_to_strvec(vals); |
| 9200 | for_lcur = for_list; |
| 9201 | debug_print_strings("for_list", for_list); |
| 9202 | } |
| 9203 | if (!*for_lcur) { |
| 9204 | /* "for" loop is over, clean up */ |
| 9205 | free(for_list); |
| 9206 | for_list = NULL; |
| 9207 | for_lcur = NULL; |
| 9208 | break; |
| 9209 | } |
| 9210 | /* Insert next value from for_lcur */ |
| 9211 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9212 | 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] | 9213 | continue; |
| 9214 | } |
| 9215 | if (rword == RES_IN) { |
| 9216 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 9217 | } |
| 9218 | if (rword == RES_DONE) { |
| 9219 | continue; /* "done" has no cmds too */ |
| 9220 | } |
| 9221 | #endif |
| 9222 | #if ENABLE_HUSH_CASE |
| 9223 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9224 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9225 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 9226 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 9227 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 9228 | //unbackslash(case_word); |
| 9229 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9230 | continue; |
| 9231 | } |
| 9232 | if (rword == RES_MATCH) { |
| 9233 | char **argv; |
| 9234 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9235 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9236 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 9237 | break; |
| 9238 | /* all prev words didn't match, does this one match? */ |
| 9239 | argv = pi->cmds->argv; |
| 9240 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9241 | char *pattern; |
| 9242 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 9243 | pattern = expand_string_to_string(*argv, |
| 9244 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9245 | /*unbackslash:*/ 0 |
| 9246 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9247 | /* TODO: which FNM_xxx flags to use? */ |
| 9248 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9249 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 9250 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9251 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9252 | if (cond_code == 0) { |
| 9253 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9254 | free(case_word); |
| 9255 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9256 | break; |
| 9257 | } |
| 9258 | argv++; |
| 9259 | } |
| 9260 | continue; |
| 9261 | } |
| 9262 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9263 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9264 | if (cond_code != 0) |
| 9265 | continue; /* not matched yet, skip this pipe */ |
| 9266 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9267 | if (rword == RES_ESAC) { |
| 9268 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 9269 | if (case_word) { |
| 9270 | /* "case" did not match anything: still set $? (to 0) */ |
| 9271 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9272 | } |
| 9273 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9274 | #endif |
| 9275 | /* Just pressing <enter> in shell should check for jobs. |
| 9276 | * OTOH, in non-interactive shell this is useless |
| 9277 | * and only leads to extra job checks */ |
| 9278 | if (pi->num_cmds == 0) { |
| 9279 | if (G_interactive_fd) |
| 9280 | goto check_jobs_and_continue; |
| 9281 | continue; |
| 9282 | } |
| 9283 | |
| 9284 | /* After analyzing all keywords and conditions, we decided |
| 9285 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 9286 | * after run_pipe to collect any background children, |
| 9287 | * even if list execution is to be stopped. */ |
| 9288 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9289 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9290 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9291 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9292 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 9293 | if (r != -1) { |
| 9294 | /* We ran a builtin, function, or group. |
| 9295 | * rcode is already known |
| 9296 | * and we don't need to wait for anything. */ |
| 9297 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 9298 | G.last_exitcode = rcode; |
| 9299 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9300 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9301 | /* Was it "break" or "continue"? */ |
| 9302 | if (G.flag_break_continue) { |
| 9303 | smallint fbc = G.flag_break_continue; |
| 9304 | /* We might fall into outer *loop*, |
| 9305 | * don't want to break it too */ |
| 9306 | if (loop_top) { |
| 9307 | G.depth_break_continue--; |
| 9308 | if (G.depth_break_continue == 0) |
| 9309 | G.flag_break_continue = 0; |
| 9310 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9311 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9312 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9313 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9314 | break; |
| 9315 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9316 | /* "continue": simulate end of loop */ |
| 9317 | rword = RES_DONE; |
| 9318 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9319 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9320 | #endif |
| 9321 | if (G_flag_return_in_progress == 1) { |
| 9322 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9323 | break; |
| 9324 | } |
| 9325 | } else if (pi->followup == PIPE_BG) { |
| 9326 | /* What does bash do with attempts to background builtins? */ |
| 9327 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9328 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9329 | * I'm NOT treating inner &'s as jobs */ |
| 9330 | #if ENABLE_HUSH_JOB |
| 9331 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9332 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9333 | #endif |
| 9334 | /* Last command's pid goes to $! */ |
| 9335 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9336 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9337 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9338 | /* 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] | 9339 | rcode = EXIT_SUCCESS; |
| 9340 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9341 | } else { |
| 9342 | #if ENABLE_HUSH_JOB |
| 9343 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9344 | /* Waits for completion, then fg's main shell */ |
| 9345 | rcode = checkjobs_and_fg_shell(pi); |
| 9346 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9347 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9348 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9349 | #endif |
| 9350 | /* This one just waits for completion */ |
| 9351 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 9352 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 9353 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9354 | G.last_exitcode = rcode; |
| 9355 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9356 | } |
| 9357 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9358 | /* Handle "set -e" */ |
| 9359 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 9360 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 9361 | if (G.errexit_depth == 0) |
| 9362 | hush_exit(rcode); |
| 9363 | } |
| 9364 | G.errexit_depth = sv_errexit_depth; |
| 9365 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9366 | /* Analyze how result affects subsequent commands */ |
| 9367 | #if ENABLE_HUSH_IF |
| 9368 | if (rword == RES_IF || rword == RES_ELIF) |
| 9369 | cond_code = rcode; |
| 9370 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9371 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9372 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9373 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9374 | #if ENABLE_HUSH_LOOPS |
| 9375 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9376 | if (pi->next |
| 9377 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9378 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9379 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9380 | if (rword == RES_WHILE) { |
| 9381 | if (rcode) { |
| 9382 | /* "while false; do...done" - exitcode 0 */ |
| 9383 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9384 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9385 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9386 | } |
| 9387 | } |
| 9388 | if (rword == RES_UNTIL) { |
| 9389 | if (!rcode) { |
| 9390 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9391 | break; |
| 9392 | } |
| 9393 | } |
| 9394 | } |
| 9395 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9396 | } /* for (pi) */ |
| 9397 | |
| 9398 | #if ENABLE_HUSH_JOB |
| 9399 | G.run_list_level--; |
| 9400 | #endif |
| 9401 | #if ENABLE_HUSH_LOOPS |
| 9402 | if (loop_top) |
| 9403 | G.depth_of_loop--; |
| 9404 | free(for_list); |
| 9405 | #endif |
| 9406 | #if ENABLE_HUSH_CASE |
| 9407 | free(case_word); |
| 9408 | #endif |
| 9409 | debug_leave(); |
| 9410 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9411 | return rcode; |
| 9412 | } |
| 9413 | |
| 9414 | /* Select which version we will use */ |
| 9415 | static int run_and_free_list(struct pipe *pi) |
| 9416 | { |
| 9417 | int rcode = 0; |
| 9418 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9419 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9420 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9421 | rcode = run_list(pi); |
| 9422 | } |
| 9423 | /* free_pipe_list has the side effect of clearing memory. |
| 9424 | * In the long run that function can be merged with run_list, |
| 9425 | * but doing that now would hobble the debugging effort. */ |
| 9426 | free_pipe_list(pi); |
| 9427 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9428 | return rcode; |
| 9429 | } |
| 9430 | |
| 9431 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9432 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9433 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9434 | sighandler_t old_handler; |
| 9435 | unsigned sig = 0; |
| 9436 | while ((mask >>= 1) != 0) { |
| 9437 | sig++; |
| 9438 | if (!(mask & 1)) |
| 9439 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9440 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9441 | /* POSIX allows shell to re-enable SIGCHLD |
| 9442 | * even if it was SIG_IGN on entry. |
| 9443 | * Therefore we skip IGN check for it: |
| 9444 | */ |
| 9445 | if (sig == SIGCHLD) |
| 9446 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9447 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9448 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9449 | */ |
| 9450 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9451 | if (old_handler == SIG_IGN) { |
| 9452 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9453 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9454 | #if ENABLE_HUSH_TRAP |
| 9455 | if (!G_traps) |
| 9456 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9457 | free(G_traps[sig]); |
| 9458 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9459 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9460 | } |
| 9461 | } |
| 9462 | } |
| 9463 | |
| 9464 | /* Called a few times only (or even once if "sh -c") */ |
| 9465 | static void install_special_sighandlers(void) |
| 9466 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9467 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9468 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9469 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9470 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9471 | if (G_interactive_fd) { |
| 9472 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9473 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9474 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9475 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9476 | /* Careful, do not re-install handlers we already installed */ |
| 9477 | if (G.special_sig_mask != mask) { |
| 9478 | unsigned diff = mask & ~G.special_sig_mask; |
| 9479 | G.special_sig_mask = mask; |
| 9480 | install_sighandlers(diff); |
| 9481 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9482 | } |
| 9483 | |
| 9484 | #if ENABLE_HUSH_JOB |
| 9485 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9486 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9487 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9488 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9489 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9490 | |
| 9491 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9492 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9493 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9494 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9495 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9496 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9497 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9498 | + (1 << SIGABRT) |
| 9499 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9500 | + (1 << SIGPIPE) |
| 9501 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9502 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9503 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9504 | * we never want to restore pgrp on exit, and this fn is not called |
| 9505 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9506 | /*+ (1 << SIGHUP )*/ |
| 9507 | /*+ (1 << SIGTERM)*/ |
| 9508 | /*+ (1 << SIGINT )*/ |
| 9509 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9510 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9511 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9512 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9513 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9514 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9515 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9516 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9517 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9518 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9519 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9520 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9521 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9522 | break; |
| 9523 | case 'x': |
| 9524 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9525 | break; |
| 9526 | case 'o': |
| 9527 | if (!o_opt) { |
| 9528 | /* "set -+o" without parameter. |
| 9529 | * in bash, set -o produces this output: |
| 9530 | * pipefail off |
| 9531 | * and set +o: |
| 9532 | * set +o pipefail |
| 9533 | * We always use the second form. |
| 9534 | */ |
| 9535 | const char *p = o_opt_strings; |
| 9536 | idx = 0; |
| 9537 | while (*p) { |
| 9538 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9539 | idx++; |
| 9540 | p += strlen(p) + 1; |
| 9541 | } |
| 9542 | break; |
| 9543 | } |
| 9544 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9545 | if (idx >= 0) { |
| 9546 | G.o_opt[idx] = state; |
| 9547 | break; |
| 9548 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9549 | case 'e': |
| 9550 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9551 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9552 | default: |
| 9553 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9554 | } |
| 9555 | return EXIT_SUCCESS; |
| 9556 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9557 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9558 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9559 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9560 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9561 | enum { |
| 9562 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9563 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9564 | }; |
| 9565 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9566 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9567 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9568 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9569 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9570 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9571 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9572 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9573 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9574 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9575 | #if ENABLE_HUSH_FAST |
| 9576 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9577 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9578 | #if !BB_MMU |
| 9579 | G.argv0_for_re_execing = argv[0]; |
| 9580 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9581 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9582 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9583 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9584 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9585 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9586 | shell_ver->flg_export = 1; |
| 9587 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9588 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9589 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9590 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9591 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9592 | /* Create shell local variables from the values |
| 9593 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9594 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9595 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9596 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9597 | if (e) while (*e) { |
| 9598 | char *value = strchr(*e, '='); |
| 9599 | if (value) { /* paranoia */ |
| 9600 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9601 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9602 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9603 | cur_var->max_len = strlen(*e); |
| 9604 | cur_var->flg_export = 1; |
| 9605 | } |
| 9606 | e++; |
| 9607 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9608 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9609 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9610 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9611 | |
| 9612 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9613 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9614 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9615 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9616 | /* Set (but not export) PS1/2 unless already set */ |
| 9617 | if (!get_local_var_value("PS1")) |
| 9618 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9619 | if (!get_local_var_value("PS2")) |
| 9620 | set_local_var_from_halves("PS2", "> "); |
| 9621 | #endif |
| 9622 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9623 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9624 | /* Set (but not export) HOSTNAME unless already set */ |
| 9625 | if (!get_local_var_value("HOSTNAME")) { |
| 9626 | struct utsname uts; |
| 9627 | uname(&uts); |
| 9628 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9629 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9630 | /* bash also exports SHLVL and _, |
| 9631 | * and sets (but doesn't export) the following variables: |
| 9632 | * BASH=/bin/bash |
| 9633 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9634 | * BASH_VERSION='3.2.0(1)-release' |
| 9635 | * HOSTTYPE=i386 |
| 9636 | * MACHTYPE=i386-pc-linux-gnu |
| 9637 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9638 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9639 | * EUID=<NNNNN> |
| 9640 | * UID=<NNNNN> |
| 9641 | * GROUPS=() |
| 9642 | * LINES=<NNN> |
| 9643 | * COLUMNS=<NNN> |
| 9644 | * BASH_ARGC=() |
| 9645 | * BASH_ARGV=() |
| 9646 | * BASH_LINENO=() |
| 9647 | * BASH_SOURCE=() |
| 9648 | * DIRSTACK=() |
| 9649 | * PIPESTATUS=([0]="0") |
| 9650 | * HISTFILE=/<xxx>/.bash_history |
| 9651 | * HISTFILESIZE=500 |
| 9652 | * HISTSIZE=500 |
| 9653 | * MAILCHECK=60 |
| 9654 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9655 | * SHELL=/bin/bash |
| 9656 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9657 | * TERM=dumb |
| 9658 | * OPTERR=1 |
| 9659 | * OPTIND=1 |
| 9660 | * IFS=$' \t\n' |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9661 | * PS4='+ ' |
| 9662 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9663 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9664 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9665 | #if ENABLE_HUSH_LINENO_VAR |
| 9666 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9667 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9668 | set_local_var(p, /*flags*/ 0); |
| 9669 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9670 | } |
| 9671 | #endif |
| 9672 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9673 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9674 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9675 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9676 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9677 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9678 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9679 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9680 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9681 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9682 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9683 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9684 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9685 | * 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] | 9686 | * in order to intercept (more) signals. |
| 9687 | */ |
| 9688 | |
| 9689 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9690 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9691 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9692 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9693 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9694 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9695 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9696 | "<:$:R:V:" |
| 9697 | # if ENABLE_HUSH_FUNCTIONS |
| 9698 | "F:" |
| 9699 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9700 | #endif |
| 9701 | ); |
| 9702 | if (opt <= 0) |
| 9703 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9704 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9705 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9706 | /* Possibilities: |
| 9707 | * sh ... -c 'script' |
| 9708 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9709 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9710 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9711 | * "" needs to be replaced with NULL |
| 9712 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9713 | * Note: the form without ARG0 never happens: |
| 9714 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9715 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9716 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9717 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9718 | G.root_ppid = getppid(); |
| 9719 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9720 | G.global_argv = argv + optind; |
| 9721 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9722 | if (builtin_argc) { |
| 9723 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9724 | const struct built_in_command *x; |
| 9725 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9726 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9727 | x = find_builtin(optarg); |
| 9728 | if (x) { /* paranoia */ |
| 9729 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9730 | G.global_argv += builtin_argc; |
| 9731 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9732 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9733 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9734 | } |
| 9735 | goto final_return; |
| 9736 | } |
| 9737 | if (!G.global_argv[0]) { |
| 9738 | /* -c 'script' (no params): prevent empty $0 */ |
| 9739 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9740 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9741 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9742 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9743 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9744 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9745 | goto final_return; |
| 9746 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9747 | /* Well, we cannot just declare interactiveness, |
| 9748 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9749 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9750 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9751 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9752 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9753 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9754 | case 'l': |
| 9755 | flags |= OPT_login; |
| 9756 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9757 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9758 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9759 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9760 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9761 | case '$': { |
| 9762 | unsigned long long empty_trap_mask; |
| 9763 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9764 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9765 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9766 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9767 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9768 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9769 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9770 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9771 | optarg++; |
| 9772 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9773 | optarg++; |
| 9774 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9775 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9776 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9777 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9778 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9779 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9780 | for (sig = 1; sig < NSIG; sig++) { |
| 9781 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9782 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9783 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9784 | } |
| 9785 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9786 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9787 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9788 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9789 | optarg++; |
| 9790 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9791 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 9792 | # if ENABLE_HUSH_FUNCTIONS |
| 9793 | /* nommu uses re-exec trick for "... | func | ...", |
| 9794 | * should allow "return". |
| 9795 | * This accidentally allows returns in subshells. |
| 9796 | */ |
| 9797 | G_flag_return_in_progress = -1; |
| 9798 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9799 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9800 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9801 | case 'R': |
| 9802 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9803 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9804 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9805 | # if ENABLE_HUSH_FUNCTIONS |
| 9806 | case 'F': { |
| 9807 | struct function *funcp = new_function(optarg); |
| 9808 | /* funcp->name is already set to optarg */ |
| 9809 | /* funcp->body is set to NULL. It's a special case. */ |
| 9810 | funcp->body_as_string = argv[optind]; |
| 9811 | optind++; |
| 9812 | break; |
| 9813 | } |
| 9814 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9815 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9816 | case 'n': |
| 9817 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9818 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9819 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9820 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9821 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9822 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9823 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9824 | " or: sh -c command [args]...\n\n"); |
| 9825 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9826 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9827 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9828 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9829 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9830 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9831 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9832 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9833 | G.global_argc = argc - (optind - 1); |
| 9834 | G.global_argv = argv + (optind - 1); |
| 9835 | G.global_argv[0] = argv[0]; |
| 9836 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9837 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9838 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9839 | G.root_ppid = getppid(); |
| 9840 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9841 | |
| 9842 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9843 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9844 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9845 | debug_printf("sourcing /etc/profile\n"); |
| 9846 | input = fopen_for_read("/etc/profile"); |
| 9847 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9848 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9849 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9850 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9851 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9852 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9853 | /* bash: after sourcing /etc/profile, |
| 9854 | * tries to source (in the given order): |
| 9855 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9856 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9857 | * bash also sources ~/.bash_logout on exit. |
| 9858 | * If called as sh, skips .bash_XXX files. |
| 9859 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9860 | } |
| 9861 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9862 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 9863 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9864 | FILE *input; |
| 9865 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9866 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9867 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9868 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9869 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9870 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9871 | G.global_argc--; |
| 9872 | G.global_argv++; |
| 9873 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9874 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9875 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9876 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9877 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9878 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9879 | parse_and_run_file(input); |
| 9880 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9881 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9882 | #endif |
| 9883 | goto final_return; |
| 9884 | } |
| 9885 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9886 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9887 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9888 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9889 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9890 | /* A shell is interactive if the '-i' flag was given, |
| 9891 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9892 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9893 | * no arguments remaining or the -s flag given |
| 9894 | * standard input is a terminal |
| 9895 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9896 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9897 | */ |
| 9898 | #if ENABLE_HUSH_JOB |
| 9899 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9900 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9901 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9902 | if (G_saved_tty_pgrp < 0) |
| 9903 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9904 | |
| 9905 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9906 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9907 | if (G_interactive_fd < 0) { |
| 9908 | /* try to dup to any fd */ |
| 9909 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9910 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9911 | /* give up */ |
| 9912 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9913 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9914 | } |
| 9915 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9916 | // TODO: track & disallow any attempts of user |
| 9917 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9918 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9919 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9920 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9921 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9922 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9923 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9924 | /* If we were run as 'hush &', sleep until we are |
| 9925 | * in the foreground (tty pgrp == our pgrp). |
| 9926 | * If we get started under a job aware app (like bash), |
| 9927 | * make sure we are now in charge so we don't fight over |
| 9928 | * who gets the foreground */ |
| 9929 | while (1) { |
| 9930 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9931 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9932 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9933 | break; |
| 9934 | /* send TTIN to ourself (should stop us) */ |
| 9935 | kill(- shell_pgrp, SIGTTIN); |
| 9936 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9937 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9938 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9939 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9940 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9941 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9942 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9943 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9944 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9945 | /* Put ourselves in our own process group |
| 9946 | * (bash, too, does this only if ctty is available) */ |
| 9947 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9948 | /* Grab control of the terminal */ |
| 9949 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9950 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9951 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9952 | |
| 9953 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9954 | { |
| 9955 | const char *hp = get_local_var_value("HISTFILE"); |
| 9956 | if (!hp) { |
| 9957 | hp = get_local_var_value("HOME"); |
| 9958 | if (hp) |
| 9959 | hp = concat_path_file(hp, ".hush_history"); |
| 9960 | } else { |
| 9961 | hp = xstrdup(hp); |
| 9962 | } |
| 9963 | if (hp) { |
| 9964 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9965 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9966 | } |
| 9967 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9968 | hp = get_local_var_value("HISTFILESIZE"); |
| 9969 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9970 | # endif |
| 9971 | } |
| 9972 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9973 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9974 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9975 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9976 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9977 | /* No job control compiled in, only prompt/line editing */ |
| 9978 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9979 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9980 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9981 | /* try to dup to any fd */ |
Denys Vlasenko | d1a8323 | 2018-06-26 15:50:33 +0200 | [diff] [blame] | 9982 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9983 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9984 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9985 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9986 | } |
| 9987 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9988 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9989 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9990 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9991 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9992 | #else |
| 9993 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9994 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9995 | #endif |
| 9996 | /* bash: |
| 9997 | * if interactive but not a login shell, sources ~/.bashrc |
| 9998 | * (--norc turns this off, --rcfile <file> overrides) |
| 9999 | */ |
| 10000 | |
| 10001 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 10002 | /* note: ash and hush share this string */ |
| 10003 | printf("\n\n%s %s\n" |
| 10004 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 10005 | "\n", |
| 10006 | bb_banner, |
| 10007 | "hush - the humble shell" |
| 10008 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 10009 | } |
| 10010 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10011 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10012 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10013 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10014 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10015 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 10016 | |
| 10017 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10018 | /* |
| 10019 | * Built-ins |
| 10020 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10021 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10022 | { |
| 10023 | return 0; |
| 10024 | } |
| 10025 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10026 | #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] | 10027 | 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] | 10028 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10029 | int argc = string_array_len(argv); |
| 10030 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10031 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10032 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10033 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10034 | static int FAST_FUNC builtin_test(char **argv) |
| 10035 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10036 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10037 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10038 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10039 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10040 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10041 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10042 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10043 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10044 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10045 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10046 | static int FAST_FUNC builtin_printf(char **argv) |
| 10047 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10048 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10049 | } |
| 10050 | #endif |
| 10051 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10052 | #if ENABLE_HUSH_HELP |
| 10053 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 10054 | { |
| 10055 | const struct built_in_command *x; |
| 10056 | |
| 10057 | printf( |
| 10058 | "Built-in commands:\n" |
| 10059 | "------------------\n"); |
| 10060 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 10061 | if (x->b_descr) |
| 10062 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 10063 | } |
| 10064 | return EXIT_SUCCESS; |
| 10065 | } |
| 10066 | #endif |
| 10067 | |
| 10068 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 10069 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 10070 | { |
| 10071 | show_history(G.line_input_state); |
| 10072 | return EXIT_SUCCESS; |
| 10073 | } |
| 10074 | #endif |
| 10075 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10076 | static char **skip_dash_dash(char **argv) |
| 10077 | { |
| 10078 | argv++; |
| 10079 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 10080 | argv++; |
| 10081 | return argv; |
| 10082 | } |
| 10083 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10084 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10085 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10086 | const char *newdir; |
| 10087 | |
| 10088 | argv = skip_dash_dash(argv); |
| 10089 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10090 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10091 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10092 | * bash says "bash: cd: HOME not set" and does nothing |
| 10093 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10094 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 10095 | const char *home = get_local_var_value("HOME"); |
| 10096 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 10097 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10098 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10099 | /* Mimic bash message exactly */ |
| 10100 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10101 | return EXIT_FAILURE; |
| 10102 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10103 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 10104 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 10105 | * set it again, but do not export. bash does the same. |
| 10106 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10107 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10108 | return EXIT_SUCCESS; |
| 10109 | } |
| 10110 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10111 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 10112 | { |
| 10113 | puts(get_cwd(0)); |
| 10114 | return EXIT_SUCCESS; |
| 10115 | } |
| 10116 | |
| 10117 | static int FAST_FUNC builtin_eval(char **argv) |
| 10118 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10119 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10120 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10121 | if (!argv[0]) |
| 10122 | return EXIT_SUCCESS; |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10123 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10124 | if (!argv[1]) { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10125 | /* bash: |
| 10126 | * eval "echo Hi; done" ("done" is syntax error): |
| 10127 | * "echo Hi" will not execute too. |
| 10128 | */ |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10129 | parse_and_run_string(argv[0]); |
| 10130 | } else { |
| 10131 | /* "The eval utility shall construct a command by |
| 10132 | * concatenating arguments together, separating |
| 10133 | * each with a <space> character." |
| 10134 | */ |
| 10135 | char *str, *p; |
| 10136 | unsigned len = 0; |
| 10137 | char **pp = argv; |
| 10138 | do |
| 10139 | len += strlen(*pp) + 1; |
| 10140 | while (*++pp); |
| 10141 | str = p = xmalloc(len); |
| 10142 | pp = argv; |
| 10143 | for (;;) { |
| 10144 | p = stpcpy(p, *pp); |
| 10145 | pp++; |
| 10146 | if (!*pp) |
| 10147 | break; |
| 10148 | *p++ = ' '; |
| 10149 | } |
| 10150 | parse_and_run_string(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10151 | free(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10152 | } |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10153 | return G.last_exitcode; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10154 | } |
| 10155 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10156 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10157 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10158 | argv = skip_dash_dash(argv); |
| 10159 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10160 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10161 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10162 | /* Careful: we can end up here after [v]fork. Do not restore |
| 10163 | * tty pgrp then, only top-level shell process does that */ |
| 10164 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 10165 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 10166 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 10167 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 10168 | * open here. However, they are all CLOEXEC, and execv below |
| 10169 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 10170 | * it should show no extra open fds in the "ls" process. |
| 10171 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 10172 | */ |
| 10173 | //close_saved_fds_and_FILE_fds(); |
| 10174 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 10175 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10176 | * 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] | 10177 | * and tcsetpgrp, and this is inherently racy. |
| 10178 | */ |
| 10179 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10180 | } |
| 10181 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10182 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10183 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 10184 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10185 | |
| 10186 | /* interactive bash: |
| 10187 | * # trap "echo EEE" EXIT |
| 10188 | * # exit |
| 10189 | * exit |
| 10190 | * There are stopped jobs. |
| 10191 | * (if there are _stopped_ jobs, running ones don't count) |
| 10192 | * # exit |
| 10193 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 10194 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10195 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 10196 | * 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] | 10197 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 10198 | |
| 10199 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10200 | argv = skip_dash_dash(argv); |
| 10201 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10202 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10203 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 10204 | xfunc_error_retval = 255; |
| 10205 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10206 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10209 | #if ENABLE_HUSH_TYPE |
| 10210 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 10211 | static int FAST_FUNC builtin_type(char **argv) |
| 10212 | { |
| 10213 | int ret = EXIT_SUCCESS; |
| 10214 | |
| 10215 | while (*++argv) { |
| 10216 | const char *type; |
| 10217 | char *path = NULL; |
| 10218 | |
| 10219 | if (0) {} /* make conditional compile easier below */ |
| 10220 | /*else if (find_alias(*argv)) |
| 10221 | type = "an alias";*/ |
| 10222 | #if ENABLE_HUSH_FUNCTIONS |
| 10223 | else if (find_function(*argv)) |
| 10224 | type = "a function"; |
| 10225 | #endif |
| 10226 | else if (find_builtin(*argv)) |
| 10227 | type = "a shell builtin"; |
| 10228 | else if ((path = find_in_path(*argv)) != NULL) |
| 10229 | type = path; |
| 10230 | else { |
| 10231 | bb_error_msg("type: %s: not found", *argv); |
| 10232 | ret = EXIT_FAILURE; |
| 10233 | continue; |
| 10234 | } |
| 10235 | |
| 10236 | printf("%s is %s\n", *argv, type); |
| 10237 | free(path); |
| 10238 | } |
| 10239 | |
| 10240 | return ret; |
| 10241 | } |
| 10242 | #endif |
| 10243 | |
| 10244 | #if ENABLE_HUSH_READ |
| 10245 | /* Interruptibility of read builtin in bash |
| 10246 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 10247 | * |
| 10248 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 10249 | * |
| 10250 | * SIGINT: |
| 10251 | * - terminates non-interactive shell; |
| 10252 | * - interrupts read in interactive shell; |
| 10253 | * if it has non-empty trap: |
| 10254 | * - executes trap and returns to command prompt in interactive shell; |
| 10255 | * - executes trap and returns to read in non-interactive shell; |
| 10256 | * SIGTERM: |
| 10257 | * - is ignored (does not interrupt) read in interactive shell; |
| 10258 | * - terminates non-interactive shell; |
| 10259 | * if it has non-empty trap: |
| 10260 | * - executes trap and returns to read; |
| 10261 | * SIGHUP: |
| 10262 | * - terminates shell (regardless of interactivity); |
| 10263 | * if it has non-empty trap: |
| 10264 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10265 | * SIGCHLD from children: |
| 10266 | * - does not interrupt read regardless of interactivity: |
| 10267 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10268 | */ |
| 10269 | static int FAST_FUNC builtin_read(char **argv) |
| 10270 | { |
| 10271 | const char *r; |
| 10272 | char *opt_n = NULL; |
| 10273 | char *opt_p = NULL; |
| 10274 | char *opt_t = NULL; |
| 10275 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10276 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10277 | const char *ifs; |
| 10278 | int read_flags; |
| 10279 | |
| 10280 | /* "!": do not abort on errors. |
| 10281 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 10282 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10283 | read_flags = getopt32(argv, |
| 10284 | #if BASH_READ_D |
| 10285 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 10286 | #else |
| 10287 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 10288 | #endif |
| 10289 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10290 | if (read_flags == (uint32_t)-1) |
| 10291 | return EXIT_FAILURE; |
| 10292 | argv += optind; |
| 10293 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 10294 | |
| 10295 | again: |
| 10296 | r = shell_builtin_read(set_local_var_from_halves, |
| 10297 | argv, |
| 10298 | ifs, |
| 10299 | read_flags, |
| 10300 | opt_n, |
| 10301 | opt_p, |
| 10302 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10303 | opt_u, |
| 10304 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10305 | ); |
| 10306 | |
| 10307 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 10308 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10309 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10310 | goto again; |
| 10311 | } |
| 10312 | |
| 10313 | if ((uintptr_t)r > 1) { |
| 10314 | bb_error_msg("%s", r); |
| 10315 | r = (char*)(uintptr_t)1; |
| 10316 | } |
| 10317 | |
| 10318 | return (uintptr_t)r; |
| 10319 | } |
| 10320 | #endif |
| 10321 | |
| 10322 | #if ENABLE_HUSH_UMASK |
| 10323 | static int FAST_FUNC builtin_umask(char **argv) |
| 10324 | { |
| 10325 | int rc; |
| 10326 | mode_t mask; |
| 10327 | |
| 10328 | rc = 1; |
| 10329 | mask = umask(0); |
| 10330 | argv = skip_dash_dash(argv); |
| 10331 | if (argv[0]) { |
| 10332 | mode_t old_mask = mask; |
| 10333 | |
| 10334 | /* numeric umasks are taken as-is */ |
| 10335 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 10336 | if (!isdigit(argv[0][0])) |
| 10337 | mask ^= 0777; |
| 10338 | mask = bb_parse_mode(argv[0], mask); |
| 10339 | if (!isdigit(argv[0][0])) |
| 10340 | mask ^= 0777; |
| 10341 | if ((unsigned)mask > 0777) { |
| 10342 | mask = old_mask; |
| 10343 | /* bash messages: |
| 10344 | * bash: umask: 'q': invalid symbolic mode operator |
| 10345 | * bash: umask: 999: octal number out of range |
| 10346 | */ |
| 10347 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 10348 | rc = 0; |
| 10349 | } |
| 10350 | } else { |
| 10351 | /* Mimic bash */ |
| 10352 | printf("%04o\n", (unsigned) mask); |
| 10353 | /* fall through and restore mask which we set to 0 */ |
| 10354 | } |
| 10355 | umask(mask); |
| 10356 | |
| 10357 | return !rc; /* rc != 0 - success */ |
| 10358 | } |
| 10359 | #endif |
| 10360 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10361 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10362 | static void print_escaped(const char *s) |
| 10363 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10364 | if (*s == '\'') |
| 10365 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10366 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10367 | const char *p = strchrnul(s, '\''); |
| 10368 | /* print 'xxxx', possibly just '' */ |
| 10369 | printf("'%.*s'", (int)(p - s), s); |
| 10370 | if (*p == '\0') |
| 10371 | break; |
| 10372 | s = p; |
| 10373 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10374 | /* s points to '; print "'''...'''" */ |
| 10375 | putchar('"'); |
| 10376 | do putchar('\''); while (*++s == '\''); |
| 10377 | putchar('"'); |
| 10378 | } while (*s); |
| 10379 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10380 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10381 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10382 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10383 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10384 | { |
| 10385 | do { |
| 10386 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10387 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10388 | |
| 10389 | /* So far we do not check that name is valid (TODO?) */ |
| 10390 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10391 | if (*name_end == '\0') { |
| 10392 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10393 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10394 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10395 | var = vpp ? *vpp : NULL; |
| 10396 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10397 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10398 | /* export -n NAME (without =VALUE) */ |
| 10399 | if (var) { |
| 10400 | var->flg_export = 0; |
| 10401 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10402 | unsetenv(name); |
| 10403 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10404 | continue; |
| 10405 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10406 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10407 | /* export NAME (without =VALUE) */ |
| 10408 | if (var) { |
| 10409 | var->flg_export = 1; |
| 10410 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10411 | putenv(var->varstr); |
| 10412 | continue; |
| 10413 | } |
| 10414 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10415 | if (flags & SETFLAG_MAKE_RO) { |
| 10416 | /* readonly NAME (without =VALUE) */ |
| 10417 | if (var) { |
| 10418 | var->flg_read_only = 1; |
| 10419 | continue; |
| 10420 | } |
| 10421 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10422 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10423 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10424 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10425 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10426 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10427 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10428 | continue; |
| 10429 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10430 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10431 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10432 | /* Exporting non-existing variable. |
| 10433 | * bash does not put it in environment, |
| 10434 | * but remembers that it is exported, |
| 10435 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10436 | * We just set it to "" and export. |
| 10437 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10438 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10439 | * bash sets the value to "". |
| 10440 | */ |
| 10441 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10442 | * bash remembers NAME and disallows its creation |
| 10443 | * in the future. |
| 10444 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10445 | name = xasprintf("%s=", name); |
| 10446 | } else { |
| 10447 | /* (Un)exporting/making local NAME=VALUE */ |
| 10448 | name = xstrdup(name); |
| 10449 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10450 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10451 | if (set_local_var(name, flags)) |
| 10452 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10453 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10454 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10455 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10456 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10457 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10458 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10459 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10460 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10461 | unsigned opt_unexport; |
| 10462 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10463 | #if ENABLE_HUSH_EXPORT_N |
| 10464 | /* "!": do not abort on errors */ |
| 10465 | opt_unexport = getopt32(argv, "!n"); |
| 10466 | if (opt_unexport == (uint32_t)-1) |
| 10467 | return EXIT_FAILURE; |
| 10468 | argv += optind; |
| 10469 | #else |
| 10470 | opt_unexport = 0; |
| 10471 | argv++; |
| 10472 | #endif |
| 10473 | |
| 10474 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10475 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10476 | if (e) { |
| 10477 | while (*e) { |
| 10478 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10479 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10480 | #else |
| 10481 | /* ash emits: export VAR='VAL' |
| 10482 | * bash: declare -x VAR="VAL" |
| 10483 | * we follow ash example */ |
| 10484 | const char *s = *e++; |
| 10485 | const char *p = strchr(s, '='); |
| 10486 | |
| 10487 | if (!p) /* wtf? take next variable */ |
| 10488 | continue; |
| 10489 | /* export var= */ |
| 10490 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10491 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10492 | putchar('\n'); |
| 10493 | #endif |
| 10494 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10495 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10496 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10497 | return EXIT_SUCCESS; |
| 10498 | } |
| 10499 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10500 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10501 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10502 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10503 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10504 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10505 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10506 | { |
| 10507 | if (G.func_nest_level == 0) { |
| 10508 | bb_error_msg("%s: not in a function", argv[0]); |
| 10509 | return EXIT_FAILURE; /* bash compat */ |
| 10510 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10511 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10512 | /* Since all builtins run in a nested variable level, |
| 10513 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10514 | * after builtin returns. |
| 10515 | */ |
| 10516 | 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] | 10517 | } |
| 10518 | #endif |
| 10519 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10520 | #if ENABLE_HUSH_READONLY |
| 10521 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10522 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10523 | argv++; |
| 10524 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10525 | /* bash: readonly [-p]: list all readonly VARs |
| 10526 | * (-p has no effect in bash) |
| 10527 | */ |
| 10528 | struct variable *e; |
| 10529 | for (e = G.top_var; e; e = e->next) { |
| 10530 | if (e->flg_read_only) { |
| 10531 | //TODO: quote value: readonly VAR='VAL' |
| 10532 | printf("readonly %s\n", e->varstr); |
| 10533 | } |
| 10534 | } |
| 10535 | return EXIT_SUCCESS; |
| 10536 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10537 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10538 | } |
| 10539 | #endif |
| 10540 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10541 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10542 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10543 | static int FAST_FUNC builtin_unset(char **argv) |
| 10544 | { |
| 10545 | int ret; |
| 10546 | unsigned opts; |
| 10547 | |
| 10548 | /* "!": do not abort on errors */ |
| 10549 | /* "+": stop at 1st non-option */ |
| 10550 | opts = getopt32(argv, "!+vf"); |
| 10551 | if (opts == (unsigned)-1) |
| 10552 | return EXIT_FAILURE; |
| 10553 | if (opts == 3) { |
| 10554 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10555 | return EXIT_FAILURE; |
| 10556 | } |
| 10557 | argv += optind; |
| 10558 | |
| 10559 | ret = EXIT_SUCCESS; |
| 10560 | while (*argv) { |
| 10561 | if (!(opts & 2)) { /* not -f */ |
| 10562 | if (unset_local_var(*argv)) { |
| 10563 | /* unset <nonexistent_var> doesn't fail. |
| 10564 | * Error is when one tries to unset RO var. |
| 10565 | * Message was printed by unset_local_var. */ |
| 10566 | ret = EXIT_FAILURE; |
| 10567 | } |
| 10568 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10569 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10570 | else { |
| 10571 | unset_func(*argv); |
| 10572 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10573 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10574 | argv++; |
| 10575 | } |
| 10576 | return ret; |
| 10577 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10578 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10579 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10580 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10581 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10582 | * built-in 'set' handler |
| 10583 | * SUSv3 says: |
| 10584 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10585 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10586 | * set -- [argument...] |
| 10587 | * set -o |
| 10588 | * set +o |
| 10589 | * Implementations shall support the options in both their hyphen and |
| 10590 | * plus-sign forms. These options can also be specified as options to sh. |
| 10591 | * Examples: |
| 10592 | * Write out all variables and their values: set |
| 10593 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10594 | * Turn on the -x and -v options: set -xv |
| 10595 | * Unset all positional parameters: set -- |
| 10596 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10597 | * Set the positional parameters to the expansion of x, even if x expands |
| 10598 | * with a leading '-' or '+': set -- $x |
| 10599 | * |
| 10600 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10601 | */ |
| 10602 | static int FAST_FUNC builtin_set(char **argv) |
| 10603 | { |
| 10604 | int n; |
| 10605 | char **pp, **g_argv; |
| 10606 | char *arg = *++argv; |
| 10607 | |
| 10608 | if (arg == NULL) { |
| 10609 | struct variable *e; |
| 10610 | for (e = G.top_var; e; e = e->next) |
| 10611 | puts(e->varstr); |
| 10612 | return EXIT_SUCCESS; |
| 10613 | } |
| 10614 | |
| 10615 | do { |
| 10616 | if (strcmp(arg, "--") == 0) { |
| 10617 | ++argv; |
| 10618 | goto set_argv; |
| 10619 | } |
| 10620 | if (arg[0] != '+' && arg[0] != '-') |
| 10621 | break; |
| 10622 | for (n = 1; arg[n]; ++n) { |
| 10623 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10624 | goto error; |
| 10625 | if (arg[n] == 'o' && argv[1]) |
| 10626 | argv++; |
| 10627 | } |
| 10628 | } while ((arg = *++argv) != NULL); |
| 10629 | /* Now argv[0] is 1st argument */ |
| 10630 | |
| 10631 | if (arg == NULL) |
| 10632 | return EXIT_SUCCESS; |
| 10633 | set_argv: |
| 10634 | |
| 10635 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10636 | g_argv = G.global_argv; |
| 10637 | if (G.global_args_malloced) { |
| 10638 | pp = g_argv; |
| 10639 | while (*++pp) |
| 10640 | free(*pp); |
| 10641 | g_argv[1] = NULL; |
| 10642 | } else { |
| 10643 | G.global_args_malloced = 1; |
| 10644 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10645 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10646 | g_argv = pp; |
| 10647 | } |
| 10648 | /* This realloc's G.global_argv */ |
| 10649 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10650 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10651 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10652 | |
| 10653 | return EXIT_SUCCESS; |
| 10654 | |
| 10655 | /* Nothing known, so abort */ |
| 10656 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10657 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10658 | return EXIT_FAILURE; |
| 10659 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10660 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10661 | |
| 10662 | static int FAST_FUNC builtin_shift(char **argv) |
| 10663 | { |
| 10664 | int n = 1; |
| 10665 | argv = skip_dash_dash(argv); |
| 10666 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10667 | n = bb_strtou(argv[0], NULL, 10); |
| 10668 | if (errno || n < 0) { |
| 10669 | /* shared string with ash.c */ |
| 10670 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10671 | /* |
| 10672 | * ash aborts in this case. |
| 10673 | * bash prints error message and set $? to 1. |
| 10674 | * Interestingly, for "shift 99999" bash does not |
| 10675 | * print error message, but does set $? to 1 |
| 10676 | * (and does no shifting at all). |
| 10677 | */ |
| 10678 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10679 | } |
| 10680 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10681 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10682 | int m = 1; |
| 10683 | while (m <= n) |
| 10684 | free(G.global_argv[m++]); |
| 10685 | } |
| 10686 | G.global_argc -= n; |
| 10687 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10688 | G.global_argc * sizeof(G.global_argv[0])); |
| 10689 | return EXIT_SUCCESS; |
| 10690 | } |
| 10691 | return EXIT_FAILURE; |
| 10692 | } |
| 10693 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10694 | #if ENABLE_HUSH_GETOPTS |
| 10695 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10696 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10697 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10698 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10699 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10700 | If a required argument is not found, and getopts is not silent, |
| 10701 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10702 | diagnostic message is printed. If getopts is silent, then a |
| 10703 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10704 | character found. |
| 10705 | |
| 10706 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10707 | |
| 10708 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10709 | */ |
| 10710 | char cbuf[2]; |
| 10711 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10712 | int c, n, exitcode, my_opterr; |
| 10713 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10714 | |
| 10715 | optstring = *++argv; |
| 10716 | if (!optstring || !(var = *++argv)) { |
| 10717 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10718 | return EXIT_FAILURE; |
| 10719 | } |
| 10720 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10721 | if (argv[1]) |
| 10722 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10723 | else |
| 10724 | argv = G.global_argv; |
| 10725 | cbuf[1] = '\0'; |
| 10726 | |
| 10727 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10728 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10729 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10730 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10731 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10732 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10733 | |
| 10734 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10735 | /*if (optstring[0] != '+')*/ { |
| 10736 | char *s = alloca(strlen(optstring) + 2); |
| 10737 | sprintf(s, "+%s", optstring); |
| 10738 | optstring = s; |
| 10739 | } |
| 10740 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10741 | /* Naively, now we should just |
| 10742 | * cp = get_local_var_value("OPTIND"); |
| 10743 | * optind = cp ? atoi(cp) : 0; |
| 10744 | * optarg = NULL; |
| 10745 | * opterr = my_opterr; |
| 10746 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10747 | * and be done? Not so fast... |
| 10748 | * Unlike normal getopt() usage in C programs, here |
| 10749 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10750 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10751 | * invocations of "getopts", there will be calls to shell builtins |
| 10752 | * which use getopt() internally. Example: |
| 10753 | * while getopts "abc" RES -a -bc -abc de; do |
| 10754 | * unset -ff func |
| 10755 | * done |
| 10756 | * This would not work correctly: getopt() call inside "unset" |
| 10757 | * modifies internal libc state which is tracking position in |
| 10758 | * multi-option strings ("-abc"). At best, it can skip options |
| 10759 | * or return the same option infinitely. With glibc implementation |
| 10760 | * of getopt(), it would use outright invalid pointers and return |
| 10761 | * garbage even _without_ "unset" mangling internal state. |
| 10762 | * |
| 10763 | * We resort to resetting getopt() state and calling it N times, |
| 10764 | * until we get Nth result (or failure). |
| 10765 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10766 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10767 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10768 | count = 0; |
| 10769 | n = string_array_len(argv); |
| 10770 | do { |
| 10771 | optarg = NULL; |
| 10772 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10773 | c = getopt(n, argv, optstring); |
| 10774 | if (c < 0) |
| 10775 | break; |
| 10776 | count++; |
| 10777 | } while (count <= G.getopt_count); |
| 10778 | |
| 10779 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10780 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10781 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10782 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10783 | |
| 10784 | /* Set OPTARG */ |
| 10785 | /* Always set or unset, never left as-is, even on exit/error: |
| 10786 | * "If no option was found, or if the option that was found |
| 10787 | * does not have an option-argument, OPTARG shall be unset." |
| 10788 | */ |
| 10789 | cp = optarg; |
| 10790 | if (c == '?') { |
| 10791 | /* If ":optstring" and unknown option is seen, |
| 10792 | * it is stored to OPTARG. |
| 10793 | */ |
| 10794 | if (optstring[1] == ':') { |
| 10795 | cbuf[0] = optopt; |
| 10796 | cp = cbuf; |
| 10797 | } |
| 10798 | } |
| 10799 | if (cp) |
| 10800 | set_local_var_from_halves("OPTARG", cp); |
| 10801 | else |
| 10802 | unset_local_var("OPTARG"); |
| 10803 | |
| 10804 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10805 | exitcode = EXIT_SUCCESS; |
| 10806 | if (c < 0) { /* -1: end of options */ |
| 10807 | exitcode = EXIT_FAILURE; |
| 10808 | c = '?'; |
| 10809 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10810 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10811 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10812 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10813 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10814 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10815 | return exitcode; |
| 10816 | } |
| 10817 | #endif |
| 10818 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10819 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10820 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10821 | char *arg_path, *filename; |
| 10822 | FILE *input; |
| 10823 | save_arg_t sv; |
| 10824 | char *args_need_save; |
| 10825 | #if ENABLE_HUSH_FUNCTIONS |
| 10826 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10827 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10828 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10829 | argv = skip_dash_dash(argv); |
| 10830 | filename = argv[0]; |
| 10831 | if (!filename) { |
| 10832 | /* bash says: "bash: .: filename argument required" */ |
| 10833 | return 2; /* bash compat */ |
| 10834 | } |
| 10835 | arg_path = NULL; |
| 10836 | if (!strchr(filename, '/')) { |
| 10837 | arg_path = find_in_path(filename); |
| 10838 | if (arg_path) |
| 10839 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10840 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10841 | errno = ENOENT; |
| 10842 | bb_simple_perror_msg(filename); |
| 10843 | return EXIT_FAILURE; |
| 10844 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10845 | } |
| 10846 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10847 | free(arg_path); |
| 10848 | if (!input) { |
| 10849 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10850 | /* POSIX: non-interactive shell should abort here, |
| 10851 | * not merely fail. So far no one complained :) |
| 10852 | */ |
| 10853 | return EXIT_FAILURE; |
| 10854 | } |
| 10855 | |
| 10856 | #if ENABLE_HUSH_FUNCTIONS |
| 10857 | sv_flg = G_flag_return_in_progress; |
| 10858 | /* "we are inside sourced file, ok to use return" */ |
| 10859 | G_flag_return_in_progress = -1; |
| 10860 | #endif |
| 10861 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10862 | if (args_need_save) |
| 10863 | save_and_replace_G_args(&sv, argv); |
| 10864 | |
| 10865 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10866 | G.last_exitcode = 0; |
| 10867 | parse_and_run_file(input); |
| 10868 | fclose_and_forget(input); |
| 10869 | |
| 10870 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10871 | restore_G_args(&sv, argv); |
| 10872 | #if ENABLE_HUSH_FUNCTIONS |
| 10873 | G_flag_return_in_progress = sv_flg; |
| 10874 | #endif |
| 10875 | |
| 10876 | return G.last_exitcode; |
| 10877 | } |
| 10878 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10879 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10880 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10881 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10882 | int sig; |
| 10883 | char *new_cmd; |
| 10884 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10885 | if (!G_traps) |
| 10886 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10887 | |
| 10888 | argv++; |
| 10889 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10890 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10891 | /* No args: print all trapped */ |
| 10892 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10893 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10894 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10895 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10896 | /* note: bash adds "SIG", but only if invoked |
| 10897 | * as "bash". If called as "sh", or if set -o posix, |
| 10898 | * then it prints short signal names. |
| 10899 | * We are printing short names: */ |
| 10900 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10901 | } |
| 10902 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10903 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10904 | return EXIT_SUCCESS; |
| 10905 | } |
| 10906 | |
| 10907 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10908 | /* If first arg is a number: reset all specified signals */ |
| 10909 | sig = bb_strtou(*argv, NULL, 10); |
| 10910 | if (errno == 0) { |
| 10911 | int ret; |
| 10912 | process_sig_list: |
| 10913 | ret = EXIT_SUCCESS; |
| 10914 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10915 | sighandler_t handler; |
| 10916 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10917 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10918 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10919 | ret = EXIT_FAILURE; |
| 10920 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10921 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10922 | continue; |
| 10923 | } |
| 10924 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10925 | free(G_traps[sig]); |
| 10926 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10927 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10928 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10929 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10930 | |
| 10931 | /* There is no signal for 0 (EXIT) */ |
| 10932 | if (sig == 0) |
| 10933 | continue; |
| 10934 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10935 | if (new_cmd) |
| 10936 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10937 | else |
| 10938 | /* We are removing trap handler */ |
| 10939 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10940 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10941 | } |
| 10942 | return ret; |
| 10943 | } |
| 10944 | |
| 10945 | if (!argv[1]) { /* no second arg */ |
| 10946 | bb_error_msg("trap: invalid arguments"); |
| 10947 | return EXIT_FAILURE; |
| 10948 | } |
| 10949 | |
| 10950 | /* First arg is "-": reset all specified to default */ |
| 10951 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10952 | /* Everything else: set arg as signal handler |
| 10953 | * (includes "" case, which ignores signal) */ |
| 10954 | if (argv[0][0] == '-') { |
| 10955 | if (argv[0][1] == '\0') { /* "-" */ |
| 10956 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10957 | goto reset_traps; |
| 10958 | } |
| 10959 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10960 | argv++; |
| 10961 | } |
| 10962 | /* else: "-something", no special meaning */ |
| 10963 | } |
| 10964 | new_cmd = *argv; |
| 10965 | reset_traps: |
| 10966 | argv++; |
| 10967 | goto process_sig_list; |
| 10968 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10969 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10970 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10971 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10972 | static struct pipe *parse_jobspec(const char *str) |
| 10973 | { |
| 10974 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10975 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10976 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10977 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10978 | if (str[0] != '%' |
| 10979 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10980 | ) { |
| 10981 | bb_error_msg("bad argument '%s'", str); |
| 10982 | return NULL; |
| 10983 | } |
| 10984 | /* It is "%%", "%+" or "%" - current job */ |
| 10985 | jobnum = G.last_jobid; |
| 10986 | if (jobnum == 0) { |
| 10987 | bb_error_msg("no current job"); |
| 10988 | return NULL; |
| 10989 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10990 | } |
| 10991 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10992 | if (pi->jobid == jobnum) { |
| 10993 | return pi; |
| 10994 | } |
| 10995 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10996 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10997 | return NULL; |
| 10998 | } |
| 10999 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11000 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 11001 | { |
| 11002 | struct pipe *job; |
| 11003 | const char *status_string; |
| 11004 | |
| 11005 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 11006 | for (job = G.job_list; job; job = job->next) { |
| 11007 | if (job->alive_cmds == job->stopped_cmds) |
| 11008 | status_string = "Stopped"; |
| 11009 | else |
| 11010 | status_string = "Running"; |
| 11011 | |
| 11012 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 11013 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11014 | |
| 11015 | clean_up_last_dead_job(); |
| 11016 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11017 | return EXIT_SUCCESS; |
| 11018 | } |
| 11019 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11020 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11021 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11022 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11023 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11024 | struct pipe *pi; |
| 11025 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11026 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11027 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 11028 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11029 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 11030 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11031 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11032 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11033 | goto found; |
| 11034 | } |
| 11035 | } |
| 11036 | bb_error_msg("%s: no current job", argv[0]); |
| 11037 | return EXIT_FAILURE; |
| 11038 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11039 | |
| 11040 | pi = parse_jobspec(argv[1]); |
| 11041 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11042 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11043 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 11044 | /* TODO: bash prints a string representation |
| 11045 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 11046 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11047 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11048 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11049 | } |
| 11050 | |
| 11051 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11052 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 11053 | for (i = 0; i < pi->num_cmds; i++) { |
| 11054 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11055 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11056 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11057 | |
| 11058 | i = kill(- pi->pgrp, SIGCONT); |
| 11059 | if (i < 0) { |
| 11060 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11061 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11062 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11063 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11064 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11065 | } |
| 11066 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11067 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11068 | 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] | 11069 | return checkjobs_and_fg_shell(pi); |
| 11070 | } |
| 11071 | return EXIT_SUCCESS; |
| 11072 | } |
| 11073 | #endif |
| 11074 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11075 | #if ENABLE_HUSH_KILL |
| 11076 | static int FAST_FUNC builtin_kill(char **argv) |
| 11077 | { |
| 11078 | int ret = 0; |
| 11079 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11080 | # if ENABLE_HUSH_JOB |
| 11081 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 11082 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11083 | |
| 11084 | do { |
| 11085 | struct pipe *pi; |
| 11086 | char *dst; |
| 11087 | int j, n; |
| 11088 | |
| 11089 | if (argv[i][0] != '%') |
| 11090 | continue; |
| 11091 | /* |
| 11092 | * "kill %N" - job kill |
| 11093 | * Converting to pgrp / pid kill |
| 11094 | */ |
| 11095 | pi = parse_jobspec(argv[i]); |
| 11096 | if (!pi) { |
| 11097 | /* Eat bad jobspec */ |
| 11098 | j = i; |
| 11099 | do { |
| 11100 | j++; |
| 11101 | argv[j - 1] = argv[j]; |
| 11102 | } while (argv[j]); |
| 11103 | ret = 1; |
| 11104 | i--; |
| 11105 | continue; |
| 11106 | } |
| 11107 | /* |
| 11108 | * In jobs started under job control, we signal |
| 11109 | * entire process group by kill -PGRP_ID. |
| 11110 | * This happens, f.e., in interactive shell. |
| 11111 | * |
| 11112 | * Otherwise, we signal each child via |
| 11113 | * kill PID1 PID2 PID3. |
| 11114 | * Testcases: |
| 11115 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 11116 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 11117 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 11118 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 11119 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11120 | dst = alloca(n * sizeof(int)*4); |
| 11121 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11122 | if (G_interactive_fd) |
| 11123 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11124 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11125 | struct command *cmd = &pi->cmds[j]; |
| 11126 | /* Skip exited members of the job */ |
| 11127 | if (cmd->pid == 0) |
| 11128 | continue; |
| 11129 | /* |
| 11130 | * kill_main has matching code to expect |
| 11131 | * leading space. Needed to not confuse |
| 11132 | * negative pids with "kill -SIGNAL_NO" syntax |
| 11133 | */ |
| 11134 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 11135 | } |
| 11136 | *dst = '\0'; |
| 11137 | } while (argv[++i]); |
| 11138 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11139 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11140 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11141 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11142 | ret = run_applet_main(argv, kill_main); |
| 11143 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11144 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11145 | return ret; |
| 11146 | } |
| 11147 | #endif |
| 11148 | |
| 11149 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11150 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11151 | #if !ENABLE_HUSH_JOB |
| 11152 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 11153 | #endif |
| 11154 | 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] | 11155 | { |
| 11156 | int ret = 0; |
| 11157 | for (;;) { |
| 11158 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11159 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11160 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11161 | if (!sigisemptyset(&G.pending_set)) |
| 11162 | goto check_sig; |
| 11163 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11164 | /* waitpid is not interruptible by SA_RESTARTed |
| 11165 | * signals which we use. Thus, this ugly dance: |
| 11166 | */ |
| 11167 | |
| 11168 | /* Make sure possible SIGCHLD is stored in kernel's |
| 11169 | * pending signal mask before we call waitpid. |
| 11170 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11171 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11172 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11173 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 11174 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11175 | |
| 11176 | if (!sigisemptyset(&G.pending_set)) { |
| 11177 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11178 | goto restore; |
| 11179 | } |
| 11180 | |
| 11181 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11182 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11183 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11184 | debug_printf_exec("checkjobs:%d\n", ret); |
| 11185 | #if ENABLE_HUSH_JOB |
| 11186 | if (waitfor_pipe) { |
| 11187 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 11188 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 11189 | if (rcode >= 0) { |
| 11190 | ret = rcode; |
| 11191 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11192 | break; |
| 11193 | } |
| 11194 | } |
| 11195 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11196 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 11197 | /* if ret == 0, no children changed state */ |
| 11198 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11199 | if (errno == ECHILD || ret) { |
| 11200 | ret--; |
| 11201 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11202 | ret = 0; |
| 11203 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11204 | break; |
| 11205 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11206 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11207 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 11208 | /* Note: sigsuspend invokes signal handler */ |
| 11209 | sigsuspend(&oldset); |
| 11210 | restore: |
| 11211 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11212 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11213 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11214 | sig = check_and_run_traps(); |
| 11215 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 11216 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11217 | ret = 128 + sig; |
| 11218 | break; |
| 11219 | } |
| 11220 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 11221 | } |
| 11222 | return ret; |
| 11223 | } |
| 11224 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11225 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11226 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11227 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11228 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11229 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11230 | argv = skip_dash_dash(argv); |
| 11231 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 11232 | /* Don't care about wait results */ |
| 11233 | /* Note 1: must wait until there are no more children */ |
| 11234 | /* Note 2: must be interruptible */ |
| 11235 | /* Examples: |
| 11236 | * $ sleep 3 & sleep 6 & wait |
| 11237 | * [1] 30934 sleep 3 |
| 11238 | * [2] 30935 sleep 6 |
| 11239 | * [1] Done sleep 3 |
| 11240 | * [2] Done sleep 6 |
| 11241 | * $ sleep 3 & sleep 6 & wait |
| 11242 | * [1] 30936 sleep 3 |
| 11243 | * [2] 30937 sleep 6 |
| 11244 | * [1] Done sleep 3 |
| 11245 | * ^C <-- after ~4 sec from keyboard |
| 11246 | * $ |
| 11247 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11248 | 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] | 11249 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11250 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11251 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11252 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11253 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11254 | #if ENABLE_HUSH_JOB |
| 11255 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11256 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11257 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11258 | wait_pipe = parse_jobspec(*argv); |
| 11259 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11260 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11261 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11262 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11263 | } else { |
| 11264 | /* waiting on "last dead job" removes it */ |
| 11265 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 11266 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11267 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11268 | /* else: parse_jobspec() already emitted error msg */ |
| 11269 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11270 | } |
| 11271 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11272 | /* mimic bash message */ |
| 11273 | 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] | 11274 | ret = EXIT_FAILURE; |
| 11275 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11276 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11277 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11278 | /* Do we have such child? */ |
| 11279 | ret = waitpid(pid, &status, WNOHANG); |
| 11280 | if (ret < 0) { |
| 11281 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11282 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11283 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 11284 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11285 | /* "wait $!" but last bg task has already exited. Try: |
| 11286 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 11287 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11288 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11289 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11290 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11291 | } else { |
| 11292 | /* Example: "wait 1". mimic bash message */ |
| 11293 | 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] | 11294 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11295 | } else { |
| 11296 | /* ??? */ |
| 11297 | bb_perror_msg("wait %s", *argv); |
| 11298 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11299 | continue; /* bash checks all argv[] */ |
| 11300 | } |
| 11301 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11302 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11303 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11304 | } else { |
| 11305 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11306 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 11307 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11308 | if (WIFSIGNALED(status)) |
| 11309 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11310 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11311 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11312 | |
| 11313 | return ret; |
| 11314 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11315 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11316 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11317 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 11318 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 11319 | { |
| 11320 | if (argv[1]) { |
| 11321 | def = bb_strtou(argv[1], NULL, 10); |
| 11322 | if (errno || def < def_min || argv[2]) { |
| 11323 | bb_error_msg("%s: bad arguments", argv[0]); |
| 11324 | def = UINT_MAX; |
| 11325 | } |
| 11326 | } |
| 11327 | return def; |
| 11328 | } |
| 11329 | #endif |
| 11330 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11331 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11332 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11333 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11334 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11335 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11336 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11337 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 11338 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 11339 | return EXIT_SUCCESS; /* bash compat */ |
| 11340 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11341 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11342 | |
| 11343 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 11344 | if (depth == UINT_MAX) |
| 11345 | G.flag_break_continue = BC_BREAK; |
| 11346 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11347 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11348 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11349 | return EXIT_SUCCESS; |
| 11350 | } |
| 11351 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11352 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11353 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11354 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 11355 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11356 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11357 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11358 | |
| 11359 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11360 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11361 | { |
| 11362 | int rc; |
| 11363 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11364 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11365 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 11366 | return EXIT_FAILURE; /* bash compat */ |
| 11367 | } |
| 11368 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11369 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11370 | |
| 11371 | /* bash: |
| 11372 | * out of range: wraps around at 256, does not error out |
| 11373 | * non-numeric param: |
| 11374 | * f() { false; return qwe; }; f; echo $? |
| 11375 | * bash: return: qwe: numeric argument required <== we do this |
| 11376 | * 255 <== we also do this |
| 11377 | */ |
| 11378 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 11379 | return rc; |
| 11380 | } |
| 11381 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11382 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11383 | #if ENABLE_HUSH_TIMES |
| 11384 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11385 | { |
| 11386 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11387 | ' ', offsetof(struct tms, tms_utime), |
| 11388 | '\n', offsetof(struct tms, tms_stime), |
| 11389 | ' ', offsetof(struct tms, tms_cutime), |
| 11390 | '\n', offsetof(struct tms, tms_cstime), |
| 11391 | 0 |
| 11392 | }; |
| 11393 | const uint8_t *p; |
| 11394 | unsigned clk_tck; |
| 11395 | struct tms buf; |
| 11396 | |
| 11397 | clk_tck = bb_clk_tck(); |
| 11398 | |
| 11399 | times(&buf); |
| 11400 | p = times_tbl; |
| 11401 | do { |
| 11402 | unsigned sec, frac; |
| 11403 | unsigned long t; |
| 11404 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11405 | sec = t / clk_tck; |
| 11406 | frac = t % clk_tck; |
| 11407 | printf("%um%u.%03us%c", |
| 11408 | sec / 60, sec % 60, |
| 11409 | (frac * 1000) / clk_tck, |
| 11410 | p[0]); |
| 11411 | p += 2; |
| 11412 | } while (*p); |
| 11413 | |
| 11414 | return EXIT_SUCCESS; |
| 11415 | } |
| 11416 | #endif |
| 11417 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11418 | #if ENABLE_HUSH_MEMLEAK |
| 11419 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11420 | { |
| 11421 | void *p; |
| 11422 | unsigned long l; |
| 11423 | |
| 11424 | # ifdef M_TRIM_THRESHOLD |
| 11425 | /* Optional. Reduces probability of false positives */ |
| 11426 | malloc_trim(0); |
| 11427 | # endif |
| 11428 | /* Crude attempt to find where "free memory" starts, |
| 11429 | * sans fragmentation. */ |
| 11430 | p = malloc(240); |
| 11431 | l = (unsigned long)p; |
| 11432 | free(p); |
| 11433 | p = malloc(3400); |
| 11434 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11435 | free(p); |
| 11436 | |
| 11437 | |
| 11438 | # if 0 /* debug */ |
| 11439 | { |
| 11440 | struct mallinfo mi = mallinfo(); |
| 11441 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11442 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11443 | } |
| 11444 | # endif |
| 11445 | |
| 11446 | if (!G.memleak_value) |
| 11447 | G.memleak_value = l; |
| 11448 | |
| 11449 | l -= G.memleak_value; |
| 11450 | if ((long)l < 0) |
| 11451 | l = 0; |
| 11452 | l /= 1024; |
| 11453 | if (l > 127) |
| 11454 | l = 127; |
| 11455 | |
| 11456 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11457 | return l; |
| 11458 | } |
| 11459 | #endif |