Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3 | * A prototype Bourne shell grammar parser. |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7 | * |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com> |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10 | * |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 12 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 13 | * Credits: |
| 14 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 15 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 16 | * execution engine, the builtins, and much of the underlying |
| 17 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 19 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 20 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 21 | * Troan, which they placed in the public domain. I don't know |
| 22 | * how much of the Johnson/Troan code has survived the repeated |
| 23 | * rewrites. |
| 24 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 25 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 26 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 27 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 28 | * and many builtins derived from contributions by Erik Andersen. |
| 29 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 30 | * |
| 31 | * There are two big (and related) architecture differences between |
| 32 | * this parser and the lash parser. One is that this version is |
| 33 | * actually designed from the ground up to understand nearly all |
| 34 | * of the Bourne grammar. The second, consequential change is that |
| 35 | * the parser and input reader have been turned inside out. Now, |
| 36 | * the parser is in control, and asks for input as needed. The old |
| 37 | * way had the input reader in control, and it asked for parsing to |
| 38 | * take place as needed. The new way makes it much easier to properly |
| 39 | * handle the recursion implicit in the various substitutions, especially |
| 40 | * across continuation lines. |
| 41 | * |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 42 | * TODOs: |
| 43 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 44 | * make complex ${var%...} constructs support optional |
| 45 | * make here documents optional |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 46 | * special variables (done: PWD, PPID, RANDOM) |
| 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * tilde expansion |
| 49 | * aliases |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 50 | * "command" missing features: |
| 51 | * command -p CMD: run CMD using default $PATH |
| 52 | * (can use this to override standalone shell as well?) |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 53 | * command BLTIN: disables special-ness (e.g. errors do not abort) |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 54 | * command -V CMD1 CMD2 CMD3 (multiple args) (not in standard) |
| 55 | * builtins mandated by standards we don't support: |
| 56 | * [un]alias, fc: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 57 | * fc -l[nr] [BEG] [END]: list range of commands in history |
| 58 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands |
| 59 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 60 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 61 | * Bash compat TODO: |
| 62 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 63 | * reserved words: function select |
| 64 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 65 | * process substitution: <(list) and >(list) |
| 66 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 67 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 68 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 69 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 70 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 71 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 72 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 73 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 74 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 75 | * indirect expansion: ${!VAR} |
| 76 | * substring op on @: ${@:n:m} |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 77 | * |
| 78 | * Won't do: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 79 | * Some builtins mandated by standards: |
| 80 | * newgrp [GRP]: not a builtin in bash but a suid binary |
| 81 | * which spawns a new shell with new group ID |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 82 | * |
| 83 | * Status of [[ support: |
| 84 | * [[ args ]] are CMD_SINGLEWORD_NOGLOB: |
| 85 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 86 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 87 | * TODO: |
| 88 | * &&/|| are AND/OR ops, -a/-o are not |
| 89 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
| 90 | * = is glob match operator, not equality operator: STR = GLOB |
| 91 | * (in GLOB, quoting is significant on char-by-char basis: a*cd"*") |
| 92 | * == same as = |
| 93 | * add =~ regex match operator: STR =~ REGEX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 94 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 95 | //config:config HUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 96 | //config: bool "hush (64 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 97 | //config: default y |
| 98 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 99 | //config: hush is a small shell. It handles the normal flow control |
| 100 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 101 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 102 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 104 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 105 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 106 | //config: It does not handle select, aliases, tilde expansion, |
| 107 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 108 | //config: |
| 109 | //config:config HUSH_BASH_COMPAT |
| 110 | //config: bool "bash-compatible extensions" |
| 111 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 112 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 113 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 114 | //config:config HUSH_BRACE_EXPANSION |
| 115 | //config: bool "Brace expansion" |
| 116 | //config: default y |
| 117 | //config: depends on HUSH_BASH_COMPAT |
| 118 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 119 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 120 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 121 | //config:config HUSH_LINENO_VAR |
| 122 | //config: bool "$LINENO variable" |
| 123 | //config: default y |
| 124 | //config: depends on HUSH_BASH_COMPAT |
| 125 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 126 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 127 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 128 | //config: default n # do not encourage non-standard behavior |
| 129 | //config: depends on HUSH_BASH_COMPAT |
| 130 | //config: help |
| 131 | //config: This is not compliant with standards. Avoid if possible. |
| 132 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 133 | //config:config HUSH_INTERACTIVE |
| 134 | //config: bool "Interactive mode" |
| 135 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 136 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 137 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 138 | //config: Enable interactive mode (prompt and command editing). |
| 139 | //config: Without this, hush simply reads and executes commands |
| 140 | //config: from stdin just like a shell script from a file. |
| 141 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 143 | //config:config HUSH_SAVEHISTORY |
| 144 | //config: bool "Save command history to .hush_history" |
| 145 | //config: default y |
| 146 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 147 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 148 | //config:config HUSH_JOB |
| 149 | //config: bool "Job control" |
| 150 | //config: default y |
| 151 | //config: depends on HUSH_INTERACTIVE |
| 152 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 153 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 154 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 155 | //config: "cmd &" still works by simply spawning a process and immediately |
| 156 | //config: prompting for next command (or executing next command in a script), |
| 157 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 158 | //config: |
| 159 | //config:config HUSH_TICK |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 160 | //config: bool "Support process substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 161 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 162 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 163 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 164 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config: |
| 166 | //config:config HUSH_IF |
| 167 | //config: bool "Support if/then/elif/else/fi" |
| 168 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 169 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 170 | //config: |
| 171 | //config:config HUSH_LOOPS |
| 172 | //config: bool "Support for, while and until loops" |
| 173 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 174 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 175 | //config: |
| 176 | //config:config HUSH_CASE |
| 177 | //config: bool "Support case ... esac statement" |
| 178 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 179 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 180 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 181 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 182 | //config: |
| 183 | //config:config HUSH_FUNCTIONS |
| 184 | //config: bool "Support funcname() { commands; } syntax" |
| 185 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 186 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 187 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 188 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: |
| 190 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 191 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 192 | //config: default y |
| 193 | //config: depends on HUSH_FUNCTIONS |
| 194 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 195 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 196 | //config: |
| 197 | //config:config HUSH_RANDOM_SUPPORT |
| 198 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 199 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 200 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 201 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 202 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 203 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 204 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 205 | //config:config HUSH_MODE_X |
| 206 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 207 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 208 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 209 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 210 | //config: This instructs hush to print commands before execution. |
| 211 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 212 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 213 | //config:config HUSH_ECHO |
| 214 | //config: bool "echo builtin" |
| 215 | //config: default y |
| 216 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 217 | //config: |
| 218 | //config:config HUSH_PRINTF |
| 219 | //config: bool "printf builtin" |
| 220 | //config: default y |
| 221 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 222 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 223 | //config:config HUSH_TEST |
| 224 | //config: bool "test builtin" |
| 225 | //config: default y |
| 226 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 227 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 228 | //config:config HUSH_HELP |
| 229 | //config: bool "help builtin" |
| 230 | //config: default y |
| 231 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 232 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 233 | //config:config HUSH_EXPORT |
| 234 | //config: bool "export builtin" |
| 235 | //config: default y |
| 236 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 237 | //config: |
| 238 | //config:config HUSH_EXPORT_N |
| 239 | //config: bool "Support 'export -n' option" |
| 240 | //config: default y |
| 241 | //config: depends on HUSH_EXPORT |
| 242 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 243 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 244 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 245 | //config:config HUSH_READONLY |
| 246 | //config: bool "readonly builtin" |
| 247 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 248 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 249 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 250 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 251 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 252 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 253 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 254 | //config: default y |
| 255 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 256 | //config: |
| 257 | //config:config HUSH_WAIT |
| 258 | //config: bool "wait builtin" |
| 259 | //config: default y |
| 260 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 261 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 262 | //config:config HUSH_COMMAND |
| 263 | //config: bool "command builtin" |
| 264 | //config: default y |
| 265 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 266 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 267 | //config:config HUSH_TRAP |
| 268 | //config: bool "trap builtin" |
| 269 | //config: default y |
| 270 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 271 | //config: |
| 272 | //config:config HUSH_TYPE |
| 273 | //config: bool "type builtin" |
| 274 | //config: default y |
| 275 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 276 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 277 | //config:config HUSH_TIMES |
| 278 | //config: bool "times builtin" |
| 279 | //config: default y |
| 280 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 281 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 282 | //config:config HUSH_READ |
| 283 | //config: bool "read builtin" |
| 284 | //config: default y |
| 285 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 286 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 287 | //config:config HUSH_SET |
| 288 | //config: bool "set builtin" |
| 289 | //config: default y |
| 290 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 291 | //config: |
| 292 | //config:config HUSH_UNSET |
| 293 | //config: bool "unset builtin" |
| 294 | //config: default y |
| 295 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 296 | //config: |
| 297 | //config:config HUSH_ULIMIT |
| 298 | //config: bool "ulimit builtin" |
| 299 | //config: default y |
| 300 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 301 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 302 | //config:config HUSH_UMASK |
| 303 | //config: bool "umask builtin" |
| 304 | //config: default y |
| 305 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 306 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 307 | //config:config HUSH_GETOPTS |
| 308 | //config: bool "getopts builtin" |
| 309 | //config: default y |
| 310 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 311 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 312 | //config:config HUSH_MEMLEAK |
| 313 | //config: bool "memleak builtin (debugging)" |
| 314 | //config: default n |
| 315 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 317 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 318 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 319 | //applet:IF_SH_IS_HUSH( APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 320 | //applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 321 | |
| 322 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 323 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 324 | //kbuild:lib-$(CONFIG_BASH_IS_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 325 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 326 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 327 | /* -i (interactive) is also accepted, |
| 328 | * but does nothing, therefore not shown in help. |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 329 | * NOMMU-specific options are not meant to be used by users, |
| 330 | * therefore we don't show them either. |
| 331 | */ |
| 332 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 333 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 334 | //usage:#define hush_full_usage "\n\n" |
| 335 | //usage: "Unix shell interpreter" |
| 336 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 337 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 338 | || defined(__APPLE__) \ |
| 339 | ) |
| 340 | # include <malloc.h> /* for malloc_trim */ |
| 341 | #endif |
| 342 | #include <glob.h> |
| 343 | /* #include <dmalloc.h> */ |
| 344 | #if ENABLE_HUSH_CASE |
| 345 | # include <fnmatch.h> |
| 346 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 347 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 348 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 349 | |
| 350 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 351 | #include "unicode.h" |
| 352 | #include "shell_common.h" |
| 353 | #include "math.h" |
| 354 | #include "match.h" |
| 355 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 356 | # include "random.h" |
| 357 | #else |
| 358 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 359 | #endif |
| 360 | #ifndef F_DUPFD_CLOEXEC |
| 361 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 362 | #endif |
| 363 | #ifndef PIPE_BUF |
| 364 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 365 | #endif |
| 366 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 367 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 368 | /* So far, all bash compat is controlled by one config option */ |
| 369 | /* Separate defines document which part of code implements what */ |
| 370 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 371 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 372 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 373 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 374 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 375 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 376 | |
| 377 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 378 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 379 | #define LEAK_HUNTING 0 |
| 380 | #define BUILD_AS_NOMMU 0 |
| 381 | /* Enable/disable sanity checks. Ok to enable in production, |
| 382 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 383 | * Keeping 1 for now even in released versions. |
| 384 | */ |
| 385 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 386 | /* Slightly bigger (+200 bytes), but faster hush. |
| 387 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 388 | * which allows us to do fewer waitpid's. |
| 389 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 390 | * and therefore waitpid will return the same result as last time) |
| 391 | */ |
| 392 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 393 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 394 | * So far ${var%...} ops are always enabled: |
| 395 | */ |
| 396 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 397 | |
| 398 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 399 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 400 | # undef BB_MMU |
| 401 | # undef USE_FOR_NOMMU |
| 402 | # undef USE_FOR_MMU |
| 403 | # define BB_MMU 0 |
| 404 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 405 | # define USE_FOR_MMU(...) |
| 406 | #endif |
| 407 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 408 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 409 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 410 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 411 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 412 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 413 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 414 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 415 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 416 | # define IF_FEATURE_SH_STANDALONE(...) |
| 417 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 418 | #endif |
| 419 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 420 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 421 | # undef ENABLE_FEATURE_EDITING |
| 422 | # define ENABLE_FEATURE_EDITING 0 |
| 423 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 424 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 425 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 426 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 427 | #endif |
| 428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 429 | /* Do we support ANY keywords? */ |
| 430 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 431 | # define HAS_KEYWORDS 1 |
| 432 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 433 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 434 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 435 | # define HAS_KEYWORDS 0 |
| 436 | # define IF_HAS_KEYWORDS(...) |
| 437 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 438 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 439 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 440 | /* If you comment out one of these below, it will be #defined later |
| 441 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 442 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 443 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf_parse(...) do {} while (0) |
| 445 | #define debug_print_tree(a, b) do {} while (0) |
| 446 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 447 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 448 | #define debug_printf_jobs(...) do {} while (0) |
| 449 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 450 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 451 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 452 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 453 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 454 | #define debug_printf_subst(...) do {} while (0) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 455 | #define debug_printf_prompt(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 456 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 458 | #define ERR_PTR ((void*)(long)1) |
| 459 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 460 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 461 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 462 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 463 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 464 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 465 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 466 | /* Support / and // replace ops */ |
| 467 | /* Note that // is stored as \ in "encoded" string representation */ |
| 468 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 469 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 470 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 471 | #else |
| 472 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 473 | # define VAR_SUBST_OPS "%#:-=+?" |
| 474 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 475 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 476 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 477 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 478 | #define SPECIAL_VAR_SYMBOL_CHR '\3' |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 479 | #define SPECIAL_VAR_SYMBOL 3 |
| 480 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 481 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 482 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 483 | struct variable; |
| 484 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 485 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 486 | |
| 487 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 488 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 489 | */ |
| 490 | #if !BB_MMU |
| 491 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 492 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 493 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 494 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 495 | } nommu_save_t; |
| 496 | #endif |
| 497 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 498 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 499 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 500 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 501 | RES_IF , |
| 502 | RES_THEN , |
| 503 | RES_ELIF , |
| 504 | RES_ELSE , |
| 505 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 506 | #endif |
| 507 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 508 | RES_FOR , |
| 509 | RES_WHILE , |
| 510 | RES_UNTIL , |
| 511 | RES_DO , |
| 512 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 513 | #endif |
| 514 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 515 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 516 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 517 | #if ENABLE_HUSH_CASE |
| 518 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 519 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 520 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 521 | RES_MATCH , /* "word)" */ |
| 522 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 523 | RES_ESAC , |
| 524 | #endif |
| 525 | RES_XXXX , |
| 526 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 527 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 528 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 529 | typedef struct o_string { |
| 530 | char *data; |
| 531 | int length; /* position where data is appended */ |
| 532 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 533 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 534 | /* At least some part of the string was inside '' or "", |
| 535 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 536 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 537 | smallint has_empty_slot; |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 538 | smallint ended_in_ifs; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 539 | } o_string; |
| 540 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 541 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 542 | EXP_FLAG_GLOB = 0x2, |
| 543 | /* Protect newly added chars against globbing |
| 544 | * by prepending \ to *, ?, [, \ */ |
| 545 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 546 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 547 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 548 | #define NULL_O_STRING { NULL } |
| 549 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 550 | #ifndef debug_printf_parse |
| 551 | static const char *const assignment_flag[] = { |
| 552 | "MAYBE_ASSIGNMENT", |
| 553 | "DEFINITELY_ASSIGNMENT", |
| 554 | "NOT_ASSIGNMENT", |
| 555 | "WORD_IS_KEYWORD", |
| 556 | }; |
| 557 | #endif |
| 558 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 559 | typedef struct in_str { |
| 560 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 561 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 562 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 563 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 564 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 565 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 566 | /* The descrip member of this structure is only used to make |
| 567 | * debugging output pretty */ |
| 568 | static const struct { |
| 569 | int mode; |
| 570 | signed char default_fd; |
| 571 | char descrip[3]; |
| 572 | } redir_table[] = { |
| 573 | { O_RDONLY, 0, "<" }, |
| 574 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 575 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 576 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 577 | { O_RDONLY, 0, "<<" }, |
| 578 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 579 | /* { O_RDONLY, 77, "<<" }, */ |
| 580 | }; |
| 581 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 582 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 583 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 584 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 585 | int rd_fd; /* fd to redirect */ |
| 586 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 587 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 588 | smallint rd_type; /* (enum redir_type) */ |
| 589 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 590 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 591 | * bit 0: do we need to trim leading tabs? |
| 592 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 593 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 594 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 595 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 596 | REDIRECT_INPUT = 0, |
| 597 | REDIRECT_OVERWRITE = 1, |
| 598 | REDIRECT_APPEND = 2, |
| 599 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 600 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 601 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 602 | |
| 603 | REDIRFD_CLOSE = -3, |
| 604 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 605 | REDIRFD_TO_FILE = -1, |
| 606 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 607 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 608 | HEREDOC_SKIPTABS = 1, |
| 609 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 610 | } redir_type; |
| 611 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 612 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 613 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 614 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 615 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 616 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 617 | unsigned lineno; |
| 618 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 619 | smallint cmd_type; /* CMD_xxx */ |
| 620 | #define CMD_NORMAL 0 |
| 621 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 622 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 623 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 624 | * "export v=t*" |
| 625 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 626 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 627 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 628 | #if ENABLE_HUSH_FUNCTIONS |
| 629 | # define CMD_FUNCDEF 3 |
| 630 | #endif |
| 631 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 632 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 633 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 634 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 635 | #if !BB_MMU |
| 636 | char *group_as_string; |
| 637 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 638 | #if ENABLE_HUSH_FUNCTIONS |
| 639 | struct function *child_func; |
| 640 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 641 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 642 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 643 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 644 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 645 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 646 | * we put those fields back into cmd->xxx |
| 647 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 648 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 649 | * Without this trick, loop would execute a;b;b;b;... |
| 650 | * instead of correct sequence a;b;a;b;... |
| 651 | * When command is freed, it severs the link |
| 652 | * (sets ->child_func->parent_cmd to NULL). |
| 653 | */ |
| 654 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 655 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 656 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 657 | * and on execution these are substituted with their values. |
| 658 | * Substitution can make _several_ words out of one argv[n]! |
| 659 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 660 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 661 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 662 | struct redir_struct *redirects; /* I/O redirections */ |
| 663 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 664 | /* Is there anything in this command at all? */ |
| 665 | #define IS_NULL_CMD(cmd) \ |
| 666 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 667 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 668 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 669 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 670 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 671 | int alive_cmds; /* number of commands running (not exited) */ |
| 672 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 673 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 674 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 675 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 676 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 677 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 678 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 679 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 680 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 681 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 682 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 683 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 684 | PIPE_SEQ = 0, |
| 685 | PIPE_AND = 1, |
| 686 | PIPE_OR = 2, |
| 687 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 688 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 689 | /* Is there anything in this pipe at all? */ |
| 690 | #define IS_NULL_PIPE(pi) \ |
| 691 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 692 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 693 | /* This holds pointers to the various results of parsing */ |
| 694 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 695 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 696 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 697 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 698 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 699 | /* last command in pipe (being constructed right now) */ |
| 700 | struct command *command; |
| 701 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 702 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 703 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 704 | #if !BB_MMU |
| 705 | o_string as_string; |
| 706 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 707 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 708 | #if HAS_KEYWORDS |
| 709 | smallint ctx_res_w; |
| 710 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 711 | #if ENABLE_HUSH_CASE |
| 712 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 713 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 714 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 715 | int old_flag; |
| 716 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 717 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 718 | * when we see "if" or "then", we malloc and copy current context, |
| 719 | * and make ->stack point to it. then we parse pipeN. |
| 720 | * when closing "then" / fi" / whatever is found, |
| 721 | * we move list_head into ->stack->command->group, |
| 722 | * copy ->stack into current context, and delete ->stack. |
| 723 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 724 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 725 | struct parse_context *stack; |
| 726 | #endif |
| 727 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 728 | enum { |
| 729 | MAYBE_ASSIGNMENT = 0, |
| 730 | DEFINITELY_ASSIGNMENT = 1, |
| 731 | NOT_ASSIGNMENT = 2, |
| 732 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 733 | WORD_IS_KEYWORD = 3, |
| 734 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 735 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 736 | /* On program start, environ points to initial environment. |
| 737 | * putenv adds new pointers into it, unsetenv removes them. |
| 738 | * Neither of these (de)allocates the strings. |
| 739 | * setenv allocates new strings in malloc space and does putenv, |
| 740 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 741 | #define setenv(...) setenv_is_leaky_dont_use() |
| 742 | struct variable { |
| 743 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 744 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 745 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 746 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 747 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 748 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 749 | }; |
| 750 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 751 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 752 | BC_BREAK = 1, |
| 753 | BC_CONTINUE = 2, |
| 754 | }; |
| 755 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 756 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 757 | struct function { |
| 758 | struct function *next; |
| 759 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 760 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 761 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 762 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 763 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 764 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 765 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 766 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 767 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 768 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 769 | /* set -/+o OPT support. (TODO: make it optional) |
| 770 | * bash supports the following opts: |
| 771 | * allexport off |
| 772 | * braceexpand on |
| 773 | * emacs on |
| 774 | * errexit off |
| 775 | * errtrace off |
| 776 | * functrace off |
| 777 | * hashall on |
| 778 | * histexpand off |
| 779 | * history on |
| 780 | * ignoreeof off |
| 781 | * interactive-comments on |
| 782 | * keyword off |
| 783 | * monitor on |
| 784 | * noclobber off |
| 785 | * noexec off |
| 786 | * noglob off |
| 787 | * nolog off |
| 788 | * notify off |
| 789 | * nounset off |
| 790 | * onecmd off |
| 791 | * physical off |
| 792 | * pipefail off |
| 793 | * posix off |
| 794 | * privileged off |
| 795 | * verbose off |
| 796 | * vi off |
| 797 | * xtrace off |
| 798 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 799 | static const char o_opt_strings[] ALIGN1 = |
| 800 | "pipefail\0" |
| 801 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 802 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 803 | #if ENABLE_HUSH_MODE_X |
| 804 | "xtrace\0" |
| 805 | #endif |
| 806 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 807 | enum { |
| 808 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 809 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 810 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 811 | #if ENABLE_HUSH_MODE_X |
| 812 | OPT_O_XTRACE, |
| 813 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 814 | NUM_OPT_O |
| 815 | }; |
| 816 | |
| 817 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 818 | struct FILE_list { |
| 819 | struct FILE_list *next; |
| 820 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 821 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 822 | }; |
| 823 | |
| 824 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 825 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 826 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 827 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 828 | /* interactive_fd != 0 means we are an interactive shell. |
| 829 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 830 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 831 | * job control still works, but terminal signals |
| 832 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 833 | * process groups can only be created with "cmd &". |
| 834 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 835 | * to give tty to the foreground process group, |
| 836 | * and will take it back when the group is stopped (^Z) |
| 837 | * or killed (^C). |
| 838 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 839 | #if ENABLE_HUSH_INTERACTIVE |
| 840 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 841 | * _AND_ if we decided to act interactively */ |
| 842 | int interactive_fd; |
| 843 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 844 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 845 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 846 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 847 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 848 | #endif |
| 849 | #if ENABLE_FEATURE_EDITING |
| 850 | line_input_t *line_input_state; |
| 851 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 852 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 853 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 854 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 855 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 856 | random_t random_gen; |
| 857 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 858 | #if ENABLE_HUSH_JOB |
| 859 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 860 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 861 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 862 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 863 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 864 | #else |
| 865 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 866 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 867 | /* How deeply are we in context where "set -e" is ignored */ |
| 868 | int errexit_depth; |
| 869 | /* "set -e" rules (do we follow them correctly?): |
| 870 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 871 | * Shell does not exit if failed command is part of condition in |
| 872 | * if/while, part of && or || list except the last command, any command |
| 873 | * in a pipe but the last, or if the command's return value is being |
| 874 | * inverted with !. If a compound command other than a subshell returns a |
| 875 | * non-zero status because a command failed while -e was being ignored, the |
| 876 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 877 | * exits [ERR is a bashism]. |
| 878 | * |
| 879 | * If a compound command or function executes in a context where -e is |
| 880 | * ignored, none of the commands executed within are affected by the -e |
| 881 | * setting. If a compound command or function sets -e while executing in a |
| 882 | * context where -e is ignored, that setting does not have any effect until |
| 883 | * the compound command or the command containing the function call completes. |
| 884 | */ |
| 885 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 886 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 887 | #if ENABLE_HUSH_MODE_X |
| 888 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 889 | #else |
| 890 | # define G_x_mode 0 |
| 891 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 892 | #if ENABLE_HUSH_INTERACTIVE |
| 893 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 894 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 895 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 896 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 897 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 898 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 899 | #if ENABLE_HUSH_FUNCTIONS |
| 900 | /* 0: outside of a function (or sourced file) |
| 901 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 902 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 903 | */ |
| 904 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 905 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 906 | #else |
| 907 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 908 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 909 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 910 | /* These support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 911 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 912 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 913 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 914 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 915 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 916 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 917 | # define G_global_args_malloced (G.global_args_malloced) |
| 918 | #else |
| 919 | # define G_global_args_malloced 0 |
| 920 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 921 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 922 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 923 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 924 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 925 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 926 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 927 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 928 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 929 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 930 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 931 | #if ENABLE_HUSH_GETOPTS |
| 932 | unsigned getopt_count; |
| 933 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 934 | const char *ifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 935 | char *ifs_whitespace; /* = G.ifs or malloced */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 936 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 937 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 938 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 939 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 940 | unsigned var_nest_level; |
| 941 | #if ENABLE_HUSH_FUNCTIONS |
| 942 | # if ENABLE_HUSH_LOCAL |
| 943 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 944 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 945 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 946 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 947 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 948 | #if ENABLE_HUSH_FAST |
| 949 | unsigned count_SIGCHLD; |
| 950 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 951 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 952 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 953 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 954 | unsigned lineno; |
| 955 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 956 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 957 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 958 | /* Which signals have non-DFL handler (even with no traps set)? |
| 959 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 960 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 961 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 962 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 963 | * Other than these two times, never modified. |
| 964 | */ |
| 965 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 966 | #if ENABLE_HUSH_JOB |
| 967 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 968 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 969 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 970 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 971 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 972 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 973 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 974 | # define G_traps G.traps |
| 975 | #else |
| 976 | # define G_traps ((char**)NULL) |
| 977 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 978 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 979 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 980 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 981 | #endif |
| 982 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 983 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 984 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 985 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 986 | #if ENABLE_FEATURE_EDITING |
| 987 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 988 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 989 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 990 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 991 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 992 | * (too many defines). Also, I actually prefer to see when a variable |
| 993 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 994 | #define INIT_G() do { \ |
| 995 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 996 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 997 | sigfillset(&G.sa.sa_mask); \ |
| 998 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 999 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1000 | |
| 1001 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1002 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1003 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1004 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1005 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1006 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1007 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1008 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1009 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1010 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1011 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1012 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1013 | #if ENABLE_HUSH_READONLY |
| 1014 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1015 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1016 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1017 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1018 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1019 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1020 | #if ENABLE_HUSH_GETOPTS |
| 1021 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1022 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1023 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1024 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1025 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1026 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1027 | static int builtin_history(char **argv) FAST_FUNC; |
| 1028 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1029 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1030 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1031 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1032 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1033 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1034 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1035 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1036 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1037 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1038 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1039 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1040 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1041 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1042 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1043 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1044 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1045 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1046 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1047 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1048 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1049 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1050 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1051 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1052 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1053 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1054 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1055 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1056 | #if ENABLE_HUSH_TIMES |
| 1057 | static int builtin_times(char **argv) FAST_FUNC; |
| 1058 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1059 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1060 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1061 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1062 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1063 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1064 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1065 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1066 | #if ENABLE_HUSH_KILL |
| 1067 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1068 | #endif |
| 1069 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1070 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1071 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1072 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1073 | static int builtin_break(char **argv) FAST_FUNC; |
| 1074 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1075 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1076 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1077 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1078 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* Table of built-in functions. They can be forked or not, depending on |
| 1081 | * context: within pipes, they fork. As simple commands, they do not. |
| 1082 | * When used in non-forking context, they can change global variables |
| 1083 | * in the parent shell process. If forked, of course they cannot. |
| 1084 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1085 | * still be set at the end. */ |
| 1086 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1087 | const char *b_cmd; |
| 1088 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1089 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1090 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1091 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1092 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1093 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1094 | #endif |
| 1095 | }; |
| 1096 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1097 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1098 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1099 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1100 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1101 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1102 | #endif |
| 1103 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1104 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1105 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1106 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1107 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1108 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1109 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1110 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1111 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1112 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1113 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1114 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1115 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1116 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1117 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1118 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1119 | #if ENABLE_HUSH_GETOPTS |
| 1120 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1121 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1122 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1123 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1124 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1125 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1126 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1127 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1128 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1129 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1130 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1131 | #if ENABLE_HUSH_KILL |
| 1132 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1133 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1134 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1135 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1136 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1137 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1138 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1139 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1140 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1141 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1142 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1143 | #if ENABLE_HUSH_READONLY |
| 1144 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1145 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1146 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1147 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1148 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1149 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1150 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1151 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1152 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1153 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1154 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1155 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1156 | #if ENABLE_HUSH_TIMES |
| 1157 | BLTIN("times" , builtin_times , NULL), |
| 1158 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1159 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1160 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1161 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1162 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1163 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1164 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1165 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1166 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1167 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1168 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1169 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1170 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1171 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1172 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1173 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1174 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1175 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1176 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1177 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1178 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1179 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1180 | * (it's cheaper to run an external program in this case): |
| 1181 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1182 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1183 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1184 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1185 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1186 | #if BASH_TEST2 |
| 1187 | BLTIN("[[" , builtin_test , NULL), |
| 1188 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1189 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1190 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1191 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1192 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1193 | BLTIN("printf" , builtin_printf , NULL), |
| 1194 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1195 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1196 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1197 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1198 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1201 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1202 | /* Debug printouts. |
| 1203 | */ |
| 1204 | #if HUSH_DEBUG |
| 1205 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1206 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1207 | # define debug_enter() (G.debug_indent++) |
| 1208 | # define debug_leave() (G.debug_indent--) |
| 1209 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1210 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1211 | # define debug_enter() ((void)0) |
| 1212 | # define debug_leave() ((void)0) |
| 1213 | #endif |
| 1214 | |
| 1215 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1216 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1217 | #endif |
| 1218 | |
| 1219 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1220 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1221 | #endif |
| 1222 | |
| 1223 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1224 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | |
| 1227 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1228 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1229 | #endif |
| 1230 | |
| 1231 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1232 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1233 | # define DEBUG_JOBS 1 |
| 1234 | #else |
| 1235 | # define DEBUG_JOBS 0 |
| 1236 | #endif |
| 1237 | |
| 1238 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1239 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1240 | # define DEBUG_EXPAND 1 |
| 1241 | #else |
| 1242 | # define DEBUG_EXPAND 0 |
| 1243 | #endif |
| 1244 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1245 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1246 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1247 | #endif |
| 1248 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1249 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1250 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1251 | # define DEBUG_GLOB 1 |
| 1252 | #else |
| 1253 | # define DEBUG_GLOB 0 |
| 1254 | #endif |
| 1255 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1256 | #ifndef debug_printf_redir |
| 1257 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1258 | #endif |
| 1259 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1260 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1261 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | |
| 1264 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1265 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1266 | #endif |
| 1267 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1268 | #ifndef debug_printf_prompt |
| 1269 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1270 | #endif |
| 1271 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1272 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1273 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1274 | # define DEBUG_CLEAN 1 |
| 1275 | #else |
| 1276 | # define DEBUG_CLEAN 0 |
| 1277 | #endif |
| 1278 | |
| 1279 | #if DEBUG_EXPAND |
| 1280 | static void debug_print_strings(const char *prefix, char **vv) |
| 1281 | { |
| 1282 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1283 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1284 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1285 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1286 | } |
| 1287 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1288 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1289 | #endif |
| 1290 | |
| 1291 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1292 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1293 | */ |
| 1294 | #if LEAK_HUNTING |
| 1295 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1296 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1297 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1298 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1299 | return ptr; |
| 1300 | } |
| 1301 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1302 | { |
| 1303 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1304 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1305 | return ptr; |
| 1306 | } |
| 1307 | static char *xxstrdup(int lineno, const char *str) |
| 1308 | { |
| 1309 | char *ptr = xstrdup(str); |
| 1310 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1311 | return ptr; |
| 1312 | } |
| 1313 | static void xxfree(void *ptr) |
| 1314 | { |
| 1315 | fdprintf(2, "free %p\n", ptr); |
| 1316 | free(ptr); |
| 1317 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1318 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1319 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1320 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1321 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | |
| 1324 | |
| 1325 | /* Syntax and runtime errors. They always abort scripts. |
| 1326 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1327 | * and return to the prompt. |
| 1328 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1329 | */ |
| 1330 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1331 | # 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] | 1332 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1333 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1334 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1335 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1336 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1337 | #endif |
| 1338 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1339 | static void die_if_script(void) |
| 1340 | { |
| 1341 | if (!G_interactive_fd) { |
| 1342 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1343 | xfunc_error_retval = G.last_exitcode; |
| 1344 | xfunc_die(); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1349 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1350 | va_list p; |
| 1351 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1352 | #if HUSH_DEBUG >= 2 |
| 1353 | bb_error_msg("hush.c:%u", lineno); |
| 1354 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1355 | va_start(p, fmt); |
| 1356 | bb_verror_msg(fmt, p, NULL); |
| 1357 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1358 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1359 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1360 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1361 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1362 | { |
| 1363 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1364 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1365 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1366 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1367 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1370 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1371 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1372 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1373 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1376 | 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] | 1377 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1378 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1379 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1380 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1381 | } |
| 1382 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1383 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1384 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1385 | char msg[2] = { ch, '\0' }; |
| 1386 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1389 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1390 | { |
| 1391 | char msg[2]; |
| 1392 | msg[0] = ch; |
| 1393 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1394 | #if HUSH_DEBUG >= 2 |
| 1395 | bb_error_msg("hush.c:%u", lineno); |
| 1396 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1397 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1398 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1401 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1402 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1403 | # undef syntax_error |
| 1404 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1405 | # undef syntax_error_unterm_ch |
| 1406 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1407 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1408 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1409 | # 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] | 1410 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1411 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1412 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1413 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1414 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1415 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1416 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1417 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1418 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1419 | static void cmdedit_update_prompt(void); |
| 1420 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1421 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1422 | #endif |
| 1423 | |
| 1424 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1425 | /* Utility functions |
| 1426 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1427 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1428 | static char *unbackslash(char *src) |
| 1429 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1430 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1431 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1432 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1433 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame] | 1434 | if (*src != '\0') { |
| 1435 | /* \x -> x */ |
| 1436 | *dst++ = *src++; |
| 1437 | continue; |
| 1438 | } |
| 1439 | /* else: "\<nul>". Do not delete this backslash. |
| 1440 | * Testcase: eval 'echo ok\' |
| 1441 | */ |
| 1442 | *dst++ = '\\'; |
| 1443 | /* fallthrough */ |
| 1444 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1445 | if ((*dst++ = *src++) == '\0') |
| 1446 | break; |
| 1447 | } |
| 1448 | return dst; |
| 1449 | } |
| 1450 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1451 | 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] | 1452 | { |
| 1453 | int i; |
| 1454 | unsigned count1; |
| 1455 | unsigned count2; |
| 1456 | char **v; |
| 1457 | |
| 1458 | v = strings; |
| 1459 | count1 = 0; |
| 1460 | if (v) { |
| 1461 | while (*v) { |
| 1462 | count1++; |
| 1463 | v++; |
| 1464 | } |
| 1465 | } |
| 1466 | count2 = 0; |
| 1467 | v = add; |
| 1468 | while (*v) { |
| 1469 | count2++; |
| 1470 | v++; |
| 1471 | } |
| 1472 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1473 | v[count1 + count2] = NULL; |
| 1474 | i = count2; |
| 1475 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1476 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1477 | return v; |
| 1478 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1479 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1480 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1481 | { |
| 1482 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1483 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1484 | return ptr; |
| 1485 | } |
| 1486 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1487 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1488 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1489 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1490 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1491 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1492 | { |
| 1493 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1494 | v[0] = add; |
| 1495 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1496 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1497 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1498 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1499 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1500 | { |
| 1501 | char **ptr = add_string_to_strings(strings, add); |
| 1502 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1503 | return ptr; |
| 1504 | } |
| 1505 | #define add_string_to_strings(strings, add) \ |
| 1506 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1507 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1508 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1509 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1510 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1511 | char **v; |
| 1512 | |
| 1513 | if (!strings) |
| 1514 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1515 | v = strings; |
| 1516 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1517 | free(*v); |
| 1518 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1519 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1520 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1523 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1524 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1525 | int newfd; |
| 1526 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1527 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1528 | if (newfd >= 0) { |
| 1529 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1530 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1531 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1532 | if (errno == EBUSY) |
| 1533 | goto repeat; |
| 1534 | if (errno == EINTR) |
| 1535 | goto repeat; |
| 1536 | } |
| 1537 | return newfd; |
| 1538 | } |
| 1539 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1540 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1541 | { |
| 1542 | int newfd; |
| 1543 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1544 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1545 | if (newfd < 0) { |
| 1546 | if (errno == EBUSY) |
| 1547 | goto repeat; |
| 1548 | if (errno == EINTR) |
| 1549 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1550 | /* fd was not open? */ |
| 1551 | if (errno == EBADF) |
| 1552 | return fd; |
| 1553 | xfunc_die(); |
| 1554 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1555 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1556 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1557 | close(fd); |
| 1558 | return newfd; |
| 1559 | } |
| 1560 | |
| 1561 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1562 | /* Manipulating the list of open FILEs */ |
| 1563 | static FILE *remember_FILE(FILE *fp) |
| 1564 | { |
| 1565 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1566 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1567 | n->next = G.FILE_list; |
| 1568 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1569 | n->fp = fp; |
| 1570 | n->fd = fileno(fp); |
| 1571 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1572 | } |
| 1573 | return fp; |
| 1574 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1575 | static void fclose_and_forget(FILE *fp) |
| 1576 | { |
| 1577 | struct FILE_list **pp = &G.FILE_list; |
| 1578 | while (*pp) { |
| 1579 | struct FILE_list *cur = *pp; |
| 1580 | if (cur->fp == fp) { |
| 1581 | *pp = cur->next; |
| 1582 | free(cur); |
| 1583 | break; |
| 1584 | } |
| 1585 | pp = &cur->next; |
| 1586 | } |
| 1587 | fclose(fp); |
| 1588 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1589 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1590 | { |
| 1591 | struct FILE_list *fl = G.FILE_list; |
| 1592 | while (fl) { |
| 1593 | if (fd == fl->fd) { |
| 1594 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1595 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1596 | debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1597 | return 1; |
| 1598 | } |
| 1599 | fl = fl->next; |
| 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | static void restore_redirected_FILEs(void) |
| 1604 | { |
| 1605 | struct FILE_list *fl = G.FILE_list; |
| 1606 | while (fl) { |
| 1607 | int should_be = fileno(fl->fp); |
| 1608 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1609 | debug_printf_redir("restoring script fd from %d to %d\n", fl->fd, should_be); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1610 | xmove_fd(fl->fd, should_be); |
| 1611 | fl->fd = should_be; |
| 1612 | } |
| 1613 | fl = fl->next; |
| 1614 | } |
| 1615 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1616 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1617 | static void close_all_FILE_list(void) |
| 1618 | { |
| 1619 | struct FILE_list *fl = G.FILE_list; |
| 1620 | while (fl) { |
| 1621 | /* fclose would also free FILE object. |
| 1622 | * It is disastrous if we share memory with a vforked parent. |
| 1623 | * I'm not sure we never come here after vfork. |
| 1624 | * Therefore just close fd, nothing more. |
| 1625 | */ |
| 1626 | /*fclose(fl->fp); - unsafe */ |
| 1627 | close(fl->fd); |
| 1628 | fl = fl->next; |
| 1629 | } |
| 1630 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1631 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1632 | static int fd_in_FILEs(int fd) |
| 1633 | { |
| 1634 | struct FILE_list *fl = G.FILE_list; |
| 1635 | while (fl) { |
| 1636 | if (fl->fd == fd) |
| 1637 | return 1; |
| 1638 | fl = fl->next; |
| 1639 | } |
| 1640 | return 0; |
| 1641 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1642 | |
| 1643 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1644 | /* Helpers for setting new $n and restoring them back |
| 1645 | */ |
| 1646 | typedef struct save_arg_t { |
| 1647 | char *sv_argv0; |
| 1648 | char **sv_g_argv; |
| 1649 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1650 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1651 | } save_arg_t; |
| 1652 | |
| 1653 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1654 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1655 | sv->sv_argv0 = argv[0]; |
| 1656 | sv->sv_g_argv = G.global_argv; |
| 1657 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1658 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1659 | |
| 1660 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1661 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1662 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1663 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1664 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
| 1667 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1668 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1669 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1670 | if (G.global_args_malloced) { |
| 1671 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1672 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1673 | while (*++pp) /* note: does not free $0 */ |
| 1674 | free(*pp); |
| 1675 | free(G.global_argv); |
| 1676 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1677 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1678 | argv[0] = sv->sv_argv0; |
| 1679 | G.global_argv = sv->sv_g_argv; |
| 1680 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1681 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1685 | /* Basic theory of signal handling in shell |
| 1686 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1687 | * This does not describe what hush does, rather, it is current understanding |
| 1688 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1689 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1690 | * |
| 1691 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1692 | * is finished or backgrounded. It is the same in interactive and |
| 1693 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1694 | * 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] | 1695 | * ^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] | 1696 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1697 | * pipe. |
| 1698 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1699 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1700 | * or by SIGINT in interactive shell. |
| 1701 | * |
| 1702 | * Trap handlers will execute even within trap handlers. (right?) |
| 1703 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1704 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1705 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1706 | * |
| 1707 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1708 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1709 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1710 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1711 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1712 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1713 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1714 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1715 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1716 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1717 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1718 | * |
| 1719 | * SIGQUIT: ignore |
| 1720 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1721 | * SIGHUP (interactive): |
| 1722 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1723 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1724 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1725 | * that all pipe members are stopped. Try this in bash: |
| 1726 | * while :; do :; done - ^Z does not background it |
| 1727 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1728 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1729 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1730 | * to interactive shell while shell is waiting for a pipe, |
| 1731 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1732 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1733 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1734 | * Example 2: this does not wait and does not execute ls: |
| 1735 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1736 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1737 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1738 | * Example 4: this does not wait and does not execute ls: |
| 1739 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1740 | * |
| 1741 | * (What happens to signals which are IGN on shell start?) |
| 1742 | * (What happens with signal mask on shell start?) |
| 1743 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1744 | * Old implementation |
| 1745 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1746 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1747 | * We block all signals which we don't want to take action immediately, |
| 1748 | * i.e. we block all signals which need to have special handling as described |
| 1749 | * above, and all signals which have traps set. |
| 1750 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1751 | * and act on them. |
| 1752 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1753 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1754 | * sigset_t blocked_set: current blocked signal set |
| 1755 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1756 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1757 | * 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] | 1758 | * "trap 'cmd' SIGxxx": |
| 1759 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1760 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1761 | * unblock signals with special interactive handling |
| 1762 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1763 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1764 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1765 | * 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] | 1766 | * 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] | 1767 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1768 | * 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] | 1769 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1770 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1771 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1772 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1773 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1774 | * 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] | 1775 | * 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] | 1776 | * |
| 1777 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1778 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1779 | * masked signals are not visible! |
| 1780 | * |
| 1781 | * New implementation |
| 1782 | * ================== |
| 1783 | * We record each signal we are interested in by installing signal handler |
| 1784 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1785 | * We are interested in: signals which need to have special handling |
| 1786 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1787 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1788 | * After each pipe execution, we extract any pending signals |
| 1789 | * and act on them. |
| 1790 | * |
| 1791 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1792 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1793 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1794 | * sigset_t pending_set: set of sigs we received. |
| 1795 | * |
| 1796 | * "trap - SIGxxx": |
| 1797 | * if sig is in special_sig_mask, set handler back to: |
| 1798 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1799 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1800 | * else: set handler back to SIG_DFL |
| 1801 | * "trap 'cmd' SIGxxx": |
| 1802 | * set handler to record_pending_signo. |
| 1803 | * "trap '' SIGxxx": |
| 1804 | * set handler to SIG_IGN. |
| 1805 | * after [v]fork, if we plan to be a shell: |
| 1806 | * set signals with special interactive handling to SIG_DFL |
| 1807 | * (because child shell is not interactive), |
| 1808 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1809 | * after [v]fork, if we plan to exec: |
| 1810 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1811 | * |
| 1812 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1813 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1814 | * |
| 1815 | * Note (compat): |
| 1816 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1817 | * are set to the default actions". bash interprets it so that traps which |
| 1818 | * 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] | 1819 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1820 | enum { |
| 1821 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1822 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1823 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1824 | | (1 << SIGHUP) |
| 1825 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1826 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1827 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1828 | | (1 << SIGTTIN) |
| 1829 | | (1 << SIGTTOU) |
| 1830 | | (1 << SIGTSTP) |
| 1831 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1832 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1833 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1834 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1835 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1836 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1837 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1838 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1839 | if (sig == SIGCHLD) { |
| 1840 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1841 | //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] | 1842 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1843 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1844 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1845 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1846 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1847 | { |
| 1848 | struct sigaction old_sa; |
| 1849 | |
| 1850 | /* We could use signal() to install handlers... almost: |
| 1851 | * except that we need to mask ALL signals while handlers run. |
| 1852 | * I saw signal nesting in strace, race window isn't small. |
| 1853 | * SA_RESTART is also needed, but in Linux, signal() |
| 1854 | * sets SA_RESTART too. |
| 1855 | */ |
| 1856 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1857 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1858 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1859 | G.sa.sa_handler = handler; |
| 1860 | sigaction(sig, &G.sa, &old_sa); |
| 1861 | return old_sa.sa_handler; |
| 1862 | } |
| 1863 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1864 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1865 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1866 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1867 | static void restore_ttypgrp_and__exit(void) |
| 1868 | { |
| 1869 | /* xfunc has failed! die die die */ |
| 1870 | /* no EXIT traps, this is an escape hatch! */ |
| 1871 | G.exiting = 1; |
| 1872 | hush_exit(xfunc_error_retval); |
| 1873 | } |
| 1874 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1875 | #if ENABLE_HUSH_JOB |
| 1876 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1877 | /* Needed only on some libc: |
| 1878 | * It was observed that on exit(), fgetc'ed buffered data |
| 1879 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1880 | * With the net effect that even after fork(), not vfork(), |
| 1881 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1882 | * noexec_applet_here |
| 1883 | * echo END_OF_SCRIPT |
| 1884 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1885 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1886 | * 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] | 1887 | * and in `cmd` handling. |
| 1888 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1889 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1890 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1891 | static void fflush_and__exit(void) |
| 1892 | { |
| 1893 | fflush_all(); |
| 1894 | _exit(xfunc_error_retval); |
| 1895 | } |
| 1896 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1897 | /* 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] | 1898 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1899 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1900 | # 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] | 1901 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1902 | /* Restores tty foreground process group, and exits. |
| 1903 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1904 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1905 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1906 | * We also call it if xfunc is exiting. |
| 1907 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1908 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1909 | static void sigexit(int sig) |
| 1910 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1911 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1912 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1913 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1914 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1915 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1916 | */ |
| 1917 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1918 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1919 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1920 | |
| 1921 | /* Not a signal, just exit */ |
| 1922 | if (sig <= 0) |
| 1923 | _exit(- sig); |
| 1924 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1925 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1926 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1927 | #else |
| 1928 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1929 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1930 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1931 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1932 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1933 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1934 | static sighandler_t pick_sighandler(unsigned sig) |
| 1935 | { |
| 1936 | sighandler_t handler = SIG_DFL; |
| 1937 | if (sig < sizeof(unsigned)*8) { |
| 1938 | unsigned sigmask = (1 << sig); |
| 1939 | |
| 1940 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1941 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1942 | if (G_fatal_sig_mask & sigmask) |
| 1943 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1944 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1945 | #endif |
| 1946 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1947 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1948 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1949 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1950 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1951 | * in an endless loop when we try to do some |
| 1952 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1953 | */ |
| 1954 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1955 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1956 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1957 | } |
| 1958 | return handler; |
| 1959 | } |
| 1960 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1961 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1962 | static void hush_exit(int exitcode) |
| 1963 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1964 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1965 | save_history(G.line_input_state); |
| 1966 | #endif |
| 1967 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1968 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1969 | 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] | 1970 | char *argv[3]; |
| 1971 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1972 | 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] | 1973 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1974 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1975 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1976 | * "trap" will still show it, if executed |
| 1977 | * in the handler */ |
| 1978 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1979 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1980 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1981 | #if ENABLE_FEATURE_CLEAN_UP |
| 1982 | { |
| 1983 | struct variable *cur_var; |
| 1984 | if (G.cwd != bb_msg_unknown) |
| 1985 | free((char*)G.cwd); |
| 1986 | cur_var = G.top_var; |
| 1987 | while (cur_var) { |
| 1988 | struct variable *tmp = cur_var; |
| 1989 | if (!cur_var->max_len) |
| 1990 | free(cur_var->varstr); |
| 1991 | cur_var = cur_var->next; |
| 1992 | free(tmp); |
| 1993 | } |
| 1994 | } |
| 1995 | #endif |
| 1996 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1997 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1998 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1999 | sigexit(- (exitcode & 0xff)); |
| 2000 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 2001 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 2002 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2005 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2006 | //TODO: return a mask of ALL handled sigs? |
| 2007 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2008 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2009 | int last_sig = 0; |
| 2010 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2011 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2012 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2013 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2014 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2015 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2016 | sig = 0; |
| 2017 | do { |
| 2018 | sig++; |
| 2019 | if (sigismember(&G.pending_set, sig)) { |
| 2020 | sigdelset(&G.pending_set, sig); |
| 2021 | goto got_sig; |
| 2022 | } |
| 2023 | } while (sig < NSIG); |
| 2024 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2025 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2026 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2027 | 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] | 2028 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2029 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2030 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2031 | char *argv[3]; |
| 2032 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2033 | argv[1] = xstrdup(G_traps[sig]); |
| 2034 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2035 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2036 | save_rcode = G.last_exitcode; |
| 2037 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2038 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2039 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2040 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2041 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2042 | } /* else: "" trap, ignoring signal */ |
| 2043 | continue; |
| 2044 | } |
| 2045 | /* not a trap: special action */ |
| 2046 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2047 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2048 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2049 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2050 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2051 | break; |
| 2052 | #if ENABLE_HUSH_JOB |
| 2053 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2054 | //TODO: why are we doing this? ash and dash don't do this, |
| 2055 | //they have no handler for SIGHUP at all, |
| 2056 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2057 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2058 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2059 | /* bash is observed to signal whole process groups, |
| 2060 | * not individual processes */ |
| 2061 | for (job = G.job_list; job; job = job->next) { |
| 2062 | if (job->pgrp <= 0) |
| 2063 | continue; |
| 2064 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2065 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2066 | kill(- job->pgrp, SIGCONT); |
| 2067 | } |
| 2068 | sigexit(SIGHUP); |
| 2069 | } |
| 2070 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2071 | #if ENABLE_HUSH_FAST |
| 2072 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2073 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2074 | G.count_SIGCHLD++; |
| 2075 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2076 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2077 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2078 | * This simplifies wait builtin a bit. |
| 2079 | */ |
| 2080 | break; |
| 2081 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2082 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2083 | 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] | 2084 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2085 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2086 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2087 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2088 | * in interactive shell, because TERM is ignored. |
| 2089 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | } |
| 2092 | } |
| 2093 | return last_sig; |
| 2094 | } |
| 2095 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2096 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2097 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2098 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2099 | if (force || G.cwd == NULL) { |
| 2100 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2101 | * we must not try to free(bb_msg_unknown) */ |
| 2102 | if (G.cwd == bb_msg_unknown) |
| 2103 | G.cwd = NULL; |
| 2104 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2105 | if (!G.cwd) |
| 2106 | G.cwd = bb_msg_unknown; |
| 2107 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2108 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2111 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2112 | /* |
| 2113 | * Shell and environment variable support |
| 2114 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2115 | 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] | 2116 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2117 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2118 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2119 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2120 | pp = &G.top_var; |
| 2121 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2122 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2123 | return pp; |
| 2124 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2125 | } |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2129 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2130 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2131 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2132 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2133 | |
| 2134 | if (G.expanded_assignments) { |
| 2135 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2136 | while (*cpp) { |
| 2137 | char *cp = *cpp; |
| 2138 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2139 | return cp + len + 1; |
| 2140 | cpp++; |
| 2141 | } |
| 2142 | } |
| 2143 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2144 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2145 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2146 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2147 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2148 | if (strcmp(name, "PPID") == 0) |
| 2149 | return utoa(G.root_ppid); |
| 2150 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2151 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2152 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2153 | return utoa(next_random(&G.random_gen)); |
| 2154 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2155 | return NULL; |
| 2156 | } |
| 2157 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2158 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2159 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2160 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2161 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2162 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2163 | cmdedit_update_prompt(); |
| 2164 | return; |
| 2165 | } |
| 2166 | |
| 2167 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2168 | && name_len == 6 |
| 2169 | ) { |
| 2170 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2171 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2172 | G.lineno_var = NULL; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2173 | return; |
| 2174 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2175 | #endif |
| 2176 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2177 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2178 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2179 | return; |
| 2180 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2181 | #endif |
| 2182 | } |
| 2183 | } |
| 2184 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2185 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2186 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2187 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2188 | #define SETFLAG_EXPORT (1 << 0) |
| 2189 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2190 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2191 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2192 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2193 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2194 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2195 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2196 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2197 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2198 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2199 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2200 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2201 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2202 | if (HUSH_DEBUG && !eq_sign) |
| 2203 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2204 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2205 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2206 | cur_pp = &G.top_var; |
| 2207 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2208 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2209 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2210 | continue; |
| 2211 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2212 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2213 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2214 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2215 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2216 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2217 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2218 | //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] | 2219 | return -1; |
| 2220 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2221 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2222 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2223 | *eq_sign = '\0'; |
| 2224 | unsetenv(str); |
| 2225 | *eq_sign = '='; |
| 2226 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2227 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2228 | /* bash 3.2.33(1) and exported vars: |
| 2229 | * # export z=z |
| 2230 | * # f() { local z=a; env | grep ^z; } |
| 2231 | * # f |
| 2232 | * z=a |
| 2233 | * # env | grep ^z |
| 2234 | * z=z |
| 2235 | */ |
| 2236 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2237 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2238 | /* New variable is local ("local VAR=VAL" or |
| 2239 | * "VAR=VAL cmd") |
| 2240 | * and existing one is global, or local |
| 2241 | * on a lower level that new one. |
| 2242 | * Remove it from global variable list: |
| 2243 | */ |
| 2244 | *cur_pp = cur->next; |
| 2245 | if (G.shadowed_vars_pp) { |
| 2246 | /* Save in "shadowed" list */ |
| 2247 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2248 | cur->flg_export ? "exported " : "", |
| 2249 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2250 | ); |
| 2251 | cur->next = *G.shadowed_vars_pp; |
| 2252 | *G.shadowed_vars_pp = cur; |
| 2253 | } else { |
| 2254 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2255 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2256 | cur->flg_export ? "exported " : "", |
| 2257 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2258 | ); |
| 2259 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2260 | free_me = cur->varstr; /* then free it later */ |
| 2261 | free(cur); |
| 2262 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2265 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2266 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2267 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2268 | free_and_exp: |
| 2269 | free(str); |
| 2270 | goto exp; |
| 2271 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2272 | |
| 2273 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2274 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2275 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2276 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2277 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2278 | strcpy(cur->varstr, str); |
| 2279 | goto free_and_exp; |
| 2280 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2281 | /* Can't reuse */ |
| 2282 | cur->max_len = 0; |
| 2283 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2284 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2285 | /* max_len == 0 signifies "malloced" var, which we can |
| 2286 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2287 | * if cur->flg_export is 1, it is in the environment. |
| 2288 | * We should either unsetenv+free, or wait until putenv, |
| 2289 | * then putenv(new)+free(old). |
| 2290 | */ |
| 2291 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2292 | goto set_str_and_exp; |
| 2293 | } |
| 2294 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2295 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2296 | 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] | 2297 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2298 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2299 | cur->next = *cur_pp; |
| 2300 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2301 | |
| 2302 | set_str_and_exp: |
| 2303 | cur->varstr = str; |
| 2304 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2305 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2306 | if (flags & SETFLAG_MAKE_RO) { |
| 2307 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2308 | } |
| 2309 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2310 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2311 | cur->flg_export = 1; |
| 2312 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2313 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2314 | cur->flg_export = 0; |
| 2315 | /* unsetenv was already done */ |
| 2316 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2317 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2318 | debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2319 | i = putenv(cur->varstr); |
| 2320 | /* only now we can free old exported malloced string */ |
| 2321 | free(free_me); |
| 2322 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2323 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2324 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2325 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2326 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2327 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2328 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2329 | return 0; |
| 2330 | } |
| 2331 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2332 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2333 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2334 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2335 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2336 | } |
| 2337 | |
Denys Vlasenko | 35a017c | 2018-06-26 18:27:54 +0200 | [diff] [blame] | 2338 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2339 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2340 | { |
| 2341 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2342 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2343 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2344 | cur_pp = &G.top_var; |
| 2345 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2346 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2347 | && cur->varstr[name_len] == '=' |
| 2348 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2349 | if (cur->flg_read_only) { |
| 2350 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2351 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2352 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2353 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2354 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2355 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2356 | bb_unsetenv(cur->varstr); |
| 2357 | if (!cur->max_len) |
| 2358 | free(cur->varstr); |
| 2359 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2360 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2361 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2362 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2363 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2364 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2365 | |
| 2366 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2367 | handle_changed_special_names(name, name_len); |
| 2368 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2369 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2372 | static int unset_local_var(const char *name) |
| 2373 | { |
| 2374 | return unset_local_var_len(name, strlen(name)); |
| 2375 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2376 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2377 | |
Denys Vlasenko | b2b14cb | 2018-06-26 18:09:22 +0200 | [diff] [blame] | 2378 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ || ENABLE_HUSH_GETOPTS \ |
| 2379 | || (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2380 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2381 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2382 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2383 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2384 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2385 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2386 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2387 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2388 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2389 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2390 | */ |
| 2391 | static void add_vars(struct variable *var) |
| 2392 | { |
| 2393 | struct variable *next; |
| 2394 | |
| 2395 | while (var) { |
| 2396 | next = var->next; |
| 2397 | var->next = G.top_var; |
| 2398 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2399 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2400 | 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] | 2401 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2402 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2403 | 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] | 2404 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2405 | var = next; |
| 2406 | } |
| 2407 | } |
| 2408 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2409 | /* We put strings[i] into variable table and possibly putenv them. |
| 2410 | * If variable is read only, we can free the strings[i] |
| 2411 | * which attempts to overwrite it. |
| 2412 | * The strings[] vector itself is freed. |
| 2413 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2414 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2415 | { |
| 2416 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2417 | |
| 2418 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2419 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2420 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2421 | s = strings; |
| 2422 | while (*s) { |
| 2423 | struct variable *var_p; |
| 2424 | struct variable **var_pp; |
| 2425 | char *eq; |
| 2426 | |
| 2427 | eq = strchr(*s, '='); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2428 | if (HUSH_DEBUG && !eq) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2429 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2430 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
| 2431 | if (var_pp) { |
| 2432 | var_p = *var_pp; |
| 2433 | if (var_p->flg_read_only) { |
| 2434 | char **p; |
| 2435 | bb_error_msg("%s: readonly variable", *s); |
| 2436 | /* |
| 2437 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2438 | * If VAR is readonly, leaving it in the list |
| 2439 | * after asssignment error (msg above) |
| 2440 | * causes doubled error message later, on unset. |
| 2441 | */ |
| 2442 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2443 | free(*s); |
| 2444 | p = s; |
| 2445 | do { *p = p[1]; p++; } while (*p); |
| 2446 | goto next; |
| 2447 | } |
| 2448 | /* below, set_local_var() with nest level will |
| 2449 | * "shadow" (remove) this variable from |
| 2450 | * global linked list. |
| 2451 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2452 | } |
Denys Vlasenko | e36a589 | 2018-07-18 16:12:23 +0200 | [diff] [blame] | 2453 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
| 2454 | 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] | 2455 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2456 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2457 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2458 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | |
| 2462 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2463 | * Unicode helper |
| 2464 | */ |
| 2465 | static void reinit_unicode_for_hush(void) |
| 2466 | { |
| 2467 | /* Unicode support should be activated even if LANG is set |
| 2468 | * _during_ shell execution, not only if it was set when |
| 2469 | * shell was started. Therefore, re-check LANG every time: |
| 2470 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2471 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2472 | || ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 4c201c0 | 2018-07-17 15:04:17 +0200 | [diff] [blame] | 2473 | ) { |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2474 | const char *s = get_local_var_value("LC_ALL"); |
| 2475 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2476 | if (!s) s = get_local_var_value("LANG"); |
| 2477 | reinit_unicode(s); |
| 2478 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2481 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2482 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2483 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2484 | |
| 2485 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2486 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2487 | * echo $P\ |
| 2488 | * \ |
| 2489 | * AT\ |
| 2490 | * H\ |
| 2491 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2492 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2493 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2494 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2495 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2496 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2497 | G.PS1 = get_local_var_value("PS1"); |
| 2498 | if (G.PS1 == NULL) |
| 2499 | G.PS1 = ""; |
| 2500 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2501 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2502 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2503 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2504 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2505 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2506 | { |
| 2507 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2508 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2509 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2510 | |
| 2511 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2512 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2513 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2514 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2515 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2516 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2517 | /* bash uses $PWD value, even if it is set by user. |
| 2518 | * It uses current dir only if PWD is unset. |
| 2519 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2520 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2521 | } |
| 2522 | prompt_str = G.PS1; |
| 2523 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2524 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2525 | return prompt_str; |
| 2526 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2527 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2528 | { |
| 2529 | int r; |
| 2530 | const char *prompt_str; |
| 2531 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2532 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2533 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2534 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2535 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2536 | if (G.flag_SIGINT) { |
| 2537 | /* There was ^C'ed, make it look prettier: */ |
| 2538 | bb_putchar('\n'); |
| 2539 | G.flag_SIGINT = 0; |
| 2540 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2541 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2542 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2543 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2544 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2545 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2546 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2547 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2548 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2549 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2550 | if (r != 0 && !G.flag_SIGINT) |
| 2551 | break; |
| 2552 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2553 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2554 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2555 | G.last_exitcode = 128 + SIGINT; |
| 2556 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2557 | if (r < 0) { |
| 2558 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2559 | i->p = NULL; |
| 2560 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2561 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2562 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2563 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2564 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2565 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2566 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2567 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2568 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2569 | /* Why check_and_run_traps here? Try this interactively: |
| 2570 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2571 | * $ <[enter], repeatedly...> |
| 2572 | * Without check_and_run_traps, handler never runs. |
| 2573 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2574 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2575 | fputs(prompt_str, stdout); |
| 2576 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2577 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2578 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2579 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2580 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2581 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2582 | * it generates SIGINT as usual. |
| 2583 | */ |
| 2584 | check_and_run_traps(); |
| 2585 | if (G.flag_SIGINT) |
| 2586 | G.last_exitcode = 128 + SIGINT; |
| 2587 | if (r != '\0') |
| 2588 | break; |
| 2589 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2590 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2591 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2592 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2593 | /* This is the magic location that prints prompts |
| 2594 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2595 | static int fgetc_interactive(struct in_str *i) |
| 2596 | { |
| 2597 | int ch; |
| 2598 | /* If it's interactive stdin, get new line. */ |
| 2599 | if (G_interactive_fd && i->file == stdin) { |
| 2600 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2601 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2602 | G.promptmode = 1; /* PS2 */ |
| 2603 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2604 | } else { |
| 2605 | /* Not stdin: script file, sourced file, etc */ |
| 2606 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2607 | } |
| 2608 | return ch; |
| 2609 | } |
| 2610 | #else |
| 2611 | static inline int fgetc_interactive(struct in_str *i) |
| 2612 | { |
| 2613 | int ch; |
| 2614 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2615 | return ch; |
| 2616 | } |
| 2617 | #endif /* INTERACTIVE */ |
| 2618 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2619 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2620 | { |
| 2621 | int ch; |
| 2622 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2623 | if (!i->file) { |
| 2624 | /* string-based in_str */ |
| 2625 | ch = (unsigned char)*i->p; |
| 2626 | if (ch != '\0') { |
| 2627 | i->p++; |
| 2628 | i->last_char = ch; |
| 2629 | return ch; |
| 2630 | } |
| 2631 | return EOF; |
| 2632 | } |
| 2633 | |
| 2634 | /* FILE-based in_str */ |
| 2635 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2636 | #if ENABLE_FEATURE_EDITING |
| 2637 | /* This can be stdin, check line editing char[] buffer */ |
| 2638 | if (i->p && *i->p != '\0') { |
| 2639 | ch = (unsigned char)*i->p++; |
| 2640 | goto out; |
| 2641 | } |
| 2642 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2643 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2644 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2645 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2646 | int ch2 = i->peek_buf[1]; |
| 2647 | i->peek_buf[0] = ch2; |
| 2648 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2649 | goto out; |
| 2650 | i->peek_buf[1] = 0; |
| 2651 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2652 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2653 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2654 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2655 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2656 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2657 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2658 | #if ENABLE_HUSH_LINENO_VAR |
| 2659 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2660 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2661 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2662 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2663 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2664 | return ch; |
| 2665 | } |
| 2666 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2667 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2668 | { |
| 2669 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2670 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2671 | if (!i->file) { |
| 2672 | /* string-based in_str */ |
| 2673 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2674 | return (unsigned char)*i->p; |
| 2675 | } |
| 2676 | |
| 2677 | /* FILE-based in_str */ |
| 2678 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2679 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2680 | /* This can be stdin, check line editing char[] buffer */ |
| 2681 | if (i->p && *i->p != '\0') |
| 2682 | return (unsigned char)*i->p; |
| 2683 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2684 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2685 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2686 | if (ch != 0) |
| 2687 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2688 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2689 | /* Need to get a new char */ |
| 2690 | ch = fgetc_interactive(i); |
| 2691 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2692 | |
| 2693 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2694 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2695 | if (i->p) { |
| 2696 | i->p -= 1; |
| 2697 | return ch; |
| 2698 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2699 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2700 | i->peek_buf[0] = ch; |
| 2701 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2702 | return ch; |
| 2703 | } |
| 2704 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2705 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2706 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2707 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2708 | */ |
| 2709 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2710 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2711 | int ch; |
| 2712 | |
| 2713 | /* There are two cases when i->p[] buffer exists. |
| 2714 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2715 | * (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] | 2716 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2717 | * the peek2 result is in i->p[1]. |
| 2718 | */ |
| 2719 | if (i->p) |
| 2720 | return (unsigned char)i->p[1]; |
| 2721 | |
| 2722 | /* Now we know it is a file-based in_str. */ |
| 2723 | |
| 2724 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2725 | /* Is there 2nd char? */ |
| 2726 | ch = i->peek_buf[1]; |
| 2727 | if (ch == 0) { |
| 2728 | /* We did not read it yet, get it now */ |
| 2729 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2730 | i->peek_buf[1] = ch; |
| 2731 | } |
| 2732 | |
| 2733 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2734 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2735 | } |
| 2736 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2737 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2738 | { |
| 2739 | for (;;) { |
| 2740 | int ch, ch2; |
| 2741 | |
| 2742 | ch = i_getch(input); |
| 2743 | if (ch != '\\') |
| 2744 | return ch; |
| 2745 | ch2 = i_peek(input); |
| 2746 | if (ch2 != '\n') |
| 2747 | return ch; |
| 2748 | /* backslash+newline, skip it */ |
| 2749 | i_getch(input); |
| 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2754 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2755 | */ |
| 2756 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2757 | { |
| 2758 | for (;;) { |
| 2759 | int ch, ch2; |
| 2760 | |
| 2761 | ch = i_peek(input); |
| 2762 | if (ch != '\\') |
| 2763 | return ch; |
| 2764 | ch2 = i_peek2(input); |
| 2765 | if (ch2 != '\n') |
| 2766 | return ch; |
| 2767 | /* backslash+newline, skip it */ |
| 2768 | i_getch(input); |
| 2769 | i_getch(input); |
| 2770 | } |
| 2771 | } |
| 2772 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2773 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2774 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2775 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2776 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2777 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2781 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2782 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2783 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2784 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2788 | /* |
| 2789 | * o_string support |
| 2790 | */ |
| 2791 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2792 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2793 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2794 | { |
| 2795 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2796 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2797 | if (o->data) |
| 2798 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2801 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2802 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2803 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2804 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2807 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2808 | { |
| 2809 | free(o->data); |
| 2810 | } |
| 2811 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2812 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2813 | { |
| 2814 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2815 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2816 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2817 | } |
| 2818 | } |
| 2819 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2820 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2821 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2822 | 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] | 2823 | if (o->length < o->maxlen) { |
| 2824 | /* likely. avoid o_grow_by() call */ |
| 2825 | add: |
| 2826 | o->data[o->length] = ch; |
| 2827 | o->length++; |
| 2828 | o->data[o->length] = '\0'; |
| 2829 | return; |
| 2830 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2831 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2832 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2835 | #if 0 |
| 2836 | /* Valid only if we know o_string is not empty */ |
| 2837 | static void o_delchr(o_string *o) |
| 2838 | { |
| 2839 | o->length--; |
| 2840 | o->data[o->length] = '\0'; |
| 2841 | } |
| 2842 | #endif |
| 2843 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2844 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2845 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2846 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2847 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2848 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2851 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2852 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2853 | o_addblock(o, str, strlen(str)); |
| 2854 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2855 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2856 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2857 | static void nommu_addchr(o_string *o, int ch) |
| 2858 | { |
| 2859 | if (o) |
| 2860 | o_addchr(o, ch); |
| 2861 | } |
| 2862 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2863 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2864 | #endif |
| 2865 | |
| 2866 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2867 | { |
| 2868 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2869 | } |
| 2870 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2871 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2872 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2873 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2874 | * Apparently, on unquoted $v bash still does globbing |
| 2875 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2876 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2877 | * quoting mechanisms on $v expansion side: one protects |
| 2878 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2879 | * We have only second one. |
| 2880 | */ |
| 2881 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2882 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2883 | # define MAYBE_BRACES "{}" |
| 2884 | #else |
| 2885 | # define MAYBE_BRACES "" |
| 2886 | #endif |
| 2887 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2888 | /* My analysis of quoting semantics tells me that state information |
| 2889 | * is associated with a destination, not a source. |
| 2890 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2891 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2892 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2893 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2894 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2895 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2896 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2897 | o_grow_by(o, sz); |
| 2898 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2899 | o->data[o->length] = '\\'; |
| 2900 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2901 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2902 | o->data[o->length] = ch; |
| 2903 | o->length++; |
| 2904 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2907 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2908 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2909 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2910 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2911 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2912 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2913 | sz++; |
| 2914 | o->data[o->length] = '\\'; |
| 2915 | o->length++; |
| 2916 | } |
| 2917 | o_grow_by(o, sz); |
| 2918 | o->data[o->length] = ch; |
| 2919 | o->length++; |
| 2920 | o->data[o->length] = '\0'; |
| 2921 | } |
| 2922 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2923 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2924 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2925 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2926 | char ch; |
| 2927 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2928 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2929 | if (ordinary_cnt > len) /* paranoia */ |
| 2930 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2931 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2932 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2933 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2934 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2935 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2936 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2937 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2938 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2939 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2940 | sz++; |
| 2941 | o->data[o->length] = '\\'; |
| 2942 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2943 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2944 | o_grow_by(o, sz); |
| 2945 | o->data[o->length] = ch; |
| 2946 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2947 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2948 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2951 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2952 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2953 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2954 | o_addblock(o, str, len); |
| 2955 | return; |
| 2956 | } |
| 2957 | o_addqblock(o, str, len); |
| 2958 | } |
| 2959 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2960 | static void o_addQstr(o_string *o, const char *str) |
| 2961 | { |
| 2962 | o_addQblock(o, str, strlen(str)); |
| 2963 | } |
| 2964 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2965 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2966 | * 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] | 2967 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2968 | * list[i] contains an INDEX (int!) into this string data. |
| 2969 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2970 | * but list[i]'s need not be modified. |
| 2971 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2972 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2973 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2974 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2975 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2976 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2977 | { |
| 2978 | char **list = (char**)o->data; |
| 2979 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2980 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2981 | |
| 2982 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2983 | 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] | 2984 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2985 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2986 | o->has_quoted_part, |
| 2987 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2988 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2989 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2990 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2991 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2992 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2993 | i++; |
| 2994 | } |
| 2995 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2996 | 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] | 2997 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2998 | 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] | 2999 | } |
| 3000 | } |
| 3001 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3002 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3003 | #endif |
| 3004 | |
| 3005 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3006 | * in list[n] so that it points past last stored byte so far. |
| 3007 | * It returns n+1. */ |
| 3008 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3009 | { |
| 3010 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3011 | int string_start; |
| 3012 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3013 | |
| 3014 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3015 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3016 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3017 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3018 | 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] | 3019 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3020 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3021 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3022 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3023 | memmove(list + n + 0x10, list + n, string_len); |
| 3024 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3025 | } else { |
| 3026 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3027 | n, string_len, string_start); |
| 3028 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3029 | } else { |
| 3030 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3031 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3032 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3033 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3034 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3035 | o->has_empty_slot = 0; |
| 3036 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3037 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3038 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3039 | return n + 1; |
| 3040 | } |
| 3041 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3042 | /* "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] | 3043 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3044 | { |
| 3045 | char **list = (char**)o->data; |
| 3046 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3047 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3048 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3051 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3052 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3053 | * first, it processes even {a} (no commas), second, |
| 3054 | * 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] | 3055 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3056 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3057 | |
| 3058 | /* Helper */ |
| 3059 | static int glob_needed(const char *s) |
| 3060 | { |
| 3061 | while (*s) { |
| 3062 | if (*s == '\\') { |
| 3063 | if (!s[1]) |
| 3064 | return 0; |
| 3065 | s += 2; |
| 3066 | continue; |
| 3067 | } |
| 3068 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3069 | return 1; |
| 3070 | s++; |
| 3071 | } |
| 3072 | return 0; |
| 3073 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3074 | /* Return pointer to next closing brace or to comma */ |
| 3075 | static const char *next_brace_sub(const char *cp) |
| 3076 | { |
| 3077 | unsigned depth = 0; |
| 3078 | cp++; |
| 3079 | while (*cp != '\0') { |
| 3080 | if (*cp == '\\') { |
| 3081 | if (*++cp == '\0') |
| 3082 | break; |
| 3083 | cp++; |
| 3084 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3085 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3086 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3087 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3088 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3089 | depth++; |
| 3090 | } |
| 3091 | |
| 3092 | return *cp != '\0' ? cp : NULL; |
| 3093 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3094 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3095 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3096 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3097 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3098 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3099 | const char *next; |
| 3100 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3101 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3102 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3103 | |
| 3104 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3105 | |
| 3106 | begin = pattern; |
| 3107 | while (1) { |
| 3108 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3109 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3110 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3111 | /* Find the first sub-pattern and at the same time |
| 3112 | * find the rest after the closing brace */ |
| 3113 | next = next_brace_sub(begin); |
| 3114 | if (next == NULL) { |
| 3115 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3116 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3117 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3118 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3119 | /* "{abc}" with no commas - illegal |
| 3120 | * brace expr, disregard and skip it */ |
| 3121 | begin = next + 1; |
| 3122 | continue; |
| 3123 | } |
| 3124 | break; |
| 3125 | } |
| 3126 | if (*begin == '\\' && begin[1] != '\0') |
| 3127 | begin++; |
| 3128 | begin++; |
| 3129 | } |
| 3130 | debug_printf_glob("begin:%s\n", begin); |
| 3131 | debug_printf_glob("next:%s\n", next); |
| 3132 | |
| 3133 | /* Now find the end of the whole brace expression */ |
| 3134 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3135 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3136 | rest = next_brace_sub(rest); |
| 3137 | if (rest == NULL) { |
| 3138 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3139 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3140 | } |
| 3141 | debug_printf_glob("rest:%s\n", rest); |
| 3142 | } |
| 3143 | rest_len = strlen(++rest) + 1; |
| 3144 | |
| 3145 | /* We are sure the brace expression is well-formed */ |
| 3146 | |
| 3147 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3148 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3149 | |
| 3150 | /* We have a brace expression. BEGIN points to the opening {, |
| 3151 | * NEXT points past the terminator of the first element, and REST |
| 3152 | * points past the final }. We will accumulate result names from |
| 3153 | * recursive runs for each brace alternative in the buffer using |
| 3154 | * GLOB_APPEND. */ |
| 3155 | |
| 3156 | p = begin + 1; |
| 3157 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3158 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3159 | memcpy( |
| 3160 | mempcpy( |
| 3161 | mempcpy(new_pattern_buf, |
| 3162 | /* We know the prefix for all sub-patterns */ |
| 3163 | pattern, begin - pattern), |
| 3164 | p, next - p), |
| 3165 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3166 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3167 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3168 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3169 | */ |
| 3170 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3171 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3172 | /* We saw the last entry */ |
| 3173 | break; |
| 3174 | } |
| 3175 | p = next + 1; |
| 3176 | next = next_brace_sub(next); |
| 3177 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3178 | free(new_pattern_buf); |
| 3179 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3180 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3181 | simple_glob: |
| 3182 | { |
| 3183 | int gr; |
| 3184 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3185 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3186 | memset(&globdata, 0, sizeof(globdata)); |
| 3187 | gr = glob(pattern, 0, NULL, &globdata); |
| 3188 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3189 | if (gr != 0) { |
| 3190 | if (gr == GLOB_NOMATCH) { |
| 3191 | globfree(&globdata); |
| 3192 | /* NB: garbles parameter */ |
| 3193 | unbackslash(pattern); |
| 3194 | o_addstr_with_NUL(o, pattern); |
| 3195 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3196 | return o_save_ptr_helper(o, n); |
| 3197 | } |
| 3198 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3199 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3200 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3201 | * but we didn't specify it. Paranoia again. */ |
| 3202 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3203 | } |
| 3204 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3205 | char **argv = globdata.gl_pathv; |
| 3206 | while (1) { |
| 3207 | o_addstr_with_NUL(o, *argv); |
| 3208 | n = o_save_ptr_helper(o, n); |
| 3209 | argv++; |
| 3210 | if (!*argv) |
| 3211 | break; |
| 3212 | } |
| 3213 | } |
| 3214 | globfree(&globdata); |
| 3215 | } |
| 3216 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3217 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3218 | /* Performs globbing on last list[], |
| 3219 | * saving each result as a new list[]. |
| 3220 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3221 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3222 | { |
| 3223 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3224 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3225 | 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] | 3226 | if (!o->data) |
| 3227 | return o_save_ptr_helper(o, n); |
| 3228 | pattern = o->data + o_get_last_ptr(o, n); |
| 3229 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3230 | if (!glob_needed(pattern)) { |
| 3231 | /* unbackslash last string in o in place, fix length */ |
| 3232 | o->length = unbackslash(pattern) - o->data; |
| 3233 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3234 | return o_save_ptr_helper(o, n); |
| 3235 | } |
| 3236 | |
| 3237 | copy = xstrdup(pattern); |
| 3238 | /* "forget" pattern in o */ |
| 3239 | o->length = pattern - o->data; |
| 3240 | n = glob_brace(copy, o, n); |
| 3241 | free(copy); |
| 3242 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3243 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3244 | return n; |
| 3245 | } |
| 3246 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3247 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3248 | |
| 3249 | /* Helper */ |
| 3250 | static int glob_needed(const char *s) |
| 3251 | { |
| 3252 | while (*s) { |
| 3253 | if (*s == '\\') { |
| 3254 | if (!s[1]) |
| 3255 | return 0; |
| 3256 | s += 2; |
| 3257 | continue; |
| 3258 | } |
| 3259 | if (*s == '*' || *s == '[' || *s == '?') |
| 3260 | return 1; |
| 3261 | s++; |
| 3262 | } |
| 3263 | return 0; |
| 3264 | } |
| 3265 | /* Performs globbing on last list[], |
| 3266 | * saving each result as a new list[]. |
| 3267 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3268 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3269 | { |
| 3270 | glob_t globdata; |
| 3271 | int gr; |
| 3272 | char *pattern; |
| 3273 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3274 | 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] | 3275 | if (!o->data) |
| 3276 | return o_save_ptr_helper(o, n); |
| 3277 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3278 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3279 | if (!glob_needed(pattern)) { |
| 3280 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3281 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3282 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3283 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3284 | return o_save_ptr_helper(o, n); |
| 3285 | } |
| 3286 | |
| 3287 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3288 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3289 | * If we glob "*.\*" and don't find anything, we need |
| 3290 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3291 | * will return "*.\*"! |
| 3292 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3293 | gr = glob(pattern, 0, NULL, &globdata); |
| 3294 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3295 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3296 | if (gr == GLOB_NOMATCH) { |
| 3297 | globfree(&globdata); |
| 3298 | goto literal; |
| 3299 | } |
| 3300 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3301 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3302 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3303 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3304 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3305 | } |
| 3306 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3307 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3308 | /* "forget" pattern in o */ |
| 3309 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3310 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3311 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3312 | n = o_save_ptr_helper(o, n); |
| 3313 | argv++; |
| 3314 | if (!*argv) |
| 3315 | break; |
| 3316 | } |
| 3317 | } |
| 3318 | globfree(&globdata); |
| 3319 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3320 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3321 | return n; |
| 3322 | } |
| 3323 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3324 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3325 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3326 | /* 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] | 3327 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3328 | static int o_save_ptr(o_string *o, int n) |
| 3329 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3330 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3331 | /* If o->has_empty_slot, list[n] was already globbed |
| 3332 | * (if it was requested back then when it was filled) |
| 3333 | * so don't do that again! */ |
| 3334 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3335 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3336 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3337 | return o_save_ptr_helper(o, n); |
| 3338 | } |
| 3339 | |
| 3340 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3341 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3342 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3343 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3344 | int string_start; |
| 3345 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3346 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3347 | if (DEBUG_EXPAND) |
| 3348 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3349 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3350 | list = (char**)o->data; |
| 3351 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3352 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3353 | while (n) { |
| 3354 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3355 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3356 | } |
| 3357 | return list; |
| 3358 | } |
| 3359 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3360 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3361 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3362 | /* Returns pi->next - next pipe in the list */ |
| 3363 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3364 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3365 | struct pipe *next; |
| 3366 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3367 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3368 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3369 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3370 | struct command *command; |
| 3371 | struct redir_struct *r, *rnext; |
| 3372 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3373 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3374 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3375 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3376 | if (DEBUG_CLEAN) { |
| 3377 | int a; |
| 3378 | char **p; |
| 3379 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3380 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3381 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3382 | } |
| 3383 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3384 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3385 | } |
| 3386 | /* not "else if": on syntax error, we may have both! */ |
| 3387 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3388 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3389 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3390 | free_pipe_list(command->group); |
| 3391 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3392 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3393 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3394 | /* else is crucial here. |
| 3395 | * If group != NULL, child_func is meaningless */ |
| 3396 | #if ENABLE_HUSH_FUNCTIONS |
| 3397 | else if (command->child_func) { |
| 3398 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3399 | command->child_func->parent_cmd = NULL; |
| 3400 | } |
| 3401 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3402 | #if !BB_MMU |
| 3403 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3404 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3405 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3406 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3407 | debug_printf_clean(" redirect %d%s", |
| 3408 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3409 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3410 | if (r->rd_filename) { |
| 3411 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3412 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3413 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3414 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3415 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3416 | rnext = r->next; |
| 3417 | free(r); |
| 3418 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3419 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3420 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3421 | 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] | 3422 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3423 | #if ENABLE_HUSH_JOB |
| 3424 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3425 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3426 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3427 | |
| 3428 | next = pi->next; |
| 3429 | free(pi); |
| 3430 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3431 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3432 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3433 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3434 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3435 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3436 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3437 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3438 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3439 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3440 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3441 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3445 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3446 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3447 | #ifndef debug_print_tree |
| 3448 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3449 | { |
| 3450 | static const char *const PIPE[] = { |
| 3451 | [PIPE_SEQ] = "SEQ", |
| 3452 | [PIPE_AND] = "AND", |
| 3453 | [PIPE_OR ] = "OR" , |
| 3454 | [PIPE_BG ] = "BG" , |
| 3455 | }; |
| 3456 | static const char *RES[] = { |
| 3457 | [RES_NONE ] = "NONE" , |
| 3458 | # if ENABLE_HUSH_IF |
| 3459 | [RES_IF ] = "IF" , |
| 3460 | [RES_THEN ] = "THEN" , |
| 3461 | [RES_ELIF ] = "ELIF" , |
| 3462 | [RES_ELSE ] = "ELSE" , |
| 3463 | [RES_FI ] = "FI" , |
| 3464 | # endif |
| 3465 | # if ENABLE_HUSH_LOOPS |
| 3466 | [RES_FOR ] = "FOR" , |
| 3467 | [RES_WHILE] = "WHILE", |
| 3468 | [RES_UNTIL] = "UNTIL", |
| 3469 | [RES_DO ] = "DO" , |
| 3470 | [RES_DONE ] = "DONE" , |
| 3471 | # endif |
| 3472 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3473 | [RES_IN ] = "IN" , |
| 3474 | # endif |
| 3475 | # if ENABLE_HUSH_CASE |
| 3476 | [RES_CASE ] = "CASE" , |
| 3477 | [RES_CASE_IN ] = "CASE_IN" , |
| 3478 | [RES_MATCH] = "MATCH", |
| 3479 | [RES_CASE_BODY] = "CASE_BODY", |
| 3480 | [RES_ESAC ] = "ESAC" , |
| 3481 | # endif |
| 3482 | [RES_XXXX ] = "XXXX" , |
| 3483 | [RES_SNTX ] = "SNTX" , |
| 3484 | }; |
| 3485 | static const char *const CMDTYPE[] = { |
| 3486 | "{}", |
| 3487 | "()", |
| 3488 | "[noglob]", |
| 3489 | # if ENABLE_HUSH_FUNCTIONS |
| 3490 | "func()", |
| 3491 | # endif |
| 3492 | }; |
| 3493 | |
| 3494 | int pin, prn; |
| 3495 | |
| 3496 | pin = 0; |
| 3497 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3498 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3499 | lvl*2, "", |
| 3500 | pin, |
| 3501 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3502 | RES[pi->res_word], |
| 3503 | pi->followup, PIPE[pi->followup] |
| 3504 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3505 | prn = 0; |
| 3506 | while (prn < pi->num_cmds) { |
| 3507 | struct command *command = &pi->cmds[prn]; |
| 3508 | char **argv = command->argv; |
| 3509 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3510 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3511 | lvl*2, "", prn, |
| 3512 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3513 | #if ENABLE_HUSH_LINENO_VAR |
| 3514 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3515 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3516 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3517 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3518 | CMDTYPE[command->cmd_type], |
| 3519 | argv |
| 3520 | # if !BB_MMU |
| 3521 | , " group_as_string:", command->group_as_string |
| 3522 | # else |
| 3523 | , "", "" |
| 3524 | # endif |
| 3525 | ); |
| 3526 | debug_print_tree(command->group, lvl+1); |
| 3527 | prn++; |
| 3528 | continue; |
| 3529 | } |
| 3530 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3531 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3532 | argv++; |
| 3533 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3534 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3535 | prn++; |
| 3536 | } |
| 3537 | pi = pi->next; |
| 3538 | pin++; |
| 3539 | } |
| 3540 | } |
| 3541 | #endif /* debug_print_tree */ |
| 3542 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3543 | static struct pipe *new_pipe(void) |
| 3544 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3545 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3546 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3547 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3548 | return pi; |
| 3549 | } |
| 3550 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3551 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3552 | * if ctx->command is NULL. |
| 3553 | * No errors possible here. |
| 3554 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3555 | static int done_command(struct parse_context *ctx) |
| 3556 | { |
| 3557 | /* The command is really already in the pipe structure, so |
| 3558 | * advance the pipe counter and make a new, null command. */ |
| 3559 | struct pipe *pi = ctx->pipe; |
| 3560 | struct command *command = ctx->command; |
| 3561 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3562 | #if 0 /* Instead we emit error message at run time */ |
| 3563 | if (ctx->pending_redirect) { |
| 3564 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3565 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3566 | ctx->pending_redirect = NULL; |
| 3567 | } |
| 3568 | #endif |
| 3569 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3570 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3571 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3572 | 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] | 3573 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3574 | } |
| 3575 | pi->num_cmds++; |
| 3576 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3577 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3578 | } else { |
| 3579 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3580 | } |
| 3581 | |
| 3582 | /* Only real trickiness here is that the uncommitted |
| 3583 | * command structure is not counted in pi->num_cmds. */ |
| 3584 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3585 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3586 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3587 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3588 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3589 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3590 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3591 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3592 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3593 | } |
| 3594 | |
| 3595 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3596 | { |
| 3597 | int not_null; |
| 3598 | |
| 3599 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3600 | /* Close previous command */ |
| 3601 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3602 | #if HAS_KEYWORDS |
| 3603 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3604 | ctx->ctx_inverted = 0; |
| 3605 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3606 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3607 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3608 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3609 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3610 | * in a backgrounded subshell. |
| 3611 | */ |
| 3612 | struct pipe *pi; |
| 3613 | struct command *command; |
| 3614 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3615 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3616 | pi = ctx->list_head; |
| 3617 | while (pi != ctx->pipe) { |
| 3618 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3619 | goto no_conv; |
| 3620 | pi = pi->next; |
| 3621 | } |
| 3622 | |
| 3623 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3624 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3625 | pi = xzalloc(sizeof(*pi)); |
| 3626 | pi->followup = PIPE_BG; |
| 3627 | pi->num_cmds = 1; |
| 3628 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3629 | command = &pi->cmds[0]; |
| 3630 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3631 | command->cmd_type = CMD_NORMAL; |
| 3632 | command->group = ctx->list_head; |
| 3633 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3634 | command->group_as_string = xstrndup( |
| 3635 | ctx->as_string.data, |
| 3636 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3637 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3638 | #endif |
| 3639 | /* Replace all pipes in ctx with one newly created */ |
| 3640 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3641 | } else { |
| 3642 | no_conv: |
| 3643 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3644 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* Without this check, even just <enter> on command line generates |
| 3647 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3648 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3649 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3650 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3651 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3652 | #endif |
| 3653 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3654 | || ctx->ctx_res_w == RES_DONE |
| 3655 | || ctx->ctx_res_w == RES_FOR |
| 3656 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3657 | #endif |
| 3658 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3659 | || ctx->ctx_res_w == RES_ESAC |
| 3660 | #endif |
| 3661 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3662 | struct pipe *new_p; |
| 3663 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3664 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3665 | not_null, ctx->ctx_res_w); |
| 3666 | new_p = new_pipe(); |
| 3667 | ctx->pipe->next = new_p; |
| 3668 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3669 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3670 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3671 | * This is used to control execution. |
| 3672 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3673 | * cases where variable or value happens to match a keyword): |
| 3674 | */ |
| 3675 | #if ENABLE_HUSH_LOOPS |
| 3676 | if (ctx->ctx_res_w == RES_FOR |
| 3677 | || ctx->ctx_res_w == RES_IN) |
| 3678 | ctx->ctx_res_w = RES_NONE; |
| 3679 | #endif |
| 3680 | #if ENABLE_HUSH_CASE |
| 3681 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3682 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3683 | if (ctx->ctx_res_w == RES_CASE) |
| 3684 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3685 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3686 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3687 | /* Create the memory for command, roughly: |
| 3688 | * ctx->pipe->cmds = new struct command; |
| 3689 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3690 | */ |
| 3691 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3692 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3693 | } |
| 3694 | debug_printf_parse("done_pipe return\n"); |
| 3695 | } |
| 3696 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3697 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3698 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3699 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3700 | if (MAYBE_ASSIGNMENT != 0) |
| 3701 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3702 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3703 | /* Create the memory for command, roughly: |
| 3704 | * ctx->pipe->cmds = new struct command; |
| 3705 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3706 | */ |
| 3707 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3708 | } |
| 3709 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3710 | /* If a reserved word is found and processed, parse context is modified |
| 3711 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3712 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3713 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3714 | struct reserved_combo { |
| 3715 | char literal[6]; |
| 3716 | unsigned char res; |
| 3717 | unsigned char assignment_flag; |
| 3718 | int flag; |
| 3719 | }; |
| 3720 | enum { |
| 3721 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3722 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3723 | FLAG_IF = (1 << RES_IF ), |
| 3724 | FLAG_THEN = (1 << RES_THEN ), |
| 3725 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3726 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3727 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3728 | # endif |
| 3729 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3730 | FLAG_FOR = (1 << RES_FOR ), |
| 3731 | FLAG_WHILE = (1 << RES_WHILE), |
| 3732 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3733 | FLAG_DO = (1 << RES_DO ), |
| 3734 | FLAG_DONE = (1 << RES_DONE ), |
| 3735 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3736 | # endif |
| 3737 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3738 | FLAG_MATCH = (1 << RES_MATCH), |
| 3739 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3740 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3741 | FLAG_START = (1 << RES_XXXX ), |
| 3742 | }; |
| 3743 | |
| 3744 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3745 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3746 | /* Mostly a list of accepted follow-up reserved words. |
| 3747 | * FLAG_END means we are done with the sequence, and are ready |
| 3748 | * to turn the compound list into a command. |
| 3749 | * FLAG_START means the word must start a new compound list. |
| 3750 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3751 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3752 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3753 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3754 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3755 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3756 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3757 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3758 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3759 | # endif |
| 3760 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3761 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3762 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3763 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3764 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3765 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3766 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3767 | # endif |
| 3768 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3769 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3770 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3771 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3772 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3773 | const struct reserved_combo *r; |
| 3774 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3775 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3776 | if (strcmp(word->data, r->literal) == 0) |
| 3777 | return r; |
| 3778 | } |
| 3779 | return NULL; |
| 3780 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3781 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3782 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3783 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3784 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3785 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3786 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3787 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3788 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3789 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3790 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3791 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3792 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3793 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3794 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3795 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3796 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3797 | |
| 3798 | 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] | 3799 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3800 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3801 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3802 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3803 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3804 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3805 | if (r->flag == 0) { /* '!' */ |
| 3806 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3807 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3808 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3809 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3810 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3811 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3812 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3813 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3814 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3815 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3816 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3817 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3818 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3819 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3820 | } 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] | 3821 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3822 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3823 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3824 | } else { |
| 3825 | /* "{...} fi" is ok. "{...} if" is not |
| 3826 | * Example: |
| 3827 | * if { echo foo; } then { echo bar; } fi */ |
| 3828 | if (ctx->command->group) |
| 3829 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3830 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3831 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3832 | ctx->ctx_res_w = r->res; |
| 3833 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3834 | ctx->is_assignment = r->assignment_flag; |
| 3835 | 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] | 3836 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3837 | if (ctx->old_flag & FLAG_END) { |
| 3838 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3839 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3840 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3841 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3842 | old = ctx->stack; |
| 3843 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3844 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3845 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3846 | /* At this point, the compound command's string is in |
| 3847 | * ctx->as_string... except for the leading keyword! |
| 3848 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3849 | * ctx->as_string will contain "true; then echo a; fi", |
| 3850 | * with "if " remaining in old->as_string! |
| 3851 | */ |
| 3852 | { |
| 3853 | char *str; |
| 3854 | int len = old->as_string.length; |
| 3855 | /* Concatenate halves */ |
| 3856 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3857 | o_free_unsafe(&ctx->as_string); |
| 3858 | /* Find where leading keyword starts in first half */ |
| 3859 | str = old->as_string.data + len; |
| 3860 | if (str > old->as_string.data) |
| 3861 | str--; /* skip whitespace after keyword */ |
| 3862 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3863 | str--; |
| 3864 | /* Ugh, we're done with this horrid hack */ |
| 3865 | old->command->group_as_string = xstrdup(str); |
| 3866 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3867 | old->command->group_as_string); |
| 3868 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3869 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3870 | *ctx = *old; /* physical copy */ |
| 3871 | free(old); |
| 3872 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3873 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3875 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3876 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3877 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3878 | * Normal return is 0. Syntax errors return 1. |
| 3879 | * Note: on return, word is reset, but not o_free'd! |
| 3880 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3881 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3882 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3883 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3884 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3885 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 3886 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3887 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3888 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3889 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3890 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3891 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3892 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3893 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3894 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3895 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3896 | * If the redirection operator is "<<" or "<<-", the word |
| 3897 | * that follows the redirection operator shall be |
| 3898 | * subjected to quote removal; it is unspecified whether |
| 3899 | * any of the other expansions occur. For the other |
| 3900 | * redirection operators, the word that follows the |
| 3901 | * redirection operator shall be subjected to tilde |
| 3902 | * expansion, parameter expansion, command substitution, |
| 3903 | * arithmetic expansion, and quote removal. |
| 3904 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3905 | * on the word by a non-interactive shell; an interactive |
| 3906 | * shell may perform it, but shall do so only when |
| 3907 | * the expansion would result in one word." |
| 3908 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3909 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3910 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3911 | // as written: |
| 3912 | // <<EOF$t |
| 3913 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3914 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3915 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3916 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3917 | /* Cater for >\file case: |
| 3918 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3919 | * Same with heredocs: |
| 3920 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3921 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3922 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3923 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3924 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3925 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3926 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3927 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3928 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3929 | 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] | 3930 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3931 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3932 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3933 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3934 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3935 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3936 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3937 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3938 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3939 | ctx->ctx_dsemicolon = 0; |
| 3940 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3941 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3942 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3943 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3944 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3945 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3946 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3947 | # if ENABLE_HUSH_CASE |
| 3948 | && ctx->ctx_res_w != RES_CASE |
| 3949 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3950 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3951 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3952 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3953 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3954 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3955 | # if ENABLE_HUSH_LINENO_VAR |
| 3956 | /* Case: |
| 3957 | * "while ...; do |
| 3958 | * cmd ..." |
| 3959 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3960 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3961 | */ |
| 3962 | if (0 |
| 3963 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3964 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3965 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3966 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3967 | ) { |
| 3968 | done_pipe(ctx, PIPE_SEQ); |
| 3969 | } |
| 3970 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3971 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3972 | debug_printf_parse("done_word return %d\n", |
| 3973 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3974 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3975 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3976 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 3977 | if (0 |
| 3978 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3979 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3980 | # endif |
| 3981 | /* In bash, local/export/readonly are special, args |
| 3982 | * are assignments and therefore expansion of them |
| 3983 | * should be "one-word" expansion: |
| 3984 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 3985 | * compare with: |
| 3986 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 3987 | * ls: cannot access i=a: No such file or directory |
| 3988 | * ls: cannot access b: No such file or directory |
| 3989 | * Note: bash 3.2.33(1) does this only if export word |
| 3990 | * itself is not quoted: |
| 3991 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 3992 | * aaa bbb |
| 3993 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 3994 | * aaa |
| 3995 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3996 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 3997 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 3998 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3999 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4000 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 4001 | } |
| 4002 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4003 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4004 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4005 | #endif /* HAS_KEYWORDS */ |
| 4006 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4007 | if (command->group) { |
| 4008 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4009 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4010 | debug_printf_parse("done_word return 1: syntax error, " |
| 4011 | "groups and arglists don't mix\n"); |
| 4012 | return 1; |
| 4013 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4014 | |
| 4015 | /* If this word wasn't an assignment, next ones definitely |
| 4016 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4017 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4018 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4019 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4020 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4021 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4022 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4023 | command->assignment_cnt++; |
| 4024 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4025 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4026 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4027 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4028 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4029 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4030 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4031 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4032 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4033 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4034 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4035 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4036 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4037 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4038 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4039 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4040 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4041 | return 1; |
| 4042 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4043 | /* Force FOR to have just one word (variable name) */ |
| 4044 | /* NB: basically, this makes hush see "for v in ..." |
| 4045 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4046 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4047 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4048 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4049 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4050 | #if ENABLE_HUSH_CASE |
| 4051 | /* Force CASE to have just one word */ |
| 4052 | if (ctx->ctx_res_w == RES_CASE) { |
| 4053 | done_pipe(ctx, PIPE_SEQ); |
| 4054 | } |
| 4055 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4056 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4057 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4058 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4059 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4060 | return 0; |
| 4061 | } |
| 4062 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4063 | |
| 4064 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4065 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4066 | * Return: |
| 4067 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4068 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4069 | * REDIRFD_TO_FILE if no & was seen, |
| 4070 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4071 | */ |
| 4072 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4073 | #define parse_redir_right_fd(as_string, input) \ |
| 4074 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4075 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4076 | 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] | 4077 | { |
| 4078 | int ch, d, ok; |
| 4079 | |
| 4080 | ch = i_peek(input); |
| 4081 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4082 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4083 | |
| 4084 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4085 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4086 | ch = i_peek(input); |
| 4087 | if (ch == '-') { |
| 4088 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4089 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4090 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4091 | } |
| 4092 | d = 0; |
| 4093 | ok = 0; |
| 4094 | while (ch != EOF && isdigit(ch)) { |
| 4095 | d = d*10 + (ch-'0'); |
| 4096 | ok = 1; |
| 4097 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4098 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4099 | ch = i_peek(input); |
| 4100 | } |
| 4101 | if (ok) return d; |
| 4102 | |
| 4103 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4104 | |
| 4105 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4106 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4109 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4110 | */ |
| 4111 | static int parse_redirect(struct parse_context *ctx, |
| 4112 | int fd, |
| 4113 | redir_type style, |
| 4114 | struct in_str *input) |
| 4115 | { |
| 4116 | struct command *command = ctx->command; |
| 4117 | struct redir_struct *redir; |
| 4118 | struct redir_struct **redirp; |
| 4119 | int dup_num; |
| 4120 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4121 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4122 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4123 | /* Check for a '>&1' type redirect */ |
| 4124 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4125 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4126 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4127 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4128 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4129 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4130 | if (dup_num) { /* <<-... */ |
| 4131 | ch = i_getch(input); |
| 4132 | nommu_addchr(&ctx->as_string, ch); |
| 4133 | ch = i_peek(input); |
| 4134 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4135 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4136 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4137 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4138 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4139 | if (ch == '|') { |
| 4140 | /* >|FILE redirect ("clobbering" >). |
| 4141 | * Since we do not support "set -o noclobber" yet, |
| 4142 | * >| and > are the same for now. Just eat |. |
| 4143 | */ |
| 4144 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4145 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | /* Create a new redir_struct and append it to the linked list */ |
| 4150 | redirp = &command->redirects; |
| 4151 | while ((redir = *redirp) != NULL) { |
| 4152 | redirp = &(redir->next); |
| 4153 | } |
| 4154 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4155 | /* redir->next = NULL; */ |
| 4156 | /* redir->rd_filename = NULL; */ |
| 4157 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4158 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4159 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4160 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4161 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4162 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4163 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4164 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4165 | /* Erik had a check here that the file descriptor in question |
| 4166 | * is legit; I postpone that to "run time" |
| 4167 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4168 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4169 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4170 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4171 | #if 0 /* Instead we emit error message at run time */ |
| 4172 | if (ctx->pending_redirect) { |
| 4173 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4174 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4175 | } |
| 4176 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4177 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4178 | * end of the next parsed word. */ |
| 4179 | ctx->pending_redirect = redir; |
| 4180 | } |
| 4181 | return 0; |
| 4182 | } |
| 4183 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4184 | /* If a redirect is immediately preceded by a number, that number is |
| 4185 | * supposed to tell which file descriptor to redirect. This routine |
| 4186 | * looks for such preceding numbers. In an ideal world this routine |
| 4187 | * needs to handle all the following classes of redirects... |
| 4188 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4189 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4190 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4191 | * 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] | 4192 | * |
| 4193 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4194 | * "2.7 Redirection |
| 4195 | * ... If n is quoted, the number shall not be recognized as part of |
| 4196 | * the redirection expression. For example: |
| 4197 | * echo \2>a |
| 4198 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4199 | * 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] | 4200 | * |
| 4201 | * A -1 return means no valid number was found, |
| 4202 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4203 | */ |
| 4204 | static int redirect_opt_num(o_string *o) |
| 4205 | { |
| 4206 | int num; |
| 4207 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4208 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4209 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4210 | num = bb_strtou(o->data, NULL, 10); |
| 4211 | if (errno || num < 0) |
| 4212 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4213 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4214 | return num; |
| 4215 | } |
| 4216 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4217 | #if BB_MMU |
| 4218 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4219 | fetch_till_str(input, word, skip_tabs) |
| 4220 | #endif |
| 4221 | static char *fetch_till_str(o_string *as_string, |
| 4222 | struct in_str *input, |
| 4223 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4224 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4225 | { |
| 4226 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4227 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4228 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4229 | int ch; |
| 4230 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4231 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4232 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4233 | while (1) { |
| 4234 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4235 | if (ch != EOF) |
| 4236 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4237 | if (ch == '\n' || ch == EOF) { |
| 4238 | check_heredoc_end: |
| 4239 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4240 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4241 | heredoc.data[past_EOL] = '\0'; |
| 4242 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4243 | return heredoc.data; |
| 4244 | } |
| 4245 | if (ch == '\n') { |
| 4246 | /* This is a new line. |
| 4247 | * Remember position and backslash-escaping status. |
| 4248 | */ |
| 4249 | o_addchr(&heredoc, ch); |
| 4250 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4251 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4252 | past_EOL = heredoc.length; |
| 4253 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4254 | do { |
| 4255 | ch = i_getch(input); |
| 4256 | if (ch != EOF) |
| 4257 | nommu_addchr(as_string, ch); |
| 4258 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4259 | /* If this immediately ended the line, |
| 4260 | * go back to end-of-line checks. |
| 4261 | */ |
| 4262 | if (ch == '\n') |
| 4263 | goto check_heredoc_end; |
| 4264 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4265 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4266 | } |
| 4267 | if (ch == EOF) { |
| 4268 | o_free_unsafe(&heredoc); |
| 4269 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4270 | } |
| 4271 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4272 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4273 | if (prev == '\\' && ch == '\\') |
| 4274 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4275 | prev = 0; /* not \ */ |
| 4276 | else |
| 4277 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4281 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4282 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4283 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4284 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4285 | { |
| 4286 | struct pipe *pi = ctx->list_head; |
| 4287 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4288 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4289 | int i; |
| 4290 | struct command *cmd = pi->cmds; |
| 4291 | |
| 4292 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4293 | pi->num_cmds, |
| 4294 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4295 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4296 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4297 | |
| 4298 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4299 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4300 | while (redir) { |
| 4301 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4302 | char *p; |
| 4303 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4304 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4305 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4306 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4307 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4308 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4309 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4310 | return 1; |
| 4311 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4312 | free(redir->rd_filename); |
| 4313 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4314 | heredoc_cnt--; |
| 4315 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4316 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4317 | } |
| 4318 | cmd++; |
| 4319 | } |
| 4320 | pi = pi->next; |
| 4321 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4322 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4323 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4324 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4325 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4326 | #endif |
| 4327 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4331 | static int run_list(struct pipe *pi); |
| 4332 | #if BB_MMU |
| 4333 | #define parse_stream(pstring, input, end_trigger) \ |
| 4334 | parse_stream(input, end_trigger) |
| 4335 | #endif |
| 4336 | static struct pipe *parse_stream(char **pstring, |
| 4337 | struct in_str *input, |
| 4338 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4339 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4340 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4341 | static int parse_group(struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4342 | struct in_str *input, int ch) |
| 4343 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4344 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4345 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4346 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4347 | #if BB_MMU |
| 4348 | # define as_string NULL |
| 4349 | #else |
| 4350 | char *as_string = NULL; |
| 4351 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4352 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4353 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4354 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4355 | |
| 4356 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4357 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4358 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4359 | if (ctx->word.length) |
| 4360 | if (done_word(ctx)) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4361 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4362 | if (!command->argv) |
| 4363 | goto skip; /* (... */ |
| 4364 | if (command->argv[1]) { /* word word ... (... */ |
| 4365 | syntax_error_unexpected_ch('('); |
| 4366 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4367 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4368 | /* it is "word(..." or "word (..." */ |
| 4369 | do |
| 4370 | ch = i_getch(input); |
| 4371 | while (ch == ' ' || ch == '\t'); |
| 4372 | if (ch != ')') { |
| 4373 | syntax_error_unexpected_ch(ch); |
| 4374 | return 1; |
| 4375 | } |
| 4376 | nommu_addchr(&ctx->as_string, ch); |
| 4377 | do |
| 4378 | ch = i_getch(input); |
| 4379 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4380 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4381 | syntax_error_unexpected_ch(ch); |
| 4382 | return 1; |
| 4383 | } |
| 4384 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4385 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4386 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4387 | } |
| 4388 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4389 | |
| 4390 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4391 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4392 | || ctx->word.length /* word{... */ |
| 4393 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4394 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4395 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4396 | debug_printf_parse("parse_group return 1: " |
| 4397 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4398 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4399 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4400 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4401 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4402 | IF_HUSH_FUNCTIONS(skip:) |
| 4403 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4404 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4405 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4406 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4407 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4408 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4409 | } else { |
| 4410 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4411 | ch = i_peek(input); |
| 4412 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4413 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4414 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4415 | syntax_error_unexpected_ch(ch); |
| 4416 | return 1; |
| 4417 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4418 | if (ch != '(') { |
| 4419 | ch = i_getch(input); |
| 4420 | nommu_addchr(&ctx->as_string, ch); |
| 4421 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4422 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4423 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4424 | pipe_list = parse_stream(&as_string, input, endch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4425 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4426 | if (as_string) |
| 4427 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4428 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4429 | |
| 4430 | /* empty ()/{} or parse error? */ |
| 4431 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4432 | /* parse_stream already emitted error msg */ |
| 4433 | if (!BB_MMU) |
| 4434 | free(as_string); |
| 4435 | debug_printf_parse("parse_group return 1: " |
| 4436 | "parse_stream returned %p\n", pipe_list); |
| 4437 | return 1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4438 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4439 | #if !BB_MMU |
| 4440 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4441 | command->group_as_string = as_string; |
| 4442 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4443 | command->group_as_string); |
| 4444 | #endif |
| 4445 | |
| 4446 | #if ENABLE_HUSH_FUNCTIONS |
| 4447 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4448 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4449 | struct command *cmd2; |
| 4450 | |
| 4451 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4452 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4453 | cmd2->group = pipe_list; |
| 4454 | # if !BB_MMU |
| 4455 | //UNTESTED! |
| 4456 | cmd2->group_as_string = command->group_as_string; |
| 4457 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4458 | # endif |
| 4459 | |
| 4460 | pipe_list = new_pipe(); |
| 4461 | pipe_list->cmds = cmd2; |
| 4462 | pipe_list->num_cmds = 1; |
| 4463 | } |
| 4464 | #endif |
| 4465 | |
| 4466 | command->group = pipe_list; |
| 4467 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4468 | debug_printf_parse("parse_group return 0\n"); |
| 4469 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4470 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4471 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4474 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4475 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4476 | 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] | 4477 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4478 | 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] | 4479 | { |
| 4480 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4481 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4482 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4483 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4484 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4485 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4486 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4487 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4488 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4489 | } |
| 4490 | } |
| 4491 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4492 | 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] | 4493 | { |
| 4494 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4495 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4496 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4497 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4498 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4499 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4500 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4501 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4502 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4503 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4504 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4505 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4506 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4507 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4508 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4509 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4510 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4511 | continue; |
| 4512 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4513 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4514 | } |
| 4515 | } |
| 4516 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4517 | * \` quoting. |
| 4518 | * "Within the backquoted style of command substitution, backslash |
| 4519 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4520 | * The search for the matching backquote shall be satisfied by the first |
| 4521 | * backquote found without a preceding backslash; during this search, |
| 4522 | * if a non-escaped backquote is encountered within a shell comment, |
| 4523 | * a here-document, an embedded command substitution of the $(command) |
| 4524 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4525 | * or double-quoted string that begins, but does not end, within the |
| 4526 | * "`...`" sequence produces undefined results." |
| 4527 | * Example Output |
| 4528 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4529 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4530 | 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] | 4531 | { |
| 4532 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4533 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4534 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4535 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4536 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4537 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4538 | ch = i_getch(input); |
| 4539 | if (ch != '`' |
| 4540 | && ch != '$' |
| 4541 | && ch != '\\' |
| 4542 | && (!in_dquote || ch != '"') |
| 4543 | ) { |
| 4544 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4545 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4546 | } |
| 4547 | if (ch == EOF) { |
| 4548 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4549 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4550 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4551 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4552 | } |
| 4553 | } |
| 4554 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4555 | * quoting and nested ()s. |
| 4556 | * "With the $(command) style of command substitution, all characters |
| 4557 | * following the open parenthesis to the matching closing parenthesis |
| 4558 | * constitute the command. Any valid shell script can be used for command, |
| 4559 | * except a script consisting solely of redirections which produces |
| 4560 | * unspecified results." |
| 4561 | * Example Output |
| 4562 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4563 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4564 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4565 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4566 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4567 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4568 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4569 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4570 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4571 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4572 | 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] | 4573 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4574 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4575 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4576 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4577 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4578 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4579 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4580 | |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4581 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4582 | G.promptmode = 1; /* PS2 */ |
Denys Vlasenko | 817a202 | 2018-06-26 15:35:17 +0200 | [diff] [blame] | 4583 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4584 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4585 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4586 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4587 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4588 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4589 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4590 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4591 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4592 | if (ch == end_ch |
| 4593 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4594 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4595 | # endif |
| 4596 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4597 | if (!dbl) |
| 4598 | break; |
| 4599 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4600 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4601 | i_getch(input); /* eat second ')' */ |
| 4602 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4603 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4604 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4605 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4606 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4607 | if (ch == '(' || ch == '{') { |
| 4608 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4609 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4610 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4611 | o_addchr(dest, ch); |
| 4612 | continue; |
| 4613 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4614 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4615 | if (!add_till_single_quote(dest, input)) |
| 4616 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4617 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4618 | continue; |
| 4619 | } |
| 4620 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4621 | if (!add_till_double_quote(dest, input)) |
| 4622 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4623 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4624 | continue; |
| 4625 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4626 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4627 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4628 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4629 | o_addchr(dest, ch); |
| 4630 | continue; |
| 4631 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4632 | if (ch == '\\') { |
| 4633 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4634 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4635 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4636 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4637 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4638 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4639 | #if 0 |
| 4640 | if (ch == '\n') { |
| 4641 | /* "backslash+newline", ignore both */ |
| 4642 | o_delchr(dest); /* undo insertion of '\' */ |
| 4643 | continue; |
| 4644 | } |
| 4645 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4646 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4647 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4648 | continue; |
| 4649 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4650 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4651 | 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] | 4652 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4653 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4654 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4655 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4656 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4657 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4658 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4659 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4660 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4661 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4662 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4663 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4664 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4665 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4666 | 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] | 4667 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4668 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4669 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4670 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4671 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4672 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4673 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4674 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4675 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4676 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4677 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4678 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4679 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4680 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4681 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4682 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4683 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4684 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4685 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4686 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4687 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4688 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4689 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4690 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4691 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4692 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4693 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4694 | o_addchr(dest, ch | quote_mask); |
| 4695 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4696 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4697 | case '$': /* pid */ |
| 4698 | case '!': /* last bg pid */ |
| 4699 | case '?': /* last exit code */ |
| 4700 | case '#': /* number of args */ |
| 4701 | case '*': /* args */ |
| 4702 | case '@': /* args */ |
| 4703 | goto make_one_char_var; |
| 4704 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4705 | char len_single_ch; |
| 4706 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4707 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4708 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4709 | ch = i_getch(input); /* eat '{' */ |
| 4710 | nommu_addchr(as_string, ch); |
| 4711 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4712 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4713 | /* It should be ${?}, or ${#var}, |
| 4714 | * or even ${?+subst} - operator acting on a special variable, |
| 4715 | * or the beginning of variable name. |
| 4716 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4717 | if (ch == EOF |
| 4718 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4719 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4720 | bad_dollar_syntax: |
| 4721 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4722 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4723 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4724 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4725 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4726 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4727 | ch |= quote_mask; |
| 4728 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4729 | /* 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] | 4730 | * However, this regresses some of our testsuite cases |
| 4731 | * which check invalid constructs like ${%}. |
| 4732 | * Oh well... let's check that the var name part is fine... */ |
| 4733 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4734 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4735 | unsigned pos; |
| 4736 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4737 | o_addchr(dest, ch); |
| 4738 | debug_printf_parse(": '%c'\n", ch); |
| 4739 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4740 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4741 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4742 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4743 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4744 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4745 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4746 | unsigned end_ch; |
| 4747 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4748 | /* handle parameter expansions |
| 4749 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4750 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4751 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4752 | if (len_single_ch != '#' |
| 4753 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4754 | || i_peek(input) != '}' |
| 4755 | ) { |
| 4756 | goto bad_dollar_syntax; |
| 4757 | } |
| 4758 | /* else: it's "length of C" ${#C} op, |
| 4759 | * where C is a single char |
| 4760 | * special var name, e.g. ${#!}. |
| 4761 | */ |
| 4762 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4763 | /* Eat everything until closing '}' (or ':') */ |
| 4764 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4765 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4766 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4767 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4768 | ) { |
| 4769 | /* It's ${var:N[:M]} thing */ |
| 4770 | end_ch = '}' * 0x100 + ':'; |
| 4771 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4772 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4773 | && ch == '/' |
| 4774 | ) { |
| 4775 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4776 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4777 | i_getch(input); |
| 4778 | nommu_addchr(as_string, '/'); |
| 4779 | ch = '\\'; |
| 4780 | } |
| 4781 | end_ch = '}' * 0x100 + '/'; |
| 4782 | } |
| 4783 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4784 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4785 | if (!BB_MMU) |
| 4786 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4787 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4788 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4789 | if (last_ch == 0) /* error? */ |
| 4790 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4791 | #else |
| 4792 | #error Simple code to only allow ${var} is not implemented |
| 4793 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4794 | if (as_string) { |
| 4795 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4796 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4797 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4798 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4799 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4800 | && (end_ch & 0xff00) |
| 4801 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4802 | /* close the first block: */ |
| 4803 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4804 | /* while parsing N from ${var:N[:M]} |
| 4805 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4806 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4807 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4808 | end_ch = '}'; |
| 4809 | goto again; |
| 4810 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4811 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4812 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4813 | /* it's ${var:N} - emulate :999999999 */ |
| 4814 | o_addstr(dest, "999999999"); |
| 4815 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4816 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4817 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4818 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4819 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4820 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4821 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4822 | break; |
| 4823 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4824 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4825 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4826 | unsigned pos; |
| 4827 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4828 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4829 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4830 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4831 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4832 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4833 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4834 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4835 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4836 | if (!BB_MMU) |
| 4837 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4838 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4839 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4840 | if (as_string) { |
| 4841 | o_addstr(as_string, dest->data + pos); |
| 4842 | o_addchr(as_string, ')'); |
| 4843 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4844 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4845 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4846 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4847 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4848 | # endif |
| 4849 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4850 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4851 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4852 | if (!BB_MMU) |
| 4853 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4854 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4855 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4856 | if (as_string) { |
| 4857 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4858 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4859 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4860 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4861 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4862 | break; |
| 4863 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4864 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4865 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4866 | goto make_var; |
| 4867 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4868 | /* TODO: $_ and $-: */ |
| 4869 | /* $_ Shell or shell script name; or last argument of last command |
| 4870 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4871 | * but in command's env, set to full pathname used to invoke it */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4872 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4873 | ch = i_getch(input); |
| 4874 | nommu_addchr(as_string, ch); |
| 4875 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4876 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4877 | ch = '_'; |
| 4878 | goto make_var1; |
| 4879 | } |
| 4880 | /* else: it's $_ */ |
| 4881 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4882 | default: |
| 4883 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4885 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4886 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4887 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4890 | #if BB_MMU |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4891 | #define encode_string(as_string, dest, input, dquote_end) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4892 | encode_string(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4893 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4894 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4895 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4896 | o_string *dest, |
| 4897 | struct in_str *input, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4898 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4899 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4900 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4901 | int next; |
| 4902 | |
| 4903 | again: |
| 4904 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4905 | if (ch != EOF) |
| 4906 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4907 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4908 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4909 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4910 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4911 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4912 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4913 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4914 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4915 | } |
| 4916 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4917 | if (ch != '\n') { |
| 4918 | next = i_peek(input); |
| 4919 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4920 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4921 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 4922 | if (ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4923 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 4924 | /* Testcase: in interactive shell a file with |
| 4925 | * echo "unterminated string\<eof> |
| 4926 | * is sourced. |
| 4927 | */ |
| 4928 | syntax_error_unterm_ch('"'); |
| 4929 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4930 | } |
| 4931 | /* bash: |
| 4932 | * "The backslash retains its special meaning [in "..."] |
| 4933 | * only when followed by one of the following characters: |
| 4934 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4935 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4936 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4937 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4938 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4939 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4940 | ch = i_getch(input); /* eat next */ |
| 4941 | if (ch == '\n') |
| 4942 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4943 | } /* else: ch remains == '\\', and we double it below: */ |
| 4944 | 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] | 4945 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4946 | goto again; |
| 4947 | } |
| 4948 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4949 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4950 | debug_printf_parse("encode_string return 0: " |
| 4951 | "parse_dollar returned 0 (error)\n"); |
| 4952 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4953 | } |
| 4954 | goto again; |
| 4955 | } |
| 4956 | #if ENABLE_HUSH_TICK |
| 4957 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4958 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4959 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4960 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4961 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4962 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4963 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4964 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4965 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4966 | } |
| 4967 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4968 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4969 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4970 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4971 | } |
| 4972 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4973 | /* |
| 4974 | * Scan input until EOF or end_trigger char. |
| 4975 | * Return a list of pipes to execute, or NULL on EOF |
| 4976 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4977 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4978 | * reset parsing machinery and start parsing anew, |
| 4979 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4980 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4981 | static struct pipe *parse_stream(char **pstring, |
| 4982 | struct in_str *input, |
| 4983 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4984 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4985 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4986 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4987 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4988 | /* 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] | 4989 | * 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] | 4990 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4991 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4992 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4993 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4994 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4995 | initialize_context(&ctx); |
| 4996 | |
| 4997 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 4998 | * Preventing this: |
| 4999 | */ |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5000 | ctx.word.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5001 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5002 | /* We used to separate words on $IFS here. This was wrong. |
| 5003 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5004 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5005 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5006 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5007 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5008 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5009 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5010 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5011 | int ch; |
| 5012 | int next; |
| 5013 | int redir_fd; |
| 5014 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5015 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5016 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5017 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5018 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5019 | if (ch == EOF) { |
| 5020 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5021 | |
| 5022 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5023 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5024 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5025 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5026 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5027 | syntax_error_unterm_ch('('); |
| 5028 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5029 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5030 | if (end_trigger == '}') { |
| 5031 | syntax_error_unterm_ch('{'); |
| 5032 | goto parse_error; |
| 5033 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5034 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5035 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5036 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5037 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5038 | o_free(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5039 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5040 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5041 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5042 | /* (this makes bare "&" cmd a no-op. |
| 5043 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5044 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5045 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5046 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5047 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5048 | pi = NULL; |
| 5049 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5050 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5051 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5052 | if (pstring) |
| 5053 | *pstring = ctx.as_string.data; |
| 5054 | else |
| 5055 | o_free_unsafe(&ctx.as_string); |
| 5056 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5057 | debug_leave(); |
| 5058 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5059 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5060 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5061 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5062 | /* Handle "'" and "\" first, as they won't play nice with |
| 5063 | * i_peek_and_eat_bkslash_nl() anyway: |
| 5064 | * echo z\\ |
| 5065 | * and |
| 5066 | * echo '\ |
| 5067 | * ' |
| 5068 | * would break. |
| 5069 | */ |
Denys Vlasenko | f693b60 | 2018-04-11 20:00:43 +0200 | [diff] [blame] | 5070 | if (ch == '\\') { |
| 5071 | ch = i_getch(input); |
| 5072 | if (ch == '\n') |
| 5073 | continue; /* drop \<newline>, get next char */ |
| 5074 | nommu_addchr(&ctx.as_string, '\\'); |
| 5075 | o_addchr(&ctx.word, '\\'); |
| 5076 | if (ch == EOF) { |
| 5077 | /* Testcase: eval 'echo Ok\' */ |
| 5078 | /* bash-4.3.43 was removing backslash, |
| 5079 | * but 4.4.19 retains it, most other shells too |
| 5080 | */ |
| 5081 | continue; /* get next char */ |
| 5082 | } |
| 5083 | /* Example: echo Hello \2>file |
| 5084 | * we need to know that word 2 is quoted |
| 5085 | */ |
| 5086 | ctx.word.has_quoted_part = 1; |
| 5087 | nommu_addchr(&ctx.as_string, ch); |
| 5088 | o_addchr(&ctx.word, ch); |
| 5089 | continue; /* get next char */ |
| 5090 | } |
| 5091 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5092 | if (ch == '\'') { |
| 5093 | ctx.word.has_quoted_part = 1; |
| 5094 | next = i_getch(input); |
| 5095 | if (next == '\'' && !ctx.pending_redirect) |
| 5096 | goto insert_empty_quoted_str_marker; |
| 5097 | |
| 5098 | ch = next; |
| 5099 | while (1) { |
| 5100 | if (ch == EOF) { |
| 5101 | syntax_error_unterm_ch('\''); |
| 5102 | goto parse_error; |
| 5103 | } |
| 5104 | nommu_addchr(&ctx.as_string, ch); |
| 5105 | if (ch == '\'') |
| 5106 | break; |
| 5107 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5108 | /* Convert raw ^C to corresponding special variable reference */ |
| 5109 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5110 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5111 | } |
| 5112 | o_addqchr(&ctx.word, ch); |
| 5113 | ch = i_getch(input); |
| 5114 | } |
| 5115 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5116 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5117 | |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5118 | next = '\0'; |
| 5119 | if (ch != '\n') |
| 5120 | next = i_peek_and_eat_bkslash_nl(input); |
| 5121 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5122 | is_special = "{}<>;&|()#" /* special outside of "str" */ |
Denys Vlasenko | 0403bed | 2018-04-11 01:33:54 +0200 | [diff] [blame] | 5123 | "$\"" IF_HUSH_TICK("`") /* always special */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5124 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5125 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5126 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5127 | || ctx.word.length /* word{... - non-special */ |
| 5128 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5129 | || (next != ';' /* }; - special */ |
| 5130 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5131 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5132 | && next != '&' /* }& and }&& ... - special */ |
| 5133 | && next != '|' /* }|| ... - special */ |
| 5134 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5135 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5136 | ) { |
| 5137 | /* They are not special, skip "{}" */ |
| 5138 | is_special += 2; |
| 5139 | } |
| 5140 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5141 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5142 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5143 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5144 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5145 | o_addQchr(&ctx.word, ch); |
| 5146 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5147 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5148 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5149 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5150 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5151 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5152 | 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] | 5153 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5154 | continue; |
| 5155 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5156 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5157 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5158 | #if ENABLE_HUSH_LINENO_VAR |
| 5159 | /* Case: |
| 5160 | * "while ...; do<whitespace><newline> |
| 5161 | * cmd ..." |
| 5162 | * would think that "cmd" starts in <whitespace> - |
| 5163 | * i.e., at the previous line. |
| 5164 | * We need to skip all whitespace before newlines. |
| 5165 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5166 | while (ch != '\n') { |
| 5167 | next = i_peek(input); |
| 5168 | if (next != ' ' && next != '\t' && next != '\n') |
| 5169 | break; /* next char is not ws */ |
| 5170 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5171 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5172 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5173 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5174 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5175 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5176 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5177 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5178 | /* Is this a case when newline is simply ignored? |
| 5179 | * Some examples: |
| 5180 | * "cmd | <newline> cmd ..." |
| 5181 | * "case ... in <newline> word) ..." |
| 5182 | */ |
| 5183 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5184 | && ctx.word.length == 0 && !ctx.word.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5185 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5186 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5187 | * Without check #1, interactive shell |
| 5188 | * ignores even bare <newline>, |
| 5189 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5190 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5191 | * ps2> _ <=== wrong, should be ps1 |
| 5192 | * Without check #2, "cmd & <newline>" |
| 5193 | * is similarly mistreated. |
| 5194 | * (BTW, this makes "cmd & cmd" |
| 5195 | * and "cmd && cmd" non-orthogonal. |
| 5196 | * Really, ask yourself, why |
| 5197 | * "cmd && <newline>" doesn't start |
| 5198 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5199 | * The only reason is that it might be |
| 5200 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5201 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5202 | */ |
| 5203 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5204 | if (pi->num_cmds != 0 /* check #1 */ |
| 5205 | && pi->followup != PIPE_BG /* check #2 */ |
| 5206 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5207 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5208 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5209 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5210 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5211 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5212 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5213 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5214 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5215 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5216 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5217 | heredoc_cnt = 0; |
| 5218 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5219 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5220 | 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] | 5221 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5222 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5223 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5224 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5225 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5226 | |
| 5227 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5228 | * } is an ordinary char in this case, even inside { cmd; } |
| 5229 | * Pathological example: { ""}; } should exec "}" cmd |
| 5230 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5231 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5232 | if (ctx.word.length != 0 /* word} */ |
| 5233 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5234 | ) { |
| 5235 | goto ordinary_char; |
| 5236 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5237 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5238 | /* Generally, there should be semicolon: "cmd; }" |
| 5239 | * However, bash allows to omit it if "cmd" is |
| 5240 | * a group. Examples: |
| 5241 | * { { echo 1; } } |
| 5242 | * {(echo 1)} |
| 5243 | * { echo 0 >&2 | { echo 1; } } |
| 5244 | * { while false; do :; done } |
| 5245 | * { case a in b) ;; esac } |
| 5246 | */ |
| 5247 | if (ctx.command->group) |
| 5248 | goto term_group; |
| 5249 | goto ordinary_char; |
| 5250 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5251 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5252 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5253 | goto skip_end_trigger; |
| 5254 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5255 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5256 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5257 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5258 | && (ch != ';' || heredoc_cnt == 0) |
| 5259 | #if ENABLE_HUSH_CASE |
| 5260 | && (ch != ')' |
| 5261 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5262 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5263 | ) |
| 5264 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5265 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5266 | if (heredoc_cnt) { |
| 5267 | /* This is technically valid: |
| 5268 | * { cat <<HERE; }; echo Ok |
| 5269 | * heredoc |
| 5270 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5271 | * HERE |
| 5272 | * but we don't support this. |
| 5273 | * We require heredoc to be in enclosing {}/(), |
| 5274 | * if any. |
| 5275 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5276 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5277 | goto parse_error; |
| 5278 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5279 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5280 | goto parse_error; |
| 5281 | } |
| 5282 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5283 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5284 | 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] | 5285 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5286 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5287 | 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] | 5288 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5289 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5290 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5291 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5292 | if (pstring) |
| 5293 | *pstring = ctx.as_string.data; |
| 5294 | else |
| 5295 | o_free_unsafe(&ctx.as_string); |
| 5296 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5297 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5298 | /* Example: bare "{ }", "()" */ |
| 5299 | G.last_exitcode = 2; /* bash compat */ |
| 5300 | syntax_error_unexpected_ch(ch); |
| 5301 | goto parse_error2; |
| 5302 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5303 | debug_printf_parse("parse_stream return %p: " |
| 5304 | "end_trigger char found\n", |
| 5305 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5306 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5307 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5308 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5309 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5310 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5311 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5312 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5313 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5314 | /* Catch <, > before deciding whether this word is |
| 5315 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5316 | switch (ch) { |
| 5317 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5318 | redir_fd = redirect_opt_num(&ctx.word); |
| 5319 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5320 | goto parse_error; |
| 5321 | } |
| 5322 | redir_style = REDIRECT_OVERWRITE; |
| 5323 | if (next == '>') { |
| 5324 | redir_style = REDIRECT_APPEND; |
| 5325 | ch = i_getch(input); |
| 5326 | nommu_addchr(&ctx.as_string, ch); |
| 5327 | } |
| 5328 | #if 0 |
| 5329 | else if (next == '(') { |
| 5330 | syntax_error(">(process) not supported"); |
| 5331 | goto parse_error; |
| 5332 | } |
| 5333 | #endif |
| 5334 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5335 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5336 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5337 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5338 | redir_fd = redirect_opt_num(&ctx.word); |
| 5339 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5340 | goto parse_error; |
| 5341 | } |
| 5342 | redir_style = REDIRECT_INPUT; |
| 5343 | if (next == '<') { |
| 5344 | redir_style = REDIRECT_HEREDOC; |
| 5345 | heredoc_cnt++; |
| 5346 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5347 | ch = i_getch(input); |
| 5348 | nommu_addchr(&ctx.as_string, ch); |
| 5349 | } else if (next == '>') { |
| 5350 | redir_style = REDIRECT_IO; |
| 5351 | ch = i_getch(input); |
| 5352 | nommu_addchr(&ctx.as_string, ch); |
| 5353 | } |
| 5354 | #if 0 |
| 5355 | else if (next == '(') { |
| 5356 | syntax_error("<(process) not supported"); |
| 5357 | goto parse_error; |
| 5358 | } |
| 5359 | #endif |
| 5360 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5361 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5362 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5363 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5364 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5365 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5366 | /* note: we do not add it to &ctx.as_string */ |
| 5367 | /* TODO: in bash: |
| 5368 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5369 | * cmd "$(cmd2 #comment)" - syntax error |
| 5370 | * cmd "`cmd2 #comment`" - ok |
| 5371 | * We accept both (comment ends where command subst ends, in both cases). |
| 5372 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5373 | while (1) { |
| 5374 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5375 | if (ch == '\n') { |
| 5376 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5377 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5378 | } |
| 5379 | ch = i_getch(input); |
| 5380 | if (ch == EOF) |
| 5381 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5382 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5383 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5384 | } |
| 5385 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5386 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5387 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5388 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5389 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5390 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5391 | && !ctx.pending_redirect |
| 5392 | ) { |
| 5393 | /* ch is a special char and thus this word |
| 5394 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5395 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5396 | 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] | 5397 | } |
| 5398 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5399 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5400 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5401 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5402 | case SPECIAL_VAR_SYMBOL: |
| 5403 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5404 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5405 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5406 | /* fall through */ |
| 5407 | case '#': |
| 5408 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5409 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5410 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5411 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5412 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5413 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5414 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5415 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5416 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5417 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5418 | case '"': |
| 5419 | ctx.word.has_quoted_part = 1; |
| 5420 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5421 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5422 | insert_empty_quoted_str_marker: |
| 5423 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5424 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5425 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5426 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5427 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5428 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5429 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5430 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"')) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5431 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5432 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5433 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5434 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5435 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5436 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5437 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5438 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5439 | o_addchr(&ctx.word, '`'); |
| 5440 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5441 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5442 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5443 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5444 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5445 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5446 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5447 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5448 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5449 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5450 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5451 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5452 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5453 | #if ENABLE_HUSH_CASE |
| 5454 | case_semi: |
| 5455 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5456 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5457 | goto parse_error; |
| 5458 | } |
| 5459 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5460 | #if ENABLE_HUSH_CASE |
| 5461 | /* Eat multiple semicolons, detect |
| 5462 | * whether it means something special */ |
| 5463 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5464 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5465 | if (ch != ';') |
| 5466 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5467 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5468 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5469 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5470 | ctx.ctx_dsemicolon = 1; |
| 5471 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5472 | break; |
| 5473 | } |
| 5474 | } |
| 5475 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5476 | new_cmd: |
| 5477 | /* We just finished a cmd. New one may start |
| 5478 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5479 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5480 | 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] | 5481 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5482 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5483 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5484 | goto parse_error; |
| 5485 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5486 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5487 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5488 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5489 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5490 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5491 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5492 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5493 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5494 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5495 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5496 | goto parse_error; |
| 5497 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5498 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5499 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5500 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5501 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5502 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5503 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5504 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5505 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5506 | } else { |
| 5507 | /* we could pick up a file descriptor choice here |
| 5508 | * with redirect_opt_num(), but bash doesn't do it. |
| 5509 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5510 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5511 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5512 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5513 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5514 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5515 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5516 | if (ctx.ctx_res_w == RES_MATCH |
| 5517 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5518 | && ctx.word.length == 0 /* not word(... */ |
| 5519 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5520 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5521 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5522 | } |
| 5523 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5524 | case '{': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5525 | if (parse_group(&ctx, input, ch) != 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5526 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5527 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5528 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5529 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5530 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5531 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5532 | goto case_semi; |
| 5533 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5534 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5535 | /* proper use of this character is caught by end_trigger: |
| 5536 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5537 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5538 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5539 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5540 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5541 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5542 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5543 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5544 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5545 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5546 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5547 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5548 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5549 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5550 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5551 | struct parse_context *pctx; |
| 5552 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5553 | |
| 5554 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5555 | * Sample for finding leaks on syntax error recovery path. |
| 5556 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5557 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5558 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5559 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5560 | * 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] | 5561 | */ |
| 5562 | pctx = &ctx; |
| 5563 | do { |
| 5564 | /* Update pipe/command counts, |
| 5565 | * otherwise freeing may miss some */ |
| 5566 | done_pipe(pctx, PIPE_SEQ); |
| 5567 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5568 | pctx->list_head, pctx); |
| 5569 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5570 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5571 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5572 | #if !BB_MMU |
| 5573 | o_free_unsafe(&pctx->as_string); |
| 5574 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5575 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5576 | if (pctx != &ctx) { |
| 5577 | free(pctx); |
| 5578 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5579 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5580 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5581 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5582 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5583 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5584 | if (pstring) |
| 5585 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5586 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5587 | debug_leave(); |
| 5588 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5589 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5592 | |
| 5593 | /*** Execution routines ***/ |
| 5594 | |
| 5595 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5596 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5597 | #define expand_string_to_string(str, EXP_flags, do_unbackslash) \ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5598 | expand_string_to_string(str) |
| 5599 | #endif |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5600 | 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] | 5601 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5602 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5603 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5604 | |
| 5605 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5606 | * all variable references within and returns a pointer to |
| 5607 | * a list of expanded strings, possibly with larger number |
| 5608 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5609 | * This new list is allocated as a single malloc block. |
| 5610 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5611 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5612 | * Caller can deallocate entire list by single free(list). */ |
| 5613 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5614 | /* A horde of its helpers come first: */ |
| 5615 | |
| 5616 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5617 | { |
| 5618 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5619 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5620 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5621 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5622 | if (c == '{' || c == '}') { |
| 5623 | /* { -> \{, } -> \} */ |
| 5624 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5625 | /* And now we want to add { or } and continue: |
| 5626 | * o_addchr(o, c); |
| 5627 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5628 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5629 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5630 | } |
| 5631 | #endif |
| 5632 | o_addchr(o, c); |
| 5633 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5634 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5635 | o_addchr(o, '\\'); |
| 5636 | if (len) { |
| 5637 | len--; |
| 5638 | o_addchr(o, '\\'); |
| 5639 | o_addchr(o, *str++); |
| 5640 | } |
| 5641 | } |
| 5642 | } |
| 5643 | } |
| 5644 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5645 | /* Store given string, finalizing the word and starting new one whenever |
| 5646 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5647 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5648 | * Return in output->ended_in_ifs: |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5649 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5650 | */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5651 | 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] | 5652 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5653 | int last_is_ifs = 0; |
| 5654 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5655 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5656 | int word_len; |
| 5657 | |
| 5658 | if (!*str) /* EOL - do not finalize word */ |
| 5659 | break; |
| 5660 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5661 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5662 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5663 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5664 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5665 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5666 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5667 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5668 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5669 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5670 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5671 | /*/ Why can't we do it easier? */ |
| 5672 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5673 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5674 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5675 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5676 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5677 | if (!*str) /* EOL - do not finalize word */ |
| 5678 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5679 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5680 | |
| 5681 | /* We know str here points to at least one IFS char */ |
| 5682 | last_is_ifs = 1; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5683 | str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5684 | if (!*str) /* EOL - do not finalize word */ |
| 5685 | break; |
| 5686 | |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5687 | if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */ |
| 5688 | && strchr(G.ifs, *str) /* the second check would fail */ |
| 5689 | ) { |
| 5690 | /* This is a non-whitespace $IFS char */ |
| 5691 | /* Skip it and IFS whitespace chars, start new word */ |
| 5692 | str++; |
| 5693 | str += strspn(str, G.ifs_whitespace); |
| 5694 | goto new_word; |
| 5695 | } |
| 5696 | |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5697 | /* Start new word... but not always! */ |
| 5698 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5699 | if (output->has_quoted_part |
| 5700 | /* Case "v=' a'; echo $v": |
| 5701 | * here nothing precedes the space in $v expansion, |
| 5702 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5703 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5704 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5705 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5706 | ) { |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 5707 | new_word: |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5708 | o_addchr(output, '\0'); |
| 5709 | debug_print_list("expand_on_ifs", output, n); |
| 5710 | n = o_save_ptr(output, n); |
| 5711 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5712 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5713 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5714 | output->ended_in_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5715 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5716 | return n; |
| 5717 | } |
| 5718 | |
| 5719 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5720 | * they are in double quotes, with the exception that they are not :). |
| 5721 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5722 | * |
| 5723 | * Returns malloced string. |
| 5724 | * As an optimization, we return NULL if expansion is not needed. |
| 5725 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5726 | static char *encode_then_expand_string(const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5727 | { |
| 5728 | char *exp_str; |
| 5729 | struct in_str input; |
| 5730 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5731 | const char *cp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5732 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5733 | cp = str; |
| 5734 | for (;;) { |
| 5735 | if (!*cp) return NULL; /* string has no special chars */ |
| 5736 | if (*cp == '$') break; |
| 5737 | if (*cp == '\\') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5738 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5739 | if (*cp == '`') break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5740 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5741 | cp++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5742 | } |
| 5743 | |
| 5744 | /* We need to expand. Example: |
| 5745 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5746 | */ |
| 5747 | setup_string_in_str(&input, str); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5748 | encode_string(NULL, &dest, &input, EOF); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5749 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5750 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5751 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5752 | EXP_FLAG_ESC_GLOB_CHARS, |
| 5753 | /*unbackslash:*/ 1 |
| 5754 | ); |
| 5755 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5756 | o_free_unsafe(&dest); |
| 5757 | return exp_str; |
| 5758 | } |
| 5759 | |
| 5760 | #if !BASH_PATTERN_SUBST |
| 5761 | #define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \ |
| 5762 | encode_then_expand_vararg(str, handle_squotes) |
| 5763 | #endif |
| 5764 | static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash) |
| 5765 | { |
| 5766 | #if !BASH_PATTERN_SUBST |
| 5767 | const int do_unbackslash = 0; |
| 5768 | #endif |
| 5769 | char *exp_str; |
| 5770 | struct in_str input; |
| 5771 | o_string dest = NULL_O_STRING; |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5772 | const char *cp; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5773 | |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5774 | cp = str; |
| 5775 | for (;;) { |
| 5776 | if (!*cp) return NULL; /* string has no special chars */ |
| 5777 | if (*cp == '$') break; |
| 5778 | if (*cp == '\\') break; |
| 5779 | if (*cp == '\'') break; |
| 5780 | if (*cp == '"') break; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5781 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5782 | if (*cp == '`') break; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5783 | #endif |
Denys Vlasenko | 0d2e0de | 2018-07-17 14:33:19 +0200 | [diff] [blame] | 5784 | cp++; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | /* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}. |
| 5788 | * These can contain single- and double-quoted strings, |
| 5789 | * and treated as if the ARG string is initially unquoted. IOW: |
| 5790 | * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be |
| 5791 | * a dquoted string: "${var#"zz"}"), the difference only comes later |
| 5792 | * (word splitting and globbing of the ${var...} result). |
| 5793 | */ |
| 5794 | |
| 5795 | setup_string_in_str(&input, str); |
Denys Vlasenko | 8b08d5a | 2018-07-18 15:48:53 +0200 | [diff] [blame] | 5796 | dest.data = xzalloc(1); /* start as "", not as NULL */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5797 | exp_str = NULL; |
| 5798 | |
| 5799 | for (;;) { |
| 5800 | int ch; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5801 | |
| 5802 | ch = i_getch(&input); |
| 5803 | if (ch == EOF) { |
| 5804 | if (dest.o_expflags) { /* EXP_FLAG_ESC_GLOB_CHARS set? */ |
| 5805 | syntax_error_unterm_ch('"'); |
| 5806 | goto ret; /* error */ |
| 5807 | } |
| 5808 | break; |
| 5809 | } |
| 5810 | debug_printf_parse("%s: ch=%c (%d) escape=%d\n", |
| 5811 | __func__, ch, ch, !!dest.o_expflags); |
| 5812 | if (ch == '\'' && handle_squotes && !dest.o_expflags) { |
| 5813 | //quoting version of add_till_single_quote() (try to merge?): |
| 5814 | for (;;) { |
| 5815 | ch = i_getch(&input); |
| 5816 | if (ch == EOF) { |
| 5817 | syntax_error_unterm_ch('\''); |
| 5818 | goto ret; /* error */ |
| 5819 | } |
| 5820 | if (ch == '\'') |
| 5821 | break; |
| 5822 | o_addqchr(&dest, ch); |
| 5823 | } |
| 5824 | continue; |
| 5825 | } |
| 5826 | if (ch == '"') { |
| 5827 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
| 5828 | continue; |
| 5829 | } |
| 5830 | if (ch == '\\') { |
| 5831 | ch = i_getch(&input); |
| 5832 | if (ch == EOF) { |
| 5833 | //example? error message? syntax_error_unterm_ch('"'); |
| 5834 | debug_printf_parse("%s: error: \\<eof>\n", __func__); |
| 5835 | goto ret; |
| 5836 | } |
| 5837 | o_addqchr(&dest, ch); |
| 5838 | continue; |
| 5839 | } |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5840 | if (ch == '$') { |
| 5841 | if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) { |
| 5842 | debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__); |
| 5843 | goto ret; |
| 5844 | } |
| 5845 | continue; |
| 5846 | } |
| 5847 | #if ENABLE_HUSH_TICK |
| 5848 | if (ch == '`') { |
| 5849 | //unsigned pos = dest->length; |
| 5850 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5851 | o_addchr(&dest, 0x80 | '`'); |
| 5852 | if (!add_till_backquote(&dest, &input, |
| 5853 | /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */ |
| 5854 | ) |
| 5855 | ) { |
| 5856 | goto ret; /* error */ |
| 5857 | } |
| 5858 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5859 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
| 5860 | continue; |
| 5861 | } |
| 5862 | #endif |
| 5863 | o_addQchr(&dest, ch); |
| 5864 | } /* for (;;) */ |
| 5865 | |
| 5866 | debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data); |
| 5867 | exp_str = expand_string_to_string(dest.data, |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 5868 | do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0, |
| 5869 | do_unbackslash |
| 5870 | ); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5871 | ret: |
| 5872 | debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5873 | o_free_unsafe(&dest); |
| 5874 | return exp_str; |
| 5875 | } |
| 5876 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5877 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5878 | 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] | 5879 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5880 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5881 | arith_t res; |
| 5882 | char *exp_str; |
| 5883 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5884 | math_state.lookupvar = get_local_var_value; |
| 5885 | math_state.setvar = set_local_var_from_halves; |
| 5886 | //math_state.endofname = endofname; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5887 | exp_str = encode_then_expand_string(arg); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5888 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5889 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5890 | if (errmsg_p) |
| 5891 | *errmsg_p = math_state.errmsg; |
| 5892 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5893 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5894 | return res; |
| 5895 | } |
| 5896 | #endif |
| 5897 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5898 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5899 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5900 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5901 | { |
| 5902 | while (1) { |
| 5903 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5904 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5905 | if (end) { |
| 5906 | *size = end - val; |
| 5907 | return val; |
| 5908 | } |
| 5909 | if (*val == '\0') |
| 5910 | return NULL; |
| 5911 | /* Optimization: if "*pat" did not match the start of "string", |
| 5912 | * we know that "tring", "ring" etc will not match too: |
| 5913 | */ |
| 5914 | if (pattern[0] == '*') |
| 5915 | return NULL; |
| 5916 | val++; |
| 5917 | } |
| 5918 | } |
| 5919 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5920 | { |
| 5921 | char *result = NULL; |
| 5922 | unsigned res_len = 0; |
| 5923 | unsigned repl_len = strlen(repl); |
| 5924 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5925 | /* Null pattern never matches, including if "var" is empty */ |
| 5926 | if (!pattern[0]) |
| 5927 | return result; /* NULL, no replaces happened */ |
| 5928 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5929 | while (1) { |
| 5930 | int size; |
| 5931 | char *s = strstr_pattern(val, pattern, &size); |
| 5932 | if (!s) |
| 5933 | break; |
| 5934 | |
| 5935 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5936 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5937 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5938 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5939 | |
| 5940 | val = s + size; |
| 5941 | if (exp_op == '/') |
| 5942 | break; |
| 5943 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5944 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5945 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5946 | strcpy(result + res_len, val); |
| 5947 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5948 | } |
| 5949 | debug_printf_varexp("result:'%s'\n", result); |
| 5950 | return result; |
| 5951 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5952 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5953 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5954 | static int append_str_maybe_ifs_split(o_string *output, int n, |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 5955 | int first_ch, const char *val) |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 5956 | { |
| 5957 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
| 5958 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5959 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 5960 | if (val && val[0]) |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5961 | n = expand_on_ifs(output, n, val); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 5962 | } else { /* quoted "$VAR" */ |
| 5963 | output->has_quoted_part = 1; |
| 5964 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5965 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
| 5966 | if (val && val[0]) |
| 5967 | o_addQstr(output, val); |
| 5968 | } |
| 5969 | return n; |
| 5970 | } |
| 5971 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5972 | /* Helper: |
| 5973 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5974 | */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 5975 | static NOINLINE int expand_one_var(o_string *output, |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 5976 | int n, int first_ch, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5977 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5978 | const char *val; |
| 5979 | char *to_be_freed; |
| 5980 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5981 | char *var; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5982 | char exp_op; |
| 5983 | char exp_save = exp_save; /* for compiler */ |
| 5984 | char *exp_saveptr; /* points to expansion operator */ |
| 5985 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5986 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5987 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5988 | val = NULL; |
| 5989 | to_be_freed = NULL; |
| 5990 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5991 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5992 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5993 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5994 | arg0 = arg[0]; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5995 | arg[0] = (arg0 & 0x7f); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5996 | exp_op = 0; |
| 5997 | |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 5998 | if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5999 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 6000 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 6001 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6002 | ) { |
| 6003 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6004 | var++; |
| 6005 | exp_op = 'L'; |
| 6006 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6007 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6008 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6009 | && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6010 | ) { |
| 6011 | /* ${?:0}, ${#[:]%0} etc */ |
| 6012 | exp_saveptr = var + 1; |
| 6013 | } else { |
| 6014 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 6015 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 6016 | } |
| 6017 | exp_op = exp_save = *exp_saveptr; |
| 6018 | if (exp_op) { |
| 6019 | exp_word = exp_saveptr + 1; |
| 6020 | if (exp_op == ':') { |
| 6021 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6022 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6023 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6024 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6025 | ) { |
| 6026 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 6027 | exp_op = ':'; |
| 6028 | exp_word--; |
| 6029 | } |
| 6030 | } |
| 6031 | *exp_saveptr = '\0'; |
| 6032 | } /* else: it's not an expansion op, but bare ${var} */ |
| 6033 | } |
| 6034 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6035 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6036 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 6037 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6038 | int nn = xatoi_positive(var); |
| 6039 | if (nn < G.global_argc) |
| 6040 | val = G.global_argv[nn]; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6041 | /* else val remains NULL: $N with too big N */ |
| 6042 | } else { |
| 6043 | switch (var[0]) { |
| 6044 | case '$': /* pid */ |
| 6045 | val = utoa(G.root_pid); |
| 6046 | break; |
| 6047 | case '!': /* bg pid */ |
| 6048 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 6049 | break; |
| 6050 | case '?': /* exitcode */ |
| 6051 | val = utoa(G.last_exitcode); |
| 6052 | break; |
| 6053 | case '#': /* argc */ |
| 6054 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 6055 | break; |
| 6056 | default: |
| 6057 | val = get_local_var_value(var); |
| 6058 | } |
| 6059 | } |
| 6060 | |
| 6061 | /* Handle any expansions */ |
| 6062 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6063 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6064 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 6065 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6066 | debug_printf_expand("%s\n", val); |
| 6067 | } else if (exp_op) { |
| 6068 | if (exp_op == '%' || exp_op == '#') { |
| 6069 | /* Standard-mandated substring removal ops: |
| 6070 | * ${parameter%word} - remove smallest suffix pattern |
| 6071 | * ${parameter%%word} - remove largest suffix pattern |
| 6072 | * ${parameter#word} - remove smallest prefix pattern |
| 6073 | * ${parameter##word} - remove largest prefix pattern |
| 6074 | * |
| 6075 | * Word is expanded to produce a glob pattern. |
| 6076 | * Then var's value is matched to it and matching part removed. |
| 6077 | */ |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6078 | //FIXME: ${x#...${...}...} |
| 6079 | //should evaluate inner ${...} even if x is "" and no shrinking of it is possible - |
| 6080 | //inner ${...} may have side effects! |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6081 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6082 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6083 | char *exp_exp_word; |
| 6084 | char *loc; |
| 6085 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 6086 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6087 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6088 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6089 | 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] | 6090 | if (exp_exp_word) |
| 6091 | exp_word = exp_exp_word; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6092 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
| 6093 | /* |
| 6094 | * HACK ALERT. We depend here on the fact that |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6095 | * G.global_argv and results of utoa and get_local_var_value |
| 6096 | * are actually in writable memory: |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6097 | * scan_and_match momentarily stores NULs there. |
| 6098 | */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6099 | t = (char*)val; |
| 6100 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 6101 | 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] | 6102 | free(exp_exp_word); |
| 6103 | if (loc) { /* match was found */ |
| 6104 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6105 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6106 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6107 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6108 | } |
| 6109 | } |
| 6110 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6111 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6112 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6113 | /* It's ${var/[/]pattern[/repl]} thing. |
| 6114 | * Note that in encoded form it has TWO parts: |
| 6115 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6116 | * and if // is used, it is encoded as \: |
| 6117 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6118 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6119 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 6120 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6121 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6122 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6123 | * >az >bz |
| 6124 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 6125 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 6126 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 6127 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6128 | * (note that a*z _pattern_ is never globbed!) |
| 6129 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6130 | char *pattern, *repl, *t; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6131 | pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6132 | if (!pattern) |
| 6133 | pattern = xstrdup(exp_word); |
| 6134 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6135 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6136 | exp_word = p; |
| 6137 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6138 | *p = '\0'; |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6139 | repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6140 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6141 | /* HACK ALERT. We depend here on the fact that |
| 6142 | * G.global_argv and results of utoa and get_local_var_value |
| 6143 | * are actually in writable memory: |
| 6144 | * replace_pattern momentarily stores NULs there. */ |
| 6145 | t = (char*)val; |
| 6146 | to_be_freed = replace_pattern(t, |
| 6147 | pattern, |
| 6148 | (repl ? repl : exp_word), |
| 6149 | exp_op); |
| 6150 | if (to_be_freed) /* at least one replace happened */ |
| 6151 | val = to_be_freed; |
| 6152 | free(pattern); |
| 6153 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6154 | } else { |
| 6155 | /* Empty variable always gives nothing */ |
| 6156 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6157 | /* Just skip "replace" part */ |
| 6158 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6159 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6160 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6161 | } |
| 6162 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6163 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6164 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6165 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6166 | /* It's ${var:N[:M]} bashism. |
| 6167 | * Note that in encoded form it has TWO parts: |
| 6168 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6169 | */ |
| 6170 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6171 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6172 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6173 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6174 | if (errmsg) |
| 6175 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6176 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6177 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6178 | exp_word = p; |
| 6179 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6180 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6181 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6182 | if (errmsg) |
| 6183 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6184 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6185 | if (beg < 0) { |
| 6186 | /* negative beg counts from the end */ |
| 6187 | beg = (arith_t)strlen(val) + beg; |
| 6188 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6189 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6190 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6191 | debug_printf_varexp("from val:'%s'\n", val); |
| 6192 | if (len < 0) { |
| 6193 | /* in bash, len=-n means strlen()-n */ |
| 6194 | len = (arith_t)strlen(val) - beg + len; |
| 6195 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6196 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6197 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6198 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6199 | arith_err: |
| 6200 | val = NULL; |
| 6201 | } else { |
| 6202 | /* Paranoia. What if user entered 9999999999999 |
| 6203 | * which fits in arith_t but not int? */ |
| 6204 | if (len >= INT_MAX) |
| 6205 | len = INT_MAX; |
| 6206 | val = to_be_freed = xstrndup(val + beg, len); |
| 6207 | } |
| 6208 | debug_printf_varexp("val:'%s'\n", val); |
| 6209 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6210 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6211 | val = NULL; |
| 6212 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6213 | } else { /* one of "-=+?" */ |
| 6214 | /* Standard-mandated substitution ops: |
| 6215 | * ${var?word} - indicate error if unset |
| 6216 | * If var is unset, word (or a message indicating it is unset |
| 6217 | * if word is null) is written to standard error |
| 6218 | * and the shell exits with a non-zero exit status. |
| 6219 | * Otherwise, the value of var is substituted. |
| 6220 | * ${var-word} - use default value |
| 6221 | * If var is unset, word is substituted. |
| 6222 | * ${var=word} - assign and use default value |
| 6223 | * If var is unset, word is assigned to var. |
| 6224 | * In all cases, final value of var is substituted. |
| 6225 | * ${var+word} - use alternative value |
| 6226 | * If var is unset, null is substituted. |
| 6227 | * Otherwise, word is substituted. |
| 6228 | * |
| 6229 | * Word is subjected to tilde expansion, parameter expansion, |
| 6230 | * command substitution, and arithmetic expansion. |
| 6231 | * If word is not needed, it is not expanded. |
| 6232 | * |
| 6233 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6234 | * but also treat null var as if it is unset. |
| 6235 | */ |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6236 | /* |
| 6237 | * Word-splitting and squote behavior of bash: |
| 6238 | * $ f() { for i; do echo "|$i|"; done; }; |
| 6239 | * |
| 6240 | * $ x=; f ${x:?'x y' z} |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6241 | * bash: x: x y z #BUG: does not abort, ${} results in empty expansion |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6242 | * $ x=; f "${x:?'x y' z}" |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6243 | * bash: x: x y z # dash prints: dash: x: 'x y' z #BUG: does not abort, ${} results in "" |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6244 | * |
| 6245 | * $ x=; f ${x:='x y' z} |
| 6246 | * |x| |
| 6247 | * |y| |
| 6248 | * |z| |
| 6249 | * $ x=; f "${x:='x y' z}" |
| 6250 | * |'x y' z| |
| 6251 | * |
| 6252 | * $ x=x; f ${x:+'x y' z} |
| 6253 | * |x y| |
| 6254 | * |z| |
| 6255 | * $ x=x; f "${x:+'x y' z}" |
| 6256 | * |'x y' z| |
| 6257 | * |
| 6258 | * $ x=; f ${x:-'x y' z} |
| 6259 | * |x y| |
| 6260 | * |z| |
| 6261 | * $ x=; f "${x:-'x y' z}" |
| 6262 | * |'x y' z| |
| 6263 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6264 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6265 | if (exp_op == '+') |
| 6266 | use_word = !use_word; |
| 6267 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6268 | (exp_save == ':') ? "true" : "false", use_word); |
| 6269 | if (use_word) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 6270 | //FIXME: unquoted ${x:+"b c" d} and ${x:+'b c' d} should expand to two words |
| 6271 | //currently it expands to three. |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6272 | to_be_freed = encode_then_expand_vararg(exp_word, |
| 6273 | /*handle_squotes:*/ !(arg0 & 0x80), |
| 6274 | /*unbackslash:*/ 0 |
| 6275 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6276 | if (to_be_freed) |
| 6277 | exp_word = to_be_freed; |
| 6278 | if (exp_op == '?') { |
| 6279 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6280 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6281 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 6282 | exp_word[0] |
| 6283 | ? exp_word |
| 6284 | : "parameter null or not set" |
| 6285 | /* ash has more specific messages, a-la: */ |
| 6286 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6287 | ); |
| 6288 | //TODO: how interactive bash aborts expansion mid-command? |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6289 | //It aborts the entire line, returns to prompt: |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6290 | // $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO |
| 6291 | // bash: x: x y z |
| 6292 | // $ |
| 6293 | // ("echo YO" is not executed, neither the f function call) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6294 | } else { |
| 6295 | val = exp_word; |
| 6296 | } |
| 6297 | |
| 6298 | if (exp_op == '=') { |
| 6299 | /* ${var=[word]} or ${var:=[word]} */ |
| 6300 | if (isdigit(var[0]) || var[0] == '#') { |
| 6301 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6302 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6303 | val = NULL; |
| 6304 | } else { |
| 6305 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 6306 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6307 | } |
| 6308 | } |
| 6309 | } |
| 6310 | } /* one of "-=+?" */ |
| 6311 | |
| 6312 | *exp_saveptr = exp_save; |
| 6313 | } /* if (exp_op) */ |
| 6314 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6315 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6316 | *pp = p; |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6317 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6318 | n = append_str_maybe_ifs_split(output, n, first_ch, val); |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6319 | |
| 6320 | free(to_be_freed); |
| 6321 | return n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6322 | } |
| 6323 | |
| 6324 | /* Expand all variable references in given string, adding words to list[] |
| 6325 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6326 | * to be filled). This routine is extremely tricky: has to deal with |
| 6327 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6328 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6329 | 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] | 6330 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6331 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6332 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6333 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6334 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6335 | char *p; |
| 6336 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6337 | output->ended_in_ifs = 0; /* did last unquoted expansion end with IFS chars? */ |
| 6338 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6339 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6340 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6341 | debug_print_list("expand_vars_to_list", output, n); |
| 6342 | n = o_save_ptr(output, n); |
| 6343 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6344 | |
| 6345 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6346 | char first_ch; |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6347 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6348 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6349 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6350 | |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6351 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6352 | o_addchr(output, '\0'); |
| 6353 | n = o_save_ptr(output, n); |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6354 | output->ended_in_ifs = 0; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6355 | } |
| 6356 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6357 | o_addblock(output, arg, p - arg); |
| 6358 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6359 | arg = ++p; |
| 6360 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6361 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6362 | /* Fetch special var name (if it is indeed one of them) |
| 6363 | * and quote bit, force the bit on if singleword expansion - |
| 6364 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6365 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6366 | |
| 6367 | /* Is this variable quoted and thus expansion can't be null? |
| 6368 | * "$@" is special. Even if quoted, it can still |
| 6369 | * expand to nothing (not even an empty string), |
| 6370 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6371 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6372 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6373 | |
| 6374 | switch (first_ch & 0x7f) { |
| 6375 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6376 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6377 | case '@': { |
| 6378 | int i; |
| 6379 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6380 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6381 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6382 | 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] | 6383 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6384 | while (G.global_argv[i]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6385 | n = expand_on_ifs(output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6386 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6387 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6388 | /* this argv[] is not empty and not last: |
| 6389 | * put terminating NUL, start new word */ |
| 6390 | o_addchr(output, '\0'); |
| 6391 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6392 | n = o_save_ptr(output, n); |
| 6393 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6394 | } |
| 6395 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6396 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6397 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6398 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6399 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6400 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6401 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6402 | while (1) { |
| 6403 | o_addQstr(output, G.global_argv[i]); |
| 6404 | if (++i >= G.global_argc) |
| 6405 | break; |
| 6406 | o_addchr(output, '\0'); |
| 6407 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6408 | n = o_save_ptr(output, n); |
| 6409 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6410 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6411 | while (1) { |
| 6412 | o_addQstr(output, G.global_argv[i]); |
| 6413 | if (!G.global_argv[++i]) |
| 6414 | break; |
| 6415 | if (G.ifs[0]) |
| 6416 | o_addchr(output, G.ifs[0]); |
| 6417 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6418 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6419 | } |
| 6420 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6421 | } |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6422 | case SPECIAL_VAR_SYMBOL: { |
| 6423 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6424 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6425 | output->has_quoted_part = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6426 | cant_be_null = 0x80; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6427 | arg++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6428 | break; |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6429 | } |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6430 | case SPECIAL_VAR_QUOTED_SVS: |
| 6431 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6432 | /* "^C variable", represents literal ^C char (possible in scripts) */ |
| 6433 | o_addchr(output, SPECIAL_VAR_SYMBOL_CHR); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6434 | arg++; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6435 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6436 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6437 | case '`': { |
| 6438 | /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6439 | o_string subst_result = NULL_O_STRING; |
| 6440 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6441 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6442 | arg++; |
| 6443 | /* Can't just stuff it into output o_string, |
| 6444 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6445 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6446 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6447 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6448 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6449 | 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] | 6450 | n = append_str_maybe_ifs_split(output, n, first_ch, subst_result.data); |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6451 | o_free_unsafe(&subst_result); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6452 | break; |
Denys Vlasenko | 116b50a | 2018-07-19 11:16:53 +0200 | [diff] [blame] | 6453 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6454 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6455 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6456 | case '+': { |
| 6457 | /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6458 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6459 | |
| 6460 | arg++; /* skip '+' */ |
| 6461 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6462 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6463 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6464 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6465 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6466 | o_addstr(output, arith_buf); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6467 | break; |
| 6468 | } |
| 6469 | #endif |
Denys Vlasenko | 8a6a461 | 2018-07-19 12:14:47 +0200 | [diff] [blame] | 6470 | default: |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6471 | /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6472 | n = expand_one_var(output, n, first_ch, arg, &p); |
Denys Vlasenko | 18e8b61 | 2018-07-20 14:24:56 +0200 | [diff] [blame^] | 6473 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6474 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6475 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6476 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6477 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6478 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6479 | *p = SPECIAL_VAR_SYMBOL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6480 | arg = ++p; |
| 6481 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6482 | |
| 6483 | if (arg[0]) { |
Denys Vlasenko | 168579a | 2018-07-19 13:45:54 +0200 | [diff] [blame] | 6484 | if (output->ended_in_ifs) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6485 | o_addchr(output, '\0'); |
| 6486 | n = o_save_ptr(output, n); |
| 6487 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6488 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6489 | /* this part is literal, and it was already pre-quoted |
| 6490 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6491 | o_addstr_with_NUL(output, arg); |
| 6492 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6493 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6494 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6495 | ) { |
| 6496 | n--; |
| 6497 | /* allow to reuse list[n] later without re-growth */ |
| 6498 | output->has_empty_slot = 1; |
| 6499 | } else { |
| 6500 | o_addchr(output, '\0'); |
| 6501 | } |
| 6502 | |
| 6503 | return n; |
| 6504 | } |
| 6505 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6506 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6507 | { |
| 6508 | int n; |
| 6509 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6510 | o_string output = NULL_O_STRING; |
| 6511 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6512 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6513 | |
| 6514 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6515 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6516 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6517 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6518 | } |
| 6519 | debug_print_list("expand_variables", &output, n); |
| 6520 | |
| 6521 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6522 | list = o_finalize_list(&output, n); |
| 6523 | debug_print_strings("expand_variables[1]", list); |
| 6524 | return list; |
| 6525 | } |
| 6526 | |
| 6527 | static char **expand_strvec_to_strvec(char **argv) |
| 6528 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6529 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6530 | } |
| 6531 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6532 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6533 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6534 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6535 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6536 | } |
| 6537 | #endif |
| 6538 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6539 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6540 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6541 | * |
| 6542 | * NB: should NOT do globbing! |
| 6543 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6544 | */ |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6545 | 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] | 6546 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6547 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6548 | const int do_unbackslash = 1; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6549 | const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6550 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6551 | char *argv[2], **list; |
| 6552 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6553 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6554 | /* This is generally an optimization, but it also |
| 6555 | * handles "", which otherwise trips over !list[0] check below. |
| 6556 | * (is this ever happens that we actually get str="" here?) |
| 6557 | */ |
| 6558 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6559 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6560 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6561 | return xstrdup(str); |
| 6562 | } |
| 6563 | |
| 6564 | argv[0] = (char*)str; |
| 6565 | argv[1] = NULL; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6566 | list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | 2e71101 | 2018-07-18 16:02:25 +0200 | [diff] [blame] | 6567 | if (!list[0]) { |
| 6568 | /* Example where it happens: |
| 6569 | * x=; echo ${x:-"$@"} |
| 6570 | */ |
| 6571 | ((char*)list)[0] = '\0'; |
| 6572 | } else { |
| 6573 | if (HUSH_DEBUG) |
| 6574 | if (list[1]) |
| 6575 | bb_error_msg_and_die("BUG in varexp2"); |
| 6576 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6577 | overlapping_strcpy((char*)list, list[0]); |
| 6578 | if (do_unbackslash) |
| 6579 | unbackslash((char*)list); |
| 6580 | } |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6581 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6582 | return (char*)list; |
| 6583 | } |
| 6584 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6585 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6586 | static char* expand_strvec_to_string(char **argv) |
| 6587 | { |
| 6588 | char **list; |
| 6589 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6590 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6591 | /* Convert all NULs to spaces */ |
| 6592 | if (list[0]) { |
| 6593 | int n = 1; |
| 6594 | while (list[n]) { |
| 6595 | if (HUSH_DEBUG) |
| 6596 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6597 | bb_error_msg_and_die("BUG in varexp3"); |
| 6598 | /* bash uses ' ' regardless of $IFS contents */ |
| 6599 | list[n][-1] = ' '; |
| 6600 | n++; |
| 6601 | } |
| 6602 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6603 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6604 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6605 | return (char*)list; |
| 6606 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6607 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6608 | |
| 6609 | static char **expand_assignments(char **argv, int count) |
| 6610 | { |
| 6611 | int i; |
| 6612 | char **p; |
| 6613 | |
| 6614 | G.expanded_assignments = p = NULL; |
| 6615 | /* Expand assignments into one string each */ |
| 6616 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 6617 | p = add_string_to_strings(p, |
| 6618 | expand_string_to_string(argv[i], |
| 6619 | EXP_FLAG_ESC_GLOB_CHARS, |
| 6620 | /*unbackslash:*/ 1 |
| 6621 | ) |
| 6622 | ); |
| 6623 | G.expanded_assignments = p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6624 | } |
| 6625 | G.expanded_assignments = NULL; |
| 6626 | return p; |
| 6627 | } |
| 6628 | |
| 6629 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6630 | static void switch_off_special_sigs(unsigned mask) |
| 6631 | { |
| 6632 | unsigned sig = 0; |
| 6633 | while ((mask >>= 1) != 0) { |
| 6634 | sig++; |
| 6635 | if (!(mask & 1)) |
| 6636 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6637 | #if ENABLE_HUSH_TRAP |
| 6638 | if (G_traps) { |
| 6639 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6640 | /* trap is '', has to remain SIG_IGN */ |
| 6641 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6642 | free(G_traps[sig]); |
| 6643 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6644 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6645 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6646 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6647 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6648 | } |
| 6649 | } |
| 6650 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6651 | #if BB_MMU |
| 6652 | /* never called */ |
| 6653 | void re_execute_shell(char ***to_free, const char *s, |
| 6654 | char *g_argv0, char **g_argv, |
| 6655 | char **builtin_argv) NORETURN; |
| 6656 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6657 | static void reset_traps_to_defaults(void) |
| 6658 | { |
| 6659 | /* This function is always called in a child shell |
| 6660 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6661 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6662 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6663 | unsigned mask; |
| 6664 | |
| 6665 | /* Child shells are not interactive. |
| 6666 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6667 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6668 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6669 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6670 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6671 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6672 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6673 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6674 | /* Switch off special sigs */ |
| 6675 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6676 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6677 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6678 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6679 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6680 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6681 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6682 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6683 | # if ENABLE_HUSH_TRAP |
| 6684 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6685 | return; |
| 6686 | |
| 6687 | /* Reset all sigs to default except ones with empty traps */ |
| 6688 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6689 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6690 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6691 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6692 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6693 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6694 | free(G_traps[sig]); |
| 6695 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6696 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6697 | if (sig == 0) |
| 6698 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6699 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6700 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6701 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | #else /* !BB_MMU */ |
| 6705 | |
| 6706 | static void re_execute_shell(char ***to_free, const char *s, |
| 6707 | char *g_argv0, char **g_argv, |
| 6708 | char **builtin_argv) NORETURN; |
| 6709 | static void re_execute_shell(char ***to_free, const char *s, |
| 6710 | char *g_argv0, char **g_argv, |
| 6711 | char **builtin_argv) |
| 6712 | { |
| 6713 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6714 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6715 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6716 | char *heredoc_argv[4]; |
| 6717 | struct variable *cur; |
| 6718 | # if ENABLE_HUSH_FUNCTIONS |
| 6719 | struct function *funcp; |
| 6720 | # endif |
| 6721 | char **argv, **pp; |
| 6722 | unsigned cnt; |
| 6723 | unsigned long long empty_trap_mask; |
| 6724 | |
| 6725 | if (!g_argv0) { /* heredoc */ |
| 6726 | argv = heredoc_argv; |
| 6727 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6728 | argv[1] = (char *) "-<"; |
| 6729 | argv[2] = (char *) s; |
| 6730 | argv[3] = NULL; |
| 6731 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6732 | goto do_exec; |
| 6733 | } |
| 6734 | |
| 6735 | cnt = 0; |
| 6736 | pp = builtin_argv; |
| 6737 | if (pp) while (*pp++) |
| 6738 | cnt++; |
| 6739 | |
| 6740 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6741 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6742 | int sig; |
| 6743 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6744 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6745 | empty_trap_mask |= 1LL << sig; |
| 6746 | } |
| 6747 | } |
| 6748 | |
| 6749 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6750 | , (unsigned) G.root_pid |
| 6751 | , (unsigned) G.root_ppid |
| 6752 | , (unsigned) G.last_bg_pid |
| 6753 | , (unsigned) G.last_exitcode |
| 6754 | , cnt |
| 6755 | , empty_trap_mask |
| 6756 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6757 | ); |
| 6758 | # undef NOMMU_HACK_FMT |
| 6759 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6760 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6761 | */ |
| 6762 | cnt += 6; |
| 6763 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6764 | if (!cur->flg_export || cur->flg_read_only) |
| 6765 | cnt += 2; |
| 6766 | } |
| 6767 | # if ENABLE_HUSH_FUNCTIONS |
| 6768 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6769 | cnt += 3; |
| 6770 | # endif |
| 6771 | pp = g_argv; |
| 6772 | while (*pp++) |
| 6773 | cnt++; |
| 6774 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6775 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6776 | *pp++ = param_buf; |
| 6777 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6778 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6779 | continue; |
| 6780 | if (cur->flg_read_only) { |
| 6781 | *pp++ = (char *) "-R"; |
| 6782 | *pp++ = cur->varstr; |
| 6783 | } else if (!cur->flg_export) { |
| 6784 | *pp++ = (char *) "-V"; |
| 6785 | *pp++ = cur->varstr; |
| 6786 | } |
| 6787 | } |
| 6788 | # if ENABLE_HUSH_FUNCTIONS |
| 6789 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6790 | *pp++ = (char *) "-F"; |
| 6791 | *pp++ = funcp->name; |
| 6792 | *pp++ = funcp->body_as_string; |
| 6793 | } |
| 6794 | # endif |
| 6795 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6796 | * |
| 6797 | * However, POSIX says that subshells reset signals with traps |
| 6798 | * to SIG_DFL. |
| 6799 | * I tested bash-3.2 and it not only does that with true subshells |
| 6800 | * of the form ( list ), but with any forked children shells. |
| 6801 | * I set trap "echo W" WINCH; and then tried: |
| 6802 | * |
| 6803 | * { echo 1; sleep 20; echo 2; } & |
| 6804 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6805 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6806 | * |
| 6807 | * In all these cases sending SIGWINCH to the child shell |
| 6808 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6809 | * _inside_ group (just before echo 1), it works. |
| 6810 | * |
| 6811 | * 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] | 6812 | */ |
| 6813 | *pp++ = (char *) "-c"; |
| 6814 | *pp++ = (char *) s; |
| 6815 | if (builtin_argv) { |
| 6816 | while (*++builtin_argv) |
| 6817 | *pp++ = *builtin_argv; |
| 6818 | *pp++ = (char *) ""; |
| 6819 | } |
| 6820 | *pp++ = g_argv0; |
| 6821 | while (*g_argv) |
| 6822 | *pp++ = *g_argv++; |
| 6823 | /* *pp = NULL; - is already there */ |
| 6824 | pp = environ; |
| 6825 | |
| 6826 | do_exec: |
| 6827 | 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] | 6828 | /* Don't propagate SIG_IGN to the child */ |
| 6829 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6830 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6831 | execve(bb_busybox_exec_path, argv, pp); |
| 6832 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6833 | if (argv[0][0] == '/') |
| 6834 | execve(argv[0], argv, pp); |
| 6835 | xfunc_error_retval = 127; |
| 6836 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6837 | } |
| 6838 | #endif /* !BB_MMU */ |
| 6839 | |
| 6840 | |
| 6841 | static int run_and_free_list(struct pipe *pi); |
| 6842 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6843 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6844 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6845 | * end_trigger controls how often we stop parsing |
| 6846 | * NUL: parse all, execute, return |
| 6847 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6848 | */ |
| 6849 | 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] | 6850 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6851 | /* Why we need empty flag? |
| 6852 | * An obscure corner case "false; ``; echo $?": |
| 6853 | * empty command in `` should still set $? to 0. |
| 6854 | * But we can't just set $? to 0 at the start, |
| 6855 | * this breaks "false; echo `echo $?`" case. |
| 6856 | */ |
| 6857 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6858 | while (1) { |
| 6859 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6860 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6861 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 6862 | if (end_trigger == ';') { |
| 6863 | G.promptmode = 0; /* PS1 */ |
| 6864 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 6865 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6866 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6867 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6868 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6869 | /* If we are in "big" script |
| 6870 | * (not in `cmd` or something similar)... |
| 6871 | */ |
| 6872 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6873 | /* Discard cached input (rest of line) */ |
| 6874 | int ch = inp->last_char; |
| 6875 | while (ch != EOF && ch != '\n') { |
| 6876 | //bb_error_msg("Discarded:'%c'", ch); |
| 6877 | ch = i_getch(inp); |
| 6878 | } |
| 6879 | /* Force prompt */ |
| 6880 | inp->p = NULL; |
| 6881 | /* This stream isn't empty */ |
| 6882 | empty = 0; |
| 6883 | continue; |
| 6884 | } |
| 6885 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6886 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6887 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6888 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6889 | debug_print_tree(pipe_list, 0); |
| 6890 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6891 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6892 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6893 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6894 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6895 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6896 | } |
| 6897 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6898 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6899 | { |
| 6900 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6901 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 6902 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6903 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6904 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6905 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6906 | } |
| 6907 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6908 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6909 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6910 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6911 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 6912 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6913 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 6914 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6915 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6916 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6917 | } |
| 6918 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6919 | #if ENABLE_HUSH_TICK |
| 6920 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6921 | { |
| 6922 | pid_t pid; |
| 6923 | int channel[2]; |
| 6924 | # if !BB_MMU |
| 6925 | char **to_free = NULL; |
| 6926 | # endif |
| 6927 | |
| 6928 | xpipe(channel); |
| 6929 | pid = BB_MMU ? xfork() : xvfork(); |
| 6930 | if (pid == 0) { /* child */ |
| 6931 | disable_restore_tty_pgrp_on_exit(); |
| 6932 | /* Process substitution is not considered to be usual |
| 6933 | * 'command execution'. |
| 6934 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6935 | */ |
| 6936 | bb_signals(0 |
| 6937 | + (1 << SIGTSTP) |
| 6938 | + (1 << SIGTTIN) |
| 6939 | + (1 << SIGTTOU) |
| 6940 | , SIG_IGN); |
| 6941 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6942 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6943 | xmove_fd(channel[1], 1); |
| 6944 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6945 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6946 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6947 | /* Awful hack for `trap` or $(trap). |
| 6948 | * |
| 6949 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6950 | * contains an example where "trap" is executed in a subshell: |
| 6951 | * |
| 6952 | * save_traps=$(trap) |
| 6953 | * ... |
| 6954 | * eval "$save_traps" |
| 6955 | * |
| 6956 | * Standard does not say that "trap" in subshell shall print |
| 6957 | * parent shell's traps. It only says that its output |
| 6958 | * must have suitable form, but then, in the above example |
| 6959 | * (which is not supposed to be normative), it implies that. |
| 6960 | * |
| 6961 | * bash (and probably other shell) does implement it |
| 6962 | * (traps are reset to defaults, but "trap" still shows them), |
| 6963 | * but as a result, "trap" logic is hopelessly messed up: |
| 6964 | * |
| 6965 | * # trap |
| 6966 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6967 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6968 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6969 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6970 | * trap -- 'echo Ho' SIGWINCH |
| 6971 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6972 | * trap -- 'echo Ho' SIGWINCH |
| 6973 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6974 | * trap -- 'echo Ho' SIGWINCH |
| 6975 | * |
| 6976 | * The rules when to forget and when to not forget traps |
| 6977 | * get really complex and nonsensical. |
| 6978 | * |
| 6979 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6980 | */ |
| 6981 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6982 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6983 | && skip_whitespace(s + 4)[0] == '\0' |
| 6984 | ) { |
| 6985 | static const char *const argv[] = { NULL, NULL }; |
| 6986 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6987 | fflush_all(); /* important */ |
| 6988 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6989 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6990 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6991 | # if BB_MMU |
| 6992 | reset_traps_to_defaults(); |
| 6993 | parse_and_run_string(s); |
| 6994 | _exit(G.last_exitcode); |
| 6995 | # else |
| 6996 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6997 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6998 | * huge=`cat BIG` # was blocking here forever |
| 6999 | * echo OK |
| 7000 | */ |
| 7001 | re_execute_shell(&to_free, |
| 7002 | s, |
| 7003 | G.global_argv[0], |
| 7004 | G.global_argv + 1, |
| 7005 | NULL); |
| 7006 | # endif |
| 7007 | } |
| 7008 | |
| 7009 | /* parent */ |
| 7010 | *pid_p = pid; |
| 7011 | # if ENABLE_HUSH_FAST |
| 7012 | G.count_SIGCHLD++; |
| 7013 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 7014 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 7015 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7016 | # endif |
| 7017 | enable_restore_tty_pgrp_on_exit(); |
| 7018 | # if !BB_MMU |
| 7019 | free(to_free); |
| 7020 | # endif |
| 7021 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 7022 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | /* Return code is exit status of the process that is run. */ |
| 7026 | static int process_command_subs(o_string *dest, const char *s) |
| 7027 | { |
| 7028 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7029 | pid_t pid; |
| 7030 | int status, ch, eol_cnt; |
| 7031 | |
| 7032 | fp = generate_stream_from_string(s, &pid); |
| 7033 | |
| 7034 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7035 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 7036 | while ((ch = getc(fp)) != EOF) { |
| 7037 | if (ch == '\0') |
| 7038 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7039 | if (ch == '\n') { |
| 7040 | eol_cnt++; |
| 7041 | continue; |
| 7042 | } |
| 7043 | while (eol_cnt) { |
| 7044 | o_addchr(dest, '\n'); |
| 7045 | eol_cnt--; |
| 7046 | } |
| 7047 | o_addQchr(dest, ch); |
| 7048 | } |
| 7049 | |
| 7050 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 7051 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7052 | /* We need to extract exitcode. Test case |
| 7053 | * "true; echo `sleep 1; false` $?" |
| 7054 | * should print 1 */ |
| 7055 | safe_waitpid(pid, &status, 0); |
| 7056 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 7057 | return WEXITSTATUS(status); |
| 7058 | } |
| 7059 | #endif /* ENABLE_HUSH_TICK */ |
| 7060 | |
| 7061 | |
| 7062 | static void setup_heredoc(struct redir_struct *redir) |
| 7063 | { |
| 7064 | struct fd_pair pair; |
| 7065 | pid_t pid; |
| 7066 | int len, written; |
| 7067 | /* the _body_ of heredoc (misleading field name) */ |
| 7068 | const char *heredoc = redir->rd_filename; |
| 7069 | char *expanded; |
| 7070 | #if !BB_MMU |
| 7071 | char **to_free; |
| 7072 | #endif |
| 7073 | |
| 7074 | expanded = NULL; |
| 7075 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | b762c78 | 2018-07-17 14:21:38 +0200 | [diff] [blame] | 7076 | expanded = encode_then_expand_string(heredoc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7077 | if (expanded) |
| 7078 | heredoc = expanded; |
| 7079 | } |
| 7080 | len = strlen(heredoc); |
| 7081 | |
| 7082 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 7083 | xpiped_pair(pair); |
| 7084 | xmove_fd(pair.rd, redir->rd_fd); |
| 7085 | |
| 7086 | /* Try writing without forking. Newer kernels have |
| 7087 | * dynamically growing pipes. Must use non-blocking write! */ |
| 7088 | ndelay_on(pair.wr); |
| 7089 | while (1) { |
| 7090 | written = write(pair.wr, heredoc, len); |
| 7091 | if (written <= 0) |
| 7092 | break; |
| 7093 | len -= written; |
| 7094 | if (len == 0) { |
| 7095 | close(pair.wr); |
| 7096 | free(expanded); |
| 7097 | return; |
| 7098 | } |
| 7099 | heredoc += written; |
| 7100 | } |
| 7101 | ndelay_off(pair.wr); |
| 7102 | |
| 7103 | /* Okay, pipe buffer was not big enough */ |
| 7104 | /* Note: we must not create a stray child (bastard? :) |
| 7105 | * for the unsuspecting parent process. Child creates a grandchild |
| 7106 | * and exits before parent execs the process which consumes heredoc |
| 7107 | * (that exec happens after we return from this function) */ |
| 7108 | #if !BB_MMU |
| 7109 | to_free = NULL; |
| 7110 | #endif |
| 7111 | pid = xvfork(); |
| 7112 | if (pid == 0) { |
| 7113 | /* child */ |
| 7114 | disable_restore_tty_pgrp_on_exit(); |
| 7115 | pid = BB_MMU ? xfork() : xvfork(); |
| 7116 | if (pid != 0) |
| 7117 | _exit(0); |
| 7118 | /* grandchild */ |
| 7119 | close(redir->rd_fd); /* read side of the pipe */ |
| 7120 | #if BB_MMU |
| 7121 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 7122 | _exit(0); |
| 7123 | #else |
| 7124 | /* Delegate blocking writes to another process */ |
| 7125 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 7126 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 7127 | #endif |
| 7128 | } |
| 7129 | /* parent */ |
| 7130 | #if ENABLE_HUSH_FAST |
| 7131 | G.count_SIGCHLD++; |
| 7132 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7133 | #endif |
| 7134 | enable_restore_tty_pgrp_on_exit(); |
| 7135 | #if !BB_MMU |
| 7136 | free(to_free); |
| 7137 | #endif |
| 7138 | close(pair.wr); |
| 7139 | free(expanded); |
| 7140 | wait(NULL); /* wait till child has died */ |
| 7141 | } |
| 7142 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7143 | struct squirrel { |
| 7144 | int orig_fd; |
| 7145 | int moved_to; |
| 7146 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 7147 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 7148 | }; |
| 7149 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7150 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 7151 | { |
| 7152 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 7153 | sq[i].orig_fd = orig; |
| 7154 | sq[i].moved_to = moved; |
| 7155 | sq[i+1].orig_fd = -1; /* end marker */ |
| 7156 | return sq; |
| 7157 | } |
| 7158 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7159 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 7160 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7161 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7162 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7163 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7164 | i = 0; |
| 7165 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7166 | /* If we collide with an already moved fd... */ |
| 7167 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7168 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7169 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7170 | if (sq[i].moved_to < 0) /* what? */ |
| 7171 | xfunc_die(); |
| 7172 | return sq; |
| 7173 | } |
| 7174 | if (fd == sq[i].orig_fd) { |
| 7175 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7176 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7177 | return sq; |
| 7178 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7179 | } |
| 7180 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7181 | /* 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] | 7182 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7183 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7184 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7185 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7186 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7187 | } |
| 7188 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7189 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7190 | { |
| 7191 | int i; |
| 7192 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7193 | i = 0; |
| 7194 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7195 | /* If we collide with an already moved fd... */ |
| 7196 | if (fd == sq[i].orig_fd) { |
| 7197 | /* Examples: |
| 7198 | * "echo 3>FILE 3>&- 3>FILE" |
| 7199 | * "echo 3>&- 3>FILE" |
| 7200 | * No need for last redirect to insert |
| 7201 | * another "need to close 3" indicator. |
| 7202 | */ |
| 7203 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7204 | return sq; |
| 7205 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7206 | } |
| 7207 | |
| 7208 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7209 | return append_squirrel(sq, i, fd, -1); |
| 7210 | } |
| 7211 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7212 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7213 | * Move all conflicting internally used fds, |
| 7214 | * and remember them so that we can restore them later. |
| 7215 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7216 | 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] | 7217 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7218 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7219 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7220 | |
| 7221 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7222 | if (fd == G.interactive_fd) { |
| 7223 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7224 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7225 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7226 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7227 | } |
| 7228 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7229 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 7230 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 7231 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7232 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 7233 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7234 | * "fileno(fd) = new_fd" can't be done. |
| 7235 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7236 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7237 | return 0; |
| 7238 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7239 | /* If this one of script's fds? */ |
| 7240 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 7241 | return 1; /* yes. "we closed fd" */ |
| 7242 | |
| 7243 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7244 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7245 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7246 | } |
| 7247 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7248 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7249 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7250 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7251 | int i; |
| 7252 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7253 | if (sq[i].moved_to >= 0) { |
| 7254 | /* We simply die on error */ |
| 7255 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7256 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7257 | } else { |
| 7258 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7259 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7260 | close(sq[i].orig_fd); |
| 7261 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7262 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7263 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7264 | } |
| 7265 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7266 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7267 | |
| 7268 | restore_redirected_FILEs(); |
| 7269 | } |
| 7270 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7271 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7272 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7273 | { |
| 7274 | if (G_interactive_fd) |
| 7275 | close(G_interactive_fd); |
| 7276 | close_all_FILE_list(); |
| 7277 | } |
| 7278 | #endif |
| 7279 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7280 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7281 | { |
| 7282 | int i; |
| 7283 | |
| 7284 | #if ENABLE_HUSH_INTERACTIVE |
| 7285 | if (fd == G.interactive_fd) |
| 7286 | return 1; |
| 7287 | #endif |
| 7288 | /* If this one of script's fds? */ |
| 7289 | if (fd_in_FILEs(fd)) |
| 7290 | return 1; |
| 7291 | |
| 7292 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7293 | if (fd == sq[i].moved_to) |
| 7294 | return 1; |
| 7295 | } |
| 7296 | return 0; |
| 7297 | } |
| 7298 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7299 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7300 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7301 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7302 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7303 | struct redir_struct *redir; |
| 7304 | |
| 7305 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7306 | int newfd; |
| 7307 | int closed; |
| 7308 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7309 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7310 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7311 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7312 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7313 | * of the heredoc */ |
| 7314 | debug_printf_parse("set heredoc '%s'\n", |
| 7315 | redir->rd_filename); |
| 7316 | setup_heredoc(redir); |
| 7317 | continue; |
| 7318 | } |
| 7319 | |
| 7320 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7321 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7322 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7323 | int mode; |
| 7324 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7325 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7326 | /* |
| 7327 | * Examples: |
| 7328 | * "cmd >" (no filename) |
| 7329 | * "cmd > <file" (2nd redirect starts too early) |
| 7330 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7331 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7332 | continue; |
| 7333 | } |
| 7334 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 7335 | p = expand_string_to_string(redir->rd_filename, |
| 7336 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7337 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7338 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7339 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7340 | /* Error message from open_or_warn can be lost |
| 7341 | * if stderr has been redirected, but bash |
| 7342 | * and ash both lose it as well |
| 7343 | * (though zsh doesn't!) |
| 7344 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7345 | return 1; |
| 7346 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7347 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7348 | /* open() gave us precisely the fd we wanted. |
| 7349 | * This means that this fd was not busy |
| 7350 | * (not opened to anywhere). |
| 7351 | * Remember to close it on restore: |
| 7352 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7353 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7354 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7355 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7356 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7357 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7358 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7359 | } |
| 7360 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7361 | if (newfd == redir->rd_fd) |
| 7362 | continue; |
| 7363 | |
| 7364 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7365 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7366 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7367 | |
| 7368 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7369 | if (newfd == REDIRFD_CLOSE) { |
| 7370 | /* "N>&-" means "close me" */ |
| 7371 | if (!closed) { |
| 7372 | /* ^^^ optimization: saving may already |
| 7373 | * have closed it. If not... */ |
| 7374 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7375 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7376 | /* Sometimes we do another close on restore, getting EBADF. |
| 7377 | * Consider "echo 3>FILE 3>&-" |
| 7378 | * first redirect remembers "need to close 3", |
| 7379 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7380 | */ |
| 7381 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7382 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7383 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7384 | //errno = EBADF; |
| 7385 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7386 | newfd = -1; /* same effect as code above */ |
| 7387 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7388 | xdup2(newfd, redir->rd_fd); |
| 7389 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7390 | /* "rd_fd > FILE" */ |
| 7391 | close(newfd); |
| 7392 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7393 | } |
| 7394 | } |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7398 | static char *find_in_path(const char *arg) |
| 7399 | { |
| 7400 | char *ret = NULL; |
| 7401 | const char *PATH = get_local_var_value("PATH"); |
| 7402 | |
| 7403 | if (!PATH) |
| 7404 | return NULL; |
| 7405 | |
| 7406 | while (1) { |
| 7407 | const char *end = strchrnul(PATH, ':'); |
| 7408 | int sz = end - PATH; /* must be int! */ |
| 7409 | |
| 7410 | free(ret); |
| 7411 | if (sz != 0) { |
| 7412 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7413 | } else { |
| 7414 | /* We have xxx::yyyy in $PATH, |
| 7415 | * it means "use current dir" */ |
| 7416 | ret = xstrdup(arg); |
| 7417 | } |
| 7418 | if (access(ret, F_OK) == 0) |
| 7419 | break; |
| 7420 | |
| 7421 | if (*end == '\0') { |
| 7422 | free(ret); |
| 7423 | return NULL; |
| 7424 | } |
| 7425 | PATH = end + 1; |
| 7426 | } |
| 7427 | |
| 7428 | return ret; |
| 7429 | } |
| 7430 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7431 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7432 | const struct built_in_command *x, |
| 7433 | const struct built_in_command *end) |
| 7434 | { |
| 7435 | while (x != end) { |
| 7436 | if (strcmp(name, x->b_cmd) != 0) { |
| 7437 | x++; |
| 7438 | continue; |
| 7439 | } |
| 7440 | debug_printf_exec("found builtin '%s'\n", name); |
| 7441 | return x; |
| 7442 | } |
| 7443 | return NULL; |
| 7444 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7445 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7446 | { |
| 7447 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7448 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7449 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7450 | { |
| 7451 | const struct built_in_command *x = find_builtin1(name); |
| 7452 | if (x) |
| 7453 | return x; |
| 7454 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7455 | } |
| 7456 | |
Denys Vlasenko | 99496dc | 2018-06-26 15:36:58 +0200 | [diff] [blame] | 7457 | static void remove_nested_vars(void) |
| 7458 | { |
| 7459 | struct variable *cur; |
| 7460 | struct variable **cur_pp; |
| 7461 | |
| 7462 | cur_pp = &G.top_var; |
| 7463 | while ((cur = *cur_pp) != NULL) { |
| 7464 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7465 | cur_pp = &cur->next; |
| 7466 | continue; |
| 7467 | } |
| 7468 | /* Unexport */ |
| 7469 | if (cur->flg_export) { |
| 7470 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7471 | bb_unsetenv(cur->varstr); |
| 7472 | } |
| 7473 | /* Remove from global list */ |
| 7474 | *cur_pp = cur->next; |
| 7475 | /* Free */ |
| 7476 | if (!cur->max_len) { |
| 7477 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7478 | free(cur->varstr); |
| 7479 | } |
| 7480 | free(cur); |
| 7481 | } |
| 7482 | } |
| 7483 | |
| 7484 | static void enter_var_nest_level(void) |
| 7485 | { |
| 7486 | G.var_nest_level++; |
| 7487 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7488 | |
| 7489 | /* Try: f() { echo -n .; f; }; f |
| 7490 | * struct variable::var_nest_level is uint16_t, |
| 7491 | * thus limiting recursion to < 2^16. |
| 7492 | * In any case, with 8 Mbyte stack SEGV happens |
| 7493 | * not too long after 2^16 recursions anyway. |
| 7494 | */ |
| 7495 | if (G.var_nest_level > 0xff00) |
| 7496 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7497 | } |
| 7498 | |
| 7499 | static void leave_var_nest_level(void) |
| 7500 | { |
| 7501 | G.var_nest_level--; |
| 7502 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7503 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7504 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7505 | |
| 7506 | remove_nested_vars(); |
| 7507 | } |
| 7508 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7509 | #if ENABLE_HUSH_FUNCTIONS |
| 7510 | static struct function **find_function_slot(const char *name) |
| 7511 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7512 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7513 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7514 | |
| 7515 | while ((funcp = *funcpp) != NULL) { |
| 7516 | if (strcmp(name, funcp->name) == 0) { |
| 7517 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7518 | break; |
| 7519 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7520 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7521 | } |
| 7522 | return funcpp; |
| 7523 | } |
| 7524 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7525 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7526 | { |
| 7527 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7528 | return funcp; |
| 7529 | } |
| 7530 | |
| 7531 | /* Note: takes ownership on name ptr */ |
| 7532 | static struct function *new_function(char *name) |
| 7533 | { |
| 7534 | struct function **funcpp = find_function_slot(name); |
| 7535 | struct function *funcp = *funcpp; |
| 7536 | |
| 7537 | if (funcp != NULL) { |
| 7538 | struct command *cmd = funcp->parent_cmd; |
| 7539 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7540 | if (!cmd) { |
| 7541 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7542 | free(funcp->name); |
| 7543 | /* Note: if !funcp->body, do not free body_as_string! |
| 7544 | * This is a special case of "-F name body" function: |
| 7545 | * body_as_string was not malloced! */ |
| 7546 | if (funcp->body) { |
| 7547 | free_pipe_list(funcp->body); |
| 7548 | # if !BB_MMU |
| 7549 | free(funcp->body_as_string); |
| 7550 | # endif |
| 7551 | } |
| 7552 | } else { |
| 7553 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7554 | cmd->argv[0] = funcp->name; |
| 7555 | cmd->group = funcp->body; |
| 7556 | # if !BB_MMU |
| 7557 | cmd->group_as_string = funcp->body_as_string; |
| 7558 | # endif |
| 7559 | } |
| 7560 | } else { |
| 7561 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7562 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7563 | /*funcp->next = NULL;*/ |
| 7564 | } |
| 7565 | |
| 7566 | funcp->name = name; |
| 7567 | return funcp; |
| 7568 | } |
| 7569 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7570 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7571 | static void unset_func(const char *name) |
| 7572 | { |
| 7573 | struct function **funcpp = find_function_slot(name); |
| 7574 | struct function *funcp = *funcpp; |
| 7575 | |
| 7576 | if (funcp != NULL) { |
| 7577 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7578 | *funcpp = funcp->next; |
| 7579 | /* funcp is unlinked now, deleting it. |
| 7580 | * Note: if !funcp->body, the function was created by |
| 7581 | * "-F name body", do not free ->body_as_string |
| 7582 | * and ->name as they were not malloced. */ |
| 7583 | if (funcp->body) { |
| 7584 | free_pipe_list(funcp->body); |
| 7585 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7586 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7587 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7588 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7589 | } |
| 7590 | free(funcp); |
| 7591 | } |
| 7592 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7593 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7594 | |
| 7595 | # if BB_MMU |
| 7596 | #define exec_function(to_free, funcp, argv) \ |
| 7597 | exec_function(funcp, argv) |
| 7598 | # endif |
| 7599 | static void exec_function(char ***to_free, |
| 7600 | const struct function *funcp, |
| 7601 | char **argv) NORETURN; |
| 7602 | static void exec_function(char ***to_free, |
| 7603 | const struct function *funcp, |
| 7604 | char **argv) |
| 7605 | { |
| 7606 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7607 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7608 | |
| 7609 | argv[0] = G.global_argv[0]; |
| 7610 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7611 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7612 | |
| 7613 | // Example when we are here: "cmd | func" |
| 7614 | // func will run with saved-redirect fds open. |
| 7615 | // $ f() { echo /proc/self/fd/*; } |
| 7616 | // $ true | f |
| 7617 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7618 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7619 | // Same in script: |
| 7620 | // $ . ./SCRIPT |
| 7621 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7622 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7623 | // They are CLOEXEC so external programs won't see them, but |
| 7624 | // for "more correctness" we might want to close those extra fds here: |
| 7625 | //? close_saved_fds_and_FILE_fds(); |
| 7626 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7627 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7628 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7629 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7630 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7631 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7632 | /* On MMU, funcp->body is always non-NULL */ |
| 7633 | n = run_list(funcp->body); |
| 7634 | fflush_all(); |
| 7635 | _exit(n); |
| 7636 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7637 | //? close_saved_fds_and_FILE_fds(); |
| 7638 | |
| 7639 | //TODO: check whether "true | func_with_return" works |
| 7640 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7641 | re_execute_shell(to_free, |
| 7642 | funcp->body_as_string, |
| 7643 | G.global_argv[0], |
| 7644 | argv + 1, |
| 7645 | NULL); |
| 7646 | # endif |
| 7647 | } |
| 7648 | |
| 7649 | static int run_function(const struct function *funcp, char **argv) |
| 7650 | { |
| 7651 | int rc; |
| 7652 | save_arg_t sv; |
| 7653 | smallint sv_flg; |
| 7654 | |
| 7655 | save_and_replace_G_args(&sv, argv); |
| 7656 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7657 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7658 | sv_flg = G_flag_return_in_progress; |
| 7659 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7660 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7661 | /* Make "local" variables properly shadow previous ones */ |
| 7662 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7663 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7664 | |
| 7665 | /* On MMU, funcp->body is always non-NULL */ |
| 7666 | # if !BB_MMU |
| 7667 | if (!funcp->body) { |
| 7668 | /* Function defined by -F */ |
| 7669 | parse_and_run_string(funcp->body_as_string); |
| 7670 | rc = G.last_exitcode; |
| 7671 | } else |
| 7672 | # endif |
| 7673 | { |
| 7674 | rc = run_list(funcp->body); |
| 7675 | } |
| 7676 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7677 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7678 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7679 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7680 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7681 | |
| 7682 | restore_G_args(&sv, argv); |
| 7683 | |
| 7684 | return rc; |
| 7685 | } |
| 7686 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7687 | |
| 7688 | |
| 7689 | #if BB_MMU |
| 7690 | #define exec_builtin(to_free, x, argv) \ |
| 7691 | exec_builtin(x, argv) |
| 7692 | #else |
| 7693 | #define exec_builtin(to_free, x, argv) \ |
| 7694 | exec_builtin(to_free, argv) |
| 7695 | #endif |
| 7696 | static void exec_builtin(char ***to_free, |
| 7697 | const struct built_in_command *x, |
| 7698 | char **argv) NORETURN; |
| 7699 | static void exec_builtin(char ***to_free, |
| 7700 | const struct built_in_command *x, |
| 7701 | char **argv) |
| 7702 | { |
| 7703 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7704 | int rcode; |
| 7705 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7706 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7707 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7708 | fflush_all(); |
| 7709 | _exit(rcode); |
| 7710 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7711 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7712 | /* On NOMMU, we must never block! |
| 7713 | * Example: { sleep 99 | read line; } & echo Ok |
| 7714 | */ |
| 7715 | re_execute_shell(to_free, |
| 7716 | argv[0], |
| 7717 | G.global_argv[0], |
| 7718 | G.global_argv + 1, |
| 7719 | argv); |
| 7720 | #endif |
| 7721 | } |
| 7722 | |
| 7723 | |
| 7724 | static void execvp_or_die(char **argv) NORETURN; |
| 7725 | static void execvp_or_die(char **argv) |
| 7726 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7727 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7728 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7729 | /* Don't propagate SIG_IGN to the child */ |
| 7730 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7731 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7732 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7733 | e = 2; |
| 7734 | if (errno == EACCES) e = 126; |
| 7735 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7736 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7737 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7738 | } |
| 7739 | |
| 7740 | #if ENABLE_HUSH_MODE_X |
| 7741 | static void dump_cmd_in_x_mode(char **argv) |
| 7742 | { |
| 7743 | if (G_x_mode && argv) { |
| 7744 | /* We want to output the line in one write op */ |
| 7745 | char *buf, *p; |
| 7746 | int len; |
| 7747 | int n; |
| 7748 | |
| 7749 | len = 3; |
| 7750 | n = 0; |
| 7751 | while (argv[n]) |
| 7752 | len += strlen(argv[n++]) + 1; |
| 7753 | buf = xmalloc(len); |
| 7754 | buf[0] = '+'; |
| 7755 | p = buf + 1; |
| 7756 | n = 0; |
| 7757 | while (argv[n]) |
| 7758 | p += sprintf(p, " %s", argv[n++]); |
| 7759 | *p++ = '\n'; |
| 7760 | *p = '\0'; |
| 7761 | fputs(buf, stderr); |
| 7762 | free(buf); |
| 7763 | } |
| 7764 | } |
| 7765 | #else |
| 7766 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7767 | #endif |
| 7768 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7769 | #if ENABLE_HUSH_COMMAND |
| 7770 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7771 | { |
| 7772 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7773 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7774 | if (!opt_vV) |
| 7775 | return; |
| 7776 | |
| 7777 | to_free = NULL; |
| 7778 | if (!explanation) { |
| 7779 | char *path = getenv("PATH"); |
| 7780 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7781 | if (!explanation) |
| 7782 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7783 | if (opt_vV != 'V') |
| 7784 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7785 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7786 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7787 | free(to_free); |
| 7788 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7789 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7790 | } |
| 7791 | #else |
| 7792 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7793 | #endif |
| 7794 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7795 | #if BB_MMU |
| 7796 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7797 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7798 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7799 | pseudo_exec(command, argv_expanded) |
| 7800 | #endif |
| 7801 | |
| 7802 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7803 | * Never returns. |
| 7804 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7805 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7806 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7807 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7808 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7809 | char **argv, int assignment_cnt, |
| 7810 | char **argv_expanded) NORETURN; |
| 7811 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7812 | char **argv, int assignment_cnt, |
| 7813 | char **argv_expanded) |
| 7814 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7815 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7816 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7817 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7818 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 7819 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7820 | |
| 7821 | new_env = expand_assignments(argv, assignment_cnt); |
| 7822 | dump_cmd_in_x_mode(new_env); |
| 7823 | |
| 7824 | if (!argv[assignment_cnt]) { |
| 7825 | /* Case when we are here: ... | var=val | ... |
| 7826 | * (note that we do not exit early, i.e., do not optimize out |
| 7827 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7828 | */ |
| 7829 | free_strings(new_env); |
| 7830 | _exit(EXIT_SUCCESS); |
| 7831 | } |
| 7832 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7833 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7834 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7835 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7836 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7837 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7838 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7839 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7840 | set_vars_and_save_old(new_env); |
| 7841 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7842 | |
| 7843 | if (argv_expanded) { |
| 7844 | argv = argv_expanded; |
| 7845 | } else { |
| 7846 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7847 | #if !BB_MMU |
| 7848 | nommu_save->argv = argv; |
| 7849 | #endif |
| 7850 | } |
| 7851 | dump_cmd_in_x_mode(argv); |
| 7852 | |
| 7853 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7854 | if (strchr(argv[0], '/') != NULL) |
| 7855 | goto skip; |
| 7856 | #endif |
| 7857 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7858 | #if ENABLE_HUSH_FUNCTIONS |
| 7859 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7860 | funcp = find_function(argv[0]); |
| 7861 | if (funcp) |
| 7862 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7863 | #endif |
| 7864 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7865 | #if ENABLE_HUSH_COMMAND |
| 7866 | /* "command BAR": run BAR without looking it up among functions |
| 7867 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 7868 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 7869 | */ |
| 7870 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 7871 | char *p; |
| 7872 | |
| 7873 | argv++; |
| 7874 | p = *argv; |
| 7875 | if (p[0] != '-' || !p[1]) |
| 7876 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 7877 | |
| 7878 | for (;;) { |
| 7879 | p++; |
| 7880 | switch (*p) { |
| 7881 | case '\0': |
| 7882 | argv++; |
| 7883 | p = *argv; |
| 7884 | if (p[0] != '-' || !p[1]) |
| 7885 | goto after_opts; |
| 7886 | continue; /* next arg is also -opts, process it too */ |
| 7887 | case 'v': |
| 7888 | case 'V': |
| 7889 | opt_vV = *p; |
| 7890 | continue; |
| 7891 | default: |
| 7892 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 7893 | } |
| 7894 | } |
| 7895 | } |
| 7896 | after_opts: |
| 7897 | # if ENABLE_HUSH_FUNCTIONS |
| 7898 | if (opt_vV && find_function(argv[0])) |
| 7899 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 7900 | # endif |
| 7901 | #endif |
| 7902 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7903 | /* Check if the command matches any of the builtins. |
| 7904 | * Depending on context, this might be redundant. But it's |
| 7905 | * easier to waste a few CPU cycles than it is to figure out |
| 7906 | * if this is one of those cases. |
| 7907 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7908 | /* Why "BB_MMU ? :" difference in logic? - |
| 7909 | * On NOMMU, it is more expensive to re-execute shell |
| 7910 | * just in order to run echo or test builtin. |
| 7911 | * It's better to skip it here and run corresponding |
| 7912 | * non-builtin later. */ |
| 7913 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7914 | if (x) { |
| 7915 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 7916 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7917 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7918 | |
| 7919 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7920 | /* Check if the command matches any busybox applets */ |
| 7921 | { |
| 7922 | int a = find_applet_by_name(argv[0]); |
| 7923 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7924 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7925 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7926 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7927 | /* Do not leak open fds from opened script files etc. |
| 7928 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7929 | * should not show tty fd open. |
| 7930 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7931 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7932 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7933 | //This casuses test failures in |
| 7934 | //redir_children_should_not_see_saved_fd_2.tests |
| 7935 | //redir_children_should_not_see_saved_fd_3.tests |
| 7936 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7937 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7938 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7939 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7940 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7941 | } |
| 7942 | # endif |
| 7943 | /* Re-exec ourselves */ |
| 7944 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7945 | /* Don't propagate SIG_IGN to the child */ |
| 7946 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7947 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7948 | execv(bb_busybox_exec_path, argv); |
| 7949 | /* If they called chroot or otherwise made the binary no longer |
| 7950 | * executable, fall through */ |
| 7951 | } |
| 7952 | } |
| 7953 | #endif |
| 7954 | |
| 7955 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7956 | skip: |
| 7957 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7958 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7959 | execvp_or_die(argv); |
| 7960 | } |
| 7961 | |
| 7962 | /* Called after [v]fork() in run_pipe |
| 7963 | */ |
| 7964 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7965 | struct command *command, |
| 7966 | char **argv_expanded) NORETURN; |
| 7967 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7968 | struct command *command, |
| 7969 | char **argv_expanded) |
| 7970 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 7971 | #if ENABLE_HUSH_FUNCTIONS |
| 7972 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7973 | /* Ignore funcdefs in pipes: |
| 7974 | * true | f() { cmd } |
| 7975 | */ |
| 7976 | _exit(0); |
| 7977 | } |
| 7978 | #endif |
| 7979 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7980 | if (command->argv) { |
| 7981 | pseudo_exec_argv(nommu_save, command->argv, |
| 7982 | command->assignment_cnt, argv_expanded); |
| 7983 | } |
| 7984 | |
| 7985 | if (command->group) { |
| 7986 | /* Cases when we are here: |
| 7987 | * ( list ) |
| 7988 | * { list } & |
| 7989 | * ... | ( list ) | ... |
| 7990 | * ... | { list } | ... |
| 7991 | */ |
| 7992 | #if BB_MMU |
| 7993 | int rcode; |
| 7994 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7995 | reset_traps_to_defaults(); |
| 7996 | rcode = run_list(command->group); |
| 7997 | /* OK to leak memory by not calling free_pipe_list, |
| 7998 | * since this process is about to exit */ |
| 7999 | _exit(rcode); |
| 8000 | #else |
| 8001 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 8002 | command->group_as_string, |
| 8003 | G.global_argv[0], |
| 8004 | G.global_argv + 1, |
| 8005 | NULL); |
| 8006 | #endif |
| 8007 | } |
| 8008 | |
| 8009 | /* Case when we are here: ... | >file */ |
| 8010 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 8011 | _exit(EXIT_SUCCESS); |
| 8012 | } |
| 8013 | |
| 8014 | #if ENABLE_HUSH_JOB |
| 8015 | static const char *get_cmdtext(struct pipe *pi) |
| 8016 | { |
| 8017 | char **argv; |
| 8018 | char *p; |
| 8019 | int len; |
| 8020 | |
| 8021 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 8022 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 8023 | * On subsequent bg argv is trashed, but we won't use it */ |
| 8024 | if (pi->cmdtext) |
| 8025 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8026 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8027 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8028 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8029 | pi->cmdtext = xzalloc(1); |
| 8030 | return pi->cmdtext; |
| 8031 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8032 | len = 0; |
| 8033 | do { |
| 8034 | len += strlen(*argv) + 1; |
| 8035 | } while (*++argv); |
| 8036 | p = xmalloc(len); |
| 8037 | pi->cmdtext = p; |
| 8038 | argv = pi->cmds[0].argv; |
| 8039 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 8040 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8041 | *p++ = ' '; |
| 8042 | } while (*++argv); |
| 8043 | p[-1] = '\0'; |
| 8044 | return pi->cmdtext; |
| 8045 | } |
| 8046 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8047 | static void remove_job_from_table(struct pipe *pi) |
| 8048 | { |
| 8049 | struct pipe *prev_pipe; |
| 8050 | |
| 8051 | if (pi == G.job_list) { |
| 8052 | G.job_list = pi->next; |
| 8053 | } else { |
| 8054 | prev_pipe = G.job_list; |
| 8055 | while (prev_pipe->next != pi) |
| 8056 | prev_pipe = prev_pipe->next; |
| 8057 | prev_pipe->next = pi->next; |
| 8058 | } |
| 8059 | G.last_jobid = 0; |
| 8060 | if (G.job_list) |
| 8061 | G.last_jobid = G.job_list->jobid; |
| 8062 | } |
| 8063 | |
| 8064 | static void delete_finished_job(struct pipe *pi) |
| 8065 | { |
| 8066 | remove_job_from_table(pi); |
| 8067 | free_pipe(pi); |
| 8068 | } |
| 8069 | |
| 8070 | static void clean_up_last_dead_job(void) |
| 8071 | { |
| 8072 | if (G.job_list && !G.job_list->alive_cmds) |
| 8073 | delete_finished_job(G.job_list); |
| 8074 | } |
| 8075 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8076 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8077 | { |
| 8078 | struct pipe *job, **jobp; |
| 8079 | int i; |
| 8080 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8081 | clean_up_last_dead_job(); |
| 8082 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8083 | /* Find the end of the list, and find next job ID to use */ |
| 8084 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8085 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8086 | while ((job = *jobp) != NULL) { |
| 8087 | if (job->jobid > i) |
| 8088 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8089 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8090 | } |
| 8091 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8092 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 8093 | /* Create a new job struct at the end */ |
| 8094 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8095 | job->next = NULL; |
| 8096 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 8097 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 8098 | for (i = 0; i < pi->num_cmds; i++) { |
| 8099 | job->cmds[i].pid = pi->cmds[i].pid; |
| 8100 | /* all other fields are not used and stay zero */ |
| 8101 | } |
| 8102 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 8103 | |
| 8104 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 8105 | 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] | 8106 | G.last_jobid = job->jobid; |
| 8107 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8108 | #endif /* JOB */ |
| 8109 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8110 | static int job_exited_or_stopped(struct pipe *pi) |
| 8111 | { |
| 8112 | int rcode, i; |
| 8113 | |
| 8114 | if (pi->alive_cmds != pi->stopped_cmds) |
| 8115 | return -1; |
| 8116 | |
| 8117 | /* All processes in fg pipe have exited or stopped */ |
| 8118 | rcode = 0; |
| 8119 | i = pi->num_cmds; |
| 8120 | while (--i >= 0) { |
| 8121 | rcode = pi->cmds[i].cmd_exitcode; |
| 8122 | /* usually last process gives overall exitstatus, |
| 8123 | * but with "set -o pipefail", last *failed* process does */ |
| 8124 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 8125 | break; |
| 8126 | } |
| 8127 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8128 | return rcode; |
| 8129 | } |
| 8130 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8131 | 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] | 8132 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8133 | #if ENABLE_HUSH_JOB |
| 8134 | struct pipe *pi; |
| 8135 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8136 | int i, dead; |
| 8137 | |
| 8138 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 8139 | |
| 8140 | #if DEBUG_JOBS |
| 8141 | if (WIFSTOPPED(status)) |
| 8142 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 8143 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 8144 | if (WIFSIGNALED(status)) |
| 8145 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 8146 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 8147 | if (WIFEXITED(status)) |
| 8148 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 8149 | childpid, WEXITSTATUS(status)); |
| 8150 | #endif |
| 8151 | /* Were we asked to wait for a fg pipe? */ |
| 8152 | if (fg_pipe) { |
| 8153 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8154 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8155 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8156 | int rcode; |
| 8157 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8158 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 8159 | if (fg_pipe->cmds[i].pid != childpid) |
| 8160 | continue; |
| 8161 | if (dead) { |
| 8162 | int ex; |
| 8163 | fg_pipe->cmds[i].pid = 0; |
| 8164 | fg_pipe->alive_cmds--; |
| 8165 | ex = WEXITSTATUS(status); |
| 8166 | /* bash prints killer signal's name for *last* |
| 8167 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8168 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8169 | */ |
| 8170 | if (WIFSIGNALED(status)) { |
| 8171 | int sig = WTERMSIG(status); |
| 8172 | if (i == fg_pipe->num_cmds-1) |
| 8173 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 8174 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 8175 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8176 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8177 | * Maybe we need to use sig | 128? */ |
| 8178 | ex = sig + 128; |
| 8179 | } |
| 8180 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8181 | } else { |
| 8182 | fg_pipe->stopped_cmds++; |
| 8183 | } |
| 8184 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8185 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8186 | rcode = job_exited_or_stopped(fg_pipe); |
| 8187 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8188 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8189 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8190 | * and "killall -STOP cat" */ |
| 8191 | if (G_interactive_fd) { |
| 8192 | #if ENABLE_HUSH_JOB |
| 8193 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8194 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8195 | #endif |
| 8196 | return rcode; |
| 8197 | } |
| 8198 | if (fg_pipe->alive_cmds == 0) |
| 8199 | return rcode; |
| 8200 | } |
| 8201 | /* There are still running processes in the fg_pipe */ |
| 8202 | return -1; |
| 8203 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8204 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8205 | } |
| 8206 | |
| 8207 | #if ENABLE_HUSH_JOB |
| 8208 | /* We were asked to wait for bg or orphaned children */ |
| 8209 | /* No need to remember exitcode in this case */ |
| 8210 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8211 | for (i = 0; i < pi->num_cmds; i++) { |
| 8212 | if (pi->cmds[i].pid == childpid) |
| 8213 | goto found_pi_and_prognum; |
| 8214 | } |
| 8215 | } |
| 8216 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8217 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8218 | return -1; /* this wasn't a process from fg_pipe */ |
| 8219 | |
| 8220 | found_pi_and_prognum: |
| 8221 | if (dead) { |
| 8222 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8223 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8224 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8225 | rcode = 128 + WTERMSIG(status); |
| 8226 | pi->cmds[i].cmd_exitcode = rcode; |
| 8227 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8228 | G.last_bg_pid_exitcode = rcode; |
| 8229 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8230 | pi->alive_cmds--; |
| 8231 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8232 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8233 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8234 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8235 | delete_finished_job(pi); |
| 8236 | } else { |
| 8237 | /* |
| 8238 | * bash deletes finished jobs from job table only in interactive mode, |
| 8239 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8240 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8241 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8242 | * We only retain one "dead" job, if it's the single job on the list. |
| 8243 | * This covers most of real-world scenarios where this is useful. |
| 8244 | */ |
| 8245 | if (pi != G.job_list) |
| 8246 | delete_finished_job(pi); |
| 8247 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8248 | } |
| 8249 | } else { |
| 8250 | /* child stopped */ |
| 8251 | pi->stopped_cmds++; |
| 8252 | } |
| 8253 | #endif |
| 8254 | return -1; /* this wasn't a process from fg_pipe */ |
| 8255 | } |
| 8256 | |
| 8257 | /* Check to see if any processes have exited -- if they have, |
| 8258 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8259 | * |
| 8260 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8261 | * Return its exitcode or zero if stopped. |
| 8262 | * |
| 8263 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8264 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8265 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8266 | * or 0 if no children changed status. |
| 8267 | * |
| 8268 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8269 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8270 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8271 | */ |
| 8272 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8273 | { |
| 8274 | int attributes; |
| 8275 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8276 | int rcode = 0; |
| 8277 | |
| 8278 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8279 | |
| 8280 | attributes = WUNTRACED; |
| 8281 | if (fg_pipe == NULL) |
| 8282 | attributes |= WNOHANG; |
| 8283 | |
| 8284 | errno = 0; |
| 8285 | #if ENABLE_HUSH_FAST |
| 8286 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8287 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8288 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8289 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8290 | /* Avoid doing waitpid syscall if possible */ |
| 8291 | if (!G.we_have_children) { |
| 8292 | errno = ECHILD; |
| 8293 | return -1; |
| 8294 | } |
| 8295 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8296 | /* We have children, but they did not exit |
| 8297 | * or stop yet (we saw no SIGCHLD) */ |
| 8298 | return 0; |
| 8299 | } |
| 8300 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8301 | } |
| 8302 | #endif |
| 8303 | |
| 8304 | /* Do we do this right? |
| 8305 | * bash-3.00# sleep 20 | false |
| 8306 | * <ctrl-Z pressed> |
| 8307 | * [3]+ Stopped sleep 20 | false |
| 8308 | * bash-3.00# echo $? |
| 8309 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8310 | * [hush 1.14.0: yes we do it right] |
| 8311 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8312 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8313 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8314 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8315 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8316 | i = G.count_SIGCHLD; |
| 8317 | #endif |
| 8318 | childpid = waitpid(-1, &status, attributes); |
| 8319 | if (childpid <= 0) { |
| 8320 | if (childpid && errno != ECHILD) |
| 8321 | bb_perror_msg("waitpid"); |
| 8322 | #if ENABLE_HUSH_FAST |
| 8323 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8324 | G.we_have_children = (childpid == 0); |
| 8325 | G.handled_SIGCHLD = i; |
| 8326 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8327 | } |
| 8328 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8329 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8330 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8331 | break; |
| 8332 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8333 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8334 | if (rcode >= 0) { |
| 8335 | /* fg_pipe exited or stopped */ |
| 8336 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8337 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8338 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8339 | 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] | 8340 | rcode = WEXITSTATUS(status); |
| 8341 | if (WIFSIGNALED(status)) |
| 8342 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8343 | if (WIFSTOPPED(status)) |
| 8344 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8345 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8346 | rcode++; |
| 8347 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8348 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8349 | /* This wasn't one of our processes, or */ |
| 8350 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8351 | } /* while (waitpid succeeds)... */ |
| 8352 | |
| 8353 | return rcode; |
| 8354 | } |
| 8355 | |
| 8356 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8357 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8358 | { |
| 8359 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8360 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8361 | if (G_saved_tty_pgrp) { |
| 8362 | /* Job finished, move the shell to the foreground */ |
| 8363 | p = getpgrp(); /* our process group id */ |
| 8364 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8365 | tcsetpgrp(G_interactive_fd, p); |
| 8366 | } |
| 8367 | return rcode; |
| 8368 | } |
| 8369 | #endif |
| 8370 | |
| 8371 | /* Start all the jobs, but don't wait for anything to finish. |
| 8372 | * See checkjobs(). |
| 8373 | * |
| 8374 | * Return code is normally -1, when the caller has to wait for children |
| 8375 | * to finish to determine the exit status of the pipe. If the pipe |
| 8376 | * is a simple builtin command, however, the action is done by the |
| 8377 | * time run_pipe returns, and the exit code is provided as the |
| 8378 | * return value. |
| 8379 | * |
| 8380 | * Returns -1 only if started some children. IOW: we have to |
| 8381 | * mask out retvals of builtins etc with 0xff! |
| 8382 | * |
| 8383 | * The only case when we do not need to [v]fork is when the pipe |
| 8384 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8385 | * cmd ; ... { list } ; ... |
| 8386 | * cmd && ... { list } && ... |
| 8387 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8388 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8389 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8390 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8391 | * |
| 8392 | * Cases when we must fork: |
| 8393 | * non-single: cmd | cmd |
| 8394 | * backgrounded: cmd & { list } & |
| 8395 | * subshell: ( list ) [&] |
| 8396 | */ |
| 8397 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | c96bb2c | 2018-06-26 15:43:56 +0200 | [diff] [blame] | 8398 | #define redirect_and_varexp_helper(command, squirrel, argv_expanded) \ |
| 8399 | redirect_and_varexp_helper(command, squirrel) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8400 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8401 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8402 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8403 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8404 | char **argv_expanded) |
| 8405 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8406 | /* Assignments occur before redirects. Try: |
| 8407 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8408 | */ |
| 8409 | |
| 8410 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8411 | dump_cmd_in_x_mode(new_env); |
| 8412 | dump_cmd_in_x_mode(argv_expanded); |
| 8413 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8414 | set_vars_and_save_old(new_env); |
| 8415 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8416 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8417 | * This is perfect for work that comes after exec(). |
| 8418 | * Is it really safe for inline use? Experimentally, |
| 8419 | * things seem to work. */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8420 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8421 | } |
| 8422 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8423 | { |
| 8424 | static const char *const null_ptr = NULL; |
| 8425 | |
| 8426 | int cmd_no; |
| 8427 | int next_infd; |
| 8428 | struct command *command; |
| 8429 | char **argv_expanded; |
| 8430 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8431 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8432 | int rcode; |
| 8433 | |
| 8434 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8435 | debug_enter(); |
| 8436 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8437 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8438 | * Result should be 3 lines: q w e, qwe, q w e |
| 8439 | */ |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8440 | if (G.ifs_whitespace != G.ifs) |
| 8441 | free(G.ifs_whitespace); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8442 | G.ifs = get_local_var_value("IFS"); |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8443 | if (G.ifs) { |
| 8444 | char *p; |
| 8445 | G.ifs_whitespace = (char*)G.ifs; |
| 8446 | p = skip_whitespace(G.ifs); |
| 8447 | if (*p) { |
| 8448 | /* Not all $IFS is whitespace */ |
| 8449 | char *d; |
| 8450 | int len = p - G.ifs; |
| 8451 | p = skip_non_whitespace(p); |
| 8452 | G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */ |
| 8453 | d = mempcpy(G.ifs_whitespace, G.ifs, len); |
| 8454 | while (*p) { |
| 8455 | if (isspace(*p)) |
| 8456 | *d++ = *p; |
| 8457 | p++; |
| 8458 | } |
| 8459 | *d = '\0'; |
| 8460 | } |
| 8461 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8462 | G.ifs = defifs; |
Denys Vlasenko | 9678636 | 2018-04-11 16:02:58 +0200 | [diff] [blame] | 8463 | G.ifs_whitespace = (char*)G.ifs; |
| 8464 | } |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8465 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8466 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8467 | pi->stopped_cmds = 0; |
| 8468 | command = &pi->cmds[0]; |
| 8469 | argv_expanded = NULL; |
| 8470 | |
| 8471 | if (pi->num_cmds != 1 |
| 8472 | || pi->followup == PIPE_BG |
| 8473 | || command->cmd_type == CMD_SUBSHELL |
| 8474 | ) { |
| 8475 | goto must_fork; |
| 8476 | } |
| 8477 | |
| 8478 | pi->alive_cmds = 1; |
| 8479 | |
| 8480 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8481 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8482 | |
| 8483 | if (command->group) { |
| 8484 | #if ENABLE_HUSH_FUNCTIONS |
| 8485 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8486 | /* "executing" func () { list } */ |
| 8487 | struct function *funcp; |
| 8488 | |
| 8489 | funcp = new_function(command->argv[0]); |
| 8490 | /* funcp->name is already set to argv[0] */ |
| 8491 | funcp->body = command->group; |
| 8492 | # if !BB_MMU |
| 8493 | funcp->body_as_string = command->group_as_string; |
| 8494 | command->group_as_string = NULL; |
| 8495 | # endif |
| 8496 | command->group = NULL; |
| 8497 | command->argv[0] = NULL; |
| 8498 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8499 | funcp->parent_cmd = command; |
| 8500 | command->child_func = funcp; |
| 8501 | |
| 8502 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8503 | debug_leave(); |
| 8504 | return EXIT_SUCCESS; |
| 8505 | } |
| 8506 | #endif |
| 8507 | /* { list } */ |
| 8508 | debug_printf("non-subshell group\n"); |
| 8509 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8510 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8511 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8512 | //FIXME: we need to pass squirrel down into run_list() |
| 8513 | //for SH_STANDALONE case, or else this construct: |
| 8514 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8515 | //has no way of closing saved fd#1 for "find", |
| 8516 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8517 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8518 | rcode = run_list(command->group) & 0xff; |
| 8519 | } |
| 8520 | restore_redirects(squirrel); |
| 8521 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8522 | debug_leave(); |
| 8523 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8524 | return rcode; |
| 8525 | } |
| 8526 | |
| 8527 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8528 | { |
| 8529 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8530 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8531 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8532 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8533 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8534 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8535 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8536 | if (G.lineno_var) |
| 8537 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8538 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8539 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8540 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8541 | /* Assignments, but no command. |
| 8542 | * Ensure redirects take effect (that is, create files). |
| 8543 | * Try "a=t >file" |
| 8544 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8545 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8546 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8547 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8548 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8549 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8550 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8551 | /* Set shell variables */ |
| 8552 | if (G_x_mode) |
| 8553 | bb_putchar_stderr('+'); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8554 | i = 0; |
| 8555 | while (i < command->assignment_cnt) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 8556 | char *p = expand_string_to_string(argv[i], |
| 8557 | EXP_FLAG_ESC_GLOB_CHARS, |
| 8558 | /*unbackslash:*/ 1 |
| 8559 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8560 | if (G_x_mode) |
| 8561 | fprintf(stderr, " %s", p); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8562 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8563 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8564 | /* assignment to readonly var / putenv error? */ |
| 8565 | rcode = 1; |
| 8566 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8567 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8568 | } |
| 8569 | if (G_x_mode) |
| 8570 | bb_putchar_stderr('\n'); |
| 8571 | /* Redirect error sets $? to 1. Otherwise, |
| 8572 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8573 | * Else, clear $?: |
| 8574 | * false; q=`exit 2`; echo $? - should print 2 |
| 8575 | * false; x=1; echo $? - should print 0 |
| 8576 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 8577 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8578 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8579 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8580 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8581 | debug_leave(); |
| 8582 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8583 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8584 | } |
| 8585 | |
| 8586 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 8587 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8588 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8589 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8590 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8591 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8592 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8593 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8594 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8595 | if (!argv_expanded[0]) { |
| 8596 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8597 | /* `false` still has to set exitcode 1 */ |
| 8598 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8599 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8600 | } |
| 8601 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8602 | old_vars = NULL; |
| 8603 | sv_shadowed = G.shadowed_vars_pp; |
| 8604 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8605 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8606 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 8607 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 8608 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8609 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8610 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8611 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8612 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8613 | /* |
| 8614 | * Variable assignments are executed, but then "forgotten": |
| 8615 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 8616 | * sleeps, but prints nothing. |
| 8617 | */ |
| 8618 | enter_var_nest_level(); |
| 8619 | G.shadowed_vars_pp = &old_vars; |
| 8620 | rcode = redirect_and_varexp_helper(command, /*squirrel:*/ NULL, argv_expanded); |
| 8621 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8622 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8623 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8624 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8625 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8626 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8627 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8628 | * a=b true; env | grep ^a= |
| 8629 | */ |
| 8630 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8631 | /* Collect all variables "shadowed" by helper |
| 8632 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 8633 | * into old_vars list: |
| 8634 | */ |
| 8635 | G.shadowed_vars_pp = &old_vars; |
| 8636 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8637 | if (rcode == 0) { |
| 8638 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8639 | /* Do not collect *to old_vars list* vars shadowed |
| 8640 | * by e.g. "local VAR" builtin (collect them |
| 8641 | * in the previously nested list instead): |
| 8642 | * don't want them to be restored immediately |
| 8643 | * after "local" completes. |
| 8644 | */ |
| 8645 | G.shadowed_vars_pp = sv_shadowed; |
| 8646 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8647 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8648 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8649 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8650 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8651 | fflush_all(); |
| 8652 | } |
| 8653 | #if ENABLE_HUSH_FUNCTIONS |
| 8654 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8655 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8656 | funcp->name, argv_expanded[1]); |
| 8657 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8658 | /* |
| 8659 | * But do collect *to old_vars list* vars shadowed |
| 8660 | * within function execution. To that end, restore |
| 8661 | * this pointer _after_ function run: |
| 8662 | */ |
| 8663 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8664 | } |
| 8665 | #endif |
| 8666 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8667 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8668 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8669 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8670 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 8671 | goto must_fork; |
| 8672 | |
| 8673 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8674 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 8675 | G.shadowed_vars_pp = &old_vars; |
| 8676 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 8677 | G.shadowed_vars_pp = sv_shadowed; |
| 8678 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8679 | if (rcode == 0) { |
| 8680 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8681 | argv_expanded[0], argv_expanded[1]); |
| 8682 | /* |
| 8683 | * Note: signals (^C) can't interrupt here. |
| 8684 | * We remember them and they will be acted upon |
| 8685 | * after applet returns. |
| 8686 | * This makes applets which can run for a long time |
| 8687 | * and/or wait for user input ineligible for NOFORK: |
| 8688 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8689 | */ |
| 8690 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8691 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 8692 | } else |
| 8693 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8694 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8695 | restore_redirects(squirrel); |
| 8696 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8697 | leave_var_nest_level(); |
| 8698 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8699 | |
| 8700 | /* |
| 8701 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8702 | * with FEATURE_SH_NOFORK=y. |
| 8703 | */ |
| 8704 | if (!funcp) { |
| 8705 | /* It was builtin or nofork. |
| 8706 | * if this would be a real fork/execed program, |
| 8707 | * it should have died if a fatal sig was received. |
| 8708 | * But OTOH, there was no separate process, |
| 8709 | * the sig was sent to _shell_, not to non-existing |
| 8710 | * child. |
| 8711 | * Let's just handle ^C only, this one is obvious: |
| 8712 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8713 | * during builtin/nofork. |
| 8714 | */ |
| 8715 | if (sigismember(&G.pending_set, SIGINT)) |
| 8716 | rcode = 128 + SIGINT; |
| 8717 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8718 | free(argv_expanded); |
| 8719 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8720 | debug_leave(); |
| 8721 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8722 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8723 | } |
| 8724 | |
| 8725 | must_fork: |
| 8726 | /* NB: argv_expanded may already be created, and that |
| 8727 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8728 | * use argv_expanded if it's non-NULL */ |
| 8729 | |
| 8730 | /* Going to fork a child per each pipe member */ |
| 8731 | pi->alive_cmds = 0; |
| 8732 | next_infd = 0; |
| 8733 | |
| 8734 | cmd_no = 0; |
| 8735 | while (cmd_no < pi->num_cmds) { |
| 8736 | struct fd_pair pipefds; |
| 8737 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8738 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8739 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8740 | nommu_save.old_vars = NULL; |
| 8741 | nommu_save.argv = NULL; |
| 8742 | nommu_save.argv_from_re_execing = NULL; |
| 8743 | #endif |
| 8744 | command = &pi->cmds[cmd_no]; |
| 8745 | cmd_no++; |
| 8746 | if (command->argv) { |
| 8747 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8748 | command->argv[0], command->argv[1]); |
| 8749 | } else { |
| 8750 | debug_printf_exec(": pipe member with no argv\n"); |
| 8751 | } |
| 8752 | |
| 8753 | /* pipes are inserted between pairs of commands */ |
| 8754 | pipefds.rd = 0; |
| 8755 | pipefds.wr = 1; |
| 8756 | if (cmd_no < pi->num_cmds) |
| 8757 | xpiped_pair(pipefds); |
| 8758 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8759 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8760 | if (G.lineno_var) |
| 8761 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8762 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8763 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8764 | command->pid = BB_MMU ? fork() : vfork(); |
| 8765 | if (!command->pid) { /* child */ |
| 8766 | #if ENABLE_HUSH_JOB |
| 8767 | disable_restore_tty_pgrp_on_exit(); |
| 8768 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8769 | |
| 8770 | /* Every child adds itself to new process group |
| 8771 | * with pgid == pid_of_first_child_in_pipe */ |
| 8772 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8773 | pid_t pgrp; |
| 8774 | pgrp = pi->pgrp; |
| 8775 | if (pgrp < 0) /* true for 1st process only */ |
| 8776 | pgrp = getpid(); |
| 8777 | if (setpgid(0, pgrp) == 0 |
| 8778 | && pi->followup != PIPE_BG |
| 8779 | && G_saved_tty_pgrp /* we have ctty */ |
| 8780 | ) { |
| 8781 | /* We do it in *every* child, not just first, |
| 8782 | * to avoid races */ |
| 8783 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8784 | } |
| 8785 | } |
| 8786 | #endif |
| 8787 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8788 | /* 1st cmd in backgrounded pipe |
| 8789 | * should have its stdin /dev/null'ed */ |
| 8790 | close(0); |
| 8791 | if (open(bb_dev_null, O_RDONLY)) |
| 8792 | xopen("/", O_RDONLY); |
| 8793 | } else { |
| 8794 | xmove_fd(next_infd, 0); |
| 8795 | } |
| 8796 | xmove_fd(pipefds.wr, 1); |
| 8797 | if (pipefds.rd > 1) |
| 8798 | close(pipefds.rd); |
| 8799 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8800 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8801 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8802 | * of cmd1 goes into pipe. |
| 8803 | */ |
| 8804 | if (setup_redirects(command, NULL)) { |
| 8805 | /* Happens when redir file can't be opened: |
| 8806 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8807 | * FOO |
| 8808 | * hush: can't open '/qwe/rty': No such file or directory |
| 8809 | * BAZ |
| 8810 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8811 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8812 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8813 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8814 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8815 | /* Stores to nommu_save list of env vars putenv'ed |
| 8816 | * (NOMMU, on MMU we don't need that) */ |
| 8817 | /* cast away volatility... */ |
| 8818 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8819 | /* pseudo_exec() does not return */ |
| 8820 | } |
| 8821 | |
| 8822 | /* parent or error */ |
| 8823 | #if ENABLE_HUSH_FAST |
| 8824 | G.count_SIGCHLD++; |
| 8825 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8826 | #endif |
| 8827 | enable_restore_tty_pgrp_on_exit(); |
| 8828 | #if !BB_MMU |
| 8829 | /* Clean up after vforked child */ |
| 8830 | free(nommu_save.argv); |
| 8831 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8832 | G.var_nest_level = sv_var_nest_level; |
| 8833 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8834 | add_vars(nommu_save.old_vars); |
| 8835 | #endif |
| 8836 | free(argv_expanded); |
| 8837 | argv_expanded = NULL; |
| 8838 | if (command->pid < 0) { /* [v]fork failed */ |
| 8839 | /* Clearly indicate, was it fork or vfork */ |
| 8840 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8841 | } else { |
| 8842 | pi->alive_cmds++; |
| 8843 | #if ENABLE_HUSH_JOB |
| 8844 | /* Second and next children need to know pid of first one */ |
| 8845 | if (pi->pgrp < 0) |
| 8846 | pi->pgrp = command->pid; |
| 8847 | #endif |
| 8848 | } |
| 8849 | |
| 8850 | if (cmd_no > 1) |
| 8851 | close(next_infd); |
| 8852 | if (cmd_no < pi->num_cmds) |
| 8853 | close(pipefds.wr); |
| 8854 | /* Pass read (output) pipe end to next iteration */ |
| 8855 | next_infd = pipefds.rd; |
| 8856 | } |
| 8857 | |
| 8858 | if (!pi->alive_cmds) { |
| 8859 | debug_leave(); |
| 8860 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8861 | return 1; |
| 8862 | } |
| 8863 | |
| 8864 | debug_leave(); |
| 8865 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8866 | return -1; |
| 8867 | } |
| 8868 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8869 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8870 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8871 | static int run_list(struct pipe *pi) |
| 8872 | { |
| 8873 | #if ENABLE_HUSH_CASE |
| 8874 | char *case_word = NULL; |
| 8875 | #endif |
| 8876 | #if ENABLE_HUSH_LOOPS |
| 8877 | struct pipe *loop_top = NULL; |
| 8878 | char **for_lcur = NULL; |
| 8879 | char **for_list = NULL; |
| 8880 | #endif |
| 8881 | smallint last_followup; |
| 8882 | smalluint rcode; |
| 8883 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8884 | smalluint cond_code = 0; |
| 8885 | #else |
| 8886 | enum { cond_code = 0 }; |
| 8887 | #endif |
| 8888 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8889 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8890 | smallint last_rword; /* ditto */ |
| 8891 | #endif |
| 8892 | |
| 8893 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8894 | debug_enter(); |
| 8895 | |
| 8896 | #if ENABLE_HUSH_LOOPS |
| 8897 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8898 | { |
| 8899 | struct pipe *cpipe; |
| 8900 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8901 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8902 | continue; |
| 8903 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8904 | if (cpipe->next == NULL) { |
| 8905 | syntax_error("malformed for"); |
| 8906 | debug_leave(); |
| 8907 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8908 | return 1; |
| 8909 | } |
| 8910 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8911 | if (cpipe->next->res_word == RES_DO) |
| 8912 | continue; |
| 8913 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8914 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8915 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8916 | ) { |
| 8917 | syntax_error("malformed for"); |
| 8918 | debug_leave(); |
| 8919 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8920 | return 1; |
| 8921 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8922 | } |
| 8923 | } |
| 8924 | #endif |
| 8925 | |
| 8926 | /* Past this point, all code paths should jump to ret: label |
| 8927 | * in order to return, no direct "return" statements please. |
| 8928 | * This helps to ensure that no memory is leaked. */ |
| 8929 | |
| 8930 | #if ENABLE_HUSH_JOB |
| 8931 | G.run_list_level++; |
| 8932 | #endif |
| 8933 | |
| 8934 | #if HAS_KEYWORDS |
| 8935 | rword = RES_NONE; |
| 8936 | last_rword = RES_XXXX; |
| 8937 | #endif |
| 8938 | last_followup = PIPE_SEQ; |
| 8939 | rcode = G.last_exitcode; |
| 8940 | |
| 8941 | /* Go through list of pipes, (maybe) executing them. */ |
| 8942 | 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] | 8943 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8944 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8945 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8946 | if (G.flag_SIGINT) |
| 8947 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8948 | if (G_flag_return_in_progress == 1) |
| 8949 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8950 | |
| 8951 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8952 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8953 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8954 | |
| 8955 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8956 | if ( |
| 8957 | #if ENABLE_HUSH_IF |
| 8958 | rword == RES_IF || rword == RES_ELIF || |
| 8959 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8960 | pi->followup != PIPE_SEQ |
| 8961 | ) { |
| 8962 | G.errexit_depth++; |
| 8963 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8964 | #if ENABLE_HUSH_LOOPS |
| 8965 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8966 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8967 | ) { |
| 8968 | /* start of a loop: remember where loop starts */ |
| 8969 | loop_top = pi; |
| 8970 | G.depth_of_loop++; |
| 8971 | } |
| 8972 | #endif |
| 8973 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8974 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8975 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8976 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8977 | ) { |
| 8978 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8979 | * and we should not execute CMD */ |
| 8980 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8981 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8982 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8983 | } |
| 8984 | } |
| 8985 | last_followup = pi->followup; |
| 8986 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8987 | #if ENABLE_HUSH_IF |
| 8988 | if (cond_code) { |
| 8989 | if (rword == RES_THEN) { |
| 8990 | /* if false; then ... fi has exitcode 0! */ |
| 8991 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8992 | /* "if <false> THEN cmd": skip cmd */ |
| 8993 | continue; |
| 8994 | } |
| 8995 | } else { |
| 8996 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8997 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8998 | * skip cmd and all following ones */ |
| 8999 | break; |
| 9000 | } |
| 9001 | } |
| 9002 | #endif |
| 9003 | #if ENABLE_HUSH_LOOPS |
| 9004 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 9005 | if (!for_lcur) { |
| 9006 | /* first loop through for */ |
| 9007 | |
| 9008 | static const char encoded_dollar_at[] ALIGN1 = { |
| 9009 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 9010 | }; /* encoded representation of "$@" */ |
| 9011 | static const char *const encoded_dollar_at_argv[] = { |
| 9012 | encoded_dollar_at, NULL |
| 9013 | }; /* argv list with one element: "$@" */ |
| 9014 | char **vals; |
| 9015 | |
| 9016 | vals = (char**)encoded_dollar_at_argv; |
| 9017 | if (pi->next->res_word == RES_IN) { |
| 9018 | /* if no variable values after "in" we skip "for" */ |
| 9019 | if (!pi->next->cmds[0].argv) { |
| 9020 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9021 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 9022 | break; |
| 9023 | } |
| 9024 | vals = pi->next->cmds[0].argv; |
| 9025 | } /* else: "for var; do..." -> assume "$@" list */ |
| 9026 | /* create list of variable values */ |
| 9027 | debug_print_strings("for_list made from", vals); |
| 9028 | for_list = expand_strvec_to_strvec(vals); |
| 9029 | for_lcur = for_list; |
| 9030 | debug_print_strings("for_list", for_list); |
| 9031 | } |
| 9032 | if (!*for_lcur) { |
| 9033 | /* "for" loop is over, clean up */ |
| 9034 | free(for_list); |
| 9035 | for_list = NULL; |
| 9036 | for_lcur = NULL; |
| 9037 | break; |
| 9038 | } |
| 9039 | /* Insert next value from for_lcur */ |
| 9040 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9041 | 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] | 9042 | continue; |
| 9043 | } |
| 9044 | if (rword == RES_IN) { |
| 9045 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 9046 | } |
| 9047 | if (rword == RES_DONE) { |
| 9048 | continue; /* "done" has no cmds too */ |
| 9049 | } |
| 9050 | #endif |
| 9051 | #if ENABLE_HUSH_CASE |
| 9052 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9053 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9054 | case_word = expand_string_to_string(pi->cmds->argv[0], |
| 9055 | EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 9056 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 9057 | //unbackslash(case_word); |
| 9058 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9059 | continue; |
| 9060 | } |
| 9061 | if (rword == RES_MATCH) { |
| 9062 | char **argv; |
| 9063 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9064 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9065 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 9066 | break; |
| 9067 | /* all prev words didn't match, does this one match? */ |
| 9068 | argv = pi->cmds->argv; |
| 9069 | while (*argv) { |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9070 | char *pattern; |
| 9071 | debug_printf_exec("expand_string_to_string('%s')\n", *argv); |
| 9072 | pattern = expand_string_to_string(*argv, |
| 9073 | EXP_FLAG_ESC_GLOB_CHARS, |
| 9074 | /*unbackslash:*/ 0 |
| 9075 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9076 | /* TODO: which FNM_xxx flags to use? */ |
| 9077 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9078 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", |
| 9079 | pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9080 | free(pattern); |
Denys Vlasenko | 3417995 | 2018-04-11 13:47:59 +0200 | [diff] [blame] | 9081 | if (cond_code == 0) { |
| 9082 | /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9083 | free(case_word); |
| 9084 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9085 | break; |
| 9086 | } |
| 9087 | argv++; |
| 9088 | } |
| 9089 | continue; |
| 9090 | } |
| 9091 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9092 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9093 | if (cond_code != 0) |
| 9094 | continue; /* not matched yet, skip this pipe */ |
| 9095 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 9096 | if (rword == RES_ESAC) { |
| 9097 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 9098 | if (case_word) { |
| 9099 | /* "case" did not match anything: still set $? (to 0) */ |
| 9100 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9101 | } |
| 9102 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9103 | #endif |
| 9104 | /* Just pressing <enter> in shell should check for jobs. |
| 9105 | * OTOH, in non-interactive shell this is useless |
| 9106 | * and only leads to extra job checks */ |
| 9107 | if (pi->num_cmds == 0) { |
| 9108 | if (G_interactive_fd) |
| 9109 | goto check_jobs_and_continue; |
| 9110 | continue; |
| 9111 | } |
| 9112 | |
| 9113 | /* After analyzing all keywords and conditions, we decided |
| 9114 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 9115 | * after run_pipe to collect any background children, |
| 9116 | * even if list execution is to be stopped. */ |
| 9117 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9118 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9119 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9120 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9121 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 9122 | if (r != -1) { |
| 9123 | /* We ran a builtin, function, or group. |
| 9124 | * rcode is already known |
| 9125 | * and we don't need to wait for anything. */ |
| 9126 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 9127 | G.last_exitcode = rcode; |
| 9128 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9129 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9130 | /* Was it "break" or "continue"? */ |
| 9131 | if (G.flag_break_continue) { |
| 9132 | smallint fbc = G.flag_break_continue; |
| 9133 | /* We might fall into outer *loop*, |
| 9134 | * don't want to break it too */ |
| 9135 | if (loop_top) { |
| 9136 | G.depth_break_continue--; |
| 9137 | if (G.depth_break_continue == 0) |
| 9138 | G.flag_break_continue = 0; |
| 9139 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 9140 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 9141 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9142 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9143 | break; |
| 9144 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9145 | /* "continue": simulate end of loop */ |
| 9146 | rword = RES_DONE; |
| 9147 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9148 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9149 | #endif |
| 9150 | if (G_flag_return_in_progress == 1) { |
| 9151 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 9152 | break; |
| 9153 | } |
| 9154 | } else if (pi->followup == PIPE_BG) { |
| 9155 | /* What does bash do with attempts to background builtins? */ |
| 9156 | /* even bash 3.2 doesn't do that well with nested bg: |
| 9157 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 9158 | * I'm NOT treating inner &'s as jobs */ |
| 9159 | #if ENABLE_HUSH_JOB |
| 9160 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 9161 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9162 | #endif |
| 9163 | /* Last command's pid goes to $! */ |
| 9164 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 9165 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9166 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 9167 | /* 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] | 9168 | rcode = EXIT_SUCCESS; |
| 9169 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9170 | } else { |
| 9171 | #if ENABLE_HUSH_JOB |
| 9172 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 9173 | /* Waits for completion, then fg's main shell */ |
| 9174 | rcode = checkjobs_and_fg_shell(pi); |
| 9175 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9176 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9177 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 9178 | #endif |
| 9179 | /* This one just waits for completion */ |
| 9180 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 9181 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 9182 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 9183 | G.last_exitcode = rcode; |
| 9184 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9185 | } |
| 9186 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9187 | /* Handle "set -e" */ |
| 9188 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 9189 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 9190 | if (G.errexit_depth == 0) |
| 9191 | hush_exit(rcode); |
| 9192 | } |
| 9193 | G.errexit_depth = sv_errexit_depth; |
| 9194 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9195 | /* Analyze how result affects subsequent commands */ |
| 9196 | #if ENABLE_HUSH_IF |
| 9197 | if (rword == RES_IF || rword == RES_ELIF) |
| 9198 | cond_code = rcode; |
| 9199 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9200 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9201 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9202 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9203 | #if ENABLE_HUSH_LOOPS |
| 9204 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9205 | if (pi->next |
| 9206 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9207 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9208 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9209 | if (rword == RES_WHILE) { |
| 9210 | if (rcode) { |
| 9211 | /* "while false; do...done" - exitcode 0 */ |
| 9212 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9213 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9214 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9215 | } |
| 9216 | } |
| 9217 | if (rword == RES_UNTIL) { |
| 9218 | if (!rcode) { |
| 9219 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9220 | break; |
| 9221 | } |
| 9222 | } |
| 9223 | } |
| 9224 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9225 | } /* for (pi) */ |
| 9226 | |
| 9227 | #if ENABLE_HUSH_JOB |
| 9228 | G.run_list_level--; |
| 9229 | #endif |
| 9230 | #if ENABLE_HUSH_LOOPS |
| 9231 | if (loop_top) |
| 9232 | G.depth_of_loop--; |
| 9233 | free(for_list); |
| 9234 | #endif |
| 9235 | #if ENABLE_HUSH_CASE |
| 9236 | free(case_word); |
| 9237 | #endif |
| 9238 | debug_leave(); |
| 9239 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9240 | return rcode; |
| 9241 | } |
| 9242 | |
| 9243 | /* Select which version we will use */ |
| 9244 | static int run_and_free_list(struct pipe *pi) |
| 9245 | { |
| 9246 | int rcode = 0; |
| 9247 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9248 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9249 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9250 | rcode = run_list(pi); |
| 9251 | } |
| 9252 | /* free_pipe_list has the side effect of clearing memory. |
| 9253 | * In the long run that function can be merged with run_list, |
| 9254 | * but doing that now would hobble the debugging effort. */ |
| 9255 | free_pipe_list(pi); |
| 9256 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9257 | return rcode; |
| 9258 | } |
| 9259 | |
| 9260 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9261 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9262 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9263 | sighandler_t old_handler; |
| 9264 | unsigned sig = 0; |
| 9265 | while ((mask >>= 1) != 0) { |
| 9266 | sig++; |
| 9267 | if (!(mask & 1)) |
| 9268 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9269 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9270 | /* POSIX allows shell to re-enable SIGCHLD |
| 9271 | * even if it was SIG_IGN on entry. |
| 9272 | * Therefore we skip IGN check for it: |
| 9273 | */ |
| 9274 | if (sig == SIGCHLD) |
| 9275 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9276 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9277 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9278 | */ |
| 9279 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9280 | if (old_handler == SIG_IGN) { |
| 9281 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9282 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9283 | #if ENABLE_HUSH_TRAP |
| 9284 | if (!G_traps) |
| 9285 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9286 | free(G_traps[sig]); |
| 9287 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9288 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9289 | } |
| 9290 | } |
| 9291 | } |
| 9292 | |
| 9293 | /* Called a few times only (or even once if "sh -c") */ |
| 9294 | static void install_special_sighandlers(void) |
| 9295 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9296 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9297 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9298 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9299 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9300 | if (G_interactive_fd) { |
| 9301 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9302 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9303 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9304 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9305 | /* Careful, do not re-install handlers we already installed */ |
| 9306 | if (G.special_sig_mask != mask) { |
| 9307 | unsigned diff = mask & ~G.special_sig_mask; |
| 9308 | G.special_sig_mask = mask; |
| 9309 | install_sighandlers(diff); |
| 9310 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9311 | } |
| 9312 | |
| 9313 | #if ENABLE_HUSH_JOB |
| 9314 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9315 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9316 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9317 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9318 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9319 | |
| 9320 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9321 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9322 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9323 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9324 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9325 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9326 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9327 | + (1 << SIGABRT) |
| 9328 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9329 | + (1 << SIGPIPE) |
| 9330 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9331 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9332 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9333 | * we never want to restore pgrp on exit, and this fn is not called |
| 9334 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9335 | /*+ (1 << SIGHUP )*/ |
| 9336 | /*+ (1 << SIGTERM)*/ |
| 9337 | /*+ (1 << SIGINT )*/ |
| 9338 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9339 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9340 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9341 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9342 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9343 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9344 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9345 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9346 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9347 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9348 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9349 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9350 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9351 | break; |
| 9352 | case 'x': |
| 9353 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9354 | break; |
| 9355 | case 'o': |
| 9356 | if (!o_opt) { |
| 9357 | /* "set -+o" without parameter. |
| 9358 | * in bash, set -o produces this output: |
| 9359 | * pipefail off |
| 9360 | * and set +o: |
| 9361 | * set +o pipefail |
| 9362 | * We always use the second form. |
| 9363 | */ |
| 9364 | const char *p = o_opt_strings; |
| 9365 | idx = 0; |
| 9366 | while (*p) { |
| 9367 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9368 | idx++; |
| 9369 | p += strlen(p) + 1; |
| 9370 | } |
| 9371 | break; |
| 9372 | } |
| 9373 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9374 | if (idx >= 0) { |
| 9375 | G.o_opt[idx] = state; |
| 9376 | break; |
| 9377 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9378 | case 'e': |
| 9379 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9380 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9381 | default: |
| 9382 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9383 | } |
| 9384 | return EXIT_SUCCESS; |
| 9385 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9386 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9387 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9388 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9389 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9390 | enum { |
| 9391 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9392 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9393 | }; |
| 9394 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9395 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9396 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9397 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9398 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9399 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9400 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9401 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9402 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9403 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9404 | #if ENABLE_HUSH_FAST |
| 9405 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9406 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9407 | #if !BB_MMU |
| 9408 | G.argv0_for_re_execing = argv[0]; |
| 9409 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9410 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9411 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9412 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9413 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9414 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9415 | shell_ver->flg_export = 1; |
| 9416 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9417 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9418 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9419 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9420 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9421 | /* Create shell local variables from the values |
| 9422 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9423 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9424 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9425 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9426 | if (e) while (*e) { |
| 9427 | char *value = strchr(*e, '='); |
| 9428 | if (value) { /* paranoia */ |
| 9429 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9430 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9431 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9432 | cur_var->max_len = strlen(*e); |
| 9433 | cur_var->flg_export = 1; |
| 9434 | } |
| 9435 | e++; |
| 9436 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9437 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9438 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9439 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9440 | |
| 9441 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9442 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9443 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9444 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9445 | /* Set (but not export) PS1/2 unless already set */ |
| 9446 | if (!get_local_var_value("PS1")) |
| 9447 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9448 | if (!get_local_var_value("PS2")) |
| 9449 | set_local_var_from_halves("PS2", "> "); |
| 9450 | #endif |
| 9451 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9452 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9453 | /* Set (but not export) HOSTNAME unless already set */ |
| 9454 | if (!get_local_var_value("HOSTNAME")) { |
| 9455 | struct utsname uts; |
| 9456 | uname(&uts); |
| 9457 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9458 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9459 | /* bash also exports SHLVL and _, |
| 9460 | * and sets (but doesn't export) the following variables: |
| 9461 | * BASH=/bin/bash |
| 9462 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9463 | * BASH_VERSION='3.2.0(1)-release' |
| 9464 | * HOSTTYPE=i386 |
| 9465 | * MACHTYPE=i386-pc-linux-gnu |
| 9466 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9467 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9468 | * EUID=<NNNNN> |
| 9469 | * UID=<NNNNN> |
| 9470 | * GROUPS=() |
| 9471 | * LINES=<NNN> |
| 9472 | * COLUMNS=<NNN> |
| 9473 | * BASH_ARGC=() |
| 9474 | * BASH_ARGV=() |
| 9475 | * BASH_LINENO=() |
| 9476 | * BASH_SOURCE=() |
| 9477 | * DIRSTACK=() |
| 9478 | * PIPESTATUS=([0]="0") |
| 9479 | * HISTFILE=/<xxx>/.bash_history |
| 9480 | * HISTFILESIZE=500 |
| 9481 | * HISTSIZE=500 |
| 9482 | * MAILCHECK=60 |
| 9483 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9484 | * SHELL=/bin/bash |
| 9485 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9486 | * TERM=dumb |
| 9487 | * OPTERR=1 |
| 9488 | * OPTIND=1 |
| 9489 | * IFS=$' \t\n' |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9490 | * PS4='+ ' |
| 9491 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9492 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9493 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9494 | #if ENABLE_HUSH_LINENO_VAR |
| 9495 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9496 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9497 | set_local_var(p, /*flags*/ 0); |
| 9498 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9499 | } |
| 9500 | #endif |
| 9501 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9502 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9503 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9504 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9505 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9506 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9507 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9508 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9509 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9510 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9511 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9512 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9513 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9514 | * 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] | 9515 | * in order to intercept (more) signals. |
| 9516 | */ |
| 9517 | |
| 9518 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9519 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9520 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9521 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9522 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9523 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9524 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9525 | "<:$:R:V:" |
| 9526 | # if ENABLE_HUSH_FUNCTIONS |
| 9527 | "F:" |
| 9528 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9529 | #endif |
| 9530 | ); |
| 9531 | if (opt <= 0) |
| 9532 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9533 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9534 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9535 | /* Possibilities: |
| 9536 | * sh ... -c 'script' |
| 9537 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9538 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9539 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9540 | * "" needs to be replaced with NULL |
| 9541 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9542 | * Note: the form without ARG0 never happens: |
| 9543 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9544 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9545 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9546 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9547 | G.root_ppid = getppid(); |
| 9548 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9549 | G.global_argv = argv + optind; |
| 9550 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9551 | if (builtin_argc) { |
| 9552 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9553 | const struct built_in_command *x; |
| 9554 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9555 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9556 | x = find_builtin(optarg); |
| 9557 | if (x) { /* paranoia */ |
| 9558 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9559 | G.global_argv += builtin_argc; |
| 9560 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9561 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9562 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9563 | } |
| 9564 | goto final_return; |
| 9565 | } |
| 9566 | if (!G.global_argv[0]) { |
| 9567 | /* -c 'script' (no params): prevent empty $0 */ |
| 9568 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9569 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9570 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9571 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9572 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9573 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9574 | goto final_return; |
| 9575 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9576 | /* Well, we cannot just declare interactiveness, |
| 9577 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9578 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9579 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9580 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9581 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9582 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9583 | case 'l': |
| 9584 | flags |= OPT_login; |
| 9585 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9586 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9587 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9588 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9589 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9590 | case '$': { |
| 9591 | unsigned long long empty_trap_mask; |
| 9592 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9593 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9594 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9595 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9596 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9597 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9598 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9599 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9600 | optarg++; |
| 9601 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9602 | optarg++; |
| 9603 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9604 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9605 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9606 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9607 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9608 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9609 | for (sig = 1; sig < NSIG; sig++) { |
| 9610 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9611 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9612 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9613 | } |
| 9614 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9615 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9616 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9617 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9618 | optarg++; |
| 9619 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9620 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 9621 | # if ENABLE_HUSH_FUNCTIONS |
| 9622 | /* nommu uses re-exec trick for "... | func | ...", |
| 9623 | * should allow "return". |
| 9624 | * This accidentally allows returns in subshells. |
| 9625 | */ |
| 9626 | G_flag_return_in_progress = -1; |
| 9627 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9628 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9629 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9630 | case 'R': |
| 9631 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9632 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9633 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9634 | # if ENABLE_HUSH_FUNCTIONS |
| 9635 | case 'F': { |
| 9636 | struct function *funcp = new_function(optarg); |
| 9637 | /* funcp->name is already set to optarg */ |
| 9638 | /* funcp->body is set to NULL. It's a special case. */ |
| 9639 | funcp->body_as_string = argv[optind]; |
| 9640 | optind++; |
| 9641 | break; |
| 9642 | } |
| 9643 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9644 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9645 | case 'n': |
| 9646 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9647 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9648 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9649 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9650 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9651 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9652 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9653 | " or: sh -c command [args]...\n\n"); |
| 9654 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9655 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9656 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9657 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9658 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9659 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9660 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9661 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9662 | G.global_argc = argc - (optind - 1); |
| 9663 | G.global_argv = argv + (optind - 1); |
| 9664 | G.global_argv[0] = argv[0]; |
| 9665 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9666 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9667 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9668 | G.root_ppid = getppid(); |
| 9669 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9670 | |
| 9671 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9672 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9673 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9674 | debug_printf("sourcing /etc/profile\n"); |
| 9675 | input = fopen_for_read("/etc/profile"); |
| 9676 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9677 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9678 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9679 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9680 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9681 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9682 | /* bash: after sourcing /etc/profile, |
| 9683 | * tries to source (in the given order): |
| 9684 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9685 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9686 | * bash also sources ~/.bash_logout on exit. |
| 9687 | * If called as sh, skips .bash_XXX files. |
| 9688 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9691 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 9692 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9693 | FILE *input; |
| 9694 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9695 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9696 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9697 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9698 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9699 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9700 | G.global_argc--; |
| 9701 | G.global_argv++; |
| 9702 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9703 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9704 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9705 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9706 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9707 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9708 | parse_and_run_file(input); |
| 9709 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9710 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9711 | #endif |
| 9712 | goto final_return; |
| 9713 | } |
| 9714 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9715 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9716 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9717 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9718 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9719 | /* A shell is interactive if the '-i' flag was given, |
| 9720 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9721 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9722 | * no arguments remaining or the -s flag given |
| 9723 | * standard input is a terminal |
| 9724 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9725 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9726 | */ |
| 9727 | #if ENABLE_HUSH_JOB |
| 9728 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9729 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9730 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9731 | if (G_saved_tty_pgrp < 0) |
| 9732 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9733 | |
| 9734 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9735 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9736 | if (G_interactive_fd < 0) { |
| 9737 | /* try to dup to any fd */ |
| 9738 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9739 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9740 | /* give up */ |
| 9741 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9742 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9743 | } |
| 9744 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9745 | // TODO: track & disallow any attempts of user |
| 9746 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9747 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9748 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9749 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9750 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9751 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9752 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9753 | /* If we were run as 'hush &', sleep until we are |
| 9754 | * in the foreground (tty pgrp == our pgrp). |
| 9755 | * If we get started under a job aware app (like bash), |
| 9756 | * make sure we are now in charge so we don't fight over |
| 9757 | * who gets the foreground */ |
| 9758 | while (1) { |
| 9759 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9760 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9761 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9762 | break; |
| 9763 | /* send TTIN to ourself (should stop us) */ |
| 9764 | kill(- shell_pgrp, SIGTTIN); |
| 9765 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9766 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9767 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9768 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9769 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9770 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9771 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9772 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9773 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9774 | /* Put ourselves in our own process group |
| 9775 | * (bash, too, does this only if ctty is available) */ |
| 9776 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9777 | /* Grab control of the terminal */ |
| 9778 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9779 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9780 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9781 | |
| 9782 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9783 | { |
| 9784 | const char *hp = get_local_var_value("HISTFILE"); |
| 9785 | if (!hp) { |
| 9786 | hp = get_local_var_value("HOME"); |
| 9787 | if (hp) |
| 9788 | hp = concat_path_file(hp, ".hush_history"); |
| 9789 | } else { |
| 9790 | hp = xstrdup(hp); |
| 9791 | } |
| 9792 | if (hp) { |
| 9793 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9794 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9795 | } |
| 9796 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9797 | hp = get_local_var_value("HISTFILESIZE"); |
| 9798 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9799 | # endif |
| 9800 | } |
| 9801 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9802 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9803 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9804 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9805 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9806 | /* No job control compiled in, only prompt/line editing */ |
| 9807 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9808 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9809 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9810 | /* try to dup to any fd */ |
Denys Vlasenko | d1a8323 | 2018-06-26 15:50:33 +0200 | [diff] [blame] | 9811 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9812 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9813 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9814 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9815 | } |
| 9816 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9817 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9818 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9819 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9820 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9821 | #else |
| 9822 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9823 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9824 | #endif |
| 9825 | /* bash: |
| 9826 | * if interactive but not a login shell, sources ~/.bashrc |
| 9827 | * (--norc turns this off, --rcfile <file> overrides) |
| 9828 | */ |
| 9829 | |
| 9830 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9831 | /* note: ash and hush share this string */ |
| 9832 | printf("\n\n%s %s\n" |
| 9833 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9834 | "\n", |
| 9835 | bb_banner, |
| 9836 | "hush - the humble shell" |
| 9837 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9838 | } |
| 9839 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9840 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9841 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9842 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9843 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9844 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9845 | |
| 9846 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9847 | /* |
| 9848 | * Built-ins |
| 9849 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9850 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9851 | { |
| 9852 | return 0; |
| 9853 | } |
| 9854 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9855 | #if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 9856 | static int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv)) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9857 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9858 | int argc = string_array_len(argv); |
| 9859 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9860 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9861 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9862 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9863 | static int FAST_FUNC builtin_test(char **argv) |
| 9864 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9865 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9866 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9867 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9868 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9869 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9870 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9871 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9872 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9873 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9874 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9875 | static int FAST_FUNC builtin_printf(char **argv) |
| 9876 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9877 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9878 | } |
| 9879 | #endif |
| 9880 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9881 | #if ENABLE_HUSH_HELP |
| 9882 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9883 | { |
| 9884 | const struct built_in_command *x; |
| 9885 | |
| 9886 | printf( |
| 9887 | "Built-in commands:\n" |
| 9888 | "------------------\n"); |
| 9889 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9890 | if (x->b_descr) |
| 9891 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9892 | } |
| 9893 | return EXIT_SUCCESS; |
| 9894 | } |
| 9895 | #endif |
| 9896 | |
| 9897 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9898 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9899 | { |
| 9900 | show_history(G.line_input_state); |
| 9901 | return EXIT_SUCCESS; |
| 9902 | } |
| 9903 | #endif |
| 9904 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9905 | static char **skip_dash_dash(char **argv) |
| 9906 | { |
| 9907 | argv++; |
| 9908 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9909 | argv++; |
| 9910 | return argv; |
| 9911 | } |
| 9912 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9913 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9914 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9915 | const char *newdir; |
| 9916 | |
| 9917 | argv = skip_dash_dash(argv); |
| 9918 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9919 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9920 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9921 | * bash says "bash: cd: HOME not set" and does nothing |
| 9922 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9923 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9924 | const char *home = get_local_var_value("HOME"); |
| 9925 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9926 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9927 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9928 | /* Mimic bash message exactly */ |
| 9929 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9930 | return EXIT_FAILURE; |
| 9931 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9932 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9933 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9934 | * set it again, but do not export. bash does the same. |
| 9935 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9936 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9937 | return EXIT_SUCCESS; |
| 9938 | } |
| 9939 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9940 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9941 | { |
| 9942 | puts(get_cwd(0)); |
| 9943 | return EXIT_SUCCESS; |
| 9944 | } |
| 9945 | |
| 9946 | static int FAST_FUNC builtin_eval(char **argv) |
| 9947 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9948 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9949 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 9950 | if (!argv[0]) |
| 9951 | return EXIT_SUCCESS; |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9952 | |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 9953 | if (!argv[1]) { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9954 | /* bash: |
| 9955 | * eval "echo Hi; done" ("done" is syntax error): |
| 9956 | * "echo Hi" will not execute too. |
| 9957 | */ |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 9958 | parse_and_run_string(argv[0]); |
| 9959 | } else { |
| 9960 | /* "The eval utility shall construct a command by |
| 9961 | * concatenating arguments together, separating |
| 9962 | * each with a <space> character." |
| 9963 | */ |
| 9964 | char *str, *p; |
| 9965 | unsigned len = 0; |
| 9966 | char **pp = argv; |
| 9967 | do |
| 9968 | len += strlen(*pp) + 1; |
| 9969 | while (*++pp); |
| 9970 | str = p = xmalloc(len); |
| 9971 | pp = argv; |
| 9972 | for (;;) { |
| 9973 | p = stpcpy(p, *pp); |
| 9974 | pp++; |
| 9975 | if (!*pp) |
| 9976 | break; |
| 9977 | *p++ = ' '; |
| 9978 | } |
| 9979 | parse_and_run_string(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9980 | free(str); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9981 | } |
Denys Vlasenko | b0441a7 | 2018-07-15 18:03:56 +0200 | [diff] [blame] | 9982 | return G.last_exitcode; |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9983 | } |
| 9984 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9985 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9986 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9987 | argv = skip_dash_dash(argv); |
| 9988 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9989 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9990 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9991 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9992 | * tty pgrp then, only top-level shell process does that */ |
| 9993 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9994 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9995 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9996 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9997 | * open here. However, they are all CLOEXEC, and execv below |
| 9998 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9999 | * it should show no extra open fds in the "ls" process. |
| 10000 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 10001 | */ |
| 10002 | //close_saved_fds_and_FILE_fds(); |
| 10003 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 10004 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10005 | * 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] | 10006 | * and tcsetpgrp, and this is inherently racy. |
| 10007 | */ |
| 10008 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10009 | } |
| 10010 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10011 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10012 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 10013 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10014 | |
| 10015 | /* interactive bash: |
| 10016 | * # trap "echo EEE" EXIT |
| 10017 | * # exit |
| 10018 | * exit |
| 10019 | * There are stopped jobs. |
| 10020 | * (if there are _stopped_ jobs, running ones don't count) |
| 10021 | * # exit |
| 10022 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 10023 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 10024 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 10025 | * 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] | 10026 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 10027 | |
| 10028 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10029 | argv = skip_dash_dash(argv); |
| 10030 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 10031 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10032 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 10033 | xfunc_error_retval = 255; |
| 10034 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10035 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10036 | } |
| 10037 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10038 | #if ENABLE_HUSH_TYPE |
| 10039 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 10040 | static int FAST_FUNC builtin_type(char **argv) |
| 10041 | { |
| 10042 | int ret = EXIT_SUCCESS; |
| 10043 | |
| 10044 | while (*++argv) { |
| 10045 | const char *type; |
| 10046 | char *path = NULL; |
| 10047 | |
| 10048 | if (0) {} /* make conditional compile easier below */ |
| 10049 | /*else if (find_alias(*argv)) |
| 10050 | type = "an alias";*/ |
| 10051 | #if ENABLE_HUSH_FUNCTIONS |
| 10052 | else if (find_function(*argv)) |
| 10053 | type = "a function"; |
| 10054 | #endif |
| 10055 | else if (find_builtin(*argv)) |
| 10056 | type = "a shell builtin"; |
| 10057 | else if ((path = find_in_path(*argv)) != NULL) |
| 10058 | type = path; |
| 10059 | else { |
| 10060 | bb_error_msg("type: %s: not found", *argv); |
| 10061 | ret = EXIT_FAILURE; |
| 10062 | continue; |
| 10063 | } |
| 10064 | |
| 10065 | printf("%s is %s\n", *argv, type); |
| 10066 | free(path); |
| 10067 | } |
| 10068 | |
| 10069 | return ret; |
| 10070 | } |
| 10071 | #endif |
| 10072 | |
| 10073 | #if ENABLE_HUSH_READ |
| 10074 | /* Interruptibility of read builtin in bash |
| 10075 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 10076 | * |
| 10077 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 10078 | * |
| 10079 | * SIGINT: |
| 10080 | * - terminates non-interactive shell; |
| 10081 | * - interrupts read in interactive shell; |
| 10082 | * if it has non-empty trap: |
| 10083 | * - executes trap and returns to command prompt in interactive shell; |
| 10084 | * - executes trap and returns to read in non-interactive shell; |
| 10085 | * SIGTERM: |
| 10086 | * - is ignored (does not interrupt) read in interactive shell; |
| 10087 | * - terminates non-interactive shell; |
| 10088 | * if it has non-empty trap: |
| 10089 | * - executes trap and returns to read; |
| 10090 | * SIGHUP: |
| 10091 | * - terminates shell (regardless of interactivity); |
| 10092 | * if it has non-empty trap: |
| 10093 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10094 | * SIGCHLD from children: |
| 10095 | * - does not interrupt read regardless of interactivity: |
| 10096 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10097 | */ |
| 10098 | static int FAST_FUNC builtin_read(char **argv) |
| 10099 | { |
| 10100 | const char *r; |
| 10101 | char *opt_n = NULL; |
| 10102 | char *opt_p = NULL; |
| 10103 | char *opt_t = NULL; |
| 10104 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10105 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10106 | const char *ifs; |
| 10107 | int read_flags; |
| 10108 | |
| 10109 | /* "!": do not abort on errors. |
| 10110 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 10111 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10112 | read_flags = getopt32(argv, |
| 10113 | #if BASH_READ_D |
| 10114 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 10115 | #else |
| 10116 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 10117 | #endif |
| 10118 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10119 | if (read_flags == (uint32_t)-1) |
| 10120 | return EXIT_FAILURE; |
| 10121 | argv += optind; |
| 10122 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 10123 | |
| 10124 | again: |
| 10125 | r = shell_builtin_read(set_local_var_from_halves, |
| 10126 | argv, |
| 10127 | ifs, |
| 10128 | read_flags, |
| 10129 | opt_n, |
| 10130 | opt_p, |
| 10131 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 10132 | opt_u, |
| 10133 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10134 | ); |
| 10135 | |
| 10136 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 10137 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 10138 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10139 | goto again; |
| 10140 | } |
| 10141 | |
| 10142 | if ((uintptr_t)r > 1) { |
| 10143 | bb_error_msg("%s", r); |
| 10144 | r = (char*)(uintptr_t)1; |
| 10145 | } |
| 10146 | |
| 10147 | return (uintptr_t)r; |
| 10148 | } |
| 10149 | #endif |
| 10150 | |
| 10151 | #if ENABLE_HUSH_UMASK |
| 10152 | static int FAST_FUNC builtin_umask(char **argv) |
| 10153 | { |
| 10154 | int rc; |
| 10155 | mode_t mask; |
| 10156 | |
| 10157 | rc = 1; |
| 10158 | mask = umask(0); |
| 10159 | argv = skip_dash_dash(argv); |
| 10160 | if (argv[0]) { |
| 10161 | mode_t old_mask = mask; |
| 10162 | |
| 10163 | /* numeric umasks are taken as-is */ |
| 10164 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 10165 | if (!isdigit(argv[0][0])) |
| 10166 | mask ^= 0777; |
| 10167 | mask = bb_parse_mode(argv[0], mask); |
| 10168 | if (!isdigit(argv[0][0])) |
| 10169 | mask ^= 0777; |
| 10170 | if ((unsigned)mask > 0777) { |
| 10171 | mask = old_mask; |
| 10172 | /* bash messages: |
| 10173 | * bash: umask: 'q': invalid symbolic mode operator |
| 10174 | * bash: umask: 999: octal number out of range |
| 10175 | */ |
| 10176 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 10177 | rc = 0; |
| 10178 | } |
| 10179 | } else { |
| 10180 | /* Mimic bash */ |
| 10181 | printf("%04o\n", (unsigned) mask); |
| 10182 | /* fall through and restore mask which we set to 0 */ |
| 10183 | } |
| 10184 | umask(mask); |
| 10185 | |
| 10186 | return !rc; /* rc != 0 - success */ |
| 10187 | } |
| 10188 | #endif |
| 10189 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10190 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10191 | static void print_escaped(const char *s) |
| 10192 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10193 | if (*s == '\'') |
| 10194 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10195 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 10196 | const char *p = strchrnul(s, '\''); |
| 10197 | /* print 'xxxx', possibly just '' */ |
| 10198 | printf("'%.*s'", (int)(p - s), s); |
| 10199 | if (*p == '\0') |
| 10200 | break; |
| 10201 | s = p; |
| 10202 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10203 | /* s points to '; print "'''...'''" */ |
| 10204 | putchar('"'); |
| 10205 | do putchar('\''); while (*++s == '\''); |
| 10206 | putchar('"'); |
| 10207 | } while (*s); |
| 10208 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10209 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10210 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10211 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10212 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10213 | { |
| 10214 | do { |
| 10215 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10216 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10217 | |
| 10218 | /* So far we do not check that name is valid (TODO?) */ |
| 10219 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10220 | if (*name_end == '\0') { |
| 10221 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10222 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10223 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10224 | var = vpp ? *vpp : NULL; |
| 10225 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10226 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10227 | /* export -n NAME (without =VALUE) */ |
| 10228 | if (var) { |
| 10229 | var->flg_export = 0; |
| 10230 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10231 | unsetenv(name); |
| 10232 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10233 | continue; |
| 10234 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10235 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10236 | /* export NAME (without =VALUE) */ |
| 10237 | if (var) { |
| 10238 | var->flg_export = 1; |
| 10239 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10240 | putenv(var->varstr); |
| 10241 | continue; |
| 10242 | } |
| 10243 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10244 | if (flags & SETFLAG_MAKE_RO) { |
| 10245 | /* readonly NAME (without =VALUE) */ |
| 10246 | if (var) { |
| 10247 | var->flg_read_only = 1; |
| 10248 | continue; |
| 10249 | } |
| 10250 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10251 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10252 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10253 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10254 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10255 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10256 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10257 | continue; |
| 10258 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10259 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10260 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10261 | /* Exporting non-existing variable. |
| 10262 | * bash does not put it in environment, |
| 10263 | * but remembers that it is exported, |
| 10264 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10265 | * We just set it to "" and export. |
| 10266 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10267 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10268 | * bash sets the value to "". |
| 10269 | */ |
| 10270 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10271 | * bash remembers NAME and disallows its creation |
| 10272 | * in the future. |
| 10273 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10274 | name = xasprintf("%s=", name); |
| 10275 | } else { |
| 10276 | /* (Un)exporting/making local NAME=VALUE */ |
| 10277 | name = xstrdup(name); |
| 10278 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10279 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10280 | if (set_local_var(name, flags)) |
| 10281 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10282 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10283 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10284 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10285 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10286 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10287 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10288 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10289 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10290 | unsigned opt_unexport; |
| 10291 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10292 | #if ENABLE_HUSH_EXPORT_N |
| 10293 | /* "!": do not abort on errors */ |
| 10294 | opt_unexport = getopt32(argv, "!n"); |
| 10295 | if (opt_unexport == (uint32_t)-1) |
| 10296 | return EXIT_FAILURE; |
| 10297 | argv += optind; |
| 10298 | #else |
| 10299 | opt_unexport = 0; |
| 10300 | argv++; |
| 10301 | #endif |
| 10302 | |
| 10303 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10304 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10305 | if (e) { |
| 10306 | while (*e) { |
| 10307 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10308 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10309 | #else |
| 10310 | /* ash emits: export VAR='VAL' |
| 10311 | * bash: declare -x VAR="VAL" |
| 10312 | * we follow ash example */ |
| 10313 | const char *s = *e++; |
| 10314 | const char *p = strchr(s, '='); |
| 10315 | |
| 10316 | if (!p) /* wtf? take next variable */ |
| 10317 | continue; |
| 10318 | /* export var= */ |
| 10319 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10320 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10321 | putchar('\n'); |
| 10322 | #endif |
| 10323 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10324 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10325 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10326 | return EXIT_SUCCESS; |
| 10327 | } |
| 10328 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10329 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10330 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10331 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10332 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10333 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10334 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10335 | { |
| 10336 | if (G.func_nest_level == 0) { |
| 10337 | bb_error_msg("%s: not in a function", argv[0]); |
| 10338 | return EXIT_FAILURE; /* bash compat */ |
| 10339 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10340 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10341 | /* Since all builtins run in a nested variable level, |
| 10342 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10343 | * after builtin returns. |
| 10344 | */ |
| 10345 | 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] | 10346 | } |
| 10347 | #endif |
| 10348 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10349 | #if ENABLE_HUSH_READONLY |
| 10350 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10351 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10352 | argv++; |
| 10353 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10354 | /* bash: readonly [-p]: list all readonly VARs |
| 10355 | * (-p has no effect in bash) |
| 10356 | */ |
| 10357 | struct variable *e; |
| 10358 | for (e = G.top_var; e; e = e->next) { |
| 10359 | if (e->flg_read_only) { |
| 10360 | //TODO: quote value: readonly VAR='VAL' |
| 10361 | printf("readonly %s\n", e->varstr); |
| 10362 | } |
| 10363 | } |
| 10364 | return EXIT_SUCCESS; |
| 10365 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10366 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10367 | } |
| 10368 | #endif |
| 10369 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10370 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10371 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10372 | static int FAST_FUNC builtin_unset(char **argv) |
| 10373 | { |
| 10374 | int ret; |
| 10375 | unsigned opts; |
| 10376 | |
| 10377 | /* "!": do not abort on errors */ |
| 10378 | /* "+": stop at 1st non-option */ |
| 10379 | opts = getopt32(argv, "!+vf"); |
| 10380 | if (opts == (unsigned)-1) |
| 10381 | return EXIT_FAILURE; |
| 10382 | if (opts == 3) { |
| 10383 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10384 | return EXIT_FAILURE; |
| 10385 | } |
| 10386 | argv += optind; |
| 10387 | |
| 10388 | ret = EXIT_SUCCESS; |
| 10389 | while (*argv) { |
| 10390 | if (!(opts & 2)) { /* not -f */ |
| 10391 | if (unset_local_var(*argv)) { |
| 10392 | /* unset <nonexistent_var> doesn't fail. |
| 10393 | * Error is when one tries to unset RO var. |
| 10394 | * Message was printed by unset_local_var. */ |
| 10395 | ret = EXIT_FAILURE; |
| 10396 | } |
| 10397 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10398 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10399 | else { |
| 10400 | unset_func(*argv); |
| 10401 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10402 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10403 | argv++; |
| 10404 | } |
| 10405 | return ret; |
| 10406 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10407 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10408 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10409 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10410 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10411 | * built-in 'set' handler |
| 10412 | * SUSv3 says: |
| 10413 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10414 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10415 | * set -- [argument...] |
| 10416 | * set -o |
| 10417 | * set +o |
| 10418 | * Implementations shall support the options in both their hyphen and |
| 10419 | * plus-sign forms. These options can also be specified as options to sh. |
| 10420 | * Examples: |
| 10421 | * Write out all variables and their values: set |
| 10422 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10423 | * Turn on the -x and -v options: set -xv |
| 10424 | * Unset all positional parameters: set -- |
| 10425 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10426 | * Set the positional parameters to the expansion of x, even if x expands |
| 10427 | * with a leading '-' or '+': set -- $x |
| 10428 | * |
| 10429 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10430 | */ |
| 10431 | static int FAST_FUNC builtin_set(char **argv) |
| 10432 | { |
| 10433 | int n; |
| 10434 | char **pp, **g_argv; |
| 10435 | char *arg = *++argv; |
| 10436 | |
| 10437 | if (arg == NULL) { |
| 10438 | struct variable *e; |
| 10439 | for (e = G.top_var; e; e = e->next) |
| 10440 | puts(e->varstr); |
| 10441 | return EXIT_SUCCESS; |
| 10442 | } |
| 10443 | |
| 10444 | do { |
| 10445 | if (strcmp(arg, "--") == 0) { |
| 10446 | ++argv; |
| 10447 | goto set_argv; |
| 10448 | } |
| 10449 | if (arg[0] != '+' && arg[0] != '-') |
| 10450 | break; |
| 10451 | for (n = 1; arg[n]; ++n) { |
| 10452 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10453 | goto error; |
| 10454 | if (arg[n] == 'o' && argv[1]) |
| 10455 | argv++; |
| 10456 | } |
| 10457 | } while ((arg = *++argv) != NULL); |
| 10458 | /* Now argv[0] is 1st argument */ |
| 10459 | |
| 10460 | if (arg == NULL) |
| 10461 | return EXIT_SUCCESS; |
| 10462 | set_argv: |
| 10463 | |
| 10464 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10465 | g_argv = G.global_argv; |
| 10466 | if (G.global_args_malloced) { |
| 10467 | pp = g_argv; |
| 10468 | while (*++pp) |
| 10469 | free(*pp); |
| 10470 | g_argv[1] = NULL; |
| 10471 | } else { |
| 10472 | G.global_args_malloced = 1; |
| 10473 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10474 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10475 | g_argv = pp; |
| 10476 | } |
| 10477 | /* This realloc's G.global_argv */ |
| 10478 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10479 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10480 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10481 | |
| 10482 | return EXIT_SUCCESS; |
| 10483 | |
| 10484 | /* Nothing known, so abort */ |
| 10485 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10486 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10487 | return EXIT_FAILURE; |
| 10488 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10489 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10490 | |
| 10491 | static int FAST_FUNC builtin_shift(char **argv) |
| 10492 | { |
| 10493 | int n = 1; |
| 10494 | argv = skip_dash_dash(argv); |
| 10495 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10496 | n = bb_strtou(argv[0], NULL, 10); |
| 10497 | if (errno || n < 0) { |
| 10498 | /* shared string with ash.c */ |
| 10499 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10500 | /* |
| 10501 | * ash aborts in this case. |
| 10502 | * bash prints error message and set $? to 1. |
| 10503 | * Interestingly, for "shift 99999" bash does not |
| 10504 | * print error message, but does set $? to 1 |
| 10505 | * (and does no shifting at all). |
| 10506 | */ |
| 10507 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10508 | } |
| 10509 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10510 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10511 | int m = 1; |
| 10512 | while (m <= n) |
| 10513 | free(G.global_argv[m++]); |
| 10514 | } |
| 10515 | G.global_argc -= n; |
| 10516 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10517 | G.global_argc * sizeof(G.global_argv[0])); |
| 10518 | return EXIT_SUCCESS; |
| 10519 | } |
| 10520 | return EXIT_FAILURE; |
| 10521 | } |
| 10522 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10523 | #if ENABLE_HUSH_GETOPTS |
| 10524 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10525 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10526 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10527 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10528 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10529 | If a required argument is not found, and getopts is not silent, |
| 10530 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10531 | diagnostic message is printed. If getopts is silent, then a |
| 10532 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10533 | character found. |
| 10534 | |
| 10535 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10536 | |
| 10537 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10538 | */ |
| 10539 | char cbuf[2]; |
| 10540 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10541 | int c, n, exitcode, my_opterr; |
| 10542 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10543 | |
| 10544 | optstring = *++argv; |
| 10545 | if (!optstring || !(var = *++argv)) { |
| 10546 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10547 | return EXIT_FAILURE; |
| 10548 | } |
| 10549 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10550 | if (argv[1]) |
| 10551 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10552 | else |
| 10553 | argv = G.global_argv; |
| 10554 | cbuf[1] = '\0'; |
| 10555 | |
| 10556 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10557 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10558 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10559 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10560 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10561 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10562 | |
| 10563 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10564 | /*if (optstring[0] != '+')*/ { |
| 10565 | char *s = alloca(strlen(optstring) + 2); |
| 10566 | sprintf(s, "+%s", optstring); |
| 10567 | optstring = s; |
| 10568 | } |
| 10569 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10570 | /* Naively, now we should just |
| 10571 | * cp = get_local_var_value("OPTIND"); |
| 10572 | * optind = cp ? atoi(cp) : 0; |
| 10573 | * optarg = NULL; |
| 10574 | * opterr = my_opterr; |
| 10575 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10576 | * and be done? Not so fast... |
| 10577 | * Unlike normal getopt() usage in C programs, here |
| 10578 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10579 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10580 | * invocations of "getopts", there will be calls to shell builtins |
| 10581 | * which use getopt() internally. Example: |
| 10582 | * while getopts "abc" RES -a -bc -abc de; do |
| 10583 | * unset -ff func |
| 10584 | * done |
| 10585 | * This would not work correctly: getopt() call inside "unset" |
| 10586 | * modifies internal libc state which is tracking position in |
| 10587 | * multi-option strings ("-abc"). At best, it can skip options |
| 10588 | * or return the same option infinitely. With glibc implementation |
| 10589 | * of getopt(), it would use outright invalid pointers and return |
| 10590 | * garbage even _without_ "unset" mangling internal state. |
| 10591 | * |
| 10592 | * We resort to resetting getopt() state and calling it N times, |
| 10593 | * until we get Nth result (or failure). |
| 10594 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10595 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10596 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10597 | count = 0; |
| 10598 | n = string_array_len(argv); |
| 10599 | do { |
| 10600 | optarg = NULL; |
| 10601 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10602 | c = getopt(n, argv, optstring); |
| 10603 | if (c < 0) |
| 10604 | break; |
| 10605 | count++; |
| 10606 | } while (count <= G.getopt_count); |
| 10607 | |
| 10608 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10609 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10610 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10611 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10612 | |
| 10613 | /* Set OPTARG */ |
| 10614 | /* Always set or unset, never left as-is, even on exit/error: |
| 10615 | * "If no option was found, or if the option that was found |
| 10616 | * does not have an option-argument, OPTARG shall be unset." |
| 10617 | */ |
| 10618 | cp = optarg; |
| 10619 | if (c == '?') { |
| 10620 | /* If ":optstring" and unknown option is seen, |
| 10621 | * it is stored to OPTARG. |
| 10622 | */ |
| 10623 | if (optstring[1] == ':') { |
| 10624 | cbuf[0] = optopt; |
| 10625 | cp = cbuf; |
| 10626 | } |
| 10627 | } |
| 10628 | if (cp) |
| 10629 | set_local_var_from_halves("OPTARG", cp); |
| 10630 | else |
| 10631 | unset_local_var("OPTARG"); |
| 10632 | |
| 10633 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10634 | exitcode = EXIT_SUCCESS; |
| 10635 | if (c < 0) { /* -1: end of options */ |
| 10636 | exitcode = EXIT_FAILURE; |
| 10637 | c = '?'; |
| 10638 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10639 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10640 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10641 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10642 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10643 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10644 | return exitcode; |
| 10645 | } |
| 10646 | #endif |
| 10647 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10648 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10649 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10650 | char *arg_path, *filename; |
| 10651 | FILE *input; |
| 10652 | save_arg_t sv; |
| 10653 | char *args_need_save; |
| 10654 | #if ENABLE_HUSH_FUNCTIONS |
| 10655 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10656 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10657 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10658 | argv = skip_dash_dash(argv); |
| 10659 | filename = argv[0]; |
| 10660 | if (!filename) { |
| 10661 | /* bash says: "bash: .: filename argument required" */ |
| 10662 | return 2; /* bash compat */ |
| 10663 | } |
| 10664 | arg_path = NULL; |
| 10665 | if (!strchr(filename, '/')) { |
| 10666 | arg_path = find_in_path(filename); |
| 10667 | if (arg_path) |
| 10668 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10669 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10670 | errno = ENOENT; |
| 10671 | bb_simple_perror_msg(filename); |
| 10672 | return EXIT_FAILURE; |
| 10673 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10674 | } |
| 10675 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10676 | free(arg_path); |
| 10677 | if (!input) { |
| 10678 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10679 | /* POSIX: non-interactive shell should abort here, |
| 10680 | * not merely fail. So far no one complained :) |
| 10681 | */ |
| 10682 | return EXIT_FAILURE; |
| 10683 | } |
| 10684 | |
| 10685 | #if ENABLE_HUSH_FUNCTIONS |
| 10686 | sv_flg = G_flag_return_in_progress; |
| 10687 | /* "we are inside sourced file, ok to use return" */ |
| 10688 | G_flag_return_in_progress = -1; |
| 10689 | #endif |
| 10690 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10691 | if (args_need_save) |
| 10692 | save_and_replace_G_args(&sv, argv); |
| 10693 | |
| 10694 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10695 | G.last_exitcode = 0; |
| 10696 | parse_and_run_file(input); |
| 10697 | fclose_and_forget(input); |
| 10698 | |
| 10699 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10700 | restore_G_args(&sv, argv); |
| 10701 | #if ENABLE_HUSH_FUNCTIONS |
| 10702 | G_flag_return_in_progress = sv_flg; |
| 10703 | #endif |
| 10704 | |
| 10705 | return G.last_exitcode; |
| 10706 | } |
| 10707 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10708 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10709 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10710 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10711 | int sig; |
| 10712 | char *new_cmd; |
| 10713 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10714 | if (!G_traps) |
| 10715 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10716 | |
| 10717 | argv++; |
| 10718 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10719 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10720 | /* No args: print all trapped */ |
| 10721 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10722 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10723 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10724 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10725 | /* note: bash adds "SIG", but only if invoked |
| 10726 | * as "bash". If called as "sh", or if set -o posix, |
| 10727 | * then it prints short signal names. |
| 10728 | * We are printing short names: */ |
| 10729 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10730 | } |
| 10731 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10732 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10733 | return EXIT_SUCCESS; |
| 10734 | } |
| 10735 | |
| 10736 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10737 | /* If first arg is a number: reset all specified signals */ |
| 10738 | sig = bb_strtou(*argv, NULL, 10); |
| 10739 | if (errno == 0) { |
| 10740 | int ret; |
| 10741 | process_sig_list: |
| 10742 | ret = EXIT_SUCCESS; |
| 10743 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10744 | sighandler_t handler; |
| 10745 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10746 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10747 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10748 | ret = EXIT_FAILURE; |
| 10749 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10750 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10751 | continue; |
| 10752 | } |
| 10753 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10754 | free(G_traps[sig]); |
| 10755 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10756 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10757 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10758 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10759 | |
| 10760 | /* There is no signal for 0 (EXIT) */ |
| 10761 | if (sig == 0) |
| 10762 | continue; |
| 10763 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10764 | if (new_cmd) |
| 10765 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10766 | else |
| 10767 | /* We are removing trap handler */ |
| 10768 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10769 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10770 | } |
| 10771 | return ret; |
| 10772 | } |
| 10773 | |
| 10774 | if (!argv[1]) { /* no second arg */ |
| 10775 | bb_error_msg("trap: invalid arguments"); |
| 10776 | return EXIT_FAILURE; |
| 10777 | } |
| 10778 | |
| 10779 | /* First arg is "-": reset all specified to default */ |
| 10780 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10781 | /* Everything else: set arg as signal handler |
| 10782 | * (includes "" case, which ignores signal) */ |
| 10783 | if (argv[0][0] == '-') { |
| 10784 | if (argv[0][1] == '\0') { /* "-" */ |
| 10785 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10786 | goto reset_traps; |
| 10787 | } |
| 10788 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10789 | argv++; |
| 10790 | } |
| 10791 | /* else: "-something", no special meaning */ |
| 10792 | } |
| 10793 | new_cmd = *argv; |
| 10794 | reset_traps: |
| 10795 | argv++; |
| 10796 | goto process_sig_list; |
| 10797 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10798 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10799 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10800 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10801 | static struct pipe *parse_jobspec(const char *str) |
| 10802 | { |
| 10803 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10804 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10805 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10806 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10807 | if (str[0] != '%' |
| 10808 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10809 | ) { |
| 10810 | bb_error_msg("bad argument '%s'", str); |
| 10811 | return NULL; |
| 10812 | } |
| 10813 | /* It is "%%", "%+" or "%" - current job */ |
| 10814 | jobnum = G.last_jobid; |
| 10815 | if (jobnum == 0) { |
| 10816 | bb_error_msg("no current job"); |
| 10817 | return NULL; |
| 10818 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10819 | } |
| 10820 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10821 | if (pi->jobid == jobnum) { |
| 10822 | return pi; |
| 10823 | } |
| 10824 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10825 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10826 | return NULL; |
| 10827 | } |
| 10828 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10829 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10830 | { |
| 10831 | struct pipe *job; |
| 10832 | const char *status_string; |
| 10833 | |
| 10834 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10835 | for (job = G.job_list; job; job = job->next) { |
| 10836 | if (job->alive_cmds == job->stopped_cmds) |
| 10837 | status_string = "Stopped"; |
| 10838 | else |
| 10839 | status_string = "Running"; |
| 10840 | |
| 10841 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10842 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10843 | |
| 10844 | clean_up_last_dead_job(); |
| 10845 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10846 | return EXIT_SUCCESS; |
| 10847 | } |
| 10848 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10849 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10850 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10851 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10852 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10853 | struct pipe *pi; |
| 10854 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10855 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10856 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10857 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10858 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10859 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10860 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10861 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10862 | goto found; |
| 10863 | } |
| 10864 | } |
| 10865 | bb_error_msg("%s: no current job", argv[0]); |
| 10866 | return EXIT_FAILURE; |
| 10867 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10868 | |
| 10869 | pi = parse_jobspec(argv[1]); |
| 10870 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10871 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10872 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10873 | /* TODO: bash prints a string representation |
| 10874 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10875 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10876 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10877 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10878 | } |
| 10879 | |
| 10880 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10881 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10882 | for (i = 0; i < pi->num_cmds; i++) { |
| 10883 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10884 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10885 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10886 | |
| 10887 | i = kill(- pi->pgrp, SIGCONT); |
| 10888 | if (i < 0) { |
| 10889 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10890 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10891 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10892 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10893 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10894 | } |
| 10895 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10896 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10897 | 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] | 10898 | return checkjobs_and_fg_shell(pi); |
| 10899 | } |
| 10900 | return EXIT_SUCCESS; |
| 10901 | } |
| 10902 | #endif |
| 10903 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10904 | #if ENABLE_HUSH_KILL |
| 10905 | static int FAST_FUNC builtin_kill(char **argv) |
| 10906 | { |
| 10907 | int ret = 0; |
| 10908 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10909 | # if ENABLE_HUSH_JOB |
| 10910 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10911 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10912 | |
| 10913 | do { |
| 10914 | struct pipe *pi; |
| 10915 | char *dst; |
| 10916 | int j, n; |
| 10917 | |
| 10918 | if (argv[i][0] != '%') |
| 10919 | continue; |
| 10920 | /* |
| 10921 | * "kill %N" - job kill |
| 10922 | * Converting to pgrp / pid kill |
| 10923 | */ |
| 10924 | pi = parse_jobspec(argv[i]); |
| 10925 | if (!pi) { |
| 10926 | /* Eat bad jobspec */ |
| 10927 | j = i; |
| 10928 | do { |
| 10929 | j++; |
| 10930 | argv[j - 1] = argv[j]; |
| 10931 | } while (argv[j]); |
| 10932 | ret = 1; |
| 10933 | i--; |
| 10934 | continue; |
| 10935 | } |
| 10936 | /* |
| 10937 | * In jobs started under job control, we signal |
| 10938 | * entire process group by kill -PGRP_ID. |
| 10939 | * This happens, f.e., in interactive shell. |
| 10940 | * |
| 10941 | * Otherwise, we signal each child via |
| 10942 | * kill PID1 PID2 PID3. |
| 10943 | * Testcases: |
| 10944 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10945 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10946 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10947 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10948 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10949 | dst = alloca(n * sizeof(int)*4); |
| 10950 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10951 | if (G_interactive_fd) |
| 10952 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10953 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10954 | struct command *cmd = &pi->cmds[j]; |
| 10955 | /* Skip exited members of the job */ |
| 10956 | if (cmd->pid == 0) |
| 10957 | continue; |
| 10958 | /* |
| 10959 | * kill_main has matching code to expect |
| 10960 | * leading space. Needed to not confuse |
| 10961 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10962 | */ |
| 10963 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10964 | } |
| 10965 | *dst = '\0'; |
| 10966 | } while (argv[++i]); |
| 10967 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10968 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10969 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10970 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10971 | ret = run_applet_main(argv, kill_main); |
| 10972 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10973 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10974 | return ret; |
| 10975 | } |
| 10976 | #endif |
| 10977 | |
| 10978 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10979 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10980 | #if !ENABLE_HUSH_JOB |
| 10981 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10982 | #endif |
| 10983 | 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] | 10984 | { |
| 10985 | int ret = 0; |
| 10986 | for (;;) { |
| 10987 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10988 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10989 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10990 | if (!sigisemptyset(&G.pending_set)) |
| 10991 | goto check_sig; |
| 10992 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10993 | /* waitpid is not interruptible by SA_RESTARTed |
| 10994 | * signals which we use. Thus, this ugly dance: |
| 10995 | */ |
| 10996 | |
| 10997 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10998 | * pending signal mask before we call waitpid. |
| 10999 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11000 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11001 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11002 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 11003 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11004 | |
| 11005 | if (!sigisemptyset(&G.pending_set)) { |
| 11006 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11007 | goto restore; |
| 11008 | } |
| 11009 | |
| 11010 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11011 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11012 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11013 | debug_printf_exec("checkjobs:%d\n", ret); |
| 11014 | #if ENABLE_HUSH_JOB |
| 11015 | if (waitfor_pipe) { |
| 11016 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 11017 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 11018 | if (rcode >= 0) { |
| 11019 | ret = rcode; |
| 11020 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11021 | break; |
| 11022 | } |
| 11023 | } |
| 11024 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11025 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 11026 | /* if ret == 0, no children changed state */ |
| 11027 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11028 | if (errno == ECHILD || ret) { |
| 11029 | ret--; |
| 11030 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11031 | ret = 0; |
| 11032 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 11033 | break; |
| 11034 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11035 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11036 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 11037 | /* Note: sigsuspend invokes signal handler */ |
| 11038 | sigsuspend(&oldset); |
| 11039 | restore: |
| 11040 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 11041 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11042 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11043 | sig = check_and_run_traps(); |
| 11044 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 11045 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11046 | ret = 128 + sig; |
| 11047 | break; |
| 11048 | } |
| 11049 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 11050 | } |
| 11051 | return ret; |
| 11052 | } |
| 11053 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11054 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11055 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11056 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 11057 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11058 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 11059 | argv = skip_dash_dash(argv); |
| 11060 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 11061 | /* Don't care about wait results */ |
| 11062 | /* Note 1: must wait until there are no more children */ |
| 11063 | /* Note 2: must be interruptible */ |
| 11064 | /* Examples: |
| 11065 | * $ sleep 3 & sleep 6 & wait |
| 11066 | * [1] 30934 sleep 3 |
| 11067 | * [2] 30935 sleep 6 |
| 11068 | * [1] Done sleep 3 |
| 11069 | * [2] Done sleep 6 |
| 11070 | * $ sleep 3 & sleep 6 & wait |
| 11071 | * [1] 30936 sleep 3 |
| 11072 | * [2] 30937 sleep 6 |
| 11073 | * [1] Done sleep 3 |
| 11074 | * ^C <-- after ~4 sec from keyboard |
| 11075 | * $ |
| 11076 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11077 | 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] | 11078 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11079 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11080 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11081 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11082 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11083 | #if ENABLE_HUSH_JOB |
| 11084 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11085 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11086 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11087 | wait_pipe = parse_jobspec(*argv); |
| 11088 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11089 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11090 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11091 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 11092 | } else { |
| 11093 | /* waiting on "last dead job" removes it */ |
| 11094 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 11095 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11096 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 11097 | /* else: parse_jobspec() already emitted error msg */ |
| 11098 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 11099 | } |
| 11100 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11101 | /* mimic bash message */ |
| 11102 | 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] | 11103 | ret = EXIT_FAILURE; |
| 11104 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 11105 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11106 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11107 | /* Do we have such child? */ |
| 11108 | ret = waitpid(pid, &status, WNOHANG); |
| 11109 | if (ret < 0) { |
| 11110 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11111 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11112 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 11113 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11114 | /* "wait $!" but last bg task has already exited. Try: |
| 11115 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 11116 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11117 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11118 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 11119 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 11120 | } else { |
| 11121 | /* Example: "wait 1". mimic bash message */ |
| 11122 | bb_error_msg("wait: pid %d is not a child of this shell", (int)pid); |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11123 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11124 | } else { |
| 11125 | /* ??? */ |
| 11126 | bb_perror_msg("wait %s", *argv); |
| 11127 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11128 | continue; /* bash checks all argv[] */ |
| 11129 | } |
| 11130 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11131 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11132 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 11133 | } else { |
| 11134 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 11135 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 11136 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11137 | if (WIFSIGNALED(status)) |
| 11138 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11139 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 11140 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11141 | |
| 11142 | return ret; |
| 11143 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 11144 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 11145 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11146 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 11147 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 11148 | { |
| 11149 | if (argv[1]) { |
| 11150 | def = bb_strtou(argv[1], NULL, 10); |
| 11151 | if (errno || def < def_min || argv[2]) { |
| 11152 | bb_error_msg("%s: bad arguments", argv[0]); |
| 11153 | def = UINT_MAX; |
| 11154 | } |
| 11155 | } |
| 11156 | return def; |
| 11157 | } |
| 11158 | #endif |
| 11159 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11160 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11161 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11162 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11163 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11164 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11165 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11166 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 11167 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 11168 | return EXIT_SUCCESS; /* bash compat */ |
| 11169 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 11170 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11171 | |
| 11172 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 11173 | if (depth == UINT_MAX) |
| 11174 | G.flag_break_continue = BC_BREAK; |
| 11175 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 11176 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11177 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11178 | return EXIT_SUCCESS; |
| 11179 | } |
| 11180 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11181 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11182 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 11183 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 11184 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 11185 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 11186 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11187 | |
| 11188 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 11189 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11190 | { |
| 11191 | int rc; |
| 11192 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11193 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11194 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 11195 | return EXIT_FAILURE; /* bash compat */ |
| 11196 | } |
| 11197 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11198 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11199 | |
| 11200 | /* bash: |
| 11201 | * out of range: wraps around at 256, does not error out |
| 11202 | * non-numeric param: |
| 11203 | * f() { false; return qwe; }; f; echo $? |
| 11204 | * bash: return: qwe: numeric argument required <== we do this |
| 11205 | * 255 <== we also do this |
| 11206 | */ |
| 11207 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 11208 | return rc; |
| 11209 | } |
| 11210 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11211 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11212 | #if ENABLE_HUSH_TIMES |
| 11213 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11214 | { |
| 11215 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11216 | ' ', offsetof(struct tms, tms_utime), |
| 11217 | '\n', offsetof(struct tms, tms_stime), |
| 11218 | ' ', offsetof(struct tms, tms_cutime), |
| 11219 | '\n', offsetof(struct tms, tms_cstime), |
| 11220 | 0 |
| 11221 | }; |
| 11222 | const uint8_t *p; |
| 11223 | unsigned clk_tck; |
| 11224 | struct tms buf; |
| 11225 | |
| 11226 | clk_tck = bb_clk_tck(); |
| 11227 | |
| 11228 | times(&buf); |
| 11229 | p = times_tbl; |
| 11230 | do { |
| 11231 | unsigned sec, frac; |
| 11232 | unsigned long t; |
| 11233 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11234 | sec = t / clk_tck; |
| 11235 | frac = t % clk_tck; |
| 11236 | printf("%um%u.%03us%c", |
| 11237 | sec / 60, sec % 60, |
| 11238 | (frac * 1000) / clk_tck, |
| 11239 | p[0]); |
| 11240 | p += 2; |
| 11241 | } while (*p); |
| 11242 | |
| 11243 | return EXIT_SUCCESS; |
| 11244 | } |
| 11245 | #endif |
| 11246 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11247 | #if ENABLE_HUSH_MEMLEAK |
| 11248 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11249 | { |
| 11250 | void *p; |
| 11251 | unsigned long l; |
| 11252 | |
| 11253 | # ifdef M_TRIM_THRESHOLD |
| 11254 | /* Optional. Reduces probability of false positives */ |
| 11255 | malloc_trim(0); |
| 11256 | # endif |
| 11257 | /* Crude attempt to find where "free memory" starts, |
| 11258 | * sans fragmentation. */ |
| 11259 | p = malloc(240); |
| 11260 | l = (unsigned long)p; |
| 11261 | free(p); |
| 11262 | p = malloc(3400); |
| 11263 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11264 | free(p); |
| 11265 | |
| 11266 | |
| 11267 | # if 0 /* debug */ |
| 11268 | { |
| 11269 | struct mallinfo mi = mallinfo(); |
| 11270 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11271 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11272 | } |
| 11273 | # endif |
| 11274 | |
| 11275 | if (!G.memleak_value) |
| 11276 | G.memleak_value = l; |
| 11277 | |
| 11278 | l -= G.memleak_value; |
| 11279 | if ((long)l < 0) |
| 11280 | l = 0; |
| 11281 | l /= 1024; |
| 11282 | if (l > 127) |
| 11283 | l = 127; |
| 11284 | |
| 11285 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11286 | return l; |
| 11287 | } |
| 11288 | #endif |