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 | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 130 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 131 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 132 | //config: default n # do not encourage non-standard behavior |
| 133 | //config: depends on HUSH_BASH_COMPAT |
| 134 | //config: help |
| 135 | //config: This is not compliant with standards. Avoid if possible. |
| 136 | //config: |
Denys Vlasenko | cbfdeba | 2021-03-10 16:31:05 +0100 | [diff] [blame] | 137 | //config:config HUSH_LINENO_VAR |
| 138 | //config: bool "$LINENO variable (bashism)" |
| 139 | //config: default y |
| 140 | //config: depends on SHELL_HUSH |
| 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 | aaf3d5b | 2021-10-13 11:15:52 +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 | |
Denys Vlasenko | 7c3e96d | 2021-10-12 01:24:32 +0200 | [diff] [blame] | 376 | #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !ENABLE_SHELL_ASH |
Ron Yorston | 71df2d3 | 2018-11-27 14:34:25 +0000 | [diff] [blame] | 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 |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 387 | #define BASH_DOLLAR_SQUOTE ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 388 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 389 | #define BASH_EPOCH_VARS ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 390 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 391 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 392 | |
| 393 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 394 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 395 | #define LEAK_HUNTING 0 |
| 396 | #define BUILD_AS_NOMMU 0 |
| 397 | /* Enable/disable sanity checks. Ok to enable in production, |
| 398 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 399 | * Keeping 1 for now even in released versions. |
| 400 | */ |
| 401 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 402 | /* Slightly bigger (+200 bytes), but faster hush. |
| 403 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 404 | * which allows us to do fewer waitpid's. |
| 405 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 406 | * and therefore waitpid will return the same result as last time) |
| 407 | */ |
| 408 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 409 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 410 | * So far ${var%...} ops are always enabled: |
| 411 | */ |
| 412 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 413 | |
| 414 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 415 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 416 | # undef BB_MMU |
| 417 | # undef USE_FOR_NOMMU |
| 418 | # undef USE_FOR_MMU |
| 419 | # define BB_MMU 0 |
| 420 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 421 | # define USE_FOR_MMU(...) |
| 422 | #endif |
| 423 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 424 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 425 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 426 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 427 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 428 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 429 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 430 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 431 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 432 | # define IF_FEATURE_SH_STANDALONE(...) |
| 433 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 434 | #endif |
| 435 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 436 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 437 | # undef ENABLE_FEATURE_EDITING |
| 438 | # define ENABLE_FEATURE_EDITING 0 |
| 439 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 440 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 441 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 442 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 443 | #endif |
| 444 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 445 | /* Do we support ANY keywords? */ |
| 446 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 447 | # define HAS_KEYWORDS 1 |
| 448 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 449 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 450 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 451 | # define HAS_KEYWORDS 0 |
| 452 | # define IF_HAS_KEYWORDS(...) |
| 453 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 454 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 456 | /* If you comment out one of these below, it will be #defined later |
| 457 | * to perform debug printfs to stderr: */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 458 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 459 | /* Finer-grained debug switches */ |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 460 | #define debug_printf_parse(...) do {} while (0) |
| 461 | #define debug_printf_heredoc(...) do {} while (0) |
| 462 | #define debug_print_tree(a, b) do {} while (0) |
| 463 | #define debug_printf_exec(...) do {} while (0) |
| 464 | #define debug_printf_env(...) do {} while (0) |
| 465 | #define debug_printf_jobs(...) do {} while (0) |
| 466 | #define debug_printf_expand(...) do {} while (0) |
| 467 | #define debug_printf_varexp(...) do {} while (0) |
| 468 | #define debug_printf_glob(...) do {} while (0) |
| 469 | #define debug_printf_redir(...) do {} while (0) |
| 470 | #define debug_printf_list(...) do {} while (0) |
| 471 | #define debug_printf_subst(...) do {} while (0) |
| 472 | #define debug_printf_prompt(...) do {} while (0) |
| 473 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 474 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 475 | #define ERR_PTR ((void*)(long)1) |
| 476 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 477 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 478 | |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 479 | #define _SPECIAL_VARS_STR "_*@$!?#-" |
| 480 | #define SPECIAL_VARS_STR ("_*@$!?#-" + 1) |
| 481 | #define NUMERIC_SPECVARS_STR ("_*@$!?#-" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 482 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 483 | /* Support / and // replace ops */ |
| 484 | /* Note that // is stored as \ in "encoded" string representation */ |
| 485 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 486 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 487 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 488 | #else |
| 489 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 490 | # define VAR_SUBST_OPS "%#:-=+?" |
| 491 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 492 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 493 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 494 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 495 | #define SPECIAL_VAR_SYMBOL 3 |
| 496 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 497 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 498 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 499 | struct variable; |
| 500 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 501 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 502 | |
| 503 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 504 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 505 | */ |
| 506 | #if !BB_MMU |
| 507 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 508 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 509 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 510 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 511 | } nommu_save_t; |
| 512 | #endif |
| 513 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 514 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 515 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 516 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 517 | RES_IF , |
| 518 | RES_THEN , |
| 519 | RES_ELIF , |
| 520 | RES_ELSE , |
| 521 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 522 | #endif |
| 523 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 524 | RES_FOR , |
| 525 | RES_WHILE , |
| 526 | RES_UNTIL , |
| 527 | RES_DO , |
| 528 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 529 | #endif |
| 530 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 531 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 532 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 533 | #if ENABLE_HUSH_CASE |
| 534 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 535 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 536 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 537 | RES_MATCH , /* "word)" */ |
| 538 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 539 | RES_ESAC , |
| 540 | #endif |
| 541 | RES_XXXX , |
| 542 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 543 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 544 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 545 | typedef struct o_string { |
| 546 | char *data; |
| 547 | int length; /* position where data is appended */ |
| 548 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 549 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 550 | /* At least some part of the string was inside '' or "", |
| 551 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 552 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 553 | smallint has_empty_slot; |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 554 | smallint ended_in_ifs; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 555 | } o_string; |
| 556 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 557 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 558 | EXP_FLAG_GLOB = 0x2, |
| 559 | /* Protect newly added chars against globbing |
| 560 | * by prepending \ to *, ?, [, \ */ |
| 561 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 562 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 563 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 564 | #define NULL_O_STRING { NULL } |
| 565 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 566 | #ifndef debug_printf_parse |
Denys Vlasenko | 987be93 | 2022-02-06 20:07:12 +0100 | [diff] [blame] | 567 | static const char *const assignment_flag[] ALIGN_PTR = { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 568 | "MAYBE_ASSIGNMENT", |
| 569 | "DEFINITELY_ASSIGNMENT", |
| 570 | "NOT_ASSIGNMENT", |
| 571 | "WORD_IS_KEYWORD", |
| 572 | }; |
| 573 | #endif |
| 574 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 575 | /* We almost can use standard FILE api, but we need an ability to move |
| 576 | * its fd when redirects coincide with it. No api exists for that |
| 577 | * (RFE for it at https://sourceware.org/bugzilla/show_bug.cgi?id=21902). |
| 578 | * HFILE is our internal alternative. Only supports reading. |
| 579 | * Since we now can, we incorporate linked list of all opened HFILEs |
| 580 | * into the struct (used to be a separate mini-list). |
| 581 | */ |
| 582 | typedef struct HFILE { |
| 583 | char *cur; |
| 584 | char *end; |
| 585 | struct HFILE *next_hfile; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 586 | int fd; |
| 587 | char buf[1024]; |
| 588 | } HFILE; |
| 589 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 590 | typedef struct in_str { |
| 591 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 592 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 593 | int last_char; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 594 | HFILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 595 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 596 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 597 | /* The descrip member of this structure is only used to make |
| 598 | * debugging output pretty */ |
| 599 | static const struct { |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 600 | int32_t mode; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 601 | signed char default_fd; |
| 602 | char descrip[3]; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 603 | } redir_table[] ALIGN4 = { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 604 | { O_RDONLY, 0, "<" }, |
| 605 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 606 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 607 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 608 | { O_RDONLY, 0, "<<" }, |
| 609 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 610 | /* { O_RDONLY, 77, "<<" }, */ |
| 611 | }; |
| 612 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 613 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 614 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 615 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 616 | int rd_fd; /* fd to redirect */ |
| 617 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 618 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 619 | smallint rd_type; /* (enum redir_type) */ |
| 620 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 621 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 622 | * bit 0: do we need to trim leading tabs? |
| 623 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 624 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 625 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 626 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 627 | REDIRECT_INPUT = 0, |
| 628 | REDIRECT_OVERWRITE = 1, |
| 629 | REDIRECT_APPEND = 2, |
| 630 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 631 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 632 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 633 | |
| 634 | REDIRFD_CLOSE = -3, |
| 635 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 636 | REDIRFD_TO_FILE = -1, |
| 637 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 638 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 639 | HEREDOC_SKIPTABS = 1, |
| 640 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 641 | } redir_type; |
| 642 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 643 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 644 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 645 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 646 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 647 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 648 | unsigned lineno; |
| 649 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 650 | smallint cmd_type; /* CMD_xxx */ |
| 651 | #define CMD_NORMAL 0 |
| 652 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 653 | #if BASH_TEST2 |
| 654 | /* used for "[[ EXPR ]]" */ |
| 655 | # define CMD_TEST2_SINGLEWORD_NOGLOB 2 |
| 656 | #endif |
Denys Vlasenko | 77a51a2 | 2020-12-29 16:53:11 +0100 | [diff] [blame] | 657 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 658 | /* used to prevent word splitting and globbing in "export v=t*" */ |
| 659 | # define CMD_SINGLEWORD_NOGLOB 3 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 660 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 661 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 662 | # define CMD_FUNCDEF 4 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 663 | #endif |
| 664 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 665 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 666 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 667 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 668 | #if !BB_MMU |
| 669 | char *group_as_string; |
| 670 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 671 | #if ENABLE_HUSH_FUNCTIONS |
| 672 | struct function *child_func; |
| 673 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 674 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 675 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 676 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 677 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 678 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 679 | * we put those fields back into cmd->xxx |
| 680 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 681 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 682 | * Without this trick, loop would execute a;b;b;b;... |
| 683 | * instead of correct sequence a;b;a;b;... |
| 684 | * When command is freed, it severs the link |
| 685 | * (sets ->child_func->parent_cmd to NULL). |
| 686 | */ |
| 687 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 688 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 689 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 690 | * and on execution these are substituted with their values. |
| 691 | * Substitution can make _several_ words out of one argv[n]! |
| 692 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 693 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 694 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 695 | struct redir_struct *redirects; /* I/O redirections */ |
| 696 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 697 | /* Is there anything in this command at all? */ |
| 698 | #define IS_NULL_CMD(cmd) \ |
| 699 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 700 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 701 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 702 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 703 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 704 | int alive_cmds; /* number of commands running (not exited) */ |
| 705 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 706 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 707 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 708 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 709 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 710 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 711 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 712 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 713 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 714 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 715 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 716 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 717 | PIPE_SEQ = 0, |
| 718 | PIPE_AND = 1, |
| 719 | PIPE_OR = 2, |
| 720 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 721 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 722 | /* Is there anything in this pipe at all? */ |
| 723 | #define IS_NULL_PIPE(pi) \ |
| 724 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 725 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 726 | /* This holds pointers to the various results of parsing */ |
| 727 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 728 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 729 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 730 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 731 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 732 | /* last command in pipe (being constructed right now) */ |
| 733 | struct command *command; |
| 734 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 735 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 736 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 737 | #if !BB_MMU |
| 738 | o_string as_string; |
| 739 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 740 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 741 | #if HAS_KEYWORDS |
| 742 | smallint ctx_res_w; |
| 743 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 744 | #if ENABLE_HUSH_CASE |
| 745 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 746 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 747 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 748 | int old_flag; |
| 749 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 750 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 751 | * when we see "if" or "then", we malloc and copy current context, |
| 752 | * and make ->stack point to it. then we parse pipeN. |
| 753 | * when closing "then" / fi" / whatever is found, |
| 754 | * we move list_head into ->stack->command->group, |
| 755 | * copy ->stack into current context, and delete ->stack. |
| 756 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 757 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 758 | struct parse_context *stack; |
| 759 | #endif |
| 760 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 761 | enum { |
| 762 | MAYBE_ASSIGNMENT = 0, |
| 763 | DEFINITELY_ASSIGNMENT = 1, |
| 764 | NOT_ASSIGNMENT = 2, |
| 765 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 766 | WORD_IS_KEYWORD = 3, |
| 767 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 768 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 769 | /* On program start, environ points to initial environment. |
| 770 | * putenv adds new pointers into it, unsetenv removes them. |
| 771 | * Neither of these (de)allocates the strings. |
| 772 | * setenv allocates new strings in malloc space and does putenv, |
| 773 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 774 | #define setenv(...) setenv_is_leaky_dont_use() |
| 775 | struct variable { |
| 776 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 777 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 778 | 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] | 779 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 780 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 781 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 782 | }; |
| 783 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 784 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 785 | BC_BREAK = 1, |
| 786 | BC_CONTINUE = 2, |
| 787 | }; |
| 788 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 789 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 790 | struct function { |
| 791 | struct function *next; |
| 792 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 793 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 794 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 795 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 796 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 797 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 798 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 799 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 800 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 801 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 802 | /* set -/+o OPT support. (TODO: make it optional) |
| 803 | * bash supports the following opts: |
| 804 | * allexport off |
| 805 | * braceexpand on |
| 806 | * emacs on |
| 807 | * errexit off |
| 808 | * errtrace off |
| 809 | * functrace off |
| 810 | * hashall on |
| 811 | * histexpand off |
| 812 | * history on |
| 813 | * ignoreeof off |
| 814 | * interactive-comments on |
| 815 | * keyword off |
| 816 | * monitor on |
| 817 | * noclobber off |
| 818 | * noexec off |
| 819 | * noglob off |
| 820 | * nolog off |
| 821 | * notify off |
| 822 | * nounset off |
| 823 | * onecmd off |
| 824 | * physical off |
| 825 | * pipefail off |
| 826 | * posix off |
| 827 | * privileged off |
| 828 | * verbose off |
| 829 | * vi off |
| 830 | * xtrace off |
| 831 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 832 | static const char o_opt_strings[] ALIGN1 = |
| 833 | "pipefail\0" |
| 834 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 835 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 836 | #if ENABLE_HUSH_MODE_X |
| 837 | "xtrace\0" |
| 838 | #endif |
| 839 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 840 | enum { |
| 841 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 842 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 843 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 844 | #if ENABLE_HUSH_MODE_X |
| 845 | OPT_O_XTRACE, |
| 846 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 847 | NUM_OPT_O |
| 848 | }; |
| 849 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 850 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 851 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 852 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 853 | /* interactive_fd != 0 means we are an interactive shell. |
| 854 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 855 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 856 | * job control still works, but terminal signals |
| 857 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 858 | * process groups can only be created with "cmd &". |
| 859 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 860 | * to give tty to the foreground process group, |
| 861 | * and will take it back when the group is stopped (^Z) |
| 862 | * or killed (^C). |
| 863 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 864 | #if ENABLE_HUSH_INTERACTIVE |
| 865 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 866 | * _AND_ if we decided to act interactively */ |
| 867 | int interactive_fd; |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 868 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(char *PS1;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 869 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 870 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 871 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 872 | #endif |
| 873 | #if ENABLE_FEATURE_EDITING |
| 874 | line_input_t *line_input_state; |
| 875 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 876 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 877 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 878 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 879 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 880 | random_t random_gen; |
| 881 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 882 | #if ENABLE_HUSH_JOB |
| 883 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 884 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 885 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 886 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 887 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 888 | #else |
| 889 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 890 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 891 | /* How deeply are we in context where "set -e" is ignored */ |
| 892 | int errexit_depth; |
| 893 | /* "set -e" rules (do we follow them correctly?): |
| 894 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 895 | * Shell does not exit if failed command is part of condition in |
| 896 | * if/while, part of && or || list except the last command, any command |
| 897 | * in a pipe but the last, or if the command's return value is being |
| 898 | * inverted with !. If a compound command other than a subshell returns a |
| 899 | * non-zero status because a command failed while -e was being ignored, the |
| 900 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 901 | * exits [ERR is a bashism]. |
| 902 | * |
| 903 | * If a compound command or function executes in a context where -e is |
| 904 | * ignored, none of the commands executed within are affected by the -e |
| 905 | * setting. If a compound command or function sets -e while executing in a |
| 906 | * context where -e is ignored, that setting does not have any effect until |
| 907 | * the compound command or the command containing the function call completes. |
| 908 | */ |
| 909 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 910 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 911 | #if ENABLE_HUSH_MODE_X |
| 912 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 913 | #else |
| 914 | # define G_x_mode 0 |
| 915 | #endif |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 916 | char opt_s; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 917 | char opt_c; |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 918 | #if ENABLE_HUSH_INTERACTIVE |
| 919 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 920 | #endif |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 921 | /* set by signal handler if SIGINT is received _and_ its trap is not set */ |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 922 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 923 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 924 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 925 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 926 | #if ENABLE_HUSH_FUNCTIONS |
| 927 | /* 0: outside of a function (or sourced file) |
| 928 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 929 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 930 | */ |
| 931 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 932 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 933 | #else |
| 934 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 935 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 936 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 937 | /* These support $? */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 938 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 939 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 940 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 941 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 942 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 943 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 944 | # define G_global_args_malloced (G.global_args_malloced) |
| 945 | #else |
| 946 | # define G_global_args_malloced 0 |
| 947 | #endif |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 948 | #if ENABLE_HUSH_BASH_COMPAT |
| 949 | int dead_job_exitcode; /* for "wait -n" */ |
| 950 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 951 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 952 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 953 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 954 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 955 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 956 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 957 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 958 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 959 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 960 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 961 | #if ENABLE_HUSH_GETOPTS |
| 962 | unsigned getopt_count; |
| 963 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 964 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 965 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 966 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 967 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 968 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 969 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 970 | unsigned var_nest_level; |
| 971 | #if ENABLE_HUSH_FUNCTIONS |
| 972 | # if ENABLE_HUSH_LOCAL |
| 973 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 974 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 975 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 976 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 977 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 978 | #if ENABLE_HUSH_FAST |
| 979 | unsigned count_SIGCHLD; |
| 980 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 981 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 982 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 983 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 984 | unsigned parse_lineno; |
| 985 | unsigned execute_lineno; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 986 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 987 | HFILE *HFILE_list; |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 988 | HFILE *HFILE_stdin; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 989 | /* Which signals have non-DFL handler (even with no traps set)? |
| 990 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 991 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 992 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 993 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 994 | * Other than these two times, never modified. |
| 995 | */ |
| 996 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 997 | #if ENABLE_HUSH_JOB |
| 998 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 999 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1000 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1001 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1002 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1003 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 1004 | int pre_trap_exitcode; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 1005 | # if ENABLE_HUSH_FUNCTIONS |
| 1006 | int return_exitcode; |
| 1007 | # endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1008 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1009 | # define G_traps G.traps |
| 1010 | #else |
| 1011 | # define G_traps ((char**)NULL) |
| 1012 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1013 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1014 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1015 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1016 | #endif |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 1017 | #if ENABLE_HUSH_MODE_X |
| 1018 | unsigned x_mode_depth; |
| 1019 | /* "set -x" output should not be redirectable with subsequent 2>FILE. |
| 1020 | * We dup fd#2 to x_mode_fd when "set -x" is executed, and use it |
| 1021 | * for all subsequent output. |
| 1022 | */ |
| 1023 | int x_mode_fd; |
| 1024 | o_string x_mode_buf; |
| 1025 | #endif |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1026 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1027 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1028 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1029 | struct sigaction sa; |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 1030 | char optstring_buf[sizeof("eixcs")]; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 1031 | #if BASH_EPOCH_VARS |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 1032 | char epoch_buf[sizeof("%llu.nnnnnn") + sizeof(long long)*3]; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 1033 | #endif |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 1034 | #if ENABLE_FEATURE_EDITING |
| 1035 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 1036 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1037 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1038 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1039 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 1040 | * (too many defines). Also, I actually prefer to see when a variable |
| 1041 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1042 | #define INIT_G() do { \ |
| 1043 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1044 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 1045 | sigfillset(&G.sa.sa_mask); \ |
| 1046 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1047 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1048 | |
| 1049 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1050 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1051 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1052 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1053 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1054 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1055 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1056 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1057 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1058 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1059 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1060 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1061 | #if ENABLE_HUSH_READONLY |
| 1062 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1063 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1064 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1065 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1066 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1067 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1068 | #if ENABLE_HUSH_GETOPTS |
| 1069 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1070 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1071 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1072 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1073 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1074 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1075 | static int builtin_history(char **argv) FAST_FUNC; |
| 1076 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1077 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1078 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1079 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1080 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1081 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1082 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1083 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1084 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1085 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1086 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1087 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1088 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1089 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1090 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1091 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1092 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1093 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1094 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1095 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1096 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1097 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1098 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1099 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1100 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1101 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1102 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1103 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1104 | #if ENABLE_HUSH_TIMES |
| 1105 | static int builtin_times(char **argv) FAST_FUNC; |
| 1106 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1107 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1108 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1109 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1110 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1111 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1112 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1113 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1114 | #if ENABLE_HUSH_KILL |
| 1115 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1116 | #endif |
| 1117 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1118 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1119 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1120 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1121 | static int builtin_break(char **argv) FAST_FUNC; |
| 1122 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1123 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1124 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1125 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1126 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* Table of built-in functions. They can be forked or not, depending on |
| 1129 | * context: within pipes, they fork. As simple commands, they do not. |
| 1130 | * When used in non-forking context, they can change global variables |
| 1131 | * in the parent shell process. If forked, of course they cannot. |
| 1132 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1133 | * still be set at the end. */ |
| 1134 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1135 | const char *b_cmd; |
| 1136 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1137 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1138 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1139 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1140 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1141 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1142 | #endif |
| 1143 | }; |
| 1144 | |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 1145 | static const struct built_in_command bltins1[] ALIGN_PTR = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1146 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1147 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1148 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1149 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1150 | #endif |
| 1151 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1152 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1153 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1154 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1155 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1156 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1157 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1158 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1159 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1160 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1161 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1162 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1163 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1164 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1165 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1166 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1167 | #if ENABLE_HUSH_GETOPTS |
| 1168 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1169 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1170 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1171 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1172 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1173 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1174 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1175 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1176 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1177 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1178 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1179 | #if ENABLE_HUSH_KILL |
| 1180 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1181 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1182 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1183 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1184 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1185 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1186 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1187 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1188 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1189 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1190 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1191 | #if ENABLE_HUSH_READONLY |
| 1192 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1193 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1194 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1195 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1196 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1197 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1198 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1199 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1200 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1201 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1202 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1203 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1204 | #if ENABLE_HUSH_TIMES |
| 1205 | BLTIN("times" , builtin_times , NULL), |
| 1206 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1207 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1208 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1209 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1210 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1211 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1212 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1213 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1214 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1215 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1216 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1217 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1218 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1219 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1220 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1221 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1222 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1223 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1224 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1225 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1226 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1227 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1228 | * (it's cheaper to run an external program in this case): |
| 1229 | */ |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 1230 | static const struct built_in_command bltins2[] ALIGN_PTR = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1231 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1232 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1233 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1234 | #if BASH_TEST2 |
| 1235 | BLTIN("[[" , builtin_test , NULL), |
| 1236 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1237 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1238 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1239 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1240 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1241 | BLTIN("printf" , builtin_printf , NULL), |
| 1242 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1243 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1244 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1245 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1246 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1247 | }; |
| 1248 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1249 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1250 | /* Debug printouts. |
| 1251 | */ |
Denys Vlasenko | a8e7441 | 2018-07-28 12:16:30 +0200 | [diff] [blame] | 1252 | #if HUSH_DEBUG >= 2 |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1253 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1254 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1255 | # define debug_enter() (G.debug_indent++) |
| 1256 | # define debug_leave() (G.debug_indent--) |
| 1257 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1258 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1259 | # define debug_enter() ((void)0) |
| 1260 | # define debug_leave() ((void)0) |
| 1261 | #endif |
| 1262 | |
| 1263 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1264 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1265 | #endif |
| 1266 | |
| 1267 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1268 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1269 | #endif |
| 1270 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 1271 | #ifndef debug_printf_heredoc |
| 1272 | # define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1273 | #endif |
| 1274 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1275 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1276 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1277 | #endif |
| 1278 | |
| 1279 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1280 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1281 | #endif |
| 1282 | |
| 1283 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1284 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1285 | # define DEBUG_JOBS 1 |
| 1286 | #else |
| 1287 | # define DEBUG_JOBS 0 |
| 1288 | #endif |
| 1289 | |
| 1290 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1291 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1292 | # define DEBUG_EXPAND 1 |
| 1293 | #else |
| 1294 | # define DEBUG_EXPAND 0 |
| 1295 | #endif |
| 1296 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1297 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1298 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1299 | #endif |
| 1300 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1301 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1302 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1303 | # define DEBUG_GLOB 1 |
| 1304 | #else |
| 1305 | # define DEBUG_GLOB 0 |
| 1306 | #endif |
| 1307 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1308 | #ifndef debug_printf_redir |
| 1309 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1310 | #endif |
| 1311 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1312 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1313 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1314 | #endif |
| 1315 | |
| 1316 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1317 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1318 | #endif |
| 1319 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1320 | #ifndef debug_printf_prompt |
| 1321 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1322 | #endif |
| 1323 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1324 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1325 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1326 | # define DEBUG_CLEAN 1 |
| 1327 | #else |
| 1328 | # define DEBUG_CLEAN 0 |
| 1329 | #endif |
| 1330 | |
| 1331 | #if DEBUG_EXPAND |
| 1332 | static void debug_print_strings(const char *prefix, char **vv) |
| 1333 | { |
| 1334 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1335 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1336 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1337 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1338 | } |
| 1339 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1340 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | |
| 1343 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1344 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1345 | */ |
| 1346 | #if LEAK_HUNTING |
| 1347 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1348 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1349 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1350 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1351 | return ptr; |
| 1352 | } |
| 1353 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1354 | { |
| 1355 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1356 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1357 | return ptr; |
| 1358 | } |
| 1359 | static char *xxstrdup(int lineno, const char *str) |
| 1360 | { |
| 1361 | char *ptr = xstrdup(str); |
| 1362 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1363 | return ptr; |
| 1364 | } |
| 1365 | static void xxfree(void *ptr) |
| 1366 | { |
| 1367 | fdprintf(2, "free %p\n", ptr); |
| 1368 | free(ptr); |
| 1369 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1370 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1371 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1372 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1373 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1374 | #endif |
| 1375 | |
| 1376 | |
| 1377 | /* Syntax and runtime errors. They always abort scripts. |
| 1378 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1379 | * and return to the prompt. |
| 1380 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1381 | */ |
| 1382 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1383 | # 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] | 1384 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1385 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1386 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1387 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1388 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1389 | #endif |
| 1390 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1391 | static void die_if_script(void) |
| 1392 | { |
| 1393 | if (!G_interactive_fd) { |
| 1394 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1395 | xfunc_error_retval = G.last_exitcode; |
| 1396 | xfunc_die(); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1401 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1402 | va_list p; |
| 1403 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1404 | #if HUSH_DEBUG >= 2 |
| 1405 | bb_error_msg("hush.c:%u", lineno); |
| 1406 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1407 | va_start(p, fmt); |
| 1408 | bb_verror_msg(fmt, p, NULL); |
| 1409 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1410 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1411 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1412 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1413 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1414 | { |
| 1415 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1416 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1417 | else |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1418 | bb_simple_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1419 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1422 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1423 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1424 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1425 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1428 | 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] | 1429 | { |
Denys Vlasenko | 18bcaf3 | 2020-12-23 23:01:18 +0100 | [diff] [blame] | 1430 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1431 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1432 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1433 | } |
| 1434 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1435 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1436 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1437 | char msg[2] = { ch, '\0' }; |
| 1438 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1441 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1442 | { |
| 1443 | char msg[2]; |
| 1444 | msg[0] = ch; |
| 1445 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1446 | #if HUSH_DEBUG >= 2 |
| 1447 | bb_error_msg("hush.c:%u", lineno); |
| 1448 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1449 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1450 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1453 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1454 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1455 | # undef syntax_error |
| 1456 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1457 | # undef syntax_error_unterm_ch |
| 1458 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1459 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1460 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1461 | # 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] | 1462 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1463 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1464 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1465 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1466 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1467 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1468 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1469 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1470 | /* Utility functions |
| 1471 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1472 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1473 | static char *unbackslash(char *src) |
| 1474 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1475 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1476 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1477 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1478 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1479 | if (*src != '\0') { |
| 1480 | /* \x -> x */ |
| 1481 | *dst++ = *src++; |
| 1482 | continue; |
| 1483 | } |
| 1484 | /* else: "\<nul>". Do not delete this backslash. |
| 1485 | * Testcase: eval 'echo ok\' |
| 1486 | */ |
| 1487 | *dst++ = '\\'; |
| 1488 | /* fallthrough */ |
| 1489 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1490 | if ((*dst++ = *src++) == '\0') |
| 1491 | break; |
| 1492 | } |
| 1493 | return dst; |
| 1494 | } |
| 1495 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1496 | 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] | 1497 | { |
| 1498 | int i; |
| 1499 | unsigned count1; |
| 1500 | unsigned count2; |
| 1501 | char **v; |
| 1502 | |
| 1503 | v = strings; |
| 1504 | count1 = 0; |
| 1505 | if (v) { |
| 1506 | while (*v) { |
| 1507 | count1++; |
| 1508 | v++; |
| 1509 | } |
| 1510 | } |
| 1511 | count2 = 0; |
| 1512 | v = add; |
| 1513 | while (*v) { |
| 1514 | count2++; |
| 1515 | v++; |
| 1516 | } |
| 1517 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1518 | v[count1 + count2] = NULL; |
| 1519 | i = count2; |
| 1520 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1521 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1522 | return v; |
| 1523 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1524 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1525 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1526 | { |
| 1527 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1528 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1529 | return ptr; |
| 1530 | } |
| 1531 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1532 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1533 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1534 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1535 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1536 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1537 | { |
| 1538 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1539 | v[0] = add; |
| 1540 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1541 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1542 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1543 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1544 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1545 | { |
| 1546 | char **ptr = add_string_to_strings(strings, add); |
| 1547 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1548 | return ptr; |
| 1549 | } |
| 1550 | #define add_string_to_strings(strings, add) \ |
| 1551 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1552 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1553 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1554 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1555 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1556 | char **v; |
| 1557 | |
| 1558 | if (!strings) |
| 1559 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1560 | v = strings; |
| 1561 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1562 | free(*v); |
| 1563 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1564 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1565 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1568 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1569 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1570 | int newfd; |
| 1571 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1572 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1573 | if (newfd >= 0) { |
| 1574 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1575 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1576 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1577 | if (errno == EBUSY) |
| 1578 | goto repeat; |
| 1579 | if (errno == EINTR) |
| 1580 | goto repeat; |
| 1581 | } |
| 1582 | return newfd; |
| 1583 | } |
| 1584 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1585 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1586 | { |
| 1587 | int newfd; |
| 1588 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1589 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1590 | if (newfd < 0) { |
| 1591 | if (errno == EBUSY) |
| 1592 | goto repeat; |
| 1593 | if (errno == EINTR) |
| 1594 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1595 | /* fd was not open? */ |
| 1596 | if (errno == EBADF) |
| 1597 | return fd; |
| 1598 | xfunc_die(); |
| 1599 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1600 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1601 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1602 | close(fd); |
| 1603 | return newfd; |
| 1604 | } |
| 1605 | |
| 1606 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1607 | /* Manipulating HFILEs */ |
| 1608 | static HFILE *hfopen(const char *name) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1609 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1610 | HFILE *fp; |
| 1611 | int fd; |
| 1612 | |
| 1613 | fd = STDIN_FILENO; |
| 1614 | if (name) { |
| 1615 | fd = open(name, O_RDONLY | O_CLOEXEC); |
| 1616 | if (fd < 0) |
| 1617 | return NULL; |
| 1618 | if (O_CLOEXEC == 0) /* ancient libc */ |
| 1619 | close_on_exec_on(fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1620 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1621 | |
| 1622 | fp = xmalloc(sizeof(*fp)); |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 1623 | if (name == NULL) |
| 1624 | G.HFILE_stdin = fp; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1625 | fp->fd = fd; |
| 1626 | fp->cur = fp->end = fp->buf; |
| 1627 | fp->next_hfile = G.HFILE_list; |
| 1628 | G.HFILE_list = fp; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1629 | return fp; |
| 1630 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1631 | static void hfclose(HFILE *fp) |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1632 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1633 | HFILE **pp = &G.HFILE_list; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1634 | while (*pp) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1635 | HFILE *cur = *pp; |
| 1636 | if (cur == fp) { |
| 1637 | *pp = cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1638 | break; |
| 1639 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1640 | pp = &cur->next_hfile; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1641 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1642 | if (fp->fd >= 0) |
| 1643 | close(fp->fd); |
| 1644 | free(fp); |
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 | static int refill_HFILE_and_getc(HFILE *fp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1647 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1648 | int n; |
| 1649 | |
| 1650 | if (fp->fd < 0) { |
| 1651 | /* Already saw EOF */ |
| 1652 | return EOF; |
| 1653 | } |
Denys Vlasenko | 521220e | 2020-12-23 23:44:55 +0100 | [diff] [blame] | 1654 | #if ENABLE_HUSH_INTERACTIVE && !ENABLE_FEATURE_EDITING |
| 1655 | /* If user presses ^C, read() restarts after SIGINT (we use SA_RESTART). |
| 1656 | * IOW: ^C will not immediately stop line input. |
| 1657 | * But poll() is different: it does NOT restart after signals. |
| 1658 | */ |
| 1659 | if (fp == G.HFILE_stdin) { |
| 1660 | struct pollfd pfd[1]; |
| 1661 | pfd[0].fd = fp->fd; |
| 1662 | pfd[0].events = POLLIN; |
| 1663 | n = poll(pfd, 1, -1); |
| 1664 | if (n < 0 |
| 1665 | /*&& errno == EINTR - assumed true */ |
| 1666 | && sigismember(&G.pending_set, SIGINT) |
| 1667 | ) { |
| 1668 | return '\0'; |
| 1669 | } |
| 1670 | } |
| 1671 | #else |
| 1672 | /* if FEATURE_EDITING=y, we do not use this routine for interactive input */ |
| 1673 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1674 | /* Try to buffer more input */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1675 | n = safe_read(fp->fd, fp->buf, sizeof(fp->buf)); |
| 1676 | if (n < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1677 | bb_simple_perror_msg("read error"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1678 | n = 0; |
| 1679 | } |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 1680 | fp->cur = fp->buf; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1681 | fp->end = fp->buf + n; |
| 1682 | if (n == 0) { |
| 1683 | /* EOF/error */ |
| 1684 | close(fp->fd); |
| 1685 | fp->fd = -1; |
| 1686 | return EOF; |
| 1687 | } |
| 1688 | return (unsigned char)(*fp->cur++); |
| 1689 | } |
| 1690 | /* Inlined for common case of non-empty buffer. |
| 1691 | */ |
| 1692 | static ALWAYS_INLINE int hfgetc(HFILE *fp) |
| 1693 | { |
| 1694 | if (fp->cur < fp->end) |
| 1695 | return (unsigned char)(*fp->cur++); |
| 1696 | /* Buffer empty */ |
| 1697 | return refill_HFILE_and_getc(fp); |
| 1698 | } |
| 1699 | static int move_HFILEs_on_redirect(int fd, int avoid_fd) |
| 1700 | { |
| 1701 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1702 | while (fl) { |
| 1703 | if (fd == fl->fd) { |
| 1704 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1705 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1706 | 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] | 1707 | return 1; /* "found and moved" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1708 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1709 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1710 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 1711 | #if ENABLE_HUSH_MODE_X |
| 1712 | if (G.x_mode_fd > 0 && fd == G.x_mode_fd) { |
| 1713 | G.x_mode_fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
| 1714 | return 1; /* "found and moved" */ |
| 1715 | } |
| 1716 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1717 | return 0; /* "not in the list" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1718 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1719 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1720 | static void close_all_HFILE_list(void) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1721 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1722 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1723 | while (fl) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1724 | /* hfclose would also free HFILE object. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1725 | * It is disastrous if we share memory with a vforked parent. |
| 1726 | * I'm not sure we never come here after vfork. |
| 1727 | * Therefore just close fd, nothing more. |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1728 | * |
| 1729 | * ">" instead of ">=": we don't close fd#0, |
| 1730 | * interactive shell uses hfopen(NULL) as stdin input |
| 1731 | * which has fl->fd == 0, but fd#0 gets redirected in pipes. |
| 1732 | * If we'd close it here, then e.g. interactive "set | sort" |
| 1733 | * with NOFORKed sort, would have sort's input fd closed. |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1734 | */ |
Denys Vlasenko | e9dccab | 2018-08-05 14:55:01 +0200 | [diff] [blame] | 1735 | if (fl->fd > 0) |
| 1736 | /*hfclose(fl); - unsafe */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1737 | close(fl->fd); |
| 1738 | fl = fl->next_hfile; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1739 | } |
| 1740 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1741 | #endif |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1742 | static int fd_in_HFILEs(int fd) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1743 | { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1744 | HFILE *fl = G.HFILE_list; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1745 | while (fl) { |
| 1746 | if (fl->fd == fd) |
| 1747 | return 1; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 1748 | fl = fl->next_hfile; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1749 | } |
| 1750 | return 0; |
| 1751 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1752 | |
| 1753 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1754 | /* Helpers for setting new $n and restoring them back |
| 1755 | */ |
| 1756 | typedef struct save_arg_t { |
| 1757 | char *sv_argv0; |
| 1758 | char **sv_g_argv; |
| 1759 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1760 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1761 | } save_arg_t; |
| 1762 | |
| 1763 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1764 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1765 | sv->sv_argv0 = argv[0]; |
| 1766 | sv->sv_g_argv = G.global_argv; |
| 1767 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1768 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1769 | |
| 1770 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1771 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1772 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1773 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1774 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1778 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1779 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1780 | if (G.global_args_malloced) { |
| 1781 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1782 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1783 | while (*++pp) /* note: does not free $0 */ |
| 1784 | free(*pp); |
| 1785 | free(G.global_argv); |
| 1786 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1787 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1788 | argv[0] = sv->sv_argv0; |
| 1789 | G.global_argv = sv->sv_g_argv; |
| 1790 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1791 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1795 | /* Basic theory of signal handling in shell |
| 1796 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1797 | * This does not describe what hush does, rather, it is current understanding |
| 1798 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1799 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1800 | * |
| 1801 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1802 | * is finished or backgrounded. It is the same in interactive and |
| 1803 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1804 | * 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] | 1805 | * ^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] | 1806 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1807 | * pipe. |
| 1808 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1809 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1810 | * or by SIGINT in interactive shell. |
| 1811 | * |
| 1812 | * Trap handlers will execute even within trap handlers. (right?) |
| 1813 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1814 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1815 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1816 | * |
| 1817 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1818 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1819 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1820 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1821 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1822 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1823 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1824 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1825 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1826 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1827 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1828 | * |
| 1829 | * SIGQUIT: ignore |
| 1830 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1831 | * SIGHUP (interactive): |
| 1832 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1833 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1834 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1835 | * that all pipe members are stopped. Try this in bash: |
| 1836 | * while :; do :; done - ^Z does not background it |
| 1837 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1838 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1839 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1840 | * to interactive shell while shell is waiting for a pipe, |
| 1841 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1842 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1843 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1844 | * Example 2: this does not wait and does not execute ls: |
| 1845 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1846 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1847 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1848 | * Example 4: this does not wait and does not execute ls: |
| 1849 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1850 | * |
| 1851 | * (What happens to signals which are IGN on shell start?) |
| 1852 | * (What happens with signal mask on shell start?) |
| 1853 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1854 | * Old implementation |
| 1855 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1856 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1857 | * We block all signals which we don't want to take action immediately, |
| 1858 | * i.e. we block all signals which need to have special handling as described |
| 1859 | * above, and all signals which have traps set. |
| 1860 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1861 | * and act on them. |
| 1862 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1863 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1864 | * sigset_t blocked_set: current blocked signal set |
| 1865 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1866 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1867 | * 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] | 1868 | * "trap 'cmd' SIGxxx": |
| 1869 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1870 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1871 | * unblock signals with special interactive handling |
| 1872 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1873 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1874 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1875 | * 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] | 1876 | * 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] | 1877 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1878 | * 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] | 1879 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1880 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1881 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1882 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1883 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1884 | * 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] | 1885 | * 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] | 1886 | * |
| 1887 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1888 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1889 | * masked signals are not visible! |
| 1890 | * |
| 1891 | * New implementation |
| 1892 | * ================== |
| 1893 | * We record each signal we are interested in by installing signal handler |
| 1894 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1895 | * We are interested in: signals which need to have special handling |
| 1896 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1897 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1898 | * After each pipe execution, we extract any pending signals |
| 1899 | * and act on them. |
| 1900 | * |
| 1901 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1902 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1903 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1904 | * sigset_t pending_set: set of sigs we received. |
| 1905 | * |
| 1906 | * "trap - SIGxxx": |
| 1907 | * if sig is in special_sig_mask, set handler back to: |
| 1908 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1909 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1910 | * else: set handler back to SIG_DFL |
| 1911 | * "trap 'cmd' SIGxxx": |
| 1912 | * set handler to record_pending_signo. |
| 1913 | * "trap '' SIGxxx": |
| 1914 | * set handler to SIG_IGN. |
| 1915 | * after [v]fork, if we plan to be a shell: |
| 1916 | * set signals with special interactive handling to SIG_DFL |
| 1917 | * (because child shell is not interactive), |
| 1918 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1919 | * after [v]fork, if we plan to exec: |
| 1920 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1921 | * |
| 1922 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1923 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1924 | * |
| 1925 | * Note (compat): |
| 1926 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1927 | * are set to the default actions". bash interprets it so that traps which |
| 1928 | * 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] | 1929 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1930 | enum { |
| 1931 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1932 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1933 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1934 | | (1 << SIGHUP) |
| 1935 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1936 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1937 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1938 | | (1 << SIGTTIN) |
| 1939 | | (1 << SIGTTOU) |
| 1940 | | (1 << SIGTSTP) |
| 1941 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1942 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1943 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1944 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1945 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1946 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1947 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 1948 | #if ENABLE_FEATURE_EDITING |
| 1949 | bb_got_signal = sig; /* for read_line_input: "we got a signal" */ |
| 1950 | #endif |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1951 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1952 | if (sig == SIGCHLD) { |
| 1953 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1954 | //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] | 1955 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1956 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1957 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1958 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1959 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1960 | { |
| 1961 | struct sigaction old_sa; |
| 1962 | |
| 1963 | /* We could use signal() to install handlers... almost: |
| 1964 | * except that we need to mask ALL signals while handlers run. |
| 1965 | * I saw signal nesting in strace, race window isn't small. |
| 1966 | * SA_RESTART is also needed, but in Linux, signal() |
| 1967 | * sets SA_RESTART too. |
| 1968 | */ |
| 1969 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1970 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1971 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1972 | G.sa.sa_handler = handler; |
| 1973 | sigaction(sig, &G.sa, &old_sa); |
| 1974 | return old_sa.sa_handler; |
| 1975 | } |
| 1976 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1977 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1978 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1979 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1980 | static void restore_ttypgrp_and__exit(void) |
| 1981 | { |
| 1982 | /* xfunc has failed! die die die */ |
| 1983 | /* no EXIT traps, this is an escape hatch! */ |
| 1984 | G.exiting = 1; |
| 1985 | hush_exit(xfunc_error_retval); |
| 1986 | } |
| 1987 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1988 | #if ENABLE_HUSH_JOB |
| 1989 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1990 | /* Needed only on some libc: |
| 1991 | * It was observed that on exit(), fgetc'ed buffered data |
| 1992 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1993 | * With the net effect that even after fork(), not vfork(), |
| 1994 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1995 | * noexec_applet_here |
| 1996 | * echo END_OF_SCRIPT |
| 1997 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1998 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1999 | * 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] | 2000 | * and in `cmd` handling. |
| 2001 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 2002 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 2003 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 2004 | static void fflush_and__exit(void) |
| 2005 | { |
| 2006 | fflush_all(); |
| 2007 | _exit(xfunc_error_retval); |
| 2008 | } |
| 2009 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2010 | /* 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] | 2011 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 2012 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 2013 | # 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] | 2014 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2015 | /* Restores tty foreground process group, and exits. |
| 2016 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2017 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2018 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 2019 | * We also call it if xfunc is exiting. |
| 2020 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2021 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2022 | static void sigexit(int sig) |
| 2023 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2024 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 2025 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 2026 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 2027 | /* Disable all signals: job control, SIGPIPE, etc. |
| 2028 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 2029 | */ |
| 2030 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 2031 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 2032 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2033 | |
| 2034 | /* Not a signal, just exit */ |
| 2035 | if (sig <= 0) |
| 2036 | _exit(- sig); |
| 2037 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 2038 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 2039 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2040 | #else |
| 2041 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2042 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 2043 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2044 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 2045 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2046 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2047 | static sighandler_t pick_sighandler(unsigned sig) |
| 2048 | { |
| 2049 | sighandler_t handler = SIG_DFL; |
| 2050 | if (sig < sizeof(unsigned)*8) { |
| 2051 | unsigned sigmask = (1 << sig); |
| 2052 | |
| 2053 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2054 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2055 | if (G_fatal_sig_mask & sigmask) |
| 2056 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2057 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2058 | #endif |
| 2059 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 2060 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2061 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2062 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2063 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 2064 | * in an endless loop when we try to do some |
| 2065 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2066 | */ |
| 2067 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 2068 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 2069 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2070 | } |
| 2071 | return handler; |
| 2072 | } |
| 2073 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2074 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2075 | static void hush_exit(int exitcode) |
| 2076 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 2077 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
Denys Vlasenko | 00eb23b | 2020-12-21 21:36:58 +0100 | [diff] [blame] | 2078 | save_history(G.line_input_state); /* may be NULL */ |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 2079 | #endif |
| 2080 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 2081 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2082 | 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] | 2083 | char *argv[3]; |
| 2084 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 2085 | 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] | 2086 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 2087 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2088 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 2089 | * "trap" will still show it, if executed |
| 2090 | * in the handler */ |
| 2091 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 2092 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2093 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2094 | #if ENABLE_FEATURE_CLEAN_UP |
| 2095 | { |
| 2096 | struct variable *cur_var; |
| 2097 | if (G.cwd != bb_msg_unknown) |
| 2098 | free((char*)G.cwd); |
| 2099 | cur_var = G.top_var; |
| 2100 | while (cur_var) { |
| 2101 | struct variable *tmp = cur_var; |
| 2102 | if (!cur_var->max_len) |
| 2103 | free(cur_var->varstr); |
| 2104 | cur_var = cur_var->next; |
| 2105 | free(tmp); |
| 2106 | } |
| 2107 | } |
| 2108 | #endif |
| 2109 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2110 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2111 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2112 | sigexit(- (exitcode & 0xff)); |
| 2113 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2114 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2115 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2118 | //TODO: return a mask of ALL handled sigs? |
| 2119 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2120 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2121 | int last_sig = 0; |
| 2122 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2123 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2124 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2125 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2126 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2127 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2128 | sig = 0; |
| 2129 | do { |
| 2130 | sig++; |
| 2131 | if (sigismember(&G.pending_set, sig)) { |
| 2132 | sigdelset(&G.pending_set, sig); |
| 2133 | goto got_sig; |
| 2134 | } |
| 2135 | } while (sig < NSIG); |
| 2136 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2137 | got_sig: |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2138 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2139 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2140 | 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] | 2141 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2142 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2143 | smalluint save_rcode; |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2144 | int save_pre; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2145 | char *argv[3]; |
| 2146 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2147 | argv[1] = xstrdup(G_traps[sig]); |
| 2148 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2149 | argv[2] = NULL; |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2150 | save_pre = G.pre_trap_exitcode; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 2151 | G.pre_trap_exitcode = save_rcode = G.last_exitcode; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2152 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2153 | free(argv[1]); |
Denys Vlasenko | 3ced804 | 2020-02-21 02:55:53 +0100 | [diff] [blame] | 2154 | G.pre_trap_exitcode = save_pre; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2155 | G.last_exitcode = save_rcode; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2156 | # if ENABLE_HUSH_FUNCTIONS |
| 2157 | if (G.return_exitcode >= 0) { |
| 2158 | debug_printf_exec("trap exitcode:%d\n", G.return_exitcode); |
| 2159 | G.last_exitcode = G.return_exitcode; |
| 2160 | } |
| 2161 | # endif |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2162 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2163 | } /* else: "" trap, ignoring signal */ |
| 2164 | continue; |
| 2165 | } |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 2166 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2167 | /* not a trap: special action */ |
| 2168 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2169 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2170 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2171 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2172 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2173 | break; |
| 2174 | #if ENABLE_HUSH_JOB |
| 2175 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2176 | //TODO: why are we doing this? ash and dash don't do this, |
| 2177 | //they have no handler for SIGHUP at all, |
| 2178 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2179 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2180 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2181 | /* bash is observed to signal whole process groups, |
| 2182 | * not individual processes */ |
| 2183 | for (job = G.job_list; job; job = job->next) { |
| 2184 | if (job->pgrp <= 0) |
| 2185 | continue; |
| 2186 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2187 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2188 | kill(- job->pgrp, SIGCONT); |
| 2189 | } |
| 2190 | sigexit(SIGHUP); |
| 2191 | } |
| 2192 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2193 | #if ENABLE_HUSH_FAST |
| 2194 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2195 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2196 | G.count_SIGCHLD++; |
| 2197 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2198 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2199 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2200 | * This simplifies wait builtin a bit. |
| 2201 | */ |
| 2202 | break; |
| 2203 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2204 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2205 | 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] | 2206 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2207 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2208 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2209 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2210 | * in interactive shell, because TERM is ignored. |
| 2211 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2212 | break; |
| 2213 | } |
| 2214 | } |
| 2215 | return last_sig; |
| 2216 | } |
| 2217 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2218 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2219 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2220 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2221 | if (force || G.cwd == NULL) { |
| 2222 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2223 | * we must not try to free(bb_msg_unknown) */ |
| 2224 | if (G.cwd == bb_msg_unknown) |
| 2225 | G.cwd = NULL; |
| 2226 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2227 | if (!G.cwd) |
| 2228 | G.cwd = bb_msg_unknown; |
| 2229 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2230 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2233 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2234 | /* |
| 2235 | * Shell and environment variable support |
| 2236 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2237 | 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] | 2238 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2239 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2240 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2241 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2242 | pp = &G.top_var; |
| 2243 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2244 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2245 | return pp; |
| 2246 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2247 | } |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2251 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2252 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2253 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2254 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2255 | |
| 2256 | if (G.expanded_assignments) { |
| 2257 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2258 | while (*cpp) { |
| 2259 | char *cp = *cpp; |
| 2260 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2261 | return cp + len + 1; |
| 2262 | cpp++; |
| 2263 | } |
| 2264 | } |
| 2265 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2266 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2267 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2268 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2269 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2270 | if (strcmp(name, "PPID") == 0) |
| 2271 | return utoa(G.root_ppid); |
| 2272 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2273 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2274 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2275 | return utoa(next_random(&G.random_gen)); |
| 2276 | #endif |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2277 | #if ENABLE_HUSH_LINENO_VAR |
| 2278 | if (strcmp(name, "LINENO") == 0) |
| 2279 | return utoa(G.execute_lineno); |
| 2280 | #endif |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2281 | #if BASH_EPOCH_VARS |
| 2282 | { |
| 2283 | const char *fmt = NULL; |
| 2284 | if (strcmp(name, "EPOCHSECONDS") == 0) |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 2285 | fmt = "%llu"; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2286 | else if (strcmp(name, "EPOCHREALTIME") == 0) |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 2287 | fmt = "%llu.%06u"; |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2288 | if (fmt) { |
| 2289 | struct timeval tv; |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 2290 | xgettimeofday(&tv); |
| 2291 | sprintf(G.epoch_buf, fmt, (unsigned long long)tv.tv_sec, |
Ron Yorston | a81700b | 2019-04-15 10:48:29 +0100 | [diff] [blame] | 2292 | (unsigned)tv.tv_usec); |
| 2293 | return G.epoch_buf; |
| 2294 | } |
| 2295 | } |
| 2296 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2297 | return NULL; |
| 2298 | } |
| 2299 | |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2300 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2301 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2302 | { |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2303 | if (name_len == 6) { |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2304 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2305 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2306 | return; |
| 2307 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2308 | } |
| 2309 | } |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2310 | #else |
| 2311 | /* Do not even bother evaluating arguments */ |
| 2312 | # define handle_changed_special_names(...) ((void)0) |
| 2313 | #endif |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2314 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2315 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2316 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2317 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2318 | #define SETFLAG_EXPORT (1 << 0) |
| 2319 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2320 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2321 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2322 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2323 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2324 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2325 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2326 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2327 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2328 | int name_len; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2329 | int retval; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2330 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2331 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2332 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2333 | if (HUSH_DEBUG && !eq_sign) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2334 | bb_simple_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2335 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2336 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2337 | cur_pp = &G.top_var; |
| 2338 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2339 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2340 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2341 | continue; |
| 2342 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2343 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2344 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2345 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2346 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2347 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2348 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2349 | //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] | 2350 | return -1; |
| 2351 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2352 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2353 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2354 | *eq_sign = '\0'; |
| 2355 | unsetenv(str); |
| 2356 | *eq_sign = '='; |
| 2357 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2358 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2359 | /* bash 3.2.33(1) and exported vars: |
| 2360 | * # export z=z |
| 2361 | * # f() { local z=a; env | grep ^z; } |
| 2362 | * # f |
| 2363 | * z=a |
| 2364 | * # env | grep ^z |
| 2365 | * z=z |
| 2366 | */ |
| 2367 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2368 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2369 | /* New variable is local ("local VAR=VAL" or |
| 2370 | * "VAR=VAL cmd") |
| 2371 | * and existing one is global, or local |
| 2372 | * on a lower level that new one. |
| 2373 | * Remove it from global variable list: |
| 2374 | */ |
| 2375 | *cur_pp = cur->next; |
| 2376 | if (G.shadowed_vars_pp) { |
| 2377 | /* Save in "shadowed" list */ |
| 2378 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2379 | cur->flg_export ? "exported " : "", |
| 2380 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2381 | ); |
| 2382 | cur->next = *G.shadowed_vars_pp; |
| 2383 | *G.shadowed_vars_pp = cur; |
| 2384 | } else { |
| 2385 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2386 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2387 | cur->flg_export ? "exported " : "", |
| 2388 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2389 | ); |
| 2390 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2391 | free_me = cur->varstr; /* then free it later */ |
| 2392 | free(cur); |
| 2393 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2394 | break; |
| 2395 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2396 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2397 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2398 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2399 | free_and_exp: |
| 2400 | free(str); |
| 2401 | goto exp; |
| 2402 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2403 | |
| 2404 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2405 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2406 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2407 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2408 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2409 | strcpy(cur->varstr, str); |
| 2410 | goto free_and_exp; |
| 2411 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2412 | /* Can't reuse */ |
| 2413 | cur->max_len = 0; |
| 2414 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2415 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2416 | /* max_len == 0 signifies "malloced" var, which we can |
| 2417 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2418 | * if cur->flg_export is 1, it is in the environment. |
| 2419 | * We should either unsetenv+free, or wait until putenv, |
| 2420 | * then putenv(new)+free(old). |
| 2421 | */ |
| 2422 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2423 | goto set_str_and_exp; |
| 2424 | } |
| 2425 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2426 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2427 | 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] | 2428 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2429 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2430 | cur->next = *cur_pp; |
| 2431 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2432 | |
| 2433 | set_str_and_exp: |
| 2434 | cur->varstr = str; |
| 2435 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2436 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2437 | if (flags & SETFLAG_MAKE_RO) { |
| 2438 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2439 | } |
| 2440 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2441 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2442 | cur->flg_export = 1; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2443 | retval = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2444 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2445 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2446 | cur->flg_export = 0; |
| 2447 | /* unsetenv was already done */ |
| 2448 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2449 | 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] | 2450 | retval = putenv(cur->varstr); |
| 2451 | /* fall through to "free(free_me)" - |
| 2452 | * only now we can free old exported malloced string |
| 2453 | */ |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2454 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2455 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2456 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2457 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2458 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2459 | |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 2460 | return retval; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 2463 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
| 2464 | { |
| 2465 | char *var = xasprintf("%s=%s", name, val); |
| 2466 | set_local_var(var, /*flag:*/ 0); |
| 2467 | } |
| 2468 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2469 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2470 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2471 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2472 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2473 | } |
| 2474 | |
Denys Vlasenko | 35a017c | 2018-06-26 18:27:54 +0200 | [diff] [blame] | 2475 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2476 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2477 | { |
| 2478 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2479 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2480 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2481 | cur_pp = &G.top_var; |
| 2482 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2483 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2484 | && cur->varstr[name_len] == '=' |
| 2485 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2486 | if (cur->flg_read_only) { |
| 2487 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2488 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2489 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2490 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2491 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2492 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2493 | bb_unsetenv(cur->varstr); |
| 2494 | if (!cur->max_len) |
| 2495 | free(cur->varstr); |
| 2496 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2497 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2498 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2499 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2500 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2501 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2502 | |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2503 | /* 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] | 2504 | handle_changed_special_names(name, name_len); |
| 2505 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2506 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2509 | static int unset_local_var(const char *name) |
| 2510 | { |
| 2511 | return unset_local_var_len(name, strlen(name)); |
| 2512 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2513 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2514 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2515 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2516 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2517 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2518 | */ |
| 2519 | static void add_vars(struct variable *var) |
| 2520 | { |
| 2521 | struct variable *next; |
| 2522 | |
| 2523 | while (var) { |
| 2524 | next = var->next; |
| 2525 | var->next = G.top_var; |
| 2526 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2527 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2528 | 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] | 2529 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2530 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2531 | 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] | 2532 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2533 | var = next; |
| 2534 | } |
| 2535 | } |
| 2536 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2537 | /* We put strings[i] into variable table and possibly putenv them. |
| 2538 | * If variable is read only, we can free the strings[i] |
| 2539 | * which attempts to overwrite it. |
| 2540 | * The strings[] vector itself is freed. |
| 2541 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2542 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2543 | { |
| 2544 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2545 | |
| 2546 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2547 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2548 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2549 | s = strings; |
| 2550 | while (*s) { |
| 2551 | struct variable *var_p; |
| 2552 | struct variable **var_pp; |
| 2553 | char *eq; |
| 2554 | |
| 2555 | eq = strchr(*s, '='); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2556 | if (HUSH_DEBUG && !eq) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 2557 | bb_simple_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2558 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
| 2559 | if (var_pp) { |
| 2560 | var_p = *var_pp; |
| 2561 | if (var_p->flg_read_only) { |
| 2562 | char **p; |
| 2563 | bb_error_msg("%s: readonly variable", *s); |
| 2564 | /* |
| 2565 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2566 | * If VAR is readonly, leaving it in the list |
| 2567 | * after asssignment error (msg above) |
| 2568 | * causes doubled error message later, on unset. |
| 2569 | */ |
| 2570 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2571 | free(*s); |
| 2572 | p = s; |
| 2573 | do { *p = p[1]; p++; } while (*p); |
| 2574 | goto next; |
| 2575 | } |
| 2576 | /* below, set_local_var() with nest level will |
| 2577 | * "shadow" (remove) this variable from |
| 2578 | * global linked list. |
| 2579 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2580 | } |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2581 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
| 2582 | 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] | 2583 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2584 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2585 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2586 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2591 | * Unicode helper |
| 2592 | */ |
| 2593 | static void reinit_unicode_for_hush(void) |
| 2594 | { |
| 2595 | /* Unicode support should be activated even if LANG is set |
| 2596 | * _during_ shell execution, not only if it was set when |
| 2597 | * shell was started. Therefore, re-check LANG every time: |
| 2598 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2599 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2600 | || ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 4c201c0 | 2018-07-17 15:04:17 +0200 | [diff] [blame] | 2601 | ) { |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2602 | const char *s = get_local_var_value("LC_ALL"); |
| 2603 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2604 | if (!s) s = get_local_var_value("LANG"); |
| 2605 | reinit_unicode(s); |
| 2606 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2609 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2610 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2611 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2612 | |
| 2613 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2614 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2615 | * echo $P\ |
| 2616 | * \ |
| 2617 | * AT\ |
| 2618 | * H\ |
| 2619 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2620 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2621 | */ |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2622 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2623 | { |
| 2624 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2625 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2626 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2627 | |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2628 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2629 | prompt_str = get_local_var_value(G.promptmode == 0 ? "PS1" : "PS2"); |
| 2630 | if (!prompt_str) |
| 2631 | prompt_str = ""; |
| 2632 | # else |
| 2633 | prompt_str = "> "; /* if PS2, else... */ |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2634 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2635 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
| 2636 | free(G.PS1); |
| 2637 | /* bash uses $PWD value, even if it is set by user. |
| 2638 | * It uses current dir only if PWD is unset. |
| 2639 | * We always use current dir. */ |
Denys Vlasenko | 649acb9 | 2020-12-23 15:29:13 +0100 | [diff] [blame] | 2640 | prompt_str = G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2641 | } |
Denys Vlasenko | 4ebcdf7 | 2019-05-16 15:39:19 +0200 | [diff] [blame] | 2642 | # endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2643 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2644 | return prompt_str; |
| 2645 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2646 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2647 | { |
Denys Vlasenko | 46a71dc | 2020-12-25 18:49:29 +0100 | [diff] [blame] | 2648 | # if ENABLE_FEATURE_EDITING |
| 2649 | /* In EDITING case, this function reads next input line, |
| 2650 | * saves it in i->p, then returns 1st char of it. |
| 2651 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2652 | int r; |
| 2653 | const char *prompt_str; |
| 2654 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2655 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2656 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2657 | reinit_unicode_for_hush(); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2658 | G.flag_SIGINT = 0; |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 2659 | |
| 2660 | bb_got_signal = 0; |
| 2661 | if (!sigisemptyset(&G.pending_set)) { |
| 2662 | /* Whoops, already got a signal, do not call read_line_input */ |
| 2663 | bb_got_signal = r = -1; |
| 2664 | } else { |
| 2665 | /* For shell, LI_INTERRUPTIBLE is set: |
| 2666 | * read_line_input will abort on either |
| 2667 | * getting EINTR in poll(), or if it sees bb_got_signal != 0 |
| 2668 | * (IOW: if signal arrives before poll() is reached). |
| 2669 | * Interactive testcases: |
| 2670 | * (while kill -INT $$; do sleep 1; done) & |
| 2671 | * #^^^ prints ^C, prints prompt, repeats |
| 2672 | * trap 'echo I' int; (while kill -INT $$; do sleep 1; done) & |
| 2673 | * #^^^ prints ^C, prints "I", prints prompt, repeats |
| 2674 | * trap 'echo T' term; (while kill $$; do sleep 1; done) & |
| 2675 | * #^^^ prints "T", prints prompt, repeats |
| 2676 | * #(bash 5.0.17 exits after first "T", looks like a bug) |
| 2677 | */ |
| 2678 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2679 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 2680 | ); |
| 2681 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
| 2682 | if (r == 0) |
| 2683 | raise(SIGINT); |
| 2684 | } |
| 2685 | /* bash prints ^C (before running a trap, if any) |
| 2686 | * both on keyboard ^C and on real SIGINT (non-kbd generated). |
| 2687 | */ |
| 2688 | if (sigismember(&G.pending_set, SIGINT)) { |
| 2689 | write(STDOUT_FILENO, "^C\n", 3); |
| 2690 | G.last_exitcode = 128 | SIGINT; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 2691 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2692 | check_and_run_traps(); |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 2693 | if (r == 0) /* keyboard ^C? */ |
| 2694 | continue; /* go back, read another input line */ |
| 2695 | if (r > 0) /* normal input? (no ^C, no ^D, no signals) */ |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2696 | break; |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 2697 | if (!bb_got_signal) { |
| 2698 | /* r < 0: ^D/EOF/error detected (but not signal) */ |
| 2699 | /* ^D on interactive input goes to next line before exiting: */ |
| 2700 | write(STDOUT_FILENO, "\n", 1); |
| 2701 | i->p = NULL; |
| 2702 | i->peek_buf[0] = r = EOF; |
| 2703 | return r; |
| 2704 | } |
| 2705 | /* it was a signal: go back, read another input line */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2706 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2707 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2708 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2709 | # else |
Denys Vlasenko | 46a71dc | 2020-12-25 18:49:29 +0100 | [diff] [blame] | 2710 | /* In !EDITING case, this function gets called for every char. |
| 2711 | * Buffering happens deeper in the call chain, in hfgetc(i->file). |
| 2712 | */ |
| 2713 | int r; |
| 2714 | |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2715 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2716 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2717 | if (i->last_char == '\0' || i->last_char == '\n') { |
Denys Vlasenko | 46a71dc | 2020-12-25 18:49:29 +0100 | [diff] [blame] | 2718 | const char *prompt_str = setup_prompt_string(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2719 | /* Why check_and_run_traps here? Try this interactively: |
| 2720 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2721 | * $ <[enter], repeatedly...> |
| 2722 | * Without check_and_run_traps, handler never runs. |
| 2723 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2724 | check_and_run_traps(); |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 2725 | fputs_stdout(prompt_str); |
Denys Vlasenko | 521220e | 2020-12-23 23:44:55 +0100 | [diff] [blame] | 2726 | fflush_all(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2727 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2728 | r = hfgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2729 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2730 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2731 | * it generates SIGINT as usual. |
| 2732 | */ |
| 2733 | check_and_run_traps(); |
Denys Vlasenko | 521220e | 2020-12-23 23:44:55 +0100 | [diff] [blame] | 2734 | if (r != '\0' && !G.flag_SIGINT) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2735 | break; |
Denys Vlasenko | 521220e | 2020-12-23 23:44:55 +0100 | [diff] [blame] | 2736 | if (G.flag_SIGINT) { |
| 2737 | /* ^C or SIGINT: repeat */ |
| 2738 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2739 | /* kernel prints "^C" itself, just print newline: */ |
| 2740 | write(STDOUT_FILENO, "\n", 1); |
| 2741 | G.last_exitcode = 128 | SIGINT; |
| 2742 | } |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2743 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2744 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2745 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2746 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2747 | /* This is the magic location that prints prompts |
| 2748 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2749 | static int fgetc_interactive(struct in_str *i) |
| 2750 | { |
| 2751 | int ch; |
| 2752 | /* If it's interactive stdin, get new line. */ |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 2753 | if (G_interactive_fd && i->file == G.HFILE_stdin) { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2754 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2755 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2756 | G.promptmode = 1; /* PS2 */ |
| 2757 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2758 | } else { |
| 2759 | /* Not stdin: script file, sourced file, etc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2760 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2761 | } |
| 2762 | return ch; |
| 2763 | } |
Denys Vlasenko | 649acb9 | 2020-12-23 15:29:13 +0100 | [diff] [blame] | 2764 | #else /* !INTERACTIVE */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2765 | static ALWAYS_INLINE int fgetc_interactive(struct in_str *i) |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2766 | { |
| 2767 | int ch; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2768 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2769 | return ch; |
| 2770 | } |
Denys Vlasenko | 649acb9 | 2020-12-23 15:29:13 +0100 | [diff] [blame] | 2771 | #endif /* !INTERACTIVE */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2772 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2773 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2774 | { |
| 2775 | int ch; |
| 2776 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2777 | if (!i->file) { |
| 2778 | /* string-based in_str */ |
| 2779 | ch = (unsigned char)*i->p; |
| 2780 | if (ch != '\0') { |
| 2781 | i->p++; |
| 2782 | i->last_char = ch; |
Denys Vlasenko | 574b9c4 | 2021-09-07 21:44:44 +0200 | [diff] [blame] | 2783 | #if ENABLE_HUSH_LINENO_VAR |
| 2784 | if (ch == '\n') { |
| 2785 | G.parse_lineno++; |
| 2786 | debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno); |
| 2787 | } |
| 2788 | #endif |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2789 | return ch; |
| 2790 | } |
| 2791 | return EOF; |
| 2792 | } |
| 2793 | |
| 2794 | /* FILE-based in_str */ |
| 2795 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2796 | #if ENABLE_FEATURE_EDITING |
| 2797 | /* This can be stdin, check line editing char[] buffer */ |
| 2798 | if (i->p && *i->p != '\0') { |
| 2799 | ch = (unsigned char)*i->p++; |
| 2800 | goto out; |
| 2801 | } |
| 2802 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2803 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2804 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2805 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2806 | int ch2 = i->peek_buf[1]; |
| 2807 | i->peek_buf[0] = ch2; |
| 2808 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2809 | goto out; |
| 2810 | i->peek_buf[1] = 0; |
| 2811 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2812 | } |
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 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2815 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2816 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2817 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2818 | #if ENABLE_HUSH_LINENO_VAR |
| 2819 | if (ch == '\n') { |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 2820 | G.parse_lineno++; |
| 2821 | debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2822 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2823 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2824 | return ch; |
| 2825 | } |
| 2826 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2827 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2828 | { |
| 2829 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2830 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2831 | if (!i->file) { |
| 2832 | /* string-based in_str */ |
| 2833 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2834 | return (unsigned char)*i->p; |
| 2835 | } |
| 2836 | |
| 2837 | /* FILE-based in_str */ |
| 2838 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2839 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2840 | /* This can be stdin, check line editing char[] buffer */ |
| 2841 | if (i->p && *i->p != '\0') |
| 2842 | return (unsigned char)*i->p; |
| 2843 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2844 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2845 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2846 | if (ch != 0) |
| 2847 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2848 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2849 | /* Need to get a new char */ |
| 2850 | ch = fgetc_interactive(i); |
| 2851 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2852 | |
| 2853 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2854 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2855 | if (i->p) { |
| 2856 | i->p -= 1; |
| 2857 | return ch; |
| 2858 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2859 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2860 | i->peek_buf[0] = ch; |
| 2861 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2862 | return ch; |
| 2863 | } |
| 2864 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2865 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2866 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2867 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2868 | */ |
| 2869 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2870 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2871 | int ch; |
| 2872 | |
| 2873 | /* There are two cases when i->p[] buffer exists. |
| 2874 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2875 | * (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] | 2876 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2877 | * the peek2 result is in i->p[1]. |
| 2878 | */ |
| 2879 | if (i->p) |
| 2880 | return (unsigned char)i->p[1]; |
| 2881 | |
| 2882 | /* Now we know it is a file-based in_str. */ |
| 2883 | |
| 2884 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2885 | /* Is there 2nd char? */ |
| 2886 | ch = i->peek_buf[1]; |
| 2887 | if (ch == 0) { |
| 2888 | /* We did not read it yet, get it now */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2889 | do ch = hfgetc(i->file); while (ch == '\0'); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2890 | i->peek_buf[1] = ch; |
| 2891 | } |
| 2892 | |
| 2893 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2894 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2895 | } |
| 2896 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2897 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2898 | { |
| 2899 | for (;;) { |
| 2900 | int ch, ch2; |
| 2901 | |
| 2902 | ch = i_getch(input); |
| 2903 | if (ch != '\\') |
| 2904 | return ch; |
| 2905 | ch2 = i_peek(input); |
| 2906 | if (ch2 != '\n') |
| 2907 | return ch; |
| 2908 | /* backslash+newline, skip it */ |
| 2909 | i_getch(input); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2914 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2915 | */ |
| 2916 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2917 | { |
| 2918 | for (;;) { |
| 2919 | int ch, ch2; |
| 2920 | |
| 2921 | ch = i_peek(input); |
| 2922 | if (ch != '\\') |
| 2923 | return ch; |
| 2924 | ch2 = i_peek2(input); |
| 2925 | if (ch2 != '\n') |
| 2926 | return ch; |
| 2927 | /* backslash+newline, skip it */ |
| 2928 | i_getch(input); |
| 2929 | i_getch(input); |
| 2930 | } |
| 2931 | } |
| 2932 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2933 | static void setup_file_in_str(struct in_str *i, HFILE *fp) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2934 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2935 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 2936 | i->file = fp; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2937 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2941 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2942 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2943 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2944 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2948 | /* |
| 2949 | * o_string support |
| 2950 | */ |
| 2951 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2952 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2953 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2954 | { |
| 2955 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2956 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2957 | if (o->data) |
| 2958 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2961 | static void o_free_and_set_NULL(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2962 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2963 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2964 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 2967 | static ALWAYS_INLINE void o_free(o_string *o) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2968 | { |
| 2969 | free(o->data); |
| 2970 | } |
| 2971 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2972 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2973 | { |
| 2974 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2975 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2976 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2977 | } |
| 2978 | } |
| 2979 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2980 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2981 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2982 | 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] | 2983 | if (o->length < o->maxlen) { |
| 2984 | /* likely. avoid o_grow_by() call */ |
| 2985 | add: |
| 2986 | o->data[o->length] = ch; |
| 2987 | o->length++; |
| 2988 | o->data[o->length] = '\0'; |
| 2989 | return; |
| 2990 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2991 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2992 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2995 | #if 0 |
| 2996 | /* Valid only if we know o_string is not empty */ |
| 2997 | static void o_delchr(o_string *o) |
| 2998 | { |
| 2999 | o->length--; |
| 3000 | o->data[o->length] = '\0'; |
| 3001 | } |
| 3002 | #endif |
| 3003 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3004 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3005 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3006 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 3007 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3008 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3011 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3012 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3013 | o_addblock(o, str, strlen(str)); |
| 3014 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3015 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 3016 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 3017 | { |
| 3018 | o_addblock(o, str, strlen(str) + 1); |
| 3019 | } |
| 3020 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3021 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3022 | static void nommu_addchr(o_string *o, int ch) |
| 3023 | { |
| 3024 | if (o) |
| 3025 | o_addchr(o, ch); |
| 3026 | } |
| 3027 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3028 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3029 | #endif |
| 3030 | |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 3031 | #if ENABLE_HUSH_MODE_X |
| 3032 | static void x_mode_addchr(int ch) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3033 | { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 3034 | o_addchr(&G.x_mode_buf, ch); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3035 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 3036 | static void x_mode_addstr(const char *str) |
| 3037 | { |
| 3038 | o_addstr(&G.x_mode_buf, str); |
| 3039 | } |
| 3040 | static void x_mode_addblock(const char *str, int len) |
| 3041 | { |
| 3042 | o_addblock(&G.x_mode_buf, str, len); |
| 3043 | } |
| 3044 | static void x_mode_prefix(void) |
| 3045 | { |
| 3046 | int n = G.x_mode_depth; |
| 3047 | do x_mode_addchr('+'); while (--n >= 0); |
| 3048 | } |
| 3049 | static void x_mode_flush(void) |
| 3050 | { |
| 3051 | int len = G.x_mode_buf.length; |
| 3052 | if (len <= 0) |
| 3053 | return; |
| 3054 | if (G.x_mode_fd > 0) { |
| 3055 | G.x_mode_buf.data[len] = '\n'; |
| 3056 | full_write(G.x_mode_fd, G.x_mode_buf.data, len + 1); |
| 3057 | } |
| 3058 | G.x_mode_buf.length = 0; |
| 3059 | } |
| 3060 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3061 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3062 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3063 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3064 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 3065 | * Apparently, on unquoted $v bash still does globbing |
| 3066 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 3067 | * but NOT brace expansion! Thus, there should be TWO independent |
| 3068 | * quoting mechanisms on $v expansion side: one protects |
| 3069 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 3070 | * We have only second one. |
| 3071 | */ |
| 3072 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3073 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3074 | # define MAYBE_BRACES "{}" |
| 3075 | #else |
| 3076 | # define MAYBE_BRACES "" |
| 3077 | #endif |
| 3078 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3079 | /* My analysis of quoting semantics tells me that state information |
| 3080 | * is associated with a destination, not a source. |
| 3081 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3082 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3083 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3084 | int sz = 1; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3085 | /* '-' is included because of this case: |
| 3086 | * >filename0 >filename1 >filename9; v='-'; echo filename[0"$v"9] |
| 3087 | */ |
| 3088 | char *found = strchr("*?[-\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3089 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3090 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3091 | o_grow_by(o, sz); |
| 3092 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3093 | o->data[o->length] = '\\'; |
| 3094 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3095 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3096 | o->data[o->length] = ch; |
| 3097 | o->length++; |
| 3098 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3101 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3102 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3103 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3104 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3105 | && strchr("*?[-\\" MAYBE_BRACES, ch) |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3106 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3107 | sz++; |
| 3108 | o->data[o->length] = '\\'; |
| 3109 | o->length++; |
| 3110 | } |
| 3111 | o_grow_by(o, sz); |
| 3112 | o->data[o->length] = ch; |
| 3113 | o->length++; |
| 3114 | o->data[o->length] = '\0'; |
| 3115 | } |
| 3116 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3117 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3118 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3119 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3120 | char ch; |
| 3121 | int sz; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3122 | int ordinary_cnt = strcspn(str, "*?[-\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3123 | if (ordinary_cnt > len) /* paranoia */ |
| 3124 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3125 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3126 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3127 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3128 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3129 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3130 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3131 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3132 | sz = 1; |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 3133 | if (ch) { /* it is necessarily one of "*?[-\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3134 | sz++; |
| 3135 | o->data[o->length] = '\\'; |
| 3136 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3137 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 3138 | o_grow_by(o, sz); |
| 3139 | o->data[o->length] = ch; |
| 3140 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3141 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3142 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3145 | static void o_addQblock(o_string *o, const char *str, int len) |
| 3146 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3147 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3148 | o_addblock(o, str, len); |
| 3149 | return; |
| 3150 | } |
| 3151 | o_addqblock(o, str, len); |
| 3152 | } |
| 3153 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3154 | static void o_addQstr(o_string *o, const char *str) |
| 3155 | { |
| 3156 | o_addQblock(o, str, strlen(str)); |
| 3157 | } |
| 3158 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3159 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 3160 | * 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] | 3161 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3162 | * list[i] contains an INDEX (int!) into this string data. |
| 3163 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 3164 | * but list[i]'s need not be modified. |
| 3165 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3166 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3167 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 3168 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3169 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 3170 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 3171 | { |
| 3172 | char **list = (char**)o->data; |
| 3173 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3174 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3175 | |
| 3176 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3177 | 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] | 3178 | prefix, list, n, string_start, o->length, o->maxlen, |
| 3179 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 3180 | o->has_quoted_part, |
| 3181 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3182 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3183 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3184 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 3185 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 3186 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3187 | i++; |
| 3188 | } |
| 3189 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3190 | 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] | 3191 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3192 | 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] | 3193 | } |
| 3194 | } |
| 3195 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3196 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3197 | #endif |
| 3198 | |
| 3199 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3200 | * in list[n] so that it points past last stored byte so far. |
| 3201 | * It returns n+1. */ |
| 3202 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3203 | { |
| 3204 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3205 | int string_start; |
| 3206 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3207 | |
| 3208 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3209 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3210 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3211 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3212 | 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] | 3213 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3214 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3215 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3216 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3217 | memmove(list + n + 0x10, list + n, string_len); |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 3218 | /* |
| 3219 | * expand_on_ifs() has a "previous argv[] ends in IFS?" |
| 3220 | * check. (grep for -prev-ifs-check-). |
| 3221 | * Ensure that argv[-1][last] is not garbage |
| 3222 | * but zero bytes, to save index check there. |
| 3223 | */ |
| 3224 | list[n + 0x10 - 1] = 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3225 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3226 | } else { |
| 3227 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3228 | n, string_len, string_start); |
| 3229 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3230 | } else { |
| 3231 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3232 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3233 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3234 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3235 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3236 | o->has_empty_slot = 0; |
| 3237 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3238 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3239 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3240 | return n + 1; |
| 3241 | } |
| 3242 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3243 | /* "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] | 3244 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3245 | { |
| 3246 | char **list = (char**)o->data; |
| 3247 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3248 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3249 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3252 | /* |
| 3253 | * Globbing routines. |
| 3254 | * |
| 3255 | * Most words in commands need to be globbed, even ones which are |
| 3256 | * (single or double) quoted. This stems from the possiblity of |
| 3257 | * constructs like "abc"* and 'abc'* - these should be globbed. |
| 3258 | * Having a different code path for fully-quoted strings ("abc", |
| 3259 | * 'abc') would only help performance-wise, but we still need |
| 3260 | * code for partially-quoted strings. |
| 3261 | * |
| 3262 | * 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] | 3263 | * the logic can't be "shell-syntax argument is first transformed |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3264 | * to a string, then globbed, and if globbing does not match anything, |
| 3265 | * it is used verbatim". Here are two examples where it fails: |
| 3266 | * |
| 3267 | * echo 'b\*'? |
| 3268 | * |
| 3269 | * The globbing can't be avoided (because of '?' at the end). |
| 3270 | * The glob pattern is: b\\\*? - IOW, both \ and * are literals |
| 3271 | * 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] | 3272 | * - IOW: they "unbackslash" the glob pattern. |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3273 | * Now, look at this: |
| 3274 | * |
| 3275 | * v='\\\*'; echo b$v? |
| 3276 | * |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3277 | * The glob pattern is the same here: b\\\*? - the unquoted $v expansion |
Denys Vlasenko | 4bf0854 | 2018-08-11 18:44:11 +0200 | [diff] [blame] | 3278 | * should be used as glob pattern with no changes. However, if glob |
Denys Vlasenko | c97df29 | 2018-08-14 11:04:58 +0200 | [diff] [blame] | 3279 | * 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] | 3280 | * |
| 3281 | * ash implements this by having an encoded representation of the word |
| 3282 | * to glob, which IS NOT THE SAME as the glob pattern - it has more data. |
| 3283 | * Glob pattern is derived from it. If glob fails, the decision what result |
| 3284 | * should be is made using that encoded representation. Not glob pattern. |
| 3285 | */ |
| 3286 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3287 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3288 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3289 | * first, it processes even {a} (no commas), second, |
| 3290 | * 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] | 3291 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3292 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3293 | |
| 3294 | /* Helper */ |
| 3295 | static int glob_needed(const char *s) |
| 3296 | { |
| 3297 | while (*s) { |
| 3298 | if (*s == '\\') { |
| 3299 | if (!s[1]) |
| 3300 | return 0; |
| 3301 | s += 2; |
| 3302 | continue; |
| 3303 | } |
| 3304 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3305 | return 1; |
| 3306 | s++; |
| 3307 | } |
| 3308 | return 0; |
| 3309 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3310 | /* Return pointer to next closing brace or to comma */ |
| 3311 | static const char *next_brace_sub(const char *cp) |
| 3312 | { |
| 3313 | unsigned depth = 0; |
| 3314 | cp++; |
| 3315 | while (*cp != '\0') { |
| 3316 | if (*cp == '\\') { |
| 3317 | if (*++cp == '\0') |
| 3318 | break; |
| 3319 | cp++; |
| 3320 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3321 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3322 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3323 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3324 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3325 | depth++; |
| 3326 | } |
| 3327 | |
| 3328 | return *cp != '\0' ? cp : NULL; |
| 3329 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3330 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3331 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3332 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3333 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3334 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3335 | const char *next; |
| 3336 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3337 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3338 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3339 | |
| 3340 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3341 | |
| 3342 | begin = pattern; |
| 3343 | while (1) { |
| 3344 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3345 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3346 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3347 | /* Find the first sub-pattern and at the same time |
| 3348 | * find the rest after the closing brace */ |
| 3349 | next = next_brace_sub(begin); |
| 3350 | if (next == NULL) { |
| 3351 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3352 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3353 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3354 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3355 | /* "{abc}" with no commas - illegal |
| 3356 | * brace expr, disregard and skip it */ |
| 3357 | begin = next + 1; |
| 3358 | continue; |
| 3359 | } |
| 3360 | break; |
| 3361 | } |
| 3362 | if (*begin == '\\' && begin[1] != '\0') |
| 3363 | begin++; |
| 3364 | begin++; |
| 3365 | } |
| 3366 | debug_printf_glob("begin:%s\n", begin); |
| 3367 | debug_printf_glob("next:%s\n", next); |
| 3368 | |
| 3369 | /* Now find the end of the whole brace expression */ |
| 3370 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3371 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3372 | rest = next_brace_sub(rest); |
| 3373 | if (rest == NULL) { |
| 3374 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3375 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3376 | } |
| 3377 | debug_printf_glob("rest:%s\n", rest); |
| 3378 | } |
| 3379 | rest_len = strlen(++rest) + 1; |
| 3380 | |
| 3381 | /* We are sure the brace expression is well-formed */ |
| 3382 | |
| 3383 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3384 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3385 | |
| 3386 | /* We have a brace expression. BEGIN points to the opening {, |
| 3387 | * NEXT points past the terminator of the first element, and REST |
| 3388 | * points past the final }. We will accumulate result names from |
| 3389 | * recursive runs for each brace alternative in the buffer using |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 3390 | * GLOB_APPEND. */ |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3391 | |
| 3392 | p = begin + 1; |
| 3393 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3394 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3395 | memcpy( |
| 3396 | mempcpy( |
| 3397 | mempcpy(new_pattern_buf, |
| 3398 | /* We know the prefix for all sub-patterns */ |
| 3399 | pattern, begin - pattern), |
| 3400 | p, next - p), |
| 3401 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3402 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3403 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3404 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3405 | */ |
| 3406 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3407 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3408 | /* We saw the last entry */ |
| 3409 | break; |
| 3410 | } |
| 3411 | p = next + 1; |
| 3412 | next = next_brace_sub(next); |
| 3413 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3414 | free(new_pattern_buf); |
| 3415 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3416 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3417 | simple_glob: |
| 3418 | { |
| 3419 | int gr; |
| 3420 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3421 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3422 | memset(&globdata, 0, sizeof(globdata)); |
| 3423 | gr = glob(pattern, 0, NULL, &globdata); |
| 3424 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3425 | if (gr != 0) { |
| 3426 | if (gr == GLOB_NOMATCH) { |
| 3427 | globfree(&globdata); |
| 3428 | /* NB: garbles parameter */ |
| 3429 | unbackslash(pattern); |
| 3430 | o_addstr_with_NUL(o, pattern); |
| 3431 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3432 | return o_save_ptr_helper(o, n); |
| 3433 | } |
| 3434 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3435 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3436 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3437 | * but we didn't specify it. Paranoia again. */ |
| 3438 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3439 | } |
| 3440 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3441 | char **argv = globdata.gl_pathv; |
| 3442 | while (1) { |
| 3443 | o_addstr_with_NUL(o, *argv); |
| 3444 | n = o_save_ptr_helper(o, n); |
| 3445 | argv++; |
| 3446 | if (!*argv) |
| 3447 | break; |
| 3448 | } |
| 3449 | } |
| 3450 | globfree(&globdata); |
| 3451 | } |
| 3452 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3453 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3454 | /* Performs globbing on last list[], |
| 3455 | * saving each result as a new list[]. |
| 3456 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3457 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3458 | { |
| 3459 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3460 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3461 | 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] | 3462 | if (!o->data) |
| 3463 | return o_save_ptr_helper(o, n); |
| 3464 | pattern = o->data + o_get_last_ptr(o, n); |
| 3465 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3466 | if (!glob_needed(pattern)) { |
| 3467 | /* unbackslash last string in o in place, fix length */ |
| 3468 | o->length = unbackslash(pattern) - o->data; |
| 3469 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3470 | return o_save_ptr_helper(o, n); |
| 3471 | } |
| 3472 | |
| 3473 | copy = xstrdup(pattern); |
| 3474 | /* "forget" pattern in o */ |
| 3475 | o->length = pattern - o->data; |
| 3476 | n = glob_brace(copy, o, n); |
| 3477 | free(copy); |
| 3478 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3479 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3480 | return n; |
| 3481 | } |
| 3482 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3483 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3484 | |
| 3485 | /* Helper */ |
| 3486 | static int glob_needed(const char *s) |
| 3487 | { |
| 3488 | while (*s) { |
| 3489 | if (*s == '\\') { |
| 3490 | if (!s[1]) |
| 3491 | return 0; |
| 3492 | s += 2; |
| 3493 | continue; |
| 3494 | } |
| 3495 | if (*s == '*' || *s == '[' || *s == '?') |
| 3496 | return 1; |
| 3497 | s++; |
| 3498 | } |
| 3499 | return 0; |
| 3500 | } |
| 3501 | /* Performs globbing on last list[], |
| 3502 | * saving each result as a new list[]. |
| 3503 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3504 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3505 | { |
| 3506 | glob_t globdata; |
| 3507 | int gr; |
| 3508 | char *pattern; |
| 3509 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3510 | 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] | 3511 | if (!o->data) |
| 3512 | return o_save_ptr_helper(o, n); |
| 3513 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3514 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3515 | if (!glob_needed(pattern)) { |
| 3516 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3517 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3518 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3519 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3520 | return o_save_ptr_helper(o, n); |
| 3521 | } |
| 3522 | |
| 3523 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3524 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3525 | * If we glob "*.\*" and don't find anything, we need |
| 3526 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3527 | * will return "*.\*"! |
| 3528 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3529 | gr = glob(pattern, 0, NULL, &globdata); |
| 3530 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3531 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3532 | if (gr == GLOB_NOMATCH) { |
| 3533 | globfree(&globdata); |
| 3534 | goto literal; |
| 3535 | } |
| 3536 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3537 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3538 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3539 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3540 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3541 | } |
| 3542 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3543 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3544 | /* "forget" pattern in o */ |
| 3545 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3546 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3547 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3548 | n = o_save_ptr_helper(o, n); |
| 3549 | argv++; |
| 3550 | if (!*argv) |
| 3551 | break; |
| 3552 | } |
| 3553 | } |
| 3554 | globfree(&globdata); |
| 3555 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3556 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3557 | return n; |
| 3558 | } |
| 3559 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3560 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3561 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3562 | /* 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] | 3563 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3564 | static int o_save_ptr(o_string *o, int n) |
| 3565 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3566 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3567 | /* If o->has_empty_slot, list[n] was already globbed |
| 3568 | * (if it was requested back then when it was filled) |
| 3569 | * so don't do that again! */ |
| 3570 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3571 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3572 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3573 | return o_save_ptr_helper(o, n); |
| 3574 | } |
| 3575 | |
| 3576 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3577 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3578 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3579 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3580 | int string_start; |
| 3581 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3582 | if (DEBUG_EXPAND) |
| 3583 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3584 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3585 | list = (char**)o->data; |
| 3586 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3587 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3588 | while (n) { |
| 3589 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3590 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3591 | } |
| 3592 | return list; |
| 3593 | } |
| 3594 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3595 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3596 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3597 | /* Returns pi->next - next pipe in the list */ |
| 3598 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3599 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3600 | struct pipe *next; |
| 3601 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3602 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3603 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3604 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3605 | struct command *command; |
| 3606 | struct redir_struct *r, *rnext; |
| 3607 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3608 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3609 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3610 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3611 | if (DEBUG_CLEAN) { |
| 3612 | int a; |
| 3613 | char **p; |
| 3614 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3615 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3616 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3617 | } |
| 3618 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3619 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3620 | } |
| 3621 | /* not "else if": on syntax error, we may have both! */ |
| 3622 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3623 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3624 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3625 | free_pipe_list(command->group); |
| 3626 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3627 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3628 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3629 | /* else is crucial here. |
| 3630 | * If group != NULL, child_func is meaningless */ |
| 3631 | #if ENABLE_HUSH_FUNCTIONS |
| 3632 | else if (command->child_func) { |
| 3633 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3634 | command->child_func->parent_cmd = NULL; |
| 3635 | } |
| 3636 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3637 | #if !BB_MMU |
| 3638 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3639 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3640 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3641 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3642 | debug_printf_clean(" redirect %d%s", |
| 3643 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3644 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3645 | if (r->rd_filename) { |
| 3646 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3647 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3648 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3649 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3650 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3651 | rnext = r->next; |
| 3652 | free(r); |
| 3653 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3654 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3655 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3656 | 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] | 3657 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3658 | #if ENABLE_HUSH_JOB |
| 3659 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3660 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3661 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3662 | |
| 3663 | next = pi->next; |
| 3664 | free(pi); |
| 3665 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3666 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3667 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3668 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3669 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3670 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3671 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3672 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3673 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3674 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3675 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3676 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3680 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3681 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3682 | #ifndef debug_print_tree |
| 3683 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3684 | { |
Denys Vlasenko | 987be93 | 2022-02-06 20:07:12 +0100 | [diff] [blame] | 3685 | static const char *const PIPE[] ALIGN_PTR = { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3686 | [PIPE_SEQ] = "SEQ", |
| 3687 | [PIPE_AND] = "AND", |
| 3688 | [PIPE_OR ] = "OR" , |
| 3689 | [PIPE_BG ] = "BG" , |
| 3690 | }; |
| 3691 | static const char *RES[] = { |
| 3692 | [RES_NONE ] = "NONE" , |
| 3693 | # if ENABLE_HUSH_IF |
| 3694 | [RES_IF ] = "IF" , |
| 3695 | [RES_THEN ] = "THEN" , |
| 3696 | [RES_ELIF ] = "ELIF" , |
| 3697 | [RES_ELSE ] = "ELSE" , |
| 3698 | [RES_FI ] = "FI" , |
| 3699 | # endif |
| 3700 | # if ENABLE_HUSH_LOOPS |
| 3701 | [RES_FOR ] = "FOR" , |
| 3702 | [RES_WHILE] = "WHILE", |
| 3703 | [RES_UNTIL] = "UNTIL", |
| 3704 | [RES_DO ] = "DO" , |
| 3705 | [RES_DONE ] = "DONE" , |
| 3706 | # endif |
| 3707 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3708 | [RES_IN ] = "IN" , |
| 3709 | # endif |
| 3710 | # if ENABLE_HUSH_CASE |
| 3711 | [RES_CASE ] = "CASE" , |
| 3712 | [RES_CASE_IN ] = "CASE_IN" , |
| 3713 | [RES_MATCH] = "MATCH", |
| 3714 | [RES_CASE_BODY] = "CASE_BODY", |
| 3715 | [RES_ESAC ] = "ESAC" , |
| 3716 | # endif |
| 3717 | [RES_XXXX ] = "XXXX" , |
| 3718 | [RES_SNTX ] = "SNTX" , |
| 3719 | }; |
Denys Vlasenko | 987be93 | 2022-02-06 20:07:12 +0100 | [diff] [blame] | 3720 | static const char *const CMDTYPE[] ALIGN_PTR = { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3721 | "{}", |
| 3722 | "()", |
| 3723 | "[noglob]", |
| 3724 | # if ENABLE_HUSH_FUNCTIONS |
| 3725 | "func()", |
| 3726 | # endif |
| 3727 | }; |
| 3728 | |
| 3729 | int pin, prn; |
| 3730 | |
| 3731 | pin = 0; |
| 3732 | while (pi) { |
Denys Vlasenko | 83a4967 | 2021-06-15 18:12:13 +0200 | [diff] [blame] | 3733 | fdprintf(2, "%*spipe %d #cmds:%d %sres_word=%s followup=%d %s\n", |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3734 | lvl*2, "", |
| 3735 | pin, |
Denys Vlasenko | 83a4967 | 2021-06-15 18:12:13 +0200 | [diff] [blame] | 3736 | pi->num_cmds, |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3737 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3738 | RES[pi->res_word], |
| 3739 | pi->followup, PIPE[pi->followup] |
| 3740 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3741 | prn = 0; |
| 3742 | while (prn < pi->num_cmds) { |
| 3743 | struct command *command = &pi->cmds[prn]; |
| 3744 | char **argv = command->argv; |
| 3745 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3746 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3747 | lvl*2, "", prn, |
| 3748 | command->assignment_cnt); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 3749 | # if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3750 | fdprintf(2, " LINENO:%u", command->lineno); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 3751 | # endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3752 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3753 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3754 | CMDTYPE[command->cmd_type], |
| 3755 | argv |
| 3756 | # if !BB_MMU |
| 3757 | , " group_as_string:", command->group_as_string |
| 3758 | # else |
| 3759 | , "", "" |
| 3760 | # endif |
| 3761 | ); |
| 3762 | debug_print_tree(command->group, lvl+1); |
| 3763 | prn++; |
| 3764 | continue; |
| 3765 | } |
| 3766 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3767 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3768 | argv++; |
| 3769 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 3770 | if (command->redirects) |
| 3771 | fdprintf(2, " {redir}"); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3772 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3773 | prn++; |
| 3774 | } |
| 3775 | pi = pi->next; |
| 3776 | pin++; |
| 3777 | } |
| 3778 | } |
| 3779 | #endif /* debug_print_tree */ |
| 3780 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3781 | static struct pipe *new_pipe(void) |
| 3782 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3783 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3784 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3785 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3786 | return pi; |
| 3787 | } |
| 3788 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3789 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3790 | * if ctx->command is NULL. |
| 3791 | * No errors possible here. |
| 3792 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3793 | static int done_command(struct parse_context *ctx) |
| 3794 | { |
| 3795 | /* The command is really already in the pipe structure, so |
| 3796 | * advance the pipe counter and make a new, null command. */ |
| 3797 | struct pipe *pi = ctx->pipe; |
| 3798 | struct command *command = ctx->command; |
| 3799 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3800 | #if 0 /* Instead we emit error message at run time */ |
| 3801 | if (ctx->pending_redirect) { |
| 3802 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3803 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3804 | ctx->pending_redirect = NULL; |
| 3805 | } |
| 3806 | #endif |
| 3807 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3808 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3809 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3810 | 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] | 3811 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3812 | } |
| 3813 | pi->num_cmds++; |
| 3814 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3815 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3816 | } else { |
| 3817 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3818 | } |
| 3819 | |
| 3820 | /* Only real trickiness here is that the uncommitted |
| 3821 | * command structure is not counted in pi->num_cmds. */ |
| 3822 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3823 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3824 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3825 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3826 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 3827 | command->lineno = G.parse_lineno; |
| 3828 | 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] | 3829 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3830 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3831 | } |
| 3832 | |
| 3833 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3834 | { |
| 3835 | int not_null; |
| 3836 | |
| 3837 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3838 | /* Close previous command */ |
| 3839 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3840 | #if HAS_KEYWORDS |
| 3841 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3842 | ctx->ctx_inverted = 0; |
| 3843 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3844 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3845 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3846 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3847 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3848 | * in a backgrounded subshell. |
| 3849 | */ |
| 3850 | struct pipe *pi; |
| 3851 | struct command *command; |
| 3852 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3853 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3854 | pi = ctx->list_head; |
| 3855 | while (pi != ctx->pipe) { |
| 3856 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3857 | goto no_conv; |
| 3858 | pi = pi->next; |
| 3859 | } |
| 3860 | |
| 3861 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3862 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3863 | pi = xzalloc(sizeof(*pi)); |
| 3864 | pi->followup = PIPE_BG; |
| 3865 | pi->num_cmds = 1; |
| 3866 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3867 | command = &pi->cmds[0]; |
| 3868 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3869 | command->cmd_type = CMD_NORMAL; |
| 3870 | command->group = ctx->list_head; |
| 3871 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3872 | command->group_as_string = xstrndup( |
| 3873 | ctx->as_string.data, |
| 3874 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3875 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3876 | #endif |
| 3877 | /* Replace all pipes in ctx with one newly created */ |
| 3878 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | 83a4967 | 2021-06-15 18:12:13 +0200 | [diff] [blame] | 3879 | /* for cases like "cmd && &", do not be tricked by last command |
| 3880 | * being null - the entire {...} & is NOT null! */ |
| 3881 | not_null = 1; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3882 | } else { |
| 3883 | no_conv: |
| 3884 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3885 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3886 | |
| 3887 | /* Without this check, even just <enter> on command line generates |
| 3888 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3889 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3890 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3891 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3892 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3893 | #endif |
| 3894 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3895 | || ctx->ctx_res_w == RES_DONE |
| 3896 | || ctx->ctx_res_w == RES_FOR |
| 3897 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3898 | #endif |
| 3899 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3900 | || ctx->ctx_res_w == RES_ESAC |
| 3901 | #endif |
| 3902 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3903 | struct pipe *new_p; |
| 3904 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3905 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3906 | not_null, ctx->ctx_res_w); |
| 3907 | new_p = new_pipe(); |
| 3908 | ctx->pipe->next = new_p; |
| 3909 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3910 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3911 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3912 | * This is used to control execution. |
| 3913 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3914 | * cases where variable or value happens to match a keyword): |
| 3915 | */ |
| 3916 | #if ENABLE_HUSH_LOOPS |
| 3917 | if (ctx->ctx_res_w == RES_FOR |
| 3918 | || ctx->ctx_res_w == RES_IN) |
| 3919 | ctx->ctx_res_w = RES_NONE; |
| 3920 | #endif |
| 3921 | #if ENABLE_HUSH_CASE |
| 3922 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3923 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3924 | if (ctx->ctx_res_w == RES_CASE) |
| 3925 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3926 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3927 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3928 | /* Create the memory for command, roughly: |
| 3929 | * ctx->pipe->cmds = new struct command; |
| 3930 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3931 | */ |
| 3932 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3933 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3934 | } |
| 3935 | debug_printf_parse("done_pipe return\n"); |
| 3936 | } |
| 3937 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3938 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3939 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3940 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3941 | if (MAYBE_ASSIGNMENT != 0) |
| 3942 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3943 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3944 | /* Create the memory for command, roughly: |
| 3945 | * ctx->pipe->cmds = new struct command; |
| 3946 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3947 | */ |
| 3948 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3951 | /* If a reserved word is found and processed, parse context is modified |
| 3952 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3953 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3954 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3955 | struct reserved_combo { |
| 3956 | char literal[6]; |
| 3957 | unsigned char res; |
| 3958 | unsigned char assignment_flag; |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 3959 | uint32_t flag; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3960 | }; |
| 3961 | enum { |
| 3962 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3963 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3964 | FLAG_IF = (1 << RES_IF ), |
| 3965 | FLAG_THEN = (1 << RES_THEN ), |
| 3966 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3967 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3968 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3969 | # endif |
| 3970 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3971 | FLAG_FOR = (1 << RES_FOR ), |
| 3972 | FLAG_WHILE = (1 << RES_WHILE), |
| 3973 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3974 | FLAG_DO = (1 << RES_DO ), |
| 3975 | FLAG_DONE = (1 << RES_DONE ), |
| 3976 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3977 | # endif |
| 3978 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3979 | FLAG_MATCH = (1 << RES_MATCH), |
| 3980 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3981 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3982 | FLAG_START = (1 << RES_XXXX ), |
| 3983 | }; |
| 3984 | |
| 3985 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3986 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3987 | /* Mostly a list of accepted follow-up reserved words. |
| 3988 | * FLAG_END means we are done with the sequence, and are ready |
| 3989 | * to turn the compound list into a command. |
| 3990 | * FLAG_START means the word must start a new compound list. |
| 3991 | */ |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 3992 | static const struct reserved_combo reserved_list[] ALIGN4 = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3993 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3994 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3995 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3996 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3997 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3998 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3999 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4000 | # endif |
| 4001 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4002 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 4003 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 4004 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 4005 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 4006 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 4007 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4008 | # endif |
| 4009 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4010 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 4011 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4012 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4013 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4014 | const struct reserved_combo *r; |
| 4015 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 4016 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4017 | if (strcmp(word->data, r->literal) == 0) |
| 4018 | return r; |
| 4019 | } |
| 4020 | return NULL; |
| 4021 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4022 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4023 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4024 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4025 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4026 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4027 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4028 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4029 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4030 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4031 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 4032 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4033 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4034 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4035 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4036 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4037 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4038 | |
| 4039 | 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] | 4040 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4041 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 4042 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4043 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4044 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4045 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4046 | if (r->flag == 0) { /* '!' */ |
| 4047 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4048 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4049 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4050 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4051 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4052 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4053 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4054 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4055 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4056 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 4057 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4058 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4059 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4060 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4061 | } 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] | 4062 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4063 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4064 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4065 | } else { |
| 4066 | /* "{...} fi" is ok. "{...} if" is not |
| 4067 | * Example: |
| 4068 | * if { echo foo; } then { echo bar; } fi */ |
| 4069 | if (ctx->command->group) |
| 4070 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4071 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4072 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4073 | ctx->ctx_res_w = r->res; |
| 4074 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4075 | ctx->is_assignment = r->assignment_flag; |
| 4076 | 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] | 4077 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4078 | if (ctx->old_flag & FLAG_END) { |
| 4079 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4080 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4081 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4082 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4083 | old = ctx->stack; |
| 4084 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4085 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4086 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4087 | /* At this point, the compound command's string is in |
| 4088 | * ctx->as_string... except for the leading keyword! |
| 4089 | * Consider this example: "echo a | if true; then echo a; fi" |
| 4090 | * ctx->as_string will contain "true; then echo a; fi", |
| 4091 | * with "if " remaining in old->as_string! |
| 4092 | */ |
| 4093 | { |
| 4094 | char *str; |
| 4095 | int len = old->as_string.length; |
| 4096 | /* Concatenate halves */ |
| 4097 | o_addstr(&old->as_string, ctx->as_string.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4098 | o_free(&ctx->as_string); |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4099 | /* Find where leading keyword starts in first half */ |
| 4100 | str = old->as_string.data + len; |
| 4101 | if (str > old->as_string.data) |
| 4102 | str--; /* skip whitespace after keyword */ |
| 4103 | while (str > old->as_string.data && isalpha(str[-1])) |
| 4104 | str--; |
| 4105 | /* Ugh, we're done with this horrid hack */ |
| 4106 | old->command->group_as_string = xstrdup(str); |
| 4107 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 4108 | old->command->group_as_string); |
| 4109 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4110 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4111 | *ctx = *old; /* physical copy */ |
| 4112 | free(old); |
| 4113 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4114 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4115 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4116 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4117 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4118 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4119 | * Normal return is 0. Syntax errors return 1. |
| 4120 | * Note: on return, word is reset, but not o_free'd! |
| 4121 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4122 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4123 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4124 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4125 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4126 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 4127 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4128 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 4129 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4130 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4131 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4132 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 4133 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 4134 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4135 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4136 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4137 | * If the redirection operator is "<<" or "<<-", the word |
| 4138 | * that follows the redirection operator shall be |
| 4139 | * subjected to quote removal; it is unspecified whether |
| 4140 | * any of the other expansions occur. For the other |
| 4141 | * redirection operators, the word that follows the |
| 4142 | * redirection operator shall be subjected to tilde |
| 4143 | * expansion, parameter expansion, command substitution, |
| 4144 | * arithmetic expansion, and quote removal. |
| 4145 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4146 | * on the word by a non-interactive shell; an interactive |
| 4147 | * shell may perform it, but shall do so only when |
| 4148 | * the expansion would result in one word." |
| 4149 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 4150 | //bash does not do parameter/command substitution or arithmetic expansion |
| 4151 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 4152 | // as written: |
| 4153 | // <<EOF$t |
| 4154 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 4155 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 4156 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4157 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4158 | /* Cater for >\file case: |
| 4159 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 4160 | * Same with heredocs: |
| 4161 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 4162 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4163 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 4164 | unbackslash(ctx->pending_redirect->rd_filename); |
| 4165 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4166 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4167 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 4168 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4169 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4170 | 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] | 4171 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4172 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4173 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4174 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4175 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4176 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 4177 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 4178 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4179 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 4180 | ctx->ctx_dsemicolon = 0; |
| 4181 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4182 | # endif |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4183 | # if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 4184 | if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB |
| 4185 | && strcmp(ctx->word.data, "]]") == 0 |
| 4186 | ) { |
| 4187 | /* allow "[[ ]] >file" etc */ |
| 4188 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4189 | } else |
| 4190 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4191 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4192 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4193 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 4194 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4195 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4196 | # if ENABLE_HUSH_CASE |
| 4197 | && ctx->ctx_res_w != RES_CASE |
| 4198 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4199 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4200 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4201 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4202 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4203 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 4204 | # if ENABLE_HUSH_LINENO_VAR |
| 4205 | /* Case: |
| 4206 | * "while ...; do |
| 4207 | * cmd ..." |
| 4208 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 4209 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 4210 | */ |
| 4211 | if (0 |
| 4212 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 4213 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 4214 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 4215 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 4216 | ) { |
| 4217 | done_pipe(ctx, PIPE_SEQ); |
| 4218 | } |
| 4219 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4220 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4221 | debug_printf_parse("done_word return %d\n", |
| 4222 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4223 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4224 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4225 | # if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 4226 | if (strcmp(ctx->word.data, "[[") == 0) { |
| 4227 | command->cmd_type = CMD_TEST2_SINGLEWORD_NOGLOB; |
| 4228 | } else |
| 4229 | # endif |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4230 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 4231 | if (0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4232 | /* In bash, local/export/readonly are special, args |
| 4233 | * are assignments and therefore expansion of them |
| 4234 | * should be "one-word" expansion: |
| 4235 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 4236 | * compare with: |
| 4237 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 4238 | * ls: cannot access i=a: No such file or directory |
| 4239 | * ls: cannot access b: No such file or directory |
| 4240 | * Note: bash 3.2.33(1) does this only if export word |
| 4241 | * itself is not quoted: |
| 4242 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 4243 | * aaa bbb |
| 4244 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 4245 | * aaa |
| 4246 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4247 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 4248 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 4249 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4250 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4251 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4252 | } |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 4253 | # else |
| 4254 | { /* empty block to pair "if ... else" */ } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4255 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4256 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4257 | #endif /* HAS_KEYWORDS */ |
| 4258 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4259 | if (command->group) { |
| 4260 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4261 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4262 | debug_printf_parse("done_word return 1: syntax error, " |
| 4263 | "groups and arglists don't mix\n"); |
| 4264 | return 1; |
| 4265 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4266 | |
| 4267 | /* If this word wasn't an assignment, next ones definitely |
| 4268 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4269 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4270 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4271 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4272 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4273 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4274 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4275 | command->assignment_cnt++; |
| 4276 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4277 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4278 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4279 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4280 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4281 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4282 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4283 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4284 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4285 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4286 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4287 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4288 | if (ctx->word.has_quoted_part |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 4289 | || endofname(command->argv[0])[0] != '\0' |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4290 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4291 | /* bash says just "not a valid identifier" */ |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 4292 | syntax_error("bad variable name in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4293 | return 1; |
| 4294 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4295 | /* Force FOR to have just one word (variable name) */ |
| 4296 | /* NB: basically, this makes hush see "for v in ..." |
| 4297 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4298 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4299 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4300 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4301 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4302 | #if ENABLE_HUSH_CASE |
| 4303 | /* Force CASE to have just one word */ |
| 4304 | if (ctx->ctx_res_w == RES_CASE) { |
| 4305 | done_pipe(ctx, PIPE_SEQ); |
| 4306 | } |
| 4307 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4308 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4309 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4310 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4311 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4312 | return 0; |
| 4313 | } |
| 4314 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4315 | |
| 4316 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4317 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4318 | * Return: |
| 4319 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4320 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4321 | * REDIRFD_TO_FILE if no & was seen, |
| 4322 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4323 | */ |
| 4324 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4325 | #define parse_redir_right_fd(as_string, input) \ |
| 4326 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4327 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4328 | 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] | 4329 | { |
| 4330 | int ch, d, ok; |
| 4331 | |
| 4332 | ch = i_peek(input); |
| 4333 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4334 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4335 | |
| 4336 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4337 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4338 | ch = i_peek(input); |
| 4339 | if (ch == '-') { |
| 4340 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4341 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4342 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4343 | } |
| 4344 | d = 0; |
| 4345 | ok = 0; |
| 4346 | while (ch != EOF && isdigit(ch)) { |
| 4347 | d = d*10 + (ch-'0'); |
| 4348 | ok = 1; |
| 4349 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4350 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4351 | ch = i_peek(input); |
| 4352 | } |
| 4353 | if (ok) return d; |
| 4354 | |
| 4355 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4356 | |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 4357 | bb_simple_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4358 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4361 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4362 | */ |
| 4363 | static int parse_redirect(struct parse_context *ctx, |
| 4364 | int fd, |
| 4365 | redir_type style, |
| 4366 | struct in_str *input) |
| 4367 | { |
| 4368 | struct command *command = ctx->command; |
| 4369 | struct redir_struct *redir; |
| 4370 | struct redir_struct **redirp; |
| 4371 | int dup_num; |
| 4372 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4373 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4374 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4375 | /* Check for a '>&1' type redirect */ |
| 4376 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4377 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4378 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4379 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4380 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4381 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4382 | if (dup_num) { /* <<-... */ |
| 4383 | ch = i_getch(input); |
| 4384 | nommu_addchr(&ctx->as_string, ch); |
| 4385 | ch = i_peek(input); |
| 4386 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4387 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4388 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4389 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4390 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4391 | if (ch == '|') { |
| 4392 | /* >|FILE redirect ("clobbering" >). |
| 4393 | * Since we do not support "set -o noclobber" yet, |
| 4394 | * >| and > are the same for now. Just eat |. |
| 4395 | */ |
| 4396 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4397 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | /* Create a new redir_struct and append it to the linked list */ |
| 4402 | redirp = &command->redirects; |
| 4403 | while ((redir = *redirp) != NULL) { |
| 4404 | redirp = &(redir->next); |
| 4405 | } |
| 4406 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4407 | /* redir->next = NULL; */ |
| 4408 | /* redir->rd_filename = NULL; */ |
| 4409 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4410 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4411 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4412 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4413 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4414 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4415 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4416 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4417 | /* Erik had a check here that the file descriptor in question |
| 4418 | * is legit; I postpone that to "run time" |
| 4419 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4420 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4421 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4422 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4423 | #if 0 /* Instead we emit error message at run time */ |
| 4424 | if (ctx->pending_redirect) { |
| 4425 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4426 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4427 | } |
| 4428 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4429 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4430 | * end of the next parsed word. */ |
| 4431 | ctx->pending_redirect = redir; |
| 4432 | } |
| 4433 | return 0; |
| 4434 | } |
| 4435 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4436 | /* If a redirect is immediately preceded by a number, that number is |
| 4437 | * supposed to tell which file descriptor to redirect. This routine |
| 4438 | * looks for such preceding numbers. In an ideal world this routine |
| 4439 | * needs to handle all the following classes of redirects... |
| 4440 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4441 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4442 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4443 | * 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] | 4444 | * |
| 4445 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4446 | * "2.7 Redirection |
| 4447 | * ... If n is quoted, the number shall not be recognized as part of |
| 4448 | * the redirection expression. For example: |
| 4449 | * echo \2>a |
| 4450 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4451 | * 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] | 4452 | * |
| 4453 | * A -1 return means no valid number was found, |
| 4454 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4455 | */ |
| 4456 | static int redirect_opt_num(o_string *o) |
| 4457 | { |
| 4458 | int num; |
| 4459 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4460 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4461 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4462 | num = bb_strtou(o->data, NULL, 10); |
| 4463 | if (errno || num < 0) |
| 4464 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4465 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4466 | return num; |
| 4467 | } |
| 4468 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4469 | #if BB_MMU |
| 4470 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4471 | fetch_till_str(input, word, skip_tabs) |
| 4472 | #endif |
| 4473 | static char *fetch_till_str(o_string *as_string, |
| 4474 | struct in_str *input, |
| 4475 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4476 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4477 | { |
| 4478 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4479 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4480 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4481 | int ch; |
| 4482 | |
Denys Vlasenko | d73cdbf | 2018-07-23 15:43:57 +0200 | [diff] [blame] | 4483 | /* Starting with "" is necessary for this case: |
| 4484 | * cat <<EOF |
| 4485 | * |
| 4486 | * xxx |
| 4487 | * EOF |
| 4488 | */ |
| 4489 | heredoc.data = xzalloc(1); /* start as "", not as NULL */ |
| 4490 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4491 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4492 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4493 | while (1) { |
| 4494 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4495 | if (ch != EOF) |
| 4496 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4497 | if (ch == '\n' || ch == EOF) { |
| 4498 | check_heredoc_end: |
| 4499 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4500 | /* End-of-line, and not a line continuation */ |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4501 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4502 | heredoc.data[past_EOL] = '\0'; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4503 | debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4504 | return heredoc.data; |
| 4505 | } |
| 4506 | if (ch == '\n') { |
| 4507 | /* This is a new line. |
| 4508 | * Remember position and backslash-escaping status. |
| 4509 | */ |
| 4510 | o_addchr(&heredoc, ch); |
| 4511 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4512 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4513 | past_EOL = heredoc.length; |
| 4514 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4515 | do { |
| 4516 | ch = i_getch(input); |
| 4517 | if (ch != EOF) |
| 4518 | nommu_addchr(as_string, ch); |
| 4519 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4520 | /* If this immediately ended the line, |
| 4521 | * go back to end-of-line checks. |
| 4522 | */ |
| 4523 | if (ch == '\n') |
| 4524 | goto check_heredoc_end; |
| 4525 | } |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4526 | } else { |
| 4527 | /* Backslash-line continuation in an unquoted |
| 4528 | * heredoc. This does not need special handling |
| 4529 | * for heredoc body (unquoted heredocs are |
| 4530 | * expanded on "execution" and that would take |
| 4531 | * care of this case too), but not the case |
| 4532 | * of line continuation *in terminator*: |
| 4533 | * cat <<EOF |
| 4534 | * Ok1 |
| 4535 | * EO\ |
| 4536 | * F |
| 4537 | */ |
| 4538 | heredoc.data[--heredoc.length] = '\0'; |
| 4539 | prev = 0; /* not '\' */ |
| 4540 | continue; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4541 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4542 | } |
| 4543 | if (ch == EOF) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 4544 | o_free(&heredoc); |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4545 | return NULL; /* error */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4546 | } |
| 4547 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4548 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4549 | if (prev == '\\' && ch == '\\') |
| 4550 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
Denys Vlasenko | dfc7394 | 2018-07-24 14:03:18 +0200 | [diff] [blame] | 4551 | prev = 0; /* not '\' */ |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4552 | else |
| 4553 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4557 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4558 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4559 | */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4560 | #if BB_MMU |
| 4561 | #define fetch_heredocs(as_string, pi, heredoc_cnt, input) \ |
| 4562 | fetch_heredocs(pi, heredoc_cnt, input) |
| 4563 | #endif |
| 4564 | 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] | 4565 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4566 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4567 | int i; |
| 4568 | struct command *cmd = pi->cmds; |
| 4569 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4570 | debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4571 | pi->num_cmds, |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4572 | cmd->argv ? cmd->argv[0] : "NONE" |
| 4573 | ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4574 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4575 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4576 | |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 4577 | debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n", |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4578 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4579 | while (redir) { |
| 4580 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4581 | char *p; |
| 4582 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4583 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4584 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4585 | p = fetch_till_str(as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4586 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4587 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4588 | syntax_error("unexpected EOF in here document"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4589 | return -1; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4590 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4591 | free(redir->rd_filename); |
| 4592 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4593 | heredoc_cnt--; |
| 4594 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4595 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4596 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4597 | if (cmd->group) { |
| 4598 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4599 | heredoc_cnt = fetch_heredocs(as_string, cmd->group, heredoc_cnt, input); |
| 4600 | //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt); |
| 4601 | if (heredoc_cnt < 0) |
| 4602 | return heredoc_cnt; /* error */ |
| 4603 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4604 | cmd++; |
| 4605 | } |
| 4606 | pi = pi->next; |
| 4607 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4608 | return heredoc_cnt; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4612 | static int run_list(struct pipe *pi); |
| 4613 | #if BB_MMU |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4614 | #define parse_stream(pstring, heredoc_cnt_ptr, input, end_trigger) \ |
| 4615 | parse_stream(heredoc_cnt_ptr, input, end_trigger) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4616 | #endif |
| 4617 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4618 | int *heredoc_cnt_ptr, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4619 | struct in_str *input, |
| 4620 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4621 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4622 | /* Returns number of heredocs not yet consumed, |
| 4623 | * or -1 on error. |
| 4624 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4625 | static int parse_group(struct parse_context *ctx, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4626 | struct in_str *input, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4627 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4628 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4629 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4630 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4631 | #if BB_MMU |
| 4632 | # define as_string NULL |
| 4633 | #else |
| 4634 | char *as_string = NULL; |
| 4635 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4636 | struct pipe *pipe_list; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4637 | int heredoc_cnt = 0; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4638 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4639 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4640 | |
| 4641 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4642 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4643 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4644 | if (ctx->word.length) |
| 4645 | if (done_word(ctx)) |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4646 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4647 | if (!command->argv) |
| 4648 | goto skip; /* (... */ |
| 4649 | if (command->argv[1]) { /* word word ... (... */ |
| 4650 | syntax_error_unexpected_ch('('); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4651 | return -1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4652 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4653 | /* it is "word(..." or "word (..." */ |
| 4654 | do |
| 4655 | ch = i_getch(input); |
| 4656 | while (ch == ' ' || ch == '\t'); |
| 4657 | if (ch != ')') { |
| 4658 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4659 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4660 | } |
| 4661 | nommu_addchr(&ctx->as_string, ch); |
| 4662 | do |
| 4663 | ch = i_getch(input); |
| 4664 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4665 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4666 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4667 | return -1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4668 | } |
| 4669 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4670 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4671 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4672 | } |
| 4673 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4674 | |
| 4675 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4676 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4677 | || ctx->word.length /* word{... */ |
| 4678 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4679 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4680 | syntax_error(NULL); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4681 | debug_printf_parse("parse_group return -1: " |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4682 | "syntax error, groups and arglists don't mix\n"); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4683 | return -1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4684 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4685 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4686 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4687 | IF_HUSH_FUNCTIONS(skip:) |
| 4688 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4689 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4690 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4691 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4692 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4693 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4694 | } else { |
| 4695 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4696 | ch = i_peek(input); |
| 4697 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4698 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4699 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4700 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4701 | return -1; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4702 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4703 | if (ch != '(') { |
| 4704 | ch = i_getch(input); |
| 4705 | nommu_addchr(&ctx->as_string, ch); |
| 4706 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4707 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4708 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4709 | debug_printf_heredoc("calling parse_stream, heredoc_cnt:%d\n", heredoc_cnt); |
| 4710 | pipe_list = parse_stream(&as_string, &heredoc_cnt, input, endch); |
| 4711 | debug_printf_heredoc("parse_stream returned: heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4712 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4713 | if (as_string) |
| 4714 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4715 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4716 | |
| 4717 | /* empty ()/{} or parse error? */ |
| 4718 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4719 | /* parse_stream already emitted error msg */ |
| 4720 | if (!BB_MMU) |
| 4721 | free(as_string); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4722 | debug_printf_parse("parse_group return -1: " |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4723 | "parse_stream returned %p\n", pipe_list); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4724 | return -1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4725 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4726 | #if !BB_MMU |
| 4727 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4728 | command->group_as_string = as_string; |
| 4729 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4730 | command->group_as_string); |
| 4731 | #endif |
| 4732 | |
| 4733 | #if ENABLE_HUSH_FUNCTIONS |
| 4734 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4735 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4736 | struct command *cmd2; |
| 4737 | |
| 4738 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4739 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4740 | cmd2->group = pipe_list; |
| 4741 | # if !BB_MMU |
| 4742 | //UNTESTED! |
| 4743 | cmd2->group_as_string = command->group_as_string; |
| 4744 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4745 | # endif |
| 4746 | |
| 4747 | pipe_list = new_pipe(); |
| 4748 | pipe_list->cmds = cmd2; |
| 4749 | pipe_list->num_cmds = 1; |
| 4750 | } |
| 4751 | #endif |
| 4752 | |
| 4753 | command->group = pipe_list; |
| 4754 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 4755 | debug_printf_parse("parse_group return %d\n", heredoc_cnt); |
| 4756 | return heredoc_cnt; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4757 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4758 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4761 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4762 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4763 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4764 | 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] | 4765 | { |
| 4766 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4767 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4768 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4769 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4770 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4771 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4772 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4773 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4774 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4775 | } |
| 4776 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4777 | static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input) |
| 4778 | { |
| 4779 | while (1) { |
| 4780 | int ch = i_getch(input); |
| 4781 | if (ch == EOF) { |
| 4782 | syntax_error_unterm_ch('\''); |
| 4783 | return 0; |
| 4784 | } |
| 4785 | if (ch == '\'') |
| 4786 | return 1; |
| 4787 | o_addqchr(dest, ch); |
| 4788 | } |
| 4789 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4790 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 4791 | 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] | 4792 | 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] | 4793 | { |
| 4794 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4795 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4796 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4797 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4798 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4799 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4800 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4801 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4802 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4803 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4804 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4805 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4806 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4807 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4808 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4809 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4810 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4811 | continue; |
| 4812 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4813 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4814 | } |
| 4815 | } |
| 4816 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4817 | * \` quoting. |
| 4818 | * "Within the backquoted style of command substitution, backslash |
| 4819 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4820 | * The search for the matching backquote shall be satisfied by the first |
| 4821 | * backquote found without a preceding backslash; during this search, |
| 4822 | * if a non-escaped backquote is encountered within a shell comment, |
| 4823 | * a here-document, an embedded command substitution of the $(command) |
| 4824 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4825 | * or double-quoted string that begins, but does not end, within the |
| 4826 | * "`...`" sequence produces undefined results." |
| 4827 | * Example Output |
| 4828 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4829 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4830 | 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] | 4831 | { |
| 4832 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4833 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4834 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4835 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4836 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4837 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4838 | ch = i_getch(input); |
| 4839 | if (ch != '`' |
| 4840 | && ch != '$' |
| 4841 | && ch != '\\' |
| 4842 | && (!in_dquote || ch != '"') |
| 4843 | ) { |
| 4844 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4845 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4846 | } |
| 4847 | if (ch == EOF) { |
| 4848 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4849 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4850 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4851 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4852 | } |
| 4853 | } |
| 4854 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4855 | * quoting and nested ()s. |
| 4856 | * "With the $(command) style of command substitution, all characters |
| 4857 | * following the open parenthesis to the matching closing parenthesis |
| 4858 | * constitute the command. Any valid shell script can be used for command, |
| 4859 | * except a script consisting solely of redirections which produces |
| 4860 | * unspecified results." |
| 4861 | * Example Output |
| 4862 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4863 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4864 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4865 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4866 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4867 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4868 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4869 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4870 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4871 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4872 | 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] | 4873 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4874 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4875 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4876 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4877 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4878 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4879 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4880 | |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4881 | # if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4882 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4883 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4884 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4885 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4886 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4887 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4888 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4889 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4890 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4891 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4892 | if (ch == end_ch |
| 4893 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4894 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4895 | # endif |
| 4896 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4897 | if (!dbl) |
| 4898 | break; |
| 4899 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4900 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4901 | i_getch(input); /* eat second ')' */ |
| 4902 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4903 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4904 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4905 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4906 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4907 | if (ch == '(' || ch == '{') { |
| 4908 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4909 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4910 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4911 | o_addchr(dest, ch); |
| 4912 | continue; |
| 4913 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4914 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4915 | if (!add_till_single_quote(dest, input)) |
| 4916 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4917 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4918 | continue; |
| 4919 | } |
| 4920 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4921 | if (!add_till_double_quote(dest, input)) |
| 4922 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4923 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4924 | continue; |
| 4925 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4926 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4927 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4928 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4929 | o_addchr(dest, ch); |
| 4930 | continue; |
| 4931 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4932 | if (ch == '\\') { |
| 4933 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4934 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4935 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4936 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4937 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4938 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4939 | # if 0 |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4940 | if (ch == '\n') { |
| 4941 | /* "backslash+newline", ignore both */ |
| 4942 | o_delchr(dest); /* undo insertion of '\' */ |
| 4943 | continue; |
| 4944 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 4945 | # endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4946 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4947 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4948 | continue; |
| 4949 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4950 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4951 | 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] | 4952 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4953 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4954 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4955 | |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 4956 | #if BASH_DOLLAR_SQUOTE |
| 4957 | /* Return code: 1 for "found and parsed", 0 for "seen something else" */ |
Denys Vlasenko | 8dd676c | 2021-07-27 04:09:45 +0200 | [diff] [blame] | 4958 | # if BB_MMU |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 4959 | #define parse_dollar_squote(as_string, dest, input) \ |
| 4960 | parse_dollar_squote(dest, input) |
| 4961 | #define as_string NULL |
Denys Vlasenko | 8dd676c | 2021-07-27 04:09:45 +0200 | [diff] [blame] | 4962 | # endif |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 4963 | static int parse_dollar_squote(o_string *as_string, o_string *dest, struct in_str *input) |
| 4964 | { |
| 4965 | int start; |
| 4966 | int ch = i_peek_and_eat_bkslash_nl(input); /* first character after the $ */ |
| 4967 | debug_printf_parse("parse_dollar_squote entered: ch='%c'\n", ch); |
| 4968 | if (ch != '\'') |
| 4969 | return 0; |
| 4970 | |
| 4971 | dest->has_quoted_part = 1; |
| 4972 | start = dest->length; |
| 4973 | |
| 4974 | ch = i_getch(input); /* eat ' */ |
| 4975 | nommu_addchr(as_string, ch); |
| 4976 | while (1) { |
| 4977 | ch = i_getch(input); |
| 4978 | nommu_addchr(as_string, ch); |
| 4979 | if (ch == EOF) { |
| 4980 | syntax_error_unterm_ch('\''); |
| 4981 | return 0; |
| 4982 | } |
| 4983 | if (ch == '\'') |
| 4984 | break; |
| 4985 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 4986 | /* Convert raw ^C to corresponding special variable reference */ |
| 4987 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4988 | o_addchr(dest, SPECIAL_VAR_QUOTED_SVS); |
| 4989 | /* will addchr() another SPECIAL_VAR_SYMBOL (see after the if() block) */ |
| 4990 | } else if (ch == '\\') { |
| 4991 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 4992 | |
| 4993 | ch = i_getch(input); |
| 4994 | nommu_addchr(as_string, ch); |
| 4995 | if (strchr(C_escapes, ch)) { |
| 4996 | char buf[4]; |
| 4997 | char *p = buf; |
| 4998 | int cnt = 2; |
| 4999 | |
| 5000 | buf[0] = ch; |
| 5001 | if ((unsigned char)(ch - '0') <= 7) { /* \ooo */ |
| 5002 | do { |
| 5003 | ch = i_peek(input); |
| 5004 | if ((unsigned char)(ch - '0') > 7) |
| 5005 | break; |
| 5006 | *++p = ch = i_getch(input); |
| 5007 | nommu_addchr(as_string, ch); |
| 5008 | } while (--cnt != 0); |
| 5009 | } else if (ch == 'x') { /* \xHH */ |
| 5010 | do { |
| 5011 | ch = i_peek(input); |
| 5012 | if (!isxdigit(ch)) |
| 5013 | break; |
| 5014 | *++p = ch = i_getch(input); |
| 5015 | nommu_addchr(as_string, ch); |
| 5016 | } while (--cnt != 0); |
| 5017 | if (cnt == 2) { /* \x but next char is "bad" */ |
| 5018 | ch = 'x'; |
| 5019 | goto unrecognized; |
| 5020 | } |
| 5021 | } /* else simple seq like \\ or \t */ |
| 5022 | *++p = '\0'; |
| 5023 | p = buf; |
| 5024 | ch = bb_process_escape_sequence((void*)&p); |
| 5025 | //bb_error_msg("buf:'%s' ch:%x", buf, ch); |
| 5026 | if (ch == '\0') |
| 5027 | continue; /* bash compat: $'...\0...' emits nothing */ |
| 5028 | } else { /* unrecognized "\z": encode both chars unless ' or " */ |
| 5029 | if (ch != '\'' && ch != '"') { |
| 5030 | unrecognized: |
| 5031 | o_addqchr(dest, '\\'); |
| 5032 | } |
| 5033 | } |
| 5034 | } /* if (\...) */ |
| 5035 | o_addqchr(dest, ch); |
| 5036 | } |
| 5037 | |
| 5038 | if (dest->length == start) { |
| 5039 | /* $'', $'\0', $'\000\x00' and the like */ |
| 5040 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5041 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5042 | } |
| 5043 | |
| 5044 | return 1; |
Denys Vlasenko | 8dd676c | 2021-07-27 04:09:45 +0200 | [diff] [blame] | 5045 | # undef as_string |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 5046 | } |
| 5047 | #else |
Denys Vlasenko | 21afdde | 2021-08-15 20:08:53 +0200 | [diff] [blame] | 5048 | # define parse_dollar_squote(as_string, dest, input) 0 |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 5049 | #endif /* BASH_DOLLAR_SQUOTE */ |
| 5050 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5051 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5052 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 5053 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 5054 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5055 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5056 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5057 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5058 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 5059 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5060 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 5061 | 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] | 5062 | |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 5063 | debug_printf_parse("parse_dollar entered: ch='%c' quote_mask:0x%x\n", ch, quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5064 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5065 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5066 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5067 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5068 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5069 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5070 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 5071 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5072 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 5073 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 5074 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 5075 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 5076 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5077 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 5078 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5079 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5080 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5081 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5082 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5083 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5084 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5085 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5086 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5087 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 5088 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5089 | o_addchr(dest, ch | quote_mask); |
| 5090 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5091 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5092 | case '$': /* pid */ |
| 5093 | case '!': /* last bg pid */ |
| 5094 | case '?': /* last exit code */ |
| 5095 | case '#': /* number of args */ |
| 5096 | case '*': /* args */ |
| 5097 | case '@': /* args */ |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 5098 | case '-': /* $- option flags set by set builtin or shell options (-i etc) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5099 | goto make_one_char_var; |
| 5100 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5101 | char len_single_ch; |
| 5102 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 5103 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5104 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5105 | ch = i_getch(input); /* eat '{' */ |
| 5106 | nommu_addchr(as_string, ch); |
| 5107 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 5108 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5109 | /* It should be ${?}, or ${#var}, |
| 5110 | * or even ${?+subst} - operator acting on a special variable, |
| 5111 | * or the beginning of variable name. |
| 5112 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 5113 | if (ch == EOF |
| 5114 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 5115 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5116 | bad_dollar_syntax: |
| 5117 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5118 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 5119 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5120 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 5121 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5122 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5123 | ch |= quote_mask; |
| 5124 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5125 | /* 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] | 5126 | * However, this regresses some of our testsuite cases |
| 5127 | * which check invalid constructs like ${%}. |
| 5128 | * Oh well... let's check that the var name part is fine... */ |
| 5129 | |
Denys Vlasenko | 97c3b5e | 2021-06-19 15:28:10 +0200 | [diff] [blame] | 5130 | if (isdigit(len_single_ch) |
| 5131 | || (len_single_ch == '#' && isdigit(i_peek_and_eat_bkslash_nl(input))) |
| 5132 | ) { |
| 5133 | /* Execution engine uses plain xatoi_positive() |
| 5134 | * to interpret ${NNN} and {#NNN}, |
| 5135 | * check syntax here in the parser. |
| 5136 | * (bash does not support expressions in ${#NN}, |
| 5137 | * e.g. ${#$var} and {#1:+WORD} are not supported). |
| 5138 | */ |
| 5139 | unsigned cnt = 9; /* max 9 digits for ${NN} and 8 for {#NN} */ |
| 5140 | while (1) { |
| 5141 | o_addchr(dest, ch); |
| 5142 | debug_printf_parse(": '%c'\n", ch); |
| 5143 | ch = i_getch_and_eat_bkslash_nl(input); |
| 5144 | nommu_addchr(as_string, ch); |
| 5145 | if (ch == '}') |
| 5146 | break; |
| 5147 | if (--cnt == 0) |
| 5148 | goto bad_dollar_syntax; |
| 5149 | if (len_single_ch != '#' && strchr(VAR_SUBST_OPS, ch)) |
| 5150 | /* ${NN<op>...} is valid */ |
| 5151 | goto eat_until_closing; |
| 5152 | if (!isdigit(ch)) |
| 5153 | goto bad_dollar_syntax; |
| 5154 | } |
| 5155 | } else |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5156 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5157 | unsigned pos; |
| 5158 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5159 | o_addchr(dest, ch); |
| 5160 | debug_printf_parse(": '%c'\n", ch); |
| 5161 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5162 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5163 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5164 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5165 | break; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5166 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5167 | unsigned end_ch; |
| 5168 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5169 | /* handle parameter expansions |
| 5170 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 5171 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5172 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 5173 | if (len_single_ch != '#' |
| 5174 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 5175 | || i_peek(input) != '}' |
| 5176 | ) { |
| 5177 | goto bad_dollar_syntax; |
| 5178 | } |
| 5179 | /* else: it's "length of C" ${#C} op, |
| 5180 | * where C is a single char |
| 5181 | * special var name, e.g. ${#!}. |
| 5182 | */ |
| 5183 | } |
Denys Vlasenko | 97c3b5e | 2021-06-19 15:28:10 +0200 | [diff] [blame] | 5184 | eat_until_closing: |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5185 | /* Eat everything until closing '}' (or ':') */ |
| 5186 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5187 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5188 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5189 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5190 | ) { |
| 5191 | /* It's ${var:N[:M]} thing */ |
| 5192 | end_ch = '}' * 0x100 + ':'; |
| 5193 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5194 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5195 | && ch == '/' |
| 5196 | ) { |
| 5197 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 5198 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 5199 | i_getch(input); |
| 5200 | nommu_addchr(as_string, '/'); |
| 5201 | ch = '\\'; |
| 5202 | } |
| 5203 | end_ch = '}' * 0x100 + '/'; |
| 5204 | } |
| 5205 | o_addchr(dest, ch); |
Denys Vlasenko | c2aa218 | 2018-08-04 22:25:28 +0200 | [diff] [blame] | 5206 | /* The pattern can't be empty. |
| 5207 | * IOW: if the first char after "${v//" is a slash, |
| 5208 | * it does not terminate the pattern - it's the first char of the pattern: |
| 5209 | * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/") |
| 5210 | * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r") |
| 5211 | */ |
| 5212 | if (i_peek(input) == '/') { |
| 5213 | o_addchr(dest, i_getch(input)); |
| 5214 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5215 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5216 | if (!BB_MMU) |
| 5217 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5218 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5219 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5220 | if (last_ch == 0) /* error? */ |
| 5221 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5222 | #else |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 5223 | # error Simple code to only allow ${var} is not implemented |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 5224 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5225 | if (as_string) { |
| 5226 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5227 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5228 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5229 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5230 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 5231 | && (end_ch & 0xff00) |
| 5232 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5233 | /* close the first block: */ |
| 5234 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5235 | /* while parsing N from ${var:N[:M]} |
| 5236 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5237 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5238 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5239 | end_ch = '}'; |
| 5240 | goto again; |
| 5241 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5242 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5243 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 5244 | /* it's ${var:N} - emulate :999999999 */ |
| 5245 | o_addstr(dest, "999999999"); |
| 5246 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 5247 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5248 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5249 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5250 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 5251 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5252 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5253 | break; |
| 5254 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5255 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5256 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5257 | unsigned pos; |
| 5258 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5259 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5260 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5261 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 5262 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5263 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5264 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5265 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 5266 | o_addchr(dest, quote_mask | '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5267 | if (!BB_MMU) |
| 5268 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5269 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 5270 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5271 | if (as_string) { |
| 5272 | o_addstr(as_string, dest->data + pos); |
| 5273 | o_addchr(as_string, ')'); |
| 5274 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5275 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5276 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5277 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 5278 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5279 | # endif |
| 5280 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5281 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5282 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5283 | if (!BB_MMU) |
| 5284 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5285 | if (!add_till_closing_bracket(dest, input, ')')) |
| 5286 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5287 | if (as_string) { |
| 5288 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 5289 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 5290 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5291 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5292 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5293 | break; |
| 5294 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 5295 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5296 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5297 | goto make_var; |
| 5298 | #if 0 |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 5299 | /* TODO: $_: */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 5300 | /* $_ Shell or shell script name; or last argument of last command |
| 5301 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 5302 | * 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] | 5303 | ch = i_getch(input); |
| 5304 | nommu_addchr(as_string, ch); |
| 5305 | ch = i_peek_and_eat_bkslash_nl(input); |
| 5306 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 5307 | ch = '_'; |
| 5308 | goto make_var1; |
| 5309 | } |
| 5310 | /* else: it's $_ */ |
| 5311 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5312 | default: |
| 5313 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5314 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5315 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 5316 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5317 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5320 | #if BB_MMU |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5321 | #define encode_string(as_string, dest, input, dquote_end) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5322 | encode_string(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5323 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5324 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5325 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5326 | o_string *dest, |
| 5327 | struct in_str *input, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5328 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5329 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5330 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5331 | int next; |
| 5332 | |
| 5333 | again: |
| 5334 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5335 | if (ch != EOF) |
| 5336 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5337 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5338 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 5339 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5340 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5341 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5342 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5343 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5344 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5345 | } |
| 5346 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5347 | if (ch != '\n') { |
| 5348 | next = i_peek(input); |
| 5349 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5350 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5351 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5352 | if (ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5353 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 5354 | /* Testcase: in interactive shell a file with |
| 5355 | * echo "unterminated string\<eof> |
| 5356 | * is sourced. |
| 5357 | */ |
| 5358 | syntax_error_unterm_ch('"'); |
| 5359 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5360 | } |
| 5361 | /* bash: |
| 5362 | * "The backslash retains its special meaning [in "..."] |
| 5363 | * only when followed by one of the following characters: |
| 5364 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5365 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5366 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 5367 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5368 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5369 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 5370 | ch = i_getch(input); /* eat next */ |
| 5371 | if (ch == '\n') |
| 5372 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5373 | } /* else: ch remains == '\\', and we double it below: */ |
| 5374 | 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] | 5375 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5376 | goto again; |
| 5377 | } |
| 5378 | if (ch == '$') { |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 5379 | //if (parse_dollar_squote(as_string, dest, input)) |
| 5380 | // goto again; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5381 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 5382 | debug_printf_parse("encode_string return 0: " |
| 5383 | "parse_dollar returned 0 (error)\n"); |
| 5384 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5385 | } |
| 5386 | goto again; |
| 5387 | } |
| 5388 | #if ENABLE_HUSH_TICK |
| 5389 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5390 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5391 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5392 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5393 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 5394 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5395 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5396 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5397 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5398 | } |
| 5399 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 5400 | o_addQchr(dest, ch); |
Denys Vlasenko | 1b7a9b6 | 2021-06-15 16:05:57 +0200 | [diff] [blame] | 5401 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5402 | /* Convert "^C" to corresponding special variable reference */ |
| 5403 | o_addchr(dest, SPECIAL_VAR_QUOTED_SVS); |
| 5404 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 5405 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5406 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5407 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5410 | /* |
| 5411 | * Scan input until EOF or end_trigger char. |
| 5412 | * Return a list of pipes to execute, or NULL on EOF |
| 5413 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5414 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5415 | * reset parsing machinery and start parsing anew, |
| 5416 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5417 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5418 | static struct pipe *parse_stream(char **pstring, |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5419 | int *heredoc_cnt_ptr, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5420 | struct in_str *input, |
| 5421 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5422 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5423 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5424 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5425 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5426 | /* 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] | 5427 | * 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] | 5428 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5429 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5430 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5431 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5432 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5433 | initialize_context(&ctx); |
| 5434 | |
| 5435 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5436 | * Preventing this: |
| 5437 | */ |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5438 | ctx.word.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5439 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5440 | /* We used to separate words on $IFS here. This was wrong. |
| 5441 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5442 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5443 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5444 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5445 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5446 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5447 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5448 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5449 | int ch; |
| 5450 | int next; |
| 5451 | int redir_fd; |
| 5452 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5453 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5454 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5455 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5456 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5457 | if (ch == EOF) { |
| 5458 | struct pipe *pi; |
Denys Vlasenko | 18bcaf3 | 2020-12-23 23:01:18 +0100 | [diff] [blame] | 5459 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5460 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5461 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5462 | goto parse_error_exitcode1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5463 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5464 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5465 | syntax_error_unterm_ch('('); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5466 | goto parse_error_exitcode1; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5467 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5468 | if (end_trigger == '}') { |
| 5469 | syntax_error_unterm_ch('{'); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5470 | goto parse_error_exitcode1; |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5471 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5472 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5473 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5474 | goto parse_error_exitcode1; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5475 | } |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5476 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5477 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5478 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5479 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5480 | /* (this makes bare "&" cmd a no-op. |
| 5481 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5482 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5483 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5484 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5485 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5486 | pi = NULL; |
| 5487 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5488 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5489 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5490 | if (pstring) |
| 5491 | *pstring = ctx.as_string.data; |
| 5492 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5493 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5494 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5495 | // heredoc_cnt must be 0 here anyway |
| 5496 | //if (heredoc_cnt_ptr) |
| 5497 | // *heredoc_cnt_ptr = heredoc_cnt; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5498 | debug_leave(); |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5499 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5500 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5501 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5502 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5503 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5504 | /* Handle "'" and "\" first, as they won't play nice with |
| 5505 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5506 | * echo z\\ |
| 5507 | * and |
| 5508 | * echo '\ |
| 5509 | * ' |
| 5510 | * would break. |
| 5511 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5512 | if (ch == '\\') { |
| 5513 | ch = i_getch(input); |
| 5514 | if (ch == '\n') |
| 5515 | continue; /* drop \<newline>, get next char */ |
| 5516 | nommu_addchr(&ctx.as_string, '\\'); |
Denys Vlasenko | 1b7a9b6 | 2021-06-15 16:05:57 +0200 | [diff] [blame] | 5517 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5518 | nommu_addchr(&ctx.as_string, ch); |
| 5519 | /* Convert \^C to corresponding special variable reference */ |
| 5520 | goto case_SPECIAL_VAR_SYMBOL; |
| 5521 | } |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5522 | o_addchr(&ctx.word, '\\'); |
| 5523 | if (ch == EOF) { |
| 5524 | /* Testcase: eval 'echo Ok\' */ |
| 5525 | /* bash-4.3.43 was removing backslash, |
| 5526 | * but 4.4.19 retains it, most other shells too |
| 5527 | */ |
| 5528 | continue; /* get next char */ |
| 5529 | } |
| 5530 | /* Example: echo Hello \2>file |
| 5531 | * we need to know that word 2 is quoted |
| 5532 | */ |
| 5533 | ctx.word.has_quoted_part = 1; |
| 5534 | nommu_addchr(&ctx.as_string, ch); |
| 5535 | o_addchr(&ctx.word, ch); |
| 5536 | continue; /* get next char */ |
| 5537 | } |
| 5538 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5539 | if (ch == '\'') { |
| 5540 | ctx.word.has_quoted_part = 1; |
| 5541 | next = i_getch(input); |
| 5542 | if (next == '\'' && !ctx.pending_redirect) |
| 5543 | goto insert_empty_quoted_str_marker; |
| 5544 | |
| 5545 | ch = next; |
| 5546 | while (1) { |
| 5547 | if (ch == EOF) { |
| 5548 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5549 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5550 | } |
| 5551 | nommu_addchr(&ctx.as_string, ch); |
| 5552 | if (ch == '\'') |
| 5553 | break; |
| 5554 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5555 | /* Convert raw ^C to corresponding special variable reference */ |
| 5556 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5557 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5558 | } |
| 5559 | o_addqchr(&ctx.word, ch); |
| 5560 | ch = i_getch(input); |
| 5561 | } |
| 5562 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5563 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5564 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5565 | next = '\0'; |
| 5566 | if (ch != '\n') |
| 5567 | next = i_peek_and_eat_bkslash_nl(input); |
| 5568 | |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 5569 | is_special = "{}<>&|();#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5570 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5571 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 5572 | #if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 5573 | if (ctx.command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB) { |
| 5574 | /* In [[ ]], {}<>&|() are not special */ |
| 5575 | is_special += 8; |
| 5576 | } else |
| 5577 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5578 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5579 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5580 | || ctx.word.length /* word{... - non-special */ |
| 5581 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5582 | || (next != ';' /* }; - special */ |
| 5583 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5584 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5585 | && next != '&' /* }& and }&& ... - special */ |
| 5586 | && next != '|' /* }|| ... - special */ |
| 5587 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5588 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5589 | ) { |
| 5590 | /* They are not special, skip "{}" */ |
| 5591 | is_special += 2; |
| 5592 | } |
| 5593 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5594 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5595 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5596 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5597 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5598 | o_addQchr(&ctx.word, ch); |
| 5599 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5600 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5601 | && ch == '=' |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 5602 | && endofname(ctx.word.data)[0] == '=' |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5603 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5604 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5605 | 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] | 5606 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5607 | continue; |
| 5608 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5609 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5610 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5611 | #if ENABLE_HUSH_LINENO_VAR |
| 5612 | /* Case: |
| 5613 | * "while ...; do<whitespace><newline> |
| 5614 | * cmd ..." |
| 5615 | * would think that "cmd" starts in <whitespace> - |
| 5616 | * i.e., at the previous line. |
| 5617 | * We need to skip all whitespace before newlines. |
| 5618 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5619 | while (ch != '\n') { |
| 5620 | next = i_peek(input); |
| 5621 | if (next != ' ' && next != '\t' && next != '\n') |
| 5622 | break; /* next char is not ws */ |
| 5623 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5624 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5625 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5626 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5627 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5628 | goto parse_error_exitcode1; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5629 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5630 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5631 | /* Is this a case when newline is simply ignored? |
| 5632 | * Some examples: |
| 5633 | * "cmd | <newline> cmd ..." |
| 5634 | * "case ... in <newline> word) ..." |
| 5635 | */ |
| 5636 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5637 | && ctx.word.length == 0 |
| 5638 | && !ctx.word.has_quoted_part |
| 5639 | && heredoc_cnt == 0 |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5640 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5641 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5642 | * Without check #1, interactive shell |
| 5643 | * ignores even bare <newline>, |
| 5644 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5645 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5646 | * ps2> _ <=== wrong, should be ps1 |
| 5647 | * Without check #2, "cmd & <newline>" |
| 5648 | * is similarly mistreated. |
| 5649 | * (BTW, this makes "cmd & cmd" |
| 5650 | * and "cmd && cmd" non-orthogonal. |
| 5651 | * Really, ask yourself, why |
| 5652 | * "cmd && <newline>" doesn't start |
| 5653 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5654 | * The only reason is that it might be |
| 5655 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5656 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5657 | */ |
| 5658 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5659 | if (pi->num_cmds != 0 /* check #1 */ |
| 5660 | && pi->followup != PIPE_BG /* check #2 */ |
| 5661 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5662 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5663 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5664 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5665 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5666 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5667 | debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5668 | if (heredoc_cnt) { |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5669 | heredoc_cnt = fetch_heredocs(&ctx.as_string, ctx.list_head, heredoc_cnt, input); |
| 5670 | if (heredoc_cnt != 0) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5671 | goto parse_error_exitcode1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5672 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5673 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5674 | 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] | 5675 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5676 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5677 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5678 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5679 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5680 | |
| 5681 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5682 | * } is an ordinary char in this case, even inside { cmd; } |
| 5683 | * Pathological example: { ""}; } should exec "}" cmd |
| 5684 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5685 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5686 | if (ctx.word.length != 0 /* word} */ |
| 5687 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5688 | ) { |
| 5689 | goto ordinary_char; |
| 5690 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5691 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5692 | /* Generally, there should be semicolon: "cmd; }" |
| 5693 | * However, bash allows to omit it if "cmd" is |
| 5694 | * a group. Examples: |
| 5695 | * { { echo 1; } } |
| 5696 | * {(echo 1)} |
| 5697 | * { echo 0 >&2 | { echo 1; } } |
| 5698 | * { while false; do :; done } |
| 5699 | * { case a in b) ;; esac } |
| 5700 | */ |
| 5701 | if (ctx.command->group) |
| 5702 | goto term_group; |
| 5703 | goto ordinary_char; |
| 5704 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5705 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5706 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5707 | goto skip_end_trigger; |
| 5708 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5709 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5710 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5711 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5712 | && (ch != ';' || heredoc_cnt == 0) |
| 5713 | #if ENABLE_HUSH_CASE |
| 5714 | && (ch != ')' |
| 5715 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5716 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5717 | ) |
| 5718 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5719 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5720 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5721 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5722 | } |
| 5723 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5724 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5725 | 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] | 5726 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5727 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5728 | 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] | 5729 | ) { |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5730 | o_free_and_set_NULL(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5731 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5732 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5733 | if (pstring) |
| 5734 | *pstring = ctx.as_string.data; |
| 5735 | else |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 5736 | o_free(&ctx.as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5737 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5738 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5739 | /* Example: bare "{ }", "()" */ |
| 5740 | G.last_exitcode = 2; /* bash compat */ |
| 5741 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5742 | goto parse_error; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5743 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5744 | if (heredoc_cnt_ptr) |
| 5745 | *heredoc_cnt_ptr = heredoc_cnt; |
| 5746 | debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5747 | debug_printf_parse("parse_stream return %p: " |
| 5748 | "end_trigger char found\n", |
| 5749 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5750 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5751 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5752 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5753 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5754 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5755 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5756 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5757 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5758 | /* Catch <, > before deciding whether this word is |
| 5759 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5760 | switch (ch) { |
| 5761 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5762 | redir_fd = redirect_opt_num(&ctx.word); |
| 5763 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5764 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5765 | } |
| 5766 | redir_style = REDIRECT_OVERWRITE; |
| 5767 | if (next == '>') { |
| 5768 | redir_style = REDIRECT_APPEND; |
| 5769 | ch = i_getch(input); |
| 5770 | nommu_addchr(&ctx.as_string, ch); |
| 5771 | } |
| 5772 | #if 0 |
| 5773 | else if (next == '(') { |
| 5774 | syntax_error(">(process) not supported"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5775 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5776 | } |
| 5777 | #endif |
| 5778 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5779 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5780 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5781 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5782 | redir_fd = redirect_opt_num(&ctx.word); |
| 5783 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5784 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5785 | } |
| 5786 | redir_style = REDIRECT_INPUT; |
| 5787 | if (next == '<') { |
| 5788 | redir_style = REDIRECT_HEREDOC; |
| 5789 | heredoc_cnt++; |
Denys Vlasenko | 3675c37 | 2018-07-23 16:31:21 +0200 | [diff] [blame] | 5790 | debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5791 | ch = i_getch(input); |
| 5792 | nommu_addchr(&ctx.as_string, ch); |
| 5793 | } else if (next == '>') { |
| 5794 | redir_style = REDIRECT_IO; |
| 5795 | ch = i_getch(input); |
| 5796 | nommu_addchr(&ctx.as_string, ch); |
| 5797 | } |
| 5798 | #if 0 |
| 5799 | else if (next == '(') { |
| 5800 | syntax_error("<(process) not supported"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5801 | goto parse_error_exitcode1; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5802 | } |
| 5803 | #endif |
| 5804 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5805 | goto parse_error_exitcode1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5806 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5807 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5808 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5809 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5810 | /* note: we do not add it to &ctx.as_string */ |
| 5811 | /* TODO: in bash: |
| 5812 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5813 | * cmd "$(cmd2 #comment)" - syntax error |
| 5814 | * cmd "`cmd2 #comment`" - ok |
| 5815 | * We accept both (comment ends where command subst ends, in both cases). |
| 5816 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5817 | while (1) { |
| 5818 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5819 | if (ch == '\n') { |
| 5820 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5821 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5822 | } |
| 5823 | ch = i_getch(input); |
| 5824 | if (ch == EOF) |
| 5825 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5826 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5827 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5828 | } |
| 5829 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5830 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5831 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5832 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5833 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5834 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5835 | && !ctx.pending_redirect |
| 5836 | ) { |
| 5837 | /* ch is a special char and thus this word |
| 5838 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5839 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5840 | 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] | 5841 | } |
| 5842 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5843 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5844 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5845 | switch (ch) { |
Denys Vlasenko | 1b7a9b6 | 2021-06-15 16:05:57 +0200 | [diff] [blame] | 5846 | case_SPECIAL_VAR_SYMBOL: |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5847 | case SPECIAL_VAR_SYMBOL: |
| 5848 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5849 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5850 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5851 | /* fall through */ |
| 5852 | case '#': |
| 5853 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5854 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5855 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5856 | case '$': |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 5857 | if (parse_dollar_squote(&ctx.as_string, &ctx.word, input)) |
| 5858 | continue; /* get next char */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5859 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5860 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5861 | "parse_dollar returned 0 (error)\n"); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5862 | goto parse_error_exitcode1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5863 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5864 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5865 | case '"': |
| 5866 | ctx.word.has_quoted_part = 1; |
| 5867 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5868 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5869 | insert_empty_quoted_str_marker: |
| 5870 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5871 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5872 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5873 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5874 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5875 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5876 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5877 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"')) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5878 | goto parse_error_exitcode1; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5879 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5880 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5881 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5882 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5883 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5884 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5885 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5886 | o_addchr(&ctx.word, '`'); |
| 5887 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5888 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5889 | goto parse_error_exitcode1; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5890 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5891 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5892 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5893 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5894 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5895 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5896 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5897 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5898 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5899 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5900 | #if ENABLE_HUSH_CASE |
| 5901 | case_semi: |
| 5902 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5903 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5904 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5905 | } |
| 5906 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5907 | #if ENABLE_HUSH_CASE |
| 5908 | /* Eat multiple semicolons, detect |
| 5909 | * whether it means something special */ |
| 5910 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5911 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5912 | if (ch != ';') |
| 5913 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5914 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5915 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5916 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5917 | ctx.ctx_dsemicolon = 1; |
| 5918 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5919 | break; |
| 5920 | } |
| 5921 | } |
| 5922 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5923 | new_cmd: |
| 5924 | /* We just finished a cmd. New one may start |
| 5925 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5926 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5927 | 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] | 5928 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5929 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5930 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5931 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5932 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5933 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5934 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5935 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5936 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5937 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5938 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5939 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5940 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5941 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5942 | if (done_word(&ctx)) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5943 | goto parse_error_exitcode1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5944 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5945 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5946 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5947 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5948 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5949 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5950 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5951 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5952 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5953 | } else { |
| 5954 | /* we could pick up a file descriptor choice here |
| 5955 | * with redirect_opt_num(), but bash doesn't do it. |
| 5956 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5957 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5958 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5959 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5960 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5961 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5962 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5963 | if (ctx.ctx_res_w == RES_MATCH |
| 5964 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5965 | && ctx.word.length == 0 /* not word(... */ |
| 5966 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5967 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5968 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5969 | } |
| 5970 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5971 | /* fall through */ |
| 5972 | case '{': { |
| 5973 | int n = parse_group(&ctx, input, ch); |
| 5974 | if (n < 0) { |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5975 | goto parse_error_exitcode1; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5976 | } |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5977 | debug_printf_heredoc("parse_group done, needs heredocs:%d\n", n); |
| 5978 | heredoc_cnt += n; |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5979 | goto new_cmd; |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 5980 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5981 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5982 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5983 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5984 | goto case_semi; |
| 5985 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5986 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5987 | /* proper use of this character is caught by end_trigger: |
| 5988 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5989 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5990 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5991 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5992 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5993 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5994 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5995 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5996 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5997 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5998 | |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 5999 | parse_error_exitcode1: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 6000 | G.last_exitcode = 1; |
Denys Vlasenko | dc9c10a | 2020-11-16 13:00:44 +0100 | [diff] [blame] | 6001 | parse_error: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6002 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6003 | struct parse_context *pctx; |
| 6004 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6005 | |
| 6006 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6007 | * Sample for finding leaks on syntax error recovery path. |
| 6008 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6009 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6010 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 6011 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 6012 | * 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] | 6013 | */ |
| 6014 | pctx = &ctx; |
| 6015 | do { |
| 6016 | /* Update pipe/command counts, |
| 6017 | * otherwise freeing may miss some */ |
| 6018 | done_pipe(pctx, PIPE_SEQ); |
| 6019 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6020 | pctx->list_head, pctx); |
| 6021 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6022 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6023 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6024 | #if !BB_MMU |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6025 | o_free(&pctx->as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6026 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6027 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6028 | if (pctx != &ctx) { |
| 6029 | free(pctx); |
| 6030 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6031 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6032 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6033 | |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 6034 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6035 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6036 | if (pstring) |
| 6037 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6038 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6039 | debug_leave(); |
| 6040 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6041 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6042 | } |
| 6043 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6044 | |
| 6045 | /*** Execution routines ***/ |
| 6046 | |
| 6047 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6048 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6049 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6050 | expand_string_to_string(str) |
| 6051 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6052 | 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] | 6053 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6054 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 6055 | #endif |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6056 | 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] | 6057 | |
| 6058 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 6059 | * all variable references within and returns a pointer to |
| 6060 | * a list of expanded strings, possibly with larger number |
| 6061 | * of strings. (Think VAR="a b"; echo $VAR). |
| 6062 | * This new list is allocated as a single malloc block. |
| 6063 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6064 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6065 | * Caller can deallocate entire list by single free(list). */ |
| 6066 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 6067 | /* A horde of its helpers come first: */ |
| 6068 | |
| 6069 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 6070 | { |
| 6071 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 6072 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 6073 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 6074 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 6075 | if (c == '{' || c == '}') { |
| 6076 | /* { -> \{, } -> \} */ |
| 6077 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 6078 | /* And now we want to add { or } and continue: |
| 6079 | * o_addchr(o, c); |
| 6080 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6081 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 6082 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 6083 | } |
| 6084 | #endif |
| 6085 | o_addchr(o, c); |
| 6086 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 6087 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 6088 | o_addchr(o, '\\'); |
| 6089 | if (len) { |
| 6090 | len--; |
| 6091 | o_addchr(o, '\\'); |
| 6092 | o_addchr(o, *str++); |
| 6093 | } |
| 6094 | } |
| 6095 | } |
| 6096 | } |
| 6097 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6098 | /* Store given string, finalizing the word and starting new one whenever |
| 6099 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6100 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6101 | * Return in output->ended_in_ifs: |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6102 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 6103 | */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6104 | 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] | 6105 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6106 | int last_is_ifs = 0; |
| 6107 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6108 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6109 | int word_len; |
| 6110 | |
| 6111 | if (!*str) /* EOL - do not finalize word */ |
| 6112 | break; |
| 6113 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6114 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6115 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 6116 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6117 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 6118 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6119 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 6120 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 6121 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6122 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6123 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6124 | /*/ Why can't we do it easier? */ |
| 6125 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 6126 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 6127 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6128 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6129 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6130 | if (!*str) /* EOL - do not finalize word */ |
| 6131 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6132 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6133 | |
| 6134 | /* We know str here points to at least one IFS char */ |
| 6135 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 6136 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6137 | if (!*str) /* EOL - do not finalize word */ |
| 6138 | break; |
| 6139 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 6140 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 6141 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 6142 | ) { |
| 6143 | /* This is a non-whitespace $IFS char */ |
| 6144 | /* Skip it and IFS whitespace chars, start new word */ |
| 6145 | str++; |
| 6146 | str += strspn(str, G.ifs_whitespace); |
| 6147 | goto new_word; |
| 6148 | } |
| 6149 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6150 | /* Start new word... but not always! */ |
| 6151 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6152 | if (output->has_quoted_part |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 6153 | /* |
| 6154 | * Case "v=' a'; echo $v": |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6155 | * here nothing precedes the space in $v expansion, |
| 6156 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6157 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 6158 | * It's okay if this accesses the byte before first argv[]: |
| 6159 | * past call to o_save_ptr() cleared it to zero byte |
| 6160 | * (grep for -prev-ifs-check-). |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6161 | */ |
Denys Vlasenko | 186cf49 | 2018-07-27 12:14:39 +0200 | [diff] [blame] | 6162 | || output->data[output->length - 1] |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6163 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 6164 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6165 | o_addchr(output, '\0'); |
| 6166 | debug_print_list("expand_on_ifs", output, n); |
| 6167 | n = o_save_ptr(output, n); |
| 6168 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6169 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6170 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6171 | output->ended_in_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6172 | debug_print_list("expand_on_ifs[1]", output, n); |
| 6173 | return n; |
| 6174 | } |
| 6175 | |
| 6176 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 6177 | * they are in double quotes, with the exception that they are not :). |
| 6178 | * Just the rules are similar: "expand only $var and `cmd`" |
| 6179 | * |
| 6180 | * Returns malloced string. |
| 6181 | * As an optimization, we return NULL if expansion is not needed. |
| 6182 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6183 | static char *encode_then_expand_string(const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6184 | { |
| 6185 | char *exp_str; |
| 6186 | struct in_str input; |
| 6187 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6188 | const char *cp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6189 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6190 | cp = str; |
| 6191 | for (;;) { |
| 6192 | if (!*cp) return NULL; /* string has no special chars */ |
| 6193 | if (*cp == '$') break; |
| 6194 | if (*cp == '\\') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6195 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6196 | if (*cp == '`') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6197 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 6198 | cp++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | /* We need to expand. Example: |
| 6202 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 6203 | */ |
| 6204 | setup_string_in_str(&input, str); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6205 | encode_string(NULL, &dest, &input, EOF); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 6206 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6207 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6208 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6209 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6210 | /*unbackslash:*/ 1 |
| 6211 | ); |
| 6212 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6213 | o_free(&dest); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6214 | return exp_str; |
| 6215 | } |
| 6216 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6217 | static const char *first_special_char_in_vararg(const char *cp) |
| 6218 | { |
| 6219 | for (;;) { |
| 6220 | if (!*cp) return NULL; /* string has no special chars */ |
| 6221 | if (*cp == '$') return cp; |
| 6222 | if (*cp == '\\') return cp; |
| 6223 | if (*cp == '\'') return cp; |
| 6224 | if (*cp == '"') return cp; |
| 6225 | #if ENABLE_HUSH_TICK |
| 6226 | if (*cp == '`') return cp; |
| 6227 | #endif |
| 6228 | /* dquoted "${x:+ARG}" should not glob, therefore |
| 6229 | * '*' et al require some non-literal processing: */ |
| 6230 | if (*cp == '*') return cp; |
| 6231 | if (*cp == '?') return cp; |
| 6232 | if (*cp == '[') return cp; |
| 6233 | cp++; |
| 6234 | } |
| 6235 | } |
| 6236 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6237 | /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}. |
| 6238 | * These can contain single- and double-quoted strings, |
| 6239 | * and treated as if the ARG string is initially unquoted. IOW: |
| 6240 | * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be |
| 6241 | * a dquoted string: "${var#"zz"}"), the difference only comes later |
| 6242 | * (word splitting and globbing of the ${var...} result). |
| 6243 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6244 | #if !BASH_PATTERN_SUBST |
| 6245 | #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ |
| 6246 | encode_then_expand_vararg(str, handle_squotes) |
| 6247 | #endif |
| 6248 | static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash) |
| 6249 | { |
Denys Vlasenko | 3d27d43 | 2018-12-27 18:03:20 +0100 | [diff] [blame] | 6250 | #if !BASH_PATTERN_SUBST && ENABLE_HUSH_CASE |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6251 | const int do_unbackslash = 0; |
| 6252 | #endif |
| 6253 | char *exp_str; |
| 6254 | struct in_str input; |
| 6255 | o_string dest = NULL_O_STRING; |
| 6256 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6257 | if (!first_special_char_in_vararg(str)) { |
| 6258 | /* string has no special chars */ |
| 6259 | return NULL; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6260 | } |
| 6261 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6262 | setup_string_in_str(&input, str); |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 6263 | dest.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6264 | exp_str = NULL; |
| 6265 | |
| 6266 | for (;;) { |
| 6267 | int ch; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6268 | |
| 6269 | ch = i_getch(&input); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6270 | debug_printf_parse("%s: ch=%c (%d) escape=%d\n", |
| 6271 | __func__, ch, ch, !!dest.o_expflags); |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6272 | |
| 6273 | if (!dest.o_expflags) { |
| 6274 | if (ch == EOF) |
| 6275 | break; |
| 6276 | if (handle_squotes && ch == '\'') { |
| 6277 | if (!add_till_single_quote_dquoted(&dest, &input)) |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6278 | goto ret; /* error */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6279 | continue; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6280 | } |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6281 | } |
| 6282 | if (ch == EOF) { |
| 6283 | syntax_error_unterm_ch('"'); |
| 6284 | goto ret; /* error */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6285 | } |
| 6286 | if (ch == '"') { |
| 6287 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
| 6288 | continue; |
| 6289 | } |
| 6290 | if (ch == '\\') { |
| 6291 | ch = i_getch(&input); |
| 6292 | if (ch == EOF) { |
| 6293 | //example? error message? syntax_error_unterm_ch('"'); |
| 6294 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6295 | goto ret; |
| 6296 | } |
| 6297 | o_addqchr(&dest, ch); |
| 6298 | continue; |
| 6299 | } |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6300 | if (ch == '$') { |
Denys Vlasenko | b278d82 | 2021-07-26 15:29:13 +0200 | [diff] [blame] | 6301 | if (parse_dollar_squote(NULL, &dest, &input)) |
| 6302 | continue; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6303 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) { |
| 6304 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6305 | goto ret; |
| 6306 | } |
| 6307 | continue; |
| 6308 | } |
| 6309 | #if ENABLE_HUSH_TICK |
| 6310 | if (ch == '`') { |
| 6311 | //unsigned pos = dest->length; |
| 6312 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6313 | o_addchr(&dest, 0x80 | '`'); |
| 6314 | if (!add_till_backquote(&dest, &input, |
| 6315 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6316 | ) |
| 6317 | ) { |
| 6318 | goto ret; /* error */ |
| 6319 | } |
| 6320 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6321 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6322 | continue; |
| 6323 | } |
| 6324 | #endif |
| 6325 | o_addQchr(&dest, ch); |
| 6326 | } /* for (;;) */ |
| 6327 | |
| 6328 | debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data); |
| 6329 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6330 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 6331 | do_unbackslash |
| 6332 | ); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6333 | ret: |
| 6334 | debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6335 | o_free(&dest); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6336 | return exp_str; |
| 6337 | } |
| 6338 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6339 | /* Expanding ARG in ${var+ARG}, ${var-ARG} |
| 6340 | */ |
Denys Vlasenko | 53b2fdc | 2021-10-10 13:50:53 +0200 | [diff] [blame] | 6341 | static NOINLINE int encode_then_append_var_plusminus(o_string *output, int n, |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6342 | char *str, int dquoted) |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6343 | { |
| 6344 | struct in_str input; |
| 6345 | o_string dest = NULL_O_STRING; |
| 6346 | |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6347 | if (!first_special_char_in_vararg(str) |
| 6348 | && '\0' == str[strcspn(str, G.ifs)] |
| 6349 | ) { |
| 6350 | /* string has no special chars |
| 6351 | * && string has no $IFS chars |
| 6352 | */ |
Denys Vlasenko | 9e0adb9 | 2019-05-15 13:39:19 +0200 | [diff] [blame] | 6353 | if (dquoted) { |
| 6354 | /* Prints 1 (quoted expansion is a "" word, not nothing): |
| 6355 | * set -- "${notexist-}"; echo $# |
| 6356 | */ |
| 6357 | output->has_quoted_part = 1; |
| 6358 | } |
Denys Vlasenko | 54fdabd | 2018-07-31 10:36:29 +0200 | [diff] [blame] | 6359 | return expand_vars_to_list(output, n, str); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6360 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6361 | |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6362 | setup_string_in_str(&input, str); |
| 6363 | |
| 6364 | for (;;) { |
| 6365 | int ch; |
| 6366 | |
| 6367 | ch = i_getch(&input); |
| 6368 | debug_printf_parse("%s: ch=%c (%d) escape=%x\n", |
| 6369 | __func__, ch, ch, dest.o_expflags); |
| 6370 | |
| 6371 | if (!dest.o_expflags) { |
| 6372 | if (ch == EOF) |
| 6373 | break; |
| 6374 | if (!dquoted && strchr(G.ifs, ch)) { |
| 6375 | /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word. |
| 6376 | * do not assume we are at the start of the word (PREFIX above). |
| 6377 | */ |
| 6378 | if (dest.data) { |
| 6379 | n = expand_vars_to_list(output, n, dest.data); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6380 | o_free_and_set_NULL(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6381 | o_addchr(output, '\0'); |
| 6382 | n = o_save_ptr(output, n); /* create next word */ |
| 6383 | } else |
| 6384 | if (output->length != o_get_last_ptr(output, n) |
| 6385 | || output->has_quoted_part |
| 6386 | ) { |
| 6387 | /* For these cases: |
| 6388 | * f() { for i; do echo "|$i|"; done; }; x=x |
| 6389 | * f a${x:+ }b # 1st condition |
| 6390 | * |a| |
| 6391 | * |b| |
| 6392 | * f ""${x:+ }b # 2nd condition |
| 6393 | * || |
| 6394 | * |b| |
| 6395 | */ |
| 6396 | o_addchr(output, '\0'); |
| 6397 | n = o_save_ptr(output, n); /* create next word */ |
| 6398 | } |
| 6399 | continue; |
| 6400 | } |
| 6401 | if (!dquoted && ch == '\'') { |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6402 | if (!add_till_single_quote_dquoted(&dest, &input)) |
| 6403 | goto ret; /* error */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6404 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6405 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6406 | continue; |
| 6407 | } |
| 6408 | } |
| 6409 | if (ch == EOF) { |
| 6410 | syntax_error_unterm_ch('"'); |
| 6411 | goto ret; /* error */ |
| 6412 | } |
| 6413 | if (ch == '"') { |
| 6414 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 6415 | if (dest.o_expflags) { |
| 6416 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6417 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6418 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6419 | continue; |
| 6420 | } |
| 6421 | if (ch == '\\') { |
| 6422 | ch = i_getch(&input); |
| 6423 | if (ch == EOF) { |
| 6424 | //example? error message? syntax_error_unterm_ch('"'); |
| 6425 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 6426 | goto ret; |
| 6427 | } |
| 6428 | o_addqchr(&dest, ch); |
| 6429 | continue; |
| 6430 | } |
| 6431 | if (ch == '$') { |
| 6432 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) { |
| 6433 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 6434 | goto ret; |
| 6435 | } |
| 6436 | continue; |
| 6437 | } |
| 6438 | #if ENABLE_HUSH_TICK |
| 6439 | if (ch == '`') { |
| 6440 | //unsigned pos = dest->length; |
| 6441 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6442 | o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`'); |
| 6443 | if (!add_till_backquote(&dest, &input, |
| 6444 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 6445 | ) |
| 6446 | ) { |
| 6447 | goto ret; /* error */ |
| 6448 | } |
| 6449 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6450 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 6451 | continue; |
| 6452 | } |
| 6453 | #endif |
Denys Vlasenko | f36caa4 | 2018-07-20 19:29:41 +0200 | [diff] [blame] | 6454 | if (dquoted) { |
| 6455 | /* Always glob-protect if in dquotes: |
| 6456 | * x=x; echo "${x:+/bin/c*}" - prints: /bin/c* |
| 6457 | * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c* |
| 6458 | */ |
| 6459 | o_addqchr(&dest, ch); |
| 6460 | } else { |
| 6461 | /* Glob-protect only if char is quoted: |
| 6462 | * x=x; echo ${x:+/bin/c*} - prints many filenames |
| 6463 | * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c* |
| 6464 | */ |
| 6465 | o_addQchr(&dest, ch); |
| 6466 | } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6467 | } /* for (;;) */ |
| 6468 | |
| 6469 | if (dest.data) { |
| 6470 | n = expand_vars_to_list(output, n, dest.data); |
| 6471 | } |
| 6472 | ret: |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 6473 | o_free(&dest); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6474 | return n; |
| 6475 | } |
| 6476 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6477 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6478 | 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] | 6479 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6480 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6481 | arith_t res; |
| 6482 | char *exp_str; |
| 6483 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6484 | math_state.lookupvar = get_local_var_value; |
| 6485 | math_state.setvar = set_local_var_from_halves; |
| 6486 | //math_state.endofname = endofname; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6487 | exp_str = encode_then_expand_string(arg); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 6488 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6489 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6490 | if (errmsg_p) |
| 6491 | *errmsg_p = math_state.errmsg; |
| 6492 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6493 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6494 | return res; |
| 6495 | } |
| 6496 | #endif |
| 6497 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6498 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6499 | /* ${var/[/]pattern[/repl]} helpers */ |
| 6500 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 6501 | { |
Denys Vlasenko | 49bcf9f | 2021-10-09 03:52:04 +0200 | [diff] [blame] | 6502 | int first_escaped = (pattern[0] == '\\' && pattern[1]); |
| 6503 | /* "first_escaped" trick allows to treat e.g. "\*no_glob_chars" |
| 6504 | * as literal too (as it is semi-common, and easy to accomodate |
| 6505 | * by just using str + 1). |
| 6506 | */ |
| 6507 | int sz = strcspn(pattern + first_escaped * 2, "*?[\\"); |
| 6508 | if ((pattern + first_escaped * 2)[sz] == '\0') { |
Denys Vlasenko | 49cc3ca | 2021-07-27 17:53:55 +0200 | [diff] [blame] | 6509 | /* Optimization for trivial patterns. |
| 6510 | * Testcase for very slow replace (performs about 22k replaces): |
| 6511 | * x=:::::::::::::::::::::: |
| 6512 | * x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;echo ${#x} |
| 6513 | * echo "${x//:/|}" |
| 6514 | */ |
Denys Vlasenko | 49bcf9f | 2021-10-09 03:52:04 +0200 | [diff] [blame] | 6515 | *size = sz + first_escaped; |
| 6516 | return strstr(val, pattern + first_escaped); |
Denys Vlasenko | 49cc3ca | 2021-07-27 17:53:55 +0200 | [diff] [blame] | 6517 | } |
| 6518 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6519 | while (1) { |
| 6520 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 6521 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 6522 | if (end) { |
| 6523 | *size = end - val; |
| 6524 | return val; |
| 6525 | } |
| 6526 | if (*val == '\0') |
| 6527 | return NULL; |
| 6528 | /* Optimization: if "*pat" did not match the start of "string", |
| 6529 | * we know that "tring", "ring" etc will not match too: |
| 6530 | */ |
| 6531 | if (pattern[0] == '*') |
| 6532 | return NULL; |
| 6533 | val++; |
| 6534 | } |
| 6535 | } |
| 6536 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 6537 | { |
| 6538 | char *result = NULL; |
| 6539 | unsigned res_len = 0; |
| 6540 | unsigned repl_len = strlen(repl); |
| 6541 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6542 | /* Null pattern never matches, including if "var" is empty */ |
| 6543 | if (!pattern[0]) |
| 6544 | return result; /* NULL, no replaces happened */ |
| 6545 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6546 | while (1) { |
| 6547 | int size; |
| 6548 | char *s = strstr_pattern(val, pattern, &size); |
| 6549 | if (!s) |
| 6550 | break; |
| 6551 | |
| 6552 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6553 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 6554 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6555 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 6556 | |
| 6557 | val = s + size; |
| 6558 | if (exp_op == '/') |
| 6559 | break; |
| 6560 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 6561 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6562 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 6563 | strcpy(result + res_len, val); |
| 6564 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 6565 | } |
| 6566 | debug_printf_varexp("result:'%s'\n", result); |
| 6567 | return result; |
| 6568 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6569 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6570 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6571 | static int append_str_maybe_ifs_split(o_string *output, int n, |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 6572 | int first_ch, const char *val) |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6573 | { |
| 6574 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 6575 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6576 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6577 | if (val && val[0]) |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6578 | n = expand_on_ifs(output, n, val); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6579 | } else { /* quoted "$VAR" */ |
| 6580 | output->has_quoted_part = 1; |
| 6581 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6582 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 6583 | if (val && val[0]) |
| 6584 | o_addQstr(output, val); |
| 6585 | } |
| 6586 | return n; |
| 6587 | } |
| 6588 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6589 | /* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6590 | */ |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6591 | static NOINLINE int expand_one_var(o_string *output, int n, |
| 6592 | int first_ch, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6593 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6594 | const char *val; |
| 6595 | char *to_be_freed; |
| 6596 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6597 | char *var; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6598 | char exp_op; |
| 6599 | char exp_save = exp_save; /* for compiler */ |
| 6600 | char *exp_saveptr; /* points to expansion operator */ |
| 6601 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6602 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6603 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 6604 | val = NULL; |
| 6605 | to_be_freed = NULL; |
| 6606 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6607 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6608 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6609 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6610 | arg0 = arg[0]; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6611 | arg[0] = (arg0 & 0x7f); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6612 | exp_op = 0; |
| 6613 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6614 | if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 6615 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 6616 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 6617 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6618 | ) { |
| 6619 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6620 | var++; |
| 6621 | exp_op = 'L'; |
| 6622 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6623 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6624 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6625 | && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6626 | ) { |
| 6627 | /* ${?:0}, ${#[:]%0} etc */ |
| 6628 | exp_saveptr = var + 1; |
| 6629 | } else { |
| 6630 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 6631 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 6632 | } |
| 6633 | exp_op = exp_save = *exp_saveptr; |
| 6634 | if (exp_op) { |
| 6635 | exp_word = exp_saveptr + 1; |
| 6636 | if (exp_op == ':') { |
| 6637 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6638 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6639 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6640 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6641 | ) { |
| 6642 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 6643 | exp_op = ':'; |
| 6644 | exp_word--; |
| 6645 | } |
| 6646 | } |
| 6647 | *exp_saveptr = '\0'; |
| 6648 | } /* else: it's not an expansion op, but bare ${var} */ |
| 6649 | } |
| 6650 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6651 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6652 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 6653 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6654 | int nn = xatoi_positive(var); |
| 6655 | if (nn < G.global_argc) |
| 6656 | val = G.global_argv[nn]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6657 | /* else val remains NULL: $N with too big N */ |
| 6658 | } else { |
| 6659 | switch (var[0]) { |
| 6660 | case '$': /* pid */ |
| 6661 | val = utoa(G.root_pid); |
| 6662 | break; |
| 6663 | case '!': /* bg pid */ |
| 6664 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 6665 | break; |
| 6666 | case '?': /* exitcode */ |
| 6667 | val = utoa(G.last_exitcode); |
| 6668 | break; |
| 6669 | case '#': /* argc */ |
| 6670 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 6671 | break; |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 6672 | case '-': { /* active options */ |
| 6673 | /* Check set_mode() to see what option chars we support */ |
| 6674 | char *cp; |
| 6675 | val = cp = G.optstring_buf; |
| 6676 | if (G.o_opt[OPT_O_ERREXIT]) |
| 6677 | *cp++ = 'e'; |
| 6678 | if (G_interactive_fd) |
| 6679 | *cp++ = 'i'; |
| 6680 | if (G_x_mode) |
| 6681 | *cp++ = 'x'; |
| 6682 | /* If G.o_opt[OPT_O_NOEXEC] is true, |
| 6683 | * commands read but are not executed, |
| 6684 | * so $- can not execute too, 'n' is never seen in $-. |
| 6685 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 6686 | if (G.opt_c) |
| 6687 | *cp++ = 'c'; |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 6688 | if (G.opt_s) |
| 6689 | *cp++ = 's'; |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 6690 | *cp = '\0'; |
| 6691 | break; |
| 6692 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6693 | default: |
| 6694 | val = get_local_var_value(var); |
| 6695 | } |
| 6696 | } |
| 6697 | |
| 6698 | /* Handle any expansions */ |
| 6699 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6700 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6701 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6702 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6703 | debug_printf_expand("%s\n", val); |
| 6704 | } else if (exp_op) { |
| 6705 | if (exp_op == '%' || exp_op == '#') { |
| 6706 | /* Standard-mandated substring removal ops: |
| 6707 | * ${parameter%word} - remove smallest suffix pattern |
| 6708 | * ${parameter%%word} - remove largest suffix pattern |
| 6709 | * ${parameter#word} - remove smallest prefix pattern |
| 6710 | * ${parameter##word} - remove largest prefix pattern |
| 6711 | * |
| 6712 | * Word is expanded to produce a glob pattern. |
| 6713 | * Then var's value is matched to it and matching part removed. |
| 6714 | */ |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6715 | /* bash compat: if x is "" and no shrinking of it is possible, |
| 6716 | * inner ${...} is not evaluated. Example: |
| 6717 | * unset b; : ${a%${b=B}}; echo $b |
| 6718 | * assignment b=B only happens if $a is not "". |
| 6719 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6720 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6721 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6722 | char *exp_exp_word; |
| 6723 | char *loc; |
| 6724 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 6725 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6726 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6727 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6728 | 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] | 6729 | if (exp_exp_word) |
| 6730 | exp_word = exp_exp_word; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6731 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
| 6732 | /* |
| 6733 | * HACK ALERT. We depend here on the fact that |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6734 | * G.global_argv and results of utoa and get_local_var_value |
| 6735 | * are actually in writable memory: |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6736 | * scan_and_match momentarily stores NULs there. |
| 6737 | */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6738 | t = (char*)val; |
| 6739 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6740 | 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] | 6741 | free(exp_exp_word); |
| 6742 | if (loc) { /* match was found */ |
| 6743 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6744 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6745 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6746 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6747 | } |
| 6748 | } |
| 6749 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6750 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6751 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6752 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6753 | * Note that in encoded form it has TWO parts: |
| 6754 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6755 | * and if // is used, it is encoded as \: |
| 6756 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6757 | */ |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6758 | /* bash compat: if var is "", both pattern and repl |
| 6759 | * are still evaluated, if it is unset, then not: |
| 6760 | * unset b; a=; : ${a/z/${b=3}}; echo $b # b=3 |
| 6761 | * unset b; unset a; : ${a/z/${b=3}}; echo $b # b not set |
| 6762 | */ |
| 6763 | if (val /*&& val[0]*/) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6764 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6765 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6766 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6767 | * >az >bz |
| 6768 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6769 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6770 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6771 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6772 | * (note that a*z _pattern_ is never globbed!) |
| 6773 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6774 | char *pattern, *repl, *t; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6775 | pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6776 | if (!pattern) |
| 6777 | pattern = xstrdup(exp_word); |
| 6778 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6779 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6780 | exp_word = p; |
| 6781 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6782 | *p = '\0'; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6783 | repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6784 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6785 | /* HACK ALERT. We depend here on the fact that |
| 6786 | * G.global_argv and results of utoa and get_local_var_value |
| 6787 | * are actually in writable memory: |
| 6788 | * replace_pattern momentarily stores NULs there. */ |
| 6789 | t = (char*)val; |
| 6790 | to_be_freed = replace_pattern(t, |
| 6791 | pattern, |
| 6792 | (repl ? repl : exp_word), |
| 6793 | exp_op); |
| 6794 | if (to_be_freed) /* at least one replace happened */ |
| 6795 | val = to_be_freed; |
| 6796 | free(pattern); |
| 6797 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6798 | } else { |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6799 | /* Unset variable always gives nothing */ |
| 6800 | // a=; echo ${a/*/w} # "w" |
| 6801 | // unset a; echo ${a/*/w} # "" |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6802 | /* Just skip "replace" part */ |
| 6803 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6804 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6805 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6806 | } |
| 6807 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6808 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6809 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6810 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6811 | /* It's ${var:N[:M]} bashism. |
| 6812 | * Note that in encoded form it has TWO parts: |
| 6813 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6814 | */ |
| 6815 | arith_t beg, len; |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6816 | unsigned vallen; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6817 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6818 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6819 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6820 | if (errmsg) |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6821 | goto empty_result; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6822 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6823 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6824 | exp_word = p; |
| 6825 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6826 | *p = '\0'; |
Denys Vlasenko | a7b52d2 | 2020-12-23 12:38:03 +0100 | [diff] [blame] | 6827 | vallen = val ? strlen(val) : 0; |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6828 | if (beg < 0) { |
| 6829 | /* negative beg counts from the end */ |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6830 | beg = (arith_t)vallen + beg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6831 | } |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6832 | /* If expansion will be empty, do not even evaluate len */ |
| 6833 | if (!val || beg < 0 || beg > vallen) { |
| 6834 | /* Why > vallen, not >=? bash: |
| 6835 | * unset b; a=ab; : ${a:2:${b=3}}; echo $b # "", b=3 (!!!) |
| 6836 | * unset b; a=a; : ${a:2:${b=3}}; echo $b # "", b not set |
| 6837 | */ |
| 6838 | goto empty_result; |
| 6839 | } |
| 6840 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6841 | if (errmsg) |
| 6842 | goto empty_result; |
| 6843 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6844 | debug_printf_varexp("from val:'%s'\n", val); |
| 6845 | if (len < 0) { |
| 6846 | /* in bash, len=-n means strlen()-n */ |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6847 | len = (arith_t)vallen - beg + len; |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6848 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6849 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6850 | } |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6851 | if (len <= 0 || !val /*|| beg >= vallen*/) { |
| 6852 | empty_result: |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6853 | val = NULL; |
| 6854 | } else { |
| 6855 | /* Paranoia. What if user entered 9999999999999 |
| 6856 | * which fits in arith_t but not int? */ |
Denys Vlasenko | 07abc7c | 2020-12-21 10:09:48 +0100 | [diff] [blame] | 6857 | if (len > INT_MAX) |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6858 | len = INT_MAX; |
| 6859 | val = to_be_freed = xstrndup(val + beg, len); |
| 6860 | } |
| 6861 | debug_printf_varexp("val:'%s'\n", val); |
| 6862 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6863 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6864 | val = NULL; |
| 6865 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6866 | } else { /* one of "-=+?" */ |
| 6867 | /* Standard-mandated substitution ops: |
| 6868 | * ${var?word} - indicate error if unset |
| 6869 | * If var is unset, word (or a message indicating it is unset |
| 6870 | * if word is null) is written to standard error |
| 6871 | * and the shell exits with a non-zero exit status. |
| 6872 | * Otherwise, the value of var is substituted. |
| 6873 | * ${var-word} - use default value |
| 6874 | * If var is unset, word is substituted. |
| 6875 | * ${var=word} - assign and use default value |
| 6876 | * If var is unset, word is assigned to var. |
| 6877 | * In all cases, final value of var is substituted. |
| 6878 | * ${var+word} - use alternative value |
| 6879 | * If var is unset, null is substituted. |
| 6880 | * Otherwise, word is substituted. |
| 6881 | * |
| 6882 | * Word is subjected to tilde expansion, parameter expansion, |
| 6883 | * command substitution, and arithmetic expansion. |
| 6884 | * If word is not needed, it is not expanded. |
| 6885 | * |
| 6886 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6887 | * but also treat null var as if it is unset. |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6888 | * |
| 6889 | * Word-splitting and single quote behavior: |
| 6890 | * |
Denys Vlasenko | c7ef818 | 2020-12-27 16:04:54 +0100 | [diff] [blame] | 6891 | * $ f() { for i; do echo "|$i|"; done; } |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6892 | * |
Denys Vlasenko | c7ef818 | 2020-12-27 16:04:54 +0100 | [diff] [blame] | 6893 | * $ x=; f ${x:?'x y' z}; echo $? |
| 6894 | * bash: x: x y z # neither f nor "echo $?" executes |
| 6895 | * (if interactive, bash does not exit, but merely aborts to prompt. $? is set to 1) |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6896 | * $ x=; f "${x:?'x y' z}" |
Denys Vlasenko | c7ef818 | 2020-12-27 16:04:54 +0100 | [diff] [blame] | 6897 | * bash: x: x y z # dash prints: dash: x: 'x y' z |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6898 | * |
| 6899 | * $ x=; f ${x:='x y' z} |
| 6900 | * |x| |
| 6901 | * |y| |
| 6902 | * |z| |
| 6903 | * $ x=; f "${x:='x y' z}" |
| 6904 | * |'x y' z| |
| 6905 | * |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 6906 | * $ x=x; f ${x:+'x y' z} |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6907 | * |x y| |
| 6908 | * |z| |
| 6909 | * $ x=x; f "${x:+'x y' z}" |
| 6910 | * |'x y' z| |
| 6911 | * |
| 6912 | * $ x=; f ${x:-'x y' z} |
| 6913 | * |x y| |
| 6914 | * |z| |
| 6915 | * $ x=; f "${x:-'x y' z}" |
| 6916 | * |'x y' z| |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6917 | */ |
| 6918 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6919 | if (exp_op == '+') |
| 6920 | use_word = !use_word; |
| 6921 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6922 | (exp_save == ':') ? "true" : "false", use_word); |
| 6923 | if (use_word) { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6924 | if (exp_op == '+' || exp_op == '-') { |
| 6925 | /* ${var+word} - use alternative value */ |
| 6926 | /* ${var-word} - use default value */ |
| 6927 | n = encode_then_append_var_plusminus(output, n, exp_word, |
| 6928 | /*dquoted:*/ (arg0 & 0x80) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6929 | ); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6930 | val = NULL; |
| 6931 | } else { |
| 6932 | /* ${var?word} - indicate error if unset */ |
| 6933 | /* ${var=word} - assign and use default value */ |
| 6934 | to_be_freed = encode_then_expand_vararg(exp_word, |
| 6935 | /*handle_squotes:*/ !(arg0 & 0x80), |
| 6936 | /*unbackslash:*/ 0 |
| 6937 | ); |
| 6938 | if (to_be_freed) |
| 6939 | exp_word = to_be_freed; |
| 6940 | if (exp_op == '?') { |
| 6941 | /* mimic bash message */ |
| 6942 | msg_and_die_if_script("%s: %s", |
| 6943 | var, |
| 6944 | exp_word[0] |
| 6945 | ? exp_word |
| 6946 | : "parameter null or not set" |
| 6947 | /* ash has more specific messages, a-la: */ |
| 6948 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
| 6949 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6950 | //TODO: how interactive bash aborts expansion mid-command? |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6951 | //It aborts the entire line, returns to prompt: |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6952 | // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO |
| 6953 | // bash: x: x y z |
| 6954 | // $ |
| 6955 | // ("echo YO" is not executed, neither the f function call) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6956 | } else { |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 6957 | val = exp_word; |
| 6958 | } |
| 6959 | if (exp_op == '=') { |
| 6960 | /* ${var=[word]} or ${var:=[word]} */ |
| 6961 | if (isdigit(var[0]) || var[0] == '#') { |
| 6962 | /* mimic bash message */ |
| 6963 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
| 6964 | val = NULL; |
| 6965 | } else { |
| 6966 | char *new_var = xasprintf("%s=%s", var, val); |
| 6967 | set_local_var(new_var, /*flag:*/ 0); |
| 6968 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6969 | } |
| 6970 | } |
| 6971 | } |
| 6972 | } /* one of "-=+?" */ |
| 6973 | |
| 6974 | *exp_saveptr = exp_save; |
| 6975 | } /* if (exp_op) */ |
| 6976 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6977 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6978 | *pp = p; |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6979 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6980 | n = append_str_maybe_ifs_split(output, n, first_ch, val); |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6981 | |
| 6982 | free(to_be_freed); |
| 6983 | return n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | /* Expand all variable references in given string, adding words to list[] |
| 6987 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6988 | * to be filled). This routine is extremely tricky: has to deal with |
| 6989 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6990 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6991 | 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] | 6992 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6993 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6994 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6995 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6996 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6997 | char *p; |
| 6998 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6999 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 7000 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7001 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 7002 | |
| 7003 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 7004 | char first_ch; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 7005 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7006 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 7007 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 7008 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 7009 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 7010 | o_addchr(output, '\0'); |
| 7011 | n = o_save_ptr(output, n); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 7012 | output->ended_in_ifs = 0; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 7013 | } |
| 7014 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7015 | o_addblock(output, arg, p - arg); |
| 7016 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 7017 | arg = ++p; |
| 7018 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 7019 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7020 | /* Fetch special var name (if it is indeed one of them) |
| 7021 | * and quote bit, force the bit on if singleword expansion - |
| 7022 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 7023 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7024 | |
| 7025 | /* Is this variable quoted and thus expansion can't be null? |
| 7026 | * "$@" is special. Even if quoted, it can still |
| 7027 | * expand to nothing (not even an empty string), |
| 7028 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7029 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7030 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7031 | |
| 7032 | switch (first_ch & 0x7f) { |
| 7033 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 7034 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 7035 | case '@': { |
| 7036 | int i; |
| 7037 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7038 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 7039 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7040 | 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] | 7041 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7042 | while (G.global_argv[i]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 7043 | n = expand_on_ifs(output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7044 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 7045 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 7046 | /* this argv[] is not empty and not last: |
| 7047 | * put terminating NUL, start new word */ |
| 7048 | o_addchr(output, '\0'); |
| 7049 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 7050 | n = o_save_ptr(output, n); |
| 7051 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 7052 | } |
| 7053 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7054 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 7055 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7056 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 7057 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7058 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 7059 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7060 | while (1) { |
| 7061 | o_addQstr(output, G.global_argv[i]); |
| 7062 | if (++i >= G.global_argc) |
| 7063 | break; |
| 7064 | o_addchr(output, '\0'); |
| 7065 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 7066 | n = o_save_ptr(output, n); |
| 7067 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7068 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7069 | while (1) { |
| 7070 | o_addQstr(output, G.global_argv[i]); |
| 7071 | if (!G.global_argv[++i]) |
| 7072 | break; |
| 7073 | if (G.ifs[0]) |
| 7074 | o_addchr(output, G.ifs[0]); |
| 7075 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 7076 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7077 | } |
| 7078 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 7079 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7080 | case SPECIAL_VAR_SYMBOL: { |
| 7081 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7082 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 7083 | output->has_quoted_part = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7084 | cant_be_null = 0x80; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7085 | arg++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7086 | break; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7087 | } |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 7088 | case SPECIAL_VAR_QUOTED_SVS: |
| 7089 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7090 | /* "^C variable", represents literal ^C char (possible in scripts) */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 7091 | o_addchr(output, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 7092 | arg++; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 7093 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7094 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7095 | case '`': { |
| 7096 | /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 7097 | o_string subst_result = NULL_O_STRING; |
| 7098 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7099 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7100 | arg++; |
| 7101 | /* Can't just stuff it into output o_string, |
| 7102 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7103 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7104 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 7105 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 7106 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7107 | 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] | 7108 | 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] | 7109 | o_free(&subst_result); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7110 | break; |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 7111 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7112 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 7113 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7114 | case '+': { |
| 7115 | /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7116 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7117 | |
| 7118 | arg++; /* skip '+' */ |
| 7119 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 7120 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 7121 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 7122 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 7123 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | e4a0612 | 2020-02-21 17:21:34 +0100 | [diff] [blame] | 7124 | if (res < 0 |
| 7125 | && first_ch == (char)('+'|0x80) |
| 7126 | /* && (output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) */ |
| 7127 | ) { |
| 7128 | /* Quoted negative ariths, like filename[0"$((-9))"], |
| 7129 | * should not be interpreted as glob ranges. |
| 7130 | * Convert leading '-' to '\-': |
| 7131 | */ |
| 7132 | o_grow_by(output, 1); |
| 7133 | output->data[output->length++] = '\\'; |
| 7134 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7135 | o_addstr(output, arith_buf); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7136 | break; |
| 7137 | } |
| 7138 | #endif |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 7139 | default: |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7140 | /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 7141 | n = expand_one_var(output, n, first_ch, arg, &p); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame] | 7142 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7143 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 7144 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 7145 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 7146 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7147 | if (*p != SPECIAL_VAR_SYMBOL) |
| 7148 | *p = SPECIAL_VAR_SYMBOL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7149 | arg = ++p; |
| 7150 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 7151 | |
Denys Vlasenko | 4c3c8a1 | 2018-07-20 19:11:09 +0200 | [diff] [blame] | 7152 | if (*arg) { |
| 7153 | /* handle trailing string */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 7154 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 7155 | o_addchr(output, '\0'); |
| 7156 | n = o_save_ptr(output, n); |
| 7157 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7158 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 7159 | /* this part is literal, and it was already pre-quoted |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 7160 | * if needed (much earlier), do not use o_addQstr here! |
| 7161 | */ |
| 7162 | o_addstr(output, arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7163 | debug_print_list("expand_vars_to_list[b]", output, n); |
Denys Vlasenko | 1856740 | 2018-07-20 17:51:31 +0200 | [diff] [blame] | 7164 | } else |
| 7165 | if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | 83e434d | 2018-07-20 17:36:06 +0200 | [diff] [blame] | 7166 | && !(cant_be_null & 0x80) /* and all vars were not quoted */ |
| 7167 | && !output->has_quoted_part |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7168 | ) { |
| 7169 | n--; |
| 7170 | /* allow to reuse list[n] later without re-growth */ |
| 7171 | output->has_empty_slot = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | return n; |
| 7175 | } |
| 7176 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 7177 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7178 | { |
| 7179 | int n; |
| 7180 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7181 | o_string output = NULL_O_STRING; |
| 7182 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 7183 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7184 | |
| 7185 | n = 0; |
Denys Vlasenko | 57235be | 2018-07-20 14:45:12 +0200 | [diff] [blame] | 7186 | for (;;) { |
| 7187 | /* go to next list[n] */ |
| 7188 | output.ended_in_ifs = 0; |
| 7189 | n = o_save_ptr(&output, n); |
| 7190 | |
| 7191 | if (!*argv) |
| 7192 | break; |
| 7193 | |
| 7194 | /* expand argv[i] */ |
| 7195 | n = expand_vars_to_list(&output, n, *argv++); |
Denys Vlasenko | 294eb46 | 2018-07-20 16:18:59 +0200 | [diff] [blame] | 7196 | /* if (!output->has_empty_slot) -- need this?? */ |
| 7197 | o_addchr(&output, '\0'); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7198 | } |
| 7199 | debug_print_list("expand_variables", &output, n); |
| 7200 | |
| 7201 | /* output.data (malloced in one block) gets returned in "list" */ |
| 7202 | list = o_finalize_list(&output, n); |
| 7203 | debug_print_strings("expand_variables[1]", list); |
| 7204 | return list; |
| 7205 | } |
| 7206 | |
| 7207 | static char **expand_strvec_to_strvec(char **argv) |
| 7208 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 7209 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7210 | } |
| 7211 | |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 7212 | #if defined(CMD_SINGLEWORD_NOGLOB) || defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7213 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 7214 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 7215 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7216 | } |
| 7217 | #endif |
| 7218 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7219 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7220 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7221 | * |
| 7222 | * NB: should NOT do globbing! |
| 7223 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 7224 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7225 | 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] | 7226 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 7227 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 7228 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7229 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 7230 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7231 | char *argv[2], **list; |
| 7232 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7233 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7234 | /* This is generally an optimization, but it also |
| 7235 | * handles "", which otherwise trips over !list[0] check below. |
| 7236 | * (is this ever happens that we actually get str="" here?) |
| 7237 | */ |
| 7238 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 7239 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7240 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7241 | return xstrdup(str); |
| 7242 | } |
| 7243 | |
| 7244 | argv[0] = (char*)str; |
| 7245 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7246 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 7247 | if (!list[0]) { |
| 7248 | /* Example where it happens: |
| 7249 | * x=; echo ${x:-"$@"} |
| 7250 | */ |
| 7251 | ((char*)list)[0] = '\0'; |
| 7252 | } else { |
| 7253 | if (HUSH_DEBUG) |
| 7254 | if (list[1]) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 7255 | bb_simple_error_msg_and_die("BUG in varexp2"); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 7256 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 7257 | overlapping_strcpy((char*)list, list[0]); |
| 7258 | if (do_unbackslash) |
| 7259 | unbackslash((char*)list); |
| 7260 | } |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7261 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7262 | return (char*)list; |
| 7263 | } |
| 7264 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 7265 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7266 | static char* expand_strvec_to_string(char **argv) |
| 7267 | { |
| 7268 | char **list; |
| 7269 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 7270 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7271 | /* Convert all NULs to spaces */ |
| 7272 | if (list[0]) { |
| 7273 | int n = 1; |
| 7274 | while (list[n]) { |
| 7275 | if (HUSH_DEBUG) |
| 7276 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 7277 | bb_error_msg_and_die("BUG in varexp3"); |
| 7278 | /* bash uses ' ' regardless of $IFS contents */ |
| 7279 | list[n][-1] = ' '; |
| 7280 | n++; |
| 7281 | } |
| 7282 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 7283 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7284 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 7285 | return (char*)list; |
| 7286 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 7287 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7288 | |
| 7289 | static char **expand_assignments(char **argv, int count) |
| 7290 | { |
| 7291 | int i; |
| 7292 | char **p; |
| 7293 | |
| 7294 | G.expanded_assignments = p = NULL; |
| 7295 | /* Expand assignments into one string each */ |
| 7296 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7297 | p = add_string_to_strings(p, |
| 7298 | expand_string_to_string(argv[i], |
| 7299 | EXP_FLAG_ESC_GLOB_CHARS, |
| 7300 | /*unbackslash:*/ 1 |
| 7301 | ) |
| 7302 | ); |
| 7303 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7304 | } |
| 7305 | G.expanded_assignments = NULL; |
| 7306 | return p; |
| 7307 | } |
| 7308 | |
| 7309 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7310 | static void switch_off_special_sigs(unsigned mask) |
| 7311 | { |
| 7312 | unsigned sig = 0; |
| 7313 | while ((mask >>= 1) != 0) { |
| 7314 | sig++; |
| 7315 | if (!(mask & 1)) |
| 7316 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7317 | #if ENABLE_HUSH_TRAP |
| 7318 | if (G_traps) { |
| 7319 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7320 | /* trap is '', has to remain SIG_IGN */ |
| 7321 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7322 | free(G_traps[sig]); |
| 7323 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7324 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7325 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7326 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7327 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7328 | } |
| 7329 | } |
| 7330 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 7331 | #if BB_MMU |
| 7332 | /* never called */ |
| 7333 | void re_execute_shell(char ***to_free, const char *s, |
| 7334 | char *g_argv0, char **g_argv, |
| 7335 | char **builtin_argv) NORETURN; |
| 7336 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7337 | static void reset_traps_to_defaults(void) |
| 7338 | { |
| 7339 | /* This function is always called in a child shell |
| 7340 | * after fork (not vfork, NOMMU doesn't use this function). |
| 7341 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7342 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7343 | unsigned mask; |
| 7344 | |
| 7345 | /* Child shells are not interactive. |
| 7346 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 7347 | * Testcase: (while :; do :; done) + ^Z should background. |
| 7348 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 7349 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7350 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7351 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7352 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7353 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7354 | /* Switch off special sigs */ |
| 7355 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7356 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7357 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7358 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 7359 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 7360 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 7361 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7362 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7363 | # if ENABLE_HUSH_TRAP |
| 7364 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7365 | return; |
| 7366 | |
| 7367 | /* Reset all sigs to default except ones with empty traps */ |
| 7368 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7369 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7370 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7371 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7372 | continue; /* empty trap: has to remain SIG_IGN */ |
| 7373 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7374 | free(G_traps[sig]); |
| 7375 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 7376 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7377 | if (sig == 0) |
| 7378 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 7379 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7380 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7381 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7382 | } |
| 7383 | |
| 7384 | #else /* !BB_MMU */ |
| 7385 | |
| 7386 | static void re_execute_shell(char ***to_free, const char *s, |
| 7387 | char *g_argv0, char **g_argv, |
| 7388 | char **builtin_argv) NORETURN; |
| 7389 | static void re_execute_shell(char ***to_free, const char *s, |
| 7390 | char *g_argv0, char **g_argv, |
| 7391 | char **builtin_argv) |
| 7392 | { |
| 7393 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 7394 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 7395 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 7396 | char *heredoc_argv[4]; |
| 7397 | struct variable *cur; |
| 7398 | # if ENABLE_HUSH_FUNCTIONS |
| 7399 | struct function *funcp; |
| 7400 | # endif |
| 7401 | char **argv, **pp; |
| 7402 | unsigned cnt; |
| 7403 | unsigned long long empty_trap_mask; |
| 7404 | |
| 7405 | if (!g_argv0) { /* heredoc */ |
| 7406 | argv = heredoc_argv; |
| 7407 | argv[0] = (char *) G.argv0_for_re_execing; |
| 7408 | argv[1] = (char *) "-<"; |
| 7409 | argv[2] = (char *) s; |
| 7410 | argv[3] = NULL; |
| 7411 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 7412 | goto do_exec; |
| 7413 | } |
| 7414 | |
| 7415 | cnt = 0; |
| 7416 | pp = builtin_argv; |
| 7417 | if (pp) while (*pp++) |
| 7418 | cnt++; |
| 7419 | |
| 7420 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7421 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7422 | int sig; |
| 7423 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7424 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7425 | empty_trap_mask |= 1LL << sig; |
| 7426 | } |
| 7427 | } |
| 7428 | |
| 7429 | sprintf(param_buf, NOMMU_HACK_FMT |
| 7430 | , (unsigned) G.root_pid |
| 7431 | , (unsigned) G.root_ppid |
| 7432 | , (unsigned) G.last_bg_pid |
| 7433 | , (unsigned) G.last_exitcode |
| 7434 | , cnt |
| 7435 | , empty_trap_mask |
| 7436 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 7437 | ); |
| 7438 | # undef NOMMU_HACK_FMT |
| 7439 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 7440 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 7441 | */ |
| 7442 | cnt += 6; |
| 7443 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7444 | if (!cur->flg_export || cur->flg_read_only) |
| 7445 | cnt += 2; |
| 7446 | } |
| 7447 | # if ENABLE_HUSH_FUNCTIONS |
| 7448 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 7449 | cnt += 3; |
| 7450 | # endif |
| 7451 | pp = g_argv; |
| 7452 | while (*pp++) |
| 7453 | cnt++; |
| 7454 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 7455 | *pp++ = (char *) G.argv0_for_re_execing; |
| 7456 | *pp++ = param_buf; |
| 7457 | for (cur = G.top_var; cur; cur = cur->next) { |
| 7458 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 7459 | continue; |
| 7460 | if (cur->flg_read_only) { |
| 7461 | *pp++ = (char *) "-R"; |
| 7462 | *pp++ = cur->varstr; |
| 7463 | } else if (!cur->flg_export) { |
| 7464 | *pp++ = (char *) "-V"; |
| 7465 | *pp++ = cur->varstr; |
| 7466 | } |
| 7467 | } |
| 7468 | # if ENABLE_HUSH_FUNCTIONS |
| 7469 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 7470 | *pp++ = (char *) "-F"; |
| 7471 | *pp++ = funcp->name; |
| 7472 | *pp++ = funcp->body_as_string; |
| 7473 | } |
| 7474 | # endif |
| 7475 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 7476 | * |
| 7477 | * However, POSIX says that subshells reset signals with traps |
| 7478 | * to SIG_DFL. |
| 7479 | * I tested bash-3.2 and it not only does that with true subshells |
| 7480 | * of the form ( list ), but with any forked children shells. |
| 7481 | * I set trap "echo W" WINCH; and then tried: |
| 7482 | * |
| 7483 | * { echo 1; sleep 20; echo 2; } & |
| 7484 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 7485 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 7486 | * |
| 7487 | * In all these cases sending SIGWINCH to the child shell |
| 7488 | * did not run the trap. If I add trap "echo V" WINCH; |
| 7489 | * _inside_ group (just before echo 1), it works. |
| 7490 | * |
| 7491 | * 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] | 7492 | */ |
| 7493 | *pp++ = (char *) "-c"; |
| 7494 | *pp++ = (char *) s; |
| 7495 | if (builtin_argv) { |
| 7496 | while (*++builtin_argv) |
| 7497 | *pp++ = *builtin_argv; |
| 7498 | *pp++ = (char *) ""; |
| 7499 | } |
| 7500 | *pp++ = g_argv0; |
| 7501 | while (*g_argv) |
| 7502 | *pp++ = *g_argv++; |
| 7503 | /* *pp = NULL; - is already there */ |
| 7504 | pp = environ; |
| 7505 | |
| 7506 | do_exec: |
| 7507 | 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] | 7508 | /* Don't propagate SIG_IGN to the child */ |
| 7509 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7510 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7511 | execve(bb_busybox_exec_path, argv, pp); |
| 7512 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 7513 | if (argv[0][0] == '/') |
| 7514 | execve(argv[0], argv, pp); |
| 7515 | xfunc_error_retval = 127; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 7516 | bb_simple_error_msg_and_die("can't re-execute the shell"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7517 | } |
| 7518 | #endif /* !BB_MMU */ |
| 7519 | |
| 7520 | |
| 7521 | static int run_and_free_list(struct pipe *pi); |
| 7522 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7523 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7524 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 7525 | * end_trigger controls how often we stop parsing |
| 7526 | * NUL: parse all, execute, return |
| 7527 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 7528 | */ |
| 7529 | 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] | 7530 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7531 | /* Why we need empty flag? |
| 7532 | * An obscure corner case "false; ``; echo $?": |
| 7533 | * empty command in `` should still set $? to 0. |
| 7534 | * But we can't just set $? to 0 at the start, |
| 7535 | * this breaks "false; echo `echo $?`" case. |
| 7536 | */ |
| 7537 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7538 | while (1) { |
| 7539 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 7540 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7541 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 7542 | if (end_trigger == ';') { |
| 7543 | G.promptmode = 0; /* PS1 */ |
| 7544 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 7545 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 7546 | #endif |
Denys Vlasenko | 474cb20 | 2018-07-24 13:03:03 +0200 | [diff] [blame] | 7547 | pipe_list = parse_stream(NULL, NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 7548 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 7549 | /* If we are in "big" script |
| 7550 | * (not in `cmd` or something similar)... |
| 7551 | */ |
| 7552 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 7553 | /* Discard cached input (rest of line) */ |
| 7554 | int ch = inp->last_char; |
| 7555 | while (ch != EOF && ch != '\n') { |
| 7556 | //bb_error_msg("Discarded:'%c'", ch); |
| 7557 | ch = i_getch(inp); |
| 7558 | } |
| 7559 | /* Force prompt */ |
| 7560 | inp->p = NULL; |
| 7561 | /* This stream isn't empty */ |
| 7562 | empty = 0; |
| 7563 | continue; |
| 7564 | } |
| 7565 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7566 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7567 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7568 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7569 | debug_print_tree(pipe_list, 0); |
| 7570 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7571 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7572 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7573 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 7574 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7575 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7576 | } |
| 7577 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7578 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7579 | { |
| 7580 | struct in_str input; |
Denys Vlasenko | 574b9c4 | 2021-09-07 21:44:44 +0200 | [diff] [blame] | 7581 | IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7582 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7583 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7584 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | 574b9c4 | 2021-09-07 21:44:44 +0200 | [diff] [blame] | 7585 | IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7588 | static void parse_and_run_file(HFILE *fp) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7589 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7590 | struct in_str input; |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7591 | IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 7592 | |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7593 | IF_HUSH_LINENO_VAR(G.parse_lineno = 1;) |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7594 | setup_file_in_str(&input, fp); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7595 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 7596 | IF_HUSH_LINENO_VAR(G.parse_lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7597 | } |
| 7598 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7599 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7600 | 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] | 7601 | { |
| 7602 | pid_t pid; |
| 7603 | int channel[2]; |
| 7604 | # if !BB_MMU |
| 7605 | char **to_free = NULL; |
| 7606 | # endif |
| 7607 | |
| 7608 | xpipe(channel); |
| 7609 | pid = BB_MMU ? xfork() : xvfork(); |
| 7610 | if (pid == 0) { /* child */ |
| 7611 | disable_restore_tty_pgrp_on_exit(); |
| 7612 | /* Process substitution is not considered to be usual |
| 7613 | * 'command execution'. |
| 7614 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 7615 | */ |
| 7616 | bb_signals(0 |
| 7617 | + (1 << SIGTSTP) |
| 7618 | + (1 << SIGTTIN) |
| 7619 | + (1 << SIGTTOU) |
| 7620 | , SIG_IGN); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7621 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 7622 | xmove_fd(channel[1], 1); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7623 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7624 | /* Awful hack for `trap` or $(trap). |
| 7625 | * |
| 7626 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 7627 | * contains an example where "trap" is executed in a subshell: |
| 7628 | * |
| 7629 | * save_traps=$(trap) |
| 7630 | * ... |
| 7631 | * eval "$save_traps" |
| 7632 | * |
| 7633 | * Standard does not say that "trap" in subshell shall print |
| 7634 | * parent shell's traps. It only says that its output |
| 7635 | * must have suitable form, but then, in the above example |
| 7636 | * (which is not supposed to be normative), it implies that. |
| 7637 | * |
| 7638 | * bash (and probably other shell) does implement it |
| 7639 | * (traps are reset to defaults, but "trap" still shows them), |
| 7640 | * but as a result, "trap" logic is hopelessly messed up: |
| 7641 | * |
| 7642 | * # trap |
| 7643 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 7644 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 7645 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 7646 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 7647 | * trap -- 'echo Ho' SIGWINCH |
| 7648 | * # echo `(trap)` <--- in subshell in subshell - output |
| 7649 | * trap -- 'echo Ho' SIGWINCH |
| 7650 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 7651 | * trap -- 'echo Ho' SIGWINCH |
| 7652 | * |
| 7653 | * The rules when to forget and when to not forget traps |
| 7654 | * get really complex and nonsensical. |
| 7655 | * |
| 7656 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 7657 | */ |
| 7658 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 7659 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7660 | && skip_whitespace(s + 4)[0] == '\0' |
| 7661 | ) { |
Denys Vlasenko | 987be93 | 2022-02-06 20:07:12 +0100 | [diff] [blame] | 7662 | static const char *const argv[] ALIGN_PTR = { NULL, NULL }; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7663 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 7664 | fflush_all(); /* important */ |
| 7665 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7666 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 7667 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7668 | # if BB_MMU |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7669 | /* Prevent it from trying to handle ctrl-z etc */ |
| 7670 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 7671 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7672 | reset_traps_to_defaults(); |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 7673 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 7674 | //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] | 7675 | parse_and_run_string(s); |
| 7676 | _exit(G.last_exitcode); |
| 7677 | # else |
| 7678 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 7679 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 7680 | * huge=`cat BIG` # was blocking here forever |
| 7681 | * echo OK |
| 7682 | */ |
| 7683 | re_execute_shell(&to_free, |
| 7684 | s, |
| 7685 | G.global_argv[0], |
| 7686 | G.global_argv + 1, |
| 7687 | NULL); |
| 7688 | # endif |
| 7689 | } |
| 7690 | |
| 7691 | /* parent */ |
| 7692 | *pid_p = pid; |
| 7693 | # if ENABLE_HUSH_FAST |
| 7694 | G.count_SIGCHLD++; |
| 7695 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 7696 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 7697 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7698 | # endif |
| 7699 | enable_restore_tty_pgrp_on_exit(); |
| 7700 | # if !BB_MMU |
| 7701 | free(to_free); |
| 7702 | # endif |
| 7703 | close(channel[1]); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7704 | return channel[0]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7705 | } |
| 7706 | |
| 7707 | /* Return code is exit status of the process that is run. */ |
| 7708 | static int process_command_subs(o_string *dest, const char *s) |
| 7709 | { |
| 7710 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7711 | pid_t pid; |
| 7712 | int status, ch, eol_cnt; |
| 7713 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7714 | fp = xfdopen_for_read(generate_stream_from_string(s, &pid)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7715 | |
| 7716 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7717 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7718 | while ((ch = getc(fp)) != EOF) { |
| 7719 | if (ch == '\0') |
| 7720 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7721 | if (ch == '\n') { |
| 7722 | eol_cnt++; |
| 7723 | continue; |
| 7724 | } |
| 7725 | while (eol_cnt) { |
| 7726 | o_addchr(dest, '\n'); |
| 7727 | eol_cnt--; |
| 7728 | } |
| 7729 | o_addQchr(dest, ch); |
| 7730 | } |
| 7731 | |
| 7732 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7733 | fclose(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7734 | /* We need to extract exitcode. Test case |
| 7735 | * "true; echo `sleep 1; false` $?" |
| 7736 | * should print 1 */ |
| 7737 | safe_waitpid(pid, &status, 0); |
| 7738 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 7739 | return WEXITSTATUS(status); |
| 7740 | } |
| 7741 | #endif /* ENABLE_HUSH_TICK */ |
| 7742 | |
| 7743 | |
| 7744 | static void setup_heredoc(struct redir_struct *redir) |
| 7745 | { |
| 7746 | struct fd_pair pair; |
| 7747 | pid_t pid; |
| 7748 | int len, written; |
| 7749 | /* the _body_ of heredoc (misleading field name) */ |
| 7750 | const char *heredoc = redir->rd_filename; |
| 7751 | char *expanded; |
| 7752 | #if !BB_MMU |
| 7753 | char **to_free; |
| 7754 | #endif |
| 7755 | |
| 7756 | expanded = NULL; |
| 7757 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 7758 | expanded = encode_then_expand_string(heredoc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7759 | if (expanded) |
| 7760 | heredoc = expanded; |
| 7761 | } |
| 7762 | len = strlen(heredoc); |
| 7763 | |
| 7764 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 7765 | xpiped_pair(pair); |
| 7766 | xmove_fd(pair.rd, redir->rd_fd); |
| 7767 | |
| 7768 | /* Try writing without forking. Newer kernels have |
| 7769 | * dynamically growing pipes. Must use non-blocking write! */ |
| 7770 | ndelay_on(pair.wr); |
| 7771 | while (1) { |
| 7772 | written = write(pair.wr, heredoc, len); |
| 7773 | if (written <= 0) |
| 7774 | break; |
| 7775 | len -= written; |
| 7776 | if (len == 0) { |
| 7777 | close(pair.wr); |
| 7778 | free(expanded); |
| 7779 | return; |
| 7780 | } |
| 7781 | heredoc += written; |
| 7782 | } |
| 7783 | ndelay_off(pair.wr); |
| 7784 | |
| 7785 | /* Okay, pipe buffer was not big enough */ |
| 7786 | /* Note: we must not create a stray child (bastard? :) |
| 7787 | * for the unsuspecting parent process. Child creates a grandchild |
| 7788 | * and exits before parent execs the process which consumes heredoc |
| 7789 | * (that exec happens after we return from this function) */ |
| 7790 | #if !BB_MMU |
| 7791 | to_free = NULL; |
| 7792 | #endif |
| 7793 | pid = xvfork(); |
| 7794 | if (pid == 0) { |
| 7795 | /* child */ |
| 7796 | disable_restore_tty_pgrp_on_exit(); |
| 7797 | pid = BB_MMU ? xfork() : xvfork(); |
| 7798 | if (pid != 0) |
| 7799 | _exit(0); |
| 7800 | /* grandchild */ |
| 7801 | close(redir->rd_fd); /* read side of the pipe */ |
| 7802 | #if BB_MMU |
| 7803 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 7804 | _exit(0); |
| 7805 | #else |
| 7806 | /* Delegate blocking writes to another process */ |
| 7807 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 7808 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 7809 | #endif |
| 7810 | } |
| 7811 | /* parent */ |
| 7812 | #if ENABLE_HUSH_FAST |
| 7813 | G.count_SIGCHLD++; |
| 7814 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7815 | #endif |
| 7816 | enable_restore_tty_pgrp_on_exit(); |
| 7817 | #if !BB_MMU |
| 7818 | free(to_free); |
| 7819 | #endif |
| 7820 | close(pair.wr); |
| 7821 | free(expanded); |
| 7822 | wait(NULL); /* wait till child has died */ |
| 7823 | } |
| 7824 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7825 | struct squirrel { |
| 7826 | int orig_fd; |
| 7827 | int moved_to; |
| 7828 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7829 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7830 | }; |
| 7831 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7832 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7833 | { |
| 7834 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7835 | sq[i].orig_fd = orig; |
| 7836 | sq[i].moved_to = moved; |
| 7837 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7838 | return sq; |
| 7839 | } |
| 7840 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7841 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7842 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7843 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7844 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7845 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7846 | i = 0; |
| 7847 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7848 | /* If we collide with an already moved fd... */ |
| 7849 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7850 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7851 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7852 | if (sq[i].moved_to < 0) /* what? */ |
| 7853 | xfunc_die(); |
| 7854 | return sq; |
| 7855 | } |
| 7856 | if (fd == sq[i].orig_fd) { |
| 7857 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7858 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7859 | return sq; |
| 7860 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7861 | } |
| 7862 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7863 | /* 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] | 7864 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7865 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7866 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7867 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7868 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7869 | } |
| 7870 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7871 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7872 | { |
| 7873 | int i; |
| 7874 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7875 | i = 0; |
| 7876 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7877 | /* If we collide with an already moved fd... */ |
| 7878 | if (fd == sq[i].orig_fd) { |
| 7879 | /* Examples: |
| 7880 | * "echo 3>FILE 3>&- 3>FILE" |
| 7881 | * "echo 3>&- 3>FILE" |
| 7882 | * No need for last redirect to insert |
| 7883 | * another "need to close 3" indicator. |
| 7884 | */ |
| 7885 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7886 | return sq; |
| 7887 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7891 | return append_squirrel(sq, i, fd, -1); |
| 7892 | } |
| 7893 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7894 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7895 | * Move all conflicting internally used fds, |
| 7896 | * and remember them so that we can restore them later. |
| 7897 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7898 | 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] | 7899 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7900 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7901 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7902 | |
| 7903 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7904 | if (fd != 0 /* don't trigger for G_interactive_fd == 0 (that's "not interactive" flag) */ |
| 7905 | && fd == G_interactive_fd |
| 7906 | ) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7907 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 7908 | G_interactive_fd = xdup_CLOEXEC_and_close(G_interactive_fd, avoid_fd); |
| 7909 | 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] | 7910 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7911 | } |
| 7912 | #endif |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7913 | /* Are we called from setup_redirects(squirrel==NULL) |
| 7914 | * in redirect in a [v]forked child? |
| 7915 | */ |
| 7916 | if (sqp == NULL) { |
| 7917 | /* No need to move script fds. |
| 7918 | * For NOMMU case, it's actively wrong: we'd change ->fd |
| 7919 | * fields in memory for the parent, but parent's fds |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7920 | * aren't moved, it would use wrong fd! |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7921 | * Reproducer: "cmd 3>FILE" in script. |
| 7922 | * If we would call move_HFILEs_on_redirect(), child would: |
| 7923 | * fcntl64(3, F_DUPFD_CLOEXEC, 10) = 10 |
| 7924 | * close(3) = 0 |
| 7925 | * and change ->fd to 10 if fd#3 is a script fd. WRONG. |
| 7926 | */ |
| 7927 | //bb_error_msg("sqp == NULL: [v]forked child"); |
| 7928 | return 0; |
| 7929 | } |
| 7930 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 7931 | /* If this one of script's fds? */ |
| 7932 | if (move_HFILEs_on_redirect(fd, avoid_fd)) |
| 7933 | return 1; /* yes. "we closed fd" (actually moved it) */ |
| 7934 | |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7935 | /* Are we called for "exec 3>FILE"? Came through |
| 7936 | * redirect_and_varexp_helper(squirrel=ERR_PTR) -> setup_redirects(ERR_PTR) |
| 7937 | * This case used to fail for this script: |
| 7938 | * exec 3>FILE |
| 7939 | * echo Ok |
| 7940 | * ...100000 more lines... |
| 7941 | * echo Ok |
| 7942 | * as follows: |
| 7943 | * read(3, "exec 3>FILE\necho Ok\necho Ok"..., 1024) = 1024 |
| 7944 | * open("FILE", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 4 |
| 7945 | * dup2(4, 3) = 3 |
| 7946 | * ^^^^^^^^ oops, we lost fd#3 opened to our script! |
| 7947 | * close(4) = 0 |
| 7948 | * write(1, "Ok\n", 3) = 3 |
| 7949 | * ... = 3 |
| 7950 | * write(1, "Ok\n", 3) = 3 |
| 7951 | * read(3, 0x94fbc08, 1024) = -1 EBADF (Bad file descriptor) |
| 7952 | * ^^^^^^^^ oops, wrong fd!!! |
| 7953 | * With this case separate from sqp == NULL and *after* move_HFILEs, |
| 7954 | * it now works: |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7955 | */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7956 | if (sqp == ERR_PTR) { |
| 7957 | /* Don't preserve redirected fds: exec is _meant_ to change these */ |
| 7958 | //bb_error_msg("sqp == ERR_PTR: exec >FILE"); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7959 | return 0; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 7960 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7961 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7962 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7963 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7964 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7965 | } |
| 7966 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7967 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7968 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7969 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7970 | int i; |
| 7971 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7972 | if (sq[i].moved_to >= 0) { |
| 7973 | /* We simply die on error */ |
| 7974 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7975 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7976 | } else { |
| 7977 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7978 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7979 | close(sq[i].orig_fd); |
| 7980 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7981 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7982 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7983 | } |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7984 | if (G.HFILE_stdin |
Denys Vlasenko | 1237d62 | 2020-12-25 19:01:49 +0100 | [diff] [blame] | 7985 | && G.HFILE_stdin->fd > STDIN_FILENO |
| 7986 | /* we compare > STDIN, not == STDIN, since hfgetc() |
| 7987 | * closes fd and sets ->fd to -1 if EOF is reached. |
| 7988 | * Testcase: echo 'pwd' | hush |
| 7989 | */ |
Denys Vlasenko | 2180656 | 2019-11-01 14:16:07 +0100 | [diff] [blame] | 7990 | ) { |
| 7991 | /* Testcase: interactive "read r <FILE; echo $r; read r; echo $r". |
| 7992 | * Redirect moves ->fd to e.g. 10, |
| 7993 | * and it is not restored above (we do not restore script fds |
| 7994 | * after redirects, we just use new, "moved" fds). |
| 7995 | * However for stdin, get_user_input() -> read_line_input(), |
| 7996 | * and read builtin, depend on fd == STDIN_FILENO. |
| 7997 | */ |
| 7998 | debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd); |
| 7999 | xmove_fd(G.HFILE_stdin->fd, STDIN_FILENO); |
| 8000 | G.HFILE_stdin->fd = STDIN_FILENO; |
| 8001 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 8002 | |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 8003 | /* If moved, G_interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 8004 | } |
| 8005 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8006 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8007 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8008 | { |
| 8009 | if (G_interactive_fd) |
| 8010 | close(G_interactive_fd); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 8011 | close_all_HFILE_list(); |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8012 | } |
| 8013 | #endif |
| 8014 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 8015 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 8016 | { |
| 8017 | int i; |
| 8018 | |
| 8019 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 8020 | if (fd == G_interactive_fd) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 8021 | return 1; |
| 8022 | #endif |
| 8023 | /* If this one of script's fds? */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 8024 | if (fd_in_HFILEs(fd)) |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 8025 | return 1; |
| 8026 | |
| 8027 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 8028 | if (fd == sq[i].moved_to) |
| 8029 | return 1; |
| 8030 | } |
| 8031 | return 0; |
| 8032 | } |
| 8033 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8034 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 8035 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8036 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8037 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8038 | struct redir_struct *redir; |
| 8039 | |
| 8040 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8041 | int newfd; |
| 8042 | int closed; |
| 8043 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8044 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8045 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8046 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8047 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 8048 | * of the heredoc */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 8049 | debug_printf_redir("set heredoc '%s'\n", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8050 | redir->rd_filename); |
| 8051 | setup_heredoc(redir); |
| 8052 | continue; |
| 8053 | } |
| 8054 | |
| 8055 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8056 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8057 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8058 | int mode; |
| 8059 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8060 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 8061 | /* Examples: |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 8062 | * "cmd >" (no filename) |
| 8063 | * "cmd > <file" (2nd redirect starts too early) |
| 8064 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 8065 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8066 | continue; |
| 8067 | } |
| 8068 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8069 | p = expand_string_to_string(redir->rd_filename, |
| 8070 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8071 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8072 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8073 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8074 | /* Error message from open_or_warn can be lost |
| 8075 | * if stderr has been redirected, but bash |
| 8076 | * and ash both lose it as well |
| 8077 | * (though zsh doesn't!) |
| 8078 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8079 | return 1; |
| 8080 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8081 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 8082 | /* open() gave us precisely the fd we wanted. |
| 8083 | * This means that this fd was not busy |
| 8084 | * (not opened to anywhere). |
| 8085 | * Remember to close it on restore: |
| 8086 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8087 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 8088 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 8089 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8090 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8091 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 8092 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8093 | } |
| 8094 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8095 | if (newfd == redir->rd_fd) |
| 8096 | continue; |
| 8097 | |
| 8098 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 8099 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 8100 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 8101 | |
| 8102 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 8103 | if (newfd == REDIRFD_CLOSE) { |
| 8104 | /* "N>&-" means "close me" */ |
| 8105 | if (!closed) { |
| 8106 | /* ^^^ optimization: saving may already |
| 8107 | * have closed it. If not... */ |
| 8108 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8109 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8110 | /* Sometimes we do another close on restore, getting EBADF. |
| 8111 | * Consider "echo 3>FILE 3>&-" |
| 8112 | * first redirect remembers "need to close 3", |
| 8113 | * and second redirect closes 3! Restore code then closes 3 again. |
| 8114 | */ |
| 8115 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 8116 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 8117 | if (internally_opened_fd(newfd, sqp && sqp != ERR_PTR ? *sqp : NULL)) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 8118 | //errno = EBADF; |
| 8119 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 8120 | newfd = -1; /* same effect as code above */ |
| 8121 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 8122 | xdup2(newfd, redir->rd_fd); |
| 8123 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 8124 | /* "rd_fd > FILE" */ |
| 8125 | close(newfd); |
| 8126 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8127 | } |
| 8128 | } |
| 8129 | return 0; |
| 8130 | } |
| 8131 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8132 | static char *find_in_path(const char *arg) |
| 8133 | { |
| 8134 | char *ret = NULL; |
| 8135 | const char *PATH = get_local_var_value("PATH"); |
| 8136 | |
| 8137 | if (!PATH) |
| 8138 | return NULL; |
| 8139 | |
| 8140 | while (1) { |
| 8141 | const char *end = strchrnul(PATH, ':'); |
| 8142 | int sz = end - PATH; /* must be int! */ |
| 8143 | |
| 8144 | free(ret); |
| 8145 | if (sz != 0) { |
| 8146 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 8147 | } else { |
| 8148 | /* We have xxx::yyyy in $PATH, |
| 8149 | * it means "use current dir" */ |
| 8150 | ret = xstrdup(arg); |
| 8151 | } |
| 8152 | if (access(ret, F_OK) == 0) |
| 8153 | break; |
| 8154 | |
| 8155 | if (*end == '\0') { |
| 8156 | free(ret); |
| 8157 | return NULL; |
| 8158 | } |
| 8159 | PATH = end + 1; |
| 8160 | } |
| 8161 | |
| 8162 | return ret; |
| 8163 | } |
| 8164 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8165 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8166 | const struct built_in_command *x, |
| 8167 | const struct built_in_command *end) |
| 8168 | { |
| 8169 | while (x != end) { |
| 8170 | if (strcmp(name, x->b_cmd) != 0) { |
| 8171 | x++; |
| 8172 | continue; |
| 8173 | } |
| 8174 | debug_printf_exec("found builtin '%s'\n", name); |
| 8175 | return x; |
| 8176 | } |
| 8177 | return NULL; |
| 8178 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8179 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8180 | { |
| 8181 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 8182 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8183 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8184 | { |
| 8185 | const struct built_in_command *x = find_builtin1(name); |
| 8186 | if (x) |
| 8187 | return x; |
| 8188 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 8189 | } |
| 8190 | |
Ron Yorston | 7d1c7d8 | 2022-03-24 12:17:25 +0000 | [diff] [blame] | 8191 | #if ENABLE_HUSH_JOB && ENABLE_FEATURE_EDITING |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 8192 | static const char * FAST_FUNC get_builtin_name(int i) |
| 8193 | { |
| 8194 | if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) { |
| 8195 | return bltins1[i].b_cmd; |
| 8196 | } |
| 8197 | i -= ARRAY_SIZE(bltins1); |
| 8198 | if (i < ARRAY_SIZE(bltins2)) { |
| 8199 | return bltins2[i].b_cmd; |
| 8200 | } |
| 8201 | return NULL; |
| 8202 | } |
| 8203 | #endif |
| 8204 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 8205 | static void remove_nested_vars(void) |
| 8206 | { |
| 8207 | struct variable *cur; |
| 8208 | struct variable **cur_pp; |
| 8209 | |
| 8210 | cur_pp = &G.top_var; |
| 8211 | while ((cur = *cur_pp) != NULL) { |
| 8212 | if (cur->var_nest_level <= G.var_nest_level) { |
| 8213 | cur_pp = &cur->next; |
| 8214 | continue; |
| 8215 | } |
| 8216 | /* Unexport */ |
| 8217 | if (cur->flg_export) { |
| 8218 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 8219 | bb_unsetenv(cur->varstr); |
| 8220 | } |
| 8221 | /* Remove from global list */ |
| 8222 | *cur_pp = cur->next; |
| 8223 | /* Free */ |
| 8224 | if (!cur->max_len) { |
| 8225 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 8226 | free(cur->varstr); |
| 8227 | } |
| 8228 | free(cur); |
| 8229 | } |
| 8230 | } |
| 8231 | |
| 8232 | static void enter_var_nest_level(void) |
| 8233 | { |
| 8234 | G.var_nest_level++; |
| 8235 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 8236 | |
| 8237 | /* Try: f() { echo -n .; f; }; f |
| 8238 | * struct variable::var_nest_level is uint16_t, |
| 8239 | * thus limiting recursion to < 2^16. |
| 8240 | * In any case, with 8 Mbyte stack SEGV happens |
| 8241 | * not too long after 2^16 recursions anyway. |
| 8242 | */ |
| 8243 | if (G.var_nest_level > 0xff00) |
| 8244 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 8245 | } |
| 8246 | |
| 8247 | static void leave_var_nest_level(void) |
| 8248 | { |
| 8249 | G.var_nest_level--; |
| 8250 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 8251 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 8252 | bb_simple_error_msg_and_die("BUG: nesting underflow"); |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 8253 | |
| 8254 | remove_nested_vars(); |
| 8255 | } |
| 8256 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8257 | #if ENABLE_HUSH_FUNCTIONS |
| 8258 | static struct function **find_function_slot(const char *name) |
| 8259 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8260 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8261 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8262 | |
| 8263 | while ((funcp = *funcpp) != NULL) { |
| 8264 | if (strcmp(name, funcp->name) == 0) { |
| 8265 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8266 | break; |
| 8267 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8268 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8269 | } |
| 8270 | return funcpp; |
| 8271 | } |
| 8272 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 8273 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8274 | { |
| 8275 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8276 | return funcp; |
| 8277 | } |
| 8278 | |
| 8279 | /* Note: takes ownership on name ptr */ |
| 8280 | static struct function *new_function(char *name) |
| 8281 | { |
| 8282 | struct function **funcpp = find_function_slot(name); |
| 8283 | struct function *funcp = *funcpp; |
| 8284 | |
| 8285 | if (funcp != NULL) { |
| 8286 | struct command *cmd = funcp->parent_cmd; |
| 8287 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 8288 | if (!cmd) { |
| 8289 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 8290 | free(funcp->name); |
| 8291 | /* Note: if !funcp->body, do not free body_as_string! |
| 8292 | * This is a special case of "-F name body" function: |
| 8293 | * body_as_string was not malloced! */ |
| 8294 | if (funcp->body) { |
| 8295 | free_pipe_list(funcp->body); |
| 8296 | # if !BB_MMU |
| 8297 | free(funcp->body_as_string); |
| 8298 | # endif |
| 8299 | } |
| 8300 | } else { |
| 8301 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 8302 | cmd->argv[0] = funcp->name; |
| 8303 | cmd->group = funcp->body; |
| 8304 | # if !BB_MMU |
| 8305 | cmd->group_as_string = funcp->body_as_string; |
| 8306 | # endif |
| 8307 | } |
| 8308 | } else { |
| 8309 | debug_printf_exec("remembering new function '%s'\n", name); |
| 8310 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 8311 | /*funcp->next = NULL;*/ |
| 8312 | } |
| 8313 | |
| 8314 | funcp->name = name; |
| 8315 | return funcp; |
| 8316 | } |
| 8317 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8318 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8319 | static void unset_func(const char *name) |
| 8320 | { |
| 8321 | struct function **funcpp = find_function_slot(name); |
| 8322 | struct function *funcp = *funcpp; |
| 8323 | |
| 8324 | if (funcp != NULL) { |
| 8325 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 8326 | *funcpp = funcp->next; |
| 8327 | /* funcp is unlinked now, deleting it. |
| 8328 | * Note: if !funcp->body, the function was created by |
| 8329 | * "-F name body", do not free ->body_as_string |
| 8330 | * and ->name as they were not malloced. */ |
| 8331 | if (funcp->body) { |
| 8332 | free_pipe_list(funcp->body); |
| 8333 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8334 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8335 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8336 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8337 | } |
| 8338 | free(funcp); |
| 8339 | } |
| 8340 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 8341 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8342 | |
| 8343 | # if BB_MMU |
| 8344 | #define exec_function(to_free, funcp, argv) \ |
| 8345 | exec_function(funcp, argv) |
| 8346 | # endif |
| 8347 | static void exec_function(char ***to_free, |
| 8348 | const struct function *funcp, |
| 8349 | char **argv) NORETURN; |
| 8350 | static void exec_function(char ***to_free, |
| 8351 | const struct function *funcp, |
| 8352 | char **argv) |
| 8353 | { |
| 8354 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8355 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8356 | |
| 8357 | argv[0] = G.global_argv[0]; |
| 8358 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 8359 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8360 | |
| 8361 | // Example when we are here: "cmd | func" |
| 8362 | // func will run with saved-redirect fds open. |
| 8363 | // $ f() { echo /proc/self/fd/*; } |
| 8364 | // $ true | f |
| 8365 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 8366 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 8367 | // Same in script: |
| 8368 | // $ . ./SCRIPT |
| 8369 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 8370 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 8371 | // They are CLOEXEC so external programs won't see them, but |
| 8372 | // for "more correctness" we might want to close those extra fds here: |
| 8373 | //? close_saved_fds_and_FILE_fds(); |
| 8374 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8375 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8376 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8377 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8378 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 8379 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8380 | /* On MMU, funcp->body is always non-NULL */ |
| 8381 | n = run_list(funcp->body); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8382 | _exit(n); |
| 8383 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8384 | //? close_saved_fds_and_FILE_fds(); |
| 8385 | |
| 8386 | //TODO: check whether "true | func_with_return" works |
| 8387 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8388 | re_execute_shell(to_free, |
| 8389 | funcp->body_as_string, |
| 8390 | G.global_argv[0], |
| 8391 | argv + 1, |
| 8392 | NULL); |
| 8393 | # endif |
| 8394 | } |
| 8395 | |
| 8396 | static int run_function(const struct function *funcp, char **argv) |
| 8397 | { |
| 8398 | int rc; |
| 8399 | save_arg_t sv; |
| 8400 | smallint sv_flg; |
| 8401 | |
| 8402 | save_and_replace_G_args(&sv, argv); |
| 8403 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8404 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8405 | sv_flg = G_flag_return_in_progress; |
| 8406 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8407 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8408 | /* Make "local" variables properly shadow previous ones */ |
| 8409 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8410 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8411 | |
| 8412 | /* On MMU, funcp->body is always non-NULL */ |
| 8413 | # if !BB_MMU |
| 8414 | if (!funcp->body) { |
| 8415 | /* Function defined by -F */ |
| 8416 | parse_and_run_string(funcp->body_as_string); |
| 8417 | rc = G.last_exitcode; |
| 8418 | } else |
| 8419 | # endif |
| 8420 | { |
| 8421 | rc = run_list(funcp->body); |
| 8422 | } |
| 8423 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 8424 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8425 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8426 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8427 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 8428 | # if ENABLE_HUSH_TRAP |
| 8429 | debug_printf_exec("G.return_exitcode=-1\n"); |
| 8430 | G.return_exitcode = -1; /* invalidate stashed return value */ |
| 8431 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8432 | |
| 8433 | restore_G_args(&sv, argv); |
| 8434 | |
| 8435 | return rc; |
| 8436 | } |
| 8437 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 8438 | |
| 8439 | |
| 8440 | #if BB_MMU |
| 8441 | #define exec_builtin(to_free, x, argv) \ |
| 8442 | exec_builtin(x, argv) |
| 8443 | #else |
| 8444 | #define exec_builtin(to_free, x, argv) \ |
| 8445 | exec_builtin(to_free, argv) |
| 8446 | #endif |
| 8447 | static void exec_builtin(char ***to_free, |
| 8448 | const struct built_in_command *x, |
| 8449 | char **argv) NORETURN; |
| 8450 | static void exec_builtin(char ***to_free, |
| 8451 | const struct built_in_command *x, |
| 8452 | char **argv) |
| 8453 | { |
| 8454 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8455 | int rcode; |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8456 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8457 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8458 | fflush_all(); |
| 8459 | _exit(rcode); |
| 8460 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8461 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8462 | /* On NOMMU, we must never block! |
| 8463 | * Example: { sleep 99 | read line; } & echo Ok |
| 8464 | */ |
| 8465 | re_execute_shell(to_free, |
| 8466 | argv[0], |
| 8467 | G.global_argv[0], |
| 8468 | G.global_argv + 1, |
| 8469 | argv); |
| 8470 | #endif |
| 8471 | } |
| 8472 | |
| 8473 | |
| 8474 | static void execvp_or_die(char **argv) NORETURN; |
| 8475 | static void execvp_or_die(char **argv) |
| 8476 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8477 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8478 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8479 | /* Don't propagate SIG_IGN to the child */ |
| 8480 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8481 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8482 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8483 | e = 2; |
| 8484 | if (errno == EACCES) e = 126; |
| 8485 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8486 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 8487 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8488 | } |
| 8489 | |
| 8490 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8491 | static void x_mode_print_optionally_squoted(const char *str) |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8492 | { |
| 8493 | unsigned len; |
| 8494 | const char *cp; |
| 8495 | |
| 8496 | cp = str; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8497 | |
| 8498 | /* the set of chars which-cause-string-to-be-squoted mimics bash */ |
| 8499 | /* test a char with: bash -c 'set -x; echo "CH"' */ |
| 8500 | if (str[strcspn(str, "\\\"'`$(){}[]<>;#&|~*?!^" |
| 8501 | " " "\001\002\003\004\005\006\007" |
| 8502 | "\010\011\012\013\014\015\016\017" |
| 8503 | "\020\021\022\023\024\025\026\027" |
| 8504 | "\030\031\032\033\034\035\036\037" |
| 8505 | ) |
| 8506 | ] == '\0' |
| 8507 | ) { |
| 8508 | /* string has no special chars */ |
| 8509 | x_mode_addstr(str); |
| 8510 | return; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8511 | } |
| 8512 | |
| 8513 | cp = str; |
| 8514 | for (;;) { |
| 8515 | /* print '....' up to EOL or first squote */ |
| 8516 | len = (int)(strchrnul(cp, '\'') - cp); |
| 8517 | if (len != 0) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8518 | x_mode_addchr('\''); |
| 8519 | x_mode_addblock(cp, len); |
| 8520 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8521 | cp += len; |
| 8522 | } |
| 8523 | if (*cp == '\0') |
| 8524 | break; |
| 8525 | /* string contains squote(s), print them as \' */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8526 | x_mode_addchr('\\'); |
| 8527 | x_mode_addchr('\''); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8528 | cp++; |
| 8529 | } |
| 8530 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8531 | static void dump_cmd_in_x_mode(char **argv) |
| 8532 | { |
| 8533 | if (G_x_mode && argv) { |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 8534 | unsigned n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8535 | |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8536 | /* "+[+++...][ cmd...]\n\0" */ |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8537 | x_mode_prefix(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8538 | n = 0; |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8539 | while (argv[n]) { |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8540 | x_mode_addchr(' '); |
| 8541 | if (argv[n][0] == '\0') { |
| 8542 | x_mode_addchr('\''); |
| 8543 | x_mode_addchr('\''); |
| 8544 | } else { |
| 8545 | x_mode_print_optionally_squoted(argv[n]); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 8546 | } |
| 8547 | n++; |
| 8548 | } |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 8549 | x_mode_flush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8550 | } |
| 8551 | } |
| 8552 | #else |
| 8553 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 8554 | #endif |
| 8555 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8556 | #if ENABLE_HUSH_COMMAND |
| 8557 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 8558 | { |
| 8559 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8560 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8561 | if (!opt_vV) |
| 8562 | return; |
| 8563 | |
| 8564 | to_free = NULL; |
| 8565 | if (!explanation) { |
| 8566 | char *path = getenv("PATH"); |
| 8567 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8568 | if (!explanation) |
| 8569 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8570 | if (opt_vV != 'V') |
| 8571 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 8572 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8573 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8574 | free(to_free); |
| 8575 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 8576 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8577 | } |
| 8578 | #else |
| 8579 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 8580 | #endif |
| 8581 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8582 | #if BB_MMU |
| 8583 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 8584 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 8585 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 8586 | pseudo_exec(command, argv_expanded) |
| 8587 | #endif |
| 8588 | |
| 8589 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 8590 | * Never returns. |
| 8591 | * Don't exit() here. If you don't exec, use _exit instead. |
| 8592 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8593 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 8594 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8595 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8596 | char **argv, int assignment_cnt, |
| 8597 | char **argv_expanded) NORETURN; |
| 8598 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 8599 | char **argv, int assignment_cnt, |
| 8600 | char **argv_expanded) |
| 8601 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8602 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8603 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8604 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8605 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 8606 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8607 | |
| 8608 | new_env = expand_assignments(argv, assignment_cnt); |
| 8609 | dump_cmd_in_x_mode(new_env); |
| 8610 | |
| 8611 | if (!argv[assignment_cnt]) { |
| 8612 | /* Case when we are here: ... | var=val | ... |
| 8613 | * (note that we do not exit early, i.e., do not optimize out |
| 8614 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 8615 | */ |
| 8616 | free_strings(new_env); |
Denys Vlasenko | db5546c | 2022-01-05 22:16:06 +0100 | [diff] [blame] | 8617 | _exit_SUCCESS(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8618 | } |
| 8619 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8620 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8621 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8622 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8623 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8624 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8625 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8626 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8627 | set_vars_and_save_old(new_env); |
| 8628 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8629 | |
| 8630 | if (argv_expanded) { |
| 8631 | argv = argv_expanded; |
| 8632 | } else { |
| 8633 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 8634 | #if !BB_MMU |
| 8635 | nommu_save->argv = argv; |
| 8636 | #endif |
| 8637 | } |
| 8638 | dump_cmd_in_x_mode(argv); |
| 8639 | |
| 8640 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8641 | if (strchr(argv[0], '/') != NULL) |
| 8642 | goto skip; |
| 8643 | #endif |
| 8644 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8645 | #if ENABLE_HUSH_FUNCTIONS |
| 8646 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8647 | funcp = find_function(argv[0]); |
| 8648 | if (funcp) |
| 8649 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8650 | #endif |
| 8651 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8652 | #if ENABLE_HUSH_COMMAND |
| 8653 | /* "command BAR": run BAR without looking it up among functions |
| 8654 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 8655 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 8656 | */ |
| 8657 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 8658 | char *p; |
| 8659 | |
| 8660 | argv++; |
| 8661 | p = *argv; |
| 8662 | if (p[0] != '-' || !p[1]) |
| 8663 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 8664 | |
| 8665 | for (;;) { |
| 8666 | p++; |
| 8667 | switch (*p) { |
| 8668 | case '\0': |
| 8669 | argv++; |
| 8670 | p = *argv; |
| 8671 | if (p[0] != '-' || !p[1]) |
| 8672 | goto after_opts; |
| 8673 | continue; /* next arg is also -opts, process it too */ |
| 8674 | case 'v': |
| 8675 | case 'V': |
| 8676 | opt_vV = *p; |
| 8677 | continue; |
| 8678 | default: |
| 8679 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 8680 | } |
| 8681 | } |
| 8682 | } |
| 8683 | after_opts: |
| 8684 | # if ENABLE_HUSH_FUNCTIONS |
| 8685 | if (opt_vV && find_function(argv[0])) |
| 8686 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 8687 | # endif |
| 8688 | #endif |
| 8689 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8690 | /* Check if the command matches any of the builtins. |
| 8691 | * Depending on context, this might be redundant. But it's |
| 8692 | * easier to waste a few CPU cycles than it is to figure out |
| 8693 | * if this is one of those cases. |
| 8694 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8695 | /* Why "BB_MMU ? :" difference in logic? - |
| 8696 | * On NOMMU, it is more expensive to re-execute shell |
| 8697 | * just in order to run echo or test builtin. |
| 8698 | * It's better to skip it here and run corresponding |
| 8699 | * non-builtin later. */ |
| 8700 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 8701 | if (x) { |
| 8702 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 8703 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8704 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8705 | |
| 8706 | #if ENABLE_FEATURE_SH_STANDALONE |
| 8707 | /* Check if the command matches any busybox applets */ |
| 8708 | { |
| 8709 | int a = find_applet_by_name(argv[0]); |
| 8710 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8711 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8712 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 8713 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 8714 | /* Do not leak open fds from opened script files etc. |
| 8715 | * Testcase: interactive "ls -l /proc/self/fd" |
| 8716 | * should not show tty fd open. |
| 8717 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 8718 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8719 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 8720 | //This casuses test failures in |
| 8721 | //redir_children_should_not_see_saved_fd_2.tests |
| 8722 | //redir_children_should_not_see_saved_fd_3.tests |
| 8723 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8724 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 8725 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 8726 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 8727 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8728 | } |
| 8729 | # endif |
| 8730 | /* Re-exec ourselves */ |
| 8731 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 8732 | /* Don't propagate SIG_IGN to the child */ |
| 8733 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 8734 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8735 | execv(bb_busybox_exec_path, argv); |
| 8736 | /* If they called chroot or otherwise made the binary no longer |
| 8737 | * executable, fall through */ |
| 8738 | } |
| 8739 | } |
| 8740 | #endif |
| 8741 | |
| 8742 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 8743 | skip: |
| 8744 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 8745 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8746 | execvp_or_die(argv); |
| 8747 | } |
| 8748 | |
| 8749 | /* Called after [v]fork() in run_pipe |
| 8750 | */ |
| 8751 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8752 | struct command *command, |
| 8753 | char **argv_expanded) NORETURN; |
| 8754 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 8755 | struct command *command, |
| 8756 | char **argv_expanded) |
| 8757 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 8758 | #if ENABLE_HUSH_FUNCTIONS |
| 8759 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8760 | /* Ignore funcdefs in pipes: |
| 8761 | * true | f() { cmd } |
| 8762 | */ |
| 8763 | _exit(0); |
| 8764 | } |
| 8765 | #endif |
| 8766 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8767 | if (command->argv) { |
| 8768 | pseudo_exec_argv(nommu_save, command->argv, |
| 8769 | command->assignment_cnt, argv_expanded); |
| 8770 | } |
| 8771 | |
| 8772 | if (command->group) { |
| 8773 | /* Cases when we are here: |
| 8774 | * ( list ) |
| 8775 | * { list } & |
| 8776 | * ... | ( list ) | ... |
| 8777 | * ... | { list } | ... |
| 8778 | */ |
| 8779 | #if BB_MMU |
| 8780 | int rcode; |
| 8781 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 8782 | reset_traps_to_defaults(); |
| 8783 | rcode = run_list(command->group); |
| 8784 | /* OK to leak memory by not calling free_pipe_list, |
| 8785 | * since this process is about to exit */ |
| 8786 | _exit(rcode); |
| 8787 | #else |
| 8788 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 8789 | command->group_as_string, |
| 8790 | G.global_argv[0], |
| 8791 | G.global_argv + 1, |
| 8792 | NULL); |
| 8793 | #endif |
| 8794 | } |
| 8795 | |
| 8796 | /* Case when we are here: ... | >file */ |
| 8797 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denys Vlasenko | db5546c | 2022-01-05 22:16:06 +0100 | [diff] [blame] | 8798 | _exit_SUCCESS(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8799 | } |
| 8800 | |
| 8801 | #if ENABLE_HUSH_JOB |
| 8802 | static const char *get_cmdtext(struct pipe *pi) |
| 8803 | { |
| 8804 | char **argv; |
| 8805 | char *p; |
| 8806 | int len; |
| 8807 | |
| 8808 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 8809 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 8810 | * On subsequent bg argv is trashed, but we won't use it */ |
| 8811 | if (pi->cmdtext) |
| 8812 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8813 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8814 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8815 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8816 | pi->cmdtext = xzalloc(1); |
| 8817 | return pi->cmdtext; |
| 8818 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8819 | len = 0; |
| 8820 | do { |
| 8821 | len += strlen(*argv) + 1; |
| 8822 | } while (*++argv); |
| 8823 | p = xmalloc(len); |
| 8824 | pi->cmdtext = p; |
| 8825 | argv = pi->cmds[0].argv; |
| 8826 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8827 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8828 | *p++ = ' '; |
| 8829 | } while (*++argv); |
| 8830 | p[-1] = '\0'; |
| 8831 | return pi->cmdtext; |
| 8832 | } |
| 8833 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8834 | static void remove_job_from_table(struct pipe *pi) |
| 8835 | { |
| 8836 | struct pipe *prev_pipe; |
| 8837 | |
| 8838 | if (pi == G.job_list) { |
| 8839 | G.job_list = pi->next; |
| 8840 | } else { |
| 8841 | prev_pipe = G.job_list; |
| 8842 | while (prev_pipe->next != pi) |
| 8843 | prev_pipe = prev_pipe->next; |
| 8844 | prev_pipe->next = pi->next; |
| 8845 | } |
| 8846 | G.last_jobid = 0; |
| 8847 | if (G.job_list) |
| 8848 | G.last_jobid = G.job_list->jobid; |
| 8849 | } |
| 8850 | |
| 8851 | static void delete_finished_job(struct pipe *pi) |
| 8852 | { |
| 8853 | remove_job_from_table(pi); |
| 8854 | free_pipe(pi); |
| 8855 | } |
| 8856 | |
| 8857 | static void clean_up_last_dead_job(void) |
| 8858 | { |
| 8859 | if (G.job_list && !G.job_list->alive_cmds) |
| 8860 | delete_finished_job(G.job_list); |
| 8861 | } |
| 8862 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8863 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8864 | { |
| 8865 | struct pipe *job, **jobp; |
| 8866 | int i; |
| 8867 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8868 | clean_up_last_dead_job(); |
| 8869 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8870 | /* Find the end of the list, and find next job ID to use */ |
| 8871 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8872 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8873 | while ((job = *jobp) != NULL) { |
| 8874 | if (job->jobid > i) |
| 8875 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8876 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8877 | } |
| 8878 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8879 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8880 | /* Create a new job struct at the end */ |
| 8881 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8882 | job->next = NULL; |
| 8883 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 8884 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 8885 | for (i = 0; i < pi->num_cmds; i++) { |
| 8886 | job->cmds[i].pid = pi->cmds[i].pid; |
| 8887 | /* all other fields are not used and stay zero */ |
| 8888 | } |
| 8889 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 8890 | |
| 8891 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 8892 | 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] | 8893 | G.last_jobid = job->jobid; |
| 8894 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8895 | #endif /* JOB */ |
| 8896 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8897 | static int job_exited_or_stopped(struct pipe *pi) |
| 8898 | { |
| 8899 | int rcode, i; |
| 8900 | |
| 8901 | if (pi->alive_cmds != pi->stopped_cmds) |
| 8902 | return -1; |
| 8903 | |
| 8904 | /* All processes in fg pipe have exited or stopped */ |
| 8905 | rcode = 0; |
| 8906 | i = pi->num_cmds; |
| 8907 | while (--i >= 0) { |
| 8908 | rcode = pi->cmds[i].cmd_exitcode; |
| 8909 | /* usually last process gives overall exitstatus, |
| 8910 | * but with "set -o pipefail", last *failed* process does */ |
| 8911 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 8912 | break; |
| 8913 | } |
| 8914 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8915 | return rcode; |
| 8916 | } |
| 8917 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8918 | 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] | 8919 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8920 | #if ENABLE_HUSH_JOB |
| 8921 | struct pipe *pi; |
| 8922 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8923 | int i, dead; |
| 8924 | |
| 8925 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 8926 | |
| 8927 | #if DEBUG_JOBS |
| 8928 | if (WIFSTOPPED(status)) |
| 8929 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8930 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8931 | if (WIFSIGNALED(status)) |
| 8932 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8933 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8934 | if (WIFEXITED(status)) |
| 8935 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8936 | childpid, WEXITSTATUS(status)); |
| 8937 | #endif |
| 8938 | /* Were we asked to wait for a fg pipe? */ |
| 8939 | if (fg_pipe) { |
| 8940 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8941 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8942 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8943 | int rcode; |
| 8944 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8945 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8946 | if (fg_pipe->cmds[i].pid != childpid) |
| 8947 | continue; |
| 8948 | if (dead) { |
| 8949 | int ex; |
| 8950 | fg_pipe->cmds[i].pid = 0; |
| 8951 | fg_pipe->alive_cmds--; |
| 8952 | ex = WEXITSTATUS(status); |
| 8953 | /* bash prints killer signal's name for *last* |
| 8954 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8955 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8956 | */ |
| 8957 | if (WIFSIGNALED(status)) { |
| 8958 | int sig = WTERMSIG(status); |
Denys Vlasenko | 77a51a2 | 2020-12-29 16:53:11 +0100 | [diff] [blame] | 8959 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | b65d6cb | 2020-10-24 03:33:32 +0200 | [diff] [blame] | 8960 | if (G.run_list_level == 1 |
| 8961 | /* ^^^^^ Do not print in nested contexts, example: |
| 8962 | * echo `sleep 1; sh -c 'kill -9 $$'` - prints "137", NOT "Killed 137" |
| 8963 | */ |
| 8964 | && i == fg_pipe->num_cmds-1 |
| 8965 | ) { |
Denys Vlasenko | e16f7eb | 2020-10-24 04:26:43 +0200 | [diff] [blame] | 8966 | /* strsignal() is for bash compat. ~600 bloat versus bbox's get_signame() */ |
| 8967 | puts(sig == SIGINT || sig == SIGPIPE ? "" : strsignal(sig)); |
Denys Vlasenko | b65d6cb | 2020-10-24 03:33:32 +0200 | [diff] [blame] | 8968 | } |
Denys Vlasenko | 77a51a2 | 2020-12-29 16:53:11 +0100 | [diff] [blame] | 8969 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8970 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 8971 | /* MIPS has 128 sigs (1..128), if sig==128, |
| 8972 | * 128 + sig would result in exitcode 256 -> 0! |
| 8973 | */ |
| 8974 | ex = 128 | sig; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8975 | } |
| 8976 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8977 | } else { |
| 8978 | fg_pipe->stopped_cmds++; |
| 8979 | } |
| 8980 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8981 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8982 | rcode = job_exited_or_stopped(fg_pipe); |
| 8983 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8984 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8985 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8986 | * and "killall -STOP cat" */ |
| 8987 | if (G_interactive_fd) { |
| 8988 | #if ENABLE_HUSH_JOB |
| 8989 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8990 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8991 | #endif |
| 8992 | return rcode; |
| 8993 | } |
| 8994 | if (fg_pipe->alive_cmds == 0) |
| 8995 | return rcode; |
| 8996 | } |
| 8997 | /* There are still running processes in the fg_pipe */ |
| 8998 | return -1; |
| 8999 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 9000 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9001 | } |
| 9002 | |
| 9003 | #if ENABLE_HUSH_JOB |
| 9004 | /* We were asked to wait for bg or orphaned children */ |
| 9005 | /* No need to remember exitcode in this case */ |
| 9006 | for (pi = G.job_list; pi; pi = pi->next) { |
| 9007 | for (i = 0; i < pi->num_cmds; i++) { |
| 9008 | if (pi->cmds[i].pid == childpid) |
| 9009 | goto found_pi_and_prognum; |
| 9010 | } |
| 9011 | } |
| 9012 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 9013 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 9014 | return -1; /* this wasn't a process from fg_pipe */ |
| 9015 | |
| 9016 | found_pi_and_prognum: |
| 9017 | if (dead) { |
| 9018 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9019 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9020 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 9021 | /* NB: not 128 + sig, MIPS has sig 128 */ |
| 9022 | rcode = 128 | WTERMSIG(status); |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9023 | pi->cmds[i].cmd_exitcode = rcode; |
| 9024 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 9025 | G.last_bg_pid_exitcode = rcode; |
| 9026 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9027 | pi->alive_cmds--; |
| 9028 | if (!pi->alive_cmds) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 9029 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 9030 | G.dead_job_exitcode = job_exited_or_stopped(pi); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 9031 | # endif |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 9032 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9033 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 9034 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 9035 | delete_finished_job(pi); |
| 9036 | } else { |
| 9037 | /* |
| 9038 | * bash deletes finished jobs from job table only in interactive mode, |
| 9039 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 9040 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 9041 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 9042 | * We only retain one "dead" job, if it's the single job on the list. |
| 9043 | * This covers most of real-world scenarios where this is useful. |
| 9044 | */ |
| 9045 | if (pi != G.job_list) |
| 9046 | delete_finished_job(pi); |
| 9047 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9048 | } |
| 9049 | } else { |
| 9050 | /* child stopped */ |
| 9051 | pi->stopped_cmds++; |
| 9052 | } |
| 9053 | #endif |
| 9054 | return -1; /* this wasn't a process from fg_pipe */ |
| 9055 | } |
| 9056 | |
| 9057 | /* Check to see if any processes have exited -- if they have, |
| 9058 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9059 | * |
| 9060 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 9061 | * Return its exitcode or zero if stopped. |
| 9062 | * |
| 9063 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 9064 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 9065 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 9066 | * or 0 if no children changed status. |
| 9067 | * |
| 9068 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 9069 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 9070 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9071 | */ |
| 9072 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 9073 | { |
| 9074 | int attributes; |
| 9075 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9076 | int rcode = 0; |
| 9077 | |
| 9078 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 9079 | |
| 9080 | attributes = WUNTRACED; |
| 9081 | if (fg_pipe == NULL) |
| 9082 | attributes |= WNOHANG; |
| 9083 | |
| 9084 | errno = 0; |
| 9085 | #if ENABLE_HUSH_FAST |
| 9086 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 9087 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 9088 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 9089 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 9090 | /* Avoid doing waitpid syscall if possible */ |
| 9091 | if (!G.we_have_children) { |
| 9092 | errno = ECHILD; |
| 9093 | return -1; |
| 9094 | } |
| 9095 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 9096 | /* We have children, but they did not exit |
| 9097 | * or stop yet (we saw no SIGCHLD) */ |
| 9098 | return 0; |
| 9099 | } |
| 9100 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 9101 | } |
| 9102 | #endif |
| 9103 | |
| 9104 | /* Do we do this right? |
| 9105 | * bash-3.00# sleep 20 | false |
| 9106 | * <ctrl-Z pressed> |
| 9107 | * [3]+ Stopped sleep 20 | false |
| 9108 | * bash-3.00# echo $? |
| 9109 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 9110 | * [hush 1.14.0: yes we do it right] |
| 9111 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9112 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9113 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9114 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9115 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9116 | i = G.count_SIGCHLD; |
| 9117 | #endif |
| 9118 | childpid = waitpid(-1, &status, attributes); |
| 9119 | if (childpid <= 0) { |
| 9120 | if (childpid && errno != ECHILD) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 9121 | bb_simple_perror_msg("waitpid"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9122 | #if ENABLE_HUSH_FAST |
| 9123 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 9124 | G.we_have_children = (childpid == 0); |
| 9125 | G.handled_SIGCHLD = i; |
| 9126 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 9127 | } |
| 9128 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9129 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 9130 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9131 | break; |
| 9132 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9133 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 9134 | if (rcode >= 0) { |
| 9135 | /* fg_pipe exited or stopped */ |
| 9136 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9137 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 9138 | if (childpid == waitfor_pid) { /* "wait PID" */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9139 | 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] | 9140 | rcode = WEXITSTATUS(status); |
| 9141 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 9142 | rcode = 128 | WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 9143 | if (WIFSTOPPED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 9144 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 | stopsig */ |
| 9145 | rcode = 128 | WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9146 | rcode++; |
| 9147 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9148 | } |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 9149 | #if ENABLE_HUSH_BASH_COMPAT |
| 9150 | if (-1 == waitfor_pid /* "wait -n" (wait for any one job) */ |
| 9151 | && G.dead_job_exitcode >= 0 /* some job did finish */ |
| 9152 | ) { |
| 9153 | debug_printf_exec("waitfor_pid:-1\n"); |
| 9154 | rcode = G.dead_job_exitcode + 1; |
| 9155 | break; |
| 9156 | } |
| 9157 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9158 | /* This wasn't one of our processes, or */ |
| 9159 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9160 | } /* while (waitpid succeeds)... */ |
| 9161 | |
| 9162 | return rcode; |
| 9163 | } |
| 9164 | |
| 9165 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 9166 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9167 | { |
| 9168 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9169 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9170 | if (G_saved_tty_pgrp) { |
| 9171 | /* Job finished, move the shell to the foreground */ |
| 9172 | p = getpgrp(); /* our process group id */ |
| 9173 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 9174 | tcsetpgrp(G_interactive_fd, p); |
| 9175 | } |
| 9176 | return rcode; |
| 9177 | } |
| 9178 | #endif |
| 9179 | |
| 9180 | /* Start all the jobs, but don't wait for anything to finish. |
| 9181 | * See checkjobs(). |
| 9182 | * |
| 9183 | * Return code is normally -1, when the caller has to wait for children |
| 9184 | * to finish to determine the exit status of the pipe. If the pipe |
| 9185 | * is a simple builtin command, however, the action is done by the |
| 9186 | * time run_pipe returns, and the exit code is provided as the |
| 9187 | * return value. |
| 9188 | * |
| 9189 | * Returns -1 only if started some children. IOW: we have to |
| 9190 | * mask out retvals of builtins etc with 0xff! |
| 9191 | * |
| 9192 | * The only case when we do not need to [v]fork is when the pipe |
| 9193 | * is single, non-backgrounded, non-subshell command. Examples: |
| 9194 | * cmd ; ... { list } ; ... |
| 9195 | * cmd && ... { list } && ... |
| 9196 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 9197 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9198 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 9199 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 9200 | * |
| 9201 | * Cases when we must fork: |
| 9202 | * non-single: cmd | cmd |
| 9203 | * backgrounded: cmd & { list } & |
| 9204 | * subshell: ( list ) [&] |
| 9205 | */ |
| 9206 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 9207 | #define redirect_and_varexp_helper(command, sqp, argv_expanded) \ |
| 9208 | redirect_and_varexp_helper(command, sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9209 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9210 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9211 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9212 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9213 | char **argv_expanded) |
| 9214 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9215 | /* Assignments occur before redirects. Try: |
| 9216 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 9217 | */ |
| 9218 | |
| 9219 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 9220 | dump_cmd_in_x_mode(new_env); |
| 9221 | dump_cmd_in_x_mode(argv_expanded); |
| 9222 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 9223 | set_vars_and_save_old(new_env); |
| 9224 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9225 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9226 | } |
| 9227 | static NOINLINE int run_pipe(struct pipe *pi) |
| 9228 | { |
| 9229 | static const char *const null_ptr = NULL; |
| 9230 | |
| 9231 | int cmd_no; |
| 9232 | int next_infd; |
| 9233 | struct command *command; |
| 9234 | char **argv_expanded; |
| 9235 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9236 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9237 | int rcode; |
| 9238 | |
| 9239 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 9240 | debug_enter(); |
| 9241 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9242 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 9243 | * Result should be 3 lines: q w e, qwe, q w e |
| 9244 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9245 | if (G.ifs_whitespace != G.ifs) |
| 9246 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9247 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9248 | if (G.ifs) { |
| 9249 | char *p; |
| 9250 | G.ifs_whitespace = (char*)G.ifs; |
| 9251 | p = skip_whitespace(G.ifs); |
| 9252 | if (*p) { |
| 9253 | /* Not all $IFS is whitespace */ |
| 9254 | char *d; |
| 9255 | int len = p - G.ifs; |
| 9256 | p = skip_non_whitespace(p); |
| 9257 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 9258 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 9259 | while (*p) { |
| 9260 | if (isspace(*p)) |
| 9261 | *d++ = *p; |
| 9262 | p++; |
| 9263 | } |
| 9264 | *d = '\0'; |
| 9265 | } |
| 9266 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9267 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 9268 | G.ifs_whitespace = (char*)G.ifs; |
| 9269 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 9270 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9271 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 9272 | pi->stopped_cmds = 0; |
| 9273 | command = &pi->cmds[0]; |
| 9274 | argv_expanded = NULL; |
| 9275 | |
| 9276 | if (pi->num_cmds != 1 |
| 9277 | || pi->followup == PIPE_BG |
| 9278 | || command->cmd_type == CMD_SUBSHELL |
| 9279 | ) { |
| 9280 | goto must_fork; |
| 9281 | } |
| 9282 | |
| 9283 | pi->alive_cmds = 1; |
| 9284 | |
| 9285 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 9286 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 9287 | |
| 9288 | if (command->group) { |
| 9289 | #if ENABLE_HUSH_FUNCTIONS |
| 9290 | if (command->cmd_type == CMD_FUNCDEF) { |
| 9291 | /* "executing" func () { list } */ |
| 9292 | struct function *funcp; |
| 9293 | |
| 9294 | funcp = new_function(command->argv[0]); |
| 9295 | /* funcp->name is already set to argv[0] */ |
| 9296 | funcp->body = command->group; |
| 9297 | # if !BB_MMU |
| 9298 | funcp->body_as_string = command->group_as_string; |
| 9299 | command->group_as_string = NULL; |
| 9300 | # endif |
| 9301 | command->group = NULL; |
| 9302 | command->argv[0] = NULL; |
| 9303 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 9304 | funcp->parent_cmd = command; |
| 9305 | command->child_func = funcp; |
| 9306 | |
| 9307 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 9308 | debug_leave(); |
| 9309 | return EXIT_SUCCESS; |
| 9310 | } |
| 9311 | #endif |
| 9312 | /* { list } */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 9313 | debug_printf_exec("non-subshell group\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9314 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9315 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9316 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 9317 | //FIXME: we need to pass squirrel down into run_list() |
| 9318 | //for SH_STANDALONE case, or else this construct: |
| 9319 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 9320 | //has no way of closing saved fd#1 for "find", |
| 9321 | //and in SH_STANDALONE mode, "find" is not execed, |
| 9322 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9323 | rcode = run_list(command->group) & 0xff; |
| 9324 | } |
| 9325 | restore_redirects(squirrel); |
| 9326 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9327 | debug_leave(); |
| 9328 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 9329 | return rcode; |
| 9330 | } |
| 9331 | |
| 9332 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 9333 | { |
| 9334 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9335 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 9336 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9337 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9338 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9339 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9340 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 9341 | G.execute_lineno = command->lineno; |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 9342 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9343 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9344 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9345 | /* Assignments, but no command. |
| 9346 | * Ensure redirects take effect (that is, create files). |
| 9347 | * Try "a=t >file" |
| 9348 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9349 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9350 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9351 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9352 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9353 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9354 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9355 | /* Set shell variables */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9356 | i = 0; |
| 9357 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9358 | char *p = expand_string_to_string(argv[i], |
| 9359 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9360 | /*unbackslash:*/ 1 |
| 9361 | ); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9362 | #if ENABLE_HUSH_MODE_X |
| 9363 | if (G_x_mode) { |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9364 | char *eq; |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9365 | if (i == 0) |
| 9366 | x_mode_prefix(); |
| 9367 | x_mode_addchr(' '); |
Denys Vlasenko | 4b70c92 | 2018-07-27 17:42:38 +0200 | [diff] [blame] | 9368 | eq = strchrnul(p, '='); |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 9369 | if (*eq) eq++; |
| 9370 | x_mode_addblock(p, (eq - p)); |
| 9371 | x_mode_print_optionally_squoted(eq); |
| 9372 | x_mode_flush(); |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 9373 | } |
| 9374 | #endif |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9375 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9376 | if (set_local_var(p, /*flag:*/ 0)) { |
| 9377 | /* assignment to readonly var / putenv error? */ |
| 9378 | rcode = 1; |
| 9379 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9380 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9381 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9382 | /* Redirect error sets $? to 1. Otherwise, |
| 9383 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9384 | * Else, clear $?: |
| 9385 | * false; q=`exit 2`; echo $? - should print 2 |
| 9386 | * false; x=1; echo $? - should print 0 |
| 9387 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 9388 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9389 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 9390 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9391 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9392 | debug_leave(); |
| 9393 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 9394 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9395 | } |
| 9396 | |
| 9397 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | d2241f5 | 2020-10-31 03:34:07 +0100 | [diff] [blame] | 9398 | #if defined(CMD_TEST2_SINGLEWORD_NOGLOB) |
| 9399 | if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB) |
| 9400 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 9401 | else |
| 9402 | #endif |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 9403 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9404 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9405 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9406 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9407 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9408 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9409 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9410 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9411 | if (!argv_expanded[0]) { |
| 9412 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 9413 | /* `false` still has to set exitcode 1 */ |
| 9414 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9415 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9416 | } |
| 9417 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9418 | old_vars = NULL; |
| 9419 | sv_shadowed = G.shadowed_vars_pp; |
| 9420 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9421 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9422 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 9423 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 9424 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 9425 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9426 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9427 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 9428 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9429 | /* |
| 9430 | * Variable assignments are executed, but then "forgotten": |
| 9431 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 9432 | * sleeps, but prints nothing. |
| 9433 | */ |
| 9434 | enter_var_nest_level(); |
| 9435 | G.shadowed_vars_pp = &old_vars; |
Denys Vlasenko | 945e9b0 | 2018-07-24 18:01:22 +0200 | [diff] [blame] | 9436 | rcode = redirect_and_varexp_helper(command, |
| 9437 | /*squirrel:*/ ERR_PTR, |
| 9438 | argv_expanded |
| 9439 | ); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9440 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9441 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9442 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9443 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9444 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9445 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9446 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 9447 | * a=b true; env | grep ^a= |
| 9448 | */ |
| 9449 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9450 | /* Collect all variables "shadowed" by helper |
| 9451 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 9452 | * into old_vars list: |
| 9453 | */ |
| 9454 | G.shadowed_vars_pp = &old_vars; |
| 9455 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9456 | if (rcode == 0) { |
| 9457 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9458 | /* Do not collect *to old_vars list* vars shadowed |
| 9459 | * by e.g. "local VAR" builtin (collect them |
| 9460 | * in the previously nested list instead): |
| 9461 | * don't want them to be restored immediately |
| 9462 | * after "local" completes. |
| 9463 | */ |
| 9464 | G.shadowed_vars_pp = sv_shadowed; |
| 9465 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9466 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 9467 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9468 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9469 | rcode = x->b_function(argv_expanded) & 0xff; |
| 9470 | fflush_all(); |
| 9471 | } |
| 9472 | #if ENABLE_HUSH_FUNCTIONS |
| 9473 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9474 | debug_printf_exec(": function '%s' '%s'...\n", |
| 9475 | funcp->name, argv_expanded[1]); |
| 9476 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9477 | /* |
| 9478 | * But do collect *to old_vars list* vars shadowed |
| 9479 | * within function execution. To that end, restore |
| 9480 | * this pointer _after_ function run: |
| 9481 | */ |
| 9482 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9483 | } |
| 9484 | #endif |
| 9485 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9486 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9487 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9488 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9489 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 9490 | goto must_fork; |
| 9491 | |
| 9492 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 9493 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 9494 | G.shadowed_vars_pp = &old_vars; |
| 9495 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 9496 | G.shadowed_vars_pp = sv_shadowed; |
| 9497 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9498 | if (rcode == 0) { |
| 9499 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 9500 | argv_expanded[0], argv_expanded[1]); |
| 9501 | /* |
| 9502 | * Note: signals (^C) can't interrupt here. |
| 9503 | * We remember them and they will be acted upon |
| 9504 | * after applet returns. |
| 9505 | * This makes applets which can run for a long time |
| 9506 | * and/or wait for user input ineligible for NOFORK: |
| 9507 | * for example, "yes" or "rm" (rm -i waits for input). |
| 9508 | */ |
| 9509 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9510 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 9511 | } else |
| 9512 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9513 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 9514 | restore_redirects(squirrel); |
| 9515 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9516 | leave_var_nest_level(); |
| 9517 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9518 | |
| 9519 | /* |
| 9520 | * Try "usleep 99999999" + ^C + "echo $?" |
| 9521 | * with FEATURE_SH_NOFORK=y. |
| 9522 | */ |
| 9523 | if (!funcp) { |
| 9524 | /* It was builtin or nofork. |
| 9525 | * if this would be a real fork/execed program, |
| 9526 | * it should have died if a fatal sig was received. |
| 9527 | * But OTOH, there was no separate process, |
| 9528 | * the sig was sent to _shell_, not to non-existing |
| 9529 | * child. |
| 9530 | * Let's just handle ^C only, this one is obvious: |
| 9531 | * we aren't ok with exitcode 0 when ^C was pressed |
| 9532 | * during builtin/nofork. |
| 9533 | */ |
| 9534 | if (sigismember(&G.pending_set, SIGINT)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 9535 | rcode = 128 | SIGINT; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9536 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 9537 | free(argv_expanded); |
| 9538 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 9539 | debug_leave(); |
| 9540 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 9541 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9542 | } |
| 9543 | |
| 9544 | must_fork: |
| 9545 | /* NB: argv_expanded may already be created, and that |
| 9546 | * might include `cmd` runs! Do not rerun it! We *must* |
| 9547 | * use argv_expanded if it's non-NULL */ |
| 9548 | |
| 9549 | /* Going to fork a child per each pipe member */ |
| 9550 | pi->alive_cmds = 0; |
| 9551 | next_infd = 0; |
| 9552 | |
| 9553 | cmd_no = 0; |
| 9554 | while (cmd_no < pi->num_cmds) { |
| 9555 | struct fd_pair pipefds; |
| 9556 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9557 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9558 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9559 | nommu_save.old_vars = NULL; |
| 9560 | nommu_save.argv = NULL; |
| 9561 | nommu_save.argv_from_re_execing = NULL; |
| 9562 | #endif |
| 9563 | command = &pi->cmds[cmd_no]; |
| 9564 | cmd_no++; |
| 9565 | if (command->argv) { |
| 9566 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 9567 | command->argv[0], command->argv[1]); |
| 9568 | } else { |
| 9569 | debug_printf_exec(": pipe member with no argv\n"); |
| 9570 | } |
| 9571 | |
| 9572 | /* pipes are inserted between pairs of commands */ |
| 9573 | pipefds.rd = 0; |
| 9574 | pipefds.wr = 1; |
| 9575 | if (cmd_no < pi->num_cmds) |
| 9576 | xpiped_pair(pipefds); |
| 9577 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9578 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 08fb82c | 2019-05-19 15:26:05 +0200 | [diff] [blame] | 9579 | G.execute_lineno = command->lineno; |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 9580 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9581 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9582 | command->pid = BB_MMU ? fork() : vfork(); |
| 9583 | if (!command->pid) { /* child */ |
| 9584 | #if ENABLE_HUSH_JOB |
| 9585 | disable_restore_tty_pgrp_on_exit(); |
| 9586 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 9587 | |
| 9588 | /* Every child adds itself to new process group |
| 9589 | * with pgid == pid_of_first_child_in_pipe */ |
| 9590 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9591 | pid_t pgrp; |
| 9592 | pgrp = pi->pgrp; |
| 9593 | if (pgrp < 0) /* true for 1st process only */ |
| 9594 | pgrp = getpid(); |
| 9595 | if (setpgid(0, pgrp) == 0 |
| 9596 | && pi->followup != PIPE_BG |
| 9597 | && G_saved_tty_pgrp /* we have ctty */ |
| 9598 | ) { |
| 9599 | /* We do it in *every* child, not just first, |
| 9600 | * to avoid races */ |
| 9601 | tcsetpgrp(G_interactive_fd, pgrp); |
| 9602 | } |
| 9603 | } |
| 9604 | #endif |
| 9605 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 9606 | /* 1st cmd in backgrounded pipe |
| 9607 | * should have its stdin /dev/null'ed */ |
| 9608 | close(0); |
| 9609 | if (open(bb_dev_null, O_RDONLY)) |
| 9610 | xopen("/", O_RDONLY); |
| 9611 | } else { |
| 9612 | xmove_fd(next_infd, 0); |
| 9613 | } |
| 9614 | xmove_fd(pipefds.wr, 1); |
| 9615 | if (pipefds.rd > 1) |
| 9616 | close(pipefds.rd); |
| 9617 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9618 | * and the pipe fd (fd#1) is available for dup'ing: |
| 9619 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 9620 | * of cmd1 goes into pipe. |
| 9621 | */ |
| 9622 | if (setup_redirects(command, NULL)) { |
| 9623 | /* Happens when redir file can't be opened: |
| 9624 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 9625 | * FOO |
| 9626 | * hush: can't open '/qwe/rty': No such file or directory |
| 9627 | * BAZ |
| 9628 | * (echo BAR is not executed, it hits _exit(1) below) |
| 9629 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9630 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 9631 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9632 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9633 | /* Stores to nommu_save list of env vars putenv'ed |
| 9634 | * (NOMMU, on MMU we don't need that) */ |
| 9635 | /* cast away volatility... */ |
| 9636 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 9637 | /* pseudo_exec() does not return */ |
| 9638 | } |
| 9639 | |
| 9640 | /* parent or error */ |
| 9641 | #if ENABLE_HUSH_FAST |
| 9642 | G.count_SIGCHLD++; |
| 9643 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 9644 | #endif |
| 9645 | enable_restore_tty_pgrp_on_exit(); |
| 9646 | #if !BB_MMU |
| 9647 | /* Clean up after vforked child */ |
| 9648 | free(nommu_save.argv); |
| 9649 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 9650 | G.var_nest_level = sv_var_nest_level; |
| 9651 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9652 | add_vars(nommu_save.old_vars); |
| 9653 | #endif |
| 9654 | free(argv_expanded); |
| 9655 | argv_expanded = NULL; |
| 9656 | if (command->pid < 0) { /* [v]fork failed */ |
| 9657 | /* Clearly indicate, was it fork or vfork */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 9658 | bb_simple_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9659 | } else { |
| 9660 | pi->alive_cmds++; |
| 9661 | #if ENABLE_HUSH_JOB |
| 9662 | /* Second and next children need to know pid of first one */ |
| 9663 | if (pi->pgrp < 0) |
| 9664 | pi->pgrp = command->pid; |
| 9665 | #endif |
| 9666 | } |
| 9667 | |
| 9668 | if (cmd_no > 1) |
| 9669 | close(next_infd); |
| 9670 | if (cmd_no < pi->num_cmds) |
| 9671 | close(pipefds.wr); |
| 9672 | /* Pass read (output) pipe end to next iteration */ |
| 9673 | next_infd = pipefds.rd; |
| 9674 | } |
| 9675 | |
| 9676 | if (!pi->alive_cmds) { |
| 9677 | debug_leave(); |
| 9678 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 9679 | return 1; |
| 9680 | } |
| 9681 | |
| 9682 | debug_leave(); |
| 9683 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 9684 | return -1; |
| 9685 | } |
| 9686 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9687 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 9688 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 9689 | static int run_list(struct pipe *pi) |
| 9690 | { |
| 9691 | #if ENABLE_HUSH_CASE |
| 9692 | char *case_word = NULL; |
| 9693 | #endif |
| 9694 | #if ENABLE_HUSH_LOOPS |
| 9695 | struct pipe *loop_top = NULL; |
| 9696 | char **for_lcur = NULL; |
| 9697 | char **for_list = NULL; |
| 9698 | #endif |
| 9699 | smallint last_followup; |
| 9700 | smalluint rcode; |
| 9701 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 9702 | smalluint cond_code = 0; |
| 9703 | #else |
| 9704 | enum { cond_code = 0 }; |
| 9705 | #endif |
| 9706 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 9707 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9708 | smallint last_rword; /* ditto */ |
| 9709 | #endif |
| 9710 | |
| 9711 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 9712 | debug_enter(); |
| 9713 | |
| 9714 | #if ENABLE_HUSH_LOOPS |
| 9715 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 9716 | { |
| 9717 | struct pipe *cpipe; |
| 9718 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 9719 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 9720 | continue; |
| 9721 | /* current word is FOR or IN (BOLD in comments below) */ |
| 9722 | if (cpipe->next == NULL) { |
| 9723 | syntax_error("malformed for"); |
| 9724 | debug_leave(); |
| 9725 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9726 | return 1; |
| 9727 | } |
| 9728 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 9729 | if (cpipe->next->res_word == RES_DO) |
| 9730 | continue; |
| 9731 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 9732 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 9733 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 9734 | ) { |
| 9735 | syntax_error("malformed for"); |
| 9736 | debug_leave(); |
| 9737 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 9738 | return 1; |
| 9739 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9740 | } |
| 9741 | } |
| 9742 | #endif |
| 9743 | |
| 9744 | /* Past this point, all code paths should jump to ret: label |
| 9745 | * in order to return, no direct "return" statements please. |
| 9746 | * This helps to ensure that no memory is leaked. */ |
| 9747 | |
| 9748 | #if ENABLE_HUSH_JOB |
| 9749 | G.run_list_level++; |
| 9750 | #endif |
| 9751 | |
| 9752 | #if HAS_KEYWORDS |
| 9753 | rword = RES_NONE; |
| 9754 | last_rword = RES_XXXX; |
| 9755 | #endif |
| 9756 | last_followup = PIPE_SEQ; |
| 9757 | rcode = G.last_exitcode; |
| 9758 | |
| 9759 | /* Go through list of pipes, (maybe) executing them. */ |
| 9760 | 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] | 9761 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9762 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9763 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9764 | if (G.flag_SIGINT) |
| 9765 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 9766 | if (G_flag_return_in_progress == 1) |
| 9767 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9768 | |
| 9769 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 9770 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 9771 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9772 | |
| 9773 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 9774 | if ( |
| 9775 | #if ENABLE_HUSH_IF |
| 9776 | rword == RES_IF || rword == RES_ELIF || |
| 9777 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9778 | pi->followup != PIPE_SEQ |
| 9779 | ) { |
| 9780 | G.errexit_depth++; |
| 9781 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9782 | #if ENABLE_HUSH_LOOPS |
| 9783 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 9784 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 9785 | ) { |
| 9786 | /* start of a loop: remember where loop starts */ |
| 9787 | loop_top = pi; |
| 9788 | G.depth_of_loop++; |
| 9789 | } |
| 9790 | #endif |
| 9791 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 9792 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 9793 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 9794 | || (rcode != 0 && last_followup == PIPE_AND) |
| 9795 | ) { |
| 9796 | /* It is "<true> || CMD" or "<false> && CMD" |
| 9797 | * and we should not execute CMD */ |
| 9798 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 9799 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9800 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9801 | } |
| 9802 | } |
| 9803 | last_followup = pi->followup; |
| 9804 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 9805 | #if ENABLE_HUSH_IF |
| 9806 | if (cond_code) { |
| 9807 | if (rword == RES_THEN) { |
| 9808 | /* if false; then ... fi has exitcode 0! */ |
| 9809 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9810 | /* "if <false> THEN cmd": skip cmd */ |
| 9811 | continue; |
| 9812 | } |
| 9813 | } else { |
| 9814 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 9815 | /* "if <true> then ... ELSE/ELIF cmd": |
| 9816 | * skip cmd and all following ones */ |
| 9817 | break; |
| 9818 | } |
| 9819 | } |
| 9820 | #endif |
| 9821 | #if ENABLE_HUSH_LOOPS |
| 9822 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 9823 | if (!for_lcur) { |
| 9824 | /* first loop through for */ |
| 9825 | |
| 9826 | static const char encoded_dollar_at[] ALIGN1 = { |
| 9827 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 9828 | }; /* encoded representation of "$@" */ |
Denys Vlasenko | 987be93 | 2022-02-06 20:07:12 +0100 | [diff] [blame] | 9829 | static const char *const encoded_dollar_at_argv[] ALIGN_PTR = { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9830 | encoded_dollar_at, NULL |
| 9831 | }; /* argv list with one element: "$@" */ |
| 9832 | char **vals; |
| 9833 | |
Denys Vlasenko | a5db1d7 | 2018-07-28 12:42:08 +0200 | [diff] [blame] | 9834 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9835 | vals = (char**)encoded_dollar_at_argv; |
| 9836 | if (pi->next->res_word == RES_IN) { |
| 9837 | /* if no variable values after "in" we skip "for" */ |
| 9838 | if (!pi->next->cmds[0].argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9839 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 9840 | break; |
| 9841 | } |
| 9842 | vals = pi->next->cmds[0].argv; |
| 9843 | } /* else: "for var; do..." -> assume "$@" list */ |
| 9844 | /* create list of variable values */ |
| 9845 | debug_print_strings("for_list made from", vals); |
| 9846 | for_list = expand_strvec_to_strvec(vals); |
| 9847 | for_lcur = for_list; |
| 9848 | debug_print_strings("for_list", for_list); |
| 9849 | } |
| 9850 | if (!*for_lcur) { |
| 9851 | /* "for" loop is over, clean up */ |
| 9852 | free(for_list); |
| 9853 | for_list = NULL; |
| 9854 | for_lcur = NULL; |
| 9855 | break; |
| 9856 | } |
| 9857 | /* Insert next value from for_lcur */ |
| 9858 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9859 | 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] | 9860 | continue; |
| 9861 | } |
| 9862 | if (rword == RES_IN) { |
| 9863 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 9864 | } |
| 9865 | if (rword == RES_DONE) { |
| 9866 | continue; /* "done" has no cmds too */ |
| 9867 | } |
| 9868 | #endif |
| 9869 | #if ENABLE_HUSH_CASE |
| 9870 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9871 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9872 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 9873 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 9874 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 9875 | //unbackslash(case_word); |
| 9876 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9877 | continue; |
| 9878 | } |
| 9879 | if (rword == RES_MATCH) { |
| 9880 | char **argv; |
| 9881 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9882 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9883 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 9884 | break; |
| 9885 | /* all prev words didn't match, does this one match? */ |
| 9886 | argv = pi->cmds->argv; |
| 9887 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9888 | char *pattern; |
| 9889 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 9890 | pattern = expand_string_to_string(*argv, |
| 9891 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9892 | /*unbackslash:*/ 0 |
| 9893 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9894 | /* TODO: which FNM_xxx flags to use? */ |
| 9895 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9896 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 9897 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9898 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9899 | if (cond_code == 0) { |
| 9900 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9901 | free(case_word); |
| 9902 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9903 | break; |
| 9904 | } |
| 9905 | argv++; |
| 9906 | } |
| 9907 | continue; |
| 9908 | } |
| 9909 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9910 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9911 | if (cond_code != 0) |
| 9912 | continue; /* not matched yet, skip this pipe */ |
| 9913 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9914 | if (rword == RES_ESAC) { |
| 9915 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 9916 | if (case_word) { |
| 9917 | /* "case" did not match anything: still set $? (to 0) */ |
| 9918 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9919 | } |
| 9920 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9921 | #endif |
| 9922 | /* Just pressing <enter> in shell should check for jobs. |
| 9923 | * OTOH, in non-interactive shell this is useless |
| 9924 | * and only leads to extra job checks */ |
| 9925 | if (pi->num_cmds == 0) { |
| 9926 | if (G_interactive_fd) |
| 9927 | goto check_jobs_and_continue; |
| 9928 | continue; |
| 9929 | } |
| 9930 | |
| 9931 | /* After analyzing all keywords and conditions, we decided |
| 9932 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 9933 | * after run_pipe to collect any background children, |
| 9934 | * even if list execution is to be stopped. */ |
| 9935 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9936 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9937 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9938 | #endif |
Denys Vlasenko | e53c7db | 2021-09-07 02:23:51 +0200 | [diff] [blame] | 9939 | rcode = r = G.o_opt[OPT_O_NOEXEC] ? 0 : run_pipe(pi); |
| 9940 | /* NB: rcode is a smalluint, r is int */ |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9941 | if (r != -1) { |
| 9942 | /* We ran a builtin, function, or group. |
| 9943 | * rcode is already known |
| 9944 | * and we don't need to wait for anything. */ |
| 9945 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 9946 | G.last_exitcode = rcode; |
| 9947 | check_and_run_traps(); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 9948 | #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS |
| 9949 | rcode = G.last_exitcode; /* "return" in trap can change it, read back */ |
| 9950 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9951 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9952 | /* Was it "break" or "continue"? */ |
| 9953 | if (G.flag_break_continue) { |
| 9954 | smallint fbc = G.flag_break_continue; |
| 9955 | /* We might fall into outer *loop*, |
| 9956 | * don't want to break it too */ |
| 9957 | if (loop_top) { |
| 9958 | G.depth_break_continue--; |
| 9959 | if (G.depth_break_continue == 0) |
| 9960 | G.flag_break_continue = 0; |
| 9961 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9962 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9963 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9964 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9965 | break; |
| 9966 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9967 | /* "continue": simulate end of loop */ |
| 9968 | rword = RES_DONE; |
| 9969 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9970 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9971 | #endif |
| 9972 | if (G_flag_return_in_progress == 1) { |
| 9973 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9974 | break; |
| 9975 | } |
| 9976 | } else if (pi->followup == PIPE_BG) { |
| 9977 | /* What does bash do with attempts to background builtins? */ |
| 9978 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9979 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9980 | * I'm NOT treating inner &'s as jobs */ |
| 9981 | #if ENABLE_HUSH_JOB |
| 9982 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9983 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9984 | #endif |
| 9985 | /* Last command's pid goes to $! */ |
| 9986 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9987 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9988 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9989 | /* 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] | 9990 | rcode = EXIT_SUCCESS; |
| 9991 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9992 | } else { |
| 9993 | #if ENABLE_HUSH_JOB |
| 9994 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9995 | /* Waits for completion, then fg's main shell */ |
| 9996 | rcode = checkjobs_and_fg_shell(pi); |
| 9997 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9998 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9999 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 10000 | #endif |
| 10001 | /* This one just waits for completion */ |
| 10002 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 10003 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 10004 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 10005 | G.last_exitcode = rcode; |
| 10006 | check_and_run_traps(); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 10007 | #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS |
| 10008 | rcode = G.last_exitcode; /* "return" in trap can change it, read back */ |
| 10009 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10010 | } |
| 10011 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 10012 | /* Handle "set -e" */ |
| 10013 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 10014 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 10015 | if (G.errexit_depth == 0) |
| 10016 | hush_exit(rcode); |
| 10017 | } |
| 10018 | G.errexit_depth = sv_errexit_depth; |
| 10019 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10020 | /* Analyze how result affects subsequent commands */ |
| 10021 | #if ENABLE_HUSH_IF |
| 10022 | if (rword == RES_IF || rword == RES_ELIF) |
| 10023 | cond_code = rcode; |
| 10024 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 10025 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10026 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 10027 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10028 | #if ENABLE_HUSH_LOOPS |
| 10029 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 10030 | if (pi->next |
| 10031 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 10032 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 10033 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10034 | if (rword == RES_WHILE) { |
| 10035 | if (rcode) { |
| 10036 | /* "while false; do...done" - exitcode 0 */ |
| 10037 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 10038 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 10039 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10040 | } |
| 10041 | } |
| 10042 | if (rword == RES_UNTIL) { |
| 10043 | if (!rcode) { |
| 10044 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10045 | break; |
| 10046 | } |
| 10047 | } |
| 10048 | } |
| 10049 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10050 | } /* for (pi) */ |
| 10051 | |
| 10052 | #if ENABLE_HUSH_JOB |
| 10053 | G.run_list_level--; |
| 10054 | #endif |
| 10055 | #if ENABLE_HUSH_LOOPS |
| 10056 | if (loop_top) |
| 10057 | G.depth_of_loop--; |
| 10058 | free(for_list); |
| 10059 | #endif |
| 10060 | #if ENABLE_HUSH_CASE |
| 10061 | free(case_word); |
| 10062 | #endif |
| 10063 | debug_leave(); |
| 10064 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 10065 | return rcode; |
| 10066 | } |
| 10067 | |
| 10068 | /* Select which version we will use */ |
| 10069 | static int run_and_free_list(struct pipe *pi) |
| 10070 | { |
| 10071 | int rcode = 0; |
| 10072 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 10073 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 10074 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 10075 | rcode = run_list(pi); |
| 10076 | } |
| 10077 | /* free_pipe_list has the side effect of clearing memory. |
| 10078 | * In the long run that function can be merged with run_list, |
| 10079 | * but doing that now would hobble the debugging effort. */ |
| 10080 | free_pipe_list(pi); |
| 10081 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 10082 | return rcode; |
| 10083 | } |
| 10084 | |
| 10085 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10086 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 10087 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10088 | sighandler_t old_handler; |
| 10089 | unsigned sig = 0; |
| 10090 | while ((mask >>= 1) != 0) { |
| 10091 | sig++; |
| 10092 | if (!(mask & 1)) |
| 10093 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10094 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10095 | /* POSIX allows shell to re-enable SIGCHLD |
| 10096 | * even if it was SIG_IGN on entry. |
| 10097 | * Therefore we skip IGN check for it: |
| 10098 | */ |
| 10099 | if (sig == SIGCHLD) |
| 10100 | continue; |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 10101 | /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 10102 | * Try: |
| 10103 | * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect |
| 10104 | * 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] | 10105 | */ |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 10106 | if (sig == SIGHUP && G_interactive_fd) |
| 10107 | continue; |
| 10108 | /* Unless one of the above signals, is it SIG_IGN? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10109 | if (old_handler == SIG_IGN) { |
| 10110 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10111 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10112 | #if ENABLE_HUSH_TRAP |
| 10113 | if (!G_traps) |
| 10114 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 10115 | free(G_traps[sig]); |
| 10116 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 10117 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10118 | } |
| 10119 | } |
| 10120 | } |
| 10121 | |
| 10122 | /* Called a few times only (or even once if "sh -c") */ |
| 10123 | static void install_special_sighandlers(void) |
| 10124 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10125 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10126 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10127 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10128 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10129 | if (G_interactive_fd) { |
| 10130 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 10131 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10132 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10133 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10134 | /* Careful, do not re-install handlers we already installed */ |
| 10135 | if (G.special_sig_mask != mask) { |
| 10136 | unsigned diff = mask & ~G.special_sig_mask; |
| 10137 | G.special_sig_mask = mask; |
| 10138 | install_sighandlers(diff); |
| 10139 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | #if ENABLE_HUSH_JOB |
| 10143 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10144 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10145 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10146 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10147 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10148 | |
| 10149 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10150 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10151 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 10152 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10153 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 10154 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10155 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10156 | + (1 << SIGABRT) |
| 10157 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 10158 | + (1 << SIGPIPE) |
| 10159 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10160 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10161 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10162 | * we never want to restore pgrp on exit, and this fn is not called |
| 10163 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10164 | /*+ (1 << SIGHUP )*/ |
| 10165 | /*+ (1 << SIGTERM)*/ |
| 10166 | /*+ (1 << SIGINT )*/ |
| 10167 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10168 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 10169 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10170 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10171 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 10172 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 10173 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10174 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10175 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10176 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10177 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10178 | case 'n': |
Denys Vlasenko | e53c7db | 2021-09-07 02:23:51 +0200 | [diff] [blame] | 10179 | /* set -n has no effect in interactive shell */ |
| 10180 | /* Try: while set -n; do echo $-; done */ |
| 10181 | if (!G_interactive_fd) |
| 10182 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10183 | break; |
| 10184 | case 'x': |
| 10185 | IF_HUSH_MODE_X(G_x_mode = state;) |
Denys Vlasenko | aa449c9 | 2018-07-28 12:13:58 +0200 | [diff] [blame] | 10186 | 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] | 10187 | break; |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 10188 | case 'e': |
| 10189 | G.o_opt[OPT_O_ERREXIT] = state; |
| 10190 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10191 | case 'o': |
| 10192 | if (!o_opt) { |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 10193 | /* "set -o" or "set +o" without parameter. |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10194 | * in bash, set -o produces this output: |
| 10195 | * pipefail off |
| 10196 | * and set +o: |
| 10197 | * set +o pipefail |
| 10198 | * We always use the second form. |
| 10199 | */ |
| 10200 | const char *p = o_opt_strings; |
| 10201 | idx = 0; |
| 10202 | while (*p) { |
| 10203 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 10204 | idx++; |
| 10205 | p += strlen(p) + 1; |
| 10206 | } |
| 10207 | break; |
| 10208 | } |
| 10209 | idx = index_in_strings(o_opt_strings, o_opt); |
| 10210 | if (idx >= 0) { |
| 10211 | G.o_opt[idx] = state; |
| 10212 | break; |
| 10213 | } |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 10214 | /* fall through to error */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10215 | default: |
| 10216 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10217 | } |
| 10218 | return EXIT_SUCCESS; |
| 10219 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10220 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 10221 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 10222 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10223 | { |
Denys Vlasenko | bb4e32b | 2020-12-20 16:36:00 +0100 | [diff] [blame] | 10224 | pid_t cached_getpid; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10225 | enum { |
| 10226 | OPT_login = (1 << 0), |
| 10227 | }; |
| 10228 | unsigned flags; |
Denys Vlasenko | 63139b5 | 2020-12-13 22:00:56 +0100 | [diff] [blame] | 10229 | #if !BB_MMU |
| 10230 | unsigned builtin_argc = 0; |
| 10231 | #endif |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10232 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10233 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10234 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 10235 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 10236 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 10237 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10238 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10239 | #if ENABLE_HUSH_TRAP |
| 10240 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 10241 | G.return_exitcode = -1; |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10242 | # endif |
| 10243 | G.pre_trap_exitcode = -1; |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 10244 | #endif |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 10245 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 10246 | #if ENABLE_HUSH_FAST |
| 10247 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 10248 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10249 | #if !BB_MMU |
| 10250 | G.argv0_for_re_execing = argv[0]; |
| 10251 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10252 | |
Denys Vlasenko | bb4e32b | 2020-12-20 16:36:00 +0100 | [diff] [blame] | 10253 | cached_getpid = getpid(); /* for tcsetpgrp() during init */ |
Denys Vlasenko | 46a71dc | 2020-12-25 18:49:29 +0100 | [diff] [blame] | 10254 | G.root_pid = cached_getpid; /* for $PID (NOMMU can override via -$HEXPID:HEXPPID:...) */ |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 10255 | G.root_ppid = getppid(); /* for $PPID (NOMMU can override) */ |
Denys Vlasenko | bb4e32b | 2020-12-20 16:36:00 +0100 | [diff] [blame] | 10256 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 10257 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10258 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 10259 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10260 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 10261 | shell_ver->flg_export = 1; |
| 10262 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 10263 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 10264 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10265 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 10266 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 10267 | /* Create shell local variables from the values |
| 10268 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10269 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10270 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 10271 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10272 | if (e) while (*e) { |
| 10273 | char *value = strchr(*e, '='); |
| 10274 | if (value) { /* paranoia */ |
| 10275 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 10276 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 10277 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10278 | cur_var->max_len = strlen(*e); |
| 10279 | cur_var->flg_export = 1; |
| 10280 | } |
| 10281 | e++; |
| 10282 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 10283 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 10284 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 10285 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10286 | |
| 10287 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10288 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 10289 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10290 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 10291 | /* Set (but not export) HOSTNAME unless already set */ |
| 10292 | if (!get_local_var_value("HOSTNAME")) { |
| 10293 | struct utsname uts; |
| 10294 | uname(&uts); |
| 10295 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 10296 | } |
Denys Vlasenko | fd6f295 | 2018-08-05 15:13:08 +0200 | [diff] [blame] | 10297 | #endif |
| 10298 | /* IFS is not inherited from the parent environment */ |
| 10299 | set_local_var_from_halves("IFS", defifs); |
| 10300 | |
Denys Vlasenko | ef8985c | 2019-05-19 16:29:09 +0200 | [diff] [blame] | 10301 | if (!get_local_var_value("PATH")) |
| 10302 | set_local_var_from_halves("PATH", bb_default_root_path); |
| 10303 | |
Denys Vlasenko | 0c36019 | 2019-05-19 15:37:50 +0200 | [diff] [blame] | 10304 | /* PS1/PS2 are set later, if we determine that we are interactive */ |
| 10305 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10306 | /* bash also exports SHLVL and _, |
| 10307 | * and sets (but doesn't export) the following variables: |
| 10308 | * BASH=/bin/bash |
| 10309 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 10310 | * BASH_VERSION='3.2.0(1)-release' |
| 10311 | * HOSTTYPE=i386 |
| 10312 | * MACHTYPE=i386-pc-linux-gnu |
| 10313 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10314 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10315 | * EUID=<NNNNN> |
| 10316 | * UID=<NNNNN> |
| 10317 | * GROUPS=() |
| 10318 | * LINES=<NNN> |
| 10319 | * COLUMNS=<NNN> |
| 10320 | * BASH_ARGC=() |
| 10321 | * BASH_ARGV=() |
| 10322 | * BASH_LINENO=() |
| 10323 | * BASH_SOURCE=() |
| 10324 | * DIRSTACK=() |
| 10325 | * PIPESTATUS=([0]="0") |
| 10326 | * HISTFILE=/<xxx>/.bash_history |
| 10327 | * HISTFILESIZE=500 |
| 10328 | * HISTSIZE=500 |
| 10329 | * MAILCHECK=60 |
| 10330 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 10331 | * SHELL=/bin/bash |
| 10332 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 10333 | * TERM=dumb |
| 10334 | * OPTERR=1 |
| 10335 | * OPTIND=1 |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10336 | * PS4='+ ' |
| 10337 | */ |
| 10338 | |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10339 | #if NUM_SCRIPTS > 0 |
| 10340 | if (argc < 0) { |
| 10341 | char *script = get_script_content(-argc - 1); |
| 10342 | G.global_argv = argv; |
| 10343 | G.global_argc = string_array_len(argv); |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10344 | //install_special_sighandlers(); - needed? |
| 10345 | parse_and_run_string(script); |
| 10346 | goto final_return; |
| 10347 | } |
| 10348 | #endif |
| 10349 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 10350 | /* Initialize some more globals to non-zero values */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 10351 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 10352 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10353 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10354 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10355 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10356 | * 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] | 10357 | * in order to intercept (more) signals. |
| 10358 | */ |
| 10359 | |
| 10360 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10361 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10362 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10363 | while (1) { |
Denys Vlasenko | 3b05305 | 2021-01-04 03:05:34 +0100 | [diff] [blame] | 10364 | int opt = getopt(argc, argv, "+" /* stop at 1st non-option */ |
| 10365 | "cexinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10366 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10367 | "<:$:R:V:" |
| 10368 | # if ENABLE_HUSH_FUNCTIONS |
| 10369 | "F:" |
| 10370 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10371 | #endif |
| 10372 | ); |
| 10373 | if (opt <= 0) |
| 10374 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10375 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10376 | case 'c': |
Denys Vlasenko | 0ab2dd4 | 2020-12-23 02:22:08 +0100 | [diff] [blame] | 10377 | /* Note: -c is not an option with param! |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10378 | * "hush -c -l SCRIPT" is valid. "hush -cSCRIPT" is not. |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10379 | */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10380 | G.opt_c = 1; |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10381 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10382 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 10383 | /* Well, we cannot just declare interactiveness, |
| 10384 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10385 | /* G_interactive_fd++; */ |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 10386 | //There are a few cases where bash -i -c 'SCRIPT' |
| 10387 | //has visible effect (differs from bash -c 'SCRIPT'): |
| 10388 | //it ignores TERM: |
| 10389 | // bash -i -c 'kill $$; echo ALIVE' |
| 10390 | // ALIVE |
Denys Vlasenko | 931c55f | 2022-01-13 12:50:48 +0100 | [diff] [blame] | 10391 | //it resets SIG_IGNed HUP to SIG_DFL: |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 10392 | // trap '' hup; bash -i -c 'kill -hup $$; echo ALIVE' |
| 10393 | // Hangup [the message is not printed by bash, it's the shell which started it] |
| 10394 | //is talkative about jobs and exiting: |
| 10395 | // bash -i -c 'sleep 1 & exit' |
| 10396 | // [1] 16170 |
| 10397 | // exit |
| 10398 | //includes $ENV file (only if run as "sh"): |
| 10399 | // echo last >/tmp/ENV; ENV=/tmp/ENV sh -i -c 'echo HERE' |
| 10400 | // last: cannot open /var/log/wtmp: No such file or directory |
| 10401 | // HERE |
| 10402 | //(under "bash", it's the opposite: it runs $BASH_ENV file only *without* -i). |
| 10403 | // |
| 10404 | //ash -i -c 'sleep 3; sleep 3', on ^C, drops into a prompt instead of exiting |
| 10405 | //(this may be a bug, bash does not do this). |
| 10406 | //(ash -i -c 'sleep 3' won't show this, the last command gets auto-"exec"ed) |
| 10407 | // |
| 10408 | //None of the above feel like useful features people would rely on. |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10409 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10410 | case 's': |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10411 | G.opt_s = 1; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 10412 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10413 | case 'l': |
| 10414 | flags |= OPT_login; |
| 10415 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10416 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10417 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 10418 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 10419 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10420 | case '$': { |
| 10421 | unsigned long long empty_trap_mask; |
| 10422 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10423 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 10424 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 10425 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 10426 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10427 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 10428 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10429 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10430 | optarg++; |
| 10431 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10432 | optarg++; |
| 10433 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 10434 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10435 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10436 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10437 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10438 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10439 | for (sig = 1; sig < NSIG; sig++) { |
| 10440 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10441 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10442 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10443 | } |
| 10444 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 10445 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10446 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10447 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10448 | optarg++; |
| 10449 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10450 | # endif |
Denys Vlasenko | 49142d4 | 2020-12-13 18:44:07 +0100 | [diff] [blame] | 10451 | /* Suppress "killed by signal" message, -$ hack is used |
| 10452 | * for subshells: echo `sh -c 'kill -9 $$'` |
| 10453 | * should be silent. |
| 10454 | */ |
| 10455 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 10456 | # if ENABLE_HUSH_FUNCTIONS |
| 10457 | /* nommu uses re-exec trick for "... | func | ...", |
| 10458 | * should allow "return". |
| 10459 | * This accidentally allows returns in subshells. |
| 10460 | */ |
| 10461 | G_flag_return_in_progress = -1; |
| 10462 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 10463 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10464 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10465 | case 'R': |
| 10466 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10467 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10468 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 10469 | # if ENABLE_HUSH_FUNCTIONS |
| 10470 | case 'F': { |
| 10471 | struct function *funcp = new_function(optarg); |
| 10472 | /* funcp->name is already set to optarg */ |
| 10473 | /* funcp->body is set to NULL. It's a special case. */ |
| 10474 | funcp->body_as_string = argv[optind]; |
| 10475 | optind++; |
| 10476 | break; |
| 10477 | } |
| 10478 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 10479 | #endif |
Denys Vlasenko | 3b05305 | 2021-01-04 03:05:34 +0100 | [diff] [blame] | 10480 | /*case '?': invalid option encountered (set_mode('?') will fail) */ |
| 10481 | /*case 'n':*/ |
| 10482 | /*case 'x':*/ |
| 10483 | /*case 'e':*/ |
| 10484 | default: |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 10485 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 10486 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 10487 | bb_show_usage(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10488 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10489 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10490 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10491 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 10492 | G.global_argc = argc - (optind - 1); |
| 10493 | G.global_argv = argv + (optind - 1); |
| 10494 | G.global_argv[0] = argv[0]; |
| 10495 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10496 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10497 | if (flags & OPT_login) { |
Denys Vlasenko | 63139b5 | 2020-12-13 22:00:56 +0100 | [diff] [blame] | 10498 | const char *hp = NULL; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10499 | HFILE *input; |
Denys Vlasenko | 63139b5 | 2020-12-13 22:00:56 +0100 | [diff] [blame] | 10500 | |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10501 | debug_printf("sourcing /etc/profile\n"); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10502 | input = hfopen("/etc/profile"); |
Denys Vlasenko | 63139b5 | 2020-12-13 22:00:56 +0100 | [diff] [blame] | 10503 | run_profile: |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10504 | if (input != NULL) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10505 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10506 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10507 | hfclose(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10508 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10509 | /* bash: after sourcing /etc/profile, |
| 10510 | * tries to source (in the given order): |
| 10511 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10512 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10513 | * bash also sources ~/.bash_logout on exit. |
| 10514 | * If called as sh, skips .bash_XXX files. |
| 10515 | */ |
Denys Vlasenko | 63139b5 | 2020-12-13 22:00:56 +0100 | [diff] [blame] | 10516 | if (!hp) { /* unless we looped on the "goto" already */ |
| 10517 | hp = get_local_var_value("HOME"); |
| 10518 | if (hp && hp[0]) { |
| 10519 | debug_printf("sourcing ~/.profile\n"); |
| 10520 | hp = concat_path_file(hp, ".profile"); |
| 10521 | input = hfopen(hp); |
| 10522 | free((char*)hp); |
| 10523 | goto run_profile; |
| 10524 | } |
| 10525 | } |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 10526 | } |
| 10527 | |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10528 | /* -c takes effect *after* -l */ |
| 10529 | if (G.opt_c) { |
| 10530 | /* Possibilities: |
| 10531 | * sh ... -c 'script' |
| 10532 | * sh ... -c 'script' ARG0 [ARG1...] |
| 10533 | * On NOMMU, if builtin_argc != 0, |
| 10534 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
| 10535 | * "" needs to be replaced with NULL |
| 10536 | * and BARGV vector fed to builtin function. |
| 10537 | * Note: the form without ARG0 never happens: |
| 10538 | * sh ... -c 'builtin' BARGV... "" |
| 10539 | */ |
| 10540 | char *script; |
| 10541 | |
| 10542 | install_special_sighandlers(); |
| 10543 | |
| 10544 | G.global_argc--; |
| 10545 | G.global_argv++; |
Denys Vlasenko | 49142d4 | 2020-12-13 18:44:07 +0100 | [diff] [blame] | 10546 | #if !BB_MMU |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10547 | if (builtin_argc) { |
| 10548 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 10549 | const struct built_in_command *x; |
| 10550 | x = find_builtin(G.global_argv[0]); |
| 10551 | if (x) { /* paranoia */ |
| 10552 | argv = G.global_argv; |
| 10553 | G.global_argc -= builtin_argc + 1; /* skip [BARGV...] "" */ |
| 10554 | G.global_argv += builtin_argc + 1; |
| 10555 | G.global_argv[-1] = NULL; /* replace "" */ |
| 10556 | G.last_exitcode = x->b_function(argv); |
| 10557 | } |
| 10558 | goto final_return; |
| 10559 | } |
Denys Vlasenko | 49142d4 | 2020-12-13 18:44:07 +0100 | [diff] [blame] | 10560 | #endif |
Denys Vlasenko | 9cabd17 | 2020-12-13 18:24:11 +0100 | [diff] [blame] | 10561 | |
| 10562 | script = G.global_argv[0]; |
| 10563 | if (!script) |
| 10564 | bb_error_msg_and_die(bb_msg_requires_arg, "-c"); |
| 10565 | if (!G.global_argv[1]) { |
| 10566 | /* -c 'script' (no params): prevent empty $0 */ |
| 10567 | G.global_argv[0] = argv[0]; |
| 10568 | } else { /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
| 10569 | G.global_argc--; |
| 10570 | G.global_argv++; |
| 10571 | } |
| 10572 | parse_and_run_string(script); |
| 10573 | goto final_return; |
| 10574 | } |
| 10575 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 10576 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10577 | if (!G.opt_s && G.global_argv[1]) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10578 | HFILE *input; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10579 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 10580 | * "bash <script>" (which is never interactive (unless -i?)) |
| 10581 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10582 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 10583 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10584 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10585 | G.global_argc--; |
| 10586 | G.global_argv++; |
| 10587 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10588 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10589 | input = hfopen(G.global_argv[0]); |
| 10590 | if (!input) { |
| 10591 | bb_simple_perror_msg_and_die(G.global_argv[0]); |
| 10592 | } |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 10593 | xfunc_error_retval = 1; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10594 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10595 | parse_and_run_file(input); |
| 10596 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10597 | hfclose(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10598 | #endif |
| 10599 | goto final_return; |
| 10600 | } |
Denys Vlasenko | f363458 | 2019-06-03 12:21:04 +0200 | [diff] [blame] | 10601 | /* "implicit" -s: bare interactive hush shows 's' in $- */ |
Denys Vlasenko | d8740b2 | 2019-05-19 19:11:21 +0200 | [diff] [blame] | 10602 | G.opt_s = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10603 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10604 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10605 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 10606 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10607 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 10608 | /* A shell is interactive if the '-i' flag was given, |
| 10609 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 10610 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10611 | * no arguments remaining or the -s flag given |
| 10612 | * standard input is a terminal |
| 10613 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10614 | * Refer to Posix.2, the description of the 'sh' utility. |
| 10615 | */ |
| 10616 | #if ENABLE_HUSH_JOB |
| 10617 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10618 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 10619 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 10620 | if (G_saved_tty_pgrp < 0) |
| 10621 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10622 | |
| 10623 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10624 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10625 | if (G_interactive_fd < 0) { |
| 10626 | /* try to dup to any fd */ |
| 10627 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10628 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10629 | /* give up */ |
| 10630 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10631 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 10632 | } |
| 10633 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10634 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10635 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10636 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10637 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10638 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10639 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10640 | /* If we were run as 'hush &', sleep until we are |
| 10641 | * in the foreground (tty pgrp == our pgrp). |
| 10642 | * If we get started under a job aware app (like bash), |
| 10643 | * make sure we are now in charge so we don't fight over |
| 10644 | * who gets the foreground */ |
| 10645 | while (1) { |
| 10646 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10647 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 10648 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10649 | break; |
| 10650 | /* send TTIN to ourself (should stop us) */ |
| 10651 | kill(- shell_pgrp, SIGTTIN); |
| 10652 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10653 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10654 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 10655 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10656 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10657 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10658 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10659 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10660 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10661 | /* Put ourselves in our own process group |
| 10662 | * (bash, too, does this only if ctty is available) */ |
| 10663 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 10664 | /* Grab control of the terminal */ |
Denys Vlasenko | bb4e32b | 2020-12-20 16:36:00 +0100 | [diff] [blame] | 10665 | tcsetpgrp(G_interactive_fd, cached_getpid); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10666 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 10667 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10668 | |
Denys Vlasenko | 76a4e83 | 2019-05-19 18:24:52 +0200 | [diff] [blame] | 10669 | # if ENABLE_FEATURE_EDITING |
| 10670 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Ron Yorston | 9e2a566 | 2020-01-21 16:01:58 +0000 | [diff] [blame] | 10671 | G.line_input_state->get_exe_name = get_builtin_name; |
Ron Yorston | 7d1c7d8 | 2022-03-24 12:17:25 +0000 | [diff] [blame] | 10672 | G.line_input_state->sh_get_var = get_local_var_value; |
Denys Vlasenko | 76a4e83 | 2019-05-19 18:24:52 +0200 | [diff] [blame] | 10673 | # endif |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10674 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 10675 | { |
| 10676 | const char *hp = get_local_var_value("HISTFILE"); |
| 10677 | if (!hp) { |
| 10678 | hp = get_local_var_value("HOME"); |
| 10679 | if (hp) |
| 10680 | hp = concat_path_file(hp, ".hush_history"); |
| 10681 | } else { |
| 10682 | hp = xstrdup(hp); |
| 10683 | } |
| 10684 | if (hp) { |
| 10685 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 10686 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 10687 | } |
| 10688 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 10689 | hp = get_local_var_value("HISTFILESIZE"); |
| 10690 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 10691 | # endif |
| 10692 | } |
| 10693 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10694 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10695 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10696 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10697 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10698 | /* No job control compiled in, only prompt/line editing */ |
| 10699 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 10700 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10701 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10702 | /* try to dup to any fd */ |
Denys Vlasenko | d1a8323 | 2018-06-26 15:50:33 +0200 | [diff] [blame] | 10703 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10704 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10705 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10706 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 10707 | } |
| 10708 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10709 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10710 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10711 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10712 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10713 | #else |
| 10714 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10715 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 10716 | #endif |
| 10717 | /* bash: |
| 10718 | * if interactive but not a login shell, sources ~/.bashrc |
| 10719 | * (--norc turns this off, --rcfile <file> overrides) |
| 10720 | */ |
| 10721 | |
Denys Vlasenko | 0c36019 | 2019-05-19 15:37:50 +0200 | [diff] [blame] | 10722 | if (G_interactive_fd) { |
| 10723 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 10724 | /* Set (but not export) PS1/2 unless already set */ |
| 10725 | if (!get_local_var_value("PS1")) |
| 10726 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 10727 | if (!get_local_var_value("PS2")) |
| 10728 | set_local_var_from_halves("PS2", "> "); |
| 10729 | #endif |
| 10730 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET) { |
| 10731 | /* note: ash and hush share this string */ |
| 10732 | printf("\n\n%s %s\n" |
| 10733 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 10734 | "\n", |
| 10735 | bb_banner, |
| 10736 | "hush - the humble shell" |
| 10737 | ); |
| 10738 | } |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 10739 | } |
| 10740 | |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 10741 | parse_and_run_file(hfopen(NULL)); /* stdin */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10742 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 10743 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10744 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10745 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 10746 | |
| 10747 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10748 | /* |
| 10749 | * Built-ins |
| 10750 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10751 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10752 | { |
| 10753 | return 0; |
| 10754 | } |
| 10755 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10756 | #if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL |
Denys Vlasenko | a8e1960 | 2020-12-14 03:52:54 +0100 | [diff] [blame] | 10757 | static NOINLINE 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] | 10758 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10759 | int argc = string_array_len(argv); |
| 10760 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10761 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10762 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 10763 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 10764 | static int FAST_FUNC builtin_test(char **argv) |
| 10765 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10766 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10767 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 10768 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10769 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10770 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10771 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10772 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10773 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 10774 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10775 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10776 | static int FAST_FUNC builtin_printf(char **argv) |
| 10777 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 10778 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 10779 | } |
| 10780 | #endif |
| 10781 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10782 | #if ENABLE_HUSH_HELP |
| 10783 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 10784 | { |
| 10785 | const struct built_in_command *x; |
| 10786 | |
| 10787 | printf( |
| 10788 | "Built-in commands:\n" |
| 10789 | "------------------\n"); |
| 10790 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 10791 | if (x->b_descr) |
| 10792 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 10793 | } |
| 10794 | return EXIT_SUCCESS; |
| 10795 | } |
| 10796 | #endif |
| 10797 | |
| 10798 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 10799 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 10800 | { |
Ron Yorston | 9f3b410 | 2019-12-16 09:31:10 +0000 | [diff] [blame] | 10801 | show_history(G.line_input_state); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10802 | return EXIT_SUCCESS; |
| 10803 | } |
| 10804 | #endif |
| 10805 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10806 | static char **skip_dash_dash(char **argv) |
| 10807 | { |
| 10808 | argv++; |
| 10809 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 10810 | argv++; |
| 10811 | return argv; |
| 10812 | } |
| 10813 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10814 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10815 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10816 | const char *newdir; |
| 10817 | |
| 10818 | argv = skip_dash_dash(argv); |
| 10819 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10820 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10821 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10822 | * bash says "bash: cd: HOME not set" and does nothing |
| 10823 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 10824 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 10825 | const char *home = get_local_var_value("HOME"); |
| 10826 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 10827 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10828 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 10829 | /* Mimic bash message exactly */ |
| 10830 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10831 | return EXIT_FAILURE; |
| 10832 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 10833 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 10834 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 10835 | * set it again, but do not export. bash does the same. |
| 10836 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10837 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10838 | return EXIT_SUCCESS; |
| 10839 | } |
| 10840 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10841 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 10842 | { |
| 10843 | puts(get_cwd(0)); |
| 10844 | return EXIT_SUCCESS; |
| 10845 | } |
| 10846 | |
| 10847 | static int FAST_FUNC builtin_eval(char **argv) |
| 10848 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10849 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10850 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10851 | if (!argv[0]) |
| 10852 | return EXIT_SUCCESS; |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 10853 | |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10854 | IF_HUSH_MODE_X(G.x_mode_depth++;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10855 | //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] | 10856 | if (!argv[1]) { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10857 | /* bash: |
| 10858 | * eval "echo Hi; done" ("done" is syntax error): |
| 10859 | * "echo Hi" will not execute too. |
| 10860 | */ |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 10861 | parse_and_run_string(argv[0]); |
| 10862 | } else { |
| 10863 | /* "The eval utility shall construct a command by |
| 10864 | * concatenating arguments together, separating |
| 10865 | * each with a <space> character." |
| 10866 | */ |
| 10867 | char *str, *p; |
| 10868 | unsigned len = 0; |
| 10869 | char **pp = argv; |
| 10870 | do |
| 10871 | len += strlen(*pp) + 1; |
| 10872 | while (*++pp); |
| 10873 | str = p = xmalloc(len); |
| 10874 | pp = argv; |
| 10875 | for (;;) { |
| 10876 | p = stpcpy(p, *pp); |
| 10877 | pp++; |
| 10878 | if (!*pp) |
| 10879 | break; |
| 10880 | *p++ = ' '; |
| 10881 | } |
| 10882 | parse_and_run_string(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10883 | free(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10884 | } |
Denys Vlasenko | 7c5f18a | 2018-07-26 15:21:50 +0200 | [diff] [blame] | 10885 | IF_HUSH_MODE_X(G.x_mode_depth--;) |
Denys Vlasenko | 9dda927 | 2018-07-27 14:12:05 +0200 | [diff] [blame] | 10886 | //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] | 10887 | return G.last_exitcode; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10888 | } |
| 10889 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10890 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10891 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10892 | argv = skip_dash_dash(argv); |
| 10893 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10894 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10895 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 10896 | /* Careful: we can end up here after [v]fork. Do not restore |
| 10897 | * tty pgrp then, only top-level shell process does that */ |
| 10898 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 10899 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 10900 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 10901 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 10902 | * open here. However, they are all CLOEXEC, and execv below |
| 10903 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 10904 | * it should show no extra open fds in the "ls" process. |
| 10905 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 10906 | */ |
| 10907 | //close_saved_fds_and_FILE_fds(); |
| 10908 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 10909 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10910 | * 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] | 10911 | * and tcsetpgrp, and this is inherently racy. |
| 10912 | */ |
| 10913 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10914 | } |
| 10915 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10916 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10917 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 10918 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10919 | |
| 10920 | /* interactive bash: |
| 10921 | * # trap "echo EEE" EXIT |
| 10922 | * # exit |
| 10923 | * exit |
| 10924 | * There are stopped jobs. |
| 10925 | * (if there are _stopped_ jobs, running ones don't count) |
| 10926 | * # exit |
| 10927 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 10928 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10929 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 10930 | * 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] | 10931 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 10932 | |
| 10933 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10934 | argv = skip_dash_dash(argv); |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10935 | if (argv[0] == NULL) { |
| 10936 | #if ENABLE_HUSH_TRAP |
| 10937 | if (G.pre_trap_exitcode >= 0) /* "exit" in trap uses $? from before the trap */ |
| 10938 | hush_exit(G.pre_trap_exitcode); |
| 10939 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10940 | hush_exit(G.last_exitcode); |
Denys Vlasenko | cc9ecd9 | 2020-02-21 02:18:06 +0100 | [diff] [blame] | 10941 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10942 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 10943 | xfunc_error_retval = 255; |
| 10944 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10945 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10946 | } |
| 10947 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10948 | #if ENABLE_HUSH_TYPE |
| 10949 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 10950 | static int FAST_FUNC builtin_type(char **argv) |
| 10951 | { |
| 10952 | int ret = EXIT_SUCCESS; |
| 10953 | |
| 10954 | while (*++argv) { |
| 10955 | const char *type; |
| 10956 | char *path = NULL; |
| 10957 | |
| 10958 | if (0) {} /* make conditional compile easier below */ |
| 10959 | /*else if (find_alias(*argv)) |
| 10960 | type = "an alias";*/ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10961 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10962 | else if (find_function(*argv)) |
| 10963 | type = "a function"; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 10964 | # endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10965 | else if (find_builtin(*argv)) |
| 10966 | type = "a shell builtin"; |
| 10967 | else if ((path = find_in_path(*argv)) != NULL) |
| 10968 | type = path; |
| 10969 | else { |
| 10970 | bb_error_msg("type: %s: not found", *argv); |
| 10971 | ret = EXIT_FAILURE; |
| 10972 | continue; |
| 10973 | } |
| 10974 | |
| 10975 | printf("%s is %s\n", *argv, type); |
| 10976 | free(path); |
| 10977 | } |
| 10978 | |
| 10979 | return ret; |
| 10980 | } |
| 10981 | #endif |
| 10982 | |
| 10983 | #if ENABLE_HUSH_READ |
| 10984 | /* Interruptibility of read builtin in bash |
| 10985 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 10986 | * |
| 10987 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 10988 | * |
| 10989 | * SIGINT: |
| 10990 | * - terminates non-interactive shell; |
| 10991 | * - interrupts read in interactive shell; |
| 10992 | * if it has non-empty trap: |
| 10993 | * - executes trap and returns to command prompt in interactive shell; |
| 10994 | * - executes trap and returns to read in non-interactive shell; |
| 10995 | * SIGTERM: |
| 10996 | * - is ignored (does not interrupt) read in interactive shell; |
| 10997 | * - terminates non-interactive shell; |
| 10998 | * if it has non-empty trap: |
| 10999 | * - executes trap and returns to read; |
| 11000 | * SIGHUP: |
| 11001 | * - terminates shell (regardless of interactivity); |
| 11002 | * if it has non-empty trap: |
| 11003 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 11004 | * SIGCHLD from children: |
| 11005 | * - does not interrupt read regardless of interactivity: |
| 11006 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11007 | */ |
| 11008 | static int FAST_FUNC builtin_read(char **argv) |
| 11009 | { |
| 11010 | const char *r; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 11011 | struct builtin_read_params params; |
| 11012 | |
| 11013 | memset(¶ms, 0, sizeof(params)); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11014 | |
| 11015 | /* "!": do not abort on errors. |
| 11016 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 11017 | */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 11018 | params.read_flags = getopt32(argv, |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11019 | # if BASH_READ_D |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 11020 | IF_NOT_HUSH_BASH_COMPAT("^") |
| 11021 | "!srn:p:t:u:d:" IF_NOT_HUSH_BASH_COMPAT("\0" "-1"/*min 1 arg*/), |
| 11022 | ¶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] | 11023 | # else |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 11024 | IF_NOT_HUSH_BASH_COMPAT("^") |
| 11025 | "!srn:p:t:u:" IF_NOT_HUSH_BASH_COMPAT("\0" "-1"/*min 1 arg*/), |
| 11026 | ¶ms.opt_n, ¶ms.opt_p, ¶ms.opt_t, ¶ms.opt_u |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11027 | # endif |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 11028 | //TODO: print "read: need variable name" |
| 11029 | //for the case of !BASH "read" with no args (now it fails silently) |
| 11030 | //(or maybe extend getopt32() to emit a message if "-1" fails) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 11031 | ); |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 11032 | if ((uint32_t)params.read_flags == (uint32_t)-1) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11033 | return EXIT_FAILURE; |
| 11034 | argv += optind; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 11035 | params.argv = argv; |
| 11036 | params.setvar = set_local_var_from_halves; |
| 11037 | params.ifs = get_local_var_value("IFS"); /* can be NULL */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11038 | |
| 11039 | again: |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 11040 | r = shell_builtin_read(¶ms); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11041 | |
| 11042 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 11043 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 11044 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11045 | goto again; |
| 11046 | } |
| 11047 | |
| 11048 | if ((uintptr_t)r > 1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11049 | bb_simple_error_msg(r); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11050 | r = (char*)(uintptr_t)1; |
| 11051 | } |
| 11052 | |
| 11053 | return (uintptr_t)r; |
| 11054 | } |
| 11055 | #endif |
| 11056 | |
| 11057 | #if ENABLE_HUSH_UMASK |
| 11058 | static int FAST_FUNC builtin_umask(char **argv) |
| 11059 | { |
| 11060 | int rc; |
| 11061 | mode_t mask; |
| 11062 | |
| 11063 | rc = 1; |
| 11064 | mask = umask(0); |
| 11065 | argv = skip_dash_dash(argv); |
| 11066 | if (argv[0]) { |
| 11067 | mode_t old_mask = mask; |
| 11068 | |
| 11069 | /* numeric umasks are taken as-is */ |
| 11070 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 11071 | if (!isdigit(argv[0][0])) |
| 11072 | mask ^= 0777; |
| 11073 | mask = bb_parse_mode(argv[0], mask); |
| 11074 | if (!isdigit(argv[0][0])) |
| 11075 | mask ^= 0777; |
| 11076 | if ((unsigned)mask > 0777) { |
| 11077 | mask = old_mask; |
| 11078 | /* bash messages: |
| 11079 | * bash: umask: 'q': invalid symbolic mode operator |
| 11080 | * bash: umask: 999: octal number out of range |
| 11081 | */ |
| 11082 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 11083 | rc = 0; |
| 11084 | } |
| 11085 | } else { |
| 11086 | /* Mimic bash */ |
| 11087 | printf("%04o\n", (unsigned) mask); |
| 11088 | /* fall through and restore mask which we set to 0 */ |
| 11089 | } |
| 11090 | umask(mask); |
| 11091 | |
| 11092 | return !rc; /* rc != 0 - success */ |
| 11093 | } |
| 11094 | #endif |
| 11095 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 11096 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11097 | static void print_escaped(const char *s) |
| 11098 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 11099 | if (*s == '\'') |
| 11100 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11101 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 11102 | const char *p = strchrnul(s, '\''); |
| 11103 | /* print 'xxxx', possibly just '' */ |
| 11104 | printf("'%.*s'", (int)(p - s), s); |
| 11105 | if (*p == '\0') |
| 11106 | break; |
| 11107 | s = p; |
| 11108 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11109 | /* s points to '; print "'''...'''" */ |
| 11110 | putchar('"'); |
| 11111 | do putchar('\''); while (*++s == '\''); |
| 11112 | putchar('"'); |
| 11113 | } while (*s); |
| 11114 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 11115 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11116 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11117 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11118 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11119 | { |
| 11120 | do { |
| 11121 | char *name = *argv; |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 11122 | const char *name_end = endofname(name); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11123 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 11124 | if (*name_end == '\0') { |
| 11125 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11126 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 11127 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 11128 | var = vpp ? *vpp : NULL; |
| 11129 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11130 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11131 | /* export -n NAME (without =VALUE) */ |
| 11132 | if (var) { |
| 11133 | var->flg_export = 0; |
| 11134 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 11135 | unsetenv(name); |
| 11136 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 11137 | continue; |
| 11138 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11139 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11140 | /* export NAME (without =VALUE) */ |
| 11141 | if (var) { |
| 11142 | var->flg_export = 1; |
| 11143 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 11144 | putenv(var->varstr); |
| 11145 | continue; |
| 11146 | } |
| 11147 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 11148 | if (flags & SETFLAG_MAKE_RO) { |
| 11149 | /* readonly NAME (without =VALUE) */ |
| 11150 | if (var) { |
| 11151 | var->flg_read_only = 1; |
| 11152 | continue; |
| 11153 | } |
| 11154 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 11155 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 11156 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11157 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 11158 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 11159 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 11160 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 11161 | continue; |
| 11162 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11163 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 11164 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11165 | /* Exporting non-existing variable. |
| 11166 | * bash does not put it in environment, |
| 11167 | * but remembers that it is exported, |
| 11168 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11169 | * We just set it to "" and export. |
| 11170 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11171 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11172 | * bash sets the value to "". |
| 11173 | */ |
| 11174 | /* Or, it's "readonly NAME" (without =VALUE). |
| 11175 | * bash remembers NAME and disallows its creation |
| 11176 | * in the future. |
| 11177 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11178 | name = xasprintf("%s=", name); |
| 11179 | } else { |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 11180 | if (*name_end != '=') { |
| 11181 | bb_error_msg("'%s': bad variable name", name); |
| 11182 | /* do not parse following argv[]s: */ |
| 11183 | return 1; |
| 11184 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11185 | /* (Un)exporting/making local NAME=VALUE */ |
| 11186 | name = xstrdup(name); |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 11187 | /* Testcase: export PS1='\w \$ ' */ |
| 11188 | unbackslash(name); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11189 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 11190 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 11191 | if (set_local_var(name, flags)) |
| 11192 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11193 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11194 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11195 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 11196 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11197 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 11198 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11199 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11200 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 11201 | unsigned opt_unexport; |
| 11202 | |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11203 | # if ENABLE_HUSH_EXPORT_N |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 11204 | /* "!": do not abort on errors */ |
| 11205 | opt_unexport = getopt32(argv, "!n"); |
| 11206 | if (opt_unexport == (uint32_t)-1) |
| 11207 | return EXIT_FAILURE; |
| 11208 | argv += optind; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11209 | # else |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 11210 | opt_unexport = 0; |
| 11211 | argv++; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11212 | # endif |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 11213 | |
| 11214 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11215 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 11216 | if (e) { |
| 11217 | while (*e) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11218 | # if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11219 | puts(*e++); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11220 | # else |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 11221 | /* ash emits: export VAR='VAL' |
| 11222 | * bash: declare -x VAR="VAL" |
| 11223 | * we follow ash example */ |
| 11224 | const char *s = *e++; |
| 11225 | const char *p = strchr(s, '='); |
| 11226 | |
| 11227 | if (!p) /* wtf? take next variable */ |
| 11228 | continue; |
| 11229 | /* export var= */ |
| 11230 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11231 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 11232 | putchar('\n'); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11233 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 11234 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 11235 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 11236 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11237 | return EXIT_SUCCESS; |
| 11238 | } |
| 11239 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11240 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11241 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 11242 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11243 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11244 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11245 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 11246 | { |
| 11247 | if (G.func_nest_level == 0) { |
| 11248 | bb_error_msg("%s: not in a function", argv[0]); |
| 11249 | return EXIT_FAILURE; /* bash compat */ |
| 11250 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11251 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 11252 | /* Since all builtins run in a nested variable level, |
| 11253 | * need to use level - 1 here. Or else the variable will be removed at once |
| 11254 | * after builtin returns. |
| 11255 | */ |
| 11256 | 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] | 11257 | } |
| 11258 | #endif |
| 11259 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11260 | #if ENABLE_HUSH_READONLY |
| 11261 | static int FAST_FUNC builtin_readonly(char **argv) |
| 11262 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11263 | argv++; |
| 11264 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11265 | /* bash: readonly [-p]: list all readonly VARs |
| 11266 | * (-p has no effect in bash) |
| 11267 | */ |
| 11268 | struct variable *e; |
| 11269 | for (e = G.top_var; e; e = e->next) { |
| 11270 | if (e->flg_read_only) { |
| 11271 | //TODO: quote value: readonly VAR='VAL' |
| 11272 | printf("readonly %s\n", e->varstr); |
| 11273 | } |
| 11274 | } |
| 11275 | return EXIT_SUCCESS; |
| 11276 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 11277 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 11278 | } |
| 11279 | #endif |
| 11280 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11281 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11282 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 11283 | static int FAST_FUNC builtin_unset(char **argv) |
| 11284 | { |
| 11285 | int ret; |
| 11286 | unsigned opts; |
| 11287 | |
| 11288 | /* "!": do not abort on errors */ |
| 11289 | /* "+": stop at 1st non-option */ |
| 11290 | opts = getopt32(argv, "!+vf"); |
| 11291 | if (opts == (unsigned)-1) |
| 11292 | return EXIT_FAILURE; |
| 11293 | if (opts == 3) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11294 | bb_simple_error_msg("unset: -v and -f are exclusive"); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11295 | return EXIT_FAILURE; |
| 11296 | } |
| 11297 | argv += optind; |
| 11298 | |
| 11299 | ret = EXIT_SUCCESS; |
| 11300 | while (*argv) { |
| 11301 | if (!(opts & 2)) { /* not -f */ |
| 11302 | if (unset_local_var(*argv)) { |
| 11303 | /* unset <nonexistent_var> doesn't fail. |
| 11304 | * Error is when one tries to unset RO var. |
| 11305 | * Message was printed by unset_local_var. */ |
| 11306 | ret = EXIT_FAILURE; |
| 11307 | } |
| 11308 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11309 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11310 | else { |
| 11311 | unset_func(*argv); |
| 11312 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11313 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11314 | argv++; |
| 11315 | } |
| 11316 | return ret; |
| 11317 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11318 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11319 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11320 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11321 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 11322 | * built-in 'set' handler |
| 11323 | * SUSv3 says: |
| 11324 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 11325 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 11326 | * set -- [argument...] |
| 11327 | * set -o |
| 11328 | * set +o |
| 11329 | * Implementations shall support the options in both their hyphen and |
| 11330 | * plus-sign forms. These options can also be specified as options to sh. |
| 11331 | * Examples: |
| 11332 | * Write out all variables and their values: set |
| 11333 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 11334 | * Turn on the -x and -v options: set -xv |
| 11335 | * Unset all positional parameters: set -- |
| 11336 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 11337 | * Set the positional parameters to the expansion of x, even if x expands |
| 11338 | * with a leading '-' or '+': set -- $x |
| 11339 | * |
| 11340 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 11341 | */ |
| 11342 | static int FAST_FUNC builtin_set(char **argv) |
| 11343 | { |
| 11344 | int n; |
| 11345 | char **pp, **g_argv; |
| 11346 | char *arg = *++argv; |
| 11347 | |
| 11348 | if (arg == NULL) { |
| 11349 | struct variable *e; |
| 11350 | for (e = G.top_var; e; e = e->next) |
| 11351 | puts(e->varstr); |
| 11352 | return EXIT_SUCCESS; |
| 11353 | } |
| 11354 | |
| 11355 | do { |
| 11356 | if (strcmp(arg, "--") == 0) { |
| 11357 | ++argv; |
| 11358 | goto set_argv; |
| 11359 | } |
| 11360 | if (arg[0] != '+' && arg[0] != '-') |
| 11361 | break; |
| 11362 | for (n = 1; arg[n]; ++n) { |
Denys Vlasenko | 18a90ec | 2019-09-05 14:07:14 +0200 | [diff] [blame] | 11363 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) { |
| 11364 | bb_error_msg("%s: %s: invalid option", "set", arg); |
| 11365 | return EXIT_FAILURE; |
| 11366 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11367 | if (arg[n] == 'o' && argv[1]) |
| 11368 | argv++; |
| 11369 | } |
| 11370 | } while ((arg = *++argv) != NULL); |
| 11371 | /* Now argv[0] is 1st argument */ |
| 11372 | |
| 11373 | if (arg == NULL) |
| 11374 | return EXIT_SUCCESS; |
| 11375 | set_argv: |
| 11376 | |
| 11377 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 11378 | g_argv = G.global_argv; |
| 11379 | if (G.global_args_malloced) { |
| 11380 | pp = g_argv; |
| 11381 | while (*++pp) |
| 11382 | free(*pp); |
| 11383 | g_argv[1] = NULL; |
| 11384 | } else { |
| 11385 | G.global_args_malloced = 1; |
| 11386 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 11387 | pp[0] = g_argv[0]; /* retain $0 */ |
| 11388 | g_argv = pp; |
| 11389 | } |
| 11390 | /* This realloc's G.global_argv */ |
| 11391 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 11392 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 11393 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11394 | |
| 11395 | return EXIT_SUCCESS; |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11396 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 11397 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11398 | |
| 11399 | static int FAST_FUNC builtin_shift(char **argv) |
| 11400 | { |
| 11401 | int n = 1; |
| 11402 | argv = skip_dash_dash(argv); |
| 11403 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 11404 | n = bb_strtou(argv[0], NULL, 10); |
| 11405 | if (errno || n < 0) { |
| 11406 | /* shared string with ash.c */ |
| 11407 | bb_error_msg("Illegal number: %s", argv[0]); |
| 11408 | /* |
| 11409 | * ash aborts in this case. |
| 11410 | * bash prints error message and set $? to 1. |
| 11411 | * Interestingly, for "shift 99999" bash does not |
| 11412 | * print error message, but does set $? to 1 |
| 11413 | * (and does no shifting at all). |
| 11414 | */ |
| 11415 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11416 | } |
| 11417 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 11418 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11419 | int m = 1; |
| 11420 | while (m <= n) |
| 11421 | free(G.global_argv[m++]); |
| 11422 | } |
| 11423 | G.global_argc -= n; |
| 11424 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 11425 | G.global_argc * sizeof(G.global_argv[0])); |
| 11426 | return EXIT_SUCCESS; |
| 11427 | } |
| 11428 | return EXIT_FAILURE; |
| 11429 | } |
| 11430 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11431 | #if ENABLE_HUSH_GETOPTS |
| 11432 | static int FAST_FUNC builtin_getopts(char **argv) |
| 11433 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11434 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 11435 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11436 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11437 | If a required argument is not found, and getopts is not silent, |
| 11438 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 11439 | diagnostic message is printed. If getopts is silent, then a |
| 11440 | colon (:) is placed in VAR and OPTARG is set to the option |
| 11441 | character found. |
| 11442 | |
| 11443 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11444 | |
| 11445 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11446 | */ |
| 11447 | char cbuf[2]; |
| 11448 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11449 | int c, n, exitcode, my_opterr; |
| 11450 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11451 | |
| 11452 | optstring = *++argv; |
| 11453 | if (!optstring || !(var = *++argv)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11454 | bb_simple_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11455 | return EXIT_FAILURE; |
| 11456 | } |
| 11457 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11458 | if (argv[1]) |
| 11459 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 11460 | else |
| 11461 | argv = G.global_argv; |
| 11462 | cbuf[1] = '\0'; |
| 11463 | |
| 11464 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11465 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11466 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 11467 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11468 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11469 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11470 | |
| 11471 | /* getopts stops on first non-option. Add "+" to force that */ |
| 11472 | /*if (optstring[0] != '+')*/ { |
| 11473 | char *s = alloca(strlen(optstring) + 2); |
| 11474 | sprintf(s, "+%s", optstring); |
| 11475 | optstring = s; |
| 11476 | } |
| 11477 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11478 | /* Naively, now we should just |
| 11479 | * cp = get_local_var_value("OPTIND"); |
| 11480 | * optind = cp ? atoi(cp) : 0; |
| 11481 | * optarg = NULL; |
| 11482 | * opterr = my_opterr; |
| 11483 | * c = getopt(string_array_len(argv), argv, optstring); |
| 11484 | * and be done? Not so fast... |
| 11485 | * Unlike normal getopt() usage in C programs, here |
| 11486 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 11487 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 11488 | * invocations of "getopts", there will be calls to shell builtins |
| 11489 | * which use getopt() internally. Example: |
| 11490 | * while getopts "abc" RES -a -bc -abc de; do |
| 11491 | * unset -ff func |
| 11492 | * done |
| 11493 | * This would not work correctly: getopt() call inside "unset" |
| 11494 | * modifies internal libc state which is tracking position in |
| 11495 | * multi-option strings ("-abc"). At best, it can skip options |
| 11496 | * or return the same option infinitely. With glibc implementation |
| 11497 | * of getopt(), it would use outright invalid pointers and return |
| 11498 | * garbage even _without_ "unset" mangling internal state. |
| 11499 | * |
| 11500 | * We resort to resetting getopt() state and calling it N times, |
| 11501 | * until we get Nth result (or failure). |
| 11502 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 11503 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11504 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11505 | count = 0; |
| 11506 | n = string_array_len(argv); |
| 11507 | do { |
| 11508 | optarg = NULL; |
| 11509 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 11510 | c = getopt(n, argv, optstring); |
| 11511 | if (c < 0) |
| 11512 | break; |
| 11513 | count++; |
| 11514 | } while (count <= G.getopt_count); |
| 11515 | |
| 11516 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 11517 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 11518 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 11519 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11520 | |
| 11521 | /* Set OPTARG */ |
| 11522 | /* Always set or unset, never left as-is, even on exit/error: |
| 11523 | * "If no option was found, or if the option that was found |
| 11524 | * does not have an option-argument, OPTARG shall be unset." |
| 11525 | */ |
| 11526 | cp = optarg; |
| 11527 | if (c == '?') { |
| 11528 | /* If ":optstring" and unknown option is seen, |
| 11529 | * it is stored to OPTARG. |
| 11530 | */ |
| 11531 | if (optstring[1] == ':') { |
| 11532 | cbuf[0] = optopt; |
| 11533 | cp = cbuf; |
| 11534 | } |
| 11535 | } |
| 11536 | if (cp) |
| 11537 | set_local_var_from_halves("OPTARG", cp); |
| 11538 | else |
| 11539 | unset_local_var("OPTARG"); |
| 11540 | |
| 11541 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11542 | exitcode = EXIT_SUCCESS; |
| 11543 | if (c < 0) { /* -1: end of options */ |
| 11544 | exitcode = EXIT_FAILURE; |
| 11545 | c = '?'; |
| 11546 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 11547 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 11548 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11549 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11550 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 11551 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 11552 | return exitcode; |
| 11553 | } |
| 11554 | #endif |
| 11555 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11556 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11557 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11558 | char *arg_path, *filename; |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11559 | HFILE *input; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11560 | save_arg_t sv; |
| 11561 | char *args_need_save; |
| 11562 | #if ENABLE_HUSH_FUNCTIONS |
| 11563 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11564 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 11565 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11566 | argv = skip_dash_dash(argv); |
| 11567 | filename = argv[0]; |
| 11568 | if (!filename) { |
| 11569 | /* bash says: "bash: .: filename argument required" */ |
| 11570 | return 2; /* bash compat */ |
| 11571 | } |
| 11572 | arg_path = NULL; |
| 11573 | if (!strchr(filename, '/')) { |
| 11574 | arg_path = find_in_path(filename); |
| 11575 | if (arg_path) |
| 11576 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 11577 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 11578 | errno = ENOENT; |
| 11579 | bb_simple_perror_msg(filename); |
| 11580 | return EXIT_FAILURE; |
| 11581 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11582 | } |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11583 | input = hfopen(filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11584 | free(arg_path); |
| 11585 | if (!input) { |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11586 | bb_perror_msg("%s", filename); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11587 | /* POSIX: non-interactive shell should abort here, |
| 11588 | * not merely fail. So far no one complained :) |
| 11589 | */ |
| 11590 | return EXIT_FAILURE; |
| 11591 | } |
| 11592 | |
| 11593 | #if ENABLE_HUSH_FUNCTIONS |
| 11594 | sv_flg = G_flag_return_in_progress; |
| 11595 | /* "we are inside sourced file, ok to use return" */ |
| 11596 | G_flag_return_in_progress = -1; |
| 11597 | #endif |
| 11598 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 11599 | if (args_need_save) |
| 11600 | save_and_replace_G_args(&sv, argv); |
| 11601 | |
| 11602 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 11603 | G.last_exitcode = 0; |
| 11604 | parse_and_run_file(input); |
Denys Vlasenko | 41ef41b | 2018-07-24 16:54:41 +0200 | [diff] [blame] | 11605 | hfclose(input); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11606 | |
| 11607 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 11608 | restore_G_args(&sv, argv); |
| 11609 | #if ENABLE_HUSH_FUNCTIONS |
| 11610 | G_flag_return_in_progress = sv_flg; |
| 11611 | #endif |
| 11612 | |
| 11613 | return G.last_exitcode; |
| 11614 | } |
| 11615 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11616 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11617 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11618 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11619 | int sig; |
| 11620 | char *new_cmd; |
| 11621 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11622 | if (!G_traps) |
| 11623 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11624 | |
| 11625 | argv++; |
| 11626 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 11627 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11628 | /* No args: print all trapped */ |
| 11629 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11630 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11631 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11632 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 11633 | /* note: bash adds "SIG", but only if invoked |
| 11634 | * as "bash". If called as "sh", or if set -o posix, |
| 11635 | * then it prints short signal names. |
| 11636 | * We are printing short names: */ |
| 11637 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11638 | } |
| 11639 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 11640 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11641 | return EXIT_SUCCESS; |
| 11642 | } |
| 11643 | |
| 11644 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11645 | /* If first arg is a number: reset all specified signals */ |
| 11646 | sig = bb_strtou(*argv, NULL, 10); |
| 11647 | if (errno == 0) { |
| 11648 | int ret; |
| 11649 | process_sig_list: |
| 11650 | ret = EXIT_SUCCESS; |
| 11651 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11652 | sighandler_t handler; |
| 11653 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11654 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 11655 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11656 | ret = EXIT_FAILURE; |
| 11657 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 11658 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11659 | continue; |
| 11660 | } |
| 11661 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11662 | free(G_traps[sig]); |
| 11663 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11664 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 11665 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11666 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11667 | |
| 11668 | /* There is no signal for 0 (EXIT) */ |
| 11669 | if (sig == 0) |
| 11670 | continue; |
| 11671 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11672 | if (new_cmd) |
| 11673 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 11674 | else |
| 11675 | /* We are removing trap handler */ |
| 11676 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 11677 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11678 | } |
| 11679 | return ret; |
| 11680 | } |
| 11681 | |
| 11682 | if (!argv[1]) { /* no second arg */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11683 | bb_simple_error_msg("trap: invalid arguments"); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11684 | return EXIT_FAILURE; |
| 11685 | } |
| 11686 | |
| 11687 | /* First arg is "-": reset all specified to default */ |
| 11688 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 11689 | /* Everything else: set arg as signal handler |
| 11690 | * (includes "" case, which ignores signal) */ |
| 11691 | if (argv[0][0] == '-') { |
| 11692 | if (argv[0][1] == '\0') { /* "-" */ |
| 11693 | /* new_cmd remains NULL: "reset these sigs" */ |
| 11694 | goto reset_traps; |
| 11695 | } |
| 11696 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 11697 | argv++; |
| 11698 | } |
| 11699 | /* else: "-something", no special meaning */ |
| 11700 | } |
| 11701 | new_cmd = *argv; |
| 11702 | reset_traps: |
| 11703 | argv++; |
| 11704 | goto process_sig_list; |
| 11705 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 11706 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 11707 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11708 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11709 | static struct pipe *parse_jobspec(const char *str) |
| 11710 | { |
| 11711 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11712 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11713 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11714 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 11715 | if (str[0] != '%' |
| 11716 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 11717 | ) { |
| 11718 | bb_error_msg("bad argument '%s'", str); |
| 11719 | return NULL; |
| 11720 | } |
| 11721 | /* It is "%%", "%+" or "%" - current job */ |
| 11722 | jobnum = G.last_jobid; |
| 11723 | if (jobnum == 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11724 | bb_simple_error_msg("no current job"); |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11725 | return NULL; |
| 11726 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11727 | } |
| 11728 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11729 | if (pi->jobid == jobnum) { |
| 11730 | return pi; |
| 11731 | } |
| 11732 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11733 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11734 | return NULL; |
| 11735 | } |
| 11736 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11737 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 11738 | { |
| 11739 | struct pipe *job; |
| 11740 | const char *status_string; |
| 11741 | |
| 11742 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 11743 | for (job = G.job_list; job; job = job->next) { |
| 11744 | if (job->alive_cmds == job->stopped_cmds) |
| 11745 | status_string = "Stopped"; |
| 11746 | else |
| 11747 | status_string = "Running"; |
| 11748 | |
| 11749 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 11750 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11751 | |
| 11752 | clean_up_last_dead_job(); |
| 11753 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11754 | return EXIT_SUCCESS; |
| 11755 | } |
| 11756 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11757 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11758 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11759 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11760 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11761 | struct pipe *pi; |
| 11762 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11763 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11764 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 11765 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11766 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 11767 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11768 | for (pi = G.job_list; pi; pi = pi->next) { |
| 11769 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11770 | goto found; |
| 11771 | } |
| 11772 | } |
| 11773 | bb_error_msg("%s: no current job", argv[0]); |
| 11774 | return EXIT_FAILURE; |
| 11775 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 11776 | |
| 11777 | pi = parse_jobspec(argv[1]); |
| 11778 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11779 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11780 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 11781 | /* TODO: bash prints a string representation |
| 11782 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 11783 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denys Vlasenko | 62f1eed | 2021-10-12 22:39:11 +0200 | [diff] [blame] | 11784 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 11785 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11786 | } |
| 11787 | |
| 11788 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11789 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 11790 | for (i = 0; i < pi->num_cmds; i++) { |
| 11791 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11792 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 11793 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11794 | |
| 11795 | i = kill(- pi->pgrp, SIGCONT); |
| 11796 | if (i < 0) { |
| 11797 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11798 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11799 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11800 | } |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 11801 | bb_simple_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 11802 | } |
| 11803 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 11804 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 11805 | 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] | 11806 | return checkjobs_and_fg_shell(pi); |
| 11807 | } |
| 11808 | return EXIT_SUCCESS; |
| 11809 | } |
| 11810 | #endif |
| 11811 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11812 | #if ENABLE_HUSH_KILL |
| 11813 | static int FAST_FUNC builtin_kill(char **argv) |
| 11814 | { |
| 11815 | int ret = 0; |
| 11816 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11817 | # if ENABLE_HUSH_JOB |
| 11818 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 11819 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11820 | |
| 11821 | do { |
| 11822 | struct pipe *pi; |
| 11823 | char *dst; |
| 11824 | int j, n; |
| 11825 | |
| 11826 | if (argv[i][0] != '%') |
| 11827 | continue; |
| 11828 | /* |
| 11829 | * "kill %N" - job kill |
| 11830 | * Converting to pgrp / pid kill |
| 11831 | */ |
| 11832 | pi = parse_jobspec(argv[i]); |
| 11833 | if (!pi) { |
| 11834 | /* Eat bad jobspec */ |
| 11835 | j = i; |
| 11836 | do { |
| 11837 | j++; |
| 11838 | argv[j - 1] = argv[j]; |
| 11839 | } while (argv[j]); |
| 11840 | ret = 1; |
| 11841 | i--; |
| 11842 | continue; |
| 11843 | } |
| 11844 | /* |
| 11845 | * In jobs started under job control, we signal |
| 11846 | * entire process group by kill -PGRP_ID. |
| 11847 | * This happens, f.e., in interactive shell. |
| 11848 | * |
| 11849 | * Otherwise, we signal each child via |
| 11850 | * kill PID1 PID2 PID3. |
| 11851 | * Testcases: |
| 11852 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 11853 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 11854 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 11855 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 11856 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11857 | dst = alloca(n * sizeof(int)*4); |
| 11858 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11859 | if (G_interactive_fd) |
| 11860 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11861 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11862 | struct command *cmd = &pi->cmds[j]; |
| 11863 | /* Skip exited members of the job */ |
| 11864 | if (cmd->pid == 0) |
| 11865 | continue; |
| 11866 | /* |
| 11867 | * kill_main has matching code to expect |
| 11868 | * leading space. Needed to not confuse |
| 11869 | * negative pids with "kill -SIGNAL_NO" syntax |
| 11870 | */ |
| 11871 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 11872 | } |
| 11873 | *dst = '\0'; |
| 11874 | } while (argv[++i]); |
| 11875 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11876 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11877 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11878 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11879 | ret = run_applet_main(argv, kill_main); |
| 11880 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 11881 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11882 | return ret; |
| 11883 | } |
| 11884 | #endif |
| 11885 | |
| 11886 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11887 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11888 | # if !ENABLE_HUSH_JOB |
| 11889 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 11890 | # endif |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11891 | 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] | 11892 | { |
| 11893 | int ret = 0; |
| 11894 | for (;;) { |
| 11895 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11896 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11897 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11898 | if (!sigisemptyset(&G.pending_set)) |
| 11899 | goto check_sig; |
| 11900 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11901 | /* waitpid is not interruptible by SA_RESTARTed |
| 11902 | * signals which we use. Thus, this ugly dance: |
| 11903 | */ |
| 11904 | |
| 11905 | /* Make sure possible SIGCHLD is stored in kernel's |
| 11906 | * pending signal mask before we call waitpid. |
| 11907 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11908 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11909 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11910 | sigfillset(&oldset); /* block all signals, remember old set */ |
Denys Vlasenko | b437df1 | 2018-12-08 15:35:24 +0100 | [diff] [blame] | 11911 | sigprocmask2(SIG_SETMASK, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11912 | |
| 11913 | if (!sigisemptyset(&G.pending_set)) { |
| 11914 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11915 | goto restore; |
| 11916 | } |
| 11917 | |
| 11918 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11919 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11920 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11921 | debug_printf_exec("checkjobs:%d\n", ret); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11922 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11923 | if (waitfor_pipe) { |
| 11924 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 11925 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 11926 | if (rcode >= 0) { |
| 11927 | ret = rcode; |
| 11928 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11929 | break; |
| 11930 | } |
| 11931 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11932 | # endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11933 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 11934 | /* if ret == 0, no children changed state */ |
| 11935 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11936 | if (errno == ECHILD || ret) { |
| 11937 | ret--; |
| 11938 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11939 | ret = 0; |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11940 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11941 | if (waitfor_pid == -1 && errno == ECHILD) { |
| 11942 | /* exitcode of "wait -n" with no children is 127, not 0 */ |
| 11943 | ret = 127; |
| 11944 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11945 | # endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11946 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11947 | break; |
| 11948 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11949 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11950 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 11951 | /* Note: sigsuspend invokes signal handler */ |
| 11952 | sigsuspend(&oldset); |
Denys Vlasenko | 23bc562 | 2020-02-18 16:46:01 +0100 | [diff] [blame] | 11953 | /* ^^^ add "sigdelset(&oldset, SIGCHLD)" before sigsuspend |
| 11954 | * to make sure SIGCHLD is not masked off? |
| 11955 | * It was reported that this: |
| 11956 | * fn() { : | return; } |
| 11957 | * shopt -s lastpipe |
| 11958 | * fn |
| 11959 | * exec hush SCRIPT |
| 11960 | * under bash 4.4.23 runs SCRIPT with SIGCHLD masked, |
| 11961 | * making "wait" commands in SCRIPT block forever. |
| 11962 | */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11963 | restore: |
| 11964 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11965 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11966 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11967 | sig = check_and_run_traps(); |
| 11968 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 11969 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 11970 | ret = 128 | sig; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11971 | break; |
| 11972 | } |
| 11973 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 11974 | } |
| 11975 | return ret; |
| 11976 | } |
| 11977 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11978 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11979 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11980 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11981 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11982 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11983 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11984 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11985 | if (argv[0] && strcmp(argv[0], "-n") == 0) { |
Denys Vlasenko | 4d1c514 | 2019-03-26 18:34:06 +0100 | [diff] [blame] | 11986 | /* wait -n */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 11987 | /* (bash accepts "wait -n PID" too and ignores PID) */ |
| 11988 | G.dead_job_exitcode = -1; |
| 11989 | 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] | 11990 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 11991 | # endif |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11992 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 11993 | /* Don't care about wait results */ |
| 11994 | /* Note 1: must wait until there are no more children */ |
| 11995 | /* Note 2: must be interruptible */ |
| 11996 | /* Examples: |
| 11997 | * $ sleep 3 & sleep 6 & wait |
| 11998 | * [1] 30934 sleep 3 |
| 11999 | * [2] 30935 sleep 6 |
| 12000 | * [1] Done sleep 3 |
| 12001 | * [2] Done sleep 6 |
| 12002 | * $ sleep 3 & sleep 6 & wait |
| 12003 | * [1] 30936 sleep 3 |
| 12004 | * [2] 30937 sleep 6 |
| 12005 | * [1] Done sleep 3 |
| 12006 | * ^C <-- after ~4 sec from keyboard |
| 12007 | * $ |
| 12008 | */ |
Denys Vlasenko | e6f51ac | 2019-03-27 18:34:10 +0100 | [diff] [blame] | 12009 | 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] | 12010 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 12011 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12012 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 12013 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12014 | if (errno || pid <= 0) { |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 12015 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 12016 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12017 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 12018 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 12019 | wait_pipe = parse_jobspec(*argv); |
| 12020 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12021 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 12022 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12023 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 12024 | } else { |
| 12025 | /* waiting on "last dead job" removes it */ |
| 12026 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 12027 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 12028 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 12029 | /* else: parse_jobspec() already emitted error msg */ |
| 12030 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 12031 | } |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 12032 | # endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 12033 | /* mimic bash message */ |
| 12034 | 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] | 12035 | ret = EXIT_FAILURE; |
| 12036 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 12037 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12038 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12039 | /* Do we have such child? */ |
| 12040 | ret = waitpid(pid, &status, WNOHANG); |
| 12041 | if (ret < 0) { |
| 12042 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 12043 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12044 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 12045 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 12046 | /* "wait $!" but last bg task has already exited. Try: |
| 12047 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 12048 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 12049 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 12050 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 12051 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 12052 | } else { |
| 12053 | /* Example: "wait 1". mimic bash message */ |
Denys Vlasenko | 259747c | 2019-11-28 10:28:14 +0100 | [diff] [blame] | 12054 | 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] | 12055 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12056 | } else { |
| 12057 | /* ??? */ |
| 12058 | bb_perror_msg("wait %s", *argv); |
| 12059 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 12060 | continue; /* bash checks all argv[] */ |
| 12061 | } |
| 12062 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12063 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12064 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 12065 | } else { |
| 12066 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 12067 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 12068 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 12069 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 93e2a22 | 2020-12-23 12:23:21 +0100 | [diff] [blame] | 12070 | ret = 128 | WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 12071 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 12072 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 12073 | |
| 12074 | return ret; |
| 12075 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 12076 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 12077 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12078 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 12079 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 12080 | { |
| 12081 | if (argv[1]) { |
| 12082 | def = bb_strtou(argv[1], NULL, 10); |
| 12083 | if (errno || def < def_min || argv[2]) { |
| 12084 | bb_error_msg("%s: bad arguments", argv[0]); |
| 12085 | def = UINT_MAX; |
| 12086 | } |
| 12087 | } |
| 12088 | return def; |
| 12089 | } |
| 12090 | #endif |
| 12091 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 12092 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12093 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 12094 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12095 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 12096 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 12097 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 12098 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 12099 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 12100 | return EXIT_SUCCESS; /* bash compat */ |
| 12101 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 12102 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12103 | |
| 12104 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 12105 | if (depth == UINT_MAX) |
| 12106 | G.flag_break_continue = BC_BREAK; |
| 12107 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 12108 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12109 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 12110 | return EXIT_SUCCESS; |
| 12111 | } |
| 12112 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12113 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 12114 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 12115 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 12116 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 12117 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 12118 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12119 | |
| 12120 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12121 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12122 | { |
| 12123 | int rc; |
| 12124 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 12125 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12126 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 12127 | return EXIT_FAILURE; /* bash compat */ |
| 12128 | } |
| 12129 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 12130 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12131 | |
| 12132 | /* bash: |
| 12133 | * out of range: wraps around at 256, does not error out |
| 12134 | * non-numeric param: |
| 12135 | * f() { false; return qwe; }; f; echo $? |
| 12136 | * bash: return: qwe: numeric argument required <== we do this |
| 12137 | * 255 <== we also do this |
| 12138 | */ |
| 12139 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
Denys Vlasenko | bb095f4 | 2020-02-20 16:37:59 +0100 | [diff] [blame] | 12140 | # if ENABLE_HUSH_TRAP |
| 12141 | if (argv[1]) { /* "return ARG" inside a running trap sets $? */ |
| 12142 | debug_printf_exec("G.return_exitcode=%d\n", rc); |
| 12143 | G.return_exitcode = rc; |
| 12144 | } |
| 12145 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 12146 | return rc; |
| 12147 | } |
| 12148 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 12149 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 12150 | #if ENABLE_HUSH_TIMES |
| 12151 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 12152 | { |
| 12153 | static const uint8_t times_tbl[] ALIGN1 = { |
| 12154 | ' ', offsetof(struct tms, tms_utime), |
| 12155 | '\n', offsetof(struct tms, tms_stime), |
| 12156 | ' ', offsetof(struct tms, tms_cutime), |
| 12157 | '\n', offsetof(struct tms, tms_cstime), |
| 12158 | 0 |
| 12159 | }; |
| 12160 | const uint8_t *p; |
| 12161 | unsigned clk_tck; |
| 12162 | struct tms buf; |
| 12163 | |
| 12164 | clk_tck = bb_clk_tck(); |
| 12165 | |
| 12166 | times(&buf); |
| 12167 | p = times_tbl; |
| 12168 | do { |
| 12169 | unsigned sec, frac; |
| 12170 | unsigned long t; |
| 12171 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12172 | sec = t / clk_tck; |
| 12173 | frac = t % clk_tck; |
| 12174 | printf("%um%u.%03us%c", |
| 12175 | sec / 60, sec % 60, |
| 12176 | (frac * 1000) / clk_tck, |
| 12177 | p[0]); |
| 12178 | p += 2; |
| 12179 | } while (*p); |
| 12180 | |
| 12181 | return EXIT_SUCCESS; |
| 12182 | } |
| 12183 | #endif |
| 12184 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 12185 | #if ENABLE_HUSH_MEMLEAK |
| 12186 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 12187 | { |
| 12188 | void *p; |
| 12189 | unsigned long l; |
| 12190 | |
| 12191 | # ifdef M_TRIM_THRESHOLD |
| 12192 | /* Optional. Reduces probability of false positives */ |
| 12193 | malloc_trim(0); |
| 12194 | # endif |
| 12195 | /* Crude attempt to find where "free memory" starts, |
| 12196 | * sans fragmentation. */ |
| 12197 | p = malloc(240); |
| 12198 | l = (unsigned long)p; |
| 12199 | free(p); |
| 12200 | p = malloc(3400); |
| 12201 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 12202 | free(p); |
| 12203 | |
| 12204 | |
| 12205 | # if 0 /* debug */ |
| 12206 | { |
| 12207 | struct mallinfo mi = mallinfo(); |
| 12208 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 12209 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 12210 | } |
| 12211 | # endif |
| 12212 | |
| 12213 | if (!G.memleak_value) |
| 12214 | G.memleak_value = l; |
| 12215 | |
| 12216 | l -= G.memleak_value; |
| 12217 | if ((long)l < 0) |
| 12218 | l = 0; |
| 12219 | l /= 1024; |
| 12220 | if (l > 127) |
| 12221 | l = 127; |
| 12222 | |
| 12223 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 12224 | return l; |
| 12225 | } |
| 12226 | #endif |