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) |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 66 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 67 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 68 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 69 | * 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] | 70 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 71 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 72 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 73 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 74 | * indirect expansion: ${!VAR} |
| 75 | * substring op on @: ${@:n:m} |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 76 | * |
| 77 | * Won't do: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 78 | * Some builtins mandated by standards: |
| 79 | * newgrp [GRP]: not a builtin in bash but a suid binary |
| 80 | * which spawns a new shell with new group ID |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 81 | * |
| 82 | * Status of [[ support: |
| 83 | * [[ args ]] are CMD_SINGLEWORD_NOGLOB: |
| 84 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 85 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 86 | * = is glob match operator, not equality operator: STR = GLOB |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 87 | * == same as = |
| 88 | * =~ is regex match operator: STR =~ REGEX |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 89 | * TODO: |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 90 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
Denys Vlasenko | a7c0653 | 2020-10-31 04:32:34 +0100 | [diff] [blame] | 91 | * in word = GLOB, quoting should be significant on char-by-char basis: a*cd"*" |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 92 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 93 | //config:config HUSH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 94 | //config: bool "hush (68 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 95 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 96 | //config: select SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 97 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 98 | //config: hush is a small shell. It handles the normal flow control |
| 99 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 100 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 101 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 102 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 103 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 104 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 105 | //config: It does not handle select, aliases, tilde expansion, |
| 106 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 107 | //config: |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 108 | // This option is visible (has a description) to make it possible to select |
| 109 | // a "scripted" applet (such as NOLOGIN) but avoid selecting any shells: |
| 110 | //config:config SHELL_HUSH |
| 111 | //config: bool "Internal shell for embedded script support" |
| 112 | //config: default n |
| 113 | //config: |
| 114 | //config:# hush options |
| 115 | //config:# It's only needed to get "nice" menuconfig indenting. |
| 116 | //config:if SHELL_HUSH || HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 117 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 118 | //config:config HUSH_BASH_COMPAT |
| 119 | //config: bool "bash-compatible extensions" |
| 120 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 121 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 122 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 123 | //config:config HUSH_BRACE_EXPANSION |
| 124 | //config: bool "Brace expansion" |
| 125 | //config: default y |
| 126 | //config: depends on HUSH_BASH_COMPAT |
| 127 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 128 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 129 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 130 | //config:config HUSH_LINENO_VAR |
| 131 | //config: bool "$LINENO variable" |
| 132 | //config: default y |
| 133 | //config: depends on HUSH_BASH_COMPAT |
| 134 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 135 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 136 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 137 | //config: default n # do not encourage non-standard behavior |
| 138 | //config: depends on HUSH_BASH_COMPAT |
| 139 | //config: help |
| 140 | //config: This is not compliant with standards. Avoid if possible. |
| 141 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config:config HUSH_INTERACTIVE |
| 143 | //config: bool "Interactive mode" |
| 144 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 145 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 146 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 147 | //config: Enable interactive mode (prompt and command editing). |
| 148 | //config: Without this, hush simply reads and executes commands |
| 149 | //config: from stdin just like a shell script from a file. |
| 150 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 151 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 152 | //config:config HUSH_SAVEHISTORY |
| 153 | //config: bool "Save command history to .hush_history" |
| 154 | //config: default y |
| 155 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 156 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 157 | //config:config HUSH_JOB |
| 158 | //config: bool "Job control" |
| 159 | //config: default y |
| 160 | //config: depends on HUSH_INTERACTIVE |
| 161 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 162 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 163 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 164 | //config: "cmd &" still works by simply spawning a process and immediately |
| 165 | //config: prompting for next command (or executing next command in a script), |
| 166 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 167 | //config: |
| 168 | //config:config HUSH_TICK |
Ron Yorston | 060f0a0 | 2018-11-09 12:00:39 +0000 | [diff] [blame] | 169 | //config: bool "Support command substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 170 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 171 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 172 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 173 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 174 | //config: |
| 175 | //config:config HUSH_IF |
| 176 | //config: bool "Support if/then/elif/else/fi" |
| 177 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 178 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 179 | //config: |
| 180 | //config:config HUSH_LOOPS |
| 181 | //config: bool "Support for, while and until loops" |
| 182 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 183 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 184 | //config: |
| 185 | //config:config HUSH_CASE |
| 186 | //config: bool "Support case ... esac statement" |
| 187 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 188 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 190 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 191 | //config: |
| 192 | //config:config HUSH_FUNCTIONS |
| 193 | //config: bool "Support funcname() { commands; } syntax" |
| 194 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 195 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 196 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 197 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 198 | //config: |
| 199 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 200 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 201 | //config: default y |
| 202 | //config: depends on HUSH_FUNCTIONS |
| 203 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 204 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 205 | //config: |
| 206 | //config:config HUSH_RANDOM_SUPPORT |
| 207 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 208 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 209 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 210 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 211 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 212 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 213 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 214 | //config:config HUSH_MODE_X |
| 215 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 216 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 217 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 218 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 219 | //config: This instructs hush to print commands before execution. |
| 220 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 221 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 222 | //config:config HUSH_ECHO |
| 223 | //config: bool "echo builtin" |
| 224 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 225 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 226 | //config: |
| 227 | //config:config HUSH_PRINTF |
| 228 | //config: bool "printf builtin" |
| 229 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 230 | //config: depends on SHELL_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 231 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 232 | //config:config HUSH_TEST |
| 233 | //config: bool "test builtin" |
| 234 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 235 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 236 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 237 | //config:config HUSH_HELP |
| 238 | //config: bool "help builtin" |
| 239 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 240 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 241 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 242 | //config:config HUSH_EXPORT |
| 243 | //config: bool "export builtin" |
| 244 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 245 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 246 | //config: |
| 247 | //config:config HUSH_EXPORT_N |
| 248 | //config: bool "Support 'export -n' option" |
| 249 | //config: default y |
| 250 | //config: depends on HUSH_EXPORT |
| 251 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 252 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 253 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 254 | //config:config HUSH_READONLY |
| 255 | //config: bool "readonly builtin" |
| 256 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 257 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 258 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 259 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 260 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 261 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 262 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 263 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 264 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 265 | //config: |
| 266 | //config:config HUSH_WAIT |
| 267 | //config: bool "wait builtin" |
| 268 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 269 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 270 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 271 | //config:config HUSH_COMMAND |
| 272 | //config: bool "command builtin" |
| 273 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 274 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 275 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 276 | //config:config HUSH_TRAP |
| 277 | //config: bool "trap builtin" |
| 278 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 279 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 280 | //config: |
| 281 | //config:config HUSH_TYPE |
| 282 | //config: bool "type builtin" |
| 283 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 284 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 285 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 286 | //config:config HUSH_TIMES |
| 287 | //config: bool "times builtin" |
| 288 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 289 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 290 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 291 | //config:config HUSH_READ |
| 292 | //config: bool "read builtin" |
| 293 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 294 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 295 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 296 | //config:config HUSH_SET |
| 297 | //config: bool "set builtin" |
| 298 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 299 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 300 | //config: |
| 301 | //config:config HUSH_UNSET |
| 302 | //config: bool "unset builtin" |
| 303 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 304 | //config: depends on SHELL_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 305 | //config: |
| 306 | //config:config HUSH_ULIMIT |
| 307 | //config: bool "ulimit builtin" |
| 308 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 309 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 310 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 311 | //config:config HUSH_UMASK |
| 312 | //config: bool "umask builtin" |
| 313 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 314 | //config: depends on SHELL_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 315 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 316 | //config:config HUSH_GETOPTS |
| 317 | //config: bool "getopts builtin" |
| 318 | //config: default y |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 319 | //config: depends on SHELL_HUSH |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 320 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 321 | //config:config HUSH_MEMLEAK |
| 322 | //config: bool "memleak builtin (debugging)" |
| 323 | //config: default n |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 324 | //config: depends on SHELL_HUSH |
| 325 | //config: |
| 326 | //config:endif # hush options |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 327 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 328 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 329 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 330 | //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] | 331 | //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] | 332 | |
Denys Vlasenko | 67e1529 | 2020-06-24 13:39:13 +0200 | [diff] [blame] | 333 | //kbuild:lib-$(CONFIG_SHELL_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 334 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 335 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 336 | /* -i (interactive) is also accepted, |
| 337 | * but does nothing, therefore not shown in help. |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 338 | * NOMMU-specific options are not meant to be used by users, |
| 339 | * therefore we don't show them either. |
| 340 | */ |
| 341 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 342 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 343 | //usage:#define hush_full_usage "\n\n" |
| 344 | //usage: "Unix shell interpreter" |
| 345 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 346 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 347 | || defined(__APPLE__) \ |
| 348 | ) |
| 349 | # include <malloc.h> /* for malloc_trim */ |
| 350 | #endif |
| 351 | #include <glob.h> |
| 352 | /* #include <dmalloc.h> */ |
| 353 | #if ENABLE_HUSH_CASE |
| 354 | # include <fnmatch.h> |
| 355 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 356 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 357 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 358 | |
| 359 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 360 | #include "unicode.h" |
| 361 | #include "shell_common.h" |
| 362 | #include "math.h" |
| 363 | #include "match.h" |
| 364 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 365 | # include "random.h" |
| 366 | #else |
| 367 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 368 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 369 | #ifndef O_CLOEXEC |
| 370 | # define O_CLOEXEC 0 |
| 371 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 372 | #ifndef F_DUPFD_CLOEXEC |
| 373 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 374 | #endif |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 375 | |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 376 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !(ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH) |
| 377 | # include "embedded_scripts.h" |
| 378 | #else |
| 379 | # define NUM_SCRIPTS 0 |
| 380 | #endif |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 381 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 382 | /* So far, all bash compat is controlled by one config option */ |
| 383 | /* Separate defines document which part of code implements what */ |
| 384 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 385 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 386 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 387 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 388 | #define BASH_EPOCH_VARS ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 389 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 390 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 391 | |
| 392 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 393 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 394 | #define LEAK_HUNTING 0 |
| 395 | #define BUILD_AS_NOMMU 0 |
| 396 | /* Enable/disable sanity checks. Ok to enable in production, |
| 397 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 398 | * Keeping 1 for now even in released versions. |
| 399 | */ |
| 400 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 401 | /* Slightly bigger (+200 bytes), but faster hush. |
| 402 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 403 | * which allows us to do fewer waitpid's. |
| 404 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 405 | * and therefore waitpid will return the same result as last time) |
| 406 | */ |
| 407 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 408 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 409 | * So far ${var%...} ops are always enabled: |
| 410 | */ |
| 411 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 412 | |
| 413 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 414 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 415 | # undef BB_MMU |
| 416 | # undef USE_FOR_NOMMU |
| 417 | # undef USE_FOR_MMU |
| 418 | # define BB_MMU 0 |
| 419 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 420 | # define USE_FOR_MMU(...) |
| 421 | #endif |
| 422 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 423 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 424 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 425 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 426 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 427 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 428 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 429 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 430 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 431 | # define IF_FEATURE_SH_STANDALONE(...) |
| 432 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 433 | #endif |
| 434 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 435 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 436 | # undef ENABLE_FEATURE_EDITING |
| 437 | # define ENABLE_FEATURE_EDITING 0 |
| 438 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 439 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 440 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 441 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 442 | #endif |
| 443 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 444 | /* Do we support ANY keywords? */ |
| 445 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 446 | # define HAS_KEYWORDS 1 |
| 447 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 448 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 449 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 450 | # define HAS_KEYWORDS 0 |
| 451 | # define IF_HAS_KEYWORDS(...) |
| 452 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 453 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 454 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 455 | /* If you comment out one of these below, it will be #defined later |
| 456 | * to perform debug printfs to stderr: */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 457 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 458 | /* Finer-grained debug switches */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 459 | #define debug_printf_parse(...) do {} while (0) |
| 460 | #define debug_printf_heredoc(...) do {} while (0) |
| 461 | #define debug_print_tree(a, b) do {} while (0) |
| 462 | #define debug_printf_exec(...) do {} while (0) |
| 463 | #define debug_printf_env(...) do {} while (0) |
| 464 | #define debug_printf_jobs(...) do {} while (0) |
| 465 | #define debug_printf_expand(...) do {} while (0) |
| 466 | #define debug_printf_varexp(...) do {} while (0) |
| 467 | #define debug_printf_glob(...) do {} while (0) |
| 468 | #define debug_printf_redir(...) do {} while (0) |
| 469 | #define debug_printf_list(...) do {} while (0) |
| 470 | #define debug_printf_subst(...) do {} while (0) |
| 471 | #define debug_printf_prompt(...) do {} while (0) |
| 472 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 473 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 474 | #define ERR_PTR ((void*)(long)1) |
| 475 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 476 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 477 | |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 478 | #define _SPECIAL_VARS_STR "_*@$!?#-" |
| 479 | #define SPECIAL_VARS_STR ("_*@$!?#-" + 1) |
| 480 | #define NUMERIC_SPECVARS_STR ("_*@$!?#-" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 481 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 482 | /* Support / and // replace ops */ |
| 483 | /* Note that // is stored as \ in "encoded" string representation */ |
| 484 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 485 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 486 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 487 | #else |
| 488 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 489 | # define VAR_SUBST_OPS "%#:-=+?" |
| 490 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 491 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 492 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 493 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 494 | #define SPECIAL_VAR_SYMBOL 3 |
| 495 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 496 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 497 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 498 | struct variable; |
| 499 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 500 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 501 | |
| 502 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 503 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 504 | */ |
| 505 | #if !BB_MMU |
| 506 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 507 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 508 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 509 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 510 | } nommu_save_t; |
| 511 | #endif |
| 512 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 513 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 514 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 515 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 516 | RES_IF , |
| 517 | RES_THEN , |
| 518 | RES_ELIF , |
| 519 | RES_ELSE , |
| 520 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 521 | #endif |
| 522 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 523 | RES_FOR , |
| 524 | RES_WHILE , |
| 525 | RES_UNTIL , |
| 526 | RES_DO , |
| 527 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 530 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 531 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 532 | #if ENABLE_HUSH_CASE |
| 533 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 534 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 535 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 536 | RES_MATCH , /* "word)" */ |
| 537 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 538 | RES_ESAC , |
| 539 | #endif |
| 540 | RES_XXXX , |
| 541 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 542 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 543 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 544 | typedef struct o_string { |
| 545 | char *data; |
| 546 | int length; /* position where data is appended */ |
| 547 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 548 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 549 | /* At least some part of the string was inside '' or "", |
| 550 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 551 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 552 | smallint has_empty_slot; |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 553 | smallint ended_in_ifs; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 554 | } o_string; |
| 555 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 556 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 557 | EXP_FLAG_GLOB = 0x2, |
| 558 | /* Protect newly added chars against globbing |
| 559 | * by prepending \ to *, ?, [, \ */ |
| 560 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 561 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 562 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 563 | #define NULL_O_STRING { NULL } |
| 564 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 565 | #ifndef debug_printf_parse |
| 566 | static const char *const assignment_flag[] = { |
| 567 | "MAYBE_ASSIGNMENT", |
| 568 | "DEFINITELY_ASSIGNMENT", |
| 569 | "NOT_ASSIGNMENT", |
| 570 | "WORD_IS_KEYWORD", |
| 571 | }; |
| 572 | #endif |
| 573 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 574 | /* We almost can use standard FILE api, but we need an ability to move |
| 575 | * its fd when redirects coincide with it. No api exists for that |
| 576 | * (RFE for it at https://sourceware.org/bugzilla/show_bug.cgi?id=21902). |
| 577 | * HFILE is our internal alternative. Only supports reading. |
| 578 | * Since we now can, we incorporate linked list of all opened HFILEs |
| 579 | * into the struct (used to be a separate mini-list). |
| 580 | */ |
| 581 | typedef struct HFILE { |
| 582 | char *cur; |
| 583 | char *end; |
| 584 | struct HFILE *next_hfile; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 585 | int fd; |
| 586 | char buf[1024]; |
| 587 | } HFILE; |
| 588 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 589 | typedef struct in_str { |
| 590 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 591 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 592 | int last_char; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 593 | HFILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 594 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 595 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 596 | /* The descrip member of this structure is only used to make |
| 597 | * debugging output pretty */ |
| 598 | static const struct { |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 599 | int32_t mode; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 600 | signed char default_fd; |
| 601 | char descrip[3]; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 602 | } redir_table[] ALIGN4 = { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 603 | { O_RDONLY, 0, "<" }, |
| 604 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 605 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 606 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 607 | { O_RDONLY, 0, "<<" }, |
| 608 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 609 | /* { O_RDONLY, 77, "<<" }, */ |
| 610 | }; |
| 611 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 612 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 613 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 614 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 615 | int rd_fd; /* fd to redirect */ |
| 616 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 617 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 618 | smallint rd_type; /* (enum redir_type) */ |
| 619 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 620 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 621 | * bit 0: do we need to trim leading tabs? |
| 622 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 623 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 624 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 625 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 626 | REDIRECT_INPUT = 0, |
| 627 | REDIRECT_OVERWRITE = 1, |
| 628 | REDIRECT_APPEND = 2, |
| 629 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 630 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 631 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 632 | |
| 633 | REDIRFD_CLOSE = -3, |
| 634 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 635 | REDIRFD_TO_FILE = -1, |
| 636 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 637 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 638 | HEREDOC_SKIPTABS = 1, |
| 639 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 640 | } redir_type; |
| 641 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 642 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 643 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 644 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 645 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 646 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 647 | unsigned lineno; |
| 648 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 649 | smallint cmd_type; /* CMD_xxx */ |
| 650 | #define CMD_NORMAL 0 |
| 651 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 652 | #if BASH_TEST2 |
| 653 | /* used for "[[ EXPR ]]" */ |
| 654 | # define CMD_TEST2_SINGLEWORD_NOGLOB 2 |
| 655 | #endif |
| 656 | #if ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 657 | /* used to prevent word splitting and globbing in "export v=t*" */ |
| 658 | # define CMD_SINGLEWORD_NOGLOB 3 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 659 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 660 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 661 | # define CMD_FUNCDEF 4 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 662 | #endif |
| 663 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 664 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 665 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 666 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 667 | #if !BB_MMU |
| 668 | char *group_as_string; |
| 669 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 670 | #if ENABLE_HUSH_FUNCTIONS |
| 671 | struct function *child_func; |
| 672 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 673 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 674 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 675 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 676 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 677 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 678 | * we put those fields back into cmd->xxx |
| 679 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 680 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 681 | * Without this trick, loop would execute a;b;b;b;... |
| 682 | * instead of correct sequence a;b;a;b;... |
| 683 | * When command is freed, it severs the link |
| 684 | * (sets ->child_func->parent_cmd to NULL). |
| 685 | */ |
| 686 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 687 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 688 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 689 | * and on execution these are substituted with their values. |
| 690 | * Substitution can make _several_ words out of one argv[n]! |
| 691 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 692 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 693 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 694 | struct redir_struct *redirects; /* I/O redirections */ |
| 695 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 696 | /* Is there anything in this command at all? */ |
| 697 | #define IS_NULL_CMD(cmd) \ |
| 698 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 699 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 700 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 701 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 702 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 703 | int alive_cmds; /* number of commands running (not exited) */ |
| 704 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 705 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 706 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 707 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 708 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 709 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 710 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 711 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 712 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 713 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 714 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 715 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 716 | PIPE_SEQ = 0, |
| 717 | PIPE_AND = 1, |
| 718 | PIPE_OR = 2, |
| 719 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 720 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 721 | /* Is there anything in this pipe at all? */ |
| 722 | #define IS_NULL_PIPE(pi) \ |
| 723 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 724 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 725 | /* This holds pointers to the various results of parsing */ |
| 726 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 727 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 728 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 729 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 730 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 731 | /* last command in pipe (being constructed right now) */ |
| 732 | struct command *command; |
| 733 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 734 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 735 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 736 | #if !BB_MMU |
| 737 | o_string as_string; |
| 738 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 739 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 740 | #if HAS_KEYWORDS |
| 741 | smallint ctx_res_w; |
| 742 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 743 | #if ENABLE_HUSH_CASE |
| 744 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 745 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 746 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 747 | int old_flag; |
| 748 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 749 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 750 | * when we see "if" or "then", we malloc and copy current context, |
| 751 | * and make ->stack point to it. then we parse pipeN. |
| 752 | * when closing "then" / fi" / whatever is found, |
| 753 | * we move list_head into ->stack->command->group, |
| 754 | * copy ->stack into current context, and delete ->stack. |
| 755 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 756 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 757 | struct parse_context *stack; |
| 758 | #endif |
| 759 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 760 | enum { |
| 761 | MAYBE_ASSIGNMENT = 0, |
| 762 | DEFINITELY_ASSIGNMENT = 1, |
| 763 | NOT_ASSIGNMENT = 2, |
| 764 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 765 | WORD_IS_KEYWORD = 3, |
| 766 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 767 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 768 | /* On program start, environ points to initial environment. |
| 769 | * putenv adds new pointers into it, unsetenv removes them. |
| 770 | * Neither of these (de)allocates the strings. |
| 771 | * setenv allocates new strings in malloc space and does putenv, |
| 772 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 773 | #define setenv(...) setenv_is_leaky_dont_use() |
| 774 | struct variable { |
| 775 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 776 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 777 | 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] | 778 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 779 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 780 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 781 | }; |
| 782 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 783 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 784 | BC_BREAK = 1, |
| 785 | BC_CONTINUE = 2, |
| 786 | }; |
| 787 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 788 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 789 | struct function { |
| 790 | struct function *next; |
| 791 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 792 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 793 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 794 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 795 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 796 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 797 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 798 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 799 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 800 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 801 | /* set -/+o OPT support. (TODO: make it optional) |
| 802 | * bash supports the following opts: |
| 803 | * allexport off |
| 804 | * braceexpand on |
| 805 | * emacs on |
| 806 | * errexit off |
| 807 | * errtrace off |
| 808 | * functrace off |
| 809 | * hashall on |
| 810 | * histexpand off |
| 811 | * history on |
| 812 | * ignoreeof off |
| 813 | * interactive-comments on |
| 814 | * keyword off |
| 815 | * monitor on |
| 816 | * noclobber off |
| 817 | * noexec off |
| 818 | * noglob off |
| 819 | * nolog off |
| 820 | * notify off |
| 821 | * nounset off |
| 822 | * onecmd off |
| 823 | * physical off |
| 824 | * pipefail off |
| 825 | * posix off |
| 826 | * privileged off |
| 827 | * verbose off |
| 828 | * vi off |
| 829 | * xtrace off |
| 830 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 831 | static const char o_opt_strings[] ALIGN1 = |
| 832 | "pipefail\0" |
| 833 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 834 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 835 | #if ENABLE_HUSH_MODE_X |
| 836 | "xtrace\0" |
| 837 | #endif |
| 838 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 839 | enum { |
| 840 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 841 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 842 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 843 | #if ENABLE_HUSH_MODE_X |
| 844 | OPT_O_XTRACE, |
| 845 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 846 | NUM_OPT_O |
| 847 | }; |
| 848 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 849 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 850 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 851 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 852 | /* interactive_fd != 0 means we are an interactive shell. |
| 853 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 854 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 855 | * job control still works, but terminal signals |
| 856 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 857 | * process groups can only be created with "cmd &". |
| 858 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 859 | * to give tty to the foreground process group, |
| 860 | * and will take it back when the group is stopped (^Z) |
| 861 | * or killed (^C). |
| 862 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 863 | #if ENABLE_HUSH_INTERACTIVE |
| 864 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 865 | * _AND_ if we decided to act interactively */ |
| 866 | int interactive_fd; |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 867 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(char *PS1;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 868 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 869 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 870 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 871 | #endif |
| 872 | #if ENABLE_FEATURE_EDITING |
| 873 | line_input_t *line_input_state; |
| 874 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 875 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 876 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 877 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 878 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 879 | random_t random_gen; |
| 880 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 881 | #if ENABLE_HUSH_JOB |
| 882 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 883 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 884 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 885 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 886 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 887 | #else |
| 888 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 889 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 890 | /* How deeply are we in context where "set -e" is ignored */ |
| 891 | int errexit_depth; |
| 892 | /* "set -e" rules (do we follow them correctly?): |
| 893 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 894 | * Shell does not exit if failed command is part of condition in |
| 895 | * if/while, part of && or || list except the last command, any command |
| 896 | * in a pipe but the last, or if the command's return value is being |
| 897 | * inverted with !. If a compound command other than a subshell returns a |
| 898 | * non-zero status because a command failed while -e was being ignored, the |
| 899 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 900 | * exits [ERR is a bashism]. |
| 901 | * |
| 902 | * If a compound command or function executes in a context where -e is |
| 903 | * ignored, none of the commands executed within are affected by the -e |
| 904 | * setting. If a compound command or function sets -e while executing in a |
| 905 | * context where -e is ignored, that setting does not have any effect until |
| 906 | * the compound command or the command containing the function call completes. |
| 907 | */ |
| 908 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 909 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 910 | #if ENABLE_HUSH_MODE_X |
| 911 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 912 | #else |
| 913 | # define G_x_mode 0 |
| 914 | #endif |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 915 | char opt_s; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 916 | char opt_c; |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 917 | #if ENABLE_HUSH_INTERACTIVE |
| 918 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 919 | # if ENABLE_FEATURE_EDITING |
| 920 | smallint flag_ctrlC; /* when set, suppresses syntax error messages */ |
| 921 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 922 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 923 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 924 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 925 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 926 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 927 | #if ENABLE_HUSH_FUNCTIONS |
| 928 | /* 0: outside of a function (or sourced file) |
| 929 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 930 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 931 | */ |
| 932 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 933 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 934 | #else |
| 935 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 936 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 937 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 938 | /* These support $? */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 939 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 940 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 941 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 942 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 943 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 944 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 945 | # define G_global_args_malloced (G.global_args_malloced) |
| 946 | #else |
| 947 | # define G_global_args_malloced 0 |
| 948 | #endif |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 949 | #if ENABLE_HUSH_BASH_COMPAT |
| 950 | int dead_job_exitcode; /* for "wait -n" */ |
| 951 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 952 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 953 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 954 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 955 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 956 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 957 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 958 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 959 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 960 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 961 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 962 | #if ENABLE_HUSH_GETOPTS |
| 963 | unsigned getopt_count; |
| 964 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 965 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 966 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 967 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 968 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 969 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 970 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 971 | unsigned var_nest_level; |
| 972 | #if ENABLE_HUSH_FUNCTIONS |
| 973 | # if ENABLE_HUSH_LOCAL |
| 974 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 975 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 976 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 977 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 978 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 979 | #if ENABLE_HUSH_FAST |
| 980 | unsigned count_SIGCHLD; |
| 981 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 982 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 983 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 984 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 985 | unsigned parse_lineno; |
| 986 | unsigned execute_lineno; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 987 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 988 | HFILE *HFILE_list; |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 989 | HFILE *HFILE_stdin; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 990 | /* Which signals have non-DFL handler (even with no traps set)? |
| 991 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 992 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 993 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 994 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 995 | * Other than these two times, never modified. |
| 996 | */ |
| 997 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 998 | #if ENABLE_HUSH_JOB |
| 999 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1000 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1001 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1002 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1003 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1004 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 1005 | int pre_trap_exitcode; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 1006 | # if ENABLE_HUSH_FUNCTIONS |
| 1007 | int return_exitcode; |
| 1008 | # endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1009 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1010 | # define G_traps G.traps |
| 1011 | #else |
| 1012 | # define G_traps ((char**)NULL) |
| 1013 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1014 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1015 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1016 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1017 | #endif |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 1018 | #if ENABLE_HUSH_MODE_X |
| 1019 | unsigned x_mode_depth; |
| 1020 | /* "set -x" output should not be redirectable with subsequent 2>FILE. |
| 1021 | * We dup fd#2 to x_mode_fd when "set -x" is executed, and use it |
| 1022 | * for all subsequent output. |
| 1023 | */ |
| 1024 | int x_mode_fd; |
| 1025 | o_string x_mode_buf; |
| 1026 | #endif |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1027 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1028 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1029 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1030 | struct sigaction sa; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 1031 | char optstring_buf[sizeof("eixcs")]; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 1032 | #if BASH_EPOCH_VARS |
| 1033 | char epoch_buf[sizeof("%lu.nnnnnn") + sizeof(long)*3]; |
| 1034 | #endif |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 1035 | #if ENABLE_FEATURE_EDITING |
| 1036 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 1037 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1038 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1039 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1040 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 1041 | * (too many defines). Also, I actually prefer to see when a variable |
| 1042 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1043 | #define INIT_G() do { \ |
| 1044 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1045 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 1046 | sigfillset(&G.sa.sa_mask); \ |
| 1047 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1048 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1049 | |
| 1050 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1051 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1052 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1053 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1054 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1055 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1056 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1057 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1058 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1059 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1060 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1061 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1062 | #if ENABLE_HUSH_READONLY |
| 1063 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1064 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1065 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1066 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1067 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1068 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1069 | #if ENABLE_HUSH_GETOPTS |
| 1070 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1071 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1072 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1073 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1074 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1075 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1076 | static int builtin_history(char **argv) FAST_FUNC; |
| 1077 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1078 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1079 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1080 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1081 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1082 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1083 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1084 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1085 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1086 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1087 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1088 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1089 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1090 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1091 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1092 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1093 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1094 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1095 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1096 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1097 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1098 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1099 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1100 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1101 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1102 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1103 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1104 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1105 | #if ENABLE_HUSH_TIMES |
| 1106 | static int builtin_times(char **argv) FAST_FUNC; |
| 1107 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1108 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1109 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1110 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1111 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1112 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1113 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1114 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1115 | #if ENABLE_HUSH_KILL |
| 1116 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1117 | #endif |
| 1118 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1119 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1120 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1121 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1122 | static int builtin_break(char **argv) FAST_FUNC; |
| 1123 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1124 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1125 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1126 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1127 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1128 | |
| 1129 | /* Table of built-in functions. They can be forked or not, depending on |
| 1130 | * context: within pipes, they fork. As simple commands, they do not. |
| 1131 | * When used in non-forking context, they can change global variables |
| 1132 | * in the parent shell process. If forked, of course they cannot. |
| 1133 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1134 | * still be set at the end. */ |
| 1135 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1136 | const char *b_cmd; |
| 1137 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1138 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1139 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1140 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1141 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1142 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1143 | #endif |
| 1144 | }; |
| 1145 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 1146 | static const struct built_in_command bltins1[] ALIGN_PTR = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1147 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1148 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1149 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1150 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1151 | #endif |
| 1152 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1153 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1154 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1155 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1156 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1157 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1158 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1159 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1160 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1161 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1162 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1163 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1164 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1165 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1166 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1167 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1168 | #if ENABLE_HUSH_GETOPTS |
| 1169 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1170 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1171 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1172 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1173 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1174 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1175 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1176 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1177 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1178 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1179 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1180 | #if ENABLE_HUSH_KILL |
| 1181 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1182 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1183 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1184 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1185 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1186 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1187 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1188 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1189 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1190 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1191 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1192 | #if ENABLE_HUSH_READONLY |
| 1193 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1194 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1195 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1196 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1197 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1198 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1199 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1200 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1201 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1202 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1203 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1204 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1205 | #if ENABLE_HUSH_TIMES |
| 1206 | BLTIN("times" , builtin_times , NULL), |
| 1207 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1208 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1209 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1210 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1211 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1212 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1213 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1214 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1215 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1216 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1217 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1218 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1219 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1220 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1221 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1222 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1223 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1224 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1225 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1226 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1227 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1228 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1229 | * (it's cheaper to run an external program in this case): |
| 1230 | */ |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 1231 | static const struct built_in_command bltins2[] ALIGN_PTR = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1232 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1233 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1234 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1235 | #if BASH_TEST2 |
| 1236 | BLTIN("[[" , builtin_test , NULL), |
| 1237 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1238 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1239 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1240 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1241 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1242 | BLTIN("printf" , builtin_printf , NULL), |
| 1243 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1244 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1245 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1246 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1247 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1248 | }; |
| 1249 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1250 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1251 | /* Debug printouts. |
| 1252 | */ |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1253 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1254 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1255 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1256 | # define debug_enter() (G.debug_indent++) |
| 1257 | # define debug_leave() (G.debug_indent--) |
| 1258 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1259 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1260 | # define debug_enter() ((void)0) |
| 1261 | # define debug_leave() ((void)0) |
| 1262 | #endif |
| 1263 | |
| 1264 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1265 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1266 | #endif |
| 1267 | |
| 1268 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1269 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1270 | #endif |
| 1271 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 1272 | #ifndef debug_printf_heredoc |
| 1273 | # define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1274 | #endif |
| 1275 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1276 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1277 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1278 | #endif |
| 1279 | |
| 1280 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1281 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1282 | #endif |
| 1283 | |
| 1284 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1285 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1286 | # define DEBUG_JOBS 1 |
| 1287 | #else |
| 1288 | # define DEBUG_JOBS 0 |
| 1289 | #endif |
| 1290 | |
| 1291 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1292 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1293 | # define DEBUG_EXPAND 1 |
| 1294 | #else |
| 1295 | # define DEBUG_EXPAND 0 |
| 1296 | #endif |
| 1297 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1298 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1299 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1300 | #endif |
| 1301 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1302 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1303 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1304 | # define DEBUG_GLOB 1 |
| 1305 | #else |
| 1306 | # define DEBUG_GLOB 0 |
| 1307 | #endif |
| 1308 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1309 | #ifndef debug_printf_redir |
| 1310 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1311 | #endif |
| 1312 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1313 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1314 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1315 | #endif |
| 1316 | |
| 1317 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1318 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1319 | #endif |
| 1320 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1321 | #ifndef debug_printf_prompt |
| 1322 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1323 | #endif |
| 1324 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1325 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1326 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1327 | # define DEBUG_CLEAN 1 |
| 1328 | #else |
| 1329 | # define DEBUG_CLEAN 0 |
| 1330 | #endif |
| 1331 | |
| 1332 | #if DEBUG_EXPAND |
| 1333 | static void debug_print_strings(const char *prefix, char **vv) |
| 1334 | { |
| 1335 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1336 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1337 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1338 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1339 | } |
| 1340 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1341 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1342 | #endif |
| 1343 | |
| 1344 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1345 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1346 | */ |
| 1347 | #if LEAK_HUNTING |
| 1348 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1349 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1350 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1351 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1352 | return ptr; |
| 1353 | } |
| 1354 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1355 | { |
| 1356 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1357 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1358 | return ptr; |
| 1359 | } |
| 1360 | static char *xxstrdup(int lineno, const char *str) |
| 1361 | { |
| 1362 | char *ptr = xstrdup(str); |
| 1363 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1364 | return ptr; |
| 1365 | } |
| 1366 | static void xxfree(void *ptr) |
| 1367 | { |
| 1368 | fdprintf(2, "free %p\n", ptr); |
| 1369 | free(ptr); |
| 1370 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1371 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1372 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1373 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1374 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1375 | #endif |
| 1376 | |
| 1377 | |
| 1378 | /* Syntax and runtime errors. They always abort scripts. |
| 1379 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1380 | * and return to the prompt. |
| 1381 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1382 | */ |
| 1383 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1384 | # 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] | 1385 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1386 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1387 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1388 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1389 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1390 | #endif |
| 1391 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1392 | static void die_if_script(void) |
| 1393 | { |
| 1394 | if (!G_interactive_fd) { |
| 1395 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1396 | xfunc_error_retval = G.last_exitcode; |
| 1397 | xfunc_die(); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1402 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1403 | va_list p; |
| 1404 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1405 | #if HUSH_DEBUG >= 2 |
| 1406 | bb_error_msg("hush.c:%u", lineno); |
| 1407 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1408 | va_start(p, fmt); |
| 1409 | bb_verror_msg(fmt, p, NULL); |
| 1410 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1411 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1412 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1413 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1414 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1415 | { |
| 1416 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1417 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1418 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1419 | bb_simple_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1420 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1423 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1424 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1425 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1426 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1429 | 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] | 1430 | { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 1431 | #if ENABLE_FEATURE_EDITING |
| 1432 | if (!G.flag_ctrlC) |
| 1433 | #endif |
| 1434 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1435 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1436 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1439 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1440 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1441 | char msg[2] = { ch, '\0' }; |
| 1442 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1445 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1446 | { |
| 1447 | char msg[2]; |
| 1448 | msg[0] = ch; |
| 1449 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1450 | #if HUSH_DEBUG >= 2 |
| 1451 | bb_error_msg("hush.c:%u", lineno); |
| 1452 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1453 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1454 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1457 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1458 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1459 | # undef syntax_error |
| 1460 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1461 | # undef syntax_error_unterm_ch |
| 1462 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1463 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1464 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1465 | # 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] | 1466 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1467 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1468 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1469 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1470 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1471 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1472 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1473 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1474 | /* Utility functions |
| 1475 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1476 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1477 | static char *unbackslash(char *src) |
| 1478 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1479 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1480 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1481 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1482 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1483 | if (*src != '\0') { |
| 1484 | /* \x -> x */ |
| 1485 | *dst++ = *src++; |
| 1486 | continue; |
| 1487 | } |
| 1488 | /* else: "\<nul>". Do not delete this backslash. |
| 1489 | * Testcase: eval 'echo ok\' |
| 1490 | */ |
| 1491 | *dst++ = '\\'; |
| 1492 | /* fallthrough */ |
| 1493 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1494 | if ((*dst++ = *src++) == '\0') |
| 1495 | break; |
| 1496 | } |
| 1497 | return dst; |
| 1498 | } |
| 1499 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1500 | 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] | 1501 | { |
| 1502 | int i; |
| 1503 | unsigned count1; |
| 1504 | unsigned count2; |
| 1505 | char **v; |
| 1506 | |
| 1507 | v = strings; |
| 1508 | count1 = 0; |
| 1509 | if (v) { |
| 1510 | while (*v) { |
| 1511 | count1++; |
| 1512 | v++; |
| 1513 | } |
| 1514 | } |
| 1515 | count2 = 0; |
| 1516 | v = add; |
| 1517 | while (*v) { |
| 1518 | count2++; |
| 1519 | v++; |
| 1520 | } |
| 1521 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1522 | v[count1 + count2] = NULL; |
| 1523 | i = count2; |
| 1524 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1525 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1526 | return v; |
| 1527 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1528 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1529 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1530 | { |
| 1531 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1532 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1533 | return ptr; |
| 1534 | } |
| 1535 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1536 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1537 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1538 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1539 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1540 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1541 | { |
| 1542 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1543 | v[0] = add; |
| 1544 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1545 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1546 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1547 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1548 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1549 | { |
| 1550 | char **ptr = add_string_to_strings(strings, add); |
| 1551 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1552 | return ptr; |
| 1553 | } |
| 1554 | #define add_string_to_strings(strings, add) \ |
| 1555 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1556 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1557 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1558 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1559 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1560 | char **v; |
| 1561 | |
| 1562 | if (!strings) |
| 1563 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1564 | v = strings; |
| 1565 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1566 | free(*v); |
| 1567 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1568 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1569 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1572 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1573 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1574 | int newfd; |
| 1575 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1576 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1577 | if (newfd >= 0) { |
| 1578 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1579 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1580 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1581 | if (errno == EBUSY) |
| 1582 | goto repeat; |
| 1583 | if (errno == EINTR) |
| 1584 | goto repeat; |
| 1585 | } |
| 1586 | return newfd; |
| 1587 | } |
| 1588 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1589 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1590 | { |
| 1591 | int newfd; |
| 1592 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1593 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1594 | if (newfd < 0) { |
| 1595 | if (errno == EBUSY) |
| 1596 | goto repeat; |
| 1597 | if (errno == EINTR) |
| 1598 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1599 | /* fd was not open? */ |
| 1600 | if (errno == EBADF) |
| 1601 | return fd; |
| 1602 | xfunc_die(); |
| 1603 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1604 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1605 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1606 | close(fd); |
| 1607 | return newfd; |
| 1608 | } |
| 1609 | |
| 1610 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1611 | /* Manipulating HFILEs */ |
| 1612 | static HFILE *hfopen(const char *name) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1613 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1614 | HFILE *fp; |
| 1615 | int fd; |
| 1616 | |
| 1617 | fd = STDIN_FILENO; |
| 1618 | if (name) { |
| 1619 | fd = open(name, O_RDONLY | O_CLOEXEC); |
| 1620 | if (fd < 0) |
| 1621 | return NULL; |
| 1622 | if (O_CLOEXEC == 0) /* ancient libc */ |
| 1623 | close_on_exec_on(fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1624 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1625 | |
| 1626 | fp = xmalloc(sizeof(*fp)); |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 1627 | if (name == NULL) |
| 1628 | G.HFILE_stdin = fp; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1629 | fp->fd = fd; |
| 1630 | fp->cur = fp->end = fp->buf; |
| 1631 | fp->next_hfile = G.HFILE_list; |
| 1632 | G.HFILE_list = fp; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1633 | return fp; |
| 1634 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1635 | static void hfclose(HFILE *fp) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1636 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1637 | HFILE **pp = &G.HFILE_list; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1638 | while (*pp) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1639 | HFILE *cur = *pp; |
| 1640 | if (cur == fp) { |
| 1641 | *pp = cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1642 | break; |
| 1643 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1644 | pp = &cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1645 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1646 | if (fp->fd >= 0) |
| 1647 | close(fp->fd); |
| 1648 | free(fp); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1649 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1650 | static int refill_HFILE_and_getc(HFILE *fp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1651 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1652 | int n; |
| 1653 | |
| 1654 | if (fp->fd < 0) { |
| 1655 | /* Already saw EOF */ |
| 1656 | return EOF; |
| 1657 | } |
| 1658 | /* Try to buffer more input */ |
| 1659 | fp->cur = fp->buf; |
| 1660 | n = safe_read(fp->fd, fp->buf, sizeof(fp->buf)); |
| 1661 | if (n < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1662 | bb_simple_perror_msg("read error"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1663 | n = 0; |
| 1664 | } |
| 1665 | fp->end = fp->buf + n; |
| 1666 | if (n == 0) { |
| 1667 | /* EOF/error */ |
| 1668 | close(fp->fd); |
| 1669 | fp->fd = -1; |
| 1670 | return EOF; |
| 1671 | } |
| 1672 | return (unsigned char)(*fp->cur++); |
| 1673 | } |
| 1674 | /* Inlined for common case of non-empty buffer. |
| 1675 | */ |
| 1676 | static ALWAYS_INLINE int hfgetc(HFILE *fp) |
| 1677 | { |
| 1678 | if (fp->cur < fp->end) |
| 1679 | return (unsigned char)(*fp->cur++); |
| 1680 | /* Buffer empty */ |
| 1681 | return refill_HFILE_and_getc(fp); |
| 1682 | } |
| 1683 | static int move_HFILEs_on_redirect(int fd, int avoid_fd) |
| 1684 | { |
| 1685 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1686 | while (fl) { |
| 1687 | if (fd == fl->fd) { |
| 1688 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1689 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1690 | debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1691 | return 1; /* "found and moved" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1692 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1693 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1694 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 1695 | #if ENABLE_HUSH_MODE_X |
| 1696 | if (G.x_mode_fd > 0 && fd == G.x_mode_fd) { |
| 1697 | G.x_mode_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 1698 | return 1; /* "found and moved" */ |
| 1699 | } |
| 1700 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1701 | return 0; /* "not in the list" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1702 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1703 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1704 | static void close_all_HFILE_list(void) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1705 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1706 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1707 | while (fl) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1708 | /* hfclose would also free HFILE object. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1709 | * It is disastrous if we share memory with a vforked parent. |
| 1710 | * I'm not sure we never come here after vfork. |
| 1711 | * Therefore just close fd, nothing more. |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1712 | * |
| 1713 | * ">" instead of ">=": we don't close fd#0, |
| 1714 | * interactive shell uses hfopen(NULL) as stdin input |
| 1715 | * which has fl->fd == 0, but fd#0 gets redirected in pipes. |
| 1716 | * If we'd close it here, then e.g. interactive "set | sort" |
| 1717 | * with NOFORKed sort, would have sort's input fd closed. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1718 | */ |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1719 | if (fl->fd > 0) |
| 1720 | /*hfclose(fl); - unsafe */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1721 | close(fl->fd); |
| 1722 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1723 | } |
| 1724 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1725 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1726 | static int fd_in_HFILEs(int fd) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1727 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1728 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1729 | while (fl) { |
| 1730 | if (fl->fd == fd) |
| 1731 | return 1; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1732 | fl = fl->next_hfile; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1733 | } |
| 1734 | return 0; |
| 1735 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1736 | |
| 1737 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1738 | /* Helpers for setting new $n and restoring them back |
| 1739 | */ |
| 1740 | typedef struct save_arg_t { |
| 1741 | char *sv_argv0; |
| 1742 | char **sv_g_argv; |
| 1743 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1744 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1745 | } save_arg_t; |
| 1746 | |
| 1747 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1748 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1749 | sv->sv_argv0 = argv[0]; |
| 1750 | sv->sv_g_argv = G.global_argv; |
| 1751 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1752 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1753 | |
| 1754 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1755 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1756 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1757 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1758 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1762 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1763 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1764 | if (G.global_args_malloced) { |
| 1765 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1766 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1767 | while (*++pp) /* note: does not free $0 */ |
| 1768 | free(*pp); |
| 1769 | free(G.global_argv); |
| 1770 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1771 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1772 | argv[0] = sv->sv_argv0; |
| 1773 | G.global_argv = sv->sv_g_argv; |
| 1774 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1775 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1779 | /* Basic theory of signal handling in shell |
| 1780 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1781 | * This does not describe what hush does, rather, it is current understanding |
| 1782 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1783 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1784 | * |
| 1785 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1786 | * is finished or backgrounded. It is the same in interactive and |
| 1787 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1788 | * 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] | 1789 | * ^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] | 1790 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1791 | * pipe. |
| 1792 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1793 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1794 | * or by SIGINT in interactive shell. |
| 1795 | * |
| 1796 | * Trap handlers will execute even within trap handlers. (right?) |
| 1797 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1798 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1799 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1800 | * |
| 1801 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1802 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1803 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1804 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1805 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1806 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1807 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1808 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1809 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1810 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1811 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1812 | * |
| 1813 | * SIGQUIT: ignore |
| 1814 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1815 | * SIGHUP (interactive): |
| 1816 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1817 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1818 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1819 | * that all pipe members are stopped. Try this in bash: |
| 1820 | * while :; do :; done - ^Z does not background it |
| 1821 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1822 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1823 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1824 | * to interactive shell while shell is waiting for a pipe, |
| 1825 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1826 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1827 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1828 | * Example 2: this does not wait and does not execute ls: |
| 1829 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1830 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1831 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1832 | * Example 4: this does not wait and does not execute ls: |
| 1833 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1834 | * |
| 1835 | * (What happens to signals which are IGN on shell start?) |
| 1836 | * (What happens with signal mask on shell start?) |
| 1837 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1838 | * Old implementation |
| 1839 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1840 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1841 | * We block all signals which we don't want to take action immediately, |
| 1842 | * i.e. we block all signals which need to have special handling as described |
| 1843 | * above, and all signals which have traps set. |
| 1844 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1845 | * and act on them. |
| 1846 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1847 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1848 | * sigset_t blocked_set: current blocked signal set |
| 1849 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1850 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1851 | * 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] | 1852 | * "trap 'cmd' SIGxxx": |
| 1853 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1854 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1855 | * unblock signals with special interactive handling |
| 1856 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1857 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1858 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1859 | * 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] | 1860 | * 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] | 1861 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1862 | * 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] | 1863 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1864 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1865 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1866 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1867 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1868 | * 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] | 1869 | * 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] | 1870 | * |
| 1871 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1872 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1873 | * masked signals are not visible! |
| 1874 | * |
| 1875 | * New implementation |
| 1876 | * ================== |
| 1877 | * We record each signal we are interested in by installing signal handler |
| 1878 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1879 | * We are interested in: signals which need to have special handling |
| 1880 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1881 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1882 | * After each pipe execution, we extract any pending signals |
| 1883 | * and act on them. |
| 1884 | * |
| 1885 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1886 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1887 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1888 | * sigset_t pending_set: set of sigs we received. |
| 1889 | * |
| 1890 | * "trap - SIGxxx": |
| 1891 | * if sig is in special_sig_mask, set handler back to: |
| 1892 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1893 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1894 | * else: set handler back to SIG_DFL |
| 1895 | * "trap 'cmd' SIGxxx": |
| 1896 | * set handler to record_pending_signo. |
| 1897 | * "trap '' SIGxxx": |
| 1898 | * set handler to SIG_IGN. |
| 1899 | * after [v]fork, if we plan to be a shell: |
| 1900 | * set signals with special interactive handling to SIG_DFL |
| 1901 | * (because child shell is not interactive), |
| 1902 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1903 | * after [v]fork, if we plan to exec: |
| 1904 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1905 | * |
| 1906 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1907 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1908 | * |
| 1909 | * Note (compat): |
| 1910 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1911 | * are set to the default actions". bash interprets it so that traps which |
| 1912 | * 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] | 1913 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1914 | enum { |
| 1915 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1916 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1917 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1918 | | (1 << SIGHUP) |
| 1919 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1920 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1921 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1922 | | (1 << SIGTTIN) |
| 1923 | | (1 << SIGTTOU) |
| 1924 | | (1 << SIGTSTP) |
| 1925 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1926 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1927 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1928 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1929 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1930 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1931 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1932 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1933 | if (sig == SIGCHLD) { |
| 1934 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1935 | //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] | 1936 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1937 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1938 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1939 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1940 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1941 | { |
| 1942 | struct sigaction old_sa; |
| 1943 | |
| 1944 | /* We could use signal() to install handlers... almost: |
| 1945 | * except that we need to mask ALL signals while handlers run. |
| 1946 | * I saw signal nesting in strace, race window isn't small. |
| 1947 | * SA_RESTART is also needed, but in Linux, signal() |
| 1948 | * sets SA_RESTART too. |
| 1949 | */ |
| 1950 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1951 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1952 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1953 | G.sa.sa_handler = handler; |
| 1954 | sigaction(sig, &G.sa, &old_sa); |
| 1955 | return old_sa.sa_handler; |
| 1956 | } |
| 1957 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1958 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1959 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1960 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1961 | static void restore_ttypgrp_and__exit(void) |
| 1962 | { |
| 1963 | /* xfunc has failed! die die die */ |
| 1964 | /* no EXIT traps, this is an escape hatch! */ |
| 1965 | G.exiting = 1; |
| 1966 | hush_exit(xfunc_error_retval); |
| 1967 | } |
| 1968 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1969 | #if ENABLE_HUSH_JOB |
| 1970 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1971 | /* Needed only on some libc: |
| 1972 | * It was observed that on exit(), fgetc'ed buffered data |
| 1973 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1974 | * With the net effect that even after fork(), not vfork(), |
| 1975 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1976 | * noexec_applet_here |
| 1977 | * echo END_OF_SCRIPT |
| 1978 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1979 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1980 | * 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] | 1981 | * and in `cmd` handling. |
| 1982 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1983 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1984 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1985 | static void fflush_and__exit(void) |
| 1986 | { |
| 1987 | fflush_all(); |
| 1988 | _exit(xfunc_error_retval); |
| 1989 | } |
| 1990 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1991 | /* 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] | 1992 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1993 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1994 | # 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] | 1995 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1996 | /* Restores tty foreground process group, and exits. |
| 1997 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1998 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1999 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 2000 | * We also call it if xfunc is exiting. |
| 2001 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2002 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2003 | static void sigexit(int sig) |
| 2004 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2005 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 2006 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 2007 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 2008 | /* Disable all signals: job control, SIGPIPE, etc. |
| 2009 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 2010 | */ |
| 2011 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 2012 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 2013 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2014 | |
| 2015 | /* Not a signal, just exit */ |
| 2016 | if (sig <= 0) |
| 2017 | _exit(- sig); |
| 2018 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 2019 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2020 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2021 | #else |
| 2022 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2023 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 2024 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2025 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 2026 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2027 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2028 | static sighandler_t pick_sighandler(unsigned sig) |
| 2029 | { |
| 2030 | sighandler_t handler = SIG_DFL; |
| 2031 | if (sig < sizeof(unsigned)*8) { |
| 2032 | unsigned sigmask = (1 << sig); |
| 2033 | |
| 2034 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2035 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2036 | if (G_fatal_sig_mask & sigmask) |
| 2037 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2038 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2039 | #endif |
| 2040 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2041 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2042 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2043 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2044 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 2045 | * in an endless loop when we try to do some |
| 2046 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2047 | */ |
| 2048 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 2049 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2050 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2051 | } |
| 2052 | return handler; |
| 2053 | } |
| 2054 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2055 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2056 | static void hush_exit(int exitcode) |
| 2057 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 2058 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
Denys Vlasenko | 76a4e83 | 2019-05-19 18:24:52 +0200 | [diff] [blame] | 2059 | if (G.line_input_state) |
| 2060 | save_history(G.line_input_state); |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 2061 | #endif |
| 2062 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 2063 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2064 | 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] | 2065 | char *argv[3]; |
| 2066 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 2067 | 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] | 2068 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 2069 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2070 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 2071 | * "trap" will still show it, if executed |
| 2072 | * in the handler */ |
| 2073 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2074 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2075 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2076 | #if ENABLE_FEATURE_CLEAN_UP |
| 2077 | { |
| 2078 | struct variable *cur_var; |
| 2079 | if (G.cwd != bb_msg_unknown) |
| 2080 | free((char*)G.cwd); |
| 2081 | cur_var = G.top_var; |
| 2082 | while (cur_var) { |
| 2083 | struct variable *tmp = cur_var; |
| 2084 | if (!cur_var->max_len) |
| 2085 | free(cur_var->varstr); |
| 2086 | cur_var = cur_var->next; |
| 2087 | free(tmp); |
| 2088 | } |
| 2089 | } |
| 2090 | #endif |
| 2091 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2092 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2093 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2094 | sigexit(- (exitcode & 0xff)); |
| 2095 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2096 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2097 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2100 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2101 | //TODO: return a mask of ALL handled sigs? |
| 2102 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2103 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2104 | int last_sig = 0; |
| 2105 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2106 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2107 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2108 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2109 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2110 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2111 | sig = 0; |
| 2112 | do { |
| 2113 | sig++; |
| 2114 | if (sigismember(&G.pending_set, sig)) { |
| 2115 | sigdelset(&G.pending_set, sig); |
| 2116 | goto got_sig; |
| 2117 | } |
| 2118 | } while (sig < NSIG); |
| 2119 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2120 | got_sig: |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2121 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2122 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2123 | 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] | 2124 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2125 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2126 | smalluint save_rcode; |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2127 | int save_pre; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2128 | char *argv[3]; |
| 2129 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2130 | argv[1] = xstrdup(G_traps[sig]); |
| 2131 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2132 | argv[2] = NULL; |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2133 | save_pre = G.pre_trap_exitcode; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 2134 | G.pre_trap_exitcode = save_rcode = G.last_exitcode; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2135 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2136 | free(argv[1]); |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2137 | G.pre_trap_exitcode = save_pre; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2138 | G.last_exitcode = save_rcode; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2139 | # if ENABLE_HUSH_FUNCTIONS |
| 2140 | if (G.return_exitcode >= 0) { |
| 2141 | debug_printf_exec("trap exitcode:%d\n", G.return_exitcode); |
| 2142 | G.last_exitcode = G.return_exitcode; |
| 2143 | } |
| 2144 | # endif |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2145 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2146 | } /* else: "" trap, ignoring signal */ |
| 2147 | continue; |
| 2148 | } |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2149 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2150 | /* not a trap: special action */ |
| 2151 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2152 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2153 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2154 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2155 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | #if ENABLE_HUSH_JOB |
| 2158 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2159 | //TODO: why are we doing this? ash and dash don't do this, |
| 2160 | //they have no handler for SIGHUP at all, |
| 2161 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2162 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2163 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2164 | /* bash is observed to signal whole process groups, |
| 2165 | * not individual processes */ |
| 2166 | for (job = G.job_list; job; job = job->next) { |
| 2167 | if (job->pgrp <= 0) |
| 2168 | continue; |
| 2169 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2170 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2171 | kill(- job->pgrp, SIGCONT); |
| 2172 | } |
| 2173 | sigexit(SIGHUP); |
| 2174 | } |
| 2175 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2176 | #if ENABLE_HUSH_FAST |
| 2177 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2178 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2179 | G.count_SIGCHLD++; |
| 2180 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2181 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2182 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2183 | * This simplifies wait builtin a bit. |
| 2184 | */ |
| 2185 | break; |
| 2186 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2187 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2188 | 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] | 2189 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2190 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2191 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2192 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2193 | * in interactive shell, because TERM is ignored. |
| 2194 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | } |
| 2197 | } |
| 2198 | return last_sig; |
| 2199 | } |
| 2200 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2201 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2202 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2203 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2204 | if (force || G.cwd == NULL) { |
| 2205 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2206 | * we must not try to free(bb_msg_unknown) */ |
| 2207 | if (G.cwd == bb_msg_unknown) |
| 2208 | G.cwd = NULL; |
| 2209 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2210 | if (!G.cwd) |
| 2211 | G.cwd = bb_msg_unknown; |
| 2212 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2213 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2216 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2217 | /* |
| 2218 | * Shell and environment variable support |
| 2219 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2220 | 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] | 2221 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2222 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2223 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2224 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2225 | pp = &G.top_var; |
| 2226 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2227 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2228 | return pp; |
| 2229 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2230 | } |
| 2231 | return NULL; |
| 2232 | } |
| 2233 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2234 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2235 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2236 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2237 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2238 | |
| 2239 | if (G.expanded_assignments) { |
| 2240 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2241 | while (*cpp) { |
| 2242 | char *cp = *cpp; |
| 2243 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2244 | return cp + len + 1; |
| 2245 | cpp++; |
| 2246 | } |
| 2247 | } |
| 2248 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2249 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2250 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2251 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2252 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2253 | if (strcmp(name, "PPID") == 0) |
| 2254 | return utoa(G.root_ppid); |
| 2255 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2256 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2257 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2258 | return utoa(next_random(&G.random_gen)); |
| 2259 | #endif |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2260 | #if ENABLE_HUSH_LINENO_VAR |
| 2261 | if (strcmp(name, "LINENO") == 0) |
| 2262 | return utoa(G.execute_lineno); |
| 2263 | #endif |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2264 | #if BASH_EPOCH_VARS |
| 2265 | { |
| 2266 | const char *fmt = NULL; |
| 2267 | if (strcmp(name, "EPOCHSECONDS") == 0) |
| 2268 | fmt = "%lu"; |
| 2269 | else if (strcmp(name, "EPOCHREALTIME") == 0) |
| 2270 | fmt = "%lu.%06u"; |
| 2271 | if (fmt) { |
| 2272 | struct timeval tv; |
| 2273 | gettimeofday(&tv, NULL); |
| 2274 | sprintf(G.epoch_buf, fmt, (unsigned long)tv.tv_sec, |
| 2275 | (unsigned)tv.tv_usec); |
| 2276 | return G.epoch_buf; |
| 2277 | } |
| 2278 | } |
| 2279 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2280 | return NULL; |
| 2281 | } |
| 2282 | |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2283 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2284 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2285 | { |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2286 | if (name_len == 6) { |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2287 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2288 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2289 | return; |
| 2290 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2291 | } |
| 2292 | } |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2293 | #else |
| 2294 | /* Do not even bother evaluating arguments */ |
| 2295 | # define handle_changed_special_names(...) ((void)0) |
| 2296 | #endif |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2297 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2298 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2299 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2300 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2301 | #define SETFLAG_EXPORT (1 << 0) |
| 2302 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2303 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2304 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2305 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2306 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2307 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2308 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2309 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2310 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2311 | int name_len; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2312 | int retval; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2313 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2314 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2315 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2316 | if (HUSH_DEBUG && !eq_sign) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2317 | bb_simple_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2318 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2319 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2320 | cur_pp = &G.top_var; |
| 2321 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2322 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2323 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2324 | continue; |
| 2325 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2326 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2327 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2328 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2329 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2330 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2331 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2332 | //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] | 2333 | return -1; |
| 2334 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2335 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2336 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2337 | *eq_sign = '\0'; |
| 2338 | unsetenv(str); |
| 2339 | *eq_sign = '='; |
| 2340 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2341 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2342 | /* bash 3.2.33(1) and exported vars: |
| 2343 | * # export z=z |
| 2344 | * # f() { local z=a; env | grep ^z; } |
| 2345 | * # f |
| 2346 | * z=a |
| 2347 | * # env | grep ^z |
| 2348 | * z=z |
| 2349 | */ |
| 2350 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2351 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2352 | /* New variable is local ("local VAR=VAL" or |
| 2353 | * "VAR=VAL cmd") |
| 2354 | * and existing one is global, or local |
| 2355 | * on a lower level that new one. |
| 2356 | * Remove it from global variable list: |
| 2357 | */ |
| 2358 | *cur_pp = cur->next; |
| 2359 | if (G.shadowed_vars_pp) { |
| 2360 | /* Save in "shadowed" list */ |
| 2361 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2362 | cur->flg_export ? "exported " : "", |
| 2363 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2364 | ); |
| 2365 | cur->next = *G.shadowed_vars_pp; |
| 2366 | *G.shadowed_vars_pp = cur; |
| 2367 | } else { |
| 2368 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2369 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2370 | cur->flg_export ? "exported " : "", |
| 2371 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2372 | ); |
| 2373 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2374 | free_me = cur->varstr; /* then free it later */ |
| 2375 | free(cur); |
| 2376 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2377 | break; |
| 2378 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2379 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2380 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2381 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2382 | free_and_exp: |
| 2383 | free(str); |
| 2384 | goto exp; |
| 2385 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2386 | |
| 2387 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2388 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2389 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2390 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2391 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2392 | strcpy(cur->varstr, str); |
| 2393 | goto free_and_exp; |
| 2394 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2395 | /* Can't reuse */ |
| 2396 | cur->max_len = 0; |
| 2397 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2398 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2399 | /* max_len == 0 signifies "malloced" var, which we can |
| 2400 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2401 | * if cur->flg_export is 1, it is in the environment. |
| 2402 | * We should either unsetenv+free, or wait until putenv, |
| 2403 | * then putenv(new)+free(old). |
| 2404 | */ |
| 2405 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2406 | goto set_str_and_exp; |
| 2407 | } |
| 2408 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2409 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2410 | 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] | 2411 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2412 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2413 | cur->next = *cur_pp; |
| 2414 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2415 | |
| 2416 | set_str_and_exp: |
| 2417 | cur->varstr = str; |
| 2418 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2419 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2420 | if (flags & SETFLAG_MAKE_RO) { |
| 2421 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2422 | } |
| 2423 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2424 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2425 | cur->flg_export = 1; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2426 | retval = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2427 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2428 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2429 | cur->flg_export = 0; |
| 2430 | /* unsetenv was already done */ |
| 2431 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2432 | debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level); |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2433 | retval = putenv(cur->varstr); |
| 2434 | /* fall through to "free(free_me)" - |
| 2435 | * only now we can free old exported malloced string |
| 2436 | */ |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2437 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2438 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2439 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2440 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2441 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2442 | |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2443 | return retval; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 2446 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
| 2447 | { |
| 2448 | char *var = xasprintf("%s=%s", name, val); |
| 2449 | set_local_var(var, /*flag:*/ 0); |
| 2450 | } |
| 2451 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2452 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2453 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2454 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2455 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2456 | } |
| 2457 | |
Denys Vlasenko | 35a017c | 2018-06-26 18:27:54 +0200 | [diff] [blame] | 2458 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2459 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2460 | { |
| 2461 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2462 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2463 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2464 | cur_pp = &G.top_var; |
| 2465 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2466 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2467 | && cur->varstr[name_len] == '=' |
| 2468 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2469 | if (cur->flg_read_only) { |
| 2470 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2471 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2472 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2473 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2474 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2475 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2476 | bb_unsetenv(cur->varstr); |
| 2477 | if (!cur->max_len) |
| 2478 | free(cur->varstr); |
| 2479 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2480 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2481 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2482 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2483 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2484 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2485 | |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2486 | /* Handle "unset LINENO" et al even if did not find the variable to unset */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2487 | handle_changed_special_names(name, name_len); |
| 2488 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2489 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2492 | static int unset_local_var(const char *name) |
| 2493 | { |
| 2494 | return unset_local_var_len(name, strlen(name)); |
| 2495 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2496 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2497 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2498 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2499 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2500 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2501 | */ |
| 2502 | static void add_vars(struct variable *var) |
| 2503 | { |
| 2504 | struct variable *next; |
| 2505 | |
| 2506 | while (var) { |
| 2507 | next = var->next; |
| 2508 | var->next = G.top_var; |
| 2509 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2510 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2511 | 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] | 2512 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2513 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2514 | 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] | 2515 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2516 | var = next; |
| 2517 | } |
| 2518 | } |
| 2519 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2520 | /* We put strings[i] into variable table and possibly putenv them. |
| 2521 | * If variable is read only, we can free the strings[i] |
| 2522 | * which attempts to overwrite it. |
| 2523 | * The strings[] vector itself is freed. |
| 2524 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2525 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2526 | { |
| 2527 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2528 | |
| 2529 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2530 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2531 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2532 | s = strings; |
| 2533 | while (*s) { |
| 2534 | struct variable *var_p; |
| 2535 | struct variable **var_pp; |
| 2536 | char *eq; |
| 2537 | |
| 2538 | eq = strchr(*s, '='); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2539 | if (HUSH_DEBUG && !eq) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2540 | bb_simple_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2541 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
| 2542 | if (var_pp) { |
| 2543 | var_p = *var_pp; |
| 2544 | if (var_p->flg_read_only) { |
| 2545 | char **p; |
| 2546 | bb_error_msg("%s: readonly variable", *s); |
| 2547 | /* |
| 2548 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2549 | * If VAR is readonly, leaving it in the list |
| 2550 | * after asssignment error (msg above) |
| 2551 | * causes doubled error message later, on unset. |
| 2552 | */ |
| 2553 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2554 | free(*s); |
| 2555 | p = s; |
| 2556 | do { *p = p[1]; p++; } while (*p); |
| 2557 | goto next; |
| 2558 | } |
| 2559 | /* below, set_local_var() with nest level will |
| 2560 | * "shadow" (remove) this variable from |
| 2561 | * global linked list. |
| 2562 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2563 | } |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2564 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
| 2565 | 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] | 2566 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2567 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2568 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2569 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2574 | * Unicode helper |
| 2575 | */ |
| 2576 | static void reinit_unicode_for_hush(void) |
| 2577 | { |
| 2578 | /* Unicode support should be activated even if LANG is set |
| 2579 | * _during_ shell execution, not only if it was set when |
| 2580 | * shell was started. Therefore, re-check LANG every time: |
| 2581 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2582 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2583 | || ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 4c201c0 | 2018-07-17 15:04:17 +0200 | [diff] [blame] | 2584 | ) { |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2585 | const char *s = get_local_var_value("LC_ALL"); |
| 2586 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2587 | if (!s) s = get_local_var_value("LANG"); |
| 2588 | reinit_unicode(s); |
| 2589 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2590 | } |
| 2591 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2592 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2593 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2594 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2595 | |
| 2596 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2597 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2598 | * echo $P\ |
| 2599 | * \ |
| 2600 | * AT\ |
| 2601 | * H\ |
| 2602 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2603 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2604 | */ |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2605 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2606 | { |
| 2607 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2608 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2609 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2610 | |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2611 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2612 | prompt_str = get_local_var_value(G.promptmode == 0 ? "PS1" : "PS2"); |
| 2613 | if (!prompt_str) |
| 2614 | prompt_str = ""; |
| 2615 | # else |
| 2616 | prompt_str = "> "; /* if PS2, else... */ |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2617 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2618 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
| 2619 | free(G.PS1); |
| 2620 | /* bash uses $PWD value, even if it is set by user. |
| 2621 | * It uses current dir only if PWD is unset. |
| 2622 | * We always use current dir. */ |
| 2623 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2624 | } |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2625 | # endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2626 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2627 | return prompt_str; |
| 2628 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2629 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2630 | { |
| 2631 | int r; |
| 2632 | const char *prompt_str; |
| 2633 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2634 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2635 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2636 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2637 | reinit_unicode_for_hush(); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2638 | G.flag_SIGINT = 0; |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2639 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2640 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2641 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2642 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2643 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2644 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2645 | if (r == 0) { |
| 2646 | G.flag_ctrlC = 1; |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2647 | raise(SIGINT); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2648 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2649 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2650 | if (r != 0 && !G.flag_SIGINT) |
| 2651 | break; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2652 | /* ^C or SIGINT: return EOF */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2653 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2654 | write(STDOUT_FILENO, "^C\n", 3); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2655 | G.last_exitcode = 128 + SIGINT; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2656 | i->p = NULL; |
| 2657 | i->peek_buf[0] = r = EOF; |
| 2658 | return r; |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2659 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2660 | if (r < 0) { |
| 2661 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2662 | i->p = NULL; |
| 2663 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2664 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2665 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2666 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2667 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2668 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2669 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2670 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2671 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2672 | /* Why check_and_run_traps here? Try this interactively: |
| 2673 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2674 | * $ <[enter], repeatedly...> |
| 2675 | * Without check_and_run_traps, handler never runs. |
| 2676 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2677 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2678 | fputs(prompt_str, stdout); |
| 2679 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2680 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2681 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2682 | r = hfgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2683 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2684 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2685 | * it generates SIGINT as usual. |
| 2686 | */ |
| 2687 | check_and_run_traps(); |
| 2688 | if (G.flag_SIGINT) |
| 2689 | G.last_exitcode = 128 + SIGINT; |
| 2690 | if (r != '\0') |
| 2691 | break; |
| 2692 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2693 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2694 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2695 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2696 | /* This is the magic location that prints prompts |
| 2697 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2698 | static int fgetc_interactive(struct in_str *i) |
| 2699 | { |
| 2700 | int ch; |
| 2701 | /* If it's interactive stdin, get new line. */ |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 2702 | if (G_interactive_fd && i->file == G.HFILE_stdin) { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2703 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2704 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2705 | G.promptmode = 1; /* PS2 */ |
| 2706 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2707 | } else { |
| 2708 | /* Not stdin: script file, sourced file, etc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2709 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2710 | } |
| 2711 | return ch; |
| 2712 | } |
| 2713 | #else |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2714 | static ALWAYS_INLINE int fgetc_interactive(struct in_str *i) |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2715 | { |
| 2716 | int ch; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2717 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2718 | return ch; |
| 2719 | } |
| 2720 | #endif /* INTERACTIVE */ |
| 2721 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2722 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2723 | { |
| 2724 | int ch; |
| 2725 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2726 | if (!i->file) { |
| 2727 | /* string-based in_str */ |
| 2728 | ch = (unsigned char)*i->p; |
| 2729 | if (ch != '\0') { |
| 2730 | i->p++; |
| 2731 | i->last_char = ch; |
| 2732 | return ch; |
| 2733 | } |
| 2734 | return EOF; |
| 2735 | } |
| 2736 | |
| 2737 | /* FILE-based in_str */ |
| 2738 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2739 | #if ENABLE_FEATURE_EDITING |
| 2740 | /* This can be stdin, check line editing char[] buffer */ |
| 2741 | if (i->p && *i->p != '\0') { |
| 2742 | ch = (unsigned char)*i->p++; |
| 2743 | goto out; |
| 2744 | } |
| 2745 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2746 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2747 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2748 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2749 | int ch2 = i->peek_buf[1]; |
| 2750 | i->peek_buf[0] = ch2; |
| 2751 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2752 | goto out; |
| 2753 | i->peek_buf[1] = 0; |
| 2754 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2755 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2756 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2757 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2758 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2759 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2760 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2761 | #if ENABLE_HUSH_LINENO_VAR |
| 2762 | if (ch == '\n') { |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2763 | G.parse_lineno++; |
| 2764 | debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2765 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2766 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2767 | return ch; |
| 2768 | } |
| 2769 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2770 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2771 | { |
| 2772 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2773 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2774 | if (!i->file) { |
| 2775 | /* string-based in_str */ |
| 2776 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2777 | return (unsigned char)*i->p; |
| 2778 | } |
| 2779 | |
| 2780 | /* FILE-based in_str */ |
| 2781 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2782 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2783 | /* This can be stdin, check line editing char[] buffer */ |
| 2784 | if (i->p && *i->p != '\0') |
| 2785 | return (unsigned char)*i->p; |
| 2786 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2787 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2788 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2789 | if (ch != 0) |
| 2790 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2791 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2792 | /* Need to get a new char */ |
| 2793 | ch = fgetc_interactive(i); |
| 2794 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2795 | |
| 2796 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2797 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2798 | if (i->p) { |
| 2799 | i->p -= 1; |
| 2800 | return ch; |
| 2801 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2802 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2803 | i->peek_buf[0] = ch; |
| 2804 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2805 | return ch; |
| 2806 | } |
| 2807 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2808 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2809 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2810 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2811 | */ |
| 2812 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2813 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2814 | int ch; |
| 2815 | |
| 2816 | /* There are two cases when i->p[] buffer exists. |
| 2817 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2818 | * (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] | 2819 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2820 | * the peek2 result is in i->p[1]. |
| 2821 | */ |
| 2822 | if (i->p) |
| 2823 | return (unsigned char)i->p[1]; |
| 2824 | |
| 2825 | /* Now we know it is a file-based in_str. */ |
| 2826 | |
| 2827 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2828 | /* Is there 2nd char? */ |
| 2829 | ch = i->peek_buf[1]; |
| 2830 | if (ch == 0) { |
| 2831 | /* We did not read it yet, get it now */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2832 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2833 | i->peek_buf[1] = ch; |
| 2834 | } |
| 2835 | |
| 2836 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2837 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2838 | } |
| 2839 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2840 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2841 | { |
| 2842 | for (;;) { |
| 2843 | int ch, ch2; |
| 2844 | |
| 2845 | ch = i_getch(input); |
| 2846 | if (ch != '\\') |
| 2847 | return ch; |
| 2848 | ch2 = i_peek(input); |
| 2849 | if (ch2 != '\n') |
| 2850 | return ch; |
| 2851 | /* backslash+newline, skip it */ |
| 2852 | i_getch(input); |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2857 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2858 | */ |
| 2859 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2860 | { |
| 2861 | for (;;) { |
| 2862 | int ch, ch2; |
| 2863 | |
| 2864 | ch = i_peek(input); |
| 2865 | if (ch != '\\') |
| 2866 | return ch; |
| 2867 | ch2 = i_peek2(input); |
| 2868 | if (ch2 != '\n') |
| 2869 | return ch; |
| 2870 | /* backslash+newline, skip it */ |
| 2871 | i_getch(input); |
| 2872 | i_getch(input); |
| 2873 | } |
| 2874 | } |
| 2875 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2876 | static void setup_file_in_str(struct in_str *i, HFILE *fp) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2877 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2878 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2879 | i->file = fp; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2880 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2884 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2885 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2886 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2887 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2891 | /* |
| 2892 | * o_string support |
| 2893 | */ |
| 2894 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2895 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2896 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2897 | { |
| 2898 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2899 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2900 | if (o->data) |
| 2901 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2904 | static void o_free_and_set_NULL(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2905 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2906 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2907 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2910 | static ALWAYS_INLINE void o_free(o_string *o) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2911 | { |
| 2912 | free(o->data); |
| 2913 | } |
| 2914 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2915 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2916 | { |
| 2917 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2918 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2919 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2920 | } |
| 2921 | } |
| 2922 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2923 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2924 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2925 | 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] | 2926 | if (o->length < o->maxlen) { |
| 2927 | /* likely. avoid o_grow_by() call */ |
| 2928 | add: |
| 2929 | o->data[o->length] = ch; |
| 2930 | o->length++; |
| 2931 | o->data[o->length] = '\0'; |
| 2932 | return; |
| 2933 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2934 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2935 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2938 | #if 0 |
| 2939 | /* Valid only if we know o_string is not empty */ |
| 2940 | static void o_delchr(o_string *o) |
| 2941 | { |
| 2942 | o->length--; |
| 2943 | o->data[o->length] = '\0'; |
| 2944 | } |
| 2945 | #endif |
| 2946 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2947 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2948 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2949 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2950 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2951 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2954 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2955 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2956 | o_addblock(o, str, strlen(str)); |
| 2957 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2958 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2959 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2960 | { |
| 2961 | o_addblock(o, str, strlen(str) + 1); |
| 2962 | } |
| 2963 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2964 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2965 | static void nommu_addchr(o_string *o, int ch) |
| 2966 | { |
| 2967 | if (o) |
| 2968 | o_addchr(o, ch); |
| 2969 | } |
| 2970 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2971 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2972 | #endif |
| 2973 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2974 | #if ENABLE_HUSH_MODE_X |
| 2975 | static void x_mode_addchr(int ch) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2976 | { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2977 | o_addchr(&G.x_mode_buf, ch); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2978 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 2979 | static void x_mode_addstr(const char *str) |
| 2980 | { |
| 2981 | o_addstr(&G.x_mode_buf, str); |
| 2982 | } |
| 2983 | static void x_mode_addblock(const char *str, int len) |
| 2984 | { |
| 2985 | o_addblock(&G.x_mode_buf, str, len); |
| 2986 | } |
| 2987 | static void x_mode_prefix(void) |
| 2988 | { |
| 2989 | int n = G.x_mode_depth; |
| 2990 | do x_mode_addchr('+'); while (--n >= 0); |
| 2991 | } |
| 2992 | static void x_mode_flush(void) |
| 2993 | { |
| 2994 | int len = G.x_mode_buf.length; |
| 2995 | if (len <= 0) |
| 2996 | return; |
| 2997 | if (G.x_mode_fd > 0) { |
| 2998 | G.x_mode_buf.data[len] = '\n'; |
| 2999 | full_write(G.x_mode_fd, G.x_mode_buf.data, len + 1); |
| 3000 | } |
| 3001 | G.x_mode_buf.length = 0; |
| 3002 | } |
| 3003 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3004 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3005 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3006 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3007 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 3008 | * Apparently, on unquoted $v bash still does globbing |
| 3009 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 3010 | * but NOT brace expansion! Thus, there should be TWO independent |
| 3011 | * quoting mechanisms on $v expansion side: one protects |
| 3012 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 3013 | * We have only second one. |
| 3014 | */ |
| 3015 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3016 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3017 | # define MAYBE_BRACES "{}" |
| 3018 | #else |
| 3019 | # define MAYBE_BRACES "" |
| 3020 | #endif |
| 3021 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3022 | /* My analysis of quoting semantics tells me that state information |
| 3023 | * is associated with a destination, not a source. |
| 3024 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3025 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3026 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3027 | int sz = 1; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3028 | /* '-' is included because of this case: |
| 3029 | * >filename0 >filename1 >filename9; v='-'; echo filename[0"$v"9] |
| 3030 | */ |
| 3031 | char *found = strchr("*?[-\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3032 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3033 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3034 | o_grow_by(o, sz); |
| 3035 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3036 | o->data[o->length] = '\\'; |
| 3037 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3038 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3039 | o->data[o->length] = ch; |
| 3040 | o->length++; |
| 3041 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3044 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3045 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3046 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3047 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3048 | && strchr("*?[-\\" MAYBE_BRACES, ch) |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3049 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3050 | sz++; |
| 3051 | o->data[o->length] = '\\'; |
| 3052 | o->length++; |
| 3053 | } |
| 3054 | o_grow_by(o, sz); |
| 3055 | o->data[o->length] = ch; |
| 3056 | o->length++; |
| 3057 | o->data[o->length] = '\0'; |
| 3058 | } |
| 3059 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3060 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3061 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3062 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3063 | char ch; |
| 3064 | int sz; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3065 | int ordinary_cnt = strcspn(str, "*?[-\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3066 | if (ordinary_cnt > len) /* paranoia */ |
| 3067 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3068 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3069 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3070 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3071 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3072 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3073 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3074 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3075 | sz = 1; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3076 | if (ch) { /* it is necessarily one of "*?[-\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3077 | sz++; |
| 3078 | o->data[o->length] = '\\'; |
| 3079 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3080 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3081 | o_grow_by(o, sz); |
| 3082 | o->data[o->length] = ch; |
| 3083 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3084 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3085 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3088 | static void o_addQblock(o_string *o, const char *str, int len) |
| 3089 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3090 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3091 | o_addblock(o, str, len); |
| 3092 | return; |
| 3093 | } |
| 3094 | o_addqblock(o, str, len); |
| 3095 | } |
| 3096 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3097 | static void o_addQstr(o_string *o, const char *str) |
| 3098 | { |
| 3099 | o_addQblock(o, str, strlen(str)); |
| 3100 | } |
| 3101 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3102 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 3103 | * 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] | 3104 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3105 | * list[i] contains an INDEX (int!) into this string data. |
| 3106 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 3107 | * but list[i]'s need not be modified. |
| 3108 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3109 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3110 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 3111 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3112 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 3113 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 3114 | { |
| 3115 | char **list = (char**)o->data; |
| 3116 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3117 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3118 | |
| 3119 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3120 | 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] | 3121 | prefix, list, n, string_start, o->length, o->maxlen, |
| 3122 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 3123 | o->has_quoted_part, |
| 3124 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3125 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3126 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3127 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 3128 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 3129 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3130 | i++; |
| 3131 | } |
| 3132 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3133 | 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] | 3134 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3135 | 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] | 3136 | } |
| 3137 | } |
| 3138 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3139 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3140 | #endif |
| 3141 | |
| 3142 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3143 | * in list[n] so that it points past last stored byte so far. |
| 3144 | * It returns n+1. */ |
| 3145 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3146 | { |
| 3147 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3148 | int string_start; |
| 3149 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3150 | |
| 3151 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3152 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3153 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3154 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3155 | 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] | 3156 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3157 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3158 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3159 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3160 | memmove(list + n + 0x10, list + n, string_len); |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 3161 | /* |
| 3162 | * expand_on_ifs() has a "previous argv[] ends in IFS?" |
| 3163 | * check. (grep for -prev-ifs-check-). |
| 3164 | * Ensure that argv[-1][last] is not garbage |
| 3165 | * but zero bytes, to save index check there. |
| 3166 | */ |
| 3167 | list[n + 0x10 - 1] = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3168 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3169 | } else { |
| 3170 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3171 | n, string_len, string_start); |
| 3172 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3173 | } else { |
| 3174 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3175 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3176 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3177 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3178 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3179 | o->has_empty_slot = 0; |
| 3180 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3181 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3182 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3183 | return n + 1; |
| 3184 | } |
| 3185 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3186 | /* "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] | 3187 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3188 | { |
| 3189 | char **list = (char**)o->data; |
| 3190 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3191 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3192 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3195 | /* |
| 3196 | * Globbing routines. |
| 3197 | * |
| 3198 | * Most words in commands need to be globbed, even ones which are |
| 3199 | * (single or double) quoted. This stems from the possiblity of |
| 3200 | * constructs like "abc"* and 'abc'* - these should be globbed. |
| 3201 | * Having a different code path for fully-quoted strings ("abc", |
| 3202 | * 'abc') would only help performance-wise, but we still need |
| 3203 | * code for partially-quoted strings. |
| 3204 | * |
| 3205 | * Unfortunately, if we want to match bash and ash behavior in all cases, |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3206 | * the logic can't be "shell-syntax argument is first transformed |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3207 | * to a string, then globbed, and if globbing does not match anything, |
| 3208 | * it is used verbatim". Here are two examples where it fails: |
| 3209 | * |
| 3210 | * echo 'b\*'? |
| 3211 | * |
| 3212 | * The globbing can't be avoided (because of '?' at the end). |
| 3213 | * The glob pattern is: b\\\*? - IOW, both \ and * are literals |
| 3214 | * and are glob-escaped. If this does not match, bash/ash print b\*? |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3215 | * - IOW: they "unbackslash" the glob pattern. |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3216 | * Now, look at this: |
| 3217 | * |
| 3218 | * v='\\\*'; echo b$v? |
| 3219 | * |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3220 | * The glob pattern is the same here: b\\\*? - the unquoted $v expansion |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3221 | * should be used as glob pattern with no changes. However, if glob |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3222 | * does not match, bash/ash print b\\\*? - NOT THE SAME as first example! |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3223 | * |
| 3224 | * ash implements this by having an encoded representation of the word |
| 3225 | * to glob, which IS NOT THE SAME as the glob pattern - it has more data. |
| 3226 | * Glob pattern is derived from it. If glob fails, the decision what result |
| 3227 | * should be is made using that encoded representation. Not glob pattern. |
| 3228 | */ |
| 3229 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3230 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3231 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3232 | * first, it processes even {a} (no commas), second, |
| 3233 | * 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] | 3234 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3235 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3236 | |
| 3237 | /* Helper */ |
| 3238 | static int glob_needed(const char *s) |
| 3239 | { |
| 3240 | while (*s) { |
| 3241 | if (*s == '\\') { |
| 3242 | if (!s[1]) |
| 3243 | return 0; |
| 3244 | s += 2; |
| 3245 | continue; |
| 3246 | } |
| 3247 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3248 | return 1; |
| 3249 | s++; |
| 3250 | } |
| 3251 | return 0; |
| 3252 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3253 | /* Return pointer to next closing brace or to comma */ |
| 3254 | static const char *next_brace_sub(const char *cp) |
| 3255 | { |
| 3256 | unsigned depth = 0; |
| 3257 | cp++; |
| 3258 | while (*cp != '\0') { |
| 3259 | if (*cp == '\\') { |
| 3260 | if (*++cp == '\0') |
| 3261 | break; |
| 3262 | cp++; |
| 3263 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3264 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3265 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3266 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3267 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3268 | depth++; |
| 3269 | } |
| 3270 | |
| 3271 | return *cp != '\0' ? cp : NULL; |
| 3272 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3273 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3274 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3275 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3276 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3277 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3278 | const char *next; |
| 3279 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3280 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3281 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3282 | |
| 3283 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3284 | |
| 3285 | begin = pattern; |
| 3286 | while (1) { |
| 3287 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3288 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3289 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3290 | /* Find the first sub-pattern and at the same time |
| 3291 | * find the rest after the closing brace */ |
| 3292 | next = next_brace_sub(begin); |
| 3293 | if (next == NULL) { |
| 3294 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3295 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3296 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3297 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3298 | /* "{abc}" with no commas - illegal |
| 3299 | * brace expr, disregard and skip it */ |
| 3300 | begin = next + 1; |
| 3301 | continue; |
| 3302 | } |
| 3303 | break; |
| 3304 | } |
| 3305 | if (*begin == '\\' && begin[1] != '\0') |
| 3306 | begin++; |
| 3307 | begin++; |
| 3308 | } |
| 3309 | debug_printf_glob("begin:%s\n", begin); |
| 3310 | debug_printf_glob("next:%s\n", next); |
| 3311 | |
| 3312 | /* Now find the end of the whole brace expression */ |
| 3313 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3314 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3315 | rest = next_brace_sub(rest); |
| 3316 | if (rest == NULL) { |
| 3317 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3318 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3319 | } |
| 3320 | debug_printf_glob("rest:%s\n", rest); |
| 3321 | } |
| 3322 | rest_len = strlen(++rest) + 1; |
| 3323 | |
| 3324 | /* We are sure the brace expression is well-formed */ |
| 3325 | |
| 3326 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3327 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3328 | |
| 3329 | /* We have a brace expression. BEGIN points to the opening {, |
| 3330 | * NEXT points past the terminator of the first element, and REST |
| 3331 | * points past the final }. We will accumulate result names from |
| 3332 | * recursive runs for each brace alternative in the buffer using |
| 3333 | * GLOB_APPEND. */ |
| 3334 | |
| 3335 | p = begin + 1; |
| 3336 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3337 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3338 | memcpy( |
| 3339 | mempcpy( |
| 3340 | mempcpy(new_pattern_buf, |
| 3341 | /* We know the prefix for all sub-patterns */ |
| 3342 | pattern, begin - pattern), |
| 3343 | p, next - p), |
| 3344 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3345 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3346 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3347 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3348 | */ |
| 3349 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3350 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3351 | /* We saw the last entry */ |
| 3352 | break; |
| 3353 | } |
| 3354 | p = next + 1; |
| 3355 | next = next_brace_sub(next); |
| 3356 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3357 | free(new_pattern_buf); |
| 3358 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3359 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3360 | simple_glob: |
| 3361 | { |
| 3362 | int gr; |
| 3363 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3364 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3365 | memset(&globdata, 0, sizeof(globdata)); |
| 3366 | gr = glob(pattern, 0, NULL, &globdata); |
| 3367 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3368 | if (gr != 0) { |
| 3369 | if (gr == GLOB_NOMATCH) { |
| 3370 | globfree(&globdata); |
| 3371 | /* NB: garbles parameter */ |
| 3372 | unbackslash(pattern); |
| 3373 | o_addstr_with_NUL(o, pattern); |
| 3374 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3375 | return o_save_ptr_helper(o, n); |
| 3376 | } |
| 3377 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3378 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3379 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3380 | * but we didn't specify it. Paranoia again. */ |
| 3381 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3382 | } |
| 3383 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3384 | char **argv = globdata.gl_pathv; |
| 3385 | while (1) { |
| 3386 | o_addstr_with_NUL(o, *argv); |
| 3387 | n = o_save_ptr_helper(o, n); |
| 3388 | argv++; |
| 3389 | if (!*argv) |
| 3390 | break; |
| 3391 | } |
| 3392 | } |
| 3393 | globfree(&globdata); |
| 3394 | } |
| 3395 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3396 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3397 | /* Performs globbing on last list[], |
| 3398 | * saving each result as a new list[]. |
| 3399 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3400 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3401 | { |
| 3402 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3403 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3404 | 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] | 3405 | if (!o->data) |
| 3406 | return o_save_ptr_helper(o, n); |
| 3407 | pattern = o->data + o_get_last_ptr(o, n); |
| 3408 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3409 | if (!glob_needed(pattern)) { |
| 3410 | /* unbackslash last string in o in place, fix length */ |
| 3411 | o->length = unbackslash(pattern) - o->data; |
| 3412 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3413 | return o_save_ptr_helper(o, n); |
| 3414 | } |
| 3415 | |
| 3416 | copy = xstrdup(pattern); |
| 3417 | /* "forget" pattern in o */ |
| 3418 | o->length = pattern - o->data; |
| 3419 | n = glob_brace(copy, o, n); |
| 3420 | free(copy); |
| 3421 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3422 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3423 | return n; |
| 3424 | } |
| 3425 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3426 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3427 | |
| 3428 | /* Helper */ |
| 3429 | static int glob_needed(const char *s) |
| 3430 | { |
| 3431 | while (*s) { |
| 3432 | if (*s == '\\') { |
| 3433 | if (!s[1]) |
| 3434 | return 0; |
| 3435 | s += 2; |
| 3436 | continue; |
| 3437 | } |
| 3438 | if (*s == '*' || *s == '[' || *s == '?') |
| 3439 | return 1; |
| 3440 | s++; |
| 3441 | } |
| 3442 | return 0; |
| 3443 | } |
| 3444 | /* Performs globbing on last list[], |
| 3445 | * saving each result as a new list[]. |
| 3446 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3447 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3448 | { |
| 3449 | glob_t globdata; |
| 3450 | int gr; |
| 3451 | char *pattern; |
| 3452 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3453 | 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] | 3454 | if (!o->data) |
| 3455 | return o_save_ptr_helper(o, n); |
| 3456 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3457 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3458 | if (!glob_needed(pattern)) { |
| 3459 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3460 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3461 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3462 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3463 | return o_save_ptr_helper(o, n); |
| 3464 | } |
| 3465 | |
| 3466 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3467 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3468 | * If we glob "*.\*" and don't find anything, we need |
| 3469 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3470 | * will return "*.\*"! |
| 3471 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3472 | gr = glob(pattern, 0, NULL, &globdata); |
| 3473 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3474 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3475 | if (gr == GLOB_NOMATCH) { |
| 3476 | globfree(&globdata); |
| 3477 | goto literal; |
| 3478 | } |
| 3479 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3480 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3481 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3482 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3483 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3484 | } |
| 3485 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3486 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3487 | /* "forget" pattern in o */ |
| 3488 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3489 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3490 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3491 | n = o_save_ptr_helper(o, n); |
| 3492 | argv++; |
| 3493 | if (!*argv) |
| 3494 | break; |
| 3495 | } |
| 3496 | } |
| 3497 | globfree(&globdata); |
| 3498 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3499 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3500 | return n; |
| 3501 | } |
| 3502 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3503 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3504 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3505 | /* 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] | 3506 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3507 | static int o_save_ptr(o_string *o, int n) |
| 3508 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3509 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3510 | /* If o->has_empty_slot, list[n] was already globbed |
| 3511 | * (if it was requested back then when it was filled) |
| 3512 | * so don't do that again! */ |
| 3513 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3514 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3515 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3516 | return o_save_ptr_helper(o, n); |
| 3517 | } |
| 3518 | |
| 3519 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3520 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3521 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3522 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3523 | int string_start; |
| 3524 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3525 | if (DEBUG_EXPAND) |
| 3526 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3527 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3528 | list = (char**)o->data; |
| 3529 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3530 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3531 | while (n) { |
| 3532 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3533 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3534 | } |
| 3535 | return list; |
| 3536 | } |
| 3537 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3538 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3539 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3540 | /* Returns pi->next - next pipe in the list */ |
| 3541 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3542 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3543 | struct pipe *next; |
| 3544 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3545 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3546 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3547 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3548 | struct command *command; |
| 3549 | struct redir_struct *r, *rnext; |
| 3550 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3551 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3552 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3553 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3554 | if (DEBUG_CLEAN) { |
| 3555 | int a; |
| 3556 | char **p; |
| 3557 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3558 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3559 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3560 | } |
| 3561 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3562 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3563 | } |
| 3564 | /* not "else if": on syntax error, we may have both! */ |
| 3565 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3566 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3567 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3568 | free_pipe_list(command->group); |
| 3569 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3570 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3571 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3572 | /* else is crucial here. |
| 3573 | * If group != NULL, child_func is meaningless */ |
| 3574 | #if ENABLE_HUSH_FUNCTIONS |
| 3575 | else if (command->child_func) { |
| 3576 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3577 | command->child_func->parent_cmd = NULL; |
| 3578 | } |
| 3579 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3580 | #if !BB_MMU |
| 3581 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3582 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3583 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3584 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3585 | debug_printf_clean(" redirect %d%s", |
| 3586 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3587 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3588 | if (r->rd_filename) { |
| 3589 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3590 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3591 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3592 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3593 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3594 | rnext = r->next; |
| 3595 | free(r); |
| 3596 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3597 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3598 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3599 | 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] | 3600 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3601 | #if ENABLE_HUSH_JOB |
| 3602 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3603 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3604 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3605 | |
| 3606 | next = pi->next; |
| 3607 | free(pi); |
| 3608 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3609 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3610 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3611 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3612 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3613 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3614 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3615 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3616 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3617 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3618 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3619 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3623 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3624 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3625 | #ifndef debug_print_tree |
| 3626 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3627 | { |
| 3628 | static const char *const PIPE[] = { |
| 3629 | [PIPE_SEQ] = "SEQ", |
| 3630 | [PIPE_AND] = "AND", |
| 3631 | [PIPE_OR ] = "OR" , |
| 3632 | [PIPE_BG ] = "BG" , |
| 3633 | }; |
| 3634 | static const char *RES[] = { |
| 3635 | [RES_NONE ] = "NONE" , |
| 3636 | # if ENABLE_HUSH_IF |
| 3637 | [RES_IF ] = "IF" , |
| 3638 | [RES_THEN ] = "THEN" , |
| 3639 | [RES_ELIF ] = "ELIF" , |
| 3640 | [RES_ELSE ] = "ELSE" , |
| 3641 | [RES_FI ] = "FI" , |
| 3642 | # endif |
| 3643 | # if ENABLE_HUSH_LOOPS |
| 3644 | [RES_FOR ] = "FOR" , |
| 3645 | [RES_WHILE] = "WHILE", |
| 3646 | [RES_UNTIL] = "UNTIL", |
| 3647 | [RES_DO ] = "DO" , |
| 3648 | [RES_DONE ] = "DONE" , |
| 3649 | # endif |
| 3650 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3651 | [RES_IN ] = "IN" , |
| 3652 | # endif |
| 3653 | # if ENABLE_HUSH_CASE |
| 3654 | [RES_CASE ] = "CASE" , |
| 3655 | [RES_CASE_IN ] = "CASE_IN" , |
| 3656 | [RES_MATCH] = "MATCH", |
| 3657 | [RES_CASE_BODY] = "CASE_BODY", |
| 3658 | [RES_ESAC ] = "ESAC" , |
| 3659 | # endif |
| 3660 | [RES_XXXX ] = "XXXX" , |
| 3661 | [RES_SNTX ] = "SNTX" , |
| 3662 | }; |
| 3663 | static const char *const CMDTYPE[] = { |
| 3664 | "{}", |
| 3665 | "()", |
| 3666 | "[noglob]", |
| 3667 | # if ENABLE_HUSH_FUNCTIONS |
| 3668 | "func()", |
| 3669 | # endif |
| 3670 | }; |
| 3671 | |
| 3672 | int pin, prn; |
| 3673 | |
| 3674 | pin = 0; |
| 3675 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3676 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3677 | lvl*2, "", |
| 3678 | pin, |
| 3679 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3680 | RES[pi->res_word], |
| 3681 | pi->followup, PIPE[pi->followup] |
| 3682 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3683 | prn = 0; |
| 3684 | while (prn < pi->num_cmds) { |
| 3685 | struct command *command = &pi->cmds[prn]; |
| 3686 | char **argv = command->argv; |
| 3687 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3688 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3689 | lvl*2, "", prn, |
| 3690 | command->assignment_cnt); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 3691 | # if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3692 | fdprintf(2, " LINENO:%u", command->lineno); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 3693 | # endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3694 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3695 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3696 | CMDTYPE[command->cmd_type], |
| 3697 | argv |
| 3698 | # if !BB_MMU |
| 3699 | , " group_as_string:", command->group_as_string |
| 3700 | # else |
| 3701 | , "", "" |
| 3702 | # endif |
| 3703 | ); |
| 3704 | debug_print_tree(command->group, lvl+1); |
| 3705 | prn++; |
| 3706 | continue; |
| 3707 | } |
| 3708 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3709 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3710 | argv++; |
| 3711 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 3712 | if (command->redirects) |
| 3713 | fdprintf(2, " {redir}"); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3714 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3715 | prn++; |
| 3716 | } |
| 3717 | pi = pi->next; |
| 3718 | pin++; |
| 3719 | } |
| 3720 | } |
| 3721 | #endif /* debug_print_tree */ |
| 3722 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3723 | static struct pipe *new_pipe(void) |
| 3724 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3725 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3726 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3727 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3728 | return pi; |
| 3729 | } |
| 3730 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3731 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3732 | * if ctx->command is NULL. |
| 3733 | * No errors possible here. |
| 3734 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3735 | static int done_command(struct parse_context *ctx) |
| 3736 | { |
| 3737 | /* The command is really already in the pipe structure, so |
| 3738 | * advance the pipe counter and make a new, null command. */ |
| 3739 | struct pipe *pi = ctx->pipe; |
| 3740 | struct command *command = ctx->command; |
| 3741 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3742 | #if 0 /* Instead we emit error message at run time */ |
| 3743 | if (ctx->pending_redirect) { |
| 3744 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3745 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3746 | ctx->pending_redirect = NULL; |
| 3747 | } |
| 3748 | #endif |
| 3749 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3750 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3751 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3752 | 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] | 3753 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3754 | } |
| 3755 | pi->num_cmds++; |
| 3756 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3757 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3758 | } else { |
| 3759 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3760 | } |
| 3761 | |
| 3762 | /* Only real trickiness here is that the uncommitted |
| 3763 | * command structure is not counted in pi->num_cmds. */ |
| 3764 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3765 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3766 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3767 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3768 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 3769 | command->lineno = G.parse_lineno; |
| 3770 | debug_printf_parse("command->lineno = G.parse_lineno (%u)\n", G.parse_lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3771 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3772 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3773 | } |
| 3774 | |
| 3775 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3776 | { |
| 3777 | int not_null; |
| 3778 | |
| 3779 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3780 | /* Close previous command */ |
| 3781 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3782 | #if HAS_KEYWORDS |
| 3783 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3784 | ctx->ctx_inverted = 0; |
| 3785 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3786 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3787 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3788 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3789 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3790 | * in a backgrounded subshell. |
| 3791 | */ |
| 3792 | struct pipe *pi; |
| 3793 | struct command *command; |
| 3794 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3795 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3796 | pi = ctx->list_head; |
| 3797 | while (pi != ctx->pipe) { |
| 3798 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3799 | goto no_conv; |
| 3800 | pi = pi->next; |
| 3801 | } |
| 3802 | |
| 3803 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3804 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3805 | pi = xzalloc(sizeof(*pi)); |
| 3806 | pi->followup = PIPE_BG; |
| 3807 | pi->num_cmds = 1; |
| 3808 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3809 | command = &pi->cmds[0]; |
| 3810 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3811 | command->cmd_type = CMD_NORMAL; |
| 3812 | command->group = ctx->list_head; |
| 3813 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3814 | command->group_as_string = xstrndup( |
| 3815 | ctx->as_string.data, |
| 3816 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3817 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3818 | #endif |
| 3819 | /* Replace all pipes in ctx with one newly created */ |
| 3820 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3821 | } else { |
| 3822 | no_conv: |
| 3823 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3824 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* Without this check, even just <enter> on command line generates |
| 3827 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3828 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3829 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3830 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3831 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3832 | #endif |
| 3833 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3834 | || ctx->ctx_res_w == RES_DONE |
| 3835 | || ctx->ctx_res_w == RES_FOR |
| 3836 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3837 | #endif |
| 3838 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3839 | || ctx->ctx_res_w == RES_ESAC |
| 3840 | #endif |
| 3841 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3842 | struct pipe *new_p; |
| 3843 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3844 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3845 | not_null, ctx->ctx_res_w); |
| 3846 | new_p = new_pipe(); |
| 3847 | ctx->pipe->next = new_p; |
| 3848 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3849 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3850 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3851 | * This is used to control execution. |
| 3852 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3853 | * cases where variable or value happens to match a keyword): |
| 3854 | */ |
| 3855 | #if ENABLE_HUSH_LOOPS |
| 3856 | if (ctx->ctx_res_w == RES_FOR |
| 3857 | || ctx->ctx_res_w == RES_IN) |
| 3858 | ctx->ctx_res_w = RES_NONE; |
| 3859 | #endif |
| 3860 | #if ENABLE_HUSH_CASE |
| 3861 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3862 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3863 | if (ctx->ctx_res_w == RES_CASE) |
| 3864 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3865 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3866 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3867 | /* Create the memory for command, roughly: |
| 3868 | * ctx->pipe->cmds = new struct command; |
| 3869 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3870 | */ |
| 3871 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3872 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3873 | } |
| 3874 | debug_printf_parse("done_pipe return\n"); |
| 3875 | } |
| 3876 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3877 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3878 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3879 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3880 | if (MAYBE_ASSIGNMENT != 0) |
| 3881 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3882 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3883 | /* Create the memory for command, roughly: |
| 3884 | * ctx->pipe->cmds = new struct command; |
| 3885 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3886 | */ |
| 3887 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3890 | /* If a reserved word is found and processed, parse context is modified |
| 3891 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3892 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3893 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3894 | struct reserved_combo { |
| 3895 | char literal[6]; |
| 3896 | unsigned char res; |
| 3897 | unsigned char assignment_flag; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 3898 | uint32_t flag; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3899 | }; |
| 3900 | enum { |
| 3901 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3902 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3903 | FLAG_IF = (1 << RES_IF ), |
| 3904 | FLAG_THEN = (1 << RES_THEN ), |
| 3905 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3906 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3907 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3908 | # endif |
| 3909 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3910 | FLAG_FOR = (1 << RES_FOR ), |
| 3911 | FLAG_WHILE = (1 << RES_WHILE), |
| 3912 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3913 | FLAG_DO = (1 << RES_DO ), |
| 3914 | FLAG_DONE = (1 << RES_DONE ), |
| 3915 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3916 | # endif |
| 3917 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3918 | FLAG_MATCH = (1 << RES_MATCH), |
| 3919 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3920 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3921 | FLAG_START = (1 << RES_XXXX ), |
| 3922 | }; |
| 3923 | |
| 3924 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3925 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3926 | /* Mostly a list of accepted follow-up reserved words. |
| 3927 | * FLAG_END means we are done with the sequence, and are ready |
| 3928 | * to turn the compound list into a command. |
| 3929 | * FLAG_START means the word must start a new compound list. |
| 3930 | */ |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame^] | 3931 | static const struct reserved_combo reserved_list[] ALIGN4 = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3932 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3933 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3934 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3935 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3936 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3937 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3938 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3939 | # endif |
| 3940 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3941 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3942 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3943 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3944 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3945 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3946 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3947 | # endif |
| 3948 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3949 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3950 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3951 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3952 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3953 | const struct reserved_combo *r; |
| 3954 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3955 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3956 | if (strcmp(word->data, r->literal) == 0) |
| 3957 | return r; |
| 3958 | } |
| 3959 | return NULL; |
| 3960 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3961 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3962 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3963 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3964 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3965 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3966 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3967 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3968 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3969 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3970 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3971 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3972 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3973 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3974 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3975 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3976 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3977 | |
| 3978 | 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] | 3979 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3980 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3981 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3982 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3983 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3984 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3985 | if (r->flag == 0) { /* '!' */ |
| 3986 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3987 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3988 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3989 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3990 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3991 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3992 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3993 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3994 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3995 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3996 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3997 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3998 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3999 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4000 | } 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] | 4001 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4002 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4003 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4004 | } else { |
| 4005 | /* "{...} fi" is ok. "{...} if" is not |
| 4006 | * Example: |
| 4007 | * if { echo foo; } then { echo bar; } fi */ |
| 4008 | if (ctx->command->group) |
| 4009 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4010 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4011 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4012 | ctx->ctx_res_w = r->res; |
| 4013 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4014 | ctx->is_assignment = r->assignment_flag; |
| 4015 | 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] | 4016 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4017 | if (ctx->old_flag & FLAG_END) { |
| 4018 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4019 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4020 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4021 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4022 | old = ctx->stack; |
| 4023 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4024 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4025 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4026 | /* At this point, the compound command's string is in |
| 4027 | * ctx->as_string... except for the leading keyword! |
| 4028 | * Consider this example: "echo a | if true; then echo a; fi" |
| 4029 | * ctx->as_string will contain "true; then echo a; fi", |
| 4030 | * with "if " remaining in old->as_string! |
| 4031 | */ |
| 4032 | { |
| 4033 | char *str; |
| 4034 | int len = old->as_string.length; |
| 4035 | /* Concatenate halves */ |
| 4036 | o_addstr(&old->as_string, ctx->as_string.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4037 | o_free(&ctx->as_string); |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4038 | /* Find where leading keyword starts in first half */ |
| 4039 | str = old->as_string.data + len; |
| 4040 | if (str > old->as_string.data) |
| 4041 | str--; /* skip whitespace after keyword */ |
| 4042 | while (str > old->as_string.data && isalpha(str[-1])) |
| 4043 | str--; |
| 4044 | /* Ugh, we're done with this horrid hack */ |
| 4045 | old->command->group_as_string = xstrdup(str); |
| 4046 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4047 | old->command->group_as_string); |
| 4048 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4049 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4050 | *ctx = *old; /* physical copy */ |
| 4051 | free(old); |
| 4052 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4053 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4054 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4055 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4056 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4057 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4058 | * Normal return is 0. Syntax errors return 1. |
| 4059 | * Note: on return, word is reset, but not o_free'd! |
| 4060 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4061 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4062 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4063 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4064 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4065 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 4066 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4067 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4068 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4069 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4070 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4071 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4072 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4073 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4074 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4075 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4076 | * If the redirection operator is "<<" or "<<-", the word |
| 4077 | * that follows the redirection operator shall be |
| 4078 | * subjected to quote removal; it is unspecified whether |
| 4079 | * any of the other expansions occur. For the other |
| 4080 | * redirection operators, the word that follows the |
| 4081 | * redirection operator shall be subjected to tilde |
| 4082 | * expansion, parameter expansion, command substitution, |
| 4083 | * arithmetic expansion, and quote removal. |
| 4084 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4085 | * on the word by a non-interactive shell; an interactive |
| 4086 | * shell may perform it, but shall do so only when |
| 4087 | * the expansion would result in one word." |
| 4088 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 4089 | //bash does not do parameter/command substitution or arithmetic expansion |
| 4090 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 4091 | // as written: |
| 4092 | // <<EOF$t |
| 4093 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4094 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 4095 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4096 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4097 | /* Cater for >\file case: |
| 4098 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4099 | * Same with heredocs: |
| 4100 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4101 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4102 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4103 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4104 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4105 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4106 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4107 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4108 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4109 | 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] | 4110 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4111 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4112 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4113 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4114 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4115 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4116 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4117 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4118 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4119 | ctx->ctx_dsemicolon = 0; |
| 4120 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4121 | # endif |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4122 | # if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 4123 | if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB |
| 4124 | && strcmp(ctx->word.data, "]]") == 0 |
| 4125 | ) { |
| 4126 | /* allow "[[ ]] >file" etc */ |
| 4127 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4128 | } else |
| 4129 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4130 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4131 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4132 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4133 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4134 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4135 | # if ENABLE_HUSH_CASE |
| 4136 | && ctx->ctx_res_w != RES_CASE |
| 4137 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4138 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4139 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4140 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4141 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4142 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4143 | # if ENABLE_HUSH_LINENO_VAR |
| 4144 | /* Case: |
| 4145 | * "while ...; do |
| 4146 | * cmd ..." |
| 4147 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 4148 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 4149 | */ |
| 4150 | if (0 |
| 4151 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 4152 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 4153 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 4154 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 4155 | ) { |
| 4156 | done_pipe(ctx, PIPE_SEQ); |
| 4157 | } |
| 4158 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4159 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4160 | debug_printf_parse("done_word return %d\n", |
| 4161 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4162 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4163 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4164 | # if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 4165 | if (strcmp(ctx->word.data, "[[") == 0) { |
| 4166 | command->cmd_type = CMD_TEST2_SINGLEWORD_NOGLOB; |
| 4167 | } else |
| 4168 | # endif |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4169 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 4170 | if (0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4171 | /* In bash, local/export/readonly are special, args |
| 4172 | * are assignments and therefore expansion of them |
| 4173 | * should be "one-word" expansion: |
| 4174 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 4175 | * compare with: |
| 4176 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 4177 | * ls: cannot access i=a: No such file or directory |
| 4178 | * ls: cannot access b: No such file or directory |
| 4179 | * Note: bash 3.2.33(1) does this only if export word |
| 4180 | * itself is not quoted: |
| 4181 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 4182 | * aaa bbb |
| 4183 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 4184 | * aaa |
| 4185 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4186 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 4187 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 4188 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4189 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4190 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4191 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4192 | # else |
| 4193 | { /* empty block to pair "if ... else" */ } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4194 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4195 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4196 | #endif /* HAS_KEYWORDS */ |
| 4197 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4198 | if (command->group) { |
| 4199 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4200 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4201 | debug_printf_parse("done_word return 1: syntax error, " |
| 4202 | "groups and arglists don't mix\n"); |
| 4203 | return 1; |
| 4204 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4205 | |
| 4206 | /* If this word wasn't an assignment, next ones definitely |
| 4207 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4208 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4209 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4210 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4211 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4212 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4213 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4214 | command->assignment_cnt++; |
| 4215 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4216 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4217 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4218 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4219 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4220 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4221 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4222 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4223 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4224 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4225 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4226 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4227 | if (ctx->word.has_quoted_part |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 4228 | || endofname(command->argv[0])[0] != '\0' |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4229 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4230 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4231 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4232 | return 1; |
| 4233 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4234 | /* Force FOR to have just one word (variable name) */ |
| 4235 | /* NB: basically, this makes hush see "for v in ..." |
| 4236 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4237 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4238 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4239 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4240 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4241 | #if ENABLE_HUSH_CASE |
| 4242 | /* Force CASE to have just one word */ |
| 4243 | if (ctx->ctx_res_w == RES_CASE) { |
| 4244 | done_pipe(ctx, PIPE_SEQ); |
| 4245 | } |
| 4246 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4247 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4248 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4249 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4250 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4251 | return 0; |
| 4252 | } |
| 4253 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4254 | |
| 4255 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4256 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4257 | * Return: |
| 4258 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4259 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4260 | * REDIRFD_TO_FILE if no & was seen, |
| 4261 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4262 | */ |
| 4263 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4264 | #define parse_redir_right_fd(as_string, input) \ |
| 4265 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4266 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4267 | 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] | 4268 | { |
| 4269 | int ch, d, ok; |
| 4270 | |
| 4271 | ch = i_peek(input); |
| 4272 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4273 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4274 | |
| 4275 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4276 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4277 | ch = i_peek(input); |
| 4278 | if (ch == '-') { |
| 4279 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4280 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4281 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4282 | } |
| 4283 | d = 0; |
| 4284 | ok = 0; |
| 4285 | while (ch != EOF && isdigit(ch)) { |
| 4286 | d = d*10 + (ch-'0'); |
| 4287 | ok = 1; |
| 4288 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4289 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4290 | ch = i_peek(input); |
| 4291 | } |
| 4292 | if (ok) return d; |
| 4293 | |
| 4294 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4295 | |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 4296 | bb_simple_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4297 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4298 | } |
| 4299 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4300 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4301 | */ |
| 4302 | static int parse_redirect(struct parse_context *ctx, |
| 4303 | int fd, |
| 4304 | redir_type style, |
| 4305 | struct in_str *input) |
| 4306 | { |
| 4307 | struct command *command = ctx->command; |
| 4308 | struct redir_struct *redir; |
| 4309 | struct redir_struct **redirp; |
| 4310 | int dup_num; |
| 4311 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4312 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4313 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4314 | /* Check for a '>&1' type redirect */ |
| 4315 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4316 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4317 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4318 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4319 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4320 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4321 | if (dup_num) { /* <<-... */ |
| 4322 | ch = i_getch(input); |
| 4323 | nommu_addchr(&ctx->as_string, ch); |
| 4324 | ch = i_peek(input); |
| 4325 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4326 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4327 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4328 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4329 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4330 | if (ch == '|') { |
| 4331 | /* >|FILE redirect ("clobbering" >). |
| 4332 | * Since we do not support "set -o noclobber" yet, |
| 4333 | * >| and > are the same for now. Just eat |. |
| 4334 | */ |
| 4335 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4336 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | /* Create a new redir_struct and append it to the linked list */ |
| 4341 | redirp = &command->redirects; |
| 4342 | while ((redir = *redirp) != NULL) { |
| 4343 | redirp = &(redir->next); |
| 4344 | } |
| 4345 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4346 | /* redir->next = NULL; */ |
| 4347 | /* redir->rd_filename = NULL; */ |
| 4348 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4349 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4350 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4351 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4352 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4353 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4354 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4355 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4356 | /* Erik had a check here that the file descriptor in question |
| 4357 | * is legit; I postpone that to "run time" |
| 4358 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4359 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4360 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4361 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4362 | #if 0 /* Instead we emit error message at run time */ |
| 4363 | if (ctx->pending_redirect) { |
| 4364 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4365 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4366 | } |
| 4367 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4368 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4369 | * end of the next parsed word. */ |
| 4370 | ctx->pending_redirect = redir; |
| 4371 | } |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4375 | /* If a redirect is immediately preceded by a number, that number is |
| 4376 | * supposed to tell which file descriptor to redirect. This routine |
| 4377 | * looks for such preceding numbers. In an ideal world this routine |
| 4378 | * needs to handle all the following classes of redirects... |
| 4379 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4380 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4381 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4382 | * 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] | 4383 | * |
| 4384 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4385 | * "2.7 Redirection |
| 4386 | * ... If n is quoted, the number shall not be recognized as part of |
| 4387 | * the redirection expression. For example: |
| 4388 | * echo \2>a |
| 4389 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4390 | * 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] | 4391 | * |
| 4392 | * A -1 return means no valid number was found, |
| 4393 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4394 | */ |
| 4395 | static int redirect_opt_num(o_string *o) |
| 4396 | { |
| 4397 | int num; |
| 4398 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4399 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4400 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4401 | num = bb_strtou(o->data, NULL, 10); |
| 4402 | if (errno || num < 0) |
| 4403 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4404 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4405 | return num; |
| 4406 | } |
| 4407 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4408 | #if BB_MMU |
| 4409 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4410 | fetch_till_str(input, word, skip_tabs) |
| 4411 | #endif |
| 4412 | static char *fetch_till_str(o_string *as_string, |
| 4413 | struct in_str *input, |
| 4414 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4415 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4416 | { |
| 4417 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4418 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4419 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4420 | int ch; |
| 4421 | |
Denys Vlasenko | d73cdbf | 2018-07-23 15:43:57 +0200 | [diff] [blame] | 4422 | /* Starting with "" is necessary for this case: |
| 4423 | * cat <<EOF |
| 4424 | * |
| 4425 | * xxx |
| 4426 | * EOF |
| 4427 | */ |
| 4428 | heredoc.data = xzalloc(1); /* start as "", not as NULL */ |
| 4429 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4430 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4431 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4432 | while (1) { |
| 4433 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4434 | if (ch != EOF) |
| 4435 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4436 | if (ch == '\n' || ch == EOF) { |
| 4437 | check_heredoc_end: |
| 4438 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4439 | /* End-of-line, and not a line continuation */ |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4440 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4441 | heredoc.data[past_EOL] = '\0'; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4442 | debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4443 | return heredoc.data; |
| 4444 | } |
| 4445 | if (ch == '\n') { |
| 4446 | /* This is a new line. |
| 4447 | * Remember position and backslash-escaping status. |
| 4448 | */ |
| 4449 | o_addchr(&heredoc, ch); |
| 4450 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4451 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4452 | past_EOL = heredoc.length; |
| 4453 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4454 | do { |
| 4455 | ch = i_getch(input); |
| 4456 | if (ch != EOF) |
| 4457 | nommu_addchr(as_string, ch); |
| 4458 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4459 | /* If this immediately ended the line, |
| 4460 | * go back to end-of-line checks. |
| 4461 | */ |
| 4462 | if (ch == '\n') |
| 4463 | goto check_heredoc_end; |
| 4464 | } |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4465 | } else { |
| 4466 | /* Backslash-line continuation in an unquoted |
| 4467 | * heredoc. This does not need special handling |
| 4468 | * for heredoc body (unquoted heredocs are |
| 4469 | * expanded on "execution" and that would take |
| 4470 | * care of this case too), but not the case |
| 4471 | * of line continuation *in terminator*: |
| 4472 | * cat <<EOF |
| 4473 | * Ok1 |
| 4474 | * EO\ |
| 4475 | * F |
| 4476 | */ |
| 4477 | heredoc.data[--heredoc.length] = '\0'; |
| 4478 | prev = 0; /* not '\' */ |
| 4479 | continue; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4480 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4481 | } |
| 4482 | if (ch == EOF) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4483 | o_free(&heredoc); |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4484 | return NULL; /* error */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4485 | } |
| 4486 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4487 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4488 | if (prev == '\\' && ch == '\\') |
| 4489 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4490 | prev = 0; /* not '\' */ |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4491 | else |
| 4492 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4496 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4497 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4498 | */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4499 | #if BB_MMU |
| 4500 | #define fetch_heredocs(as_string, pi, heredoc_cnt, input) \ |
| 4501 | fetch_heredocs(pi, heredoc_cnt, input) |
| 4502 | #endif |
| 4503 | static int fetch_heredocs(o_string *as_string, struct pipe *pi, int heredoc_cnt, struct in_str *input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4504 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4505 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4506 | int i; |
| 4507 | struct command *cmd = pi->cmds; |
| 4508 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4509 | debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4510 | pi->num_cmds, |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4511 | cmd->argv ? cmd->argv[0] : "NONE" |
| 4512 | ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4513 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4514 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4515 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4516 | debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4517 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4518 | while (redir) { |
| 4519 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4520 | char *p; |
| 4521 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4522 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4523 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4524 | p = fetch_till_str(as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4525 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4526 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4527 | syntax_error("unexpected EOF in here document"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4528 | return -1; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4529 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4530 | free(redir->rd_filename); |
| 4531 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4532 | heredoc_cnt--; |
| 4533 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4534 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4535 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4536 | if (cmd->group) { |
| 4537 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4538 | heredoc_cnt = fetch_heredocs(as_string, cmd->group, heredoc_cnt, input); |
| 4539 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4540 | if (heredoc_cnt < 0) |
| 4541 | return heredoc_cnt; /* error */ |
| 4542 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4543 | cmd++; |
| 4544 | } |
| 4545 | pi = pi->next; |
| 4546 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4547 | return heredoc_cnt; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4551 | static int run_list(struct pipe *pi); |
| 4552 | #if BB_MMU |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4553 | #define parse_stream(pstring, heredoc_cnt_ptr, input, end_trigger) \ |
| 4554 | parse_stream(heredoc_cnt_ptr, input, end_trigger) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4555 | #endif |
| 4556 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4557 | int *heredoc_cnt_ptr, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4558 | struct in_str *input, |
| 4559 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4560 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4561 | /* Returns number of heredocs not yet consumed, |
| 4562 | * or -1 on error. |
| 4563 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4564 | static int parse_group(struct parse_context *ctx, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4565 | struct in_str *input, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4566 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4567 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4568 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4569 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4570 | #if BB_MMU |
| 4571 | # define as_string NULL |
| 4572 | #else |
| 4573 | char *as_string = NULL; |
| 4574 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4575 | struct pipe *pipe_list; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4576 | int heredoc_cnt = 0; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4577 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4578 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4579 | |
| 4580 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4581 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4582 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4583 | if (ctx->word.length) |
| 4584 | if (done_word(ctx)) |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4585 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4586 | if (!command->argv) |
| 4587 | goto skip; /* (... */ |
| 4588 | if (command->argv[1]) { /* word word ... (... */ |
| 4589 | syntax_error_unexpected_ch('('); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4590 | return -1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4591 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4592 | /* it is "word(..." or "word (..." */ |
| 4593 | do |
| 4594 | ch = i_getch(input); |
| 4595 | while (ch == ' ' || ch == '\t'); |
| 4596 | if (ch != ')') { |
| 4597 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4598 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4599 | } |
| 4600 | nommu_addchr(&ctx->as_string, ch); |
| 4601 | do |
| 4602 | ch = i_getch(input); |
| 4603 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4604 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4605 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4606 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4607 | } |
| 4608 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4609 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4610 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4611 | } |
| 4612 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4613 | |
| 4614 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4615 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4616 | || ctx->word.length /* word{... */ |
| 4617 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4618 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4619 | syntax_error(NULL); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4620 | debug_printf_parse("parse_group return -1: " |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4621 | "syntax error, groups and arglists don't mix\n"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4622 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4623 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4624 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4625 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4626 | IF_HUSH_FUNCTIONS(skip:) |
| 4627 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4628 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4629 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4630 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4631 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4632 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4633 | } else { |
| 4634 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4635 | ch = i_peek(input); |
| 4636 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4637 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4638 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4639 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4640 | return -1; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4641 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4642 | if (ch != '(') { |
| 4643 | ch = i_getch(input); |
| 4644 | nommu_addchr(&ctx->as_string, ch); |
| 4645 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4646 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4647 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4648 | debug_printf_heredoc("calling parse_stream, heredoc_cnt:%d\n", heredoc_cnt); |
| 4649 | pipe_list = parse_stream(&as_string, &heredoc_cnt, input, endch); |
| 4650 | debug_printf_heredoc("parse_stream returned: heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4651 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4652 | if (as_string) |
| 4653 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4654 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4655 | |
| 4656 | /* empty ()/{} or parse error? */ |
| 4657 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4658 | /* parse_stream already emitted error msg */ |
| 4659 | if (!BB_MMU) |
| 4660 | free(as_string); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4661 | debug_printf_parse("parse_group return -1: " |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4662 | "parse_stream returned %p\n", pipe_list); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4663 | return -1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4664 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4665 | #if !BB_MMU |
| 4666 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4667 | command->group_as_string = as_string; |
| 4668 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4669 | command->group_as_string); |
| 4670 | #endif |
| 4671 | |
| 4672 | #if ENABLE_HUSH_FUNCTIONS |
| 4673 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4674 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4675 | struct command *cmd2; |
| 4676 | |
| 4677 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4678 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4679 | cmd2->group = pipe_list; |
| 4680 | # if !BB_MMU |
| 4681 | //UNTESTED! |
| 4682 | cmd2->group_as_string = command->group_as_string; |
| 4683 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4684 | # endif |
| 4685 | |
| 4686 | pipe_list = new_pipe(); |
| 4687 | pipe_list->cmds = cmd2; |
| 4688 | pipe_list->num_cmds = 1; |
| 4689 | } |
| 4690 | #endif |
| 4691 | |
| 4692 | command->group = pipe_list; |
| 4693 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4694 | debug_printf_parse("parse_group return %d\n", heredoc_cnt); |
| 4695 | return heredoc_cnt; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4696 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4697 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4700 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4701 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4702 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4703 | 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] | 4704 | { |
| 4705 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4706 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4707 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4708 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4709 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4710 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4711 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4712 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4713 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4714 | } |
| 4715 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4716 | static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input) |
| 4717 | { |
| 4718 | while (1) { |
| 4719 | int ch = i_getch(input); |
| 4720 | if (ch == EOF) { |
| 4721 | syntax_error_unterm_ch('\''); |
| 4722 | return 0; |
| 4723 | } |
| 4724 | if (ch == '\'') |
| 4725 | return 1; |
| 4726 | o_addqchr(dest, ch); |
| 4727 | } |
| 4728 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4729 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4730 | 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] | 4731 | 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] | 4732 | { |
| 4733 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4734 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4735 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4736 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4737 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4738 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4739 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4740 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4741 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4742 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4743 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4744 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4745 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4746 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4747 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4748 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4749 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4750 | continue; |
| 4751 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4752 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4753 | } |
| 4754 | } |
| 4755 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4756 | * \` quoting. |
| 4757 | * "Within the backquoted style of command substitution, backslash |
| 4758 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4759 | * The search for the matching backquote shall be satisfied by the first |
| 4760 | * backquote found without a preceding backslash; during this search, |
| 4761 | * if a non-escaped backquote is encountered within a shell comment, |
| 4762 | * a here-document, an embedded command substitution of the $(command) |
| 4763 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4764 | * or double-quoted string that begins, but does not end, within the |
| 4765 | * "`...`" sequence produces undefined results." |
| 4766 | * Example Output |
| 4767 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4768 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4769 | 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] | 4770 | { |
| 4771 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4772 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4773 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4774 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4775 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4776 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4777 | ch = i_getch(input); |
| 4778 | if (ch != '`' |
| 4779 | && ch != '$' |
| 4780 | && ch != '\\' |
| 4781 | && (!in_dquote || ch != '"') |
| 4782 | ) { |
| 4783 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4784 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4785 | } |
| 4786 | if (ch == EOF) { |
| 4787 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4788 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4789 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4790 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4794 | * quoting and nested ()s. |
| 4795 | * "With the $(command) style of command substitution, all characters |
| 4796 | * following the open parenthesis to the matching closing parenthesis |
| 4797 | * constitute the command. Any valid shell script can be used for command, |
| 4798 | * except a script consisting solely of redirections which produces |
| 4799 | * unspecified results." |
| 4800 | * Example Output |
| 4801 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4802 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4803 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4804 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4805 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4806 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4807 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4808 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4809 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4810 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4811 | 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] | 4812 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4813 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4814 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4815 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4816 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4817 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4818 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4819 | |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4820 | # if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4821 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4822 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4823 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4824 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4825 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4826 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4827 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4828 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4829 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4830 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4831 | if (ch == end_ch |
| 4832 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4833 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4834 | # endif |
| 4835 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4836 | if (!dbl) |
| 4837 | break; |
| 4838 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4839 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4840 | i_getch(input); /* eat second ')' */ |
| 4841 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4842 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4843 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4844 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4845 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4846 | if (ch == '(' || ch == '{') { |
| 4847 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4848 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4849 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4850 | o_addchr(dest, ch); |
| 4851 | continue; |
| 4852 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4853 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4854 | if (!add_till_single_quote(dest, input)) |
| 4855 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4856 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4857 | continue; |
| 4858 | } |
| 4859 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4860 | if (!add_till_double_quote(dest, input)) |
| 4861 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4862 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4863 | continue; |
| 4864 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4865 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4866 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4867 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4868 | o_addchr(dest, ch); |
| 4869 | continue; |
| 4870 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4871 | if (ch == '\\') { |
| 4872 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4873 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4874 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4875 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4876 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4877 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4878 | # if 0 |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4879 | if (ch == '\n') { |
| 4880 | /* "backslash+newline", ignore both */ |
| 4881 | o_delchr(dest); /* undo insertion of '\' */ |
| 4882 | continue; |
| 4883 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4884 | # endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4885 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4886 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4887 | continue; |
| 4888 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4889 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4890 | 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] | 4891 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4892 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4893 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4894 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4895 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4896 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4897 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4898 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4899 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4900 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4901 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4902 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4903 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4904 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4905 | 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] | 4906 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4907 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4908 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4909 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4910 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4911 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4912 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4913 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4914 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4915 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4916 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4917 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4918 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4919 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4920 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4921 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4922 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4923 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4924 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4925 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4926 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4927 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4928 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4929 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4930 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4931 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4932 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4933 | o_addchr(dest, ch | quote_mask); |
| 4934 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4935 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4936 | case '$': /* pid */ |
| 4937 | case '!': /* last bg pid */ |
| 4938 | case '?': /* last exit code */ |
| 4939 | case '#': /* number of args */ |
| 4940 | case '*': /* args */ |
| 4941 | case '@': /* args */ |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 4942 | case '-': /* $- option flags set by set builtin or shell options (-i etc) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4943 | goto make_one_char_var; |
| 4944 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4945 | char len_single_ch; |
| 4946 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4947 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4948 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4949 | ch = i_getch(input); /* eat '{' */ |
| 4950 | nommu_addchr(as_string, ch); |
| 4951 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4952 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4953 | /* It should be ${?}, or ${#var}, |
| 4954 | * or even ${?+subst} - operator acting on a special variable, |
| 4955 | * or the beginning of variable name. |
| 4956 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4957 | if (ch == EOF |
| 4958 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4959 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4960 | bad_dollar_syntax: |
| 4961 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4962 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4963 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4964 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4965 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4966 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4967 | ch |= quote_mask; |
| 4968 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4969 | /* 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] | 4970 | * However, this regresses some of our testsuite cases |
| 4971 | * which check invalid constructs like ${%}. |
| 4972 | * Oh well... let's check that the var name part is fine... */ |
| 4973 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4974 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4975 | unsigned pos; |
| 4976 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4977 | o_addchr(dest, ch); |
| 4978 | debug_printf_parse(": '%c'\n", ch); |
| 4979 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4980 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4981 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4982 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4983 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4984 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4985 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4986 | unsigned end_ch; |
| 4987 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4988 | /* handle parameter expansions |
| 4989 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4990 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4991 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4992 | if (len_single_ch != '#' |
| 4993 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4994 | || i_peek(input) != '}' |
| 4995 | ) { |
| 4996 | goto bad_dollar_syntax; |
| 4997 | } |
| 4998 | /* else: it's "length of C" ${#C} op, |
| 4999 | * where C is a single char |
| 5000 | * special var name, e.g. ${#!}. |
| 5001 | */ |
| 5002 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5003 | /* Eat everything until closing '}' (or ':') */ |
| 5004 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5005 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5006 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5007 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5008 | ) { |
| 5009 | /* It's ${var:N[:M]} thing */ |
| 5010 | end_ch = '}' * 0x100 + ':'; |
| 5011 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5012 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5013 | && ch == '/' |
| 5014 | ) { |
| 5015 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 5016 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 5017 | i_getch(input); |
| 5018 | nommu_addchr(as_string, '/'); |
| 5019 | ch = '\\'; |
| 5020 | } |
| 5021 | end_ch = '}' * 0x100 + '/'; |
| 5022 | } |
| 5023 | o_addchr(dest, ch); |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 5024 | /* The pattern can't be empty. |
| 5025 | * IOW: if the first char after "${v//" is a slash, |
| 5026 | * it does not terminate the pattern - it's the first char of the pattern: |
| 5027 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 5028 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 5029 | */ |
| 5030 | if (i_peek(input) == '/') { |
| 5031 | o_addchr(dest, i_getch(input)); |
| 5032 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5033 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5034 | if (!BB_MMU) |
| 5035 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5036 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5037 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5038 | if (last_ch == 0) /* error? */ |
| 5039 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5040 | #else |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 5041 | # error Simple code to only allow ${var} is not implemented |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5042 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5043 | if (as_string) { |
| 5044 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5045 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5046 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5047 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5048 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 5049 | && (end_ch & 0xff00) |
| 5050 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5051 | /* close the first block: */ |
| 5052 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5053 | /* while parsing N from ${var:N[:M]} |
| 5054 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5055 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5056 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5057 | end_ch = '}'; |
| 5058 | goto again; |
| 5059 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5060 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5061 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5062 | /* it's ${var:N} - emulate :999999999 */ |
| 5063 | o_addstr(dest, "999999999"); |
| 5064 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5065 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5066 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5067 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5068 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5069 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5070 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5071 | break; |
| 5072 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5073 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5074 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5075 | unsigned pos; |
| 5076 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5077 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5078 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5079 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 5080 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5081 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5082 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5083 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 5084 | o_addchr(dest, quote_mask | '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5085 | if (!BB_MMU) |
| 5086 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5087 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 5088 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5089 | if (as_string) { |
| 5090 | o_addstr(as_string, dest->data + pos); |
| 5091 | o_addchr(as_string, ')'); |
| 5092 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5093 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5094 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5095 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5096 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5097 | # endif |
| 5098 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5099 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5100 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5101 | if (!BB_MMU) |
| 5102 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5103 | if (!add_till_closing_bracket(dest, input, ')')) |
| 5104 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5105 | if (as_string) { |
| 5106 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 5107 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5108 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5109 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5110 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5111 | break; |
| 5112 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5113 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5114 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5115 | goto make_var; |
| 5116 | #if 0 |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 5117 | /* TODO: $_: */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 5118 | /* $_ Shell or shell script name; or last argument of last command |
| 5119 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 5120 | * but in command's env, set to full pathname used to invoke it */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5121 | ch = i_getch(input); |
| 5122 | nommu_addchr(as_string, ch); |
| 5123 | ch = i_peek_and_eat_bkslash_nl(input); |
| 5124 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5125 | ch = '_'; |
| 5126 | goto make_var1; |
| 5127 | } |
| 5128 | /* else: it's $_ */ |
| 5129 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5130 | default: |
| 5131 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5132 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5133 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 5134 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5135 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5138 | #if BB_MMU |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5139 | #define encode_string(as_string, dest, input, dquote_end) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5140 | encode_string(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5141 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5142 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5143 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5144 | o_string *dest, |
| 5145 | struct in_str *input, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5146 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5147 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5148 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5149 | int next; |
| 5150 | |
| 5151 | again: |
| 5152 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5153 | if (ch != EOF) |
| 5154 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5155 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5156 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 5157 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5158 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5159 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5160 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5161 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5162 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5163 | } |
| 5164 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5165 | if (ch != '\n') { |
| 5166 | next = i_peek(input); |
| 5167 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5168 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5169 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5170 | if (ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5171 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 5172 | /* Testcase: in interactive shell a file with |
| 5173 | * echo "unterminated string\<eof> |
| 5174 | * is sourced. |
| 5175 | */ |
| 5176 | syntax_error_unterm_ch('"'); |
| 5177 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5178 | } |
| 5179 | /* bash: |
| 5180 | * "The backslash retains its special meaning [in "..."] |
| 5181 | * only when followed by one of the following characters: |
| 5182 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5183 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5184 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 5185 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5186 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5187 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 5188 | ch = i_getch(input); /* eat next */ |
| 5189 | if (ch == '\n') |
| 5190 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5191 | } /* else: ch remains == '\\', and we double it below: */ |
| 5192 | 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] | 5193 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5194 | goto again; |
| 5195 | } |
| 5196 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5197 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 5198 | debug_printf_parse("encode_string return 0: " |
| 5199 | "parse_dollar returned 0 (error)\n"); |
| 5200 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5201 | } |
| 5202 | goto again; |
| 5203 | } |
| 5204 | #if ENABLE_HUSH_TICK |
| 5205 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5206 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5207 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5208 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5209 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 5210 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5211 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5212 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5213 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5214 | } |
| 5215 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5216 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5217 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5218 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5221 | /* |
| 5222 | * Scan input until EOF or end_trigger char. |
| 5223 | * Return a list of pipes to execute, or NULL on EOF |
| 5224 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5225 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5226 | * reset parsing machinery and start parsing anew, |
| 5227 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5228 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5229 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5230 | int *heredoc_cnt_ptr, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5231 | struct in_str *input, |
| 5232 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5233 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5234 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5235 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5236 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5237 | /* 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] | 5238 | * 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] | 5239 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5240 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5241 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5242 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5243 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5244 | initialize_context(&ctx); |
| 5245 | |
| 5246 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5247 | * Preventing this: |
| 5248 | */ |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5249 | ctx.word.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5250 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5251 | /* We used to separate words on $IFS here. This was wrong. |
| 5252 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5253 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5254 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5255 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5256 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5257 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5258 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5259 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5260 | int ch; |
| 5261 | int next; |
| 5262 | int redir_fd; |
| 5263 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5264 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5265 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5266 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5267 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5268 | if (ch == EOF) { |
| 5269 | struct pipe *pi; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5270 | #if ENABLE_FEATURE_EDITING |
| 5271 | if (G.flag_ctrlC) { |
| 5272 | /* testcase: interactively entering |
| 5273 | * 'qwe <cr> ^C |
| 5274 | * should not leave input in PS2 mode, waiting to close single quote. |
| 5275 | */ |
| 5276 | G.flag_ctrlC = 0; |
| 5277 | goto parse_error; |
| 5278 | } |
| 5279 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5280 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5281 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5282 | goto parse_error_exitcode1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5283 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5284 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5285 | syntax_error_unterm_ch('('); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5286 | goto parse_error_exitcode1; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5287 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5288 | if (end_trigger == '}') { |
| 5289 | syntax_error_unterm_ch('{'); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5290 | goto parse_error_exitcode1; |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5291 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5292 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5293 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5294 | goto parse_error_exitcode1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5295 | } |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5296 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5297 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5298 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5299 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5300 | /* (this makes bare "&" cmd a no-op. |
| 5301 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5302 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5303 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5304 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5305 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5306 | pi = NULL; |
| 5307 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5308 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5309 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5310 | if (pstring) |
| 5311 | *pstring = ctx.as_string.data; |
| 5312 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5313 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5314 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5315 | // heredoc_cnt must be 0 here anyway |
| 5316 | //if (heredoc_cnt_ptr) |
| 5317 | // *heredoc_cnt_ptr = heredoc_cnt; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5318 | debug_leave(); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5319 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5320 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5321 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5322 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5323 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5324 | /* Handle "'" and "\" first, as they won't play nice with |
| 5325 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5326 | * echo z\\ |
| 5327 | * and |
| 5328 | * echo '\ |
| 5329 | * ' |
| 5330 | * would break. |
| 5331 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5332 | if (ch == '\\') { |
| 5333 | ch = i_getch(input); |
| 5334 | if (ch == '\n') |
| 5335 | continue; /* drop \<newline>, get next char */ |
| 5336 | nommu_addchr(&ctx.as_string, '\\'); |
| 5337 | o_addchr(&ctx.word, '\\'); |
| 5338 | if (ch == EOF) { |
| 5339 | /* Testcase: eval 'echo Ok\' */ |
| 5340 | /* bash-4.3.43 was removing backslash, |
| 5341 | * but 4.4.19 retains it, most other shells too |
| 5342 | */ |
| 5343 | continue; /* get next char */ |
| 5344 | } |
| 5345 | /* Example: echo Hello \2>file |
| 5346 | * we need to know that word 2 is quoted |
| 5347 | */ |
| 5348 | ctx.word.has_quoted_part = 1; |
| 5349 | nommu_addchr(&ctx.as_string, ch); |
| 5350 | o_addchr(&ctx.word, ch); |
| 5351 | continue; /* get next char */ |
| 5352 | } |
| 5353 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5354 | if (ch == '\'') { |
| 5355 | ctx.word.has_quoted_part = 1; |
| 5356 | next = i_getch(input); |
| 5357 | if (next == '\'' && !ctx.pending_redirect) |
| 5358 | goto insert_empty_quoted_str_marker; |
| 5359 | |
| 5360 | ch = next; |
| 5361 | while (1) { |
| 5362 | if (ch == EOF) { |
| 5363 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5364 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5365 | } |
| 5366 | nommu_addchr(&ctx.as_string, ch); |
| 5367 | if (ch == '\'') |
| 5368 | break; |
| 5369 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5370 | /* Convert raw ^C to corresponding special variable reference */ |
| 5371 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5372 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5373 | } |
| 5374 | o_addqchr(&ctx.word, ch); |
| 5375 | ch = i_getch(input); |
| 5376 | } |
| 5377 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5378 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5379 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5380 | next = '\0'; |
| 5381 | if (ch != '\n') |
| 5382 | next = i_peek_and_eat_bkslash_nl(input); |
| 5383 | |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 5384 | is_special = "{}<>&|();#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5385 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5386 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 5387 | #if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 5388 | if (ctx.command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB) { |
| 5389 | /* In [[ ]], {}<>&|() are not special */ |
| 5390 | is_special += 8; |
| 5391 | } else |
| 5392 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5393 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5394 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5395 | || ctx.word.length /* word{... - non-special */ |
| 5396 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5397 | || (next != ';' /* }; - special */ |
| 5398 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5399 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5400 | && next != '&' /* }& and }&& ... - special */ |
| 5401 | && next != '|' /* }|| ... - special */ |
| 5402 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5403 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5404 | ) { |
| 5405 | /* They are not special, skip "{}" */ |
| 5406 | is_special += 2; |
| 5407 | } |
| 5408 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5409 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5410 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5411 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5412 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5413 | o_addQchr(&ctx.word, ch); |
| 5414 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5415 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5416 | && ch == '=' |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 5417 | && endofname(ctx.word.data)[0] == '=' |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5418 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5419 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5420 | 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] | 5421 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5422 | continue; |
| 5423 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5424 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5425 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5426 | #if ENABLE_HUSH_LINENO_VAR |
| 5427 | /* Case: |
| 5428 | * "while ...; do<whitespace><newline> |
| 5429 | * cmd ..." |
| 5430 | * would think that "cmd" starts in <whitespace> - |
| 5431 | * i.e., at the previous line. |
| 5432 | * We need to skip all whitespace before newlines. |
| 5433 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5434 | while (ch != '\n') { |
| 5435 | next = i_peek(input); |
| 5436 | if (next != ' ' && next != '\t' && next != '\n') |
| 5437 | break; /* next char is not ws */ |
| 5438 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5439 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5440 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5441 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5442 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5443 | goto parse_error_exitcode1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5444 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5445 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5446 | /* Is this a case when newline is simply ignored? |
| 5447 | * Some examples: |
| 5448 | * "cmd | <newline> cmd ..." |
| 5449 | * "case ... in <newline> word) ..." |
| 5450 | */ |
| 5451 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5452 | && ctx.word.length == 0 |
| 5453 | && !ctx.word.has_quoted_part |
| 5454 | && heredoc_cnt == 0 |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5455 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5456 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5457 | * Without check #1, interactive shell |
| 5458 | * ignores even bare <newline>, |
| 5459 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5460 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5461 | * ps2> _ <=== wrong, should be ps1 |
| 5462 | * Without check #2, "cmd & <newline>" |
| 5463 | * is similarly mistreated. |
| 5464 | * (BTW, this makes "cmd & cmd" |
| 5465 | * and "cmd && cmd" non-orthogonal. |
| 5466 | * Really, ask yourself, why |
| 5467 | * "cmd && <newline>" doesn't start |
| 5468 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5469 | * The only reason is that it might be |
| 5470 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5471 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5472 | */ |
| 5473 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5474 | if (pi->num_cmds != 0 /* check #1 */ |
| 5475 | && pi->followup != PIPE_BG /* check #2 */ |
| 5476 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5477 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5478 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5479 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5480 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5481 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5482 | debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5483 | if (heredoc_cnt) { |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5484 | heredoc_cnt = fetch_heredocs(&ctx.as_string, ctx.list_head, heredoc_cnt, input); |
| 5485 | if (heredoc_cnt != 0) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5486 | goto parse_error_exitcode1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5487 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5488 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5489 | 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] | 5490 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5491 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5492 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5493 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5494 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5495 | |
| 5496 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5497 | * } is an ordinary char in this case, even inside { cmd; } |
| 5498 | * Pathological example: { ""}; } should exec "}" cmd |
| 5499 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5500 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5501 | if (ctx.word.length != 0 /* word} */ |
| 5502 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5503 | ) { |
| 5504 | goto ordinary_char; |
| 5505 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5506 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5507 | /* Generally, there should be semicolon: "cmd; }" |
| 5508 | * However, bash allows to omit it if "cmd" is |
| 5509 | * a group. Examples: |
| 5510 | * { { echo 1; } } |
| 5511 | * {(echo 1)} |
| 5512 | * { echo 0 >&2 | { echo 1; } } |
| 5513 | * { while false; do :; done } |
| 5514 | * { case a in b) ;; esac } |
| 5515 | */ |
| 5516 | if (ctx.command->group) |
| 5517 | goto term_group; |
| 5518 | goto ordinary_char; |
| 5519 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5520 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5521 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5522 | goto skip_end_trigger; |
| 5523 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5524 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5525 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5526 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5527 | && (ch != ';' || heredoc_cnt == 0) |
| 5528 | #if ENABLE_HUSH_CASE |
| 5529 | && (ch != ')' |
| 5530 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5531 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5532 | ) |
| 5533 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5534 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5535 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5536 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5537 | } |
| 5538 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5539 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5540 | 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] | 5541 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5542 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5543 | 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] | 5544 | ) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5545 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5546 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5547 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5548 | if (pstring) |
| 5549 | *pstring = ctx.as_string.data; |
| 5550 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5551 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5552 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5553 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5554 | /* Example: bare "{ }", "()" */ |
| 5555 | G.last_exitcode = 2; /* bash compat */ |
| 5556 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5557 | goto parse_error; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5558 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5559 | if (heredoc_cnt_ptr) |
| 5560 | *heredoc_cnt_ptr = heredoc_cnt; |
| 5561 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5562 | debug_printf_parse("parse_stream return %p: " |
| 5563 | "end_trigger char found\n", |
| 5564 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5565 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5566 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5567 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5568 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5569 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5570 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5571 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5572 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5573 | /* Catch <, > before deciding whether this word is |
| 5574 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5575 | switch (ch) { |
| 5576 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5577 | redir_fd = redirect_opt_num(&ctx.word); |
| 5578 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5579 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5580 | } |
| 5581 | redir_style = REDIRECT_OVERWRITE; |
| 5582 | if (next == '>') { |
| 5583 | redir_style = REDIRECT_APPEND; |
| 5584 | ch = i_getch(input); |
| 5585 | nommu_addchr(&ctx.as_string, ch); |
| 5586 | } |
| 5587 | #if 0 |
| 5588 | else if (next == '(') { |
| 5589 | syntax_error(">(process) not supported"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5590 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5591 | } |
| 5592 | #endif |
| 5593 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5594 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5595 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5596 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5597 | redir_fd = redirect_opt_num(&ctx.word); |
| 5598 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5599 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5600 | } |
| 5601 | redir_style = REDIRECT_INPUT; |
| 5602 | if (next == '<') { |
| 5603 | redir_style = REDIRECT_HEREDOC; |
| 5604 | heredoc_cnt++; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5605 | debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5606 | ch = i_getch(input); |
| 5607 | nommu_addchr(&ctx.as_string, ch); |
| 5608 | } else if (next == '>') { |
| 5609 | redir_style = REDIRECT_IO; |
| 5610 | ch = i_getch(input); |
| 5611 | nommu_addchr(&ctx.as_string, ch); |
| 5612 | } |
| 5613 | #if 0 |
| 5614 | else if (next == '(') { |
| 5615 | syntax_error("<(process) not supported"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5616 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5617 | } |
| 5618 | #endif |
| 5619 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5620 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5621 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5622 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5623 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5624 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5625 | /* note: we do not add it to &ctx.as_string */ |
| 5626 | /* TODO: in bash: |
| 5627 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5628 | * cmd "$(cmd2 #comment)" - syntax error |
| 5629 | * cmd "`cmd2 #comment`" - ok |
| 5630 | * We accept both (comment ends where command subst ends, in both cases). |
| 5631 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5632 | while (1) { |
| 5633 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5634 | if (ch == '\n') { |
| 5635 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5636 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5637 | } |
| 5638 | ch = i_getch(input); |
| 5639 | if (ch == EOF) |
| 5640 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5641 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5642 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5643 | } |
| 5644 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5645 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5646 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5647 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5648 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5649 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5650 | && !ctx.pending_redirect |
| 5651 | ) { |
| 5652 | /* ch is a special char and thus this word |
| 5653 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5654 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5655 | 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] | 5656 | } |
| 5657 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5658 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5659 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5660 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5661 | case SPECIAL_VAR_SYMBOL: |
| 5662 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5663 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5664 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5665 | /* fall through */ |
| 5666 | case '#': |
| 5667 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5668 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5669 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5670 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5671 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5672 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5673 | "parse_dollar returned 0 (error)\n"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5674 | goto parse_error_exitcode1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5675 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5676 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5677 | case '"': |
| 5678 | ctx.word.has_quoted_part = 1; |
| 5679 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5680 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5681 | insert_empty_quoted_str_marker: |
| 5682 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5683 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5684 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5685 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5686 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5687 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5688 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5689 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"')) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5690 | goto parse_error_exitcode1; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5691 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5692 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5693 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5694 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5695 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5696 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5697 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5698 | o_addchr(&ctx.word, '`'); |
| 5699 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5700 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5701 | goto parse_error_exitcode1; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5702 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5703 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5704 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5705 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5706 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5707 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5708 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5709 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5710 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5711 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5712 | #if ENABLE_HUSH_CASE |
| 5713 | case_semi: |
| 5714 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5715 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5716 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5717 | } |
| 5718 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5719 | #if ENABLE_HUSH_CASE |
| 5720 | /* Eat multiple semicolons, detect |
| 5721 | * whether it means something special */ |
| 5722 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5723 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5724 | if (ch != ';') |
| 5725 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5726 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5727 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5728 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5729 | ctx.ctx_dsemicolon = 1; |
| 5730 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5731 | break; |
| 5732 | } |
| 5733 | } |
| 5734 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5735 | new_cmd: |
| 5736 | /* We just finished a cmd. New one may start |
| 5737 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5738 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5739 | 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] | 5740 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5741 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5742 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5743 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5744 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5745 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5746 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5747 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5748 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5749 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5750 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5751 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5752 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5753 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5754 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5755 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5756 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5757 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5758 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5759 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5760 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5761 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5762 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5763 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5764 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5765 | } else { |
| 5766 | /* we could pick up a file descriptor choice here |
| 5767 | * with redirect_opt_num(), but bash doesn't do it. |
| 5768 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5769 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5770 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5771 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5772 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5773 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5774 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5775 | if (ctx.ctx_res_w == RES_MATCH |
| 5776 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5777 | && ctx.word.length == 0 /* not word(... */ |
| 5778 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5779 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5780 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5781 | } |
| 5782 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5783 | /* fall through */ |
| 5784 | case '{': { |
| 5785 | int n = parse_group(&ctx, input, ch); |
| 5786 | if (n < 0) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5787 | goto parse_error_exitcode1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5788 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5789 | debug_printf_heredoc("parse_group done, needs heredocs:%d\n", n); |
| 5790 | heredoc_cnt += n; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5791 | goto new_cmd; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5792 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5793 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5794 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5795 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5796 | goto case_semi; |
| 5797 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5798 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5799 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5800 | /* proper use of this character is caught by end_trigger: |
| 5801 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5802 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5803 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5804 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5805 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5806 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5807 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5808 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5809 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5810 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5811 | |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5812 | parse_error_exitcode1: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5813 | G.last_exitcode = 1; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5814 | parse_error: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5815 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5816 | struct parse_context *pctx; |
| 5817 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5818 | |
| 5819 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5820 | * Sample for finding leaks on syntax error recovery path. |
| 5821 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5822 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5823 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5824 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5825 | * 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] | 5826 | */ |
| 5827 | pctx = &ctx; |
| 5828 | do { |
| 5829 | /* Update pipe/command counts, |
| 5830 | * otherwise freeing may miss some */ |
| 5831 | done_pipe(pctx, PIPE_SEQ); |
| 5832 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5833 | pctx->list_head, pctx); |
| 5834 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5835 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5836 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5837 | #if !BB_MMU |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5838 | o_free(&pctx->as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5839 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5840 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5841 | if (pctx != &ctx) { |
| 5842 | free(pctx); |
| 5843 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5844 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5845 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5846 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5847 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5848 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5849 | if (pstring) |
| 5850 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5851 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5852 | debug_leave(); |
| 5853 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5854 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5855 | } |
| 5856 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5857 | |
| 5858 | /*** Execution routines ***/ |
| 5859 | |
| 5860 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5861 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5862 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5863 | expand_string_to_string(str) |
| 5864 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5865 | 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] | 5866 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5867 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5868 | #endif |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 5869 | 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] | 5870 | |
| 5871 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5872 | * all variable references within and returns a pointer to |
| 5873 | * a list of expanded strings, possibly with larger number |
| 5874 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5875 | * This new list is allocated as a single malloc block. |
| 5876 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5877 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5878 | * Caller can deallocate entire list by single free(list). */ |
| 5879 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5880 | /* A horde of its helpers come first: */ |
| 5881 | |
| 5882 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5883 | { |
| 5884 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5885 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5886 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5887 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5888 | if (c == '{' || c == '}') { |
| 5889 | /* { -> \{, } -> \} */ |
| 5890 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5891 | /* And now we want to add { or } and continue: |
| 5892 | * o_addchr(o, c); |
| 5893 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5894 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5895 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5896 | } |
| 5897 | #endif |
| 5898 | o_addchr(o, c); |
| 5899 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5900 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5901 | o_addchr(o, '\\'); |
| 5902 | if (len) { |
| 5903 | len--; |
| 5904 | o_addchr(o, '\\'); |
| 5905 | o_addchr(o, *str++); |
| 5906 | } |
| 5907 | } |
| 5908 | } |
| 5909 | } |
| 5910 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5911 | /* Store given string, finalizing the word and starting new one whenever |
| 5912 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5913 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5914 | * Return in output->ended_in_ifs: |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5915 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5916 | */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5917 | 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] | 5918 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5919 | int last_is_ifs = 0; |
| 5920 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5921 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5922 | int word_len; |
| 5923 | |
| 5924 | if (!*str) /* EOL - do not finalize word */ |
| 5925 | break; |
| 5926 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5927 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5928 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5929 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5930 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5931 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5932 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5933 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5934 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5935 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5936 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5937 | /*/ Why can't we do it easier? */ |
| 5938 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5939 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5940 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5941 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5942 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5943 | if (!*str) /* EOL - do not finalize word */ |
| 5944 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5945 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5946 | |
| 5947 | /* We know str here points to at least one IFS char */ |
| 5948 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5949 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5950 | if (!*str) /* EOL - do not finalize word */ |
| 5951 | break; |
| 5952 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5953 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 5954 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 5955 | ) { |
| 5956 | /* This is a non-whitespace $IFS char */ |
| 5957 | /* Skip it and IFS whitespace chars, start new word */ |
| 5958 | str++; |
| 5959 | str += strspn(str, G.ifs_whitespace); |
| 5960 | goto new_word; |
| 5961 | } |
| 5962 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5963 | /* Start new word... but not always! */ |
| 5964 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5965 | if (output->has_quoted_part |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5966 | /* |
| 5967 | * Case "v=' a'; echo $v": |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5968 | * here nothing precedes the space in $v expansion, |
| 5969 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5970 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5971 | * It's okay if this accesses the byte before first argv[]: |
| 5972 | * past call to o_save_ptr() cleared it to zero byte |
| 5973 | * (grep for -prev-ifs-check-). |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5974 | */ |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 5975 | || output->data[output->length - 1] |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5976 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5977 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5978 | o_addchr(output, '\0'); |
| 5979 | debug_print_list("expand_on_ifs", output, n); |
| 5980 | n = o_save_ptr(output, n); |
| 5981 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5982 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5983 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5984 | output->ended_in_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5985 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5986 | return n; |
| 5987 | } |
| 5988 | |
| 5989 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5990 | * they are in double quotes, with the exception that they are not :). |
| 5991 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5992 | * |
| 5993 | * Returns malloced string. |
| 5994 | * As an optimization, we return NULL if expansion is not needed. |
| 5995 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5996 | static char *encode_then_expand_string(const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5997 | { |
| 5998 | char *exp_str; |
| 5999 | struct in_str input; |
| 6000 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6001 | const char *cp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6002 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6003 | cp = str; |
| 6004 | for (;;) { |
| 6005 | if (!*cp) return NULL; /* string has no special chars */ |
| 6006 | if (*cp == '$') break; |
| 6007 | if (*cp == '\\') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6008 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6009 | if (*cp == '`') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6010 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6011 | cp++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6012 | } |
| 6013 | |
| 6014 | /* We need to expand. Example: |
| 6015 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 6016 | */ |
| 6017 | setup_string_in_str(&input, str); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6018 | encode_string(NULL, &dest, &input, EOF); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 6019 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6020 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6021 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6022 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6023 | /*unbackslash:*/ 1 |
| 6024 | ); |
| 6025 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6026 | o_free(&dest); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6027 | return exp_str; |
| 6028 | } |
| 6029 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6030 | static const char *first_special_char_in_vararg(const char *cp) |
| 6031 | { |
| 6032 | for (;;) { |
| 6033 | if (!*cp) return NULL; /* string has no special chars */ |
| 6034 | if (*cp == '$') return cp; |
| 6035 | if (*cp == '\\') return cp; |
| 6036 | if (*cp == '\'') return cp; |
| 6037 | if (*cp == '"') return cp; |
| 6038 | #if ENABLE_HUSH_TICK |
| 6039 | if (*cp == '`') return cp; |
| 6040 | #endif |
| 6041 | /* dquoted "${x:+ARG}" should not glob, therefore |
| 6042 | * '*' et al require some non-literal processing: */ |
| 6043 | if (*cp == '*') return cp; |
| 6044 | if (*cp == '?') return cp; |
| 6045 | if (*cp == '[') return cp; |
| 6046 | cp++; |
| 6047 | } |
| 6048 | } |
| 6049 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6050 | /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}. |
| 6051 | * These can contain single- and double-quoted strings, |
| 6052 | * and treated as if the ARG string is initially unquoted. IOW: |
| 6053 | * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be |
| 6054 | * a dquoted string: "${var#"zz"}"), the difference only comes later |
| 6055 | * (word splitting and globbing of the ${var...} result). |
| 6056 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6057 | #if !BASH_PATTERN_SUBST |
| 6058 | #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ |
| 6059 | encode_then_expand_vararg(str, handle_squotes) |
| 6060 | #endif |
| 6061 | static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash) |
| 6062 | { |
Denys Vlasenko | 3d27d43 | 2018-12-27 18:03:20 +0100 | [diff] [blame] | 6063 | #if !BASH_PATTERN_SUBST && ENABLE_HUSH_CASE |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6064 | const int do_unbackslash = 0; |
| 6065 | #endif |
| 6066 | char *exp_str; |
| 6067 | struct in_str input; |
| 6068 | o_string dest = NULL_O_STRING; |
| 6069 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6070 | if (!first_special_char_in_vararg(str)) { |
| 6071 | /* string has no special chars */ |
| 6072 | return NULL; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6073 | } |
| 6074 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6075 | setup_string_in_str(&input, str); |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 6076 | dest.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6077 | exp_str = NULL; |
| 6078 | |
| 6079 | for (;;) { |
| 6080 | int ch; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6081 | |
| 6082 | ch = i_getch(&input); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6083 | debug_printf_parse("%s: ch=%c (%d) escape=%d\n", |
| 6084 | __func__, ch, ch, !!dest.o_expflags); |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6085 | |
| 6086 | if (!dest.o_expflags) { |
| 6087 | if (ch == EOF) |
| 6088 | break; |
| 6089 | if (handle_squotes && ch == '\'') { |
| 6090 | if (!add_till_single_quote_dquoted(&dest, &input)) |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6091 | goto ret; /* error */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6092 | continue; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6093 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6094 | } |
| 6095 | if (ch == EOF) { |
| 6096 | syntax_error_unterm_ch('"'); |
| 6097 | goto ret; /* error */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6098 | } |
| 6099 | if (ch == '"') { |
| 6100 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
| 6101 | continue; |
| 6102 | } |
| 6103 | if (ch == '\\') { |
| 6104 | ch = i_getch(&input); |
| 6105 | if (ch == EOF) { |
| 6106 | //example? error message? syntax_error_unterm_ch('"'); |
| 6107 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6108 | goto ret; |
| 6109 | } |
| 6110 | o_addqchr(&dest, ch); |
| 6111 | continue; |
| 6112 | } |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6113 | if (ch == '$') { |
| 6114 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) { |
| 6115 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6116 | goto ret; |
| 6117 | } |
| 6118 | continue; |
| 6119 | } |
| 6120 | #if ENABLE_HUSH_TICK |
| 6121 | if (ch == '`') { |
| 6122 | //unsigned pos = dest->length; |
| 6123 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6124 | o_addchr(&dest, 0x80 | '`'); |
| 6125 | if (!add_till_backquote(&dest, &input, |
| 6126 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6127 | ) |
| 6128 | ) { |
| 6129 | goto ret; /* error */ |
| 6130 | } |
| 6131 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6132 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6133 | continue; |
| 6134 | } |
| 6135 | #endif |
| 6136 | o_addQchr(&dest, ch); |
| 6137 | } /* for (;;) */ |
| 6138 | |
| 6139 | debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data); |
| 6140 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6141 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 6142 | do_unbackslash |
| 6143 | ); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6144 | ret: |
| 6145 | debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6146 | o_free(&dest); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6147 | return exp_str; |
| 6148 | } |
| 6149 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6150 | /* Expanding ARG in ${var+ARG}, ${var-ARG} |
| 6151 | */ |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6152 | static int encode_then_append_var_plusminus(o_string *output, int n, |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6153 | char *str, int dquoted) |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6154 | { |
| 6155 | struct in_str input; |
| 6156 | o_string dest = NULL_O_STRING; |
| 6157 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6158 | if (!first_special_char_in_vararg(str) |
| 6159 | && '\0' == str[strcspn(str, G.ifs)] |
| 6160 | ) { |
| 6161 | /* string has no special chars |
| 6162 | * && string has no $IFS chars |
| 6163 | */ |
Denys Vlasenko | 9e0adb9 | 2019-05-15 13:39:19 +0200 | [diff] [blame] | 6164 | if (dquoted) { |
| 6165 | /* Prints 1 (quoted expansion is a "" word, not nothing): |
| 6166 | * set -- "${notexist-}"; echo $# |
| 6167 | */ |
| 6168 | output->has_quoted_part = 1; |
| 6169 | } |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6170 | return expand_vars_to_list(output, n, str); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6171 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6172 | |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6173 | setup_string_in_str(&input, str); |
| 6174 | |
| 6175 | for (;;) { |
| 6176 | int ch; |
| 6177 | |
| 6178 | ch = i_getch(&input); |
| 6179 | debug_printf_parse("%s: ch=%c (%d) escape=%x\n", |
| 6180 | __func__, ch, ch, dest.o_expflags); |
| 6181 | |
| 6182 | if (!dest.o_expflags) { |
| 6183 | if (ch == EOF) |
| 6184 | break; |
| 6185 | if (!dquoted && strchr(G.ifs, ch)) { |
| 6186 | /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word. |
| 6187 | * do not assume we are at the start of the word (PREFIX above). |
| 6188 | */ |
| 6189 | if (dest.data) { |
| 6190 | n = expand_vars_to_list(output, n, dest.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6191 | o_free_and_set_NULL(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6192 | o_addchr(output, '\0'); |
| 6193 | n = o_save_ptr(output, n); /* create next word */ |
| 6194 | } else |
| 6195 | if (output->length != o_get_last_ptr(output, n) |
| 6196 | || output->has_quoted_part |
| 6197 | ) { |
| 6198 | /* For these cases: |
| 6199 | * f() { for i; do echo "|$i|"; done; }; x=x |
| 6200 | * f a${x:+ }b # 1st condition |
| 6201 | * |a| |
| 6202 | * |b| |
| 6203 | * f ""${x:+ }b # 2nd condition |
| 6204 | * || |
| 6205 | * |b| |
| 6206 | */ |
| 6207 | o_addchr(output, '\0'); |
| 6208 | n = o_save_ptr(output, n); /* create next word */ |
| 6209 | } |
| 6210 | continue; |
| 6211 | } |
| 6212 | if (!dquoted && ch == '\'') { |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6213 | if (!add_till_single_quote_dquoted(&dest, &input)) |
| 6214 | goto ret; /* error */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6215 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6216 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6217 | continue; |
| 6218 | } |
| 6219 | } |
| 6220 | if (ch == EOF) { |
| 6221 | syntax_error_unterm_ch('"'); |
| 6222 | goto ret; /* error */ |
| 6223 | } |
| 6224 | if (ch == '"') { |
| 6225 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6226 | if (dest.o_expflags) { |
| 6227 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6228 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6229 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6230 | continue; |
| 6231 | } |
| 6232 | if (ch == '\\') { |
| 6233 | ch = i_getch(&input); |
| 6234 | if (ch == EOF) { |
| 6235 | //example? error message? syntax_error_unterm_ch('"'); |
| 6236 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6237 | goto ret; |
| 6238 | } |
| 6239 | o_addqchr(&dest, ch); |
| 6240 | continue; |
| 6241 | } |
| 6242 | if (ch == '$') { |
| 6243 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) { |
| 6244 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6245 | goto ret; |
| 6246 | } |
| 6247 | continue; |
| 6248 | } |
| 6249 | #if ENABLE_HUSH_TICK |
| 6250 | if (ch == '`') { |
| 6251 | //unsigned pos = dest->length; |
| 6252 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6253 | o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`'); |
| 6254 | if (!add_till_backquote(&dest, &input, |
| 6255 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6256 | ) |
| 6257 | ) { |
| 6258 | goto ret; /* error */ |
| 6259 | } |
| 6260 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6261 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6262 | continue; |
| 6263 | } |
| 6264 | #endif |
Denys Vlasenko | f36caa4 | 2018-07-20 19:29:41 +0200 | [diff] [blame] | 6265 | if (dquoted) { |
| 6266 | /* Always glob-protect if in dquotes: |
| 6267 | * x=x; echo "${x:+/bin/c*}" - prints: /bin/c* |
| 6268 | * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c* |
| 6269 | */ |
| 6270 | o_addqchr(&dest, ch); |
| 6271 | } else { |
| 6272 | /* Glob-protect only if char is quoted: |
| 6273 | * x=x; echo ${x:+/bin/c*} - prints many filenames |
| 6274 | * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c* |
| 6275 | */ |
| 6276 | o_addQchr(&dest, ch); |
| 6277 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6278 | } /* for (;;) */ |
| 6279 | |
| 6280 | if (dest.data) { |
| 6281 | n = expand_vars_to_list(output, n, dest.data); |
| 6282 | } |
| 6283 | ret: |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6284 | o_free(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6285 | return n; |
| 6286 | } |
| 6287 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6288 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6289 | 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] | 6290 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6291 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6292 | arith_t res; |
| 6293 | char *exp_str; |
| 6294 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6295 | math_state.lookupvar = get_local_var_value; |
| 6296 | math_state.setvar = set_local_var_from_halves; |
| 6297 | //math_state.endofname = endofname; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6298 | exp_str = encode_then_expand_string(arg); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6299 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6300 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6301 | if (errmsg_p) |
| 6302 | *errmsg_p = math_state.errmsg; |
| 6303 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6304 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6305 | return res; |
| 6306 | } |
| 6307 | #endif |
| 6308 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6309 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6310 | /* ${var/[/]pattern[/repl]} helpers */ |
| 6311 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 6312 | { |
| 6313 | while (1) { |
| 6314 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 6315 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 6316 | if (end) { |
| 6317 | *size = end - val; |
| 6318 | return val; |
| 6319 | } |
| 6320 | if (*val == '\0') |
| 6321 | return NULL; |
| 6322 | /* Optimization: if "*pat" did not match the start of "string", |
| 6323 | * we know that "tring", "ring" etc will not match too: |
| 6324 | */ |
| 6325 | if (pattern[0] == '*') |
| 6326 | return NULL; |
| 6327 | val++; |
| 6328 | } |
| 6329 | } |
| 6330 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 6331 | { |
| 6332 | char *result = NULL; |
| 6333 | unsigned res_len = 0; |
| 6334 | unsigned repl_len = strlen(repl); |
| 6335 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6336 | /* Null pattern never matches, including if "var" is empty */ |
| 6337 | if (!pattern[0]) |
| 6338 | return result; /* NULL, no replaces happened */ |
| 6339 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6340 | while (1) { |
| 6341 | int size; |
| 6342 | char *s = strstr_pattern(val, pattern, &size); |
| 6343 | if (!s) |
| 6344 | break; |
| 6345 | |
| 6346 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6347 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 6348 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6349 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 6350 | |
| 6351 | val = s + size; |
| 6352 | if (exp_op == '/') |
| 6353 | break; |
| 6354 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6355 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6356 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 6357 | strcpy(result + res_len, val); |
| 6358 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 6359 | } |
| 6360 | debug_printf_varexp("result:'%s'\n", result); |
| 6361 | return result; |
| 6362 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6363 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6364 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6365 | static int append_str_maybe_ifs_split(o_string *output, int n, |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6366 | int first_ch, const char *val) |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6367 | { |
| 6368 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 6369 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6370 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6371 | if (val && val[0]) |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6372 | n = expand_on_ifs(output, n, val); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6373 | } else { /* quoted "$VAR" */ |
| 6374 | output->has_quoted_part = 1; |
| 6375 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6376 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6377 | if (val && val[0]) |
| 6378 | o_addQstr(output, val); |
| 6379 | } |
| 6380 | return n; |
| 6381 | } |
| 6382 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6383 | /* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6384 | */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6385 | static NOINLINE int expand_one_var(o_string *output, int n, |
| 6386 | int first_ch, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6387 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6388 | const char *val; |
| 6389 | char *to_be_freed; |
| 6390 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6391 | char *var; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6392 | char exp_op; |
| 6393 | char exp_save = exp_save; /* for compiler */ |
| 6394 | char *exp_saveptr; /* points to expansion operator */ |
| 6395 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6396 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6397 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6398 | val = NULL; |
| 6399 | to_be_freed = NULL; |
| 6400 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6401 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6402 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6403 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6404 | arg0 = arg[0]; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6405 | arg[0] = (arg0 & 0x7f); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6406 | exp_op = 0; |
| 6407 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6408 | if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 6409 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 6410 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 6411 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6412 | ) { |
| 6413 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6414 | var++; |
| 6415 | exp_op = 'L'; |
| 6416 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6417 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6418 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6419 | && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6420 | ) { |
| 6421 | /* ${?:0}, ${#[:]%0} etc */ |
| 6422 | exp_saveptr = var + 1; |
| 6423 | } else { |
| 6424 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 6425 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 6426 | } |
| 6427 | exp_op = exp_save = *exp_saveptr; |
| 6428 | if (exp_op) { |
| 6429 | exp_word = exp_saveptr + 1; |
| 6430 | if (exp_op == ':') { |
| 6431 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6432 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6433 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6434 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6435 | ) { |
| 6436 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 6437 | exp_op = ':'; |
| 6438 | exp_word--; |
| 6439 | } |
| 6440 | } |
| 6441 | *exp_saveptr = '\0'; |
| 6442 | } /* else: it's not an expansion op, but bare ${var} */ |
| 6443 | } |
| 6444 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6445 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6446 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 6447 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6448 | int nn = xatoi_positive(var); |
| 6449 | if (nn < G.global_argc) |
| 6450 | val = G.global_argv[nn]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6451 | /* else val remains NULL: $N with too big N */ |
| 6452 | } else { |
| 6453 | switch (var[0]) { |
| 6454 | case '$': /* pid */ |
| 6455 | val = utoa(G.root_pid); |
| 6456 | break; |
| 6457 | case '!': /* bg pid */ |
| 6458 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 6459 | break; |
| 6460 | case '?': /* exitcode */ |
| 6461 | val = utoa(G.last_exitcode); |
| 6462 | break; |
| 6463 | case '#': /* argc */ |
| 6464 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 6465 | break; |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 6466 | case '-': { /* active options */ |
| 6467 | /* Check set_mode() to see what option chars we support */ |
| 6468 | char *cp; |
| 6469 | val = cp = G.optstring_buf; |
| 6470 | if (G.o_opt[OPT_O_ERREXIT]) |
| 6471 | *cp++ = 'e'; |
| 6472 | if (G_interactive_fd) |
| 6473 | *cp++ = 'i'; |
| 6474 | if (G_x_mode) |
| 6475 | *cp++ = 'x'; |
| 6476 | /* If G.o_opt[OPT_O_NOEXEC] is true, |
| 6477 | * commands read but are not executed, |
| 6478 | * so $- can not execute too, 'n' is never seen in $-. |
| 6479 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 6480 | if (G.opt_c) |
| 6481 | *cp++ = 'c'; |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 6482 | if (G.opt_s) |
| 6483 | *cp++ = 's'; |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 6484 | *cp = '\0'; |
| 6485 | break; |
| 6486 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6487 | default: |
| 6488 | val = get_local_var_value(var); |
| 6489 | } |
| 6490 | } |
| 6491 | |
| 6492 | /* Handle any expansions */ |
| 6493 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6494 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6495 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6496 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6497 | debug_printf_expand("%s\n", val); |
| 6498 | } else if (exp_op) { |
| 6499 | if (exp_op == '%' || exp_op == '#') { |
| 6500 | /* Standard-mandated substring removal ops: |
| 6501 | * ${parameter%word} - remove smallest suffix pattern |
| 6502 | * ${parameter%%word} - remove largest suffix pattern |
| 6503 | * ${parameter#word} - remove smallest prefix pattern |
| 6504 | * ${parameter##word} - remove largest prefix pattern |
| 6505 | * |
| 6506 | * Word is expanded to produce a glob pattern. |
| 6507 | * Then var's value is matched to it and matching part removed. |
| 6508 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6509 | //FIXME: ${x#...${...}...} |
| 6510 | //should evaluate inner ${...} even if x is "" and no shrinking of it is possible - |
| 6511 | //inner ${...} may have side effects! |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6512 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6513 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6514 | char *exp_exp_word; |
| 6515 | char *loc; |
| 6516 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 6517 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6518 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6519 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6520 | 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] | 6521 | if (exp_exp_word) |
| 6522 | exp_word = exp_exp_word; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6523 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
| 6524 | /* |
| 6525 | * HACK ALERT. We depend here on the fact that |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6526 | * G.global_argv and results of utoa and get_local_var_value |
| 6527 | * are actually in writable memory: |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6528 | * scan_and_match momentarily stores NULs there. |
| 6529 | */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6530 | t = (char*)val; |
| 6531 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6532 | 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] | 6533 | free(exp_exp_word); |
| 6534 | if (loc) { /* match was found */ |
| 6535 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6536 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6537 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6538 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6539 | } |
| 6540 | } |
| 6541 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6542 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6543 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6544 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6545 | * Note that in encoded form it has TWO parts: |
| 6546 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6547 | * and if // is used, it is encoded as \: |
| 6548 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6549 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6550 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6551 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6552 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6553 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6554 | * >az >bz |
| 6555 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6556 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6557 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6558 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6559 | * (note that a*z _pattern_ is never globbed!) |
| 6560 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6561 | char *pattern, *repl, *t; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6562 | pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6563 | if (!pattern) |
| 6564 | pattern = xstrdup(exp_word); |
| 6565 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6566 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6567 | exp_word = p; |
| 6568 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6569 | *p = '\0'; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6570 | repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6571 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6572 | /* HACK ALERT. We depend here on the fact that |
| 6573 | * G.global_argv and results of utoa and get_local_var_value |
| 6574 | * are actually in writable memory: |
| 6575 | * replace_pattern momentarily stores NULs there. */ |
| 6576 | t = (char*)val; |
| 6577 | to_be_freed = replace_pattern(t, |
| 6578 | pattern, |
| 6579 | (repl ? repl : exp_word), |
| 6580 | exp_op); |
| 6581 | if (to_be_freed) /* at least one replace happened */ |
| 6582 | val = to_be_freed; |
| 6583 | free(pattern); |
| 6584 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6585 | } else { |
| 6586 | /* Empty variable always gives nothing */ |
| 6587 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6588 | /* Just skip "replace" part */ |
| 6589 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6590 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6591 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6592 | } |
| 6593 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6594 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6595 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6596 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6597 | /* It's ${var:N[:M]} bashism. |
| 6598 | * Note that in encoded form it has TWO parts: |
| 6599 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6600 | */ |
| 6601 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6602 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6603 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6604 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6605 | if (errmsg) |
| 6606 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6607 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6608 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6609 | exp_word = p; |
| 6610 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6611 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6612 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6613 | if (errmsg) |
| 6614 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6615 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6616 | if (beg < 0) { |
| 6617 | /* negative beg counts from the end */ |
| 6618 | beg = (arith_t)strlen(val) + beg; |
| 6619 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6620 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6621 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6622 | debug_printf_varexp("from val:'%s'\n", val); |
| 6623 | if (len < 0) { |
| 6624 | /* in bash, len=-n means strlen()-n */ |
| 6625 | len = (arith_t)strlen(val) - beg + len; |
| 6626 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6627 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6628 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6629 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6630 | arith_err: |
| 6631 | val = NULL; |
| 6632 | } else { |
| 6633 | /* Paranoia. What if user entered 9999999999999 |
| 6634 | * which fits in arith_t but not int? */ |
| 6635 | if (len >= INT_MAX) |
| 6636 | len = INT_MAX; |
| 6637 | val = to_be_freed = xstrndup(val + beg, len); |
| 6638 | } |
| 6639 | debug_printf_varexp("val:'%s'\n", val); |
| 6640 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6641 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6642 | val = NULL; |
| 6643 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6644 | } else { /* one of "-=+?" */ |
| 6645 | /* Standard-mandated substitution ops: |
| 6646 | * ${var?word} - indicate error if unset |
| 6647 | * If var is unset, word (or a message indicating it is unset |
| 6648 | * if word is null) is written to standard error |
| 6649 | * and the shell exits with a non-zero exit status. |
| 6650 | * Otherwise, the value of var is substituted. |
| 6651 | * ${var-word} - use default value |
| 6652 | * If var is unset, word is substituted. |
| 6653 | * ${var=word} - assign and use default value |
| 6654 | * If var is unset, word is assigned to var. |
| 6655 | * In all cases, final value of var is substituted. |
| 6656 | * ${var+word} - use alternative value |
| 6657 | * If var is unset, null is substituted. |
| 6658 | * Otherwise, word is substituted. |
| 6659 | * |
| 6660 | * Word is subjected to tilde expansion, parameter expansion, |
| 6661 | * command substitution, and arithmetic expansion. |
| 6662 | * If word is not needed, it is not expanded. |
| 6663 | * |
| 6664 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6665 | * but also treat null var as if it is unset. |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6666 | * |
| 6667 | * Word-splitting and single quote behavior: |
| 6668 | * |
| 6669 | * $ f() { for i; do echo "|$i|"; done; }; |
| 6670 | * |
| 6671 | * $ x=; f ${x:?'x y' z} |
| 6672 | * bash: x: x y z #BUG: does not abort, ${} results in empty expansion |
| 6673 | * $ x=; f "${x:?'x y' z}" |
| 6674 | * bash: x: x y z # dash prints: dash: x: 'x y' z #BUG: does not abort, ${} results in "" |
| 6675 | * |
| 6676 | * $ x=; f ${x:='x y' z} |
| 6677 | * |x| |
| 6678 | * |y| |
| 6679 | * |z| |
| 6680 | * $ x=; f "${x:='x y' z}" |
| 6681 | * |'x y' z| |
| 6682 | * |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6683 | * $ x=x; f ${x:+'x y' z} |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6684 | * |x y| |
| 6685 | * |z| |
| 6686 | * $ x=x; f "${x:+'x y' z}" |
| 6687 | * |'x y' z| |
| 6688 | * |
| 6689 | * $ x=; f ${x:-'x y' z} |
| 6690 | * |x y| |
| 6691 | * |z| |
| 6692 | * $ x=; f "${x:-'x y' z}" |
| 6693 | * |'x y' z| |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6694 | */ |
| 6695 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6696 | if (exp_op == '+') |
| 6697 | use_word = !use_word; |
| 6698 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6699 | (exp_save == ':') ? "true" : "false", use_word); |
| 6700 | if (use_word) { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6701 | if (exp_op == '+' || exp_op == '-') { |
| 6702 | /* ${var+word} - use alternative value */ |
| 6703 | /* ${var-word} - use default value */ |
| 6704 | n = encode_then_append_var_plusminus(output, n, exp_word, |
| 6705 | /*dquoted:*/ (arg0 & 0x80) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6706 | ); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6707 | val = NULL; |
| 6708 | } else { |
| 6709 | /* ${var?word} - indicate error if unset */ |
| 6710 | /* ${var=word} - assign and use default value */ |
| 6711 | to_be_freed = encode_then_expand_vararg(exp_word, |
| 6712 | /*handle_squotes:*/ !(arg0 & 0x80), |
| 6713 | /*unbackslash:*/ 0 |
| 6714 | ); |
| 6715 | if (to_be_freed) |
| 6716 | exp_word = to_be_freed; |
| 6717 | if (exp_op == '?') { |
| 6718 | /* mimic bash message */ |
| 6719 | msg_and_die_if_script("%s: %s", |
| 6720 | var, |
| 6721 | exp_word[0] |
| 6722 | ? exp_word |
| 6723 | : "parameter null or not set" |
| 6724 | /* ash has more specific messages, a-la: */ |
| 6725 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
| 6726 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6727 | //TODO: how interactive bash aborts expansion mid-command? |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6728 | //It aborts the entire line, returns to prompt: |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6729 | // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO |
| 6730 | // bash: x: x y z |
| 6731 | // $ |
| 6732 | // ("echo YO" is not executed, neither the f function call) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6733 | } else { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6734 | val = exp_word; |
| 6735 | } |
| 6736 | if (exp_op == '=') { |
| 6737 | /* ${var=[word]} or ${var:=[word]} */ |
| 6738 | if (isdigit(var[0]) || var[0] == '#') { |
| 6739 | /* mimic bash message */ |
| 6740 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
| 6741 | val = NULL; |
| 6742 | } else { |
| 6743 | char *new_var = xasprintf("%s=%s", var, val); |
| 6744 | set_local_var(new_var, /*flag:*/ 0); |
| 6745 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6746 | } |
| 6747 | } |
| 6748 | } |
| 6749 | } /* one of "-=+?" */ |
| 6750 | |
| 6751 | *exp_saveptr = exp_save; |
| 6752 | } /* if (exp_op) */ |
| 6753 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6754 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6755 | *pp = p; |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6756 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6757 | n = append_str_maybe_ifs_split(output, n, first_ch, val); |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6758 | |
| 6759 | free(to_be_freed); |
| 6760 | return n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | /* Expand all variable references in given string, adding words to list[] |
| 6764 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6765 | * to be filled). This routine is extremely tricky: has to deal with |
| 6766 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6767 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6768 | 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] | 6769 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6770 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6771 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6772 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6773 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6774 | char *p; |
| 6775 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6776 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6777 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6778 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6779 | |
| 6780 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6781 | char first_ch; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6782 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6783 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6784 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6785 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6786 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6787 | o_addchr(output, '\0'); |
| 6788 | n = o_save_ptr(output, n); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6789 | output->ended_in_ifs = 0; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6790 | } |
| 6791 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6792 | o_addblock(output, arg, p - arg); |
| 6793 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6794 | arg = ++p; |
| 6795 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6796 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6797 | /* Fetch special var name (if it is indeed one of them) |
| 6798 | * and quote bit, force the bit on if singleword expansion - |
| 6799 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6800 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6801 | |
| 6802 | /* Is this variable quoted and thus expansion can't be null? |
| 6803 | * "$@" is special. Even if quoted, it can still |
| 6804 | * expand to nothing (not even an empty string), |
| 6805 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6806 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6807 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6808 | |
| 6809 | switch (first_ch & 0x7f) { |
| 6810 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6811 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6812 | case '@': { |
| 6813 | int i; |
| 6814 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6815 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6816 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6817 | 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] | 6818 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6819 | while (G.global_argv[i]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6820 | n = expand_on_ifs(output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6821 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6822 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6823 | /* this argv[] is not empty and not last: |
| 6824 | * put terminating NUL, start new word */ |
| 6825 | o_addchr(output, '\0'); |
| 6826 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6827 | n = o_save_ptr(output, n); |
| 6828 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6829 | } |
| 6830 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6831 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6832 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6833 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6834 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6835 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6836 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6837 | while (1) { |
| 6838 | o_addQstr(output, G.global_argv[i]); |
| 6839 | if (++i >= G.global_argc) |
| 6840 | break; |
| 6841 | o_addchr(output, '\0'); |
| 6842 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6843 | n = o_save_ptr(output, n); |
| 6844 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6845 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6846 | while (1) { |
| 6847 | o_addQstr(output, G.global_argv[i]); |
| 6848 | if (!G.global_argv[++i]) |
| 6849 | break; |
| 6850 | if (G.ifs[0]) |
| 6851 | o_addchr(output, G.ifs[0]); |
| 6852 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6853 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6854 | } |
| 6855 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6856 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6857 | case SPECIAL_VAR_SYMBOL: { |
| 6858 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6859 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6860 | output->has_quoted_part = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6861 | cant_be_null = 0x80; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6862 | arg++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6863 | break; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6864 | } |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6865 | case SPECIAL_VAR_QUOTED_SVS: |
| 6866 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6867 | /* "^C variable", represents literal ^C char (possible in scripts) */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6868 | o_addchr(output, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6869 | arg++; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6870 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6871 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6872 | case '`': { |
| 6873 | /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6874 | o_string subst_result = NULL_O_STRING; |
| 6875 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6876 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6877 | arg++; |
| 6878 | /* Can't just stuff it into output o_string, |
| 6879 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6880 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6881 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6882 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6883 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6884 | 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] | 6885 | 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] | 6886 | o_free(&subst_result); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6887 | break; |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6888 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6889 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6890 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6891 | case '+': { |
| 6892 | /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6893 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6894 | |
| 6895 | arg++; /* skip '+' */ |
| 6896 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6897 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6898 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6899 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6900 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 6901 | if (res < 0 |
| 6902 | && first_ch == (char)('+'|0x80) |
| 6903 | /* && (output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) */ |
| 6904 | ) { |
| 6905 | /* Quoted negative ariths, like filename[0"$((-9))"], |
| 6906 | * should not be interpreted as glob ranges. |
| 6907 | * Convert leading '-' to '\-': |
| 6908 | */ |
| 6909 | o_grow_by(output, 1); |
| 6910 | output->data[output->length++] = '\\'; |
| 6911 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6912 | o_addstr(output, arith_buf); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6913 | break; |
| 6914 | } |
| 6915 | #endif |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6916 | default: |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6917 | /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6918 | n = expand_one_var(output, n, first_ch, arg, &p); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6919 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6920 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6921 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6922 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6923 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6924 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6925 | *p = SPECIAL_VAR_SYMBOL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6926 | arg = ++p; |
| 6927 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6928 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6929 | if (*arg) { |
| 6930 | /* handle trailing string */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6931 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6932 | o_addchr(output, '\0'); |
| 6933 | n = o_save_ptr(output, n); |
| 6934 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6935 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6936 | /* this part is literal, and it was already pre-quoted |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6937 | * if needed (much earlier), do not use o_addQstr here! |
| 6938 | */ |
| 6939 | o_addstr(output, arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6940 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6941 | } else |
| 6942 | if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6943 | && !(cant_be_null & 0x80) /* and all vars were not quoted */ |
| 6944 | && !output->has_quoted_part |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6945 | ) { |
| 6946 | n--; |
| 6947 | /* allow to reuse list[n] later without re-growth */ |
| 6948 | output->has_empty_slot = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | return n; |
| 6952 | } |
| 6953 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6954 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6955 | { |
| 6956 | int n; |
| 6957 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6958 | o_string output = NULL_O_STRING; |
| 6959 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6960 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6961 | |
| 6962 | n = 0; |
Denys Vlasenko | 57235be | 2018-07-20 14:45:12 +0200 | [diff] [blame] | 6963 | for (;;) { |
| 6964 | /* go to next list[n] */ |
| 6965 | output.ended_in_ifs = 0; |
| 6966 | n = o_save_ptr(&output, n); |
| 6967 | |
| 6968 | if (!*argv) |
| 6969 | break; |
| 6970 | |
| 6971 | /* expand argv[i] */ |
| 6972 | n = expand_vars_to_list(&output, n, *argv++); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6973 | /* if (!output->has_empty_slot) -- need this?? */ |
| 6974 | o_addchr(&output, '\0'); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6975 | } |
| 6976 | debug_print_list("expand_variables", &output, n); |
| 6977 | |
| 6978 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6979 | list = o_finalize_list(&output, n); |
| 6980 | debug_print_strings("expand_variables[1]", list); |
| 6981 | return list; |
| 6982 | } |
| 6983 | |
| 6984 | static char **expand_strvec_to_strvec(char **argv) |
| 6985 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6986 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6987 | } |
| 6988 | |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 6989 | #if defined(CMD_SINGLEWORD_NOGLOB) || defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6990 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6991 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6992 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6993 | } |
| 6994 | #endif |
| 6995 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6996 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6997 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6998 | * |
| 6999 | * NB: should NOT do globbing! |
| 7000 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 7001 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7002 | 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] | 7003 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 7004 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 7005 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7006 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 7007 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7008 | char *argv[2], **list; |
| 7009 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7010 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7011 | /* This is generally an optimization, but it also |
| 7012 | * handles "", which otherwise trips over !list[0] check below. |
| 7013 | * (is this ever happens that we actually get str="" here?) |
| 7014 | */ |
| 7015 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 7016 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7017 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7018 | return xstrdup(str); |
| 7019 | } |
| 7020 | |
| 7021 | argv[0] = (char*)str; |
| 7022 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7023 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 7024 | if (!list[0]) { |
| 7025 | /* Example where it happens: |
| 7026 | * x=; echo ${x:-"$@"} |
| 7027 | */ |
| 7028 | ((char*)list)[0] = '\0'; |
| 7029 | } else { |
| 7030 | if (HUSH_DEBUG) |
| 7031 | if (list[1]) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 7032 | bb_simple_error_msg_and_die("BUG in varexp2"); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 7033 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 7034 | overlapping_strcpy((char*)list, list[0]); |
| 7035 | if (do_unbackslash) |
| 7036 | unbackslash((char*)list); |
| 7037 | } |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7038 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7039 | return (char*)list; |
| 7040 | } |
| 7041 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 7042 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7043 | static char* expand_strvec_to_string(char **argv) |
| 7044 | { |
| 7045 | char **list; |
| 7046 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 7047 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7048 | /* Convert all NULs to spaces */ |
| 7049 | if (list[0]) { |
| 7050 | int n = 1; |
| 7051 | while (list[n]) { |
| 7052 | if (HUSH_DEBUG) |
| 7053 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 7054 | bb_error_msg_and_die("BUG in varexp3"); |
| 7055 | /* bash uses ' ' regardless of $IFS contents */ |
| 7056 | list[n][-1] = ' '; |
| 7057 | n++; |
| 7058 | } |
| 7059 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 7060 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7061 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 7062 | return (char*)list; |
| 7063 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 7064 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7065 | |
| 7066 | static char **expand_assignments(char **argv, int count) |
| 7067 | { |
| 7068 | int i; |
| 7069 | char **p; |
| 7070 | |
| 7071 | G.expanded_assignments = p = NULL; |
| 7072 | /* Expand assignments into one string each */ |
| 7073 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7074 | p = add_string_to_strings(p, |
| 7075 | expand_string_to_string(argv[i], |
| 7076 | EXP_FLAG_ESC_GLOB_CHARS, |
| 7077 | /*unbackslash:*/ 1 |
| 7078 | ) |
| 7079 | ); |
| 7080 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7081 | } |
| 7082 | G.expanded_assignments = NULL; |
| 7083 | return p; |
| 7084 | } |
| 7085 | |
| 7086 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7087 | static void switch_off_special_sigs(unsigned mask) |
| 7088 | { |
| 7089 | unsigned sig = 0; |
| 7090 | while ((mask >>= 1) != 0) { |
| 7091 | sig++; |
| 7092 | if (!(mask & 1)) |
| 7093 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7094 | #if ENABLE_HUSH_TRAP |
| 7095 | if (G_traps) { |
| 7096 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7097 | /* trap is '', has to remain SIG_IGN */ |
| 7098 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7099 | free(G_traps[sig]); |
| 7100 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7101 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7102 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7103 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7104 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7105 | } |
| 7106 | } |
| 7107 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 7108 | #if BB_MMU |
| 7109 | /* never called */ |
| 7110 | void re_execute_shell(char ***to_free, const char *s, |
| 7111 | char *g_argv0, char **g_argv, |
| 7112 | char **builtin_argv) NORETURN; |
| 7113 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7114 | static void reset_traps_to_defaults(void) |
| 7115 | { |
| 7116 | /* This function is always called in a child shell |
| 7117 | * after fork (not vfork, NOMMU doesn't use this function). |
| 7118 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7119 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7120 | unsigned mask; |
| 7121 | |
| 7122 | /* Child shells are not interactive. |
| 7123 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 7124 | * Testcase: (while :; do :; done) + ^Z should background. |
| 7125 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 7126 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7127 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7128 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7129 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7130 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7131 | /* Switch off special sigs */ |
| 7132 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7133 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7134 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7135 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7136 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7137 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 7138 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7139 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7140 | # if ENABLE_HUSH_TRAP |
| 7141 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7142 | return; |
| 7143 | |
| 7144 | /* Reset all sigs to default except ones with empty traps */ |
| 7145 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7146 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7147 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7148 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7149 | continue; /* empty trap: has to remain SIG_IGN */ |
| 7150 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7151 | free(G_traps[sig]); |
| 7152 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7153 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7154 | if (sig == 0) |
| 7155 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7156 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7157 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7158 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | #else /* !BB_MMU */ |
| 7162 | |
| 7163 | static void re_execute_shell(char ***to_free, const char *s, |
| 7164 | char *g_argv0, char **g_argv, |
| 7165 | char **builtin_argv) NORETURN; |
| 7166 | static void re_execute_shell(char ***to_free, const char *s, |
| 7167 | char *g_argv0, char **g_argv, |
| 7168 | char **builtin_argv) |
| 7169 | { |
| 7170 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 7171 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 7172 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 7173 | char *heredoc_argv[4]; |
| 7174 | struct variable *cur; |
| 7175 | # if ENABLE_HUSH_FUNCTIONS |
| 7176 | struct function *funcp; |
| 7177 | # endif |
| 7178 | char **argv, **pp; |
| 7179 | unsigned cnt; |
| 7180 | unsigned long long empty_trap_mask; |
| 7181 | |
| 7182 | if (!g_argv0) { /* heredoc */ |
| 7183 | argv = heredoc_argv; |
| 7184 | argv[0] = (char *) G.argv0_for_re_execing; |
| 7185 | argv[1] = (char *) "-<"; |
| 7186 | argv[2] = (char *) s; |
| 7187 | argv[3] = NULL; |
| 7188 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 7189 | goto do_exec; |
| 7190 | } |
| 7191 | |
| 7192 | cnt = 0; |
| 7193 | pp = builtin_argv; |
| 7194 | if (pp) while (*pp++) |
| 7195 | cnt++; |
| 7196 | |
| 7197 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7198 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7199 | int sig; |
| 7200 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7201 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7202 | empty_trap_mask |= 1LL << sig; |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | sprintf(param_buf, NOMMU_HACK_FMT |
| 7207 | , (unsigned) G.root_pid |
| 7208 | , (unsigned) G.root_ppid |
| 7209 | , (unsigned) G.last_bg_pid |
| 7210 | , (unsigned) G.last_exitcode |
| 7211 | , cnt |
| 7212 | , empty_trap_mask |
| 7213 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 7214 | ); |
| 7215 | # undef NOMMU_HACK_FMT |
| 7216 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 7217 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 7218 | */ |
| 7219 | cnt += 6; |
| 7220 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7221 | if (!cur->flg_export || cur->flg_read_only) |
| 7222 | cnt += 2; |
| 7223 | } |
| 7224 | # if ENABLE_HUSH_FUNCTIONS |
| 7225 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 7226 | cnt += 3; |
| 7227 | # endif |
| 7228 | pp = g_argv; |
| 7229 | while (*pp++) |
| 7230 | cnt++; |
| 7231 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 7232 | *pp++ = (char *) G.argv0_for_re_execing; |
| 7233 | *pp++ = param_buf; |
| 7234 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7235 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 7236 | continue; |
| 7237 | if (cur->flg_read_only) { |
| 7238 | *pp++ = (char *) "-R"; |
| 7239 | *pp++ = cur->varstr; |
| 7240 | } else if (!cur->flg_export) { |
| 7241 | *pp++ = (char *) "-V"; |
| 7242 | *pp++ = cur->varstr; |
| 7243 | } |
| 7244 | } |
| 7245 | # if ENABLE_HUSH_FUNCTIONS |
| 7246 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 7247 | *pp++ = (char *) "-F"; |
| 7248 | *pp++ = funcp->name; |
| 7249 | *pp++ = funcp->body_as_string; |
| 7250 | } |
| 7251 | # endif |
| 7252 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 7253 | * |
| 7254 | * However, POSIX says that subshells reset signals with traps |
| 7255 | * to SIG_DFL. |
| 7256 | * I tested bash-3.2 and it not only does that with true subshells |
| 7257 | * of the form ( list ), but with any forked children shells. |
| 7258 | * I set trap "echo W" WINCH; and then tried: |
| 7259 | * |
| 7260 | * { echo 1; sleep 20; echo 2; } & |
| 7261 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 7262 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 7263 | * |
| 7264 | * In all these cases sending SIGWINCH to the child shell |
| 7265 | * did not run the trap. If I add trap "echo V" WINCH; |
| 7266 | * _inside_ group (just before echo 1), it works. |
| 7267 | * |
| 7268 | * 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] | 7269 | */ |
| 7270 | *pp++ = (char *) "-c"; |
| 7271 | *pp++ = (char *) s; |
| 7272 | if (builtin_argv) { |
| 7273 | while (*++builtin_argv) |
| 7274 | *pp++ = *builtin_argv; |
| 7275 | *pp++ = (char *) ""; |
| 7276 | } |
| 7277 | *pp++ = g_argv0; |
| 7278 | while (*g_argv) |
| 7279 | *pp++ = *g_argv++; |
| 7280 | /* *pp = NULL; - is already there */ |
| 7281 | pp = environ; |
| 7282 | |
| 7283 | do_exec: |
| 7284 | 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] | 7285 | /* Don't propagate SIG_IGN to the child */ |
| 7286 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7287 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7288 | execve(bb_busybox_exec_path, argv, pp); |
| 7289 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 7290 | if (argv[0][0] == '/') |
| 7291 | execve(argv[0], argv, pp); |
| 7292 | xfunc_error_retval = 127; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 7293 | bb_simple_error_msg_and_die("can't re-execute the shell"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7294 | } |
| 7295 | #endif /* !BB_MMU */ |
| 7296 | |
| 7297 | |
| 7298 | static int run_and_free_list(struct pipe *pi); |
| 7299 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7300 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7301 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 7302 | * end_trigger controls how often we stop parsing |
| 7303 | * NUL: parse all, execute, return |
| 7304 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 7305 | */ |
| 7306 | 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] | 7307 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7308 | /* Why we need empty flag? |
| 7309 | * An obscure corner case "false; ``; echo $?": |
| 7310 | * empty command in `` should still set $? to 0. |
| 7311 | * But we can't just set $? to 0 at the start, |
| 7312 | * this breaks "false; echo `echo $?`" case. |
| 7313 | */ |
| 7314 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7315 | while (1) { |
| 7316 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 7317 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7318 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 7319 | if (end_trigger == ';') { |
| 7320 | G.promptmode = 0; /* PS1 */ |
| 7321 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 7322 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7323 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 7324 | pipe_list = parse_stream(NULL, NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 7325 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 7326 | /* If we are in "big" script |
| 7327 | * (not in `cmd` or something similar)... |
| 7328 | */ |
| 7329 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 7330 | /* Discard cached input (rest of line) */ |
| 7331 | int ch = inp->last_char; |
| 7332 | while (ch != EOF && ch != '\n') { |
| 7333 | //bb_error_msg("Discarded:'%c'", ch); |
| 7334 | ch = i_getch(inp); |
| 7335 | } |
| 7336 | /* Force prompt */ |
| 7337 | inp->p = NULL; |
| 7338 | /* This stream isn't empty */ |
| 7339 | empty = 0; |
| 7340 | continue; |
| 7341 | } |
| 7342 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7343 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7344 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7345 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7346 | debug_print_tree(pipe_list, 0); |
| 7347 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7348 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7349 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7350 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 7351 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7352 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7355 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7356 | { |
| 7357 | struct in_str input; |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7358 | //IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7359 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7360 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7361 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7362 | //IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7365 | static void parse_and_run_file(HFILE *fp) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7366 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7367 | struct in_str input; |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7368 | IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 7369 | |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7370 | IF_HUSH_LINENO_VAR(G.parse_lineno = 1;) |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7371 | setup_file_in_str(&input, fp); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7372 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7373 | IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7374 | } |
| 7375 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7376 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7377 | static int generate_stream_from_string(const char *s, pid_t *pid_p) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7378 | { |
| 7379 | pid_t pid; |
| 7380 | int channel[2]; |
| 7381 | # if !BB_MMU |
| 7382 | char **to_free = NULL; |
| 7383 | # endif |
| 7384 | |
| 7385 | xpipe(channel); |
| 7386 | pid = BB_MMU ? xfork() : xvfork(); |
| 7387 | if (pid == 0) { /* child */ |
| 7388 | disable_restore_tty_pgrp_on_exit(); |
| 7389 | /* Process substitution is not considered to be usual |
| 7390 | * 'command execution'. |
| 7391 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 7392 | */ |
| 7393 | bb_signals(0 |
| 7394 | + (1 << SIGTSTP) |
| 7395 | + (1 << SIGTTIN) |
| 7396 | + (1 << SIGTTOU) |
| 7397 | , SIG_IGN); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7398 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 7399 | xmove_fd(channel[1], 1); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7400 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7401 | /* Awful hack for `trap` or $(trap). |
| 7402 | * |
| 7403 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 7404 | * contains an example where "trap" is executed in a subshell: |
| 7405 | * |
| 7406 | * save_traps=$(trap) |
| 7407 | * ... |
| 7408 | * eval "$save_traps" |
| 7409 | * |
| 7410 | * Standard does not say that "trap" in subshell shall print |
| 7411 | * parent shell's traps. It only says that its output |
| 7412 | * must have suitable form, but then, in the above example |
| 7413 | * (which is not supposed to be normative), it implies that. |
| 7414 | * |
| 7415 | * bash (and probably other shell) does implement it |
| 7416 | * (traps are reset to defaults, but "trap" still shows them), |
| 7417 | * but as a result, "trap" logic is hopelessly messed up: |
| 7418 | * |
| 7419 | * # trap |
| 7420 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 7421 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 7422 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 7423 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 7424 | * trap -- 'echo Ho' SIGWINCH |
| 7425 | * # echo `(trap)` <--- in subshell in subshell - output |
| 7426 | * trap -- 'echo Ho' SIGWINCH |
| 7427 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 7428 | * trap -- 'echo Ho' SIGWINCH |
| 7429 | * |
| 7430 | * The rules when to forget and when to not forget traps |
| 7431 | * get really complex and nonsensical. |
| 7432 | * |
| 7433 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 7434 | */ |
| 7435 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 7436 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7437 | && skip_whitespace(s + 4)[0] == '\0' |
| 7438 | ) { |
| 7439 | static const char *const argv[] = { NULL, NULL }; |
| 7440 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 7441 | fflush_all(); /* important */ |
| 7442 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7443 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7444 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7445 | # if BB_MMU |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7446 | /* Prevent it from trying to handle ctrl-z etc */ |
| 7447 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 7448 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7449 | reset_traps_to_defaults(); |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7450 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 7451 | //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7452 | parse_and_run_string(s); |
| 7453 | _exit(G.last_exitcode); |
| 7454 | # else |
| 7455 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 7456 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 7457 | * huge=`cat BIG` # was blocking here forever |
| 7458 | * echo OK |
| 7459 | */ |
| 7460 | re_execute_shell(&to_free, |
| 7461 | s, |
| 7462 | G.global_argv[0], |
| 7463 | G.global_argv + 1, |
| 7464 | NULL); |
| 7465 | # endif |
| 7466 | } |
| 7467 | |
| 7468 | /* parent */ |
| 7469 | *pid_p = pid; |
| 7470 | # if ENABLE_HUSH_FAST |
| 7471 | G.count_SIGCHLD++; |
| 7472 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 7473 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 7474 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7475 | # endif |
| 7476 | enable_restore_tty_pgrp_on_exit(); |
| 7477 | # if !BB_MMU |
| 7478 | free(to_free); |
| 7479 | # endif |
| 7480 | close(channel[1]); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7481 | return channel[0]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7482 | } |
| 7483 | |
| 7484 | /* Return code is exit status of the process that is run. */ |
| 7485 | static int process_command_subs(o_string *dest, const char *s) |
| 7486 | { |
| 7487 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7488 | pid_t pid; |
| 7489 | int status, ch, eol_cnt; |
| 7490 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7491 | fp = xfdopen_for_read(generate_stream_from_string(s, &pid)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7492 | |
| 7493 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7494 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7495 | while ((ch = getc(fp)) != EOF) { |
| 7496 | if (ch == '\0') |
| 7497 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7498 | if (ch == '\n') { |
| 7499 | eol_cnt++; |
| 7500 | continue; |
| 7501 | } |
| 7502 | while (eol_cnt) { |
| 7503 | o_addchr(dest, '\n'); |
| 7504 | eol_cnt--; |
| 7505 | } |
| 7506 | o_addQchr(dest, ch); |
| 7507 | } |
| 7508 | |
| 7509 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7510 | fclose(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7511 | /* We need to extract exitcode. Test case |
| 7512 | * "true; echo `sleep 1; false` $?" |
| 7513 | * should print 1 */ |
| 7514 | safe_waitpid(pid, &status, 0); |
| 7515 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 7516 | return WEXITSTATUS(status); |
| 7517 | } |
| 7518 | #endif /* ENABLE_HUSH_TICK */ |
| 7519 | |
| 7520 | |
| 7521 | static void setup_heredoc(struct redir_struct *redir) |
| 7522 | { |
| 7523 | struct fd_pair pair; |
| 7524 | pid_t pid; |
| 7525 | int len, written; |
| 7526 | /* the _body_ of heredoc (misleading field name) */ |
| 7527 | const char *heredoc = redir->rd_filename; |
| 7528 | char *expanded; |
| 7529 | #if !BB_MMU |
| 7530 | char **to_free; |
| 7531 | #endif |
| 7532 | |
| 7533 | expanded = NULL; |
| 7534 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 7535 | expanded = encode_then_expand_string(heredoc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7536 | if (expanded) |
| 7537 | heredoc = expanded; |
| 7538 | } |
| 7539 | len = strlen(heredoc); |
| 7540 | |
| 7541 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 7542 | xpiped_pair(pair); |
| 7543 | xmove_fd(pair.rd, redir->rd_fd); |
| 7544 | |
| 7545 | /* Try writing without forking. Newer kernels have |
| 7546 | * dynamically growing pipes. Must use non-blocking write! */ |
| 7547 | ndelay_on(pair.wr); |
| 7548 | while (1) { |
| 7549 | written = write(pair.wr, heredoc, len); |
| 7550 | if (written <= 0) |
| 7551 | break; |
| 7552 | len -= written; |
| 7553 | if (len == 0) { |
| 7554 | close(pair.wr); |
| 7555 | free(expanded); |
| 7556 | return; |
| 7557 | } |
| 7558 | heredoc += written; |
| 7559 | } |
| 7560 | ndelay_off(pair.wr); |
| 7561 | |
| 7562 | /* Okay, pipe buffer was not big enough */ |
| 7563 | /* Note: we must not create a stray child (bastard? :) |
| 7564 | * for the unsuspecting parent process. Child creates a grandchild |
| 7565 | * and exits before parent execs the process which consumes heredoc |
| 7566 | * (that exec happens after we return from this function) */ |
| 7567 | #if !BB_MMU |
| 7568 | to_free = NULL; |
| 7569 | #endif |
| 7570 | pid = xvfork(); |
| 7571 | if (pid == 0) { |
| 7572 | /* child */ |
| 7573 | disable_restore_tty_pgrp_on_exit(); |
| 7574 | pid = BB_MMU ? xfork() : xvfork(); |
| 7575 | if (pid != 0) |
| 7576 | _exit(0); |
| 7577 | /* grandchild */ |
| 7578 | close(redir->rd_fd); /* read side of the pipe */ |
| 7579 | #if BB_MMU |
| 7580 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 7581 | _exit(0); |
| 7582 | #else |
| 7583 | /* Delegate blocking writes to another process */ |
| 7584 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 7585 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 7586 | #endif |
| 7587 | } |
| 7588 | /* parent */ |
| 7589 | #if ENABLE_HUSH_FAST |
| 7590 | G.count_SIGCHLD++; |
| 7591 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7592 | #endif |
| 7593 | enable_restore_tty_pgrp_on_exit(); |
| 7594 | #if !BB_MMU |
| 7595 | free(to_free); |
| 7596 | #endif |
| 7597 | close(pair.wr); |
| 7598 | free(expanded); |
| 7599 | wait(NULL); /* wait till child has died */ |
| 7600 | } |
| 7601 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7602 | struct squirrel { |
| 7603 | int orig_fd; |
| 7604 | int moved_to; |
| 7605 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7606 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7607 | }; |
| 7608 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7609 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7610 | { |
| 7611 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7612 | sq[i].orig_fd = orig; |
| 7613 | sq[i].moved_to = moved; |
| 7614 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7615 | return sq; |
| 7616 | } |
| 7617 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7618 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7619 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7620 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7621 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7622 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7623 | i = 0; |
| 7624 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7625 | /* If we collide with an already moved fd... */ |
| 7626 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7627 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7628 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7629 | if (sq[i].moved_to < 0) /* what? */ |
| 7630 | xfunc_die(); |
| 7631 | return sq; |
| 7632 | } |
| 7633 | if (fd == sq[i].orig_fd) { |
| 7634 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7635 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7636 | return sq; |
| 7637 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7638 | } |
| 7639 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7640 | /* 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] | 7641 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7642 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7643 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7644 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7645 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7646 | } |
| 7647 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7648 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7649 | { |
| 7650 | int i; |
| 7651 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7652 | i = 0; |
| 7653 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7654 | /* If we collide with an already moved fd... */ |
| 7655 | if (fd == sq[i].orig_fd) { |
| 7656 | /* Examples: |
| 7657 | * "echo 3>FILE 3>&- 3>FILE" |
| 7658 | * "echo 3>&- 3>FILE" |
| 7659 | * No need for last redirect to insert |
| 7660 | * another "need to close 3" indicator. |
| 7661 | */ |
| 7662 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7663 | return sq; |
| 7664 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7665 | } |
| 7666 | |
| 7667 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7668 | return append_squirrel(sq, i, fd, -1); |
| 7669 | } |
| 7670 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7671 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7672 | * Move all conflicting internally used fds, |
| 7673 | * and remember them so that we can restore them later. |
| 7674 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7675 | 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] | 7676 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7677 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7678 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7679 | |
| 7680 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7681 | if (fd != 0 /* don't trigger for G_interactive_fd == 0 (that's "not interactive" flag) */ |
| 7682 | && fd == G_interactive_fd |
| 7683 | ) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7684 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 7685 | G_interactive_fd = xdup_CLOEXEC_and_close(G_interactive_fd, avoid_fd); |
| 7686 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G_interactive_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7687 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7688 | } |
| 7689 | #endif |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7690 | /* Are we called from setup_redirects(squirrel==NULL) |
| 7691 | * in redirect in a [v]forked child? |
| 7692 | */ |
| 7693 | if (sqp == NULL) { |
| 7694 | /* No need to move script fds. |
| 7695 | * For NOMMU case, it's actively wrong: we'd change ->fd |
| 7696 | * fields in memory for the parent, but parent's fds |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7697 | * aren't moved, it would use wrong fd! |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7698 | * Reproducer: "cmd 3>FILE" in script. |
| 7699 | * If we would call move_HFILEs_on_redirect(), child would: |
| 7700 | * fcntl64(3, F_DUPFD_CLOEXEC, 10) = 10 |
| 7701 | * close(3) = 0 |
| 7702 | * and change ->fd to 10 if fd#3 is a script fd. WRONG. |
| 7703 | */ |
| 7704 | //bb_error_msg("sqp == NULL: [v]forked child"); |
| 7705 | return 0; |
| 7706 | } |
| 7707 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7708 | /* If this one of script's fds? */ |
| 7709 | if (move_HFILEs_on_redirect(fd, avoid_fd)) |
| 7710 | return 1; /* yes. "we closed fd" (actually moved it) */ |
| 7711 | |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7712 | /* Are we called for "exec 3>FILE"? Came through |
| 7713 | * redirect_and_varexp_helper(squirrel=ERR_PTR) -> setup_redirects(ERR_PTR) |
| 7714 | * This case used to fail for this script: |
| 7715 | * exec 3>FILE |
| 7716 | * echo Ok |
| 7717 | * ...100000 more lines... |
| 7718 | * echo Ok |
| 7719 | * as follows: |
| 7720 | * read(3, "exec 3>FILE\necho Ok\necho Ok"..., 1024) = 1024 |
| 7721 | * open("FILE", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 4 |
| 7722 | * dup2(4, 3) = 3 |
| 7723 | * ^^^^^^^^ oops, we lost fd#3 opened to our script! |
| 7724 | * close(4) = 0 |
| 7725 | * write(1, "Ok\n", 3) = 3 |
| 7726 | * ... = 3 |
| 7727 | * write(1, "Ok\n", 3) = 3 |
| 7728 | * read(3, 0x94fbc08, 1024) = -1 EBADF (Bad file descriptor) |
| 7729 | * ^^^^^^^^ oops, wrong fd!!! |
| 7730 | * With this case separate from sqp == NULL and *after* move_HFILEs, |
| 7731 | * it now works: |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7732 | */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7733 | if (sqp == ERR_PTR) { |
| 7734 | /* Don't preserve redirected fds: exec is _meant_ to change these */ |
| 7735 | //bb_error_msg("sqp == ERR_PTR: exec >FILE"); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7736 | return 0; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7737 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7738 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7739 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7740 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7741 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7742 | } |
| 7743 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7744 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7745 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7746 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7747 | int i; |
| 7748 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7749 | if (sq[i].moved_to >= 0) { |
| 7750 | /* We simply die on error */ |
| 7751 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7752 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7753 | } else { |
| 7754 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7755 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7756 | close(sq[i].orig_fd); |
| 7757 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7758 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7759 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7760 | } |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7761 | if (G.HFILE_stdin |
| 7762 | && G.HFILE_stdin->fd != STDIN_FILENO |
| 7763 | ) { |
| 7764 | /* Testcase: interactive "read r <FILE; echo $r; read r; echo $r". |
| 7765 | * Redirect moves ->fd to e.g. 10, |
| 7766 | * and it is not restored above (we do not restore script fds |
| 7767 | * after redirects, we just use new, "moved" fds). |
| 7768 | * However for stdin, get_user_input() -> read_line_input(), |
| 7769 | * and read builtin, depend on fd == STDIN_FILENO. |
| 7770 | */ |
| 7771 | debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd); |
| 7772 | xmove_fd(G.HFILE_stdin->fd, STDIN_FILENO); |
| 7773 | G.HFILE_stdin->fd = STDIN_FILENO; |
| 7774 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7775 | |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 7776 | /* If moved, G_interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7777 | } |
| 7778 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7779 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7780 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7781 | { |
| 7782 | if (G_interactive_fd) |
| 7783 | close(G_interactive_fd); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7784 | close_all_HFILE_list(); |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7785 | } |
| 7786 | #endif |
| 7787 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7788 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7789 | { |
| 7790 | int i; |
| 7791 | |
| 7792 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 7793 | if (fd == G_interactive_fd) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7794 | return 1; |
| 7795 | #endif |
| 7796 | /* If this one of script's fds? */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7797 | if (fd_in_HFILEs(fd)) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7798 | return 1; |
| 7799 | |
| 7800 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7801 | if (fd == sq[i].moved_to) |
| 7802 | return 1; |
| 7803 | } |
| 7804 | return 0; |
| 7805 | } |
| 7806 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7807 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7808 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7809 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7810 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7811 | struct redir_struct *redir; |
| 7812 | |
| 7813 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7814 | int newfd; |
| 7815 | int closed; |
| 7816 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7817 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7818 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7819 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7820 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7821 | * of the heredoc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7822 | debug_printf_redir("set heredoc '%s'\n", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7823 | redir->rd_filename); |
| 7824 | setup_heredoc(redir); |
| 7825 | continue; |
| 7826 | } |
| 7827 | |
| 7828 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7829 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7830 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7831 | int mode; |
| 7832 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7833 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7834 | /* Examples: |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7835 | * "cmd >" (no filename) |
| 7836 | * "cmd > <file" (2nd redirect starts too early) |
| 7837 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7838 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7839 | continue; |
| 7840 | } |
| 7841 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7842 | p = expand_string_to_string(redir->rd_filename, |
| 7843 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7844 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7845 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7846 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7847 | /* Error message from open_or_warn can be lost |
| 7848 | * if stderr has been redirected, but bash |
| 7849 | * and ash both lose it as well |
| 7850 | * (though zsh doesn't!) |
| 7851 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7852 | return 1; |
| 7853 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7854 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7855 | /* open() gave us precisely the fd we wanted. |
| 7856 | * This means that this fd was not busy |
| 7857 | * (not opened to anywhere). |
| 7858 | * Remember to close it on restore: |
| 7859 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7860 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7861 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7862 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7863 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7864 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7865 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7866 | } |
| 7867 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7868 | if (newfd == redir->rd_fd) |
| 7869 | continue; |
| 7870 | |
| 7871 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7872 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7873 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7874 | |
| 7875 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7876 | if (newfd == REDIRFD_CLOSE) { |
| 7877 | /* "N>&-" means "close me" */ |
| 7878 | if (!closed) { |
| 7879 | /* ^^^ optimization: saving may already |
| 7880 | * have closed it. If not... */ |
| 7881 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7882 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7883 | /* Sometimes we do another close on restore, getting EBADF. |
| 7884 | * Consider "echo 3>FILE 3>&-" |
| 7885 | * first redirect remembers "need to close 3", |
| 7886 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7887 | */ |
| 7888 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7889 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7890 | if (internally_opened_fd(newfd, sqp && sqp != ERR_PTR ? *sqp : NULL)) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7891 | //errno = EBADF; |
| 7892 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7893 | newfd = -1; /* same effect as code above */ |
| 7894 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7895 | xdup2(newfd, redir->rd_fd); |
| 7896 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7897 | /* "rd_fd > FILE" */ |
| 7898 | close(newfd); |
| 7899 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7900 | } |
| 7901 | } |
| 7902 | return 0; |
| 7903 | } |
| 7904 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7905 | static char *find_in_path(const char *arg) |
| 7906 | { |
| 7907 | char *ret = NULL; |
| 7908 | const char *PATH = get_local_var_value("PATH"); |
| 7909 | |
| 7910 | if (!PATH) |
| 7911 | return NULL; |
| 7912 | |
| 7913 | while (1) { |
| 7914 | const char *end = strchrnul(PATH, ':'); |
| 7915 | int sz = end - PATH; /* must be int! */ |
| 7916 | |
| 7917 | free(ret); |
| 7918 | if (sz != 0) { |
| 7919 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7920 | } else { |
| 7921 | /* We have xxx::yyyy in $PATH, |
| 7922 | * it means "use current dir" */ |
| 7923 | ret = xstrdup(arg); |
| 7924 | } |
| 7925 | if (access(ret, F_OK) == 0) |
| 7926 | break; |
| 7927 | |
| 7928 | if (*end == '\0') { |
| 7929 | free(ret); |
| 7930 | return NULL; |
| 7931 | } |
| 7932 | PATH = end + 1; |
| 7933 | } |
| 7934 | |
| 7935 | return ret; |
| 7936 | } |
| 7937 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7938 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7939 | const struct built_in_command *x, |
| 7940 | const struct built_in_command *end) |
| 7941 | { |
| 7942 | while (x != end) { |
| 7943 | if (strcmp(name, x->b_cmd) != 0) { |
| 7944 | x++; |
| 7945 | continue; |
| 7946 | } |
| 7947 | debug_printf_exec("found builtin '%s'\n", name); |
| 7948 | return x; |
| 7949 | } |
| 7950 | return NULL; |
| 7951 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7952 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7953 | { |
| 7954 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7955 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7956 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7957 | { |
| 7958 | const struct built_in_command *x = find_builtin1(name); |
| 7959 | if (x) |
| 7960 | return x; |
| 7961 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7962 | } |
| 7963 | |
Denys Vlasenko | d5314e7 | 2020-06-24 09:31:30 +0200 | [diff] [blame] | 7964 | #if ENABLE_HUSH_JOB && EDITING_HAS_get_exe_name |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 7965 | static const char * FAST_FUNC get_builtin_name(int i) |
| 7966 | { |
| 7967 | if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) { |
| 7968 | return bltins1[i].b_cmd; |
| 7969 | } |
| 7970 | i -= ARRAY_SIZE(bltins1); |
| 7971 | if (i < ARRAY_SIZE(bltins2)) { |
| 7972 | return bltins2[i].b_cmd; |
| 7973 | } |
| 7974 | return NULL; |
| 7975 | } |
| 7976 | #endif |
| 7977 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 7978 | static void remove_nested_vars(void) |
| 7979 | { |
| 7980 | struct variable *cur; |
| 7981 | struct variable **cur_pp; |
| 7982 | |
| 7983 | cur_pp = &G.top_var; |
| 7984 | while ((cur = *cur_pp) != NULL) { |
| 7985 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7986 | cur_pp = &cur->next; |
| 7987 | continue; |
| 7988 | } |
| 7989 | /* Unexport */ |
| 7990 | if (cur->flg_export) { |
| 7991 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7992 | bb_unsetenv(cur->varstr); |
| 7993 | } |
| 7994 | /* Remove from global list */ |
| 7995 | *cur_pp = cur->next; |
| 7996 | /* Free */ |
| 7997 | if (!cur->max_len) { |
| 7998 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7999 | free(cur->varstr); |
| 8000 | } |
| 8001 | free(cur); |
| 8002 | } |
| 8003 | } |
| 8004 | |
| 8005 | static void enter_var_nest_level(void) |
| 8006 | { |
| 8007 | G.var_nest_level++; |
| 8008 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 8009 | |
| 8010 | /* Try: f() { echo -n .; f; }; f |
| 8011 | * struct variable::var_nest_level is uint16_t, |
| 8012 | * thus limiting recursion to < 2^16. |
| 8013 | * In any case, with 8 Mbyte stack SEGV happens |
| 8014 | * not too long after 2^16 recursions anyway. |
| 8015 | */ |
| 8016 | if (G.var_nest_level > 0xff00) |
| 8017 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 8018 | } |
| 8019 | |
| 8020 | static void leave_var_nest_level(void) |
| 8021 | { |
| 8022 | G.var_nest_level--; |
| 8023 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 8024 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 8025 | bb_simple_error_msg_and_die("BUG: nesting underflow"); |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 8026 | |
| 8027 | remove_nested_vars(); |
| 8028 | } |
| 8029 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8030 | #if ENABLE_HUSH_FUNCTIONS |
| 8031 | static struct function **find_function_slot(const char *name) |
| 8032 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8033 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8034 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8035 | |
| 8036 | while ((funcp = *funcpp) != NULL) { |
| 8037 | if (strcmp(name, funcp->name) == 0) { |
| 8038 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8039 | break; |
| 8040 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8041 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8042 | } |
| 8043 | return funcpp; |
| 8044 | } |
| 8045 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8046 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8047 | { |
| 8048 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8049 | return funcp; |
| 8050 | } |
| 8051 | |
| 8052 | /* Note: takes ownership on name ptr */ |
| 8053 | static struct function *new_function(char *name) |
| 8054 | { |
| 8055 | struct function **funcpp = find_function_slot(name); |
| 8056 | struct function *funcp = *funcpp; |
| 8057 | |
| 8058 | if (funcp != NULL) { |
| 8059 | struct command *cmd = funcp->parent_cmd; |
| 8060 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 8061 | if (!cmd) { |
| 8062 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 8063 | free(funcp->name); |
| 8064 | /* Note: if !funcp->body, do not free body_as_string! |
| 8065 | * This is a special case of "-F name body" function: |
| 8066 | * body_as_string was not malloced! */ |
| 8067 | if (funcp->body) { |
| 8068 | free_pipe_list(funcp->body); |
| 8069 | # if !BB_MMU |
| 8070 | free(funcp->body_as_string); |
| 8071 | # endif |
| 8072 | } |
| 8073 | } else { |
| 8074 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 8075 | cmd->argv[0] = funcp->name; |
| 8076 | cmd->group = funcp->body; |
| 8077 | # if !BB_MMU |
| 8078 | cmd->group_as_string = funcp->body_as_string; |
| 8079 | # endif |
| 8080 | } |
| 8081 | } else { |
| 8082 | debug_printf_exec("remembering new function '%s'\n", name); |
| 8083 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 8084 | /*funcp->next = NULL;*/ |
| 8085 | } |
| 8086 | |
| 8087 | funcp->name = name; |
| 8088 | return funcp; |
| 8089 | } |
| 8090 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8091 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8092 | static void unset_func(const char *name) |
| 8093 | { |
| 8094 | struct function **funcpp = find_function_slot(name); |
| 8095 | struct function *funcp = *funcpp; |
| 8096 | |
| 8097 | if (funcp != NULL) { |
| 8098 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 8099 | *funcpp = funcp->next; |
| 8100 | /* funcp is unlinked now, deleting it. |
| 8101 | * Note: if !funcp->body, the function was created by |
| 8102 | * "-F name body", do not free ->body_as_string |
| 8103 | * and ->name as they were not malloced. */ |
| 8104 | if (funcp->body) { |
| 8105 | free_pipe_list(funcp->body); |
| 8106 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8107 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8108 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8109 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8110 | } |
| 8111 | free(funcp); |
| 8112 | } |
| 8113 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8114 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8115 | |
| 8116 | # if BB_MMU |
| 8117 | #define exec_function(to_free, funcp, argv) \ |
| 8118 | exec_function(funcp, argv) |
| 8119 | # endif |
| 8120 | static void exec_function(char ***to_free, |
| 8121 | const struct function *funcp, |
| 8122 | char **argv) NORETURN; |
| 8123 | static void exec_function(char ***to_free, |
| 8124 | const struct function *funcp, |
| 8125 | char **argv) |
| 8126 | { |
| 8127 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8128 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8129 | |
| 8130 | argv[0] = G.global_argv[0]; |
| 8131 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8132 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8133 | |
| 8134 | // Example when we are here: "cmd | func" |
| 8135 | // func will run with saved-redirect fds open. |
| 8136 | // $ f() { echo /proc/self/fd/*; } |
| 8137 | // $ true | f |
| 8138 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 8139 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 8140 | // Same in script: |
| 8141 | // $ . ./SCRIPT |
| 8142 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 8143 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 8144 | // They are CLOEXEC so external programs won't see them, but |
| 8145 | // for "more correctness" we might want to close those extra fds here: |
| 8146 | //? close_saved_fds_and_FILE_fds(); |
| 8147 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8148 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8149 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8150 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8151 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 8152 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8153 | /* On MMU, funcp->body is always non-NULL */ |
| 8154 | n = run_list(funcp->body); |
| 8155 | fflush_all(); |
| 8156 | _exit(n); |
| 8157 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8158 | //? close_saved_fds_and_FILE_fds(); |
| 8159 | |
| 8160 | //TODO: check whether "true | func_with_return" works |
| 8161 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8162 | re_execute_shell(to_free, |
| 8163 | funcp->body_as_string, |
| 8164 | G.global_argv[0], |
| 8165 | argv + 1, |
| 8166 | NULL); |
| 8167 | # endif |
| 8168 | } |
| 8169 | |
| 8170 | static int run_function(const struct function *funcp, char **argv) |
| 8171 | { |
| 8172 | int rc; |
| 8173 | save_arg_t sv; |
| 8174 | smallint sv_flg; |
| 8175 | |
| 8176 | save_and_replace_G_args(&sv, argv); |
| 8177 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8178 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8179 | sv_flg = G_flag_return_in_progress; |
| 8180 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8181 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8182 | /* Make "local" variables properly shadow previous ones */ |
| 8183 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8184 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8185 | |
| 8186 | /* On MMU, funcp->body is always non-NULL */ |
| 8187 | # if !BB_MMU |
| 8188 | if (!funcp->body) { |
| 8189 | /* Function defined by -F */ |
| 8190 | parse_and_run_string(funcp->body_as_string); |
| 8191 | rc = G.last_exitcode; |
| 8192 | } else |
| 8193 | # endif |
| 8194 | { |
| 8195 | rc = run_list(funcp->body); |
| 8196 | } |
| 8197 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8198 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8199 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8200 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8201 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 8202 | # if ENABLE_HUSH_TRAP |
| 8203 | debug_printf_exec("G.return_exitcode=-1\n"); |
| 8204 | G.return_exitcode = -1; /* invalidate stashed return value */ |
| 8205 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8206 | |
| 8207 | restore_G_args(&sv, argv); |
| 8208 | |
| 8209 | return rc; |
| 8210 | } |
| 8211 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 8212 | |
| 8213 | |
| 8214 | #if BB_MMU |
| 8215 | #define exec_builtin(to_free, x, argv) \ |
| 8216 | exec_builtin(x, argv) |
| 8217 | #else |
| 8218 | #define exec_builtin(to_free, x, argv) \ |
| 8219 | exec_builtin(to_free, argv) |
| 8220 | #endif |
| 8221 | static void exec_builtin(char ***to_free, |
| 8222 | const struct built_in_command *x, |
| 8223 | char **argv) NORETURN; |
| 8224 | static void exec_builtin(char ***to_free, |
| 8225 | const struct built_in_command *x, |
| 8226 | char **argv) |
| 8227 | { |
| 8228 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8229 | int rcode; |
| 8230 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8231 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8232 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8233 | fflush_all(); |
| 8234 | _exit(rcode); |
| 8235 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8236 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8237 | /* On NOMMU, we must never block! |
| 8238 | * Example: { sleep 99 | read line; } & echo Ok |
| 8239 | */ |
| 8240 | re_execute_shell(to_free, |
| 8241 | argv[0], |
| 8242 | G.global_argv[0], |
| 8243 | G.global_argv + 1, |
| 8244 | argv); |
| 8245 | #endif |
| 8246 | } |
| 8247 | |
| 8248 | |
| 8249 | static void execvp_or_die(char **argv) NORETURN; |
| 8250 | static void execvp_or_die(char **argv) |
| 8251 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8252 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8253 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8254 | /* Don't propagate SIG_IGN to the child */ |
| 8255 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8256 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8257 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8258 | e = 2; |
| 8259 | if (errno == EACCES) e = 126; |
| 8260 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8261 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8262 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8263 | } |
| 8264 | |
| 8265 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8266 | static void x_mode_print_optionally_squoted(const char *str) |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8267 | { |
| 8268 | unsigned len; |
| 8269 | const char *cp; |
| 8270 | |
| 8271 | cp = str; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8272 | |
| 8273 | /* the set of chars which-cause-string-to-be-squoted mimics bash */ |
| 8274 | /* test a char with: bash -c 'set -x; echo "CH"' */ |
| 8275 | if (str[strcspn(str, "\\\"'`$(){}[]<>;#&|~*?!^" |
| 8276 | " " "\001\002\003\004\005\006\007" |
| 8277 | "\010\011\012\013\014\015\016\017" |
| 8278 | "\020\021\022\023\024\025\026\027" |
| 8279 | "\030\031\032\033\034\035\036\037" |
| 8280 | ) |
| 8281 | ] == '\0' |
| 8282 | ) { |
| 8283 | /* string has no special chars */ |
| 8284 | x_mode_addstr(str); |
| 8285 | return; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8286 | } |
| 8287 | |
| 8288 | cp = str; |
| 8289 | for (;;) { |
| 8290 | /* print '....' up to EOL or first squote */ |
| 8291 | len = (int)(strchrnul(cp, '\'') - cp); |
| 8292 | if (len != 0) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8293 | x_mode_addchr('\''); |
| 8294 | x_mode_addblock(cp, len); |
| 8295 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8296 | cp += len; |
| 8297 | } |
| 8298 | if (*cp == '\0') |
| 8299 | break; |
| 8300 | /* string contains squote(s), print them as \' */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8301 | x_mode_addchr('\\'); |
| 8302 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8303 | cp++; |
| 8304 | } |
| 8305 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8306 | static void dump_cmd_in_x_mode(char **argv) |
| 8307 | { |
| 8308 | if (G_x_mode && argv) { |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 8309 | unsigned n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8310 | |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8311 | /* "+[+++...][ cmd...]\n\0" */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8312 | x_mode_prefix(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8313 | n = 0; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8314 | while (argv[n]) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8315 | x_mode_addchr(' '); |
| 8316 | if (argv[n][0] == '\0') { |
| 8317 | x_mode_addchr('\''); |
| 8318 | x_mode_addchr('\''); |
| 8319 | } else { |
| 8320 | x_mode_print_optionally_squoted(argv[n]); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8321 | } |
| 8322 | n++; |
| 8323 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8324 | x_mode_flush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8325 | } |
| 8326 | } |
| 8327 | #else |
| 8328 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 8329 | #endif |
| 8330 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8331 | #if ENABLE_HUSH_COMMAND |
| 8332 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 8333 | { |
| 8334 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8335 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8336 | if (!opt_vV) |
| 8337 | return; |
| 8338 | |
| 8339 | to_free = NULL; |
| 8340 | if (!explanation) { |
| 8341 | char *path = getenv("PATH"); |
| 8342 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8343 | if (!explanation) |
| 8344 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8345 | if (opt_vV != 'V') |
| 8346 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 8347 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8348 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8349 | free(to_free); |
| 8350 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8351 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8352 | } |
| 8353 | #else |
| 8354 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 8355 | #endif |
| 8356 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8357 | #if BB_MMU |
| 8358 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 8359 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 8360 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 8361 | pseudo_exec(command, argv_expanded) |
| 8362 | #endif |
| 8363 | |
| 8364 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 8365 | * Never returns. |
| 8366 | * Don't exit() here. If you don't exec, use _exit instead. |
| 8367 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8368 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8369 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8370 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8371 | char **argv, int assignment_cnt, |
| 8372 | char **argv_expanded) NORETURN; |
| 8373 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8374 | char **argv, int assignment_cnt, |
| 8375 | char **argv_expanded) |
| 8376 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8377 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8378 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8379 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8380 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 8381 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8382 | |
| 8383 | new_env = expand_assignments(argv, assignment_cnt); |
| 8384 | dump_cmd_in_x_mode(new_env); |
| 8385 | |
| 8386 | if (!argv[assignment_cnt]) { |
| 8387 | /* Case when we are here: ... | var=val | ... |
| 8388 | * (note that we do not exit early, i.e., do not optimize out |
| 8389 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 8390 | */ |
| 8391 | free_strings(new_env); |
| 8392 | _exit(EXIT_SUCCESS); |
| 8393 | } |
| 8394 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8395 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8396 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8397 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8398 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8399 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8400 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8401 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8402 | set_vars_and_save_old(new_env); |
| 8403 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8404 | |
| 8405 | if (argv_expanded) { |
| 8406 | argv = argv_expanded; |
| 8407 | } else { |
| 8408 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 8409 | #if !BB_MMU |
| 8410 | nommu_save->argv = argv; |
| 8411 | #endif |
| 8412 | } |
| 8413 | dump_cmd_in_x_mode(argv); |
| 8414 | |
| 8415 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8416 | if (strchr(argv[0], '/') != NULL) |
| 8417 | goto skip; |
| 8418 | #endif |
| 8419 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8420 | #if ENABLE_HUSH_FUNCTIONS |
| 8421 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8422 | funcp = find_function(argv[0]); |
| 8423 | if (funcp) |
| 8424 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8425 | #endif |
| 8426 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8427 | #if ENABLE_HUSH_COMMAND |
| 8428 | /* "command BAR": run BAR without looking it up among functions |
| 8429 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 8430 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 8431 | */ |
| 8432 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 8433 | char *p; |
| 8434 | |
| 8435 | argv++; |
| 8436 | p = *argv; |
| 8437 | if (p[0] != '-' || !p[1]) |
| 8438 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 8439 | |
| 8440 | for (;;) { |
| 8441 | p++; |
| 8442 | switch (*p) { |
| 8443 | case '\0': |
| 8444 | argv++; |
| 8445 | p = *argv; |
| 8446 | if (p[0] != '-' || !p[1]) |
| 8447 | goto after_opts; |
| 8448 | continue; /* next arg is also -opts, process it too */ |
| 8449 | case 'v': |
| 8450 | case 'V': |
| 8451 | opt_vV = *p; |
| 8452 | continue; |
| 8453 | default: |
| 8454 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 8455 | } |
| 8456 | } |
| 8457 | } |
| 8458 | after_opts: |
| 8459 | # if ENABLE_HUSH_FUNCTIONS |
| 8460 | if (opt_vV && find_function(argv[0])) |
| 8461 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 8462 | # endif |
| 8463 | #endif |
| 8464 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8465 | /* Check if the command matches any of the builtins. |
| 8466 | * Depending on context, this might be redundant. But it's |
| 8467 | * easier to waste a few CPU cycles than it is to figure out |
| 8468 | * if this is one of those cases. |
| 8469 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8470 | /* Why "BB_MMU ? :" difference in logic? - |
| 8471 | * On NOMMU, it is more expensive to re-execute shell |
| 8472 | * just in order to run echo or test builtin. |
| 8473 | * It's better to skip it here and run corresponding |
| 8474 | * non-builtin later. */ |
| 8475 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 8476 | if (x) { |
| 8477 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 8478 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8479 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8480 | |
| 8481 | #if ENABLE_FEATURE_SH_STANDALONE |
| 8482 | /* Check if the command matches any busybox applets */ |
| 8483 | { |
| 8484 | int a = find_applet_by_name(argv[0]); |
| 8485 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8486 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8487 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 8488 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8489 | /* Do not leak open fds from opened script files etc. |
| 8490 | * Testcase: interactive "ls -l /proc/self/fd" |
| 8491 | * should not show tty fd open. |
| 8492 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8493 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8494 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 8495 | //This casuses test failures in |
| 8496 | //redir_children_should_not_see_saved_fd_2.tests |
| 8497 | //redir_children_should_not_see_saved_fd_3.tests |
| 8498 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8499 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 8500 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 8501 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8502 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8503 | } |
| 8504 | # endif |
| 8505 | /* Re-exec ourselves */ |
| 8506 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8507 | /* Don't propagate SIG_IGN to the child */ |
| 8508 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8509 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8510 | execv(bb_busybox_exec_path, argv); |
| 8511 | /* If they called chroot or otherwise made the binary no longer |
| 8512 | * executable, fall through */ |
| 8513 | } |
| 8514 | } |
| 8515 | #endif |
| 8516 | |
| 8517 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8518 | skip: |
| 8519 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8520 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8521 | execvp_or_die(argv); |
| 8522 | } |
| 8523 | |
| 8524 | /* Called after [v]fork() in run_pipe |
| 8525 | */ |
| 8526 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8527 | struct command *command, |
| 8528 | char **argv_expanded) NORETURN; |
| 8529 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8530 | struct command *command, |
| 8531 | char **argv_expanded) |
| 8532 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 8533 | #if ENABLE_HUSH_FUNCTIONS |
| 8534 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8535 | /* Ignore funcdefs in pipes: |
| 8536 | * true | f() { cmd } |
| 8537 | */ |
| 8538 | _exit(0); |
| 8539 | } |
| 8540 | #endif |
| 8541 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8542 | if (command->argv) { |
| 8543 | pseudo_exec_argv(nommu_save, command->argv, |
| 8544 | command->assignment_cnt, argv_expanded); |
| 8545 | } |
| 8546 | |
| 8547 | if (command->group) { |
| 8548 | /* Cases when we are here: |
| 8549 | * ( list ) |
| 8550 | * { list } & |
| 8551 | * ... | ( list ) | ... |
| 8552 | * ... | { list } | ... |
| 8553 | */ |
| 8554 | #if BB_MMU |
| 8555 | int rcode; |
| 8556 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 8557 | reset_traps_to_defaults(); |
| 8558 | rcode = run_list(command->group); |
| 8559 | /* OK to leak memory by not calling free_pipe_list, |
| 8560 | * since this process is about to exit */ |
| 8561 | _exit(rcode); |
| 8562 | #else |
| 8563 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 8564 | command->group_as_string, |
| 8565 | G.global_argv[0], |
| 8566 | G.global_argv + 1, |
| 8567 | NULL); |
| 8568 | #endif |
| 8569 | } |
| 8570 | |
| 8571 | /* Case when we are here: ... | >file */ |
| 8572 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 8573 | _exit(EXIT_SUCCESS); |
| 8574 | } |
| 8575 | |
| 8576 | #if ENABLE_HUSH_JOB |
| 8577 | static const char *get_cmdtext(struct pipe *pi) |
| 8578 | { |
| 8579 | char **argv; |
| 8580 | char *p; |
| 8581 | int len; |
| 8582 | |
| 8583 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 8584 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 8585 | * On subsequent bg argv is trashed, but we won't use it */ |
| 8586 | if (pi->cmdtext) |
| 8587 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8588 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8589 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8590 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8591 | pi->cmdtext = xzalloc(1); |
| 8592 | return pi->cmdtext; |
| 8593 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8594 | len = 0; |
| 8595 | do { |
| 8596 | len += strlen(*argv) + 1; |
| 8597 | } while (*++argv); |
| 8598 | p = xmalloc(len); |
| 8599 | pi->cmdtext = p; |
| 8600 | argv = pi->cmds[0].argv; |
| 8601 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8602 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8603 | *p++ = ' '; |
| 8604 | } while (*++argv); |
| 8605 | p[-1] = '\0'; |
| 8606 | return pi->cmdtext; |
| 8607 | } |
| 8608 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8609 | static void remove_job_from_table(struct pipe *pi) |
| 8610 | { |
| 8611 | struct pipe *prev_pipe; |
| 8612 | |
| 8613 | if (pi == G.job_list) { |
| 8614 | G.job_list = pi->next; |
| 8615 | } else { |
| 8616 | prev_pipe = G.job_list; |
| 8617 | while (prev_pipe->next != pi) |
| 8618 | prev_pipe = prev_pipe->next; |
| 8619 | prev_pipe->next = pi->next; |
| 8620 | } |
| 8621 | G.last_jobid = 0; |
| 8622 | if (G.job_list) |
| 8623 | G.last_jobid = G.job_list->jobid; |
| 8624 | } |
| 8625 | |
| 8626 | static void delete_finished_job(struct pipe *pi) |
| 8627 | { |
| 8628 | remove_job_from_table(pi); |
| 8629 | free_pipe(pi); |
| 8630 | } |
| 8631 | |
| 8632 | static void clean_up_last_dead_job(void) |
| 8633 | { |
| 8634 | if (G.job_list && !G.job_list->alive_cmds) |
| 8635 | delete_finished_job(G.job_list); |
| 8636 | } |
| 8637 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8638 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8639 | { |
| 8640 | struct pipe *job, **jobp; |
| 8641 | int i; |
| 8642 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8643 | clean_up_last_dead_job(); |
| 8644 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8645 | /* Find the end of the list, and find next job ID to use */ |
| 8646 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8647 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8648 | while ((job = *jobp) != NULL) { |
| 8649 | if (job->jobid > i) |
| 8650 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8651 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8652 | } |
| 8653 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8654 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8655 | /* Create a new job struct at the end */ |
| 8656 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8657 | job->next = NULL; |
| 8658 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 8659 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 8660 | for (i = 0; i < pi->num_cmds; i++) { |
| 8661 | job->cmds[i].pid = pi->cmds[i].pid; |
| 8662 | /* all other fields are not used and stay zero */ |
| 8663 | } |
| 8664 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 8665 | |
| 8666 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 8667 | 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] | 8668 | G.last_jobid = job->jobid; |
| 8669 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8670 | #endif /* JOB */ |
| 8671 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8672 | static int job_exited_or_stopped(struct pipe *pi) |
| 8673 | { |
| 8674 | int rcode, i; |
| 8675 | |
| 8676 | if (pi->alive_cmds != pi->stopped_cmds) |
| 8677 | return -1; |
| 8678 | |
| 8679 | /* All processes in fg pipe have exited or stopped */ |
| 8680 | rcode = 0; |
| 8681 | i = pi->num_cmds; |
| 8682 | while (--i >= 0) { |
| 8683 | rcode = pi->cmds[i].cmd_exitcode; |
| 8684 | /* usually last process gives overall exitstatus, |
| 8685 | * but with "set -o pipefail", last *failed* process does */ |
| 8686 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 8687 | break; |
| 8688 | } |
| 8689 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8690 | return rcode; |
| 8691 | } |
| 8692 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8693 | 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] | 8694 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8695 | #if ENABLE_HUSH_JOB |
| 8696 | struct pipe *pi; |
| 8697 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8698 | int i, dead; |
| 8699 | |
| 8700 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 8701 | |
| 8702 | #if DEBUG_JOBS |
| 8703 | if (WIFSTOPPED(status)) |
| 8704 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8705 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8706 | if (WIFSIGNALED(status)) |
| 8707 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8708 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8709 | if (WIFEXITED(status)) |
| 8710 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8711 | childpid, WEXITSTATUS(status)); |
| 8712 | #endif |
| 8713 | /* Were we asked to wait for a fg pipe? */ |
| 8714 | if (fg_pipe) { |
| 8715 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8716 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8717 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8718 | int rcode; |
| 8719 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8720 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8721 | if (fg_pipe->cmds[i].pid != childpid) |
| 8722 | continue; |
| 8723 | if (dead) { |
| 8724 | int ex; |
| 8725 | fg_pipe->cmds[i].pid = 0; |
| 8726 | fg_pipe->alive_cmds--; |
| 8727 | ex = WEXITSTATUS(status); |
| 8728 | /* bash prints killer signal's name for *last* |
| 8729 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8730 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8731 | */ |
| 8732 | if (WIFSIGNALED(status)) { |
| 8733 | int sig = WTERMSIG(status); |
Denys Vlasenko | b65d6cb | 2020-10-24 03:33:32 +0200 | [diff] [blame] | 8734 | if (G.run_list_level == 1 |
| 8735 | /* ^^^^^ Do not print in nested contexts, example: |
| 8736 | * echo `sleep 1; sh -c 'kill -9 $$'` - prints "137", NOT "Killed 137" |
| 8737 | */ |
| 8738 | && i == fg_pipe->num_cmds-1 |
| 8739 | ) { |
Denys Vlasenko | e16f7eb | 2020-10-24 04:26:43 +0200 | [diff] [blame] | 8740 | /* strsignal() is for bash compat. ~600 bloat versus bbox's get_signame() */ |
| 8741 | puts(sig == SIGINT || sig == SIGPIPE ? "" : strsignal(sig)); |
Denys Vlasenko | b65d6cb | 2020-10-24 03:33:32 +0200 | [diff] [blame] | 8742 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8743 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8744 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8745 | * Maybe we need to use sig | 128? */ |
| 8746 | ex = sig + 128; |
| 8747 | } |
| 8748 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8749 | } else { |
| 8750 | fg_pipe->stopped_cmds++; |
| 8751 | } |
| 8752 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8753 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8754 | rcode = job_exited_or_stopped(fg_pipe); |
| 8755 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8756 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8757 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8758 | * and "killall -STOP cat" */ |
| 8759 | if (G_interactive_fd) { |
| 8760 | #if ENABLE_HUSH_JOB |
| 8761 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8762 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8763 | #endif |
| 8764 | return rcode; |
| 8765 | } |
| 8766 | if (fg_pipe->alive_cmds == 0) |
| 8767 | return rcode; |
| 8768 | } |
| 8769 | /* There are still running processes in the fg_pipe */ |
| 8770 | return -1; |
| 8771 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8772 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8773 | } |
| 8774 | |
| 8775 | #if ENABLE_HUSH_JOB |
| 8776 | /* We were asked to wait for bg or orphaned children */ |
| 8777 | /* No need to remember exitcode in this case */ |
| 8778 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8779 | for (i = 0; i < pi->num_cmds; i++) { |
| 8780 | if (pi->cmds[i].pid == childpid) |
| 8781 | goto found_pi_and_prognum; |
| 8782 | } |
| 8783 | } |
| 8784 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8785 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8786 | return -1; /* this wasn't a process from fg_pipe */ |
| 8787 | |
| 8788 | found_pi_and_prognum: |
| 8789 | if (dead) { |
| 8790 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8791 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8792 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8793 | rcode = 128 + WTERMSIG(status); |
| 8794 | pi->cmds[i].cmd_exitcode = rcode; |
| 8795 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8796 | G.last_bg_pid_exitcode = rcode; |
| 8797 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8798 | pi->alive_cmds--; |
| 8799 | if (!pi->alive_cmds) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 8800 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8801 | G.dead_job_exitcode = job_exited_or_stopped(pi); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 8802 | # endif |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8803 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8804 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8805 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8806 | delete_finished_job(pi); |
| 8807 | } else { |
| 8808 | /* |
| 8809 | * bash deletes finished jobs from job table only in interactive mode, |
| 8810 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8811 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8812 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8813 | * We only retain one "dead" job, if it's the single job on the list. |
| 8814 | * This covers most of real-world scenarios where this is useful. |
| 8815 | */ |
| 8816 | if (pi != G.job_list) |
| 8817 | delete_finished_job(pi); |
| 8818 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8819 | } |
| 8820 | } else { |
| 8821 | /* child stopped */ |
| 8822 | pi->stopped_cmds++; |
| 8823 | } |
| 8824 | #endif |
| 8825 | return -1; /* this wasn't a process from fg_pipe */ |
| 8826 | } |
| 8827 | |
| 8828 | /* Check to see if any processes have exited -- if they have, |
| 8829 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8830 | * |
| 8831 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8832 | * Return its exitcode or zero if stopped. |
| 8833 | * |
| 8834 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8835 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8836 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8837 | * or 0 if no children changed status. |
| 8838 | * |
| 8839 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8840 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8841 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8842 | */ |
| 8843 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8844 | { |
| 8845 | int attributes; |
| 8846 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8847 | int rcode = 0; |
| 8848 | |
| 8849 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8850 | |
| 8851 | attributes = WUNTRACED; |
| 8852 | if (fg_pipe == NULL) |
| 8853 | attributes |= WNOHANG; |
| 8854 | |
| 8855 | errno = 0; |
| 8856 | #if ENABLE_HUSH_FAST |
| 8857 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8858 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8859 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8860 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8861 | /* Avoid doing waitpid syscall if possible */ |
| 8862 | if (!G.we_have_children) { |
| 8863 | errno = ECHILD; |
| 8864 | return -1; |
| 8865 | } |
| 8866 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8867 | /* We have children, but they did not exit |
| 8868 | * or stop yet (we saw no SIGCHLD) */ |
| 8869 | return 0; |
| 8870 | } |
| 8871 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8872 | } |
| 8873 | #endif |
| 8874 | |
| 8875 | /* Do we do this right? |
| 8876 | * bash-3.00# sleep 20 | false |
| 8877 | * <ctrl-Z pressed> |
| 8878 | * [3]+ Stopped sleep 20 | false |
| 8879 | * bash-3.00# echo $? |
| 8880 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8881 | * [hush 1.14.0: yes we do it right] |
| 8882 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8883 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8884 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8885 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8886 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8887 | i = G.count_SIGCHLD; |
| 8888 | #endif |
| 8889 | childpid = waitpid(-1, &status, attributes); |
| 8890 | if (childpid <= 0) { |
| 8891 | if (childpid && errno != ECHILD) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 8892 | bb_simple_perror_msg("waitpid"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8893 | #if ENABLE_HUSH_FAST |
| 8894 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8895 | G.we_have_children = (childpid == 0); |
| 8896 | G.handled_SIGCHLD = i; |
| 8897 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8898 | } |
| 8899 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8900 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8901 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8902 | break; |
| 8903 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8904 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8905 | if (rcode >= 0) { |
| 8906 | /* fg_pipe exited or stopped */ |
| 8907 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8908 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8909 | if (childpid == waitfor_pid) { /* "wait PID" */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8910 | 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] | 8911 | rcode = WEXITSTATUS(status); |
| 8912 | if (WIFSIGNALED(status)) |
| 8913 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8914 | if (WIFSTOPPED(status)) |
| 8915 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8916 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8917 | rcode++; |
| 8918 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8919 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 8920 | #if ENABLE_HUSH_BASH_COMPAT |
| 8921 | if (-1 == waitfor_pid /* "wait -n" (wait for any one job) */ |
| 8922 | && G.dead_job_exitcode >= 0 /* some job did finish */ |
| 8923 | ) { |
| 8924 | debug_printf_exec("waitfor_pid:-1\n"); |
| 8925 | rcode = G.dead_job_exitcode + 1; |
| 8926 | break; |
| 8927 | } |
| 8928 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8929 | /* This wasn't one of our processes, or */ |
| 8930 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8931 | } /* while (waitpid succeeds)... */ |
| 8932 | |
| 8933 | return rcode; |
| 8934 | } |
| 8935 | |
| 8936 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8937 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8938 | { |
| 8939 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8940 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8941 | if (G_saved_tty_pgrp) { |
| 8942 | /* Job finished, move the shell to the foreground */ |
| 8943 | p = getpgrp(); /* our process group id */ |
| 8944 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8945 | tcsetpgrp(G_interactive_fd, p); |
| 8946 | } |
| 8947 | return rcode; |
| 8948 | } |
| 8949 | #endif |
| 8950 | |
| 8951 | /* Start all the jobs, but don't wait for anything to finish. |
| 8952 | * See checkjobs(). |
| 8953 | * |
| 8954 | * Return code is normally -1, when the caller has to wait for children |
| 8955 | * to finish to determine the exit status of the pipe. If the pipe |
| 8956 | * is a simple builtin command, however, the action is done by the |
| 8957 | * time run_pipe returns, and the exit code is provided as the |
| 8958 | * return value. |
| 8959 | * |
| 8960 | * Returns -1 only if started some children. IOW: we have to |
| 8961 | * mask out retvals of builtins etc with 0xff! |
| 8962 | * |
| 8963 | * The only case when we do not need to [v]fork is when the pipe |
| 8964 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8965 | * cmd ; ... { list } ; ... |
| 8966 | * cmd && ... { list } && ... |
| 8967 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8968 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8969 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8970 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8971 | * |
| 8972 | * Cases when we must fork: |
| 8973 | * non-single: cmd | cmd |
| 8974 | * backgrounded: cmd & { list } & |
| 8975 | * subshell: ( list ) [&] |
| 8976 | */ |
| 8977 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 8978 | #define redirect_and_varexp_helper(command, sqp, argv_expanded) \ |
| 8979 | redirect_and_varexp_helper(command, sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8980 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8981 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8982 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8983 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8984 | char **argv_expanded) |
| 8985 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8986 | /* Assignments occur before redirects. Try: |
| 8987 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8988 | */ |
| 8989 | |
| 8990 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8991 | dump_cmd_in_x_mode(new_env); |
| 8992 | dump_cmd_in_x_mode(argv_expanded); |
| 8993 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8994 | set_vars_and_save_old(new_env); |
| 8995 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8996 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8997 | } |
| 8998 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8999 | { |
| 9000 | static const char *const null_ptr = NULL; |
| 9001 | |
| 9002 | int cmd_no; |
| 9003 | int next_infd; |
| 9004 | struct command *command; |
| 9005 | char **argv_expanded; |
| 9006 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9007 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9008 | int rcode; |
| 9009 | |
| 9010 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 9011 | debug_enter(); |
| 9012 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9013 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 9014 | * Result should be 3 lines: q w e, qwe, q w e |
| 9015 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9016 | if (G.ifs_whitespace != G.ifs) |
| 9017 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9018 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9019 | if (G.ifs) { |
| 9020 | char *p; |
| 9021 | G.ifs_whitespace = (char*)G.ifs; |
| 9022 | p = skip_whitespace(G.ifs); |
| 9023 | if (*p) { |
| 9024 | /* Not all $IFS is whitespace */ |
| 9025 | char *d; |
| 9026 | int len = p - G.ifs; |
| 9027 | p = skip_non_whitespace(p); |
| 9028 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 9029 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 9030 | while (*p) { |
| 9031 | if (isspace(*p)) |
| 9032 | *d++ = *p; |
| 9033 | p++; |
| 9034 | } |
| 9035 | *d = '\0'; |
| 9036 | } |
| 9037 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9038 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9039 | G.ifs_whitespace = (char*)G.ifs; |
| 9040 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9041 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9042 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 9043 | pi->stopped_cmds = 0; |
| 9044 | command = &pi->cmds[0]; |
| 9045 | argv_expanded = NULL; |
| 9046 | |
| 9047 | if (pi->num_cmds != 1 |
| 9048 | || pi->followup == PIPE_BG |
| 9049 | || command->cmd_type == CMD_SUBSHELL |
| 9050 | ) { |
| 9051 | goto must_fork; |
| 9052 | } |
| 9053 | |
| 9054 | pi->alive_cmds = 1; |
| 9055 | |
| 9056 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 9057 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 9058 | |
| 9059 | if (command->group) { |
| 9060 | #if ENABLE_HUSH_FUNCTIONS |
| 9061 | if (command->cmd_type == CMD_FUNCDEF) { |
| 9062 | /* "executing" func () { list } */ |
| 9063 | struct function *funcp; |
| 9064 | |
| 9065 | funcp = new_function(command->argv[0]); |
| 9066 | /* funcp->name is already set to argv[0] */ |
| 9067 | funcp->body = command->group; |
| 9068 | # if !BB_MMU |
| 9069 | funcp->body_as_string = command->group_as_string; |
| 9070 | command->group_as_string = NULL; |
| 9071 | # endif |
| 9072 | command->group = NULL; |
| 9073 | command->argv[0] = NULL; |
| 9074 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 9075 | funcp->parent_cmd = command; |
| 9076 | command->child_func = funcp; |
| 9077 | |
| 9078 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 9079 | debug_leave(); |
| 9080 | return EXIT_SUCCESS; |
| 9081 | } |
| 9082 | #endif |
| 9083 | /* { list } */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 9084 | debug_printf_exec("non-subshell group\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9085 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9086 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9087 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 9088 | //FIXME: we need to pass squirrel down into run_list() |
| 9089 | //for SH_STANDALONE case, or else this construct: |
| 9090 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 9091 | //has no way of closing saved fd#1 for "find", |
| 9092 | //and in SH_STANDALONE mode, "find" is not execed, |
| 9093 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9094 | rcode = run_list(command->group) & 0xff; |
| 9095 | } |
| 9096 | restore_redirects(squirrel); |
| 9097 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9098 | debug_leave(); |
| 9099 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 9100 | return rcode; |
| 9101 | } |
| 9102 | |
| 9103 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 9104 | { |
| 9105 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9106 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 9107 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9108 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9109 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9110 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9111 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 9112 | G.execute_lineno = command->lineno; |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 9113 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9114 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9115 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9116 | /* Assignments, but no command. |
| 9117 | * Ensure redirects take effect (that is, create files). |
| 9118 | * Try "a=t >file" |
| 9119 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9120 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9121 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9122 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9123 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9124 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9125 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9126 | /* Set shell variables */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9127 | i = 0; |
| 9128 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9129 | char *p = expand_string_to_string(argv[i], |
| 9130 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9131 | /*unbackslash:*/ 1 |
| 9132 | ); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9133 | #if ENABLE_HUSH_MODE_X |
| 9134 | if (G_x_mode) { |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9135 | char *eq; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9136 | if (i == 0) |
| 9137 | x_mode_prefix(); |
| 9138 | x_mode_addchr(' '); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9139 | eq = strchrnul(p, '='); |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9140 | if (*eq) eq++; |
| 9141 | x_mode_addblock(p, (eq - p)); |
| 9142 | x_mode_print_optionally_squoted(eq); |
| 9143 | x_mode_flush(); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9144 | } |
| 9145 | #endif |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9146 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9147 | if (set_local_var(p, /*flag:*/ 0)) { |
| 9148 | /* assignment to readonly var / putenv error? */ |
| 9149 | rcode = 1; |
| 9150 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9151 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9152 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9153 | /* Redirect error sets $? to 1. Otherwise, |
| 9154 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9155 | * Else, clear $?: |
| 9156 | * false; q=`exit 2`; echo $? - should print 2 |
| 9157 | * false; x=1; echo $? - should print 0 |
| 9158 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 9159 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9160 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9161 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9162 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9163 | debug_leave(); |
| 9164 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 9165 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9166 | } |
| 9167 | |
| 9168 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 9169 | #if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 9170 | if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB) |
| 9171 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 9172 | else |
| 9173 | #endif |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 9174 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9175 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9176 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9177 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9178 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9179 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9180 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9181 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9182 | if (!argv_expanded[0]) { |
| 9183 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9184 | /* `false` still has to set exitcode 1 */ |
| 9185 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9186 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9187 | } |
| 9188 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9189 | old_vars = NULL; |
| 9190 | sv_shadowed = G.shadowed_vars_pp; |
| 9191 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9192 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9193 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 9194 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 9195 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9196 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9197 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9198 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 9199 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9200 | /* |
| 9201 | * Variable assignments are executed, but then "forgotten": |
| 9202 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 9203 | * sleeps, but prints nothing. |
| 9204 | */ |
| 9205 | enter_var_nest_level(); |
| 9206 | G.shadowed_vars_pp = &old_vars; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 9207 | rcode = redirect_and_varexp_helper(command, |
| 9208 | /*squirrel:*/ ERR_PTR, |
| 9209 | argv_expanded |
| 9210 | ); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9211 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9212 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9213 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9214 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9215 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9216 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9217 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9218 | * a=b true; env | grep ^a= |
| 9219 | */ |
| 9220 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9221 | /* Collect all variables "shadowed" by helper |
| 9222 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 9223 | * into old_vars list: |
| 9224 | */ |
| 9225 | G.shadowed_vars_pp = &old_vars; |
| 9226 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9227 | if (rcode == 0) { |
| 9228 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9229 | /* Do not collect *to old_vars list* vars shadowed |
| 9230 | * by e.g. "local VAR" builtin (collect them |
| 9231 | * in the previously nested list instead): |
| 9232 | * don't want them to be restored immediately |
| 9233 | * after "local" completes. |
| 9234 | */ |
| 9235 | G.shadowed_vars_pp = sv_shadowed; |
| 9236 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9237 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 9238 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9239 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9240 | rcode = x->b_function(argv_expanded) & 0xff; |
| 9241 | fflush_all(); |
| 9242 | } |
| 9243 | #if ENABLE_HUSH_FUNCTIONS |
| 9244 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9245 | debug_printf_exec(": function '%s' '%s'...\n", |
| 9246 | funcp->name, argv_expanded[1]); |
| 9247 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9248 | /* |
| 9249 | * But do collect *to old_vars list* vars shadowed |
| 9250 | * within function execution. To that end, restore |
| 9251 | * this pointer _after_ function run: |
| 9252 | */ |
| 9253 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9254 | } |
| 9255 | #endif |
| 9256 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9257 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9258 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9259 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9260 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 9261 | goto must_fork; |
| 9262 | |
| 9263 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9264 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 9265 | G.shadowed_vars_pp = &old_vars; |
| 9266 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 9267 | G.shadowed_vars_pp = sv_shadowed; |
| 9268 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9269 | if (rcode == 0) { |
| 9270 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 9271 | argv_expanded[0], argv_expanded[1]); |
| 9272 | /* |
| 9273 | * Note: signals (^C) can't interrupt here. |
| 9274 | * We remember them and they will be acted upon |
| 9275 | * after applet returns. |
| 9276 | * This makes applets which can run for a long time |
| 9277 | * and/or wait for user input ineligible for NOFORK: |
| 9278 | * for example, "yes" or "rm" (rm -i waits for input). |
| 9279 | */ |
| 9280 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9281 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 9282 | } else |
| 9283 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9284 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9285 | restore_redirects(squirrel); |
| 9286 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9287 | leave_var_nest_level(); |
| 9288 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9289 | |
| 9290 | /* |
| 9291 | * Try "usleep 99999999" + ^C + "echo $?" |
| 9292 | * with FEATURE_SH_NOFORK=y. |
| 9293 | */ |
| 9294 | if (!funcp) { |
| 9295 | /* It was builtin or nofork. |
| 9296 | * if this would be a real fork/execed program, |
| 9297 | * it should have died if a fatal sig was received. |
| 9298 | * But OTOH, there was no separate process, |
| 9299 | * the sig was sent to _shell_, not to non-existing |
| 9300 | * child. |
| 9301 | * Let's just handle ^C only, this one is obvious: |
| 9302 | * we aren't ok with exitcode 0 when ^C was pressed |
| 9303 | * during builtin/nofork. |
| 9304 | */ |
| 9305 | if (sigismember(&G.pending_set, SIGINT)) |
| 9306 | rcode = 128 + SIGINT; |
| 9307 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9308 | free(argv_expanded); |
| 9309 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9310 | debug_leave(); |
| 9311 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 9312 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9313 | } |
| 9314 | |
| 9315 | must_fork: |
| 9316 | /* NB: argv_expanded may already be created, and that |
| 9317 | * might include `cmd` runs! Do not rerun it! We *must* |
| 9318 | * use argv_expanded if it's non-NULL */ |
| 9319 | |
| 9320 | /* Going to fork a child per each pipe member */ |
| 9321 | pi->alive_cmds = 0; |
| 9322 | next_infd = 0; |
| 9323 | |
| 9324 | cmd_no = 0; |
| 9325 | while (cmd_no < pi->num_cmds) { |
| 9326 | struct fd_pair pipefds; |
| 9327 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9328 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9329 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9330 | nommu_save.old_vars = NULL; |
| 9331 | nommu_save.argv = NULL; |
| 9332 | nommu_save.argv_from_re_execing = NULL; |
| 9333 | #endif |
| 9334 | command = &pi->cmds[cmd_no]; |
| 9335 | cmd_no++; |
| 9336 | if (command->argv) { |
| 9337 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 9338 | command->argv[0], command->argv[1]); |
| 9339 | } else { |
| 9340 | debug_printf_exec(": pipe member with no argv\n"); |
| 9341 | } |
| 9342 | |
| 9343 | /* pipes are inserted between pairs of commands */ |
| 9344 | pipefds.rd = 0; |
| 9345 | pipefds.wr = 1; |
| 9346 | if (cmd_no < pi->num_cmds) |
| 9347 | xpiped_pair(pipefds); |
| 9348 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9349 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 9350 | G.execute_lineno = command->lineno; |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 9351 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9352 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9353 | command->pid = BB_MMU ? fork() : vfork(); |
| 9354 | if (!command->pid) { /* child */ |
| 9355 | #if ENABLE_HUSH_JOB |
| 9356 | disable_restore_tty_pgrp_on_exit(); |
| 9357 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 9358 | |
| 9359 | /* Every child adds itself to new process group |
| 9360 | * with pgid == pid_of_first_child_in_pipe */ |
| 9361 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9362 | pid_t pgrp; |
| 9363 | pgrp = pi->pgrp; |
| 9364 | if (pgrp < 0) /* true for 1st process only */ |
| 9365 | pgrp = getpid(); |
| 9366 | if (setpgid(0, pgrp) == 0 |
| 9367 | && pi->followup != PIPE_BG |
| 9368 | && G_saved_tty_pgrp /* we have ctty */ |
| 9369 | ) { |
| 9370 | /* We do it in *every* child, not just first, |
| 9371 | * to avoid races */ |
| 9372 | tcsetpgrp(G_interactive_fd, pgrp); |
| 9373 | } |
| 9374 | } |
| 9375 | #endif |
| 9376 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 9377 | /* 1st cmd in backgrounded pipe |
| 9378 | * should have its stdin /dev/null'ed */ |
| 9379 | close(0); |
| 9380 | if (open(bb_dev_null, O_RDONLY)) |
| 9381 | xopen("/", O_RDONLY); |
| 9382 | } else { |
| 9383 | xmove_fd(next_infd, 0); |
| 9384 | } |
| 9385 | xmove_fd(pipefds.wr, 1); |
| 9386 | if (pipefds.rd > 1) |
| 9387 | close(pipefds.rd); |
| 9388 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9389 | * and the pipe fd (fd#1) is available for dup'ing: |
| 9390 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 9391 | * of cmd1 goes into pipe. |
| 9392 | */ |
| 9393 | if (setup_redirects(command, NULL)) { |
| 9394 | /* Happens when redir file can't be opened: |
| 9395 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 9396 | * FOO |
| 9397 | * hush: can't open '/qwe/rty': No such file or directory |
| 9398 | * BAZ |
| 9399 | * (echo BAR is not executed, it hits _exit(1) below) |
| 9400 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9401 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9402 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9403 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9404 | /* Stores to nommu_save list of env vars putenv'ed |
| 9405 | * (NOMMU, on MMU we don't need that) */ |
| 9406 | /* cast away volatility... */ |
| 9407 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 9408 | /* pseudo_exec() does not return */ |
| 9409 | } |
| 9410 | |
| 9411 | /* parent or error */ |
| 9412 | #if ENABLE_HUSH_FAST |
| 9413 | G.count_SIGCHLD++; |
| 9414 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 9415 | #endif |
| 9416 | enable_restore_tty_pgrp_on_exit(); |
| 9417 | #if !BB_MMU |
| 9418 | /* Clean up after vforked child */ |
| 9419 | free(nommu_save.argv); |
| 9420 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9421 | G.var_nest_level = sv_var_nest_level; |
| 9422 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9423 | add_vars(nommu_save.old_vars); |
| 9424 | #endif |
| 9425 | free(argv_expanded); |
| 9426 | argv_expanded = NULL; |
| 9427 | if (command->pid < 0) { /* [v]fork failed */ |
| 9428 | /* Clearly indicate, was it fork or vfork */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 9429 | bb_simple_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9430 | } else { |
| 9431 | pi->alive_cmds++; |
| 9432 | #if ENABLE_HUSH_JOB |
| 9433 | /* Second and next children need to know pid of first one */ |
| 9434 | if (pi->pgrp < 0) |
| 9435 | pi->pgrp = command->pid; |
| 9436 | #endif |
| 9437 | } |
| 9438 | |
| 9439 | if (cmd_no > 1) |
| 9440 | close(next_infd); |
| 9441 | if (cmd_no < pi->num_cmds) |
| 9442 | close(pipefds.wr); |
| 9443 | /* Pass read (output) pipe end to next iteration */ |
| 9444 | next_infd = pipefds.rd; |
| 9445 | } |
| 9446 | |
| 9447 | if (!pi->alive_cmds) { |
| 9448 | debug_leave(); |
| 9449 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 9450 | return 1; |
| 9451 | } |
| 9452 | |
| 9453 | debug_leave(); |
| 9454 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 9455 | return -1; |
| 9456 | } |
| 9457 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9458 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 9459 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 9460 | static int run_list(struct pipe *pi) |
| 9461 | { |
| 9462 | #if ENABLE_HUSH_CASE |
| 9463 | char *case_word = NULL; |
| 9464 | #endif |
| 9465 | #if ENABLE_HUSH_LOOPS |
| 9466 | struct pipe *loop_top = NULL; |
| 9467 | char **for_lcur = NULL; |
| 9468 | char **for_list = NULL; |
| 9469 | #endif |
| 9470 | smallint last_followup; |
| 9471 | smalluint rcode; |
| 9472 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 9473 | smalluint cond_code = 0; |
| 9474 | #else |
| 9475 | enum { cond_code = 0 }; |
| 9476 | #endif |
| 9477 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 9478 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9479 | smallint last_rword; /* ditto */ |
| 9480 | #endif |
| 9481 | |
| 9482 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 9483 | debug_enter(); |
| 9484 | |
| 9485 | #if ENABLE_HUSH_LOOPS |
| 9486 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 9487 | { |
| 9488 | struct pipe *cpipe; |
| 9489 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 9490 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 9491 | continue; |
| 9492 | /* current word is FOR or IN (BOLD in comments below) */ |
| 9493 | if (cpipe->next == NULL) { |
| 9494 | syntax_error("malformed for"); |
| 9495 | debug_leave(); |
| 9496 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9497 | return 1; |
| 9498 | } |
| 9499 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 9500 | if (cpipe->next->res_word == RES_DO) |
| 9501 | continue; |
| 9502 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 9503 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 9504 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 9505 | ) { |
| 9506 | syntax_error("malformed for"); |
| 9507 | debug_leave(); |
| 9508 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9509 | return 1; |
| 9510 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9511 | } |
| 9512 | } |
| 9513 | #endif |
| 9514 | |
| 9515 | /* Past this point, all code paths should jump to ret: label |
| 9516 | * in order to return, no direct "return" statements please. |
| 9517 | * This helps to ensure that no memory is leaked. */ |
| 9518 | |
| 9519 | #if ENABLE_HUSH_JOB |
| 9520 | G.run_list_level++; |
| 9521 | #endif |
| 9522 | |
| 9523 | #if HAS_KEYWORDS |
| 9524 | rword = RES_NONE; |
| 9525 | last_rword = RES_XXXX; |
| 9526 | #endif |
| 9527 | last_followup = PIPE_SEQ; |
| 9528 | rcode = G.last_exitcode; |
| 9529 | |
| 9530 | /* Go through list of pipes, (maybe) executing them. */ |
| 9531 | 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] | 9532 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9533 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9534 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9535 | if (G.flag_SIGINT) |
| 9536 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9537 | if (G_flag_return_in_progress == 1) |
| 9538 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9539 | |
| 9540 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 9541 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 9542 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9543 | |
| 9544 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9545 | if ( |
| 9546 | #if ENABLE_HUSH_IF |
| 9547 | rword == RES_IF || rword == RES_ELIF || |
| 9548 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9549 | pi->followup != PIPE_SEQ |
| 9550 | ) { |
| 9551 | G.errexit_depth++; |
| 9552 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9553 | #if ENABLE_HUSH_LOOPS |
| 9554 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 9555 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 9556 | ) { |
| 9557 | /* start of a loop: remember where loop starts */ |
| 9558 | loop_top = pi; |
| 9559 | G.depth_of_loop++; |
| 9560 | } |
| 9561 | #endif |
| 9562 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 9563 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 9564 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 9565 | || (rcode != 0 && last_followup == PIPE_AND) |
| 9566 | ) { |
| 9567 | /* It is "<true> || CMD" or "<false> && CMD" |
| 9568 | * and we should not execute CMD */ |
| 9569 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 9570 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9571 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9572 | } |
| 9573 | } |
| 9574 | last_followup = pi->followup; |
| 9575 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 9576 | #if ENABLE_HUSH_IF |
| 9577 | if (cond_code) { |
| 9578 | if (rword == RES_THEN) { |
| 9579 | /* if false; then ... fi has exitcode 0! */ |
| 9580 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9581 | /* "if <false> THEN cmd": skip cmd */ |
| 9582 | continue; |
| 9583 | } |
| 9584 | } else { |
| 9585 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 9586 | /* "if <true> then ... ELSE/ELIF cmd": |
| 9587 | * skip cmd and all following ones */ |
| 9588 | break; |
| 9589 | } |
| 9590 | } |
| 9591 | #endif |
| 9592 | #if ENABLE_HUSH_LOOPS |
| 9593 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 9594 | if (!for_lcur) { |
| 9595 | /* first loop through for */ |
| 9596 | |
| 9597 | static const char encoded_dollar_at[] ALIGN1 = { |
| 9598 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 9599 | }; /* encoded representation of "$@" */ |
| 9600 | static const char *const encoded_dollar_at_argv[] = { |
| 9601 | encoded_dollar_at, NULL |
| 9602 | }; /* argv list with one element: "$@" */ |
| 9603 | char **vals; |
| 9604 | |
Denys Vlasenko | a5db1d7 | 2018-07-28 12:42:08 +0200 | [diff] [blame] | 9605 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9606 | vals = (char**)encoded_dollar_at_argv; |
| 9607 | if (pi->next->res_word == RES_IN) { |
| 9608 | /* if no variable values after "in" we skip "for" */ |
| 9609 | if (!pi->next->cmds[0].argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9610 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 9611 | break; |
| 9612 | } |
| 9613 | vals = pi->next->cmds[0].argv; |
| 9614 | } /* else: "for var; do..." -> assume "$@" list */ |
| 9615 | /* create list of variable values */ |
| 9616 | debug_print_strings("for_list made from", vals); |
| 9617 | for_list = expand_strvec_to_strvec(vals); |
| 9618 | for_lcur = for_list; |
| 9619 | debug_print_strings("for_list", for_list); |
| 9620 | } |
| 9621 | if (!*for_lcur) { |
| 9622 | /* "for" loop is over, clean up */ |
| 9623 | free(for_list); |
| 9624 | for_list = NULL; |
| 9625 | for_lcur = NULL; |
| 9626 | break; |
| 9627 | } |
| 9628 | /* Insert next value from for_lcur */ |
| 9629 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9630 | 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] | 9631 | continue; |
| 9632 | } |
| 9633 | if (rword == RES_IN) { |
| 9634 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 9635 | } |
| 9636 | if (rword == RES_DONE) { |
| 9637 | continue; /* "done" has no cmds too */ |
| 9638 | } |
| 9639 | #endif |
| 9640 | #if ENABLE_HUSH_CASE |
| 9641 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9642 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9643 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 9644 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 9645 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 9646 | //unbackslash(case_word); |
| 9647 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9648 | continue; |
| 9649 | } |
| 9650 | if (rword == RES_MATCH) { |
| 9651 | char **argv; |
| 9652 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9653 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9654 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 9655 | break; |
| 9656 | /* all prev words didn't match, does this one match? */ |
| 9657 | argv = pi->cmds->argv; |
| 9658 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9659 | char *pattern; |
| 9660 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 9661 | pattern = expand_string_to_string(*argv, |
| 9662 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9663 | /*unbackslash:*/ 0 |
| 9664 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9665 | /* TODO: which FNM_xxx flags to use? */ |
| 9666 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9667 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 9668 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9669 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9670 | if (cond_code == 0) { |
| 9671 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9672 | free(case_word); |
| 9673 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9674 | break; |
| 9675 | } |
| 9676 | argv++; |
| 9677 | } |
| 9678 | continue; |
| 9679 | } |
| 9680 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9681 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9682 | if (cond_code != 0) |
| 9683 | continue; /* not matched yet, skip this pipe */ |
| 9684 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9685 | if (rword == RES_ESAC) { |
| 9686 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 9687 | if (case_word) { |
| 9688 | /* "case" did not match anything: still set $? (to 0) */ |
| 9689 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9690 | } |
| 9691 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9692 | #endif |
| 9693 | /* Just pressing <enter> in shell should check for jobs. |
| 9694 | * OTOH, in non-interactive shell this is useless |
| 9695 | * and only leads to extra job checks */ |
| 9696 | if (pi->num_cmds == 0) { |
| 9697 | if (G_interactive_fd) |
| 9698 | goto check_jobs_and_continue; |
| 9699 | continue; |
| 9700 | } |
| 9701 | |
| 9702 | /* After analyzing all keywords and conditions, we decided |
| 9703 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 9704 | * after run_pipe to collect any background children, |
| 9705 | * even if list execution is to be stopped. */ |
| 9706 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9707 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9708 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9709 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9710 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 9711 | if (r != -1) { |
| 9712 | /* We ran a builtin, function, or group. |
| 9713 | * rcode is already known |
| 9714 | * and we don't need to wait for anything. */ |
| 9715 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 9716 | G.last_exitcode = rcode; |
| 9717 | check_and_run_traps(); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 9718 | #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS |
| 9719 | rcode = G.last_exitcode; /* "return" in trap can change it, read back */ |
| 9720 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9721 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9722 | /* Was it "break" or "continue"? */ |
| 9723 | if (G.flag_break_continue) { |
| 9724 | smallint fbc = G.flag_break_continue; |
| 9725 | /* We might fall into outer *loop*, |
| 9726 | * don't want to break it too */ |
| 9727 | if (loop_top) { |
| 9728 | G.depth_break_continue--; |
| 9729 | if (G.depth_break_continue == 0) |
| 9730 | G.flag_break_continue = 0; |
| 9731 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9732 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9733 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9734 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9735 | break; |
| 9736 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9737 | /* "continue": simulate end of loop */ |
| 9738 | rword = RES_DONE; |
| 9739 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9740 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9741 | #endif |
| 9742 | if (G_flag_return_in_progress == 1) { |
| 9743 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9744 | break; |
| 9745 | } |
| 9746 | } else if (pi->followup == PIPE_BG) { |
| 9747 | /* What does bash do with attempts to background builtins? */ |
| 9748 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9749 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9750 | * I'm NOT treating inner &'s as jobs */ |
| 9751 | #if ENABLE_HUSH_JOB |
| 9752 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9753 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9754 | #endif |
| 9755 | /* Last command's pid goes to $! */ |
| 9756 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9757 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9758 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9759 | /* 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] | 9760 | rcode = EXIT_SUCCESS; |
| 9761 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9762 | } else { |
| 9763 | #if ENABLE_HUSH_JOB |
| 9764 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9765 | /* Waits for completion, then fg's main shell */ |
| 9766 | rcode = checkjobs_and_fg_shell(pi); |
| 9767 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9768 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9769 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9770 | #endif |
| 9771 | /* This one just waits for completion */ |
| 9772 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 9773 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 9774 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9775 | G.last_exitcode = rcode; |
| 9776 | check_and_run_traps(); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 9777 | #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS |
| 9778 | rcode = G.last_exitcode; /* "return" in trap can change it, read back */ |
| 9779 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9780 | } |
| 9781 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9782 | /* Handle "set -e" */ |
| 9783 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 9784 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 9785 | if (G.errexit_depth == 0) |
| 9786 | hush_exit(rcode); |
| 9787 | } |
| 9788 | G.errexit_depth = sv_errexit_depth; |
| 9789 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9790 | /* Analyze how result affects subsequent commands */ |
| 9791 | #if ENABLE_HUSH_IF |
| 9792 | if (rword == RES_IF || rword == RES_ELIF) |
| 9793 | cond_code = rcode; |
| 9794 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9795 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9796 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9797 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9798 | #if ENABLE_HUSH_LOOPS |
| 9799 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9800 | if (pi->next |
| 9801 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9802 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9803 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9804 | if (rword == RES_WHILE) { |
| 9805 | if (rcode) { |
| 9806 | /* "while false; do...done" - exitcode 0 */ |
| 9807 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9808 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9809 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9810 | } |
| 9811 | } |
| 9812 | if (rword == RES_UNTIL) { |
| 9813 | if (!rcode) { |
| 9814 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9815 | break; |
| 9816 | } |
| 9817 | } |
| 9818 | } |
| 9819 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9820 | } /* for (pi) */ |
| 9821 | |
| 9822 | #if ENABLE_HUSH_JOB |
| 9823 | G.run_list_level--; |
| 9824 | #endif |
| 9825 | #if ENABLE_HUSH_LOOPS |
| 9826 | if (loop_top) |
| 9827 | G.depth_of_loop--; |
| 9828 | free(for_list); |
| 9829 | #endif |
| 9830 | #if ENABLE_HUSH_CASE |
| 9831 | free(case_word); |
| 9832 | #endif |
| 9833 | debug_leave(); |
| 9834 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9835 | return rcode; |
| 9836 | } |
| 9837 | |
| 9838 | /* Select which version we will use */ |
| 9839 | static int run_and_free_list(struct pipe *pi) |
| 9840 | { |
| 9841 | int rcode = 0; |
| 9842 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9843 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9844 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9845 | rcode = run_list(pi); |
| 9846 | } |
| 9847 | /* free_pipe_list has the side effect of clearing memory. |
| 9848 | * In the long run that function can be merged with run_list, |
| 9849 | * but doing that now would hobble the debugging effort. */ |
| 9850 | free_pipe_list(pi); |
| 9851 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9852 | return rcode; |
| 9853 | } |
| 9854 | |
| 9855 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9856 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9857 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9858 | sighandler_t old_handler; |
| 9859 | unsigned sig = 0; |
| 9860 | while ((mask >>= 1) != 0) { |
| 9861 | sig++; |
| 9862 | if (!(mask & 1)) |
| 9863 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9864 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9865 | /* POSIX allows shell to re-enable SIGCHLD |
| 9866 | * even if it was SIG_IGN on entry. |
| 9867 | * Therefore we skip IGN check for it: |
| 9868 | */ |
| 9869 | if (sig == SIGCHLD) |
| 9870 | continue; |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 9871 | /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9872 | * Try: |
| 9873 | * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect |
| 9874 | * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9875 | */ |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 9876 | if (sig == SIGHUP && G_interactive_fd) |
| 9877 | continue; |
| 9878 | /* Unless one of the above signals, is it SIG_IGN? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9879 | if (old_handler == SIG_IGN) { |
| 9880 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9881 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9882 | #if ENABLE_HUSH_TRAP |
| 9883 | if (!G_traps) |
| 9884 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9885 | free(G_traps[sig]); |
| 9886 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9887 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9888 | } |
| 9889 | } |
| 9890 | } |
| 9891 | |
| 9892 | /* Called a few times only (or even once if "sh -c") */ |
| 9893 | static void install_special_sighandlers(void) |
| 9894 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9895 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9896 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9897 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9898 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9899 | if (G_interactive_fd) { |
| 9900 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9901 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9902 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9903 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9904 | /* Careful, do not re-install handlers we already installed */ |
| 9905 | if (G.special_sig_mask != mask) { |
| 9906 | unsigned diff = mask & ~G.special_sig_mask; |
| 9907 | G.special_sig_mask = mask; |
| 9908 | install_sighandlers(diff); |
| 9909 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9910 | } |
| 9911 | |
| 9912 | #if ENABLE_HUSH_JOB |
| 9913 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9914 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9915 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9916 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9917 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9918 | |
| 9919 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9920 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9921 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9922 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9923 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9924 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9925 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9926 | + (1 << SIGABRT) |
| 9927 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9928 | + (1 << SIGPIPE) |
| 9929 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9930 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9931 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9932 | * we never want to restore pgrp on exit, and this fn is not called |
| 9933 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9934 | /*+ (1 << SIGHUP )*/ |
| 9935 | /*+ (1 << SIGTERM)*/ |
| 9936 | /*+ (1 << SIGINT )*/ |
| 9937 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9938 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9939 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9940 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9941 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9942 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9943 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9944 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9945 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9946 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9947 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9948 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9949 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9950 | break; |
| 9951 | case 'x': |
| 9952 | IF_HUSH_MODE_X(G_x_mode = state;) |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9953 | IF_HUSH_MODE_X(if (G.x_mode_fd <= 0) G.x_mode_fd = dup_CLOEXEC(2, 10);) |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9954 | break; |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 9955 | case 'e': |
| 9956 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9957 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9958 | case 'o': |
| 9959 | if (!o_opt) { |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 9960 | /* "set -o" or "set +o" without parameter. |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9961 | * in bash, set -o produces this output: |
| 9962 | * pipefail off |
| 9963 | * and set +o: |
| 9964 | * set +o pipefail |
| 9965 | * We always use the second form. |
| 9966 | */ |
| 9967 | const char *p = o_opt_strings; |
| 9968 | idx = 0; |
| 9969 | while (*p) { |
| 9970 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9971 | idx++; |
| 9972 | p += strlen(p) + 1; |
| 9973 | } |
| 9974 | break; |
| 9975 | } |
| 9976 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9977 | if (idx >= 0) { |
| 9978 | G.o_opt[idx] = state; |
| 9979 | break; |
| 9980 | } |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 9981 | /* fall through to error */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9982 | default: |
| 9983 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9984 | } |
| 9985 | return EXIT_SUCCESS; |
| 9986 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9987 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9988 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9989 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9990 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9991 | enum { |
| 9992 | OPT_login = (1 << 0), |
| 9993 | }; |
| 9994 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9995 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9996 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9997 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9998 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9999 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 10000 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 10001 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10002 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10003 | #if ENABLE_HUSH_TRAP |
| 10004 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 10005 | G.return_exitcode = -1; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10006 | # endif |
| 10007 | G.pre_trap_exitcode = -1; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 10008 | #endif |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 10009 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 10010 | #if ENABLE_HUSH_FAST |
| 10011 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 10012 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10013 | #if !BB_MMU |
| 10014 | G.argv0_for_re_execing = argv[0]; |
| 10015 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10016 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 10017 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10018 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 10019 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10020 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 10021 | shell_ver->flg_export = 1; |
| 10022 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 10023 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 10024 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10025 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10026 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 10027 | /* Create shell local variables from the values |
| 10028 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10029 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10030 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 10031 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10032 | if (e) while (*e) { |
| 10033 | char *value = strchr(*e, '='); |
| 10034 | if (value) { /* paranoia */ |
| 10035 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 10036 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 10037 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10038 | cur_var->max_len = strlen(*e); |
| 10039 | cur_var->flg_export = 1; |
| 10040 | } |
| 10041 | e++; |
| 10042 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 10043 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10044 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 10045 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10046 | |
| 10047 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10048 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 10049 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10050 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 10051 | /* Set (but not export) HOSTNAME unless already set */ |
| 10052 | if (!get_local_var_value("HOSTNAME")) { |
| 10053 | struct utsname uts; |
| 10054 | uname(&uts); |
| 10055 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 10056 | } |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 10057 | #endif |
| 10058 | /* IFS is not inherited from the parent environment */ |
| 10059 | set_local_var_from_halves("IFS", defifs); |
| 10060 | |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 10061 | if (!get_local_var_value("PATH")) |
| 10062 | set_local_var_from_halves("PATH", bb_default_root_path); |
| 10063 | |
Denys Vlasenko | 0c36019 | 2019-05-19 15:37:50 +0200 | [diff] [blame] | 10064 | /* PS1/PS2 are set later, if we determine that we are interactive */ |
| 10065 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10066 | /* bash also exports SHLVL and _, |
| 10067 | * and sets (but doesn't export) the following variables: |
| 10068 | * BASH=/bin/bash |
| 10069 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 10070 | * BASH_VERSION='3.2.0(1)-release' |
| 10071 | * HOSTTYPE=i386 |
| 10072 | * MACHTYPE=i386-pc-linux-gnu |
| 10073 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10074 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10075 | * EUID=<NNNNN> |
| 10076 | * UID=<NNNNN> |
| 10077 | * GROUPS=() |
| 10078 | * LINES=<NNN> |
| 10079 | * COLUMNS=<NNN> |
| 10080 | * BASH_ARGC=() |
| 10081 | * BASH_ARGV=() |
| 10082 | * BASH_LINENO=() |
| 10083 | * BASH_SOURCE=() |
| 10084 | * DIRSTACK=() |
| 10085 | * PIPESTATUS=([0]="0") |
| 10086 | * HISTFILE=/<xxx>/.bash_history |
| 10087 | * HISTFILESIZE=500 |
| 10088 | * HISTSIZE=500 |
| 10089 | * MAILCHECK=60 |
| 10090 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 10091 | * SHELL=/bin/bash |
| 10092 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 10093 | * TERM=dumb |
| 10094 | * OPTERR=1 |
| 10095 | * OPTIND=1 |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10096 | * PS4='+ ' |
| 10097 | */ |
| 10098 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 10099 | /* Initialize some more globals to non-zero values */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 10100 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 10101 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10102 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10103 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10104 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10105 | * 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] | 10106 | * in order to intercept (more) signals. |
| 10107 | */ |
| 10108 | |
| 10109 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10110 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10111 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10112 | builtin_argc = 0; |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 10113 | #if NUM_SCRIPTS > 0 |
| 10114 | if (argc < 0) { |
| 10115 | optarg = get_script_content(-argc - 1); |
| 10116 | optind = 0; |
| 10117 | argc = string_array_len(argv); |
| 10118 | goto run_script; |
| 10119 | } |
| 10120 | #endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10121 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10122 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10123 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10124 | "<:$:R:V:" |
| 10125 | # if ENABLE_HUSH_FUNCTIONS |
| 10126 | "F:" |
| 10127 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10128 | #endif |
| 10129 | ); |
| 10130 | if (opt <= 0) |
| 10131 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10132 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10133 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10134 | /* Possibilities: |
| 10135 | * sh ... -c 'script' |
| 10136 | * sh ... -c 'script' ARG0 [ARG1...] |
| 10137 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10138 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10139 | * "" needs to be replaced with NULL |
| 10140 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10141 | * Note: the form without ARG0 never happens: |
| 10142 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10143 | */ |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 10144 | #if NUM_SCRIPTS > 0 |
| 10145 | run_script: |
| 10146 | #endif |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10147 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10148 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10149 | G.root_ppid = getppid(); |
| 10150 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10151 | G.global_argv = argv + optind; |
| 10152 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10153 | if (builtin_argc) { |
| 10154 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 10155 | const struct built_in_command *x; |
| 10156 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10157 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10158 | x = find_builtin(optarg); |
| 10159 | if (x) { /* paranoia */ |
| 10160 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 10161 | G.global_argv += builtin_argc; |
| 10162 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 10163 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 10164 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10165 | } |
| 10166 | goto final_return; |
| 10167 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10168 | G.opt_c = 1; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10169 | if (!G.global_argv[0]) { |
| 10170 | /* -c 'script' (no params): prevent empty $0 */ |
| 10171 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 10172 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 10173 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10174 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10175 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10176 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10177 | goto final_return; |
| 10178 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 10179 | /* Well, we cannot just declare interactiveness, |
| 10180 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10181 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10182 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10183 | case 's': |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10184 | G.opt_s = 1; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10185 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10186 | case 'l': |
| 10187 | flags |= OPT_login; |
| 10188 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10189 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10190 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 10191 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10192 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10193 | case '$': { |
| 10194 | unsigned long long empty_trap_mask; |
| 10195 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10196 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 10197 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10198 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 10199 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10200 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 10201 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10202 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10203 | optarg++; |
| 10204 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10205 | optarg++; |
| 10206 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 10207 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10208 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10209 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10210 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10211 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10212 | for (sig = 1; sig < NSIG; sig++) { |
| 10213 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10214 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10215 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10216 | } |
| 10217 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10218 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10219 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10220 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10221 | optarg++; |
| 10222 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10223 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 10224 | # if ENABLE_HUSH_FUNCTIONS |
| 10225 | /* nommu uses re-exec trick for "... | func | ...", |
| 10226 | * should allow "return". |
| 10227 | * This accidentally allows returns in subshells. |
| 10228 | */ |
| 10229 | G_flag_return_in_progress = -1; |
| 10230 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10231 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10232 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10233 | case 'R': |
| 10234 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10235 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10236 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10237 | # if ENABLE_HUSH_FUNCTIONS |
| 10238 | case 'F': { |
| 10239 | struct function *funcp = new_function(optarg); |
| 10240 | /* funcp->name is already set to optarg */ |
| 10241 | /* funcp->body is set to NULL. It's a special case. */ |
| 10242 | funcp->body_as_string = argv[optind]; |
| 10243 | optind++; |
| 10244 | break; |
| 10245 | } |
| 10246 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10247 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 10248 | case 'n': |
| 10249 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 10250 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10251 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 10252 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10253 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10254 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10255 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 10256 | " or: sh -c command [args]...\n\n"); |
| 10257 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10258 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10259 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 10260 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10261 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10262 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10263 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10264 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 10265 | G.global_argc = argc - (optind - 1); |
| 10266 | G.global_argv = argv + (optind - 1); |
| 10267 | G.global_argv[0] = argv[0]; |
| 10268 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10269 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10270 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10271 | G.root_ppid = getppid(); |
| 10272 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10273 | |
| 10274 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10275 | if (flags & OPT_login) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10276 | HFILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10277 | debug_printf("sourcing /etc/profile\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10278 | input = hfopen("/etc/profile"); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10279 | if (input != NULL) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10280 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10281 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10282 | hfclose(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10283 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10284 | /* bash: after sourcing /etc/profile, |
| 10285 | * tries to source (in the given order): |
| 10286 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10287 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10288 | * bash also sources ~/.bash_logout on exit. |
| 10289 | * If called as sh, skips .bash_XXX files. |
| 10290 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10291 | } |
| 10292 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10293 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10294 | if (!G.opt_s && G.global_argv[1]) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10295 | HFILE *input; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10296 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10297 | * "bash <script>" (which is never interactive (unless -i?)) |
| 10298 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10299 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 10300 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10301 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10302 | G.global_argc--; |
| 10303 | G.global_argv++; |
| 10304 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10305 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10306 | input = hfopen(G.global_argv[0]); |
| 10307 | if (!input) { |
| 10308 | bb_simple_perror_msg_and_die(G.global_argv[0]); |
| 10309 | } |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10310 | xfunc_error_retval = 1; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10311 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10312 | parse_and_run_file(input); |
| 10313 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10314 | hfclose(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10315 | #endif |
| 10316 | goto final_return; |
| 10317 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10318 | /* "implicit" -s: bare interactive hush shows 's' in $- */ |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 10319 | G.opt_s = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10320 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10321 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10322 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10323 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10324 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10325 | /* A shell is interactive if the '-i' flag was given, |
| 10326 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 10327 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10328 | * no arguments remaining or the -s flag given |
| 10329 | * standard input is a terminal |
| 10330 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10331 | * Refer to Posix.2, the description of the 'sh' utility. |
| 10332 | */ |
| 10333 | #if ENABLE_HUSH_JOB |
| 10334 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10335 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 10336 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 10337 | if (G_saved_tty_pgrp < 0) |
| 10338 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10339 | |
| 10340 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10341 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10342 | if (G_interactive_fd < 0) { |
| 10343 | /* try to dup to any fd */ |
| 10344 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10345 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10346 | /* give up */ |
| 10347 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10348 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 10349 | } |
| 10350 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10351 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10352 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10353 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10354 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10355 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10356 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10357 | /* If we were run as 'hush &', sleep until we are |
| 10358 | * in the foreground (tty pgrp == our pgrp). |
| 10359 | * If we get started under a job aware app (like bash), |
| 10360 | * make sure we are now in charge so we don't fight over |
| 10361 | * who gets the foreground */ |
| 10362 | while (1) { |
| 10363 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10364 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 10365 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10366 | break; |
| 10367 | /* send TTIN to ourself (should stop us) */ |
| 10368 | kill(- shell_pgrp, SIGTTIN); |
| 10369 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10370 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10371 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10372 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10373 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10374 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10375 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10376 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10377 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10378 | /* Put ourselves in our own process group |
| 10379 | * (bash, too, does this only if ctty is available) */ |
| 10380 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 10381 | /* Grab control of the terminal */ |
| 10382 | tcsetpgrp(G_interactive_fd, getpid()); |
| 10383 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 10384 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10385 | |
Denys Vlasenko | 76a4e83 | 2019-05-19 18:24:52 +0200 | [diff] [blame] | 10386 | # if ENABLE_FEATURE_EDITING |
| 10387 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 10388 | # if EDITING_HAS_get_exe_name |
| 10389 | G.line_input_state->get_exe_name = get_builtin_name; |
| 10390 | # endif |
Denys Vlasenko | 76a4e83 | 2019-05-19 18:24:52 +0200 | [diff] [blame] | 10391 | # endif |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10392 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 10393 | { |
| 10394 | const char *hp = get_local_var_value("HISTFILE"); |
| 10395 | if (!hp) { |
| 10396 | hp = get_local_var_value("HOME"); |
| 10397 | if (hp) |
| 10398 | hp = concat_path_file(hp, ".hush_history"); |
| 10399 | } else { |
| 10400 | hp = xstrdup(hp); |
| 10401 | } |
| 10402 | if (hp) { |
| 10403 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10404 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 10405 | } |
| 10406 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 10407 | hp = get_local_var_value("HISTFILESIZE"); |
| 10408 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 10409 | # endif |
| 10410 | } |
| 10411 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10412 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10413 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10414 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10415 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10416 | /* No job control compiled in, only prompt/line editing */ |
| 10417 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10418 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10419 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10420 | /* try to dup to any fd */ |
Denys Vlasenko | d1a8323 | 2018-06-26 15:50:33 +0200 | [diff] [blame] | 10421 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10422 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10423 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10424 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10425 | } |
| 10426 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10427 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10428 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10429 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10430 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10431 | #else |
| 10432 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10433 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10434 | #endif |
| 10435 | /* bash: |
| 10436 | * if interactive but not a login shell, sources ~/.bashrc |
| 10437 | * (--norc turns this off, --rcfile <file> overrides) |
| 10438 | */ |
| 10439 | |
Denys Vlasenko | 0c36019 | 2019-05-19 15:37:50 +0200 | [diff] [blame] | 10440 | if (G_interactive_fd) { |
| 10441 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 10442 | /* Set (but not export) PS1/2 unless already set */ |
| 10443 | if (!get_local_var_value("PS1")) |
| 10444 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 10445 | if (!get_local_var_value("PS2")) |
| 10446 | set_local_var_from_halves("PS2", "> "); |
| 10447 | #endif |
| 10448 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET) { |
| 10449 | /* note: ash and hush share this string */ |
| 10450 | printf("\n\n%s %s\n" |
| 10451 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 10452 | "\n", |
| 10453 | bb_banner, |
| 10454 | "hush - the humble shell" |
| 10455 | ); |
| 10456 | } |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 10457 | } |
| 10458 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10459 | parse_and_run_file(hfopen(NULL)); /* stdin */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10460 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10461 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10462 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10463 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 10464 | |
| 10465 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10466 | /* |
| 10467 | * Built-ins |
| 10468 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10469 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10470 | { |
| 10471 | return 0; |
| 10472 | } |
| 10473 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10474 | #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] | 10475 | 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] | 10476 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10477 | int argc = string_array_len(argv); |
| 10478 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10479 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10480 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10481 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10482 | static int FAST_FUNC builtin_test(char **argv) |
| 10483 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10484 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10485 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10486 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10487 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10488 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10489 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10490 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10491 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10492 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10493 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10494 | static int FAST_FUNC builtin_printf(char **argv) |
| 10495 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10496 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10497 | } |
| 10498 | #endif |
| 10499 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10500 | #if ENABLE_HUSH_HELP |
| 10501 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 10502 | { |
| 10503 | const struct built_in_command *x; |
| 10504 | |
| 10505 | printf( |
| 10506 | "Built-in commands:\n" |
| 10507 | "------------------\n"); |
| 10508 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 10509 | if (x->b_descr) |
| 10510 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 10511 | } |
| 10512 | return EXIT_SUCCESS; |
| 10513 | } |
| 10514 | #endif |
| 10515 | |
| 10516 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 10517 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 10518 | { |
Ron Yorston | 9f3b410 | 2019-12-16 09:31:10 +0000 | [diff] [blame] | 10519 | show_history(G.line_input_state); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10520 | return EXIT_SUCCESS; |
| 10521 | } |
| 10522 | #endif |
| 10523 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10524 | static char **skip_dash_dash(char **argv) |
| 10525 | { |
| 10526 | argv++; |
| 10527 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 10528 | argv++; |
| 10529 | return argv; |
| 10530 | } |
| 10531 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10532 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10533 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10534 | const char *newdir; |
| 10535 | |
| 10536 | argv = skip_dash_dash(argv); |
| 10537 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10538 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10539 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10540 | * bash says "bash: cd: HOME not set" and does nothing |
| 10541 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10542 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 10543 | const char *home = get_local_var_value("HOME"); |
| 10544 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 10545 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10546 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10547 | /* Mimic bash message exactly */ |
| 10548 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10549 | return EXIT_FAILURE; |
| 10550 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10551 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 10552 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 10553 | * set it again, but do not export. bash does the same. |
| 10554 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10555 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10556 | return EXIT_SUCCESS; |
| 10557 | } |
| 10558 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10559 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 10560 | { |
| 10561 | puts(get_cwd(0)); |
| 10562 | return EXIT_SUCCESS; |
| 10563 | } |
| 10564 | |
| 10565 | static int FAST_FUNC builtin_eval(char **argv) |
| 10566 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10567 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10568 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10569 | if (!argv[0]) |
| 10570 | return EXIT_SUCCESS; |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10571 | |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10572 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10573 | //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10574 | if (!argv[1]) { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10575 | /* bash: |
| 10576 | * eval "echo Hi; done" ("done" is syntax error): |
| 10577 | * "echo Hi" will not execute too. |
| 10578 | */ |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10579 | parse_and_run_string(argv[0]); |
| 10580 | } else { |
| 10581 | /* "The eval utility shall construct a command by |
| 10582 | * concatenating arguments together, separating |
| 10583 | * each with a <space> character." |
| 10584 | */ |
| 10585 | char *str, *p; |
| 10586 | unsigned len = 0; |
| 10587 | char **pp = argv; |
| 10588 | do |
| 10589 | len += strlen(*pp) + 1; |
| 10590 | while (*++pp); |
| 10591 | str = p = xmalloc(len); |
| 10592 | pp = argv; |
| 10593 | for (;;) { |
| 10594 | p = stpcpy(p, *pp); |
| 10595 | pp++; |
| 10596 | if (!*pp) |
| 10597 | break; |
| 10598 | *p++ = ' '; |
| 10599 | } |
| 10600 | parse_and_run_string(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10601 | free(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10602 | } |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10603 | IF_HUSH_MODE_X(G.x_mode_depth--;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10604 | //bb_error_msg("%s: --x_mode_depth=%d", __func__, G.x_mode_depth); |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10605 | return G.last_exitcode; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10606 | } |
| 10607 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10608 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10609 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10610 | argv = skip_dash_dash(argv); |
| 10611 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10612 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10613 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10614 | /* Careful: we can end up here after [v]fork. Do not restore |
| 10615 | * tty pgrp then, only top-level shell process does that */ |
| 10616 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 10617 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 10618 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 10619 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 10620 | * open here. However, they are all CLOEXEC, and execv below |
| 10621 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 10622 | * it should show no extra open fds in the "ls" process. |
| 10623 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 10624 | */ |
| 10625 | //close_saved_fds_and_FILE_fds(); |
| 10626 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 10627 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10628 | * 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] | 10629 | * and tcsetpgrp, and this is inherently racy. |
| 10630 | */ |
| 10631 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10632 | } |
| 10633 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10634 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10635 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 10636 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10637 | |
| 10638 | /* interactive bash: |
| 10639 | * # trap "echo EEE" EXIT |
| 10640 | * # exit |
| 10641 | * exit |
| 10642 | * There are stopped jobs. |
| 10643 | * (if there are _stopped_ jobs, running ones don't count) |
| 10644 | * # exit |
| 10645 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 10646 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10647 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 10648 | * 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] | 10649 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 10650 | |
| 10651 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10652 | argv = skip_dash_dash(argv); |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10653 | if (argv[0] == NULL) { |
| 10654 | #if ENABLE_HUSH_TRAP |
| 10655 | if (G.pre_trap_exitcode >= 0) /* "exit" in trap uses $? from before the trap */ |
| 10656 | hush_exit(G.pre_trap_exitcode); |
| 10657 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10658 | hush_exit(G.last_exitcode); |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10659 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10660 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 10661 | xfunc_error_retval = 255; |
| 10662 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10663 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10664 | } |
| 10665 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10666 | #if ENABLE_HUSH_TYPE |
| 10667 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 10668 | static int FAST_FUNC builtin_type(char **argv) |
| 10669 | { |
| 10670 | int ret = EXIT_SUCCESS; |
| 10671 | |
| 10672 | while (*++argv) { |
| 10673 | const char *type; |
| 10674 | char *path = NULL; |
| 10675 | |
| 10676 | if (0) {} /* make conditional compile easier below */ |
| 10677 | /*else if (find_alias(*argv)) |
| 10678 | type = "an alias";*/ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10679 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10680 | else if (find_function(*argv)) |
| 10681 | type = "a function"; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10682 | # endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10683 | else if (find_builtin(*argv)) |
| 10684 | type = "a shell builtin"; |
| 10685 | else if ((path = find_in_path(*argv)) != NULL) |
| 10686 | type = path; |
| 10687 | else { |
| 10688 | bb_error_msg("type: %s: not found", *argv); |
| 10689 | ret = EXIT_FAILURE; |
| 10690 | continue; |
| 10691 | } |
| 10692 | |
| 10693 | printf("%s is %s\n", *argv, type); |
| 10694 | free(path); |
| 10695 | } |
| 10696 | |
| 10697 | return ret; |
| 10698 | } |
| 10699 | #endif |
| 10700 | |
| 10701 | #if ENABLE_HUSH_READ |
| 10702 | /* Interruptibility of read builtin in bash |
| 10703 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 10704 | * |
| 10705 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 10706 | * |
| 10707 | * SIGINT: |
| 10708 | * - terminates non-interactive shell; |
| 10709 | * - interrupts read in interactive shell; |
| 10710 | * if it has non-empty trap: |
| 10711 | * - executes trap and returns to command prompt in interactive shell; |
| 10712 | * - executes trap and returns to read in non-interactive shell; |
| 10713 | * SIGTERM: |
| 10714 | * - is ignored (does not interrupt) read in interactive shell; |
| 10715 | * - terminates non-interactive shell; |
| 10716 | * if it has non-empty trap: |
| 10717 | * - executes trap and returns to read; |
| 10718 | * SIGHUP: |
| 10719 | * - terminates shell (regardless of interactivity); |
| 10720 | * if it has non-empty trap: |
| 10721 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10722 | * SIGCHLD from children: |
| 10723 | * - does not interrupt read regardless of interactivity: |
| 10724 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10725 | */ |
| 10726 | static int FAST_FUNC builtin_read(char **argv) |
| 10727 | { |
| 10728 | const char *r; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10729 | struct builtin_read_params params; |
| 10730 | |
| 10731 | memset(¶ms, 0, sizeof(params)); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10732 | |
| 10733 | /* "!": do not abort on errors. |
| 10734 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 10735 | */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10736 | params.read_flags = getopt32(argv, |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10737 | # if BASH_READ_D |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10738 | "!srn:p:t:u:d:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u, ¶ms.opt_d |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10739 | # else |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10740 | "!srn:p:t:u:", ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10741 | # endif |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10742 | ); |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10743 | if ((uint32_t)params.read_flags == (uint32_t)-1) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10744 | return EXIT_FAILURE; |
| 10745 | argv += optind; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10746 | params.argv = argv; |
| 10747 | params.setvar = set_local_var_from_halves; |
| 10748 | params.ifs = get_local_var_value("IFS"); /* can be NULL */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10749 | |
| 10750 | again: |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 10751 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10752 | |
| 10753 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 10754 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10755 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10756 | goto again; |
| 10757 | } |
| 10758 | |
| 10759 | if ((uintptr_t)r > 1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 10760 | bb_simple_error_msg(r); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10761 | r = (char*)(uintptr_t)1; |
| 10762 | } |
| 10763 | |
| 10764 | return (uintptr_t)r; |
| 10765 | } |
| 10766 | #endif |
| 10767 | |
| 10768 | #if ENABLE_HUSH_UMASK |
| 10769 | static int FAST_FUNC builtin_umask(char **argv) |
| 10770 | { |
| 10771 | int rc; |
| 10772 | mode_t mask; |
| 10773 | |
| 10774 | rc = 1; |
| 10775 | mask = umask(0); |
| 10776 | argv = skip_dash_dash(argv); |
| 10777 | if (argv[0]) { |
| 10778 | mode_t old_mask = mask; |
| 10779 | |
| 10780 | /* numeric umasks are taken as-is */ |
| 10781 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 10782 | if (!isdigit(argv[0][0])) |
| 10783 | mask ^= 0777; |
| 10784 | mask = bb_parse_mode(argv[0], mask); |
| 10785 | if (!isdigit(argv[0][0])) |
| 10786 | mask ^= 0777; |
| 10787 | if ((unsigned)mask > 0777) { |
| 10788 | mask = old_mask; |
| 10789 | /* bash messages: |
| 10790 | * bash: umask: 'q': invalid symbolic mode operator |
| 10791 | * bash: umask: 999: octal number out of range |
| 10792 | */ |
| 10793 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 10794 | rc = 0; |
| 10795 | } |
| 10796 | } else { |
| 10797 | /* Mimic bash */ |
| 10798 | printf("%04o\n", (unsigned) mask); |
| 10799 | /* fall through and restore mask which we set to 0 */ |
| 10800 | } |
| 10801 | umask(mask); |
| 10802 | |
| 10803 | return !rc; /* rc != 0 - success */ |
| 10804 | } |
| 10805 | #endif |
| 10806 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10807 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10808 | static void print_escaped(const char *s) |
| 10809 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10810 | if (*s == '\'') |
| 10811 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10812 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10813 | const char *p = strchrnul(s, '\''); |
| 10814 | /* print 'xxxx', possibly just '' */ |
| 10815 | printf("'%.*s'", (int)(p - s), s); |
| 10816 | if (*p == '\0') |
| 10817 | break; |
| 10818 | s = p; |
| 10819 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10820 | /* s points to '; print "'''...'''" */ |
| 10821 | putchar('"'); |
| 10822 | do putchar('\''); while (*++s == '\''); |
| 10823 | putchar('"'); |
| 10824 | } while (*s); |
| 10825 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10826 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10827 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10828 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10829 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10830 | { |
| 10831 | do { |
| 10832 | char *name = *argv; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 10833 | const char *name_end = endofname(name); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10834 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10835 | if (*name_end == '\0') { |
| 10836 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10837 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10838 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10839 | var = vpp ? *vpp : NULL; |
| 10840 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10841 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10842 | /* export -n NAME (without =VALUE) */ |
| 10843 | if (var) { |
| 10844 | var->flg_export = 0; |
| 10845 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10846 | unsetenv(name); |
| 10847 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10848 | continue; |
| 10849 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10850 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10851 | /* export NAME (without =VALUE) */ |
| 10852 | if (var) { |
| 10853 | var->flg_export = 1; |
| 10854 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10855 | putenv(var->varstr); |
| 10856 | continue; |
| 10857 | } |
| 10858 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10859 | if (flags & SETFLAG_MAKE_RO) { |
| 10860 | /* readonly NAME (without =VALUE) */ |
| 10861 | if (var) { |
| 10862 | var->flg_read_only = 1; |
| 10863 | continue; |
| 10864 | } |
| 10865 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10866 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10867 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10868 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10869 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10870 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10871 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10872 | continue; |
| 10873 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10874 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10875 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10876 | /* Exporting non-existing variable. |
| 10877 | * bash does not put it in environment, |
| 10878 | * but remembers that it is exported, |
| 10879 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10880 | * We just set it to "" and export. |
| 10881 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10882 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10883 | * bash sets the value to "". |
| 10884 | */ |
| 10885 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10886 | * bash remembers NAME and disallows its creation |
| 10887 | * in the future. |
| 10888 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10889 | name = xasprintf("%s=", name); |
| 10890 | } else { |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 10891 | if (*name_end != '=') { |
| 10892 | bb_error_msg("'%s': bad variable name", name); |
| 10893 | /* do not parse following argv[]s: */ |
| 10894 | return 1; |
| 10895 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10896 | /* (Un)exporting/making local NAME=VALUE */ |
| 10897 | name = xstrdup(name); |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 10898 | /* Testcase: export PS1='\w \$ ' */ |
| 10899 | unbackslash(name); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10900 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10901 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10902 | if (set_local_var(name, flags)) |
| 10903 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10904 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10905 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10906 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10907 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10908 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10909 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10910 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10911 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10912 | unsigned opt_unexport; |
| 10913 | |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10914 | # if ENABLE_HUSH_EXPORT_N |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10915 | /* "!": do not abort on errors */ |
| 10916 | opt_unexport = getopt32(argv, "!n"); |
| 10917 | if (opt_unexport == (uint32_t)-1) |
| 10918 | return EXIT_FAILURE; |
| 10919 | argv += optind; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10920 | # else |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10921 | opt_unexport = 0; |
| 10922 | argv++; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10923 | # endif |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10924 | |
| 10925 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10926 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10927 | if (e) { |
| 10928 | while (*e) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10929 | # if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10930 | puts(*e++); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10931 | # else |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10932 | /* ash emits: export VAR='VAL' |
| 10933 | * bash: declare -x VAR="VAL" |
| 10934 | * we follow ash example */ |
| 10935 | const char *s = *e++; |
| 10936 | const char *p = strchr(s, '='); |
| 10937 | |
| 10938 | if (!p) /* wtf? take next variable */ |
| 10939 | continue; |
| 10940 | /* export var= */ |
| 10941 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10942 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10943 | putchar('\n'); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10944 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10945 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10946 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10947 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10948 | return EXIT_SUCCESS; |
| 10949 | } |
| 10950 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10951 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10952 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10953 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10954 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10955 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10956 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10957 | { |
| 10958 | if (G.func_nest_level == 0) { |
| 10959 | bb_error_msg("%s: not in a function", argv[0]); |
| 10960 | return EXIT_FAILURE; /* bash compat */ |
| 10961 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10962 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10963 | /* Since all builtins run in a nested variable level, |
| 10964 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10965 | * after builtin returns. |
| 10966 | */ |
| 10967 | 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] | 10968 | } |
| 10969 | #endif |
| 10970 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10971 | #if ENABLE_HUSH_READONLY |
| 10972 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10973 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10974 | argv++; |
| 10975 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10976 | /* bash: readonly [-p]: list all readonly VARs |
| 10977 | * (-p has no effect in bash) |
| 10978 | */ |
| 10979 | struct variable *e; |
| 10980 | for (e = G.top_var; e; e = e->next) { |
| 10981 | if (e->flg_read_only) { |
| 10982 | //TODO: quote value: readonly VAR='VAL' |
| 10983 | printf("readonly %s\n", e->varstr); |
| 10984 | } |
| 10985 | } |
| 10986 | return EXIT_SUCCESS; |
| 10987 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10988 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10989 | } |
| 10990 | #endif |
| 10991 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10992 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10993 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10994 | static int FAST_FUNC builtin_unset(char **argv) |
| 10995 | { |
| 10996 | int ret; |
| 10997 | unsigned opts; |
| 10998 | |
| 10999 | /* "!": do not abort on errors */ |
| 11000 | /* "+": stop at 1st non-option */ |
| 11001 | opts = getopt32(argv, "!+vf"); |
| 11002 | if (opts == (unsigned)-1) |
| 11003 | return EXIT_FAILURE; |
| 11004 | if (opts == 3) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11005 | bb_simple_error_msg("unset: -v and -f are exclusive"); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11006 | return EXIT_FAILURE; |
| 11007 | } |
| 11008 | argv += optind; |
| 11009 | |
| 11010 | ret = EXIT_SUCCESS; |
| 11011 | while (*argv) { |
| 11012 | if (!(opts & 2)) { /* not -f */ |
| 11013 | if (unset_local_var(*argv)) { |
| 11014 | /* unset <nonexistent_var> doesn't fail. |
| 11015 | * Error is when one tries to unset RO var. |
| 11016 | * Message was printed by unset_local_var. */ |
| 11017 | ret = EXIT_FAILURE; |
| 11018 | } |
| 11019 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11020 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11021 | else { |
| 11022 | unset_func(*argv); |
| 11023 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11024 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11025 | argv++; |
| 11026 | } |
| 11027 | return ret; |
| 11028 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11029 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11030 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11031 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11032 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 11033 | * built-in 'set' handler |
| 11034 | * SUSv3 says: |
| 11035 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 11036 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 11037 | * set -- [argument...] |
| 11038 | * set -o |
| 11039 | * set +o |
| 11040 | * Implementations shall support the options in both their hyphen and |
| 11041 | * plus-sign forms. These options can also be specified as options to sh. |
| 11042 | * Examples: |
| 11043 | * Write out all variables and their values: set |
| 11044 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 11045 | * Turn on the -x and -v options: set -xv |
| 11046 | * Unset all positional parameters: set -- |
| 11047 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 11048 | * Set the positional parameters to the expansion of x, even if x expands |
| 11049 | * with a leading '-' or '+': set -- $x |
| 11050 | * |
| 11051 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 11052 | */ |
| 11053 | static int FAST_FUNC builtin_set(char **argv) |
| 11054 | { |
| 11055 | int n; |
| 11056 | char **pp, **g_argv; |
| 11057 | char *arg = *++argv; |
| 11058 | |
| 11059 | if (arg == NULL) { |
| 11060 | struct variable *e; |
| 11061 | for (e = G.top_var; e; e = e->next) |
| 11062 | puts(e->varstr); |
| 11063 | return EXIT_SUCCESS; |
| 11064 | } |
| 11065 | |
| 11066 | do { |
| 11067 | if (strcmp(arg, "--") == 0) { |
| 11068 | ++argv; |
| 11069 | goto set_argv; |
| 11070 | } |
| 11071 | if (arg[0] != '+' && arg[0] != '-') |
| 11072 | break; |
| 11073 | for (n = 1; arg[n]; ++n) { |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 11074 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) { |
| 11075 | bb_error_msg("%s: %s: invalid option", "set", arg); |
| 11076 | return EXIT_FAILURE; |
| 11077 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11078 | if (arg[n] == 'o' && argv[1]) |
| 11079 | argv++; |
| 11080 | } |
| 11081 | } while ((arg = *++argv) != NULL); |
| 11082 | /* Now argv[0] is 1st argument */ |
| 11083 | |
| 11084 | if (arg == NULL) |
| 11085 | return EXIT_SUCCESS; |
| 11086 | set_argv: |
| 11087 | |
| 11088 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 11089 | g_argv = G.global_argv; |
| 11090 | if (G.global_args_malloced) { |
| 11091 | pp = g_argv; |
| 11092 | while (*++pp) |
| 11093 | free(*pp); |
| 11094 | g_argv[1] = NULL; |
| 11095 | } else { |
| 11096 | G.global_args_malloced = 1; |
| 11097 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 11098 | pp[0] = g_argv[0]; /* retain $0 */ |
| 11099 | g_argv = pp; |
| 11100 | } |
| 11101 | /* This realloc's G.global_argv */ |
| 11102 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 11103 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 11104 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11105 | |
| 11106 | return EXIT_SUCCESS; |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11107 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11108 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11109 | |
| 11110 | static int FAST_FUNC builtin_shift(char **argv) |
| 11111 | { |
| 11112 | int n = 1; |
| 11113 | argv = skip_dash_dash(argv); |
| 11114 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 11115 | n = bb_strtou(argv[0], NULL, 10); |
| 11116 | if (errno || n < 0) { |
| 11117 | /* shared string with ash.c */ |
| 11118 | bb_error_msg("Illegal number: %s", argv[0]); |
| 11119 | /* |
| 11120 | * ash aborts in this case. |
| 11121 | * bash prints error message and set $? to 1. |
| 11122 | * Interestingly, for "shift 99999" bash does not |
| 11123 | * print error message, but does set $? to 1 |
| 11124 | * (and does no shifting at all). |
| 11125 | */ |
| 11126 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11127 | } |
| 11128 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 11129 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11130 | int m = 1; |
| 11131 | while (m <= n) |
| 11132 | free(G.global_argv[m++]); |
| 11133 | } |
| 11134 | G.global_argc -= n; |
| 11135 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 11136 | G.global_argc * sizeof(G.global_argv[0])); |
| 11137 | return EXIT_SUCCESS; |
| 11138 | } |
| 11139 | return EXIT_FAILURE; |
| 11140 | } |
| 11141 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11142 | #if ENABLE_HUSH_GETOPTS |
| 11143 | static int FAST_FUNC builtin_getopts(char **argv) |
| 11144 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11145 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 11146 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11147 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11148 | If a required argument is not found, and getopts is not silent, |
| 11149 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 11150 | diagnostic message is printed. If getopts is silent, then a |
| 11151 | colon (:) is placed in VAR and OPTARG is set to the option |
| 11152 | character found. |
| 11153 | |
| 11154 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11155 | |
| 11156 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11157 | */ |
| 11158 | char cbuf[2]; |
| 11159 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11160 | int c, n, exitcode, my_opterr; |
| 11161 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11162 | |
| 11163 | optstring = *++argv; |
| 11164 | if (!optstring || !(var = *++argv)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11165 | bb_simple_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11166 | return EXIT_FAILURE; |
| 11167 | } |
| 11168 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11169 | if (argv[1]) |
| 11170 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 11171 | else |
| 11172 | argv = G.global_argv; |
| 11173 | cbuf[1] = '\0'; |
| 11174 | |
| 11175 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11176 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11177 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11178 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11179 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11180 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11181 | |
| 11182 | /* getopts stops on first non-option. Add "+" to force that */ |
| 11183 | /*if (optstring[0] != '+')*/ { |
| 11184 | char *s = alloca(strlen(optstring) + 2); |
| 11185 | sprintf(s, "+%s", optstring); |
| 11186 | optstring = s; |
| 11187 | } |
| 11188 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11189 | /* Naively, now we should just |
| 11190 | * cp = get_local_var_value("OPTIND"); |
| 11191 | * optind = cp ? atoi(cp) : 0; |
| 11192 | * optarg = NULL; |
| 11193 | * opterr = my_opterr; |
| 11194 | * c = getopt(string_array_len(argv), argv, optstring); |
| 11195 | * and be done? Not so fast... |
| 11196 | * Unlike normal getopt() usage in C programs, here |
| 11197 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 11198 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 11199 | * invocations of "getopts", there will be calls to shell builtins |
| 11200 | * which use getopt() internally. Example: |
| 11201 | * while getopts "abc" RES -a -bc -abc de; do |
| 11202 | * unset -ff func |
| 11203 | * done |
| 11204 | * This would not work correctly: getopt() call inside "unset" |
| 11205 | * modifies internal libc state which is tracking position in |
| 11206 | * multi-option strings ("-abc"). At best, it can skip options |
| 11207 | * or return the same option infinitely. With glibc implementation |
| 11208 | * of getopt(), it would use outright invalid pointers and return |
| 11209 | * garbage even _without_ "unset" mangling internal state. |
| 11210 | * |
| 11211 | * We resort to resetting getopt() state and calling it N times, |
| 11212 | * until we get Nth result (or failure). |
| 11213 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 11214 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11215 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11216 | count = 0; |
| 11217 | n = string_array_len(argv); |
| 11218 | do { |
| 11219 | optarg = NULL; |
| 11220 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 11221 | c = getopt(n, argv, optstring); |
| 11222 | if (c < 0) |
| 11223 | break; |
| 11224 | count++; |
| 11225 | } while (count <= G.getopt_count); |
| 11226 | |
| 11227 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 11228 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 11229 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11230 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11231 | |
| 11232 | /* Set OPTARG */ |
| 11233 | /* Always set or unset, never left as-is, even on exit/error: |
| 11234 | * "If no option was found, or if the option that was found |
| 11235 | * does not have an option-argument, OPTARG shall be unset." |
| 11236 | */ |
| 11237 | cp = optarg; |
| 11238 | if (c == '?') { |
| 11239 | /* If ":optstring" and unknown option is seen, |
| 11240 | * it is stored to OPTARG. |
| 11241 | */ |
| 11242 | if (optstring[1] == ':') { |
| 11243 | cbuf[0] = optopt; |
| 11244 | cp = cbuf; |
| 11245 | } |
| 11246 | } |
| 11247 | if (cp) |
| 11248 | set_local_var_from_halves("OPTARG", cp); |
| 11249 | else |
| 11250 | unset_local_var("OPTARG"); |
| 11251 | |
| 11252 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11253 | exitcode = EXIT_SUCCESS; |
| 11254 | if (c < 0) { /* -1: end of options */ |
| 11255 | exitcode = EXIT_FAILURE; |
| 11256 | c = '?'; |
| 11257 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11258 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11259 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11260 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11261 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11262 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11263 | return exitcode; |
| 11264 | } |
| 11265 | #endif |
| 11266 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11267 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11268 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11269 | char *arg_path, *filename; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11270 | HFILE *input; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11271 | save_arg_t sv; |
| 11272 | char *args_need_save; |
| 11273 | #if ENABLE_HUSH_FUNCTIONS |
| 11274 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11275 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11276 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11277 | argv = skip_dash_dash(argv); |
| 11278 | filename = argv[0]; |
| 11279 | if (!filename) { |
| 11280 | /* bash says: "bash: .: filename argument required" */ |
| 11281 | return 2; /* bash compat */ |
| 11282 | } |
| 11283 | arg_path = NULL; |
| 11284 | if (!strchr(filename, '/')) { |
| 11285 | arg_path = find_in_path(filename); |
| 11286 | if (arg_path) |
| 11287 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 11288 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 11289 | errno = ENOENT; |
| 11290 | bb_simple_perror_msg(filename); |
| 11291 | return EXIT_FAILURE; |
| 11292 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11293 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11294 | input = hfopen(filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11295 | free(arg_path); |
| 11296 | if (!input) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11297 | bb_perror_msg("%s", filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11298 | /* POSIX: non-interactive shell should abort here, |
| 11299 | * not merely fail. So far no one complained :) |
| 11300 | */ |
| 11301 | return EXIT_FAILURE; |
| 11302 | } |
| 11303 | |
| 11304 | #if ENABLE_HUSH_FUNCTIONS |
| 11305 | sv_flg = G_flag_return_in_progress; |
| 11306 | /* "we are inside sourced file, ok to use return" */ |
| 11307 | G_flag_return_in_progress = -1; |
| 11308 | #endif |
| 11309 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 11310 | if (args_need_save) |
| 11311 | save_and_replace_G_args(&sv, argv); |
| 11312 | |
| 11313 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 11314 | G.last_exitcode = 0; |
| 11315 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11316 | hfclose(input); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11317 | |
| 11318 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 11319 | restore_G_args(&sv, argv); |
| 11320 | #if ENABLE_HUSH_FUNCTIONS |
| 11321 | G_flag_return_in_progress = sv_flg; |
| 11322 | #endif |
| 11323 | |
| 11324 | return G.last_exitcode; |
| 11325 | } |
| 11326 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11327 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11328 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11329 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11330 | int sig; |
| 11331 | char *new_cmd; |
| 11332 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11333 | if (!G_traps) |
| 11334 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11335 | |
| 11336 | argv++; |
| 11337 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 11338 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11339 | /* No args: print all trapped */ |
| 11340 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11341 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11342 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11343 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 11344 | /* note: bash adds "SIG", but only if invoked |
| 11345 | * as "bash". If called as "sh", or if set -o posix, |
| 11346 | * then it prints short signal names. |
| 11347 | * We are printing short names: */ |
| 11348 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11349 | } |
| 11350 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 11351 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11352 | return EXIT_SUCCESS; |
| 11353 | } |
| 11354 | |
| 11355 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11356 | /* If first arg is a number: reset all specified signals */ |
| 11357 | sig = bb_strtou(*argv, NULL, 10); |
| 11358 | if (errno == 0) { |
| 11359 | int ret; |
| 11360 | process_sig_list: |
| 11361 | ret = EXIT_SUCCESS; |
| 11362 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11363 | sighandler_t handler; |
| 11364 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11365 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 11366 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11367 | ret = EXIT_FAILURE; |
| 11368 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 11369 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11370 | continue; |
| 11371 | } |
| 11372 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11373 | free(G_traps[sig]); |
| 11374 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11375 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 11376 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11377 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11378 | |
| 11379 | /* There is no signal for 0 (EXIT) */ |
| 11380 | if (sig == 0) |
| 11381 | continue; |
| 11382 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11383 | if (new_cmd) |
| 11384 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 11385 | else |
| 11386 | /* We are removing trap handler */ |
| 11387 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 11388 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11389 | } |
| 11390 | return ret; |
| 11391 | } |
| 11392 | |
| 11393 | if (!argv[1]) { /* no second arg */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11394 | bb_simple_error_msg("trap: invalid arguments"); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11395 | return EXIT_FAILURE; |
| 11396 | } |
| 11397 | |
| 11398 | /* First arg is "-": reset all specified to default */ |
| 11399 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 11400 | /* Everything else: set arg as signal handler |
| 11401 | * (includes "" case, which ignores signal) */ |
| 11402 | if (argv[0][0] == '-') { |
| 11403 | if (argv[0][1] == '\0') { /* "-" */ |
| 11404 | /* new_cmd remains NULL: "reset these sigs" */ |
| 11405 | goto reset_traps; |
| 11406 | } |
| 11407 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 11408 | argv++; |
| 11409 | } |
| 11410 | /* else: "-something", no special meaning */ |
| 11411 | } |
| 11412 | new_cmd = *argv; |
| 11413 | reset_traps: |
| 11414 | argv++; |
| 11415 | goto process_sig_list; |
| 11416 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11417 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11418 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11419 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11420 | static struct pipe *parse_jobspec(const char *str) |
| 11421 | { |
| 11422 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11423 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11424 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11425 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 11426 | if (str[0] != '%' |
| 11427 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 11428 | ) { |
| 11429 | bb_error_msg("bad argument '%s'", str); |
| 11430 | return NULL; |
| 11431 | } |
| 11432 | /* It is "%%", "%+" or "%" - current job */ |
| 11433 | jobnum = G.last_jobid; |
| 11434 | if (jobnum == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11435 | bb_simple_error_msg("no current job"); |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11436 | return NULL; |
| 11437 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11438 | } |
| 11439 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11440 | if (pi->jobid == jobnum) { |
| 11441 | return pi; |
| 11442 | } |
| 11443 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11444 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11445 | return NULL; |
| 11446 | } |
| 11447 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11448 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 11449 | { |
| 11450 | struct pipe *job; |
| 11451 | const char *status_string; |
| 11452 | |
| 11453 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 11454 | for (job = G.job_list; job; job = job->next) { |
| 11455 | if (job->alive_cmds == job->stopped_cmds) |
| 11456 | status_string = "Stopped"; |
| 11457 | else |
| 11458 | status_string = "Running"; |
| 11459 | |
| 11460 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 11461 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11462 | |
| 11463 | clean_up_last_dead_job(); |
| 11464 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11465 | return EXIT_SUCCESS; |
| 11466 | } |
| 11467 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11468 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11469 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11470 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11471 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11472 | struct pipe *pi; |
| 11473 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11474 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11475 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 11476 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11477 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 11478 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11479 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11480 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11481 | goto found; |
| 11482 | } |
| 11483 | } |
| 11484 | bb_error_msg("%s: no current job", argv[0]); |
| 11485 | return EXIT_FAILURE; |
| 11486 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11487 | |
| 11488 | pi = parse_jobspec(argv[1]); |
| 11489 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11490 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11491 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 11492 | /* TODO: bash prints a string representation |
| 11493 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 11494 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11495 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11496 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11497 | } |
| 11498 | |
| 11499 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11500 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 11501 | for (i = 0; i < pi->num_cmds; i++) { |
| 11502 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11503 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11504 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11505 | |
| 11506 | i = kill(- pi->pgrp, SIGCONT); |
| 11507 | if (i < 0) { |
| 11508 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11509 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11510 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11511 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11512 | bb_simple_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11513 | } |
| 11514 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11515 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11516 | 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] | 11517 | return checkjobs_and_fg_shell(pi); |
| 11518 | } |
| 11519 | return EXIT_SUCCESS; |
| 11520 | } |
| 11521 | #endif |
| 11522 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11523 | #if ENABLE_HUSH_KILL |
| 11524 | static int FAST_FUNC builtin_kill(char **argv) |
| 11525 | { |
| 11526 | int ret = 0; |
| 11527 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11528 | # if ENABLE_HUSH_JOB |
| 11529 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 11530 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11531 | |
| 11532 | do { |
| 11533 | struct pipe *pi; |
| 11534 | char *dst; |
| 11535 | int j, n; |
| 11536 | |
| 11537 | if (argv[i][0] != '%') |
| 11538 | continue; |
| 11539 | /* |
| 11540 | * "kill %N" - job kill |
| 11541 | * Converting to pgrp / pid kill |
| 11542 | */ |
| 11543 | pi = parse_jobspec(argv[i]); |
| 11544 | if (!pi) { |
| 11545 | /* Eat bad jobspec */ |
| 11546 | j = i; |
| 11547 | do { |
| 11548 | j++; |
| 11549 | argv[j - 1] = argv[j]; |
| 11550 | } while (argv[j]); |
| 11551 | ret = 1; |
| 11552 | i--; |
| 11553 | continue; |
| 11554 | } |
| 11555 | /* |
| 11556 | * In jobs started under job control, we signal |
| 11557 | * entire process group by kill -PGRP_ID. |
| 11558 | * This happens, f.e., in interactive shell. |
| 11559 | * |
| 11560 | * Otherwise, we signal each child via |
| 11561 | * kill PID1 PID2 PID3. |
| 11562 | * Testcases: |
| 11563 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 11564 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 11565 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 11566 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 11567 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11568 | dst = alloca(n * sizeof(int)*4); |
| 11569 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11570 | if (G_interactive_fd) |
| 11571 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11572 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11573 | struct command *cmd = &pi->cmds[j]; |
| 11574 | /* Skip exited members of the job */ |
| 11575 | if (cmd->pid == 0) |
| 11576 | continue; |
| 11577 | /* |
| 11578 | * kill_main has matching code to expect |
| 11579 | * leading space. Needed to not confuse |
| 11580 | * negative pids with "kill -SIGNAL_NO" syntax |
| 11581 | */ |
| 11582 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 11583 | } |
| 11584 | *dst = '\0'; |
| 11585 | } while (argv[++i]); |
| 11586 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11587 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11588 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11589 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11590 | ret = run_applet_main(argv, kill_main); |
| 11591 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11592 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11593 | return ret; |
| 11594 | } |
| 11595 | #endif |
| 11596 | |
| 11597 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11598 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11599 | # if !ENABLE_HUSH_JOB |
| 11600 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 11601 | # endif |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11602 | 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] | 11603 | { |
| 11604 | int ret = 0; |
| 11605 | for (;;) { |
| 11606 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11607 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11608 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11609 | if (!sigisemptyset(&G.pending_set)) |
| 11610 | goto check_sig; |
| 11611 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11612 | /* waitpid is not interruptible by SA_RESTARTed |
| 11613 | * signals which we use. Thus, this ugly dance: |
| 11614 | */ |
| 11615 | |
| 11616 | /* Make sure possible SIGCHLD is stored in kernel's |
| 11617 | * pending signal mask before we call waitpid. |
| 11618 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11619 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11620 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11621 | sigfillset(&oldset); /* block all signals, remember old set */ |
Denys Vlasenko | b437df1 | 2018-12-08 15:35:24 +0100 | [diff] [blame] | 11622 | sigprocmask2(SIG_SETMASK, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11623 | |
| 11624 | if (!sigisemptyset(&G.pending_set)) { |
| 11625 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11626 | goto restore; |
| 11627 | } |
| 11628 | |
| 11629 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11630 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11631 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11632 | debug_printf_exec("checkjobs:%d\n", ret); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11633 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11634 | if (waitfor_pipe) { |
| 11635 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 11636 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 11637 | if (rcode >= 0) { |
| 11638 | ret = rcode; |
| 11639 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11640 | break; |
| 11641 | } |
| 11642 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11643 | # endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11644 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 11645 | /* if ret == 0, no children changed state */ |
| 11646 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11647 | if (errno == ECHILD || ret) { |
| 11648 | ret--; |
| 11649 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11650 | ret = 0; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11651 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11652 | if (waitfor_pid == -1 && errno == ECHILD) { |
| 11653 | /* exitcode of "wait -n" with no children is 127, not 0 */ |
| 11654 | ret = 127; |
| 11655 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11656 | # endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11657 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11658 | break; |
| 11659 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11660 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11661 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 11662 | /* Note: sigsuspend invokes signal handler */ |
| 11663 | sigsuspend(&oldset); |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 11664 | /* ^^^ add "sigdelset(&oldset, SIGCHLD)" before sigsuspend |
| 11665 | * to make sure SIGCHLD is not masked off? |
| 11666 | * It was reported that this: |
| 11667 | * fn() { : | return; } |
| 11668 | * shopt -s lastpipe |
| 11669 | * fn |
| 11670 | * exec hush SCRIPT |
| 11671 | * under bash 4.4.23 runs SCRIPT with SIGCHLD masked, |
| 11672 | * making "wait" commands in SCRIPT block forever. |
| 11673 | */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11674 | restore: |
| 11675 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11676 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11677 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11678 | sig = check_and_run_traps(); |
| 11679 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 11680 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11681 | ret = 128 + sig; |
| 11682 | break; |
| 11683 | } |
| 11684 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 11685 | } |
| 11686 | return ret; |
| 11687 | } |
| 11688 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11689 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11690 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11691 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11692 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11693 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11694 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11695 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11696 | if (argv[0] && strcmp(argv[0], "-n") == 0) { |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11697 | /* wait -n */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11698 | /* (bash accepts "wait -n PID" too and ignores PID) */ |
| 11699 | G.dead_job_exitcode = -1; |
| 11700 | return wait_for_child_or_signal(NULL, -1 /*no job, wait for one job*/); |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11701 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11702 | # endif |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11703 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 11704 | /* Don't care about wait results */ |
| 11705 | /* Note 1: must wait until there are no more children */ |
| 11706 | /* Note 2: must be interruptible */ |
| 11707 | /* Examples: |
| 11708 | * $ sleep 3 & sleep 6 & wait |
| 11709 | * [1] 30934 sleep 3 |
| 11710 | * [2] 30935 sleep 6 |
| 11711 | * [1] Done sleep 3 |
| 11712 | * [2] Done sleep 6 |
| 11713 | * $ sleep 3 & sleep 6 & wait |
| 11714 | * [1] 30936 sleep 3 |
| 11715 | * [2] 30937 sleep 6 |
| 11716 | * [1] Done sleep 3 |
| 11717 | * ^C <-- after ~4 sec from keyboard |
| 11718 | * $ |
| 11719 | */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11720 | 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] | 11721 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11722 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11723 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11724 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11725 | if (errno || pid <= 0) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11726 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11727 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11728 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11729 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11730 | wait_pipe = parse_jobspec(*argv); |
| 11731 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11732 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11733 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11734 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11735 | } else { |
| 11736 | /* waiting on "last dead job" removes it */ |
| 11737 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 11738 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11739 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11740 | /* else: parse_jobspec() already emitted error msg */ |
| 11741 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11742 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11743 | # endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11744 | /* mimic bash message */ |
| 11745 | 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] | 11746 | ret = EXIT_FAILURE; |
| 11747 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11748 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11749 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11750 | /* Do we have such child? */ |
| 11751 | ret = waitpid(pid, &status, WNOHANG); |
| 11752 | if (ret < 0) { |
| 11753 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11754 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11755 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 11756 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11757 | /* "wait $!" but last bg task has already exited. Try: |
| 11758 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 11759 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11760 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11761 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11762 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11763 | } else { |
| 11764 | /* Example: "wait 1". mimic bash message */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11765 | bb_error_msg("wait: pid %u is not a child of this shell", (unsigned)pid); |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11766 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11767 | } else { |
| 11768 | /* ??? */ |
| 11769 | bb_perror_msg("wait %s", *argv); |
| 11770 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11771 | continue; /* bash checks all argv[] */ |
| 11772 | } |
| 11773 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11774 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11775 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11776 | } else { |
| 11777 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11778 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 11779 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11780 | if (WIFSIGNALED(status)) |
| 11781 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11782 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11783 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11784 | |
| 11785 | return ret; |
| 11786 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11787 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11788 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11789 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 11790 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 11791 | { |
| 11792 | if (argv[1]) { |
| 11793 | def = bb_strtou(argv[1], NULL, 10); |
| 11794 | if (errno || def < def_min || argv[2]) { |
| 11795 | bb_error_msg("%s: bad arguments", argv[0]); |
| 11796 | def = UINT_MAX; |
| 11797 | } |
| 11798 | } |
| 11799 | return def; |
| 11800 | } |
| 11801 | #endif |
| 11802 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11803 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11804 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11805 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11806 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11807 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11808 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11809 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 11810 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 11811 | return EXIT_SUCCESS; /* bash compat */ |
| 11812 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11813 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11814 | |
| 11815 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 11816 | if (depth == UINT_MAX) |
| 11817 | G.flag_break_continue = BC_BREAK; |
| 11818 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11819 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11820 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11821 | return EXIT_SUCCESS; |
| 11822 | } |
| 11823 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11824 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11825 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11826 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 11827 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11828 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11829 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11830 | |
| 11831 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11832 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11833 | { |
| 11834 | int rc; |
| 11835 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11836 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11837 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 11838 | return EXIT_FAILURE; /* bash compat */ |
| 11839 | } |
| 11840 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11841 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11842 | |
| 11843 | /* bash: |
| 11844 | * out of range: wraps around at 256, does not error out |
| 11845 | * non-numeric param: |
| 11846 | * f() { false; return qwe; }; f; echo $? |
| 11847 | * bash: return: qwe: numeric argument required <== we do this |
| 11848 | * 255 <== we also do this |
| 11849 | */ |
| 11850 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 11851 | # if ENABLE_HUSH_TRAP |
| 11852 | if (argv[1]) { /* "return ARG" inside a running trap sets $? */ |
| 11853 | debug_printf_exec("G.return_exitcode=%d\n", rc); |
| 11854 | G.return_exitcode = rc; |
| 11855 | } |
| 11856 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11857 | return rc; |
| 11858 | } |
| 11859 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11860 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11861 | #if ENABLE_HUSH_TIMES |
| 11862 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11863 | { |
| 11864 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11865 | ' ', offsetof(struct tms, tms_utime), |
| 11866 | '\n', offsetof(struct tms, tms_stime), |
| 11867 | ' ', offsetof(struct tms, tms_cutime), |
| 11868 | '\n', offsetof(struct tms, tms_cstime), |
| 11869 | 0 |
| 11870 | }; |
| 11871 | const uint8_t *p; |
| 11872 | unsigned clk_tck; |
| 11873 | struct tms buf; |
| 11874 | |
| 11875 | clk_tck = bb_clk_tck(); |
| 11876 | |
| 11877 | times(&buf); |
| 11878 | p = times_tbl; |
| 11879 | do { |
| 11880 | unsigned sec, frac; |
| 11881 | unsigned long t; |
| 11882 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11883 | sec = t / clk_tck; |
| 11884 | frac = t % clk_tck; |
| 11885 | printf("%um%u.%03us%c", |
| 11886 | sec / 60, sec % 60, |
| 11887 | (frac * 1000) / clk_tck, |
| 11888 | p[0]); |
| 11889 | p += 2; |
| 11890 | } while (*p); |
| 11891 | |
| 11892 | return EXIT_SUCCESS; |
| 11893 | } |
| 11894 | #endif |
| 11895 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11896 | #if ENABLE_HUSH_MEMLEAK |
| 11897 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11898 | { |
| 11899 | void *p; |
| 11900 | unsigned long l; |
| 11901 | |
| 11902 | # ifdef M_TRIM_THRESHOLD |
| 11903 | /* Optional. Reduces probability of false positives */ |
| 11904 | malloc_trim(0); |
| 11905 | # endif |
| 11906 | /* Crude attempt to find where "free memory" starts, |
| 11907 | * sans fragmentation. */ |
| 11908 | p = malloc(240); |
| 11909 | l = (unsigned long)p; |
| 11910 | free(p); |
| 11911 | p = malloc(3400); |
| 11912 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11913 | free(p); |
| 11914 | |
| 11915 | |
| 11916 | # if 0 /* debug */ |
| 11917 | { |
| 11918 | struct mallinfo mi = mallinfo(); |
| 11919 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11920 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11921 | } |
| 11922 | # endif |
| 11923 | |
| 11924 | if (!G.memleak_value) |
| 11925 | G.memleak_value = l; |
| 11926 | |
| 11927 | l -= G.memleak_value; |
| 11928 | if ((long)l < 0) |
| 11929 | l = 0; |
| 11930 | l /= 1024; |
| 11931 | if (l > 127) |
| 11932 | l = 127; |
| 11933 | |
| 11934 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11935 | return l; |
| 11936 | } |
| 11937 | #endif |