Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3 | * A prototype Bourne shell grammar parser. |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7 | * |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com> |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10 | * |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 12 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 13 | * Credits: |
| 14 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 15 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 16 | * execution engine, the builtins, and much of the underlying |
| 17 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 19 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 20 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 21 | * Troan, which they placed in the public domain. I don't know |
| 22 | * how much of the Johnson/Troan code has survived the repeated |
| 23 | * rewrites. |
| 24 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 25 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 26 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 27 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 28 | * and many builtins derived from contributions by Erik Andersen. |
| 29 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 30 | * |
| 31 | * There are two big (and related) architecture differences between |
| 32 | * this parser and the lash parser. One is that this version is |
| 33 | * actually designed from the ground up to understand nearly all |
| 34 | * of the Bourne grammar. The second, consequential change is that |
| 35 | * the parser and input reader have been turned inside out. Now, |
| 36 | * the parser is in control, and asks for input as needed. The old |
| 37 | * way had the input reader in control, and it asked for parsing to |
| 38 | * take place as needed. The new way makes it much easier to properly |
| 39 | * handle the recursion implicit in the various substitutions, especially |
| 40 | * across continuation lines. |
| 41 | * |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 42 | * TODOs: |
| 43 | * grep for "TODO" and fix (some of them are easy) |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 44 | * make complex ${var%...} constructs support optional |
| 45 | * make here documents optional |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 46 | * special variables (done: PWD, PPID, RANDOM) |
| 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * tilde expansion |
| 49 | * aliases |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 50 | * "command" missing features: |
| 51 | * command -p CMD: run CMD using default $PATH |
| 52 | * (can use this to override standalone shell as well?) |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 53 | * command BLTIN: disables special-ness (e.g. errors do not abort) |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 54 | * command -V CMD1 CMD2 CMD3 (multiple args) (not in standard) |
| 55 | * builtins mandated by standards we don't support: |
| 56 | * [un]alias, fc: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 57 | * fc -l[nr] [BEG] [END]: list range of commands in history |
| 58 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands |
| 59 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 60 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 61 | * Bash compat TODO: |
| 62 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 63 | * reserved words: function select |
| 64 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 65 | * process substitution: <(list) and >(list) |
| 66 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 67 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 68 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 69 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 70 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 71 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 72 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 73 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 74 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 75 | * indirect expansion: ${!VAR} |
| 76 | * substring op on @: ${@:n:m} |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 77 | * |
| 78 | * Won't do: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 79 | * Some builtins mandated by standards: |
| 80 | * newgrp [GRP]: not a builtin in bash but a suid binary |
| 81 | * which spawns a new shell with new group ID |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 82 | * |
| 83 | * Status of [[ support: |
| 84 | * [[ args ]] are CMD_SINGLEWORD_NOGLOB: |
| 85 | * v='a b'; [[ $v = 'a b' ]]; echo 0:$? |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame^] | 86 | * [[ /bin/n* ]]; echo 0:$? |
Denys Vlasenko | 3632cb1 | 2018-04-10 15:25:41 +0200 | [diff] [blame] | 87 | * TODO: |
| 88 | * &&/|| are AND/OR ops, -a/-o are not |
| 89 | * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) |
| 90 | * = is glob match operator, not equality operator: STR = GLOB |
| 91 | * (in GLOB, quoting is significant on char-by-char basis: a*cd"*") |
| 92 | * == same as = |
| 93 | * add =~ regex match operator: STR =~ REGEX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 94 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 95 | //config:config HUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 96 | //config: bool "hush (64 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 97 | //config: default y |
| 98 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 99 | //config: hush is a small shell. It handles the normal flow control |
| 100 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 101 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 102 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 104 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 105 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 106 | //config: It does not handle select, aliases, tilde expansion, |
| 107 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 108 | //config: |
| 109 | //config:config HUSH_BASH_COMPAT |
| 110 | //config: bool "bash-compatible extensions" |
| 111 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 112 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 113 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 114 | //config:config HUSH_BRACE_EXPANSION |
| 115 | //config: bool "Brace expansion" |
| 116 | //config: default y |
| 117 | //config: depends on HUSH_BASH_COMPAT |
| 118 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 119 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 120 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 121 | //config:config HUSH_LINENO_VAR |
| 122 | //config: bool "$LINENO variable" |
| 123 | //config: default y |
| 124 | //config: depends on HUSH_BASH_COMPAT |
| 125 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 126 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 127 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 128 | //config: default n # do not encourage non-standard behavior |
| 129 | //config: depends on HUSH_BASH_COMPAT |
| 130 | //config: help |
| 131 | //config: This is not compliant with standards. Avoid if possible. |
| 132 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 133 | //config:config HUSH_INTERACTIVE |
| 134 | //config: bool "Interactive mode" |
| 135 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 136 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 137 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 138 | //config: Enable interactive mode (prompt and command editing). |
| 139 | //config: Without this, hush simply reads and executes commands |
| 140 | //config: from stdin just like a shell script from a file. |
| 141 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 143 | //config:config HUSH_SAVEHISTORY |
| 144 | //config: bool "Save command history to .hush_history" |
| 145 | //config: default y |
| 146 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 147 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 148 | //config:config HUSH_JOB |
| 149 | //config: bool "Job control" |
| 150 | //config: default y |
| 151 | //config: depends on HUSH_INTERACTIVE |
| 152 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 153 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 154 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 155 | //config: "cmd &" still works by simply spawning a process and immediately |
| 156 | //config: prompting for next command (or executing next command in a script), |
| 157 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 158 | //config: |
| 159 | //config:config HUSH_TICK |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 160 | //config: bool "Support process substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 161 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 162 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 163 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 164 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config: |
| 166 | //config:config HUSH_IF |
| 167 | //config: bool "Support if/then/elif/else/fi" |
| 168 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 169 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 170 | //config: |
| 171 | //config:config HUSH_LOOPS |
| 172 | //config: bool "Support for, while and until loops" |
| 173 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 174 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 175 | //config: |
| 176 | //config:config HUSH_CASE |
| 177 | //config: bool "Support case ... esac statement" |
| 178 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 179 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 180 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 181 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 182 | //config: |
| 183 | //config:config HUSH_FUNCTIONS |
| 184 | //config: bool "Support funcname() { commands; } syntax" |
| 185 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 186 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 187 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 188 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: |
| 190 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 191 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 192 | //config: default y |
| 193 | //config: depends on HUSH_FUNCTIONS |
| 194 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 195 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 196 | //config: |
| 197 | //config:config HUSH_RANDOM_SUPPORT |
| 198 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 199 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 200 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 201 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 202 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 203 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 204 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 205 | //config:config HUSH_MODE_X |
| 206 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 207 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 208 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 209 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 210 | //config: This instructs hush to print commands before execution. |
| 211 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 212 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 213 | //config:config HUSH_ECHO |
| 214 | //config: bool "echo builtin" |
| 215 | //config: default y |
| 216 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 217 | //config: |
| 218 | //config:config HUSH_PRINTF |
| 219 | //config: bool "printf builtin" |
| 220 | //config: default y |
| 221 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 222 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 223 | //config:config HUSH_TEST |
| 224 | //config: bool "test builtin" |
| 225 | //config: default y |
| 226 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 227 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 228 | //config:config HUSH_HELP |
| 229 | //config: bool "help builtin" |
| 230 | //config: default y |
| 231 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 232 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 233 | //config:config HUSH_EXPORT |
| 234 | //config: bool "export builtin" |
| 235 | //config: default y |
| 236 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 237 | //config: |
| 238 | //config:config HUSH_EXPORT_N |
| 239 | //config: bool "Support 'export -n' option" |
| 240 | //config: default y |
| 241 | //config: depends on HUSH_EXPORT |
| 242 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 243 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 244 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 245 | //config:config HUSH_READONLY |
| 246 | //config: bool "readonly builtin" |
| 247 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 248 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 249 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 250 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 251 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 252 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 253 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 254 | //config: default y |
| 255 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 256 | //config: |
| 257 | //config:config HUSH_WAIT |
| 258 | //config: bool "wait builtin" |
| 259 | //config: default y |
| 260 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 261 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 262 | //config:config HUSH_COMMAND |
| 263 | //config: bool "command builtin" |
| 264 | //config: default y |
| 265 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 266 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 267 | //config:config HUSH_TRAP |
| 268 | //config: bool "trap builtin" |
| 269 | //config: default y |
| 270 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 271 | //config: |
| 272 | //config:config HUSH_TYPE |
| 273 | //config: bool "type builtin" |
| 274 | //config: default y |
| 275 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 276 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 277 | //config:config HUSH_TIMES |
| 278 | //config: bool "times builtin" |
| 279 | //config: default y |
| 280 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 281 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 282 | //config:config HUSH_READ |
| 283 | //config: bool "read builtin" |
| 284 | //config: default y |
| 285 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 286 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 287 | //config:config HUSH_SET |
| 288 | //config: bool "set builtin" |
| 289 | //config: default y |
| 290 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 291 | //config: |
| 292 | //config:config HUSH_UNSET |
| 293 | //config: bool "unset builtin" |
| 294 | //config: default y |
| 295 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 296 | //config: |
| 297 | //config:config HUSH_ULIMIT |
| 298 | //config: bool "ulimit builtin" |
| 299 | //config: default y |
| 300 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 301 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 302 | //config:config HUSH_UMASK |
| 303 | //config: bool "umask builtin" |
| 304 | //config: default y |
| 305 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 306 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 307 | //config:config HUSH_GETOPTS |
| 308 | //config: bool "getopts builtin" |
| 309 | //config: default y |
| 310 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 311 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 312 | //config:config HUSH_MEMLEAK |
| 313 | //config: bool "memleak builtin (debugging)" |
| 314 | //config: default n |
| 315 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 316 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 317 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 318 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 319 | //applet:IF_SH_IS_HUSH( APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 320 | //applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush)) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 321 | |
| 322 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 323 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 324 | //kbuild:lib-$(CONFIG_BASH_IS_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 325 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 326 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 327 | /* -i (interactive) is also accepted, |
| 328 | * but does nothing, therefore not shown in help. |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 329 | * NOMMU-specific options are not meant to be used by users, |
| 330 | * therefore we don't show them either. |
| 331 | */ |
| 332 | //usage:#define hush_trivial_usage |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 333 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 334 | //usage:#define hush_full_usage "\n\n" |
| 335 | //usage: "Unix shell interpreter" |
| 336 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 337 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 338 | || defined(__APPLE__) \ |
| 339 | ) |
| 340 | # include <malloc.h> /* for malloc_trim */ |
| 341 | #endif |
| 342 | #include <glob.h> |
| 343 | /* #include <dmalloc.h> */ |
| 344 | #if ENABLE_HUSH_CASE |
| 345 | # include <fnmatch.h> |
| 346 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 347 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 348 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 349 | |
| 350 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 351 | #include "unicode.h" |
| 352 | #include "shell_common.h" |
| 353 | #include "math.h" |
| 354 | #include "match.h" |
| 355 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 356 | # include "random.h" |
| 357 | #else |
| 358 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 359 | #endif |
| 360 | #ifndef F_DUPFD_CLOEXEC |
| 361 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 362 | #endif |
| 363 | #ifndef PIPE_BUF |
| 364 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 365 | #endif |
| 366 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 367 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 368 | /* So far, all bash compat is controlled by one config option */ |
| 369 | /* Separate defines document which part of code implements what */ |
| 370 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 371 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 372 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 373 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 374 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 375 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 376 | |
| 377 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 378 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 379 | #define LEAK_HUNTING 0 |
| 380 | #define BUILD_AS_NOMMU 0 |
| 381 | /* Enable/disable sanity checks. Ok to enable in production, |
| 382 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 383 | * Keeping 1 for now even in released versions. |
| 384 | */ |
| 385 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 386 | /* Slightly bigger (+200 bytes), but faster hush. |
| 387 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 388 | * which allows us to do fewer waitpid's. |
| 389 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 390 | * and therefore waitpid will return the same result as last time) |
| 391 | */ |
| 392 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 393 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 394 | * So far ${var%...} ops are always enabled: |
| 395 | */ |
| 396 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 397 | |
| 398 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 399 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 400 | # undef BB_MMU |
| 401 | # undef USE_FOR_NOMMU |
| 402 | # undef USE_FOR_MMU |
| 403 | # define BB_MMU 0 |
| 404 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 405 | # define USE_FOR_MMU(...) |
| 406 | #endif |
| 407 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 408 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 409 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 410 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 411 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 412 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 413 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 414 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 415 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 416 | # define IF_FEATURE_SH_STANDALONE(...) |
| 417 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 418 | #endif |
| 419 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 420 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 421 | # undef ENABLE_FEATURE_EDITING |
| 422 | # define ENABLE_FEATURE_EDITING 0 |
| 423 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 424 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 425 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 426 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 427 | #endif |
| 428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 429 | /* Do we support ANY keywords? */ |
| 430 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 431 | # define HAS_KEYWORDS 1 |
| 432 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 433 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 434 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 435 | # define HAS_KEYWORDS 0 |
| 436 | # define IF_HAS_KEYWORDS(...) |
| 437 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 438 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 439 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 440 | /* If you comment out one of these below, it will be #defined later |
| 441 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 442 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 443 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf_parse(...) do {} while (0) |
| 445 | #define debug_print_tree(a, b) do {} while (0) |
| 446 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 447 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 448 | #define debug_printf_jobs(...) do {} while (0) |
| 449 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 450 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 451 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 452 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 453 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 454 | #define debug_printf_subst(...) do {} while (0) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 455 | #define debug_printf_prompt(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 456 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 458 | #define ERR_PTR ((void*)(long)1) |
| 459 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 460 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 461 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 462 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 463 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 464 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 465 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 466 | /* Support / and // replace ops */ |
| 467 | /* Note that // is stored as \ in "encoded" string representation */ |
| 468 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 469 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 470 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 471 | #else |
| 472 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 473 | # define VAR_SUBST_OPS "%#:-=+?" |
| 474 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 475 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 476 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 477 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 478 | #define SPECIAL_VAR_SYMBOL 3 |
| 479 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 480 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 481 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 482 | struct variable; |
| 483 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 484 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 485 | |
| 486 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 487 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 488 | */ |
| 489 | #if !BB_MMU |
| 490 | typedef struct nommu_save_t { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 491 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 492 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 493 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 494 | } nommu_save_t; |
| 495 | #endif |
| 496 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 497 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 498 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 499 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 500 | RES_IF , |
| 501 | RES_THEN , |
| 502 | RES_ELIF , |
| 503 | RES_ELSE , |
| 504 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 505 | #endif |
| 506 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 507 | RES_FOR , |
| 508 | RES_WHILE , |
| 509 | RES_UNTIL , |
| 510 | RES_DO , |
| 511 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 512 | #endif |
| 513 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 514 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 515 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 516 | #if ENABLE_HUSH_CASE |
| 517 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 518 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 519 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 520 | RES_MATCH , /* "word)" */ |
| 521 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 522 | RES_ESAC , |
| 523 | #endif |
| 524 | RES_XXXX , |
| 525 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 526 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 527 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 528 | typedef struct o_string { |
| 529 | char *data; |
| 530 | int length; /* position where data is appended */ |
| 531 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 532 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 533 | /* At least some part of the string was inside '' or "", |
| 534 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 535 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 536 | smallint has_empty_slot; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 537 | } o_string; |
| 538 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 539 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 540 | EXP_FLAG_GLOB = 0x2, |
| 541 | /* Protect newly added chars against globbing |
| 542 | * by prepending \ to *, ?, [, \ */ |
| 543 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 544 | }; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 545 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 546 | #define NULL_O_STRING { NULL } |
| 547 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 548 | #ifndef debug_printf_parse |
| 549 | static const char *const assignment_flag[] = { |
| 550 | "MAYBE_ASSIGNMENT", |
| 551 | "DEFINITELY_ASSIGNMENT", |
| 552 | "NOT_ASSIGNMENT", |
| 553 | "WORD_IS_KEYWORD", |
| 554 | }; |
| 555 | #endif |
| 556 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 557 | typedef struct in_str { |
| 558 | const char *p; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 559 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 560 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 561 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 562 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 563 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 564 | /* The descrip member of this structure is only used to make |
| 565 | * debugging output pretty */ |
| 566 | static const struct { |
| 567 | int mode; |
| 568 | signed char default_fd; |
| 569 | char descrip[3]; |
| 570 | } redir_table[] = { |
| 571 | { O_RDONLY, 0, "<" }, |
| 572 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 573 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 574 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 575 | { O_RDONLY, 0, "<<" }, |
| 576 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 577 | /* { O_RDONLY, 77, "<<" }, */ |
| 578 | }; |
| 579 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 580 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 581 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 582 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 583 | int rd_fd; /* fd to redirect */ |
| 584 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 585 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 586 | smallint rd_type; /* (enum redir_type) */ |
| 587 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 588 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 589 | * bit 0: do we need to trim leading tabs? |
| 590 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 591 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 592 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 593 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 594 | REDIRECT_INPUT = 0, |
| 595 | REDIRECT_OVERWRITE = 1, |
| 596 | REDIRECT_APPEND = 2, |
| 597 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 598 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 599 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 600 | |
| 601 | REDIRFD_CLOSE = -3, |
| 602 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 603 | REDIRFD_TO_FILE = -1, |
| 604 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 605 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 606 | HEREDOC_SKIPTABS = 1, |
| 607 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 608 | } redir_type; |
| 609 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 611 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 612 | pid_t pid; /* 0 if exited */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 613 | unsigned assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 614 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 615 | unsigned lineno; |
| 616 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 617 | smallint cmd_type; /* CMD_xxx */ |
| 618 | #define CMD_NORMAL 0 |
| 619 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 620 | #if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY |
| 621 | /* used for "[[ EXPR ]]", and to prevent word splitting and globbing in |
| 622 | * "export v=t*" |
| 623 | */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 624 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 625 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 626 | #if ENABLE_HUSH_FUNCTIONS |
| 627 | # define CMD_FUNCDEF 3 |
| 628 | #endif |
| 629 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 630 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 631 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 632 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 633 | #if !BB_MMU |
| 634 | char *group_as_string; |
| 635 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 636 | #if ENABLE_HUSH_FUNCTIONS |
| 637 | struct function *child_func; |
| 638 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 639 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 640 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 641 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 642 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 643 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 644 | * we put those fields back into cmd->xxx |
| 645 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 646 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 647 | * Without this trick, loop would execute a;b;b;b;... |
| 648 | * instead of correct sequence a;b;a;b;... |
| 649 | * When command is freed, it severs the link |
| 650 | * (sets ->child_func->parent_cmd to NULL). |
| 651 | */ |
| 652 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 653 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 654 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 655 | * and on execution these are substituted with their values. |
| 656 | * Substitution can make _several_ words out of one argv[n]! |
| 657 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 658 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 659 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 660 | struct redir_struct *redirects; /* I/O redirections */ |
| 661 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 662 | /* Is there anything in this command at all? */ |
| 663 | #define IS_NULL_CMD(cmd) \ |
| 664 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 665 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 666 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 667 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 668 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 669 | int alive_cmds; /* number of commands running (not exited) */ |
| 670 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 671 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 672 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 673 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 674 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 675 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 676 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 677 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 678 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 679 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 680 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 681 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 682 | PIPE_SEQ = 0, |
| 683 | PIPE_AND = 1, |
| 684 | PIPE_OR = 2, |
| 685 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 686 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 687 | /* Is there anything in this pipe at all? */ |
| 688 | #define IS_NULL_PIPE(pi) \ |
| 689 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 690 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 691 | /* This holds pointers to the various results of parsing */ |
| 692 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 693 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 694 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 695 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 696 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 697 | /* last command in pipe (being constructed right now) */ |
| 698 | struct command *command; |
| 699 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 700 | struct redir_struct *pending_redirect; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 701 | o_string word; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 702 | #if !BB_MMU |
| 703 | o_string as_string; |
| 704 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 705 | smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 706 | #if HAS_KEYWORDS |
| 707 | smallint ctx_res_w; |
| 708 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 709 | #if ENABLE_HUSH_CASE |
| 710 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 711 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 712 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 713 | int old_flag; |
| 714 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 715 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 716 | * when we see "if" or "then", we malloc and copy current context, |
| 717 | * and make ->stack point to it. then we parse pipeN. |
| 718 | * when closing "then" / fi" / whatever is found, |
| 719 | * we move list_head into ->stack->command->group, |
| 720 | * copy ->stack into current context, and delete ->stack. |
| 721 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 722 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 723 | struct parse_context *stack; |
| 724 | #endif |
| 725 | }; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 726 | enum { |
| 727 | MAYBE_ASSIGNMENT = 0, |
| 728 | DEFINITELY_ASSIGNMENT = 1, |
| 729 | NOT_ASSIGNMENT = 2, |
| 730 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
| 731 | WORD_IS_KEYWORD = 3, |
| 732 | }; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 733 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 734 | /* On program start, environ points to initial environment. |
| 735 | * putenv adds new pointers into it, unsetenv removes them. |
| 736 | * Neither of these (de)allocates the strings. |
| 737 | * setenv allocates new strings in malloc space and does putenv, |
| 738 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 739 | #define setenv(...) setenv_is_leaky_dont_use() |
| 740 | struct variable { |
| 741 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 742 | char *varstr; /* points to "name=" portion */ |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 743 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 744 | uint16_t var_nest_level; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 745 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 746 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 747 | }; |
| 748 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 749 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 750 | BC_BREAK = 1, |
| 751 | BC_CONTINUE = 2, |
| 752 | }; |
| 753 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 754 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 755 | struct function { |
| 756 | struct function *next; |
| 757 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 758 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 759 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 760 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 761 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 762 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 763 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 764 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 765 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 766 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 767 | /* set -/+o OPT support. (TODO: make it optional) |
| 768 | * bash supports the following opts: |
| 769 | * allexport off |
| 770 | * braceexpand on |
| 771 | * emacs on |
| 772 | * errexit off |
| 773 | * errtrace off |
| 774 | * functrace off |
| 775 | * hashall on |
| 776 | * histexpand off |
| 777 | * history on |
| 778 | * ignoreeof off |
| 779 | * interactive-comments on |
| 780 | * keyword off |
| 781 | * monitor on |
| 782 | * noclobber off |
| 783 | * noexec off |
| 784 | * noglob off |
| 785 | * nolog off |
| 786 | * notify off |
| 787 | * nounset off |
| 788 | * onecmd off |
| 789 | * physical off |
| 790 | * pipefail off |
| 791 | * posix off |
| 792 | * privileged off |
| 793 | * verbose off |
| 794 | * vi off |
| 795 | * xtrace off |
| 796 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 797 | static const char o_opt_strings[] ALIGN1 = |
| 798 | "pipefail\0" |
| 799 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 800 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 801 | #if ENABLE_HUSH_MODE_X |
| 802 | "xtrace\0" |
| 803 | #endif |
| 804 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 805 | enum { |
| 806 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 807 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 808 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 809 | #if ENABLE_HUSH_MODE_X |
| 810 | OPT_O_XTRACE, |
| 811 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 812 | NUM_OPT_O |
| 813 | }; |
| 814 | |
| 815 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 816 | struct FILE_list { |
| 817 | struct FILE_list *next; |
| 818 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 819 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 820 | }; |
| 821 | |
| 822 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 823 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 824 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 825 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 826 | /* interactive_fd != 0 means we are an interactive shell. |
| 827 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 828 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 829 | * job control still works, but terminal signals |
| 830 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 831 | * process groups can only be created with "cmd &". |
| 832 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 833 | * to give tty to the foreground process group, |
| 834 | * and will take it back when the group is stopped (^Z) |
| 835 | * or killed (^C). |
| 836 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 837 | #if ENABLE_HUSH_INTERACTIVE |
| 838 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 839 | * _AND_ if we decided to act interactively */ |
| 840 | int interactive_fd; |
| 841 | const char *PS1; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 842 | IF_FEATURE_EDITING_FANCY_PROMPT(const char *PS2;) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 843 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 844 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 845 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 846 | #endif |
| 847 | #if ENABLE_FEATURE_EDITING |
| 848 | line_input_t *line_input_state; |
| 849 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 850 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 851 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 852 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 853 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 854 | random_t random_gen; |
| 855 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 856 | #if ENABLE_HUSH_JOB |
| 857 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 858 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 859 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 860 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 861 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 862 | #else |
| 863 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 864 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 865 | /* How deeply are we in context where "set -e" is ignored */ |
| 866 | int errexit_depth; |
| 867 | /* "set -e" rules (do we follow them correctly?): |
| 868 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 869 | * Shell does not exit if failed command is part of condition in |
| 870 | * if/while, part of && or || list except the last command, any command |
| 871 | * in a pipe but the last, or if the command's return value is being |
| 872 | * inverted with !. If a compound command other than a subshell returns a |
| 873 | * non-zero status because a command failed while -e was being ignored, the |
| 874 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 875 | * exits [ERR is a bashism]. |
| 876 | * |
| 877 | * If a compound command or function executes in a context where -e is |
| 878 | * ignored, none of the commands executed within are affected by the -e |
| 879 | * setting. If a compound command or function sets -e while executing in a |
| 880 | * context where -e is ignored, that setting does not have any effect until |
| 881 | * the compound command or the command containing the function call completes. |
| 882 | */ |
| 883 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 884 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 885 | #if ENABLE_HUSH_MODE_X |
| 886 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 887 | #else |
| 888 | # define G_x_mode 0 |
| 889 | #endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 890 | #if ENABLE_HUSH_INTERACTIVE |
| 891 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 892 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 893 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 894 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 895 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 896 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 897 | #if ENABLE_HUSH_FUNCTIONS |
| 898 | /* 0: outside of a function (or sourced file) |
| 899 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 900 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 901 | */ |
| 902 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 903 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 904 | #else |
| 905 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 906 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 907 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 908 | /* These support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 909 | smalluint last_exitcode; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 910 | smalluint expand_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 911 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 912 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 913 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 914 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 915 | # define G_global_args_malloced (G.global_args_malloced) |
| 916 | #else |
| 917 | # define G_global_args_malloced 0 |
| 918 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 919 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 920 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 921 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 922 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 923 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 924 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 925 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 926 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 927 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 928 | #endif |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 929 | #if ENABLE_HUSH_GETOPTS |
| 930 | unsigned getopt_count; |
| 931 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 932 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 933 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 934 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 935 | char **expanded_assignments; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 936 | struct variable **shadowed_vars_pp; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 937 | unsigned var_nest_level; |
| 938 | #if ENABLE_HUSH_FUNCTIONS |
| 939 | # if ENABLE_HUSH_LOCAL |
| 940 | unsigned func_nest_level; /* solely to prevent "local v" in non-functions */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 941 | # endif |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 942 | struct function *top_func; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 943 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 944 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 945 | #if ENABLE_HUSH_FAST |
| 946 | unsigned count_SIGCHLD; |
| 947 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 948 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 949 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 950 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 951 | unsigned lineno; |
| 952 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 953 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 954 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 955 | /* Which signals have non-DFL handler (even with no traps set)? |
| 956 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 957 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 958 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 959 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 960 | * Other than these two times, never modified. |
| 961 | */ |
| 962 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 963 | #if ENABLE_HUSH_JOB |
| 964 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 965 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 966 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 967 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 968 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 969 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 970 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 971 | # define G_traps G.traps |
| 972 | #else |
| 973 | # define G_traps ((char**)NULL) |
| 974 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 975 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 976 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 977 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 978 | #endif |
| 979 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 980 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 981 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 982 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 983 | #if ENABLE_FEATURE_EDITING |
| 984 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 985 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 986 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 987 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 988 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 989 | * (too many defines). Also, I actually prefer to see when a variable |
| 990 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 991 | #define INIT_G() do { \ |
| 992 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 993 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 994 | sigfillset(&G.sa.sa_mask); \ |
| 995 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 996 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 997 | |
| 998 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 999 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1000 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1001 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1002 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1003 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1004 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1005 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1006 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1007 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1008 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1009 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1010 | #if ENABLE_HUSH_READONLY |
| 1011 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1012 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1013 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1014 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1015 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1016 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1017 | #if ENABLE_HUSH_GETOPTS |
| 1018 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1019 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1020 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1021 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1022 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1023 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1024 | static int builtin_history(char **argv) FAST_FUNC; |
| 1025 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1026 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1027 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1028 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1029 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1030 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1031 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1032 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1033 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1034 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1035 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1036 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1037 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1038 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1039 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1040 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1041 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1042 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1043 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1044 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1045 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1046 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1047 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1048 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1049 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1050 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1051 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1052 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1053 | #if ENABLE_HUSH_TIMES |
| 1054 | static int builtin_times(char **argv) FAST_FUNC; |
| 1055 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1056 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1057 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1058 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1059 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1060 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1061 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1062 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1063 | #if ENABLE_HUSH_KILL |
| 1064 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1065 | #endif |
| 1066 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1067 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1068 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1069 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1070 | static int builtin_break(char **argv) FAST_FUNC; |
| 1071 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1072 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1073 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1074 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1075 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1076 | |
| 1077 | /* Table of built-in functions. They can be forked or not, depending on |
| 1078 | * context: within pipes, they fork. As simple commands, they do not. |
| 1079 | * When used in non-forking context, they can change global variables |
| 1080 | * in the parent shell process. If forked, of course they cannot. |
| 1081 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1082 | * still be set at the end. */ |
| 1083 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1084 | const char *b_cmd; |
| 1085 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1086 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1087 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1088 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1089 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1090 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1091 | #endif |
| 1092 | }; |
| 1093 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1094 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1095 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1096 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1097 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1098 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1099 | #endif |
| 1100 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1101 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1102 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1103 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1104 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1105 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1106 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1107 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1108 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1109 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1110 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1111 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1112 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1113 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1114 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1115 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1116 | #if ENABLE_HUSH_GETOPTS |
| 1117 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1118 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1119 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1120 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1121 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1122 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1123 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1124 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1125 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1126 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1127 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1128 | #if ENABLE_HUSH_KILL |
| 1129 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1130 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1131 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1132 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1133 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1134 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1135 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1136 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1137 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1138 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1139 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1140 | #if ENABLE_HUSH_READONLY |
| 1141 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1142 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1143 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1144 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1145 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1146 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1147 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1148 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1149 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1150 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1151 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1152 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1153 | #if ENABLE_HUSH_TIMES |
| 1154 | BLTIN("times" , builtin_times , NULL), |
| 1155 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1156 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1157 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1158 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1159 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1160 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1161 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1162 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1163 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1164 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1165 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1166 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1167 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1168 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1169 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1170 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1171 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1172 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1173 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1174 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1175 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1176 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1177 | * (it's cheaper to run an external program in this case): |
| 1178 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1179 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1180 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1181 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1182 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1183 | #if BASH_TEST2 |
| 1184 | BLTIN("[[" , builtin_test , NULL), |
| 1185 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1186 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1187 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1188 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1189 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1190 | BLTIN("printf" , builtin_printf , NULL), |
| 1191 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1192 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1193 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1194 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1195 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1196 | }; |
| 1197 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1198 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1199 | /* Debug printouts. |
| 1200 | */ |
| 1201 | #if HUSH_DEBUG |
| 1202 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1203 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1204 | # define debug_enter() (G.debug_indent++) |
| 1205 | # define debug_leave() (G.debug_indent--) |
| 1206 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1207 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1208 | # define debug_enter() ((void)0) |
| 1209 | # define debug_leave() ((void)0) |
| 1210 | #endif |
| 1211 | |
| 1212 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1213 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1214 | #endif |
| 1215 | |
| 1216 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1217 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1218 | #endif |
| 1219 | |
| 1220 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1221 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1222 | #endif |
| 1223 | |
| 1224 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1225 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1226 | #endif |
| 1227 | |
| 1228 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1229 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1230 | # define DEBUG_JOBS 1 |
| 1231 | #else |
| 1232 | # define DEBUG_JOBS 0 |
| 1233 | #endif |
| 1234 | |
| 1235 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1236 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1237 | # define DEBUG_EXPAND 1 |
| 1238 | #else |
| 1239 | # define DEBUG_EXPAND 0 |
| 1240 | #endif |
| 1241 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1242 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1243 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1244 | #endif |
| 1245 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1246 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1247 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1248 | # define DEBUG_GLOB 1 |
| 1249 | #else |
| 1250 | # define DEBUG_GLOB 0 |
| 1251 | #endif |
| 1252 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1253 | #ifndef debug_printf_redir |
| 1254 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1255 | #endif |
| 1256 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1257 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1258 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1259 | #endif |
| 1260 | |
| 1261 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1262 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1263 | #endif |
| 1264 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 1265 | #ifndef debug_printf_prompt |
| 1266 | # define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1267 | #endif |
| 1268 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1269 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1270 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1271 | # define DEBUG_CLEAN 1 |
| 1272 | #else |
| 1273 | # define DEBUG_CLEAN 0 |
| 1274 | #endif |
| 1275 | |
| 1276 | #if DEBUG_EXPAND |
| 1277 | static void debug_print_strings(const char *prefix, char **vv) |
| 1278 | { |
| 1279 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1280 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1281 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1282 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1283 | } |
| 1284 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1285 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1286 | #endif |
| 1287 | |
| 1288 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1289 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1290 | */ |
| 1291 | #if LEAK_HUNTING |
| 1292 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1293 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1294 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1295 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1296 | return ptr; |
| 1297 | } |
| 1298 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1299 | { |
| 1300 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1301 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1302 | return ptr; |
| 1303 | } |
| 1304 | static char *xxstrdup(int lineno, const char *str) |
| 1305 | { |
| 1306 | char *ptr = xstrdup(str); |
| 1307 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1308 | return ptr; |
| 1309 | } |
| 1310 | static void xxfree(void *ptr) |
| 1311 | { |
| 1312 | fdprintf(2, "free %p\n", ptr); |
| 1313 | free(ptr); |
| 1314 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1315 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1316 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1317 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1318 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1319 | #endif |
| 1320 | |
| 1321 | |
| 1322 | /* Syntax and runtime errors. They always abort scripts. |
| 1323 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1324 | * and return to the prompt. |
| 1325 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1326 | */ |
| 1327 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1328 | # 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] | 1329 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1330 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1331 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1332 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1333 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1334 | #endif |
| 1335 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1336 | static void die_if_script(void) |
| 1337 | { |
| 1338 | if (!G_interactive_fd) { |
| 1339 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1340 | xfunc_error_retval = G.last_exitcode; |
| 1341 | xfunc_die(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1346 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1347 | va_list p; |
| 1348 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1349 | #if HUSH_DEBUG >= 2 |
| 1350 | bb_error_msg("hush.c:%u", lineno); |
| 1351 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1352 | va_start(p, fmt); |
| 1353 | bb_verror_msg(fmt, p, NULL); |
| 1354 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1355 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1356 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1357 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1358 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1359 | { |
| 1360 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1361 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1362 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1363 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1364 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1367 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1368 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1369 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1370 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1373 | 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] | 1374 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1375 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1376 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1377 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1380 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1381 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1382 | char msg[2] = { ch, '\0' }; |
| 1383 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1386 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1387 | { |
| 1388 | char msg[2]; |
| 1389 | msg[0] = ch; |
| 1390 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1391 | #if HUSH_DEBUG >= 2 |
| 1392 | bb_error_msg("hush.c:%u", lineno); |
| 1393 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1394 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1395 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1398 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1399 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1400 | # undef syntax_error |
| 1401 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1402 | # undef syntax_error_unterm_ch |
| 1403 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1404 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1405 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1406 | # 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] | 1407 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1408 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1409 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1410 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1411 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1412 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1413 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1414 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 1415 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1416 | static void cmdedit_update_prompt(void); |
| 1417 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1418 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1419 | #endif |
| 1420 | |
| 1421 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1422 | /* Utility functions |
| 1423 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1424 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1425 | static char *unbackslash(char *src) |
| 1426 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1427 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1428 | while (1) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame^] | 1429 | if (*src == '\\') { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1430 | src++; |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame^] | 1431 | if (*src != '\0') { |
| 1432 | /* \x -> x */ |
| 1433 | *dst++ = *src++; |
| 1434 | continue; |
| 1435 | } |
| 1436 | /* else: "\<nul>". Do not delete this backslash. |
| 1437 | * Testcase: eval 'echo ok\' |
| 1438 | */ |
| 1439 | *dst++ = '\\'; |
| 1440 | /* fallthrough */ |
| 1441 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1442 | if ((*dst++ = *src++) == '\0') |
| 1443 | break; |
| 1444 | } |
| 1445 | return dst; |
| 1446 | } |
| 1447 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1448 | 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] | 1449 | { |
| 1450 | int i; |
| 1451 | unsigned count1; |
| 1452 | unsigned count2; |
| 1453 | char **v; |
| 1454 | |
| 1455 | v = strings; |
| 1456 | count1 = 0; |
| 1457 | if (v) { |
| 1458 | while (*v) { |
| 1459 | count1++; |
| 1460 | v++; |
| 1461 | } |
| 1462 | } |
| 1463 | count2 = 0; |
| 1464 | v = add; |
| 1465 | while (*v) { |
| 1466 | count2++; |
| 1467 | v++; |
| 1468 | } |
| 1469 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1470 | v[count1 + count2] = NULL; |
| 1471 | i = count2; |
| 1472 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1473 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1474 | return v; |
| 1475 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1476 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1477 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1478 | { |
| 1479 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1480 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1481 | return ptr; |
| 1482 | } |
| 1483 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1484 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1485 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1486 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1487 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1488 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1489 | { |
| 1490 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1491 | v[0] = add; |
| 1492 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1493 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1494 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1495 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1496 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1497 | { |
| 1498 | char **ptr = add_string_to_strings(strings, add); |
| 1499 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1500 | return ptr; |
| 1501 | } |
| 1502 | #define add_string_to_strings(strings, add) \ |
| 1503 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1504 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1505 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1506 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1507 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1508 | char **v; |
| 1509 | |
| 1510 | if (!strings) |
| 1511 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1512 | v = strings; |
| 1513 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1514 | free(*v); |
| 1515 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1516 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1517 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1520 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1521 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1522 | int newfd; |
| 1523 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1524 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1525 | if (newfd >= 0) { |
| 1526 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1527 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1528 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1529 | if (errno == EBUSY) |
| 1530 | goto repeat; |
| 1531 | if (errno == EINTR) |
| 1532 | goto repeat; |
| 1533 | } |
| 1534 | return newfd; |
| 1535 | } |
| 1536 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1537 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1538 | { |
| 1539 | int newfd; |
| 1540 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1541 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1542 | if (newfd < 0) { |
| 1543 | if (errno == EBUSY) |
| 1544 | goto repeat; |
| 1545 | if (errno == EINTR) |
| 1546 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1547 | /* fd was not open? */ |
| 1548 | if (errno == EBADF) |
| 1549 | return fd; |
| 1550 | xfunc_die(); |
| 1551 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1552 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1553 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1554 | close(fd); |
| 1555 | return newfd; |
| 1556 | } |
| 1557 | |
| 1558 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1559 | /* Manipulating the list of open FILEs */ |
| 1560 | static FILE *remember_FILE(FILE *fp) |
| 1561 | { |
| 1562 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1563 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1564 | n->next = G.FILE_list; |
| 1565 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1566 | n->fp = fp; |
| 1567 | n->fd = fileno(fp); |
| 1568 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1569 | } |
| 1570 | return fp; |
| 1571 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1572 | static void fclose_and_forget(FILE *fp) |
| 1573 | { |
| 1574 | struct FILE_list **pp = &G.FILE_list; |
| 1575 | while (*pp) { |
| 1576 | struct FILE_list *cur = *pp; |
| 1577 | if (cur->fp == fp) { |
| 1578 | *pp = cur->next; |
| 1579 | free(cur); |
| 1580 | break; |
| 1581 | } |
| 1582 | pp = &cur->next; |
| 1583 | } |
| 1584 | fclose(fp); |
| 1585 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1586 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1587 | { |
| 1588 | struct FILE_list *fl = G.FILE_list; |
| 1589 | while (fl) { |
| 1590 | if (fd == fl->fd) { |
| 1591 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1592 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1593 | 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] | 1594 | return 1; |
| 1595 | } |
| 1596 | fl = fl->next; |
| 1597 | } |
| 1598 | return 0; |
| 1599 | } |
| 1600 | static void restore_redirected_FILEs(void) |
| 1601 | { |
| 1602 | struct FILE_list *fl = G.FILE_list; |
| 1603 | while (fl) { |
| 1604 | int should_be = fileno(fl->fp); |
| 1605 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1606 | 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] | 1607 | xmove_fd(fl->fd, should_be); |
| 1608 | fl->fd = should_be; |
| 1609 | } |
| 1610 | fl = fl->next; |
| 1611 | } |
| 1612 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1613 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1614 | static void close_all_FILE_list(void) |
| 1615 | { |
| 1616 | struct FILE_list *fl = G.FILE_list; |
| 1617 | while (fl) { |
| 1618 | /* fclose would also free FILE object. |
| 1619 | * It is disastrous if we share memory with a vforked parent. |
| 1620 | * I'm not sure we never come here after vfork. |
| 1621 | * Therefore just close fd, nothing more. |
| 1622 | */ |
| 1623 | /*fclose(fl->fp); - unsafe */ |
| 1624 | close(fl->fd); |
| 1625 | fl = fl->next; |
| 1626 | } |
| 1627 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1628 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1629 | static int fd_in_FILEs(int fd) |
| 1630 | { |
| 1631 | struct FILE_list *fl = G.FILE_list; |
| 1632 | while (fl) { |
| 1633 | if (fl->fd == fd) |
| 1634 | return 1; |
| 1635 | fl = fl->next; |
| 1636 | } |
| 1637 | return 0; |
| 1638 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1639 | |
| 1640 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1641 | /* Helpers for setting new $n and restoring them back |
| 1642 | */ |
| 1643 | typedef struct save_arg_t { |
| 1644 | char *sv_argv0; |
| 1645 | char **sv_g_argv; |
| 1646 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1647 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1648 | } save_arg_t; |
| 1649 | |
| 1650 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1651 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1652 | sv->sv_argv0 = argv[0]; |
| 1653 | sv->sv_g_argv = G.global_argv; |
| 1654 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1655 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1656 | |
| 1657 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1658 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1659 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1660 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1661 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1665 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1666 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1667 | if (G.global_args_malloced) { |
| 1668 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1669 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1670 | while (*++pp) /* note: does not free $0 */ |
| 1671 | free(*pp); |
| 1672 | free(G.global_argv); |
| 1673 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1674 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1675 | argv[0] = sv->sv_argv0; |
| 1676 | G.global_argv = sv->sv_g_argv; |
| 1677 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1678 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1682 | /* Basic theory of signal handling in shell |
| 1683 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1684 | * This does not describe what hush does, rather, it is current understanding |
| 1685 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1686 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1687 | * |
| 1688 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1689 | * is finished or backgrounded. It is the same in interactive and |
| 1690 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1691 | * 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] | 1692 | * ^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] | 1693 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1694 | * pipe. |
| 1695 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1696 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1697 | * or by SIGINT in interactive shell. |
| 1698 | * |
| 1699 | * Trap handlers will execute even within trap handlers. (right?) |
| 1700 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1701 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1702 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1703 | * |
| 1704 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1705 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1706 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1707 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1708 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1709 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1710 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1711 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1712 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1713 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1714 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1715 | * |
| 1716 | * SIGQUIT: ignore |
| 1717 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1718 | * SIGHUP (interactive): |
| 1719 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1720 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1721 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1722 | * that all pipe members are stopped. Try this in bash: |
| 1723 | * while :; do :; done - ^Z does not background it |
| 1724 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1725 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1726 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1727 | * to interactive shell while shell is waiting for a pipe, |
| 1728 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1729 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1730 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1731 | * Example 2: this does not wait and does not execute ls: |
| 1732 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1733 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1734 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1735 | * Example 4: this does not wait and does not execute ls: |
| 1736 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1737 | * |
| 1738 | * (What happens to signals which are IGN on shell start?) |
| 1739 | * (What happens with signal mask on shell start?) |
| 1740 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1741 | * Old implementation |
| 1742 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1743 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1744 | * We block all signals which we don't want to take action immediately, |
| 1745 | * i.e. we block all signals which need to have special handling as described |
| 1746 | * above, and all signals which have traps set. |
| 1747 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1748 | * and act on them. |
| 1749 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1750 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1751 | * sigset_t blocked_set: current blocked signal set |
| 1752 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1753 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1754 | * 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] | 1755 | * "trap 'cmd' SIGxxx": |
| 1756 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1757 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1758 | * unblock signals with special interactive handling |
| 1759 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1760 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1761 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1762 | * 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] | 1763 | * 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] | 1764 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1765 | * 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] | 1766 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1767 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1768 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1769 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1770 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1771 | * 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] | 1772 | * 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] | 1773 | * |
| 1774 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1775 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1776 | * masked signals are not visible! |
| 1777 | * |
| 1778 | * New implementation |
| 1779 | * ================== |
| 1780 | * We record each signal we are interested in by installing signal handler |
| 1781 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1782 | * We are interested in: signals which need to have special handling |
| 1783 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1784 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1785 | * After each pipe execution, we extract any pending signals |
| 1786 | * and act on them. |
| 1787 | * |
| 1788 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1789 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1790 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1791 | * sigset_t pending_set: set of sigs we received. |
| 1792 | * |
| 1793 | * "trap - SIGxxx": |
| 1794 | * if sig is in special_sig_mask, set handler back to: |
| 1795 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1796 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1797 | * else: set handler back to SIG_DFL |
| 1798 | * "trap 'cmd' SIGxxx": |
| 1799 | * set handler to record_pending_signo. |
| 1800 | * "trap '' SIGxxx": |
| 1801 | * set handler to SIG_IGN. |
| 1802 | * after [v]fork, if we plan to be a shell: |
| 1803 | * set signals with special interactive handling to SIG_DFL |
| 1804 | * (because child shell is not interactive), |
| 1805 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1806 | * after [v]fork, if we plan to exec: |
| 1807 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1808 | * |
| 1809 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1810 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1811 | * |
| 1812 | * Note (compat): |
| 1813 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1814 | * are set to the default actions". bash interprets it so that traps which |
| 1815 | * 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] | 1816 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1817 | enum { |
| 1818 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1819 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1820 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1821 | | (1 << SIGHUP) |
| 1822 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1823 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1824 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1825 | | (1 << SIGTTIN) |
| 1826 | | (1 << SIGTTOU) |
| 1827 | | (1 << SIGTSTP) |
| 1828 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1829 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1830 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1831 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1832 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1833 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1834 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1835 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1836 | if (sig == SIGCHLD) { |
| 1837 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1838 | //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] | 1839 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1840 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1841 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1842 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1843 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1844 | { |
| 1845 | struct sigaction old_sa; |
| 1846 | |
| 1847 | /* We could use signal() to install handlers... almost: |
| 1848 | * except that we need to mask ALL signals while handlers run. |
| 1849 | * I saw signal nesting in strace, race window isn't small. |
| 1850 | * SA_RESTART is also needed, but in Linux, signal() |
| 1851 | * sets SA_RESTART too. |
| 1852 | */ |
| 1853 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1854 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1855 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1856 | G.sa.sa_handler = handler; |
| 1857 | sigaction(sig, &G.sa, &old_sa); |
| 1858 | return old_sa.sa_handler; |
| 1859 | } |
| 1860 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1861 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1862 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1863 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1864 | static void restore_ttypgrp_and__exit(void) |
| 1865 | { |
| 1866 | /* xfunc has failed! die die die */ |
| 1867 | /* no EXIT traps, this is an escape hatch! */ |
| 1868 | G.exiting = 1; |
| 1869 | hush_exit(xfunc_error_retval); |
| 1870 | } |
| 1871 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1872 | #if ENABLE_HUSH_JOB |
| 1873 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1874 | /* Needed only on some libc: |
| 1875 | * It was observed that on exit(), fgetc'ed buffered data |
| 1876 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1877 | * With the net effect that even after fork(), not vfork(), |
| 1878 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1879 | * noexec_applet_here |
| 1880 | * echo END_OF_SCRIPT |
| 1881 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1882 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1883 | * 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] | 1884 | * and in `cmd` handling. |
| 1885 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1886 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1887 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1888 | static void fflush_and__exit(void) |
| 1889 | { |
| 1890 | fflush_all(); |
| 1891 | _exit(xfunc_error_retval); |
| 1892 | } |
| 1893 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1894 | /* 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] | 1895 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1896 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1897 | # 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] | 1898 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1899 | /* Restores tty foreground process group, and exits. |
| 1900 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1901 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1902 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1903 | * We also call it if xfunc is exiting. |
| 1904 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1905 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1906 | static void sigexit(int sig) |
| 1907 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1908 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1909 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1910 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1911 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1912 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1913 | */ |
| 1914 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1915 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1916 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1917 | |
| 1918 | /* Not a signal, just exit */ |
| 1919 | if (sig <= 0) |
| 1920 | _exit(- sig); |
| 1921 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1922 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1923 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1924 | #else |
| 1925 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1926 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1927 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1928 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1929 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1930 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1931 | static sighandler_t pick_sighandler(unsigned sig) |
| 1932 | { |
| 1933 | sighandler_t handler = SIG_DFL; |
| 1934 | if (sig < sizeof(unsigned)*8) { |
| 1935 | unsigned sigmask = (1 << sig); |
| 1936 | |
| 1937 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1938 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1939 | if (G_fatal_sig_mask & sigmask) |
| 1940 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1941 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1942 | #endif |
| 1943 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1944 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1945 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1946 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1947 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1948 | * in an endless loop when we try to do some |
| 1949 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1950 | */ |
| 1951 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1952 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1953 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1954 | } |
| 1955 | return handler; |
| 1956 | } |
| 1957 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1958 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1959 | static void hush_exit(int exitcode) |
| 1960 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1961 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1962 | save_history(G.line_input_state); |
| 1963 | #endif |
| 1964 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1965 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1966 | 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] | 1967 | char *argv[3]; |
| 1968 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1969 | 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] | 1970 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1971 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1972 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1973 | * "trap" will still show it, if executed |
| 1974 | * in the handler */ |
| 1975 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1976 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1977 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1978 | #if ENABLE_FEATURE_CLEAN_UP |
| 1979 | { |
| 1980 | struct variable *cur_var; |
| 1981 | if (G.cwd != bb_msg_unknown) |
| 1982 | free((char*)G.cwd); |
| 1983 | cur_var = G.top_var; |
| 1984 | while (cur_var) { |
| 1985 | struct variable *tmp = cur_var; |
| 1986 | if (!cur_var->max_len) |
| 1987 | free(cur_var->varstr); |
| 1988 | cur_var = cur_var->next; |
| 1989 | free(tmp); |
| 1990 | } |
| 1991 | } |
| 1992 | #endif |
| 1993 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1994 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1995 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1996 | sigexit(- (exitcode & 0xff)); |
| 1997 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1998 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1999 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 2002 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2003 | //TODO: return a mask of ALL handled sigs? |
| 2004 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2005 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2006 | int last_sig = 0; |
| 2007 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2008 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2009 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 2010 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2011 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2012 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2013 | sig = 0; |
| 2014 | do { |
| 2015 | sig++; |
| 2016 | if (sigismember(&G.pending_set, sig)) { |
| 2017 | sigdelset(&G.pending_set, sig); |
| 2018 | goto got_sig; |
| 2019 | } |
| 2020 | } while (sig < NSIG); |
| 2021 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2022 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2023 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2024 | 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] | 2025 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2026 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2027 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2028 | char *argv[3]; |
| 2029 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2030 | argv[1] = xstrdup(G_traps[sig]); |
| 2031 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2032 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2033 | save_rcode = G.last_exitcode; |
| 2034 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2035 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2036 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2037 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2038 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2039 | } /* else: "" trap, ignoring signal */ |
| 2040 | continue; |
| 2041 | } |
| 2042 | /* not a trap: special action */ |
| 2043 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2044 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2045 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2046 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2047 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2048 | break; |
| 2049 | #if ENABLE_HUSH_JOB |
| 2050 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2051 | //TODO: why are we doing this? ash and dash don't do this, |
| 2052 | //they have no handler for SIGHUP at all, |
| 2053 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2054 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2055 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2056 | /* bash is observed to signal whole process groups, |
| 2057 | * not individual processes */ |
| 2058 | for (job = G.job_list; job; job = job->next) { |
| 2059 | if (job->pgrp <= 0) |
| 2060 | continue; |
| 2061 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2062 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2063 | kill(- job->pgrp, SIGCONT); |
| 2064 | } |
| 2065 | sigexit(SIGHUP); |
| 2066 | } |
| 2067 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2068 | #if ENABLE_HUSH_FAST |
| 2069 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2070 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2071 | G.count_SIGCHLD++; |
| 2072 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2073 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2074 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2075 | * This simplifies wait builtin a bit. |
| 2076 | */ |
| 2077 | break; |
| 2078 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2079 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2080 | 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] | 2081 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2082 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2083 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2084 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2085 | * in interactive shell, because TERM is ignored. |
| 2086 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
| 2089 | } |
| 2090 | return last_sig; |
| 2091 | } |
| 2092 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2093 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2094 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2095 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2096 | if (force || G.cwd == NULL) { |
| 2097 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2098 | * we must not try to free(bb_msg_unknown) */ |
| 2099 | if (G.cwd == bb_msg_unknown) |
| 2100 | G.cwd = NULL; |
| 2101 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2102 | if (!G.cwd) |
| 2103 | G.cwd = bb_msg_unknown; |
| 2104 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2105 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2108 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2109 | /* |
| 2110 | * Shell and environment variable support |
| 2111 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2112 | 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] | 2113 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2114 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2115 | struct variable *cur; |
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 | pp = &G.top_var; |
| 2118 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2119 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2120 | return pp; |
| 2121 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2122 | } |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2126 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2127 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2128 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2129 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2130 | |
| 2131 | if (G.expanded_assignments) { |
| 2132 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2133 | while (*cpp) { |
| 2134 | char *cp = *cpp; |
| 2135 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2136 | return cp + len + 1; |
| 2137 | cpp++; |
| 2138 | } |
| 2139 | } |
| 2140 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2141 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2142 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2143 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2144 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2145 | if (strcmp(name, "PPID") == 0) |
| 2146 | return utoa(G.root_ppid); |
| 2147 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2148 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2149 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2150 | return utoa(next_random(&G.random_gen)); |
| 2151 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2152 | return NULL; |
| 2153 | } |
| 2154 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2155 | static void handle_changed_special_names(const char *name, unsigned name_len) |
| 2156 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2157 | if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 2158 | && name_len == 3 && name[0] == 'P' && name[1] == 'S' |
| 2159 | ) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2160 | cmdedit_update_prompt(); |
| 2161 | return; |
| 2162 | } |
| 2163 | |
| 2164 | if ((ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) |
| 2165 | && name_len == 6 |
| 2166 | ) { |
| 2167 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2168 | if (strncmp(name, "LINENO", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2169 | G.lineno_var = NULL; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2170 | return; |
| 2171 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2172 | #endif |
| 2173 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2174 | if (strncmp(name, "OPTIND", 6) == 0) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2175 | G.getopt_count = 0; |
Denys Vlasenko | 00bd767 | 2018-04-06 14:57:53 +0200 | [diff] [blame] | 2176 | return; |
| 2177 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2178 | #endif |
| 2179 | } |
| 2180 | } |
| 2181 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2182 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2183 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2184 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2185 | #define SETFLAG_EXPORT (1 << 0) |
| 2186 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2187 | #define SETFLAG_MAKE_RO (1 << 2) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2188 | #define SETFLAG_VARLVL_SHIFT 3 |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2189 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2190 | { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2191 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2192 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2193 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2194 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2195 | int name_len; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2196 | unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2197 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2198 | eq_sign = strchr(str, '='); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2199 | if (HUSH_DEBUG && !eq_sign) |
| 2200 | bb_error_msg_and_die("BUG in setvar"); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2201 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2202 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2203 | cur_pp = &G.top_var; |
| 2204 | while ((cur = *cur_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2205 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2206 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2207 | continue; |
| 2208 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2209 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2210 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2211 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2212 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2213 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2214 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2215 | //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] | 2216 | return -1; |
| 2217 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2218 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2219 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2220 | *eq_sign = '\0'; |
| 2221 | unsetenv(str); |
| 2222 | *eq_sign = '='; |
| 2223 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2224 | if (cur->var_nest_level < local_lvl) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2225 | /* bash 3.2.33(1) and exported vars: |
| 2226 | * # export z=z |
| 2227 | * # f() { local z=a; env | grep ^z; } |
| 2228 | * # f |
| 2229 | * z=a |
| 2230 | * # env | grep ^z |
| 2231 | * z=z |
| 2232 | */ |
| 2233 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2234 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2235 | /* New variable is local ("local VAR=VAL" or |
| 2236 | * "VAR=VAL cmd") |
| 2237 | * and existing one is global, or local |
| 2238 | * on a lower level that new one. |
| 2239 | * Remove it from global variable list: |
| 2240 | */ |
| 2241 | *cur_pp = cur->next; |
| 2242 | if (G.shadowed_vars_pp) { |
| 2243 | /* Save in "shadowed" list */ |
| 2244 | debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n", |
| 2245 | cur->flg_export ? "exported " : "", |
| 2246 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2247 | ); |
| 2248 | cur->next = *G.shadowed_vars_pp; |
| 2249 | *G.shadowed_vars_pp = cur; |
| 2250 | } else { |
| 2251 | /* Came from pseudo_exec_argv(), no need to save: delete it */ |
| 2252 | debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n", |
| 2253 | cur->flg_export ? "exported " : "", |
| 2254 | cur->varstr, cur->var_nest_level, str, local_lvl |
| 2255 | ); |
| 2256 | if (cur->max_len == 0) /* allocated "VAR=VAL"? */ |
| 2257 | free_me = cur->varstr; /* then free it later */ |
| 2258 | free(cur); |
| 2259 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2260 | break; |
| 2261 | } |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2262 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2263 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2264 | debug_printf_env("assignement '%s' does not change anything\n", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2265 | free_and_exp: |
| 2266 | free(str); |
| 2267 | goto exp; |
| 2268 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2269 | |
| 2270 | /* Replace the value in the found "struct variable" */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2271 | if (cur->max_len != 0) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2272 | if (cur->max_len >= strnlen(str, cur->max_len + 1)) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2273 | /* This one is from startup env, reuse space */ |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2274 | debug_printf_env("reusing startup env for '%s'\n", str); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2275 | strcpy(cur->varstr, str); |
| 2276 | goto free_and_exp; |
| 2277 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2278 | /* Can't reuse */ |
| 2279 | cur->max_len = 0; |
| 2280 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2281 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2282 | /* max_len == 0 signifies "malloced" var, which we can |
| 2283 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2284 | * if cur->flg_export is 1, it is in the environment. |
| 2285 | * We should either unsetenv+free, or wait until putenv, |
| 2286 | * then putenv(new)+free(old). |
| 2287 | */ |
| 2288 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2289 | goto set_str_and_exp; |
| 2290 | } |
| 2291 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2292 | /* Not found or shadowed - create new variable struct */ |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2293 | 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] | 2294 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 2295 | cur->var_nest_level = local_lvl; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2296 | cur->next = *cur_pp; |
| 2297 | *cur_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2298 | |
| 2299 | set_str_and_exp: |
| 2300 | cur->varstr = str; |
| 2301 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2302 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2303 | if (flags & SETFLAG_MAKE_RO) { |
| 2304 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2305 | } |
| 2306 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2307 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2308 | cur->flg_export = 1; |
| 2309 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2310 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2311 | cur->flg_export = 0; |
| 2312 | /* unsetenv was already done */ |
| 2313 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2314 | int i; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2315 | 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] | 2316 | i = putenv(cur->varstr); |
| 2317 | /* only now we can free old exported malloced string */ |
| 2318 | free(free_me); |
| 2319 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2320 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2321 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2322 | free(free_me); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2323 | |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2324 | handle_changed_special_names(cur->varstr, name_len - 1); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2325 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2326 | return 0; |
| 2327 | } |
| 2328 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2329 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2330 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2331 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2332 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2333 | } |
| 2334 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2335 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2336 | { |
| 2337 | struct variable *cur; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2338 | struct variable **cur_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2339 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2340 | cur_pp = &G.top_var; |
| 2341 | while ((cur = *cur_pp) != NULL) { |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2342 | if (strncmp(cur->varstr, name, name_len) == 0 |
| 2343 | && cur->varstr[name_len] == '=' |
| 2344 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2345 | if (cur->flg_read_only) { |
| 2346 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2347 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2348 | } |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2349 | |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2350 | *cur_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2351 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2352 | bb_unsetenv(cur->varstr); |
| 2353 | if (!cur->max_len) |
| 2354 | free(cur->varstr); |
| 2355 | free(cur); |
Denys Vlasenko | cf079ff | 2018-04-06 14:50:12 +0200 | [diff] [blame] | 2356 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2357 | break; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2358 | } |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2359 | cur_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2360 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2361 | |
| 2362 | /* Handle "unset PS1" et al even if did not find the variable to unset */ |
| 2363 | handle_changed_special_names(name, name_len); |
| 2364 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2365 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2368 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2369 | static int unset_local_var(const char *name) |
| 2370 | { |
| 2371 | return unset_local_var_len(name, strlen(name)); |
| 2372 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2373 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2374 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2375 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2376 | 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] | 2377 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2378 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2379 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2380 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2381 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2382 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2383 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2384 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2385 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2386 | */ |
| 2387 | static void add_vars(struct variable *var) |
| 2388 | { |
| 2389 | struct variable *next; |
| 2390 | |
| 2391 | while (var) { |
| 2392 | next = var->next; |
| 2393 | var->next = G.top_var; |
| 2394 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2395 | if (var->flg_export) { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2396 | 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] | 2397 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2398 | } else { |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2399 | 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] | 2400 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2401 | var = next; |
| 2402 | } |
| 2403 | } |
| 2404 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2405 | /* We put strings[i] into variable table and possibly putenv them. |
| 2406 | * If variable is read only, we can free the strings[i] |
| 2407 | * which attempts to overwrite it. |
| 2408 | * The strings[] vector itself is freed. |
| 2409 | */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2410 | static void set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2411 | { |
| 2412 | char **s; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2413 | |
| 2414 | if (!strings) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2415 | return; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2416 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2417 | s = strings; |
| 2418 | while (*s) { |
| 2419 | struct variable *var_p; |
| 2420 | struct variable **var_pp; |
| 2421 | char *eq; |
| 2422 | |
| 2423 | eq = strchr(*s, '='); |
| 2424 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2425 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2426 | if (var_pp) { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2427 | var_p = *var_pp; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2428 | if (var_p->flg_read_only) { |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2429 | char **p; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2430 | bb_error_msg("%s: readonly variable", *s); |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2431 | /* |
| 2432 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2433 | * If VAR is readonly, leaving it in the list |
| 2434 | * after asssignment error (msg above) |
| 2435 | * causes doubled error message later, on unset. |
| 2436 | */ |
| 2437 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2438 | free(*s); |
| 2439 | p = s; |
| 2440 | do { *p = p[1]; p++; } while (*p); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2441 | goto next; |
| 2442 | } |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 2443 | /* below, set_local_var() with nest level will |
| 2444 | * "shadow" (remove) this variable from |
| 2445 | * global linked list. |
| 2446 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2447 | } |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 2448 | debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level); |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2449 | set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT); |
| 2450 | } else if (HUSH_DEBUG) { |
| 2451 | bb_error_msg_and_die("BUG in varexp4"); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2452 | } |
| 2453 | s++; |
Denys Vlasenko | 6140780 | 2018-04-04 21:14:28 +0200 | [diff] [blame] | 2454 | next: ; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2455 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 2456 | free(strings); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | |
| 2460 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2461 | * Unicode helper |
| 2462 | */ |
| 2463 | static void reinit_unicode_for_hush(void) |
| 2464 | { |
| 2465 | /* Unicode support should be activated even if LANG is set |
| 2466 | * _during_ shell execution, not only if it was set when |
| 2467 | * shell was started. Therefore, re-check LANG every time: |
| 2468 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2469 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2470 | || ENABLE_UNICODE_USING_LOCALE |
| 2471 | ) { |
| 2472 | const char *s = get_local_var_value("LC_ALL"); |
| 2473 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2474 | if (!s) s = get_local_var_value("LANG"); |
| 2475 | reinit_unicode(s); |
| 2476 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2479 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2480 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2481 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2482 | |
| 2483 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2484 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2485 | * echo $P\ |
| 2486 | * \ |
| 2487 | * AT\ |
| 2488 | * H\ |
| 2489 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2490 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2491 | */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2492 | # if ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2493 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2494 | { |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2495 | G.PS1 = get_local_var_value("PS1"); |
| 2496 | if (G.PS1 == NULL) |
| 2497 | G.PS1 = ""; |
| 2498 | G.PS2 = get_local_var_value("PS2"); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2499 | if (G.PS2 == NULL) |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2500 | G.PS2 = ""; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2501 | } |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2502 | # endif |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2503 | static const char *setup_prompt_string(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2504 | { |
| 2505 | const char *prompt_str; |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2506 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2507 | debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2508 | |
| 2509 | IF_FEATURE_EDITING_FANCY_PROMPT( prompt_str = G.PS2;) |
| 2510 | IF_NOT_FEATURE_EDITING_FANCY_PROMPT(prompt_str = "> ";) |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2511 | if (G.promptmode == 0) { /* PS1 */ |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2512 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2513 | /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */ |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2514 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2515 | /* bash uses $PWD value, even if it is set by user. |
| 2516 | * It uses current dir only if PWD is unset. |
| 2517 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2518 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 2519 | } |
| 2520 | prompt_str = G.PS1; |
| 2521 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2522 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2523 | return prompt_str; |
| 2524 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2525 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2526 | { |
| 2527 | int r; |
| 2528 | const char *prompt_str; |
| 2529 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2530 | prompt_str = setup_prompt_string(); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2531 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2532 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2533 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2534 | if (G.flag_SIGINT) { |
| 2535 | /* There was ^C'ed, make it look prettier: */ |
| 2536 | bb_putchar('\n'); |
| 2537 | G.flag_SIGINT = 0; |
| 2538 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2539 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2540 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2541 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2542 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2543 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2544 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2545 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2546 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2547 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2548 | if (r != 0 && !G.flag_SIGINT) |
| 2549 | break; |
| 2550 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2551 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2552 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2553 | G.last_exitcode = 128 + SIGINT; |
| 2554 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2555 | if (r < 0) { |
| 2556 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2557 | i->p = NULL; |
| 2558 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2559 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2560 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2561 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2562 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2563 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2564 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2565 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2566 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2567 | /* Why check_and_run_traps here? Try this interactively: |
| 2568 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2569 | * $ <[enter], repeatedly...> |
| 2570 | * Without check_and_run_traps, handler never runs. |
| 2571 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2572 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2573 | fputs(prompt_str, stdout); |
| 2574 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2575 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2576 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2577 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2578 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2579 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2580 | * it generates SIGINT as usual. |
| 2581 | */ |
| 2582 | check_and_run_traps(); |
| 2583 | if (G.flag_SIGINT) |
| 2584 | G.last_exitcode = 128 + SIGINT; |
| 2585 | if (r != '\0') |
| 2586 | break; |
| 2587 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2588 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2589 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2590 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2591 | /* This is the magic location that prints prompts |
| 2592 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2593 | static int fgetc_interactive(struct in_str *i) |
| 2594 | { |
| 2595 | int ch; |
| 2596 | /* If it's interactive stdin, get new line. */ |
| 2597 | if (G_interactive_fd && i->file == stdin) { |
| 2598 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2599 | ch = get_user_input(i); |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 2600 | G.promptmode = 1; /* PS2 */ |
| 2601 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2602 | } else { |
| 2603 | /* Not stdin: script file, sourced file, etc */ |
| 2604 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2605 | } |
| 2606 | return ch; |
| 2607 | } |
| 2608 | #else |
| 2609 | static inline int fgetc_interactive(struct in_str *i) |
| 2610 | { |
| 2611 | int ch; |
| 2612 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2613 | return ch; |
| 2614 | } |
| 2615 | #endif /* INTERACTIVE */ |
| 2616 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2617 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2618 | { |
| 2619 | int ch; |
| 2620 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2621 | if (!i->file) { |
| 2622 | /* string-based in_str */ |
| 2623 | ch = (unsigned char)*i->p; |
| 2624 | if (ch != '\0') { |
| 2625 | i->p++; |
| 2626 | i->last_char = ch; |
| 2627 | return ch; |
| 2628 | } |
| 2629 | return EOF; |
| 2630 | } |
| 2631 | |
| 2632 | /* FILE-based in_str */ |
| 2633 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2634 | #if ENABLE_FEATURE_EDITING |
| 2635 | /* This can be stdin, check line editing char[] buffer */ |
| 2636 | if (i->p && *i->p != '\0') { |
| 2637 | ch = (unsigned char)*i->p++; |
| 2638 | goto out; |
| 2639 | } |
| 2640 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2641 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2642 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2643 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2644 | int ch2 = i->peek_buf[1]; |
| 2645 | i->peek_buf[0] = ch2; |
| 2646 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2647 | goto out; |
| 2648 | i->peek_buf[1] = 0; |
| 2649 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2650 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2651 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2652 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2653 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2654 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2655 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2656 | #if ENABLE_HUSH_LINENO_VAR |
| 2657 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2658 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2659 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2660 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2661 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2662 | return ch; |
| 2663 | } |
| 2664 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2665 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2666 | { |
| 2667 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2668 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2669 | if (!i->file) { |
| 2670 | /* string-based in_str */ |
| 2671 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2672 | return (unsigned char)*i->p; |
| 2673 | } |
| 2674 | |
| 2675 | /* FILE-based in_str */ |
| 2676 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2677 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2678 | /* This can be stdin, check line editing char[] buffer */ |
| 2679 | if (i->p && *i->p != '\0') |
| 2680 | return (unsigned char)*i->p; |
| 2681 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2682 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2683 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2684 | if (ch != 0) |
| 2685 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2686 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2687 | /* Need to get a new char */ |
| 2688 | ch = fgetc_interactive(i); |
| 2689 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2690 | |
| 2691 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2692 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2693 | if (i->p) { |
| 2694 | i->p -= 1; |
| 2695 | return ch; |
| 2696 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2697 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2698 | i->peek_buf[0] = ch; |
| 2699 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2700 | return ch; |
| 2701 | } |
| 2702 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2703 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2704 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2705 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2706 | */ |
| 2707 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2708 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2709 | int ch; |
| 2710 | |
| 2711 | /* There are two cases when i->p[] buffer exists. |
| 2712 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2713 | * (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] | 2714 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2715 | * the peek2 result is in i->p[1]. |
| 2716 | */ |
| 2717 | if (i->p) |
| 2718 | return (unsigned char)i->p[1]; |
| 2719 | |
| 2720 | /* Now we know it is a file-based in_str. */ |
| 2721 | |
| 2722 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2723 | /* Is there 2nd char? */ |
| 2724 | ch = i->peek_buf[1]; |
| 2725 | if (ch == 0) { |
| 2726 | /* We did not read it yet, get it now */ |
| 2727 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2728 | i->peek_buf[1] = ch; |
| 2729 | } |
| 2730 | |
| 2731 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2732 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2735 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2736 | { |
| 2737 | for (;;) { |
| 2738 | int ch, ch2; |
| 2739 | |
| 2740 | ch = i_getch(input); |
| 2741 | if (ch != '\\') |
| 2742 | return ch; |
| 2743 | ch2 = i_peek(input); |
| 2744 | if (ch2 != '\n') |
| 2745 | return ch; |
| 2746 | /* backslash+newline, skip it */ |
| 2747 | i_getch(input); |
| 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2752 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2753 | */ |
| 2754 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2755 | { |
| 2756 | for (;;) { |
| 2757 | int ch, ch2; |
| 2758 | |
| 2759 | ch = i_peek(input); |
| 2760 | if (ch != '\\') |
| 2761 | return ch; |
| 2762 | ch2 = i_peek2(input); |
| 2763 | if (ch2 != '\n') |
| 2764 | return ch; |
| 2765 | /* backslash+newline, skip it */ |
| 2766 | i_getch(input); |
| 2767 | i_getch(input); |
| 2768 | } |
| 2769 | } |
| 2770 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2771 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2772 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2773 | memset(i, 0, sizeof(*i)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2774 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2775 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2779 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2780 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2781 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2782 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2786 | /* |
| 2787 | * o_string support |
| 2788 | */ |
| 2789 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2790 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2791 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2792 | { |
| 2793 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2794 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2795 | if (o->data) |
| 2796 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2799 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2800 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2801 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2802 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2805 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2806 | { |
| 2807 | free(o->data); |
| 2808 | } |
| 2809 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2810 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2811 | { |
| 2812 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2813 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2814 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2815 | } |
| 2816 | } |
| 2817 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2818 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2819 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2820 | 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] | 2821 | if (o->length < o->maxlen) { |
| 2822 | /* likely. avoid o_grow_by() call */ |
| 2823 | add: |
| 2824 | o->data[o->length] = ch; |
| 2825 | o->length++; |
| 2826 | o->data[o->length] = '\0'; |
| 2827 | return; |
| 2828 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2829 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2830 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2833 | #if 0 |
| 2834 | /* Valid only if we know o_string is not empty */ |
| 2835 | static void o_delchr(o_string *o) |
| 2836 | { |
| 2837 | o->length--; |
| 2838 | o->data[o->length] = '\0'; |
| 2839 | } |
| 2840 | #endif |
| 2841 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2842 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2843 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2844 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2845 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2846 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2849 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2850 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2851 | o_addblock(o, str, strlen(str)); |
| 2852 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2853 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2854 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2855 | static void nommu_addchr(o_string *o, int ch) |
| 2856 | { |
| 2857 | if (o) |
| 2858 | o_addchr(o, ch); |
| 2859 | } |
| 2860 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2861 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2862 | #endif |
| 2863 | |
| 2864 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2865 | { |
| 2866 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2869 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2870 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2871 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2872 | * Apparently, on unquoted $v bash still does globbing |
| 2873 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2874 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2875 | * quoting mechanisms on $v expansion side: one protects |
| 2876 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2877 | * We have only second one. |
| 2878 | */ |
| 2879 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2880 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2881 | # define MAYBE_BRACES "{}" |
| 2882 | #else |
| 2883 | # define MAYBE_BRACES "" |
| 2884 | #endif |
| 2885 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2886 | /* My analysis of quoting semantics tells me that state information |
| 2887 | * is associated with a destination, not a source. |
| 2888 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2889 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2890 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2891 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2892 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2893 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2894 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2895 | o_grow_by(o, sz); |
| 2896 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2897 | o->data[o->length] = '\\'; |
| 2898 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2899 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2900 | o->data[o->length] = ch; |
| 2901 | o->length++; |
| 2902 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2905 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2906 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2907 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2908 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2909 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2910 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2911 | sz++; |
| 2912 | o->data[o->length] = '\\'; |
| 2913 | o->length++; |
| 2914 | } |
| 2915 | o_grow_by(o, sz); |
| 2916 | o->data[o->length] = ch; |
| 2917 | o->length++; |
| 2918 | o->data[o->length] = '\0'; |
| 2919 | } |
| 2920 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2921 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2922 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2923 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2924 | char ch; |
| 2925 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2926 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2927 | if (ordinary_cnt > len) /* paranoia */ |
| 2928 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2929 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2930 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2931 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2932 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2933 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2934 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2935 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2936 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2937 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2938 | sz++; |
| 2939 | o->data[o->length] = '\\'; |
| 2940 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2941 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2942 | o_grow_by(o, sz); |
| 2943 | o->data[o->length] = ch; |
| 2944 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2945 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2946 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2949 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2950 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2951 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2952 | o_addblock(o, str, len); |
| 2953 | return; |
| 2954 | } |
| 2955 | o_addqblock(o, str, len); |
| 2956 | } |
| 2957 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2958 | static void o_addQstr(o_string *o, const char *str) |
| 2959 | { |
| 2960 | o_addQblock(o, str, strlen(str)); |
| 2961 | } |
| 2962 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2963 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2964 | * 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] | 2965 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2966 | * list[i] contains an INDEX (int!) into this string data. |
| 2967 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2968 | * but list[i]'s need not be modified. |
| 2969 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2970 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2971 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2972 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2973 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2974 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2975 | { |
| 2976 | char **list = (char**)o->data; |
| 2977 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2978 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2979 | |
| 2980 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2981 | 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] | 2982 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2983 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2984 | o->has_quoted_part, |
| 2985 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2986 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2987 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2988 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2989 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2990 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2991 | i++; |
| 2992 | } |
| 2993 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2994 | 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] | 2995 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2996 | 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] | 2997 | } |
| 2998 | } |
| 2999 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 3000 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3001 | #endif |
| 3002 | |
| 3003 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 3004 | * in list[n] so that it points past last stored byte so far. |
| 3005 | * It returns n+1. */ |
| 3006 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3007 | { |
| 3008 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3009 | int string_start; |
| 3010 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3011 | |
| 3012 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3013 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3014 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3015 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3016 | 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] | 3017 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 3018 | o->maxlen += 0x10 * sizeof(list[0]); |
| 3019 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 3020 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3021 | memmove(list + n + 0x10, list + n, string_len); |
| 3022 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3023 | } else { |
| 3024 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 3025 | n, string_len, string_start); |
| 3026 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3027 | } else { |
| 3028 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 3029 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 3030 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3031 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 3032 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3033 | o->has_empty_slot = 0; |
| 3034 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 3035 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3036 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3037 | return n + 1; |
| 3038 | } |
| 3039 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3040 | /* "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] | 3041 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3042 | { |
| 3043 | char **list = (char**)o->data; |
| 3044 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3045 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3046 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3049 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3050 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3051 | * first, it processes even {a} (no commas), second, |
| 3052 | * 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] | 3053 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3054 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3055 | |
| 3056 | /* Helper */ |
| 3057 | static int glob_needed(const char *s) |
| 3058 | { |
| 3059 | while (*s) { |
| 3060 | if (*s == '\\') { |
| 3061 | if (!s[1]) |
| 3062 | return 0; |
| 3063 | s += 2; |
| 3064 | continue; |
| 3065 | } |
| 3066 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3067 | return 1; |
| 3068 | s++; |
| 3069 | } |
| 3070 | return 0; |
| 3071 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3072 | /* Return pointer to next closing brace or to comma */ |
| 3073 | static const char *next_brace_sub(const char *cp) |
| 3074 | { |
| 3075 | unsigned depth = 0; |
| 3076 | cp++; |
| 3077 | while (*cp != '\0') { |
| 3078 | if (*cp == '\\') { |
| 3079 | if (*++cp == '\0') |
| 3080 | break; |
| 3081 | cp++; |
| 3082 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3083 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3084 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3085 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3086 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3087 | depth++; |
| 3088 | } |
| 3089 | |
| 3090 | return *cp != '\0' ? cp : NULL; |
| 3091 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3092 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3093 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3094 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3095 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3096 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3097 | const char *next; |
| 3098 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3099 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3100 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3101 | |
| 3102 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3103 | |
| 3104 | begin = pattern; |
| 3105 | while (1) { |
| 3106 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3107 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3108 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3109 | /* Find the first sub-pattern and at the same time |
| 3110 | * find the rest after the closing brace */ |
| 3111 | next = next_brace_sub(begin); |
| 3112 | if (next == NULL) { |
| 3113 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3114 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3115 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3116 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3117 | /* "{abc}" with no commas - illegal |
| 3118 | * brace expr, disregard and skip it */ |
| 3119 | begin = next + 1; |
| 3120 | continue; |
| 3121 | } |
| 3122 | break; |
| 3123 | } |
| 3124 | if (*begin == '\\' && begin[1] != '\0') |
| 3125 | begin++; |
| 3126 | begin++; |
| 3127 | } |
| 3128 | debug_printf_glob("begin:%s\n", begin); |
| 3129 | debug_printf_glob("next:%s\n", next); |
| 3130 | |
| 3131 | /* Now find the end of the whole brace expression */ |
| 3132 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3133 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3134 | rest = next_brace_sub(rest); |
| 3135 | if (rest == NULL) { |
| 3136 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3137 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3138 | } |
| 3139 | debug_printf_glob("rest:%s\n", rest); |
| 3140 | } |
| 3141 | rest_len = strlen(++rest) + 1; |
| 3142 | |
| 3143 | /* We are sure the brace expression is well-formed */ |
| 3144 | |
| 3145 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3146 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3147 | |
| 3148 | /* We have a brace expression. BEGIN points to the opening {, |
| 3149 | * NEXT points past the terminator of the first element, and REST |
| 3150 | * points past the final }. We will accumulate result names from |
| 3151 | * recursive runs for each brace alternative in the buffer using |
| 3152 | * GLOB_APPEND. */ |
| 3153 | |
| 3154 | p = begin + 1; |
| 3155 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3156 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3157 | memcpy( |
| 3158 | mempcpy( |
| 3159 | mempcpy(new_pattern_buf, |
| 3160 | /* We know the prefix for all sub-patterns */ |
| 3161 | pattern, begin - pattern), |
| 3162 | p, next - p), |
| 3163 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3164 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3165 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3166 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3167 | */ |
| 3168 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3169 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3170 | /* We saw the last entry */ |
| 3171 | break; |
| 3172 | } |
| 3173 | p = next + 1; |
| 3174 | next = next_brace_sub(next); |
| 3175 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3176 | free(new_pattern_buf); |
| 3177 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3178 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3179 | simple_glob: |
| 3180 | { |
| 3181 | int gr; |
| 3182 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3183 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3184 | memset(&globdata, 0, sizeof(globdata)); |
| 3185 | gr = glob(pattern, 0, NULL, &globdata); |
| 3186 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3187 | if (gr != 0) { |
| 3188 | if (gr == GLOB_NOMATCH) { |
| 3189 | globfree(&globdata); |
| 3190 | /* NB: garbles parameter */ |
| 3191 | unbackslash(pattern); |
| 3192 | o_addstr_with_NUL(o, pattern); |
| 3193 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3194 | return o_save_ptr_helper(o, n); |
| 3195 | } |
| 3196 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3197 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3198 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3199 | * but we didn't specify it. Paranoia again. */ |
| 3200 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3201 | } |
| 3202 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3203 | char **argv = globdata.gl_pathv; |
| 3204 | while (1) { |
| 3205 | o_addstr_with_NUL(o, *argv); |
| 3206 | n = o_save_ptr_helper(o, n); |
| 3207 | argv++; |
| 3208 | if (!*argv) |
| 3209 | break; |
| 3210 | } |
| 3211 | } |
| 3212 | globfree(&globdata); |
| 3213 | } |
| 3214 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3215 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3216 | /* Performs globbing on last list[], |
| 3217 | * saving each result as a new list[]. |
| 3218 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3219 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3220 | { |
| 3221 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3222 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3223 | 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] | 3224 | if (!o->data) |
| 3225 | return o_save_ptr_helper(o, n); |
| 3226 | pattern = o->data + o_get_last_ptr(o, n); |
| 3227 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3228 | if (!glob_needed(pattern)) { |
| 3229 | /* unbackslash last string in o in place, fix length */ |
| 3230 | o->length = unbackslash(pattern) - o->data; |
| 3231 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3232 | return o_save_ptr_helper(o, n); |
| 3233 | } |
| 3234 | |
| 3235 | copy = xstrdup(pattern); |
| 3236 | /* "forget" pattern in o */ |
| 3237 | o->length = pattern - o->data; |
| 3238 | n = glob_brace(copy, o, n); |
| 3239 | free(copy); |
| 3240 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3241 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3242 | return n; |
| 3243 | } |
| 3244 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3245 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3246 | |
| 3247 | /* Helper */ |
| 3248 | static int glob_needed(const char *s) |
| 3249 | { |
| 3250 | while (*s) { |
| 3251 | if (*s == '\\') { |
| 3252 | if (!s[1]) |
| 3253 | return 0; |
| 3254 | s += 2; |
| 3255 | continue; |
| 3256 | } |
| 3257 | if (*s == '*' || *s == '[' || *s == '?') |
| 3258 | return 1; |
| 3259 | s++; |
| 3260 | } |
| 3261 | return 0; |
| 3262 | } |
| 3263 | /* Performs globbing on last list[], |
| 3264 | * saving each result as a new list[]. |
| 3265 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3266 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3267 | { |
| 3268 | glob_t globdata; |
| 3269 | int gr; |
| 3270 | char *pattern; |
| 3271 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3272 | 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] | 3273 | if (!o->data) |
| 3274 | return o_save_ptr_helper(o, n); |
| 3275 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3276 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3277 | if (!glob_needed(pattern)) { |
| 3278 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3279 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3280 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3281 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3282 | return o_save_ptr_helper(o, n); |
| 3283 | } |
| 3284 | |
| 3285 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3286 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3287 | * If we glob "*.\*" and don't find anything, we need |
| 3288 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3289 | * will return "*.\*"! |
| 3290 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3291 | gr = glob(pattern, 0, NULL, &globdata); |
| 3292 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3293 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3294 | if (gr == GLOB_NOMATCH) { |
| 3295 | globfree(&globdata); |
| 3296 | goto literal; |
| 3297 | } |
| 3298 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3299 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3300 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3301 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3302 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3303 | } |
| 3304 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3305 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3306 | /* "forget" pattern in o */ |
| 3307 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3308 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3309 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3310 | n = o_save_ptr_helper(o, n); |
| 3311 | argv++; |
| 3312 | if (!*argv) |
| 3313 | break; |
| 3314 | } |
| 3315 | } |
| 3316 | globfree(&globdata); |
| 3317 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3318 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3319 | return n; |
| 3320 | } |
| 3321 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3322 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3323 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3324 | /* 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] | 3325 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3326 | static int o_save_ptr(o_string *o, int n) |
| 3327 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3328 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3329 | /* If o->has_empty_slot, list[n] was already globbed |
| 3330 | * (if it was requested back then when it was filled) |
| 3331 | * so don't do that again! */ |
| 3332 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3333 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3334 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3335 | return o_save_ptr_helper(o, n); |
| 3336 | } |
| 3337 | |
| 3338 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3339 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3340 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3341 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3342 | int string_start; |
| 3343 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3344 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3345 | if (DEBUG_EXPAND) |
| 3346 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3347 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3348 | list = (char**)o->data; |
| 3349 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3350 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3351 | while (n) { |
| 3352 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3353 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3354 | } |
| 3355 | return list; |
| 3356 | } |
| 3357 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3358 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3359 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3360 | /* Returns pi->next - next pipe in the list */ |
| 3361 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3362 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3363 | struct pipe *next; |
| 3364 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3365 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3366 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3367 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3368 | struct command *command; |
| 3369 | struct redir_struct *r, *rnext; |
| 3370 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3371 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3372 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3373 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3374 | if (DEBUG_CLEAN) { |
| 3375 | int a; |
| 3376 | char **p; |
| 3377 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3378 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3379 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3380 | } |
| 3381 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3382 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3383 | } |
| 3384 | /* not "else if": on syntax error, we may have both! */ |
| 3385 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3386 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3387 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3388 | free_pipe_list(command->group); |
| 3389 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3390 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3391 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3392 | /* else is crucial here. |
| 3393 | * If group != NULL, child_func is meaningless */ |
| 3394 | #if ENABLE_HUSH_FUNCTIONS |
| 3395 | else if (command->child_func) { |
| 3396 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3397 | command->child_func->parent_cmd = NULL; |
| 3398 | } |
| 3399 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3400 | #if !BB_MMU |
| 3401 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3402 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3403 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3404 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3405 | debug_printf_clean(" redirect %d%s", |
| 3406 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3407 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3408 | if (r->rd_filename) { |
| 3409 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3410 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3411 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3412 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3413 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3414 | rnext = r->next; |
| 3415 | free(r); |
| 3416 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3417 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3418 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3419 | 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] | 3420 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3421 | #if ENABLE_HUSH_JOB |
| 3422 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3423 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3424 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3425 | |
| 3426 | next = pi->next; |
| 3427 | free(pi); |
| 3428 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3429 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3430 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3431 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3432 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3433 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3434 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3435 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3436 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3437 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3438 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3439 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3443 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3444 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3445 | #ifndef debug_print_tree |
| 3446 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3447 | { |
| 3448 | static const char *const PIPE[] = { |
| 3449 | [PIPE_SEQ] = "SEQ", |
| 3450 | [PIPE_AND] = "AND", |
| 3451 | [PIPE_OR ] = "OR" , |
| 3452 | [PIPE_BG ] = "BG" , |
| 3453 | }; |
| 3454 | static const char *RES[] = { |
| 3455 | [RES_NONE ] = "NONE" , |
| 3456 | # if ENABLE_HUSH_IF |
| 3457 | [RES_IF ] = "IF" , |
| 3458 | [RES_THEN ] = "THEN" , |
| 3459 | [RES_ELIF ] = "ELIF" , |
| 3460 | [RES_ELSE ] = "ELSE" , |
| 3461 | [RES_FI ] = "FI" , |
| 3462 | # endif |
| 3463 | # if ENABLE_HUSH_LOOPS |
| 3464 | [RES_FOR ] = "FOR" , |
| 3465 | [RES_WHILE] = "WHILE", |
| 3466 | [RES_UNTIL] = "UNTIL", |
| 3467 | [RES_DO ] = "DO" , |
| 3468 | [RES_DONE ] = "DONE" , |
| 3469 | # endif |
| 3470 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3471 | [RES_IN ] = "IN" , |
| 3472 | # endif |
| 3473 | # if ENABLE_HUSH_CASE |
| 3474 | [RES_CASE ] = "CASE" , |
| 3475 | [RES_CASE_IN ] = "CASE_IN" , |
| 3476 | [RES_MATCH] = "MATCH", |
| 3477 | [RES_CASE_BODY] = "CASE_BODY", |
| 3478 | [RES_ESAC ] = "ESAC" , |
| 3479 | # endif |
| 3480 | [RES_XXXX ] = "XXXX" , |
| 3481 | [RES_SNTX ] = "SNTX" , |
| 3482 | }; |
| 3483 | static const char *const CMDTYPE[] = { |
| 3484 | "{}", |
| 3485 | "()", |
| 3486 | "[noglob]", |
| 3487 | # if ENABLE_HUSH_FUNCTIONS |
| 3488 | "func()", |
| 3489 | # endif |
| 3490 | }; |
| 3491 | |
| 3492 | int pin, prn; |
| 3493 | |
| 3494 | pin = 0; |
| 3495 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3496 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3497 | lvl*2, "", |
| 3498 | pin, |
| 3499 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3500 | RES[pi->res_word], |
| 3501 | pi->followup, PIPE[pi->followup] |
| 3502 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3503 | prn = 0; |
| 3504 | while (prn < pi->num_cmds) { |
| 3505 | struct command *command = &pi->cmds[prn]; |
| 3506 | char **argv = command->argv; |
| 3507 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3508 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3509 | lvl*2, "", prn, |
| 3510 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3511 | #if ENABLE_HUSH_LINENO_VAR |
| 3512 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3513 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3514 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3515 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3516 | CMDTYPE[command->cmd_type], |
| 3517 | argv |
| 3518 | # if !BB_MMU |
| 3519 | , " group_as_string:", command->group_as_string |
| 3520 | # else |
| 3521 | , "", "" |
| 3522 | # endif |
| 3523 | ); |
| 3524 | debug_print_tree(command->group, lvl+1); |
| 3525 | prn++; |
| 3526 | continue; |
| 3527 | } |
| 3528 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3529 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3530 | argv++; |
| 3531 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3532 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3533 | prn++; |
| 3534 | } |
| 3535 | pi = pi->next; |
| 3536 | pin++; |
| 3537 | } |
| 3538 | } |
| 3539 | #endif /* debug_print_tree */ |
| 3540 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3541 | static struct pipe *new_pipe(void) |
| 3542 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3543 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3544 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3545 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3546 | return pi; |
| 3547 | } |
| 3548 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3549 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3550 | * if ctx->command is NULL. |
| 3551 | * No errors possible here. |
| 3552 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3553 | static int done_command(struct parse_context *ctx) |
| 3554 | { |
| 3555 | /* The command is really already in the pipe structure, so |
| 3556 | * advance the pipe counter and make a new, null command. */ |
| 3557 | struct pipe *pi = ctx->pipe; |
| 3558 | struct command *command = ctx->command; |
| 3559 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3560 | #if 0 /* Instead we emit error message at run time */ |
| 3561 | if (ctx->pending_redirect) { |
| 3562 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3563 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3564 | ctx->pending_redirect = NULL; |
| 3565 | } |
| 3566 | #endif |
| 3567 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3568 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3569 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3570 | 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] | 3571 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3572 | } |
| 3573 | pi->num_cmds++; |
| 3574 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3575 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3576 | } else { |
| 3577 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3578 | } |
| 3579 | |
| 3580 | /* Only real trickiness here is that the uncommitted |
| 3581 | * command structure is not counted in pi->num_cmds. */ |
| 3582 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3583 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3584 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3585 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3586 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3587 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3588 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3589 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3590 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3591 | } |
| 3592 | |
| 3593 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3594 | { |
| 3595 | int not_null; |
| 3596 | |
| 3597 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3598 | /* Close previous command */ |
| 3599 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3600 | #if HAS_KEYWORDS |
| 3601 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3602 | ctx->ctx_inverted = 0; |
| 3603 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3604 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3605 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3606 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3607 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3608 | * in a backgrounded subshell. |
| 3609 | */ |
| 3610 | struct pipe *pi; |
| 3611 | struct command *command; |
| 3612 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3613 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3614 | pi = ctx->list_head; |
| 3615 | while (pi != ctx->pipe) { |
| 3616 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3617 | goto no_conv; |
| 3618 | pi = pi->next; |
| 3619 | } |
| 3620 | |
| 3621 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3622 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3623 | pi = xzalloc(sizeof(*pi)); |
| 3624 | pi->followup = PIPE_BG; |
| 3625 | pi->num_cmds = 1; |
| 3626 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3627 | command = &pi->cmds[0]; |
| 3628 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3629 | command->cmd_type = CMD_NORMAL; |
| 3630 | command->group = ctx->list_head; |
| 3631 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3632 | command->group_as_string = xstrndup( |
| 3633 | ctx->as_string.data, |
| 3634 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3635 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3636 | #endif |
| 3637 | /* Replace all pipes in ctx with one newly created */ |
| 3638 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3639 | } else { |
| 3640 | no_conv: |
| 3641 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3642 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3643 | |
| 3644 | /* Without this check, even just <enter> on command line generates |
| 3645 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3646 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3647 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3648 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3649 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3650 | #endif |
| 3651 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3652 | || ctx->ctx_res_w == RES_DONE |
| 3653 | || ctx->ctx_res_w == RES_FOR |
| 3654 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3655 | #endif |
| 3656 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3657 | || ctx->ctx_res_w == RES_ESAC |
| 3658 | #endif |
| 3659 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3660 | struct pipe *new_p; |
| 3661 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3662 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3663 | not_null, ctx->ctx_res_w); |
| 3664 | new_p = new_pipe(); |
| 3665 | ctx->pipe->next = new_p; |
| 3666 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3667 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3668 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3669 | * This is used to control execution. |
| 3670 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3671 | * cases where variable or value happens to match a keyword): |
| 3672 | */ |
| 3673 | #if ENABLE_HUSH_LOOPS |
| 3674 | if (ctx->ctx_res_w == RES_FOR |
| 3675 | || ctx->ctx_res_w == RES_IN) |
| 3676 | ctx->ctx_res_w = RES_NONE; |
| 3677 | #endif |
| 3678 | #if ENABLE_HUSH_CASE |
| 3679 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3680 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3681 | if (ctx->ctx_res_w == RES_CASE) |
| 3682 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3683 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3684 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3685 | /* Create the memory for command, roughly: |
| 3686 | * ctx->pipe->cmds = new struct command; |
| 3687 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3688 | */ |
| 3689 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3690 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3691 | } |
| 3692 | debug_printf_parse("done_pipe return\n"); |
| 3693 | } |
| 3694 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3695 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3696 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3697 | memset(ctx, 0, sizeof(*ctx)); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3698 | if (MAYBE_ASSIGNMENT != 0) |
| 3699 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3700 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3701 | /* Create the memory for command, roughly: |
| 3702 | * ctx->pipe->cmds = new struct command; |
| 3703 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3704 | */ |
| 3705 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3708 | /* If a reserved word is found and processed, parse context is modified |
| 3709 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3710 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3711 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3712 | struct reserved_combo { |
| 3713 | char literal[6]; |
| 3714 | unsigned char res; |
| 3715 | unsigned char assignment_flag; |
| 3716 | int flag; |
| 3717 | }; |
| 3718 | enum { |
| 3719 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3720 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3721 | FLAG_IF = (1 << RES_IF ), |
| 3722 | FLAG_THEN = (1 << RES_THEN ), |
| 3723 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3724 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3725 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3726 | # endif |
| 3727 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3728 | FLAG_FOR = (1 << RES_FOR ), |
| 3729 | FLAG_WHILE = (1 << RES_WHILE), |
| 3730 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3731 | FLAG_DO = (1 << RES_DO ), |
| 3732 | FLAG_DONE = (1 << RES_DONE ), |
| 3733 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3734 | # endif |
| 3735 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3736 | FLAG_MATCH = (1 << RES_MATCH), |
| 3737 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3738 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3739 | FLAG_START = (1 << RES_XXXX ), |
| 3740 | }; |
| 3741 | |
| 3742 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3743 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3744 | /* Mostly a list of accepted follow-up reserved words. |
| 3745 | * FLAG_END means we are done with the sequence, and are ready |
| 3746 | * to turn the compound list into a command. |
| 3747 | * FLAG_START means the word must start a new compound list. |
| 3748 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3749 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3750 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3751 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3752 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3753 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3754 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3755 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3756 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3757 | # endif |
| 3758 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3759 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3760 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3761 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3762 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3763 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3764 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3765 | # endif |
| 3766 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3767 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3768 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3769 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3770 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3771 | const struct reserved_combo *r; |
| 3772 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3773 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3774 | if (strcmp(word->data, r->literal) == 0) |
| 3775 | return r; |
| 3776 | } |
| 3777 | return NULL; |
| 3778 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3779 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3780 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3781 | static const struct reserved_combo* reserved_word(struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3782 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3783 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3784 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3785 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3786 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3787 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3788 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3789 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3790 | if (ctx->word.has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3791 | return 0; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3792 | r = match_reserved_word(&ctx->word); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3793 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3794 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3795 | |
| 3796 | 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] | 3797 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3798 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3799 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3800 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3801 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3802 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3803 | if (r->flag == 0) { /* '!' */ |
| 3804 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3805 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3806 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3807 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3808 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3809 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3810 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3811 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3812 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3813 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3814 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3815 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3816 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3817 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3818 | } 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] | 3819 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3820 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3821 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3822 | } else { |
| 3823 | /* "{...} fi" is ok. "{...} if" is not |
| 3824 | * Example: |
| 3825 | * if { echo foo; } then { echo bar; } fi */ |
| 3826 | if (ctx->command->group) |
| 3827 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3828 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3829 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3830 | ctx->ctx_res_w = r->res; |
| 3831 | ctx->old_flag = r->flag; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3832 | ctx->is_assignment = r->assignment_flag; |
| 3833 | 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] | 3834 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3835 | if (ctx->old_flag & FLAG_END) { |
| 3836 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3837 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3838 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3839 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3840 | old = ctx->stack; |
| 3841 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3842 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3843 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3844 | /* At this point, the compound command's string is in |
| 3845 | * ctx->as_string... except for the leading keyword! |
| 3846 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3847 | * ctx->as_string will contain "true; then echo a; fi", |
| 3848 | * with "if " remaining in old->as_string! |
| 3849 | */ |
| 3850 | { |
| 3851 | char *str; |
| 3852 | int len = old->as_string.length; |
| 3853 | /* Concatenate halves */ |
| 3854 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3855 | o_free_unsafe(&ctx->as_string); |
| 3856 | /* Find where leading keyword starts in first half */ |
| 3857 | str = old->as_string.data + len; |
| 3858 | if (str > old->as_string.data) |
| 3859 | str--; /* skip whitespace after keyword */ |
| 3860 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3861 | str--; |
| 3862 | /* Ugh, we're done with this horrid hack */ |
| 3863 | old->command->group_as_string = xstrdup(str); |
| 3864 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3865 | old->command->group_as_string); |
| 3866 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3867 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3868 | *ctx = *old; /* physical copy */ |
| 3869 | free(old); |
| 3870 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3871 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3872 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3873 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3874 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3875 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3876 | * Normal return is 0. Syntax errors return 1. |
| 3877 | * Note: on return, word is reset, but not o_free'd! |
| 3878 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3879 | static int done_word(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3880 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3881 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3882 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3883 | debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command); |
| 3884 | if (ctx->word.length == 0 && !ctx->word.has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3885 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3886 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3887 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3888 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3889 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3890 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3891 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3892 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3893 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3894 | * If the redirection operator is "<<" or "<<-", the word |
| 3895 | * that follows the redirection operator shall be |
| 3896 | * subjected to quote removal; it is unspecified whether |
| 3897 | * any of the other expansions occur. For the other |
| 3898 | * redirection operators, the word that follows the |
| 3899 | * redirection operator shall be subjected to tilde |
| 3900 | * expansion, parameter expansion, command substitution, |
| 3901 | * arithmetic expansion, and quote removal. |
| 3902 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3903 | * on the word by a non-interactive shell; an interactive |
| 3904 | * shell may perform it, but shall do so only when |
| 3905 | * the expansion would result in one word." |
| 3906 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3907 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3908 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3909 | // as written: |
| 3910 | // <<EOF$t |
| 3911 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame] | 3912 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3913 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3914 | ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3915 | /* Cater for >\file case: |
| 3916 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3917 | * Same with heredocs: |
| 3918 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3919 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3920 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3921 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3922 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3923 | if (ctx->word.has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3924 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3925 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3926 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3927 | 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] | 3928 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3929 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3930 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3931 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3932 | if (ctx->ctx_dsemicolon |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3933 | && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3934 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3935 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3936 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3937 | ctx->ctx_dsemicolon = 0; |
| 3938 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3939 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3940 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3941 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3942 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3943 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3944 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3945 | # if ENABLE_HUSH_CASE |
| 3946 | && ctx->ctx_res_w != RES_CASE |
| 3947 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3948 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3949 | const struct reserved_combo *reserved; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3950 | reserved = reserved_word(ctx); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3951 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3952 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3953 | # if ENABLE_HUSH_LINENO_VAR |
| 3954 | /* Case: |
| 3955 | * "while ...; do |
| 3956 | * cmd ..." |
| 3957 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3958 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3959 | */ |
| 3960 | if (0 |
| 3961 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3962 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3963 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3964 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3965 | ) { |
| 3966 | done_pipe(ctx, PIPE_SEQ); |
| 3967 | } |
| 3968 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3969 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3970 | debug_printf_parse("done_word return %d\n", |
| 3971 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3972 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3973 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3974 | # if defined(CMD_SINGLEWORD_NOGLOB) |
| 3975 | if (0 |
| 3976 | # if BASH_TEST2 |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3977 | || strcmp(ctx->word.data, "[[") == 0 |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3978 | # endif |
| 3979 | /* In bash, local/export/readonly are special, args |
| 3980 | * are assignments and therefore expansion of them |
| 3981 | * should be "one-word" expansion: |
| 3982 | * $ export i=`echo 'a b'` # one arg: "i=a b" |
| 3983 | * compare with: |
| 3984 | * $ ls i=`echo 'a b'` # two args: "i=a" and "b" |
| 3985 | * ls: cannot access i=a: No such file or directory |
| 3986 | * ls: cannot access b: No such file or directory |
| 3987 | * Note: bash 3.2.33(1) does this only if export word |
| 3988 | * itself is not quoted: |
| 3989 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 3990 | * aaa bbb |
| 3991 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 3992 | * aaa |
| 3993 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 3994 | IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0) |
| 3995 | IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0) |
| 3996 | IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0) |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 3997 | ) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3998 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3999 | } |
| 4000 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4001 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4002 | } |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 4003 | #endif /* HAS_KEYWORDS */ |
| 4004 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4005 | if (command->group) { |
| 4006 | /* "{ echo foo; } echo bar" - bad */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4007 | syntax_error_at(ctx->word.data); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4008 | debug_printf_parse("done_word return 1: syntax error, " |
| 4009 | "groups and arglists don't mix\n"); |
| 4010 | return 1; |
| 4011 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4012 | |
| 4013 | /* If this word wasn't an assignment, next ones definitely |
| 4014 | * can't be assignments. Even if they look like ones. */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4015 | if (ctx->is_assignment != DEFINITELY_ASSIGNMENT |
| 4016 | && ctx->is_assignment != WORD_IS_KEYWORD |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4017 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4018 | ctx->is_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4019 | } else { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4020 | if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4021 | command->assignment_cnt++; |
| 4022 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 4023 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4024 | debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]); |
| 4025 | ctx->is_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4026 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4027 | debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]); |
| 4028 | command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4029 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4030 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4031 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4032 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4033 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4034 | if (ctx->word.has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4035 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 4036 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4037 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4038 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4039 | return 1; |
| 4040 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4041 | /* Force FOR to have just one word (variable name) */ |
| 4042 | /* NB: basically, this makes hush see "for v in ..." |
| 4043 | * syntax as if it is "for v; in ...". FOR and IN become |
| 4044 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 4045 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 4046 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4047 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4048 | #if ENABLE_HUSH_CASE |
| 4049 | /* Force CASE to have just one word */ |
| 4050 | if (ctx->ctx_res_w == RES_CASE) { |
| 4051 | done_pipe(ctx, PIPE_SEQ); |
| 4052 | } |
| 4053 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4054 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4055 | o_reset_to_empty_unquoted(&ctx->word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 4056 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4057 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4058 | return 0; |
| 4059 | } |
| 4060 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4061 | |
| 4062 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4063 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4064 | * Return: |
| 4065 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4066 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4067 | * REDIRFD_TO_FILE if no & was seen, |
| 4068 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4069 | */ |
| 4070 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4071 | #define parse_redir_right_fd(as_string, input) \ |
| 4072 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4073 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4074 | 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] | 4075 | { |
| 4076 | int ch, d, ok; |
| 4077 | |
| 4078 | ch = i_peek(input); |
| 4079 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4080 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4081 | |
| 4082 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4083 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4084 | ch = i_peek(input); |
| 4085 | if (ch == '-') { |
| 4086 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4087 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4088 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4089 | } |
| 4090 | d = 0; |
| 4091 | ok = 0; |
| 4092 | while (ch != EOF && isdigit(ch)) { |
| 4093 | d = d*10 + (ch-'0'); |
| 4094 | ok = 1; |
| 4095 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4096 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4097 | ch = i_peek(input); |
| 4098 | } |
| 4099 | if (ok) return d; |
| 4100 | |
| 4101 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4102 | |
| 4103 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4104 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4107 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4108 | */ |
| 4109 | static int parse_redirect(struct parse_context *ctx, |
| 4110 | int fd, |
| 4111 | redir_type style, |
| 4112 | struct in_str *input) |
| 4113 | { |
| 4114 | struct command *command = ctx->command; |
| 4115 | struct redir_struct *redir; |
| 4116 | struct redir_struct **redirp; |
| 4117 | int dup_num; |
| 4118 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4119 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4120 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4121 | /* Check for a '>&1' type redirect */ |
| 4122 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4123 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4124 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4125 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4126 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4127 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4128 | if (dup_num) { /* <<-... */ |
| 4129 | ch = i_getch(input); |
| 4130 | nommu_addchr(&ctx->as_string, ch); |
| 4131 | ch = i_peek(input); |
| 4132 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4133 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4134 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4135 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4136 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4137 | if (ch == '|') { |
| 4138 | /* >|FILE redirect ("clobbering" >). |
| 4139 | * Since we do not support "set -o noclobber" yet, |
| 4140 | * >| and > are the same for now. Just eat |. |
| 4141 | */ |
| 4142 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4143 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | /* Create a new redir_struct and append it to the linked list */ |
| 4148 | redirp = &command->redirects; |
| 4149 | while ((redir = *redirp) != NULL) { |
| 4150 | redirp = &(redir->next); |
| 4151 | } |
| 4152 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4153 | /* redir->next = NULL; */ |
| 4154 | /* redir->rd_filename = NULL; */ |
| 4155 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4156 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4157 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4158 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4159 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4160 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4161 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4162 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4163 | /* Erik had a check here that the file descriptor in question |
| 4164 | * is legit; I postpone that to "run time" |
| 4165 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4166 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4167 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4168 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4169 | #if 0 /* Instead we emit error message at run time */ |
| 4170 | if (ctx->pending_redirect) { |
| 4171 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4172 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4173 | } |
| 4174 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4175 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4176 | * end of the next parsed word. */ |
| 4177 | ctx->pending_redirect = redir; |
| 4178 | } |
| 4179 | return 0; |
| 4180 | } |
| 4181 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4182 | /* If a redirect is immediately preceded by a number, that number is |
| 4183 | * supposed to tell which file descriptor to redirect. This routine |
| 4184 | * looks for such preceding numbers. In an ideal world this routine |
| 4185 | * needs to handle all the following classes of redirects... |
| 4186 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4187 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4188 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4189 | * 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] | 4190 | * |
| 4191 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4192 | * "2.7 Redirection |
| 4193 | * ... If n is quoted, the number shall not be recognized as part of |
| 4194 | * the redirection expression. For example: |
| 4195 | * echo \2>a |
| 4196 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4197 | * 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] | 4198 | * |
| 4199 | * A -1 return means no valid number was found, |
| 4200 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4201 | */ |
| 4202 | static int redirect_opt_num(o_string *o) |
| 4203 | { |
| 4204 | int num; |
| 4205 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4206 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4207 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4208 | num = bb_strtou(o->data, NULL, 10); |
| 4209 | if (errno || num < 0) |
| 4210 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4211 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4212 | return num; |
| 4213 | } |
| 4214 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4215 | #if BB_MMU |
| 4216 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4217 | fetch_till_str(input, word, skip_tabs) |
| 4218 | #endif |
| 4219 | static char *fetch_till_str(o_string *as_string, |
| 4220 | struct in_str *input, |
| 4221 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4222 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4223 | { |
| 4224 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4225 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4226 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4227 | int ch; |
| 4228 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4229 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4230 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4231 | while (1) { |
| 4232 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4233 | if (ch != EOF) |
| 4234 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4235 | if (ch == '\n' || ch == EOF) { |
| 4236 | check_heredoc_end: |
| 4237 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4238 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4239 | heredoc.data[past_EOL] = '\0'; |
| 4240 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4241 | return heredoc.data; |
| 4242 | } |
| 4243 | if (ch == '\n') { |
| 4244 | /* This is a new line. |
| 4245 | * Remember position and backslash-escaping status. |
| 4246 | */ |
| 4247 | o_addchr(&heredoc, ch); |
| 4248 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4249 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4250 | past_EOL = heredoc.length; |
| 4251 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4252 | do { |
| 4253 | ch = i_getch(input); |
| 4254 | if (ch != EOF) |
| 4255 | nommu_addchr(as_string, ch); |
| 4256 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4257 | /* If this immediately ended the line, |
| 4258 | * go back to end-of-line checks. |
| 4259 | */ |
| 4260 | if (ch == '\n') |
| 4261 | goto check_heredoc_end; |
| 4262 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4263 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4264 | } |
| 4265 | if (ch == EOF) { |
| 4266 | o_free_unsafe(&heredoc); |
| 4267 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4268 | } |
| 4269 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4270 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4271 | if (prev == '\\' && ch == '\\') |
| 4272 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4273 | prev = 0; /* not \ */ |
| 4274 | else |
| 4275 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4279 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4280 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4281 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4282 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4283 | { |
| 4284 | struct pipe *pi = ctx->list_head; |
| 4285 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4286 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4287 | int i; |
| 4288 | struct command *cmd = pi->cmds; |
| 4289 | |
| 4290 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4291 | pi->num_cmds, |
| 4292 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4293 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4294 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4295 | |
| 4296 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4297 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4298 | while (redir) { |
| 4299 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4300 | char *p; |
| 4301 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4302 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4303 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4304 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4305 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4306 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4307 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4308 | return 1; |
| 4309 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4310 | free(redir->rd_filename); |
| 4311 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4312 | heredoc_cnt--; |
| 4313 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4314 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4315 | } |
| 4316 | cmd++; |
| 4317 | } |
| 4318 | pi = pi->next; |
| 4319 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4320 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4321 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4322 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4323 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4324 | #endif |
| 4325 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4329 | static int run_list(struct pipe *pi); |
| 4330 | #if BB_MMU |
| 4331 | #define parse_stream(pstring, input, end_trigger) \ |
| 4332 | parse_stream(input, end_trigger) |
| 4333 | #endif |
| 4334 | static struct pipe *parse_stream(char **pstring, |
| 4335 | struct in_str *input, |
| 4336 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4337 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4338 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4339 | static int parse_group(struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4340 | struct in_str *input, int ch) |
| 4341 | { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4342 | /* ctx->word contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4343 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4344 | * it contains function name (without '()'). */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4345 | #if BB_MMU |
| 4346 | # define as_string NULL |
| 4347 | #else |
| 4348 | char *as_string = NULL; |
| 4349 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4350 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4351 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4352 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4353 | |
| 4354 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4355 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4356 | if (ch == '(' && !ctx->word.has_quoted_part) { |
| 4357 | if (ctx->word.length) |
| 4358 | if (done_word(ctx)) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4359 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4360 | if (!command->argv) |
| 4361 | goto skip; /* (... */ |
| 4362 | if (command->argv[1]) { /* word word ... (... */ |
| 4363 | syntax_error_unexpected_ch('('); |
| 4364 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4365 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4366 | /* it is "word(..." or "word (..." */ |
| 4367 | do |
| 4368 | ch = i_getch(input); |
| 4369 | while (ch == ' ' || ch == '\t'); |
| 4370 | if (ch != ')') { |
| 4371 | syntax_error_unexpected_ch(ch); |
| 4372 | return 1; |
| 4373 | } |
| 4374 | nommu_addchr(&ctx->as_string, ch); |
| 4375 | do |
| 4376 | ch = i_getch(input); |
| 4377 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4378 | if (ch != '{' && ch != '(') { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4379 | syntax_error_unexpected_ch(ch); |
| 4380 | return 1; |
| 4381 | } |
| 4382 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4383 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4384 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4385 | } |
| 4386 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4387 | |
| 4388 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4389 | if (command->argv /* word [word]{... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 4390 | || ctx->word.length /* word{... */ |
| 4391 | || ctx->word.has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4392 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4393 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4394 | debug_printf_parse("parse_group return 1: " |
| 4395 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4396 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4397 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4398 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4399 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4400 | IF_HUSH_FUNCTIONS(skip:) |
| 4401 | |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4402 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4403 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4404 | endch = ')'; |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4405 | IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF)) |
| 4406 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4407 | } else { |
| 4408 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4409 | ch = i_peek(input); |
| 4410 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4411 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4412 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4413 | syntax_error_unexpected_ch(ch); |
| 4414 | return 1; |
| 4415 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4416 | if (ch != '(') { |
| 4417 | ch = i_getch(input); |
| 4418 | nommu_addchr(&ctx->as_string, ch); |
| 4419 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4420 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4421 | |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4422 | pipe_list = parse_stream(&as_string, input, endch); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4423 | #if !BB_MMU |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4424 | if (as_string) |
| 4425 | o_addstr(&ctx->as_string, as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4426 | #endif |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4427 | |
| 4428 | /* empty ()/{} or parse error? */ |
| 4429 | if (!pipe_list || pipe_list == ERR_PTR) { |
| 4430 | /* parse_stream already emitted error msg */ |
| 4431 | if (!BB_MMU) |
| 4432 | free(as_string); |
| 4433 | debug_printf_parse("parse_group return 1: " |
| 4434 | "parse_stream returned %p\n", pipe_list); |
| 4435 | return 1; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4436 | } |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4437 | #if !BB_MMU |
| 4438 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4439 | command->group_as_string = as_string; |
| 4440 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4441 | command->group_as_string); |
| 4442 | #endif |
| 4443 | |
| 4444 | #if ENABLE_HUSH_FUNCTIONS |
| 4445 | /* Convert "f() (cmds)" to "f() {(cmds)}" */ |
| 4446 | if (command->cmd_type == CMD_FUNCDEF && endch == ')') { |
| 4447 | struct command *cmd2; |
| 4448 | |
| 4449 | cmd2 = xzalloc(sizeof(*cmd2)); |
| 4450 | cmd2->cmd_type = CMD_SUBSHELL; |
| 4451 | cmd2->group = pipe_list; |
| 4452 | # if !BB_MMU |
| 4453 | //UNTESTED! |
| 4454 | cmd2->group_as_string = command->group_as_string; |
| 4455 | command->group_as_string = xasprintf("(%s)", command->group_as_string); |
| 4456 | # endif |
| 4457 | |
| 4458 | pipe_list = new_pipe(); |
| 4459 | pipe_list->cmds = cmd2; |
| 4460 | pipe_list->num_cmds = 1; |
| 4461 | } |
| 4462 | #endif |
| 4463 | |
| 4464 | command->group = pipe_list; |
| 4465 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4466 | debug_printf_parse("parse_group return 0\n"); |
| 4467 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4468 | /* command remains "open", available for possible redirects */ |
Denys Vlasenko | fbf4485 | 2018-04-03 14:56:52 +0200 | [diff] [blame] | 4469 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4472 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4473 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4474 | 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] | 4475 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4476 | 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] | 4477 | { |
| 4478 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4479 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4480 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4481 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4482 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4483 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4484 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4485 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4486 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4487 | } |
| 4488 | } |
| 4489 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4490 | 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] | 4491 | { |
| 4492 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4493 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4494 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4495 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4496 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4497 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4498 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4499 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4500 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4501 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4502 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4503 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4504 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4505 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4506 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4507 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4508 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4509 | continue; |
| 4510 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4511 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4515 | * \` quoting. |
| 4516 | * "Within the backquoted style of command substitution, backslash |
| 4517 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4518 | * The search for the matching backquote shall be satisfied by the first |
| 4519 | * backquote found without a preceding backslash; during this search, |
| 4520 | * if a non-escaped backquote is encountered within a shell comment, |
| 4521 | * a here-document, an embedded command substitution of the $(command) |
| 4522 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4523 | * or double-quoted string that begins, but does not end, within the |
| 4524 | * "`...`" sequence produces undefined results." |
| 4525 | * Example Output |
| 4526 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4527 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4528 | 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] | 4529 | { |
| 4530 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4531 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4532 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4533 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4534 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4535 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4536 | ch = i_getch(input); |
| 4537 | if (ch != '`' |
| 4538 | && ch != '$' |
| 4539 | && ch != '\\' |
| 4540 | && (!in_dquote || ch != '"') |
| 4541 | ) { |
| 4542 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4543 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4544 | } |
| 4545 | if (ch == EOF) { |
| 4546 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4547 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4548 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4549 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4550 | } |
| 4551 | } |
| 4552 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4553 | * quoting and nested ()s. |
| 4554 | * "With the $(command) style of command substitution, all characters |
| 4555 | * following the open parenthesis to the matching closing parenthesis |
| 4556 | * constitute the command. Any valid shell script can be used for command, |
| 4557 | * except a script consisting solely of redirections which produces |
| 4558 | * unspecified results." |
| 4559 | * Example Output |
| 4560 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4561 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4562 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4563 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4564 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4565 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4566 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4567 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4568 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4569 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4570 | 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] | 4571 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4572 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4573 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4574 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4575 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4576 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4577 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4578 | |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4579 | G.promptmode = 1; /* PS2 */ |
| 4580 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 4581 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4582 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4583 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4584 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4585 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4586 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4587 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4588 | if (ch == end_ch |
| 4589 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4590 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4591 | # endif |
| 4592 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4593 | if (!dbl) |
| 4594 | break; |
| 4595 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4596 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4597 | i_getch(input); /* eat second ')' */ |
| 4598 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4599 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4600 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4601 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4602 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4603 | if (ch == '(' || ch == '{') { |
| 4604 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4605 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4606 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4607 | o_addchr(dest, ch); |
| 4608 | continue; |
| 4609 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4610 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4611 | if (!add_till_single_quote(dest, input)) |
| 4612 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4613 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4614 | continue; |
| 4615 | } |
| 4616 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4617 | if (!add_till_double_quote(dest, input)) |
| 4618 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4619 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4620 | continue; |
| 4621 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4622 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4623 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4624 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4625 | o_addchr(dest, ch); |
| 4626 | continue; |
| 4627 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4628 | if (ch == '\\') { |
| 4629 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4630 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4631 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4632 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4633 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4634 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4635 | #if 0 |
| 4636 | if (ch == '\n') { |
| 4637 | /* "backslash+newline", ignore both */ |
| 4638 | o_delchr(dest); /* undo insertion of '\' */ |
| 4639 | continue; |
| 4640 | } |
| 4641 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4642 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4643 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4644 | continue; |
| 4645 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4646 | } |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 4647 | 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] | 4648 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4649 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4650 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4651 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4652 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4653 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4654 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4655 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4656 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4657 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4658 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4659 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4660 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4661 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4662 | 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] | 4663 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4664 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4665 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4666 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4667 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4668 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4669 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4670 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4671 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4672 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4673 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4674 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4675 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4676 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4677 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4678 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4679 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4680 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4681 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4682 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4683 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4684 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4685 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4686 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4687 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4688 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4689 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4690 | o_addchr(dest, ch | quote_mask); |
| 4691 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4692 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4693 | case '$': /* pid */ |
| 4694 | case '!': /* last bg pid */ |
| 4695 | case '?': /* last exit code */ |
| 4696 | case '#': /* number of args */ |
| 4697 | case '*': /* args */ |
| 4698 | case '@': /* args */ |
| 4699 | goto make_one_char_var; |
| 4700 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4701 | char len_single_ch; |
| 4702 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4703 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4704 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4705 | ch = i_getch(input); /* eat '{' */ |
| 4706 | nommu_addchr(as_string, ch); |
| 4707 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4708 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4709 | /* It should be ${?}, or ${#var}, |
| 4710 | * or even ${?+subst} - operator acting on a special variable, |
| 4711 | * or the beginning of variable name. |
| 4712 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4713 | if (ch == EOF |
| 4714 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4715 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4716 | bad_dollar_syntax: |
| 4717 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4718 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4719 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4720 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4721 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4722 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4723 | ch |= quote_mask; |
| 4724 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4725 | /* 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] | 4726 | * However, this regresses some of our testsuite cases |
| 4727 | * which check invalid constructs like ${%}. |
| 4728 | * Oh well... let's check that the var name part is fine... */ |
| 4729 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4730 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4731 | unsigned pos; |
| 4732 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4733 | o_addchr(dest, ch); |
| 4734 | debug_printf_parse(": '%c'\n", ch); |
| 4735 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4736 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4737 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4738 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4739 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4740 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4741 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4742 | unsigned end_ch; |
| 4743 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4744 | /* handle parameter expansions |
| 4745 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4746 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4747 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4748 | if (len_single_ch != '#' |
| 4749 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4750 | || i_peek(input) != '}' |
| 4751 | ) { |
| 4752 | goto bad_dollar_syntax; |
| 4753 | } |
| 4754 | /* else: it's "length of C" ${#C} op, |
| 4755 | * where C is a single char |
| 4756 | * special var name, e.g. ${#!}. |
| 4757 | */ |
| 4758 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4759 | /* Eat everything until closing '}' (or ':') */ |
| 4760 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4761 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4762 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4763 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4764 | ) { |
| 4765 | /* It's ${var:N[:M]} thing */ |
| 4766 | end_ch = '}' * 0x100 + ':'; |
| 4767 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4768 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4769 | && ch == '/' |
| 4770 | ) { |
| 4771 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4772 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4773 | i_getch(input); |
| 4774 | nommu_addchr(as_string, '/'); |
| 4775 | ch = '\\'; |
| 4776 | } |
| 4777 | end_ch = '}' * 0x100 + '/'; |
| 4778 | } |
| 4779 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4780 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4781 | if (!BB_MMU) |
| 4782 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4783 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4784 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4785 | if (last_ch == 0) /* error? */ |
| 4786 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4787 | #else |
| 4788 | #error Simple code to only allow ${var} is not implemented |
| 4789 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4790 | if (as_string) { |
| 4791 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4792 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4793 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4794 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4795 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4796 | && (end_ch & 0xff00) |
| 4797 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4798 | /* close the first block: */ |
| 4799 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4800 | /* while parsing N from ${var:N[:M]} |
| 4801 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4802 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4803 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4804 | end_ch = '}'; |
| 4805 | goto again; |
| 4806 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4807 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4808 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4809 | /* it's ${var:N} - emulate :999999999 */ |
| 4810 | o_addstr(dest, "999999999"); |
| 4811 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4812 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4813 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4814 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4815 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4816 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4817 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4818 | break; |
| 4819 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4820 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4821 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4822 | unsigned pos; |
| 4823 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4824 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4825 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4826 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4827 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
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); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4830 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4831 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4832 | if (!BB_MMU) |
| 4833 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4834 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4835 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4836 | if (as_string) { |
| 4837 | o_addstr(as_string, dest->data + pos); |
| 4838 | o_addchr(as_string, ')'); |
| 4839 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4840 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4841 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4842 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4843 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4844 | # endif |
| 4845 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4846 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4847 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4848 | if (!BB_MMU) |
| 4849 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4850 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4851 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4852 | if (as_string) { |
| 4853 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4854 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4855 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4856 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4857 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4860 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4861 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4862 | goto make_var; |
| 4863 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4864 | /* TODO: $_ and $-: */ |
| 4865 | /* $_ Shell or shell script name; or last argument of last command |
| 4866 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4867 | * 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] | 4868 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4869 | ch = i_getch(input); |
| 4870 | nommu_addchr(as_string, ch); |
| 4871 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4872 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4873 | ch = '_'; |
| 4874 | goto make_var1; |
| 4875 | } |
| 4876 | /* else: it's $_ */ |
| 4877 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4878 | default: |
| 4879 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4880 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4881 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4882 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4883 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4886 | #if BB_MMU |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4887 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4888 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4889 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4890 | # else |
| 4891 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4892 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4893 | encode_string(dest, input, dquote_end) |
| 4894 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4895 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4896 | |
| 4897 | #else /* !MMU */ |
| 4898 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4899 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4900 | /* all parameters are needed, no macro tricks */ |
| 4901 | # else |
| 4902 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4903 | encode_string(as_string, dest, input, dquote_end) |
| 4904 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4905 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4906 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4907 | o_string *dest, |
| 4908 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4909 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4910 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4911 | { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4912 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4913 | const int process_bkslash = 1; |
| 4914 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4915 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4916 | int next; |
| 4917 | |
| 4918 | again: |
| 4919 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4920 | if (ch != EOF) |
| 4921 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4922 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4923 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4924 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4925 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4926 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4927 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4928 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4929 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4930 | } |
| 4931 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4932 | if (ch != '\n') { |
| 4933 | next = i_peek(input); |
| 4934 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4935 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4936 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4937 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4938 | if (next == EOF) { |
Denys Vlasenko | 4709df0 | 2018-04-10 14:49:01 +0200 | [diff] [blame] | 4939 | /* Testcase: in interactive shell a file with |
| 4940 | * echo "unterminated string\<eof> |
| 4941 | * is sourced. |
| 4942 | */ |
| 4943 | syntax_error_unterm_ch('"'); |
| 4944 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4945 | } |
| 4946 | /* bash: |
| 4947 | * "The backslash retains its special meaning [in "..."] |
| 4948 | * only when followed by one of the following characters: |
| 4949 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4950 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4951 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4952 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4953 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4954 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4955 | ch = i_getch(input); /* eat next */ |
| 4956 | if (ch == '\n') |
| 4957 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4958 | } /* else: ch remains == '\\', and we double it below: */ |
| 4959 | 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] | 4960 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4961 | goto again; |
| 4962 | } |
| 4963 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4964 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4965 | debug_printf_parse("encode_string return 0: " |
| 4966 | "parse_dollar returned 0 (error)\n"); |
| 4967 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4968 | } |
| 4969 | goto again; |
| 4970 | } |
| 4971 | #if ENABLE_HUSH_TICK |
| 4972 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4973 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4974 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4975 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4976 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4977 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4978 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4979 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4980 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4981 | } |
| 4982 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4983 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4984 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4985 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4988 | /* |
| 4989 | * Scan input until EOF or end_trigger char. |
| 4990 | * Return a list of pipes to execute, or NULL on EOF |
| 4991 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4992 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4993 | * reset parsing machinery and start parsing anew, |
| 4994 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4995 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4996 | static struct pipe *parse_stream(char **pstring, |
| 4997 | struct in_str *input, |
| 4998 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4999 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5000 | struct parse_context ctx; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5001 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5002 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5003 | /* 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] | 5004 | * 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] | 5005 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5006 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 5007 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5008 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5009 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5010 | initialize_context(&ctx); |
| 5011 | |
| 5012 | /* If very first arg is "" or '', ctx.word.data may end up NULL. |
| 5013 | * Preventing this: |
| 5014 | */ |
| 5015 | o_addchr(&ctx.word, '\0'); |
| 5016 | ctx.word.length = 0; |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 5017 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5018 | /* We used to separate words on $IFS here. This was wrong. |
| 5019 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5020 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5021 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5022 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5023 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5024 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5025 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5026 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5027 | int ch; |
| 5028 | int next; |
| 5029 | int redir_fd; |
| 5030 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5031 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5032 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5033 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5034 | ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5035 | if (ch == EOF) { |
| 5036 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5037 | |
| 5038 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5039 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5040 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5041 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5042 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5043 | syntax_error_unterm_ch('('); |
| 5044 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5045 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 5046 | if (end_trigger == '}') { |
| 5047 | syntax_error_unterm_ch('{'); |
| 5048 | goto parse_error; |
| 5049 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5050 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5051 | if (done_word(&ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 5052 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5053 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5054 | o_free(&ctx.word); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5055 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5056 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5057 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5058 | /* (this makes bare "&" cmd a no-op. |
| 5059 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5060 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5061 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5062 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5063 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5064 | pi = NULL; |
| 5065 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5066 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5067 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5068 | if (pstring) |
| 5069 | *pstring = ctx.as_string.data; |
| 5070 | else |
| 5071 | o_free_unsafe(&ctx.as_string); |
| 5072 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5073 | debug_leave(); |
| 5074 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5075 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5076 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5077 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5078 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5079 | if (ch == '\'') { |
| 5080 | ctx.word.has_quoted_part = 1; |
| 5081 | next = i_getch(input); |
| 5082 | if (next == '\'' && !ctx.pending_redirect) |
| 5083 | goto insert_empty_quoted_str_marker; |
| 5084 | |
| 5085 | ch = next; |
| 5086 | while (1) { |
| 5087 | if (ch == EOF) { |
| 5088 | syntax_error_unterm_ch('\''); |
| 5089 | goto parse_error; |
| 5090 | } |
| 5091 | nommu_addchr(&ctx.as_string, ch); |
| 5092 | if (ch == '\'') |
| 5093 | break; |
| 5094 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5095 | /* Convert raw ^C to corresponding special variable reference */ |
| 5096 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5097 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
| 5098 | } |
| 5099 | o_addqchr(&ctx.word, ch); |
| 5100 | ch = i_getch(input); |
| 5101 | } |
| 5102 | continue; /* get next char */ |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5103 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5104 | |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5105 | next = '\0'; |
| 5106 | if (ch != '\n' && ch != '\\') { |
| 5107 | /* Not on '\': do not break the case of "echo z\\": |
| 5108 | * on 2nd '\', i_peek_and_eat_bkslash_nl() |
| 5109 | * would stop and try to read next line, |
| 5110 | * not letting the command to execute. |
| 5111 | */ |
| 5112 | next = i_peek_and_eat_bkslash_nl(input); |
| 5113 | } |
| 5114 | |
| 5115 | is_special = "{}<>;&|()#" /* special outside of "str" */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5116 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
| 5117 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5118 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5119 | if (ctx.command->argv /* word [word]{... - non-special */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5120 | || ctx.word.length /* word{... - non-special */ |
| 5121 | || ctx.word.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5122 | || (next != ';' /* }; - special */ |
| 5123 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5124 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5125 | && next != '&' /* }& and }&& ... - special */ |
| 5126 | && next != '|' /* }|| ... - special */ |
| 5127 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5128 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5129 | ) { |
| 5130 | /* They are not special, skip "{}" */ |
| 5131 | is_special += 2; |
| 5132 | } |
| 5133 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5134 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5135 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5136 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5137 | ordinary_char: |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5138 | o_addQchr(&ctx.word, ch); |
| 5139 | if ((ctx.is_assignment == MAYBE_ASSIGNMENT |
| 5140 | || ctx.is_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5141 | && ch == '=' |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5142 | && is_well_formed_var_name(ctx.word.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5143 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5144 | ctx.is_assignment = DEFINITELY_ASSIGNMENT; |
| 5145 | 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] | 5146 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5147 | continue; |
| 5148 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5149 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5150 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5151 | #if ENABLE_HUSH_LINENO_VAR |
| 5152 | /* Case: |
| 5153 | * "while ...; do<whitespace><newline> |
| 5154 | * cmd ..." |
| 5155 | * would think that "cmd" starts in <whitespace> - |
| 5156 | * i.e., at the previous line. |
| 5157 | * We need to skip all whitespace before newlines. |
| 5158 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5159 | while (ch != '\n') { |
| 5160 | next = i_peek(input); |
| 5161 | if (next != ' ' && next != '\t' && next != '\n') |
| 5162 | break; /* next char is not ws */ |
| 5163 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5164 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5165 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5166 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5167 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5168 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5169 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5170 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5171 | /* Is this a case when newline is simply ignored? |
| 5172 | * Some examples: |
| 5173 | * "cmd | <newline> cmd ..." |
| 5174 | * "case ... in <newline> word) ..." |
| 5175 | */ |
| 5176 | if (IS_NULL_CMD(ctx.command) |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5177 | && ctx.word.length == 0 && !ctx.word.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5178 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5179 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5180 | * Without check #1, interactive shell |
| 5181 | * ignores even bare <newline>, |
| 5182 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5183 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5184 | * ps2> _ <=== wrong, should be ps1 |
| 5185 | * Without check #2, "cmd & <newline>" |
| 5186 | * is similarly mistreated. |
| 5187 | * (BTW, this makes "cmd & cmd" |
| 5188 | * and "cmd && cmd" non-orthogonal. |
| 5189 | * Really, ask yourself, why |
| 5190 | * "cmd && <newline>" doesn't start |
| 5191 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5192 | * The only reason is that it might be |
| 5193 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5194 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5195 | */ |
| 5196 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5197 | if (pi->num_cmds != 0 /* check #1 */ |
| 5198 | && pi->followup != PIPE_BG /* check #2 */ |
| 5199 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5200 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5201 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5202 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5203 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5204 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5205 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5206 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5207 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5208 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5209 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5210 | heredoc_cnt = 0; |
| 5211 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5212 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5213 | 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] | 5214 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5215 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5216 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5217 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5218 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5219 | |
| 5220 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5221 | * } is an ordinary char in this case, even inside { cmd; } |
| 5222 | * Pathological example: { ""}; } should exec "}" cmd |
| 5223 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5224 | if (ch == '}') { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5225 | if (ctx.word.length != 0 /* word} */ |
| 5226 | || ctx.word.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5227 | ) { |
| 5228 | goto ordinary_char; |
| 5229 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5230 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5231 | /* Generally, there should be semicolon: "cmd; }" |
| 5232 | * However, bash allows to omit it if "cmd" is |
| 5233 | * a group. Examples: |
| 5234 | * { { echo 1; } } |
| 5235 | * {(echo 1)} |
| 5236 | * { echo 0 >&2 | { echo 1; } } |
| 5237 | * { while false; do :; done } |
| 5238 | * { case a in b) ;; esac } |
| 5239 | */ |
| 5240 | if (ctx.command->group) |
| 5241 | goto term_group; |
| 5242 | goto ordinary_char; |
| 5243 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5244 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5245 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5246 | goto skip_end_trigger; |
| 5247 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5248 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5249 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5250 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5251 | && (ch != ';' || heredoc_cnt == 0) |
| 5252 | #if ENABLE_HUSH_CASE |
| 5253 | && (ch != ')' |
| 5254 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5255 | || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5256 | ) |
| 5257 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5258 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5259 | if (heredoc_cnt) { |
| 5260 | /* This is technically valid: |
| 5261 | * { cat <<HERE; }; echo Ok |
| 5262 | * heredoc |
| 5263 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5264 | * HERE |
| 5265 | * but we don't support this. |
| 5266 | * We require heredoc to be in enclosing {}/(), |
| 5267 | * if any. |
| 5268 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5269 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5270 | goto parse_error; |
| 5271 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5272 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5273 | goto parse_error; |
| 5274 | } |
| 5275 | done_pipe(&ctx, PIPE_SEQ); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5276 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5277 | 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] | 5278 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5279 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5280 | 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] | 5281 | ) { |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5282 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5283 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5284 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5285 | if (pstring) |
| 5286 | *pstring = ctx.as_string.data; |
| 5287 | else |
| 5288 | o_free_unsafe(&ctx.as_string); |
| 5289 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5290 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5291 | /* Example: bare "{ }", "()" */ |
| 5292 | G.last_exitcode = 2; /* bash compat */ |
| 5293 | syntax_error_unexpected_ch(ch); |
| 5294 | goto parse_error2; |
| 5295 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5296 | debug_printf_parse("parse_stream return %p: " |
| 5297 | "end_trigger char found\n", |
| 5298 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5299 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5300 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5301 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5302 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5303 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5304 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5305 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5306 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5307 | /* Catch <, > before deciding whether this word is |
| 5308 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5309 | switch (ch) { |
| 5310 | case '>': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5311 | redir_fd = redirect_opt_num(&ctx.word); |
| 5312 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5313 | goto parse_error; |
| 5314 | } |
| 5315 | redir_style = REDIRECT_OVERWRITE; |
| 5316 | if (next == '>') { |
| 5317 | redir_style = REDIRECT_APPEND; |
| 5318 | ch = i_getch(input); |
| 5319 | nommu_addchr(&ctx.as_string, ch); |
| 5320 | } |
| 5321 | #if 0 |
| 5322 | else if (next == '(') { |
| 5323 | syntax_error(">(process) not supported"); |
| 5324 | goto parse_error; |
| 5325 | } |
| 5326 | #endif |
| 5327 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5328 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5329 | continue; /* get next char */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5330 | case '<': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5331 | redir_fd = redirect_opt_num(&ctx.word); |
| 5332 | if (done_word(&ctx)) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5333 | goto parse_error; |
| 5334 | } |
| 5335 | redir_style = REDIRECT_INPUT; |
| 5336 | if (next == '<') { |
| 5337 | redir_style = REDIRECT_HEREDOC; |
| 5338 | heredoc_cnt++; |
| 5339 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5340 | ch = i_getch(input); |
| 5341 | nommu_addchr(&ctx.as_string, ch); |
| 5342 | } else if (next == '>') { |
| 5343 | redir_style = REDIRECT_IO; |
| 5344 | ch = i_getch(input); |
| 5345 | nommu_addchr(&ctx.as_string, ch); |
| 5346 | } |
| 5347 | #if 0 |
| 5348 | else if (next == '(') { |
| 5349 | syntax_error("<(process) not supported"); |
| 5350 | goto parse_error; |
| 5351 | } |
| 5352 | #endif |
| 5353 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5354 | goto parse_error; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5355 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5356 | case '#': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5357 | if (ctx.word.length == 0 && !ctx.word.has_quoted_part) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5358 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5359 | /* note: we do not add it to &ctx.as_string */ |
| 5360 | /* TODO: in bash: |
| 5361 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5362 | * cmd "$(cmd2 #comment)" - syntax error |
| 5363 | * cmd "`cmd2 #comment`" - ok |
| 5364 | * We accept both (comment ends where command subst ends, in both cases). |
| 5365 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5366 | while (1) { |
| 5367 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5368 | if (ch == '\n') { |
| 5369 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5370 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5371 | } |
| 5372 | ch = i_getch(input); |
| 5373 | if (ch == EOF) |
| 5374 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5375 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5376 | continue; /* get next char */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5377 | } |
| 5378 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5379 | } |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5380 | skip_end_trigger: |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5381 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5382 | if (ctx.is_assignment == MAYBE_ASSIGNMENT |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5383 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5384 | && !ctx.pending_redirect |
| 5385 | ) { |
| 5386 | /* ch is a special char and thus this word |
| 5387 | * cannot be an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5388 | ctx.is_assignment = NOT_ASSIGNMENT; |
| 5389 | 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] | 5390 | } |
| 5391 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5392 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5393 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5394 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5395 | case SPECIAL_VAR_SYMBOL: |
| 5396 | /* Convert raw ^C to corresponding special variable reference */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5397 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5398 | o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS); |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5399 | /* fall through */ |
| 5400 | case '#': |
| 5401 | /* non-comment #: "echo a#b" etc */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5402 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5403 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5404 | case '\\': |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5405 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame^] | 5406 | o_addchr(&ctx.word, '\\'); |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5407 | ch = i_getch(input); |
| 5408 | if (ch == EOF) { |
Denys Vlasenko | 89e9d55 | 2018-04-11 01:15:33 +0200 | [diff] [blame^] | 5409 | /* Testcase: eval 'echo Ok\' */ |
| 5410 | |
| 5411 | #if 0 /* bash-4.3.43 was removing backslash, but 4.4.19 retains it, most other shells too */ |
Denys Vlasenko | bcf5611 | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 5412 | /* Remove trailing '\' from ctx.as_string */ |
| 5413 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 5414 | #endif |
| 5415 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5416 | } |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5417 | /* Example: echo Hello \2>file |
Denys Vlasenko | bcf5611 | 2018-04-10 14:40:23 +0200 | [diff] [blame] | 5418 | * we need to know that word 2 is quoted |
| 5419 | */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5420 | ctx.word.has_quoted_part = 1; |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5421 | nommu_addchr(&ctx.as_string, ch); |
| 5422 | o_addchr(&ctx.word, ch); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5423 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5424 | case '$': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5425 | if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5426 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5427 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5428 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5429 | } |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5430 | continue; /* get next char */ |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5431 | case '"': |
| 5432 | ctx.word.has_quoted_part = 1; |
| 5433 | if (next == '"' && !ctx.pending_redirect) { |
Denys Vlasenko | 92a930b | 2018-04-10 14:20:48 +0200 | [diff] [blame] | 5434 | i_getch(input); /* eat second " */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5435 | insert_empty_quoted_str_marker: |
| 5436 | nommu_addchr(&ctx.as_string, next); |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5437 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5438 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5439 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5440 | } |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5441 | if (ctx.is_assignment == NOT_ASSIGNMENT) |
| 5442 | ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
| 5443 | if (!encode_string(&ctx.as_string, &ctx.word, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5444 | goto parse_error; |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5445 | ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5446 | continue; /* get next char */ |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5447 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5448 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5449 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5450 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5451 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5452 | o_addchr(&ctx.word, '`'); |
| 5453 | USE_FOR_NOMMU(pos = ctx.word.length;) |
| 5454 | if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0)) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5455 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5456 | # if !BB_MMU |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5457 | o_addstr(&ctx.as_string, ctx.word.data + pos); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5458 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5459 | # endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5460 | o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL); |
| 5461 | //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos); |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5462 | continue; /* get next char */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5463 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5464 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5465 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5466 | #if ENABLE_HUSH_CASE |
| 5467 | case_semi: |
| 5468 | #endif |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5469 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5470 | goto parse_error; |
| 5471 | } |
| 5472 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5473 | #if ENABLE_HUSH_CASE |
| 5474 | /* Eat multiple semicolons, detect |
| 5475 | * whether it means something special */ |
| 5476 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5477 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5478 | if (ch != ';') |
| 5479 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5480 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5481 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5482 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5483 | ctx.ctx_dsemicolon = 1; |
| 5484 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5485 | break; |
| 5486 | } |
| 5487 | } |
| 5488 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5489 | new_cmd: |
| 5490 | /* We just finished a cmd. New one may start |
| 5491 | * with an assignment */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5492 | ctx.is_assignment = MAYBE_ASSIGNMENT; |
| 5493 | 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] | 5494 | continue; /* get next char */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5495 | case '&': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5496 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5497 | goto parse_error; |
| 5498 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5499 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5500 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5501 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5502 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5503 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5504 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5505 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5506 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5507 | case '|': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5508 | if (done_word(&ctx)) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5509 | goto parse_error; |
| 5510 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5511 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5512 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5513 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5514 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5515 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5516 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5517 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5518 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5519 | } else { |
| 5520 | /* we could pick up a file descriptor choice here |
| 5521 | * with redirect_opt_num(), but bash doesn't do it. |
| 5522 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5523 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5524 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5525 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5526 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5527 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5528 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5529 | if (ctx.ctx_res_w == RES_MATCH |
| 5530 | && ctx.command->argv == NULL /* not (word|(... */ |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5531 | && ctx.word.length == 0 /* not word(... */ |
| 5532 | && ctx.word.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5533 | ) { |
Denys Vlasenko | e8b1bc0 | 2018-04-10 13:13:10 +0200 | [diff] [blame] | 5534 | continue; /* get next char */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5535 | } |
| 5536 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5537 | case '{': |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5538 | if (parse_group(&ctx, input, ch) != 0) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5539 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5540 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5541 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5542 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5543 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5544 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5545 | goto case_semi; |
| 5546 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5547 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5548 | /* proper use of this character is caught by end_trigger: |
| 5549 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5550 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5551 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5552 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5553 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5554 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5555 | if (HUSH_DEBUG) |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 5556 | bb_error_msg_and_die("BUG: unexpected %c", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5557 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5558 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5559 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5560 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5561 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5562 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5563 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5564 | struct parse_context *pctx; |
| 5565 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5568 | * Sample for finding leaks on syntax error recovery path. |
| 5569 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5570 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5571 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5572 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5573 | * 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] | 5574 | */ |
| 5575 | pctx = &ctx; |
| 5576 | do { |
| 5577 | /* Update pipe/command counts, |
| 5578 | * otherwise freeing may miss some */ |
| 5579 | done_pipe(pctx, PIPE_SEQ); |
| 5580 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5581 | pctx->list_head, pctx); |
| 5582 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5583 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5584 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5585 | #if !BB_MMU |
| 5586 | o_free_unsafe(&pctx->as_string); |
| 5587 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5588 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5589 | if (pctx != &ctx) { |
| 5590 | free(pctx); |
| 5591 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5592 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5593 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5594 | |
Denys Vlasenko | 09b7a7e | 2018-04-10 03:22:10 +0200 | [diff] [blame] | 5595 | o_free(&ctx.word); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5596 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5597 | if (pstring) |
| 5598 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5599 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5600 | debug_leave(); |
| 5601 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5602 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5605 | |
| 5606 | /*** Execution routines ***/ |
| 5607 | |
| 5608 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5609 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5610 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5611 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5612 | expand_string_to_string(str) |
| 5613 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5614 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5615 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5616 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5617 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5618 | |
| 5619 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5620 | * all variable references within and returns a pointer to |
| 5621 | * a list of expanded strings, possibly with larger number |
| 5622 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5623 | * This new list is allocated as a single malloc block. |
| 5624 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5625 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5626 | * Caller can deallocate entire list by single free(list). */ |
| 5627 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5628 | /* A horde of its helpers come first: */ |
| 5629 | |
| 5630 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5631 | { |
| 5632 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5633 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5634 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5635 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5636 | if (c == '{' || c == '}') { |
| 5637 | /* { -> \{, } -> \} */ |
| 5638 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5639 | /* And now we want to add { or } and continue: |
| 5640 | * o_addchr(o, c); |
| 5641 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5642 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5643 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5644 | } |
| 5645 | #endif |
| 5646 | o_addchr(o, c); |
| 5647 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5648 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5649 | o_addchr(o, '\\'); |
| 5650 | if (len) { |
| 5651 | len--; |
| 5652 | o_addchr(o, '\\'); |
| 5653 | o_addchr(o, *str++); |
| 5654 | } |
| 5655 | } |
| 5656 | } |
| 5657 | } |
| 5658 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5659 | /* Store given string, finalizing the word and starting new one whenever |
| 5660 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5661 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5662 | * Return in *ended_with_ifs: |
| 5663 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5664 | */ |
| 5665 | static int expand_on_ifs(int *ended_with_ifs, o_string *output, int n, const char *str) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5666 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5667 | int last_is_ifs = 0; |
| 5668 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5669 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5670 | int word_len; |
| 5671 | |
| 5672 | if (!*str) /* EOL - do not finalize word */ |
| 5673 | break; |
| 5674 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5675 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5676 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5677 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5678 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5679 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5680 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5681 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5682 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5683 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5684 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5685 | /*/ Why can't we do it easier? */ |
| 5686 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5687 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5688 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5689 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5690 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5691 | if (!*str) /* EOL - do not finalize word */ |
| 5692 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5693 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5694 | |
| 5695 | /* We know str here points to at least one IFS char */ |
| 5696 | last_is_ifs = 1; |
| 5697 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5698 | if (!*str) /* EOL - do not finalize word */ |
| 5699 | break; |
| 5700 | |
| 5701 | /* Start new word... but not always! */ |
| 5702 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5703 | if (output->has_quoted_part |
| 5704 | /* Case "v=' a'; echo $v": |
| 5705 | * here nothing precedes the space in $v expansion, |
| 5706 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5707 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5708 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5709 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5710 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5711 | o_addchr(output, '\0'); |
| 5712 | debug_print_list("expand_on_ifs", output, n); |
| 5713 | n = o_save_ptr(output, n); |
| 5714 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5715 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5716 | |
| 5717 | if (ended_with_ifs) |
| 5718 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5719 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5720 | return n; |
| 5721 | } |
| 5722 | |
| 5723 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5724 | * they are in double quotes, with the exception that they are not :). |
| 5725 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5726 | * |
| 5727 | * Returns malloced string. |
| 5728 | * As an optimization, we return NULL if expansion is not needed. |
| 5729 | */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5730 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5731 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5732 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5733 | encode_then_expand_string(str) |
| 5734 | #endif |
| 5735 | static char *encode_then_expand_string(const char *str, int process_bkslash, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5736 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5737 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 5738 | enum { do_unbackslash = 1 }; |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5739 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5740 | char *exp_str; |
| 5741 | struct in_str input; |
| 5742 | o_string dest = NULL_O_STRING; |
| 5743 | |
| 5744 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5745 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5746 | #if ENABLE_HUSH_TICK |
| 5747 | && !strchr(str, '`') |
| 5748 | #endif |
| 5749 | ) { |
| 5750 | return NULL; |
| 5751 | } |
| 5752 | |
| 5753 | /* We need to expand. Example: |
| 5754 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5755 | */ |
| 5756 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5757 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5758 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5759 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5760 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5761 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5762 | o_free_unsafe(&dest); |
| 5763 | return exp_str; |
| 5764 | } |
| 5765 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5766 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5767 | 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] | 5768 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5769 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5770 | arith_t res; |
| 5771 | char *exp_str; |
| 5772 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5773 | math_state.lookupvar = get_local_var_value; |
| 5774 | math_state.setvar = set_local_var_from_halves; |
| 5775 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5776 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5777 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5778 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5779 | if (errmsg_p) |
| 5780 | *errmsg_p = math_state.errmsg; |
| 5781 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5782 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5783 | return res; |
| 5784 | } |
| 5785 | #endif |
| 5786 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5787 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5788 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5789 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5790 | { |
| 5791 | while (1) { |
| 5792 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5793 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5794 | if (end) { |
| 5795 | *size = end - val; |
| 5796 | return val; |
| 5797 | } |
| 5798 | if (*val == '\0') |
| 5799 | return NULL; |
| 5800 | /* Optimization: if "*pat" did not match the start of "string", |
| 5801 | * we know that "tring", "ring" etc will not match too: |
| 5802 | */ |
| 5803 | if (pattern[0] == '*') |
| 5804 | return NULL; |
| 5805 | val++; |
| 5806 | } |
| 5807 | } |
| 5808 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5809 | { |
| 5810 | char *result = NULL; |
| 5811 | unsigned res_len = 0; |
| 5812 | unsigned repl_len = strlen(repl); |
| 5813 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5814 | /* Null pattern never matches, including if "var" is empty */ |
| 5815 | if (!pattern[0]) |
| 5816 | return result; /* NULL, no replaces happened */ |
| 5817 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5818 | while (1) { |
| 5819 | int size; |
| 5820 | char *s = strstr_pattern(val, pattern, &size); |
| 5821 | if (!s) |
| 5822 | break; |
| 5823 | |
| 5824 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5825 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5826 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5827 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5828 | |
| 5829 | val = s + size; |
| 5830 | if (exp_op == '/') |
| 5831 | break; |
| 5832 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5833 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5834 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5835 | strcpy(result + res_len, val); |
| 5836 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5837 | } |
| 5838 | debug_printf_varexp("result:'%s'\n", result); |
| 5839 | return result; |
| 5840 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5841 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5842 | |
| 5843 | /* Helper: |
| 5844 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5845 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5846 | static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5847 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5848 | const char *val; |
| 5849 | char *to_be_freed; |
| 5850 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5851 | char *var; |
| 5852 | char first_char; |
| 5853 | char exp_op; |
| 5854 | char exp_save = exp_save; /* for compiler */ |
| 5855 | char *exp_saveptr; /* points to expansion operator */ |
| 5856 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5857 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5858 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5859 | val = NULL; |
| 5860 | to_be_freed = NULL; |
| 5861 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5862 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5863 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5864 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5865 | arg0 = arg[0]; |
| 5866 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5867 | exp_op = 0; |
| 5868 | |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5869 | if (first_char == '#' && arg[1] /* ${#...} but not ${#} */ |
| 5870 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 5871 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 5872 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5873 | ) { |
| 5874 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5875 | var++; |
| 5876 | exp_op = 'L'; |
| 5877 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5878 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5879 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5880 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5881 | ) { |
| 5882 | /* ${?:0}, ${#[:]%0} etc */ |
| 5883 | exp_saveptr = var + 1; |
| 5884 | } else { |
| 5885 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5886 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5887 | } |
| 5888 | exp_op = exp_save = *exp_saveptr; |
| 5889 | if (exp_op) { |
| 5890 | exp_word = exp_saveptr + 1; |
| 5891 | if (exp_op == ':') { |
| 5892 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5893 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5894 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5895 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5896 | ) { |
| 5897 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5898 | exp_op = ':'; |
| 5899 | exp_word--; |
| 5900 | } |
| 5901 | } |
| 5902 | *exp_saveptr = '\0'; |
| 5903 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5904 | } |
| 5905 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5906 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5907 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5908 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5909 | int n = xatoi_positive(var); |
| 5910 | if (n < G.global_argc) |
| 5911 | val = G.global_argv[n]; |
| 5912 | /* else val remains NULL: $N with too big N */ |
| 5913 | } else { |
| 5914 | switch (var[0]) { |
| 5915 | case '$': /* pid */ |
| 5916 | val = utoa(G.root_pid); |
| 5917 | break; |
| 5918 | case '!': /* bg pid */ |
| 5919 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5920 | break; |
| 5921 | case '?': /* exitcode */ |
| 5922 | val = utoa(G.last_exitcode); |
| 5923 | break; |
| 5924 | case '#': /* argc */ |
| 5925 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5926 | break; |
| 5927 | default: |
| 5928 | val = get_local_var_value(var); |
| 5929 | } |
| 5930 | } |
| 5931 | |
| 5932 | /* Handle any expansions */ |
| 5933 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5934 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5935 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5936 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5937 | debug_printf_expand("%s\n", val); |
| 5938 | } else if (exp_op) { |
| 5939 | if (exp_op == '%' || exp_op == '#') { |
| 5940 | /* Standard-mandated substring removal ops: |
| 5941 | * ${parameter%word} - remove smallest suffix pattern |
| 5942 | * ${parameter%%word} - remove largest suffix pattern |
| 5943 | * ${parameter#word} - remove smallest prefix pattern |
| 5944 | * ${parameter##word} - remove largest prefix pattern |
| 5945 | * |
| 5946 | * Word is expanded to produce a glob pattern. |
| 5947 | * Then var's value is matched to it and matching part removed. |
| 5948 | */ |
| 5949 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5950 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5951 | char *exp_exp_word; |
| 5952 | char *loc; |
| 5953 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5954 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5955 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5956 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 5957 | /* |
| 5958 | * process_bkslash:1 unbackslash:1 breaks this: |
| 5959 | * a='a\\'; echo ${a%\\\\} # correct output is: a |
| 5960 | * process_bkslash:1 unbackslash:0 breaks this: |
| 5961 | * a='a}'; echo ${a%\}} # correct output is: a |
| 5962 | */ |
| 5963 | exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5964 | if (exp_exp_word) |
| 5965 | exp_word = exp_exp_word; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5966 | debug_printf_expand("expand: exp_exp_word:'%s'\n", exp_word); |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5967 | /* HACK ALERT. We depend here on the fact that |
| 5968 | * G.global_argv and results of utoa and get_local_var_value |
| 5969 | * are actually in writable memory: |
| 5970 | * scan_and_match momentarily stores NULs there. */ |
| 5971 | t = (char*)val; |
| 5972 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5973 | 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] | 5974 | free(exp_exp_word); |
| 5975 | if (loc) { /* match was found */ |
| 5976 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5977 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5978 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5979 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5980 | } |
| 5981 | } |
| 5982 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5983 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5984 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5985 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5986 | * Note that in encoded form it has TWO parts: |
| 5987 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5988 | * and if // is used, it is encoded as \: |
| 5989 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5990 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5991 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5992 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5993 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5994 | * by the usual expansion rules: |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 5995 | * >az >bz |
| 5996 | * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z" |
| 5997 | * v='a bz'; echo "${v/a*z/\z}" #prints "z" |
| 5998 | * v='a bz'; echo ${v/a*z/a*z} #prints "az" |
| 5999 | * v='a bz'; echo ${v/a*z/\z} #prints "z" |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6000 | * (note that a*z _pattern_ is never globbed!) |
| 6001 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6002 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6003 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6004 | if (!pattern) |
| 6005 | pattern = xstrdup(exp_word); |
| 6006 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 6007 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6008 | exp_word = p; |
| 6009 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6010 | *p = '\0'; |
Denys Vlasenko | de02625 | 2018-04-05 17:04:53 +0200 | [diff] [blame] | 6011 | repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6012 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 6013 | /* HACK ALERT. We depend here on the fact that |
| 6014 | * G.global_argv and results of utoa and get_local_var_value |
| 6015 | * are actually in writable memory: |
| 6016 | * replace_pattern momentarily stores NULs there. */ |
| 6017 | t = (char*)val; |
| 6018 | to_be_freed = replace_pattern(t, |
| 6019 | pattern, |
| 6020 | (repl ? repl : exp_word), |
| 6021 | exp_op); |
| 6022 | if (to_be_freed) /* at least one replace happened */ |
| 6023 | val = to_be_freed; |
| 6024 | free(pattern); |
| 6025 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 6026 | } else { |
| 6027 | /* Empty variable always gives nothing */ |
| 6028 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 6029 | /* Just skip "replace" part */ |
| 6030 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6031 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6032 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6033 | } |
| 6034 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6035 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6036 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6037 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6038 | /* It's ${var:N[:M]} bashism. |
| 6039 | * Note that in encoded form it has TWO parts: |
| 6040 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 6041 | */ |
| 6042 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6043 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6044 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6045 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6046 | if (errmsg) |
| 6047 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6048 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 6049 | *p++ = SPECIAL_VAR_SYMBOL; |
| 6050 | exp_word = p; |
| 6051 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6052 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6053 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 6054 | if (errmsg) |
| 6055 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6056 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6057 | if (beg < 0) { |
| 6058 | /* negative beg counts from the end */ |
| 6059 | beg = (arith_t)strlen(val) + beg; |
| 6060 | if (beg < 0) /* ${v: -999999} is "" */ |
| 6061 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6062 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6063 | debug_printf_varexp("from val:'%s'\n", val); |
| 6064 | if (len < 0) { |
| 6065 | /* in bash, len=-n means strlen()-n */ |
| 6066 | len = (arith_t)strlen(val) - beg + len; |
| 6067 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6068 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6069 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 6070 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6071 | arith_err: |
| 6072 | val = NULL; |
| 6073 | } else { |
| 6074 | /* Paranoia. What if user entered 9999999999999 |
| 6075 | * which fits in arith_t but not int? */ |
| 6076 | if (len >= INT_MAX) |
| 6077 | len = INT_MAX; |
| 6078 | val = to_be_freed = xstrndup(val + beg, len); |
| 6079 | } |
| 6080 | debug_printf_varexp("val:'%s'\n", val); |
| 6081 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6082 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6083 | val = NULL; |
| 6084 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6085 | } else { /* one of "-=+?" */ |
| 6086 | /* Standard-mandated substitution ops: |
| 6087 | * ${var?word} - indicate error if unset |
| 6088 | * If var is unset, word (or a message indicating it is unset |
| 6089 | * if word is null) is written to standard error |
| 6090 | * and the shell exits with a non-zero exit status. |
| 6091 | * Otherwise, the value of var is substituted. |
| 6092 | * ${var-word} - use default value |
| 6093 | * If var is unset, word is substituted. |
| 6094 | * ${var=word} - assign and use default value |
| 6095 | * If var is unset, word is assigned to var. |
| 6096 | * In all cases, final value of var is substituted. |
| 6097 | * ${var+word} - use alternative value |
| 6098 | * If var is unset, null is substituted. |
| 6099 | * Otherwise, word is substituted. |
| 6100 | * |
| 6101 | * Word is subjected to tilde expansion, parameter expansion, |
| 6102 | * command substitution, and arithmetic expansion. |
| 6103 | * If word is not needed, it is not expanded. |
| 6104 | * |
| 6105 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6106 | * but also treat null var as if it is unset. |
| 6107 | */ |
| 6108 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6109 | if (exp_op == '+') |
| 6110 | use_word = !use_word; |
| 6111 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6112 | (exp_save == ':') ? "true" : "false", use_word); |
| 6113 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6114 | to_be_freed = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6115 | if (to_be_freed) |
| 6116 | exp_word = to_be_freed; |
| 6117 | if (exp_op == '?') { |
| 6118 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6119 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6120 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 6121 | exp_word[0] |
| 6122 | ? exp_word |
| 6123 | : "parameter null or not set" |
| 6124 | /* ash has more specific messages, a-la: */ |
| 6125 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6126 | ); |
| 6127 | //TODO: how interactive bash aborts expansion mid-command? |
| 6128 | } else { |
| 6129 | val = exp_word; |
| 6130 | } |
| 6131 | |
| 6132 | if (exp_op == '=') { |
| 6133 | /* ${var=[word]} or ${var:=[word]} */ |
| 6134 | if (isdigit(var[0]) || var[0] == '#') { |
| 6135 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6136 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6137 | val = NULL; |
| 6138 | } else { |
| 6139 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 6140 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6141 | } |
| 6142 | } |
| 6143 | } |
| 6144 | } /* one of "-=+?" */ |
| 6145 | |
| 6146 | *exp_saveptr = exp_save; |
| 6147 | } /* if (exp_op) */ |
| 6148 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6149 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6150 | |
| 6151 | *pp = p; |
| 6152 | *to_be_freed_pp = to_be_freed; |
| 6153 | return val; |
| 6154 | } |
| 6155 | |
| 6156 | /* Expand all variable references in given string, adding words to list[] |
| 6157 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6158 | * to be filled). This routine is extremely tricky: has to deal with |
| 6159 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6160 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6161 | 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] | 6162 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6163 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6164 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6165 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6166 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6167 | int ended_in_ifs = 0; /* did last unquoted expansion end with IFS chars? */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6168 | char *p; |
| 6169 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6170 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6171 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6172 | debug_print_list("expand_vars_to_list", output, n); |
| 6173 | n = o_save_ptr(output, n); |
| 6174 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6175 | |
| 6176 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6177 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6178 | char *to_be_freed = NULL; |
| 6179 | const char *val = NULL; |
| 6180 | #if ENABLE_HUSH_TICK |
| 6181 | o_string subst_result = NULL_O_STRING; |
| 6182 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6183 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6184 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6185 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6186 | |
| 6187 | if (ended_in_ifs) { |
| 6188 | o_addchr(output, '\0'); |
| 6189 | n = o_save_ptr(output, n); |
| 6190 | ended_in_ifs = 0; |
| 6191 | } |
| 6192 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6193 | o_addblock(output, arg, p - arg); |
| 6194 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6195 | arg = ++p; |
| 6196 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6197 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6198 | /* Fetch special var name (if it is indeed one of them) |
| 6199 | * and quote bit, force the bit on if singleword expansion - |
| 6200 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6201 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6202 | |
| 6203 | /* Is this variable quoted and thus expansion can't be null? |
| 6204 | * "$@" is special. Even if quoted, it can still |
| 6205 | * expand to nothing (not even an empty string), |
| 6206 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6207 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6208 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6209 | |
| 6210 | switch (first_ch & 0x7f) { |
| 6211 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6212 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6213 | case '@': { |
| 6214 | int i; |
| 6215 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6216 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6217 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6218 | 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] | 6219 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6220 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6221 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6222 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6223 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6224 | /* this argv[] is not empty and not last: |
| 6225 | * put terminating NUL, start new word */ |
| 6226 | o_addchr(output, '\0'); |
| 6227 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6228 | n = o_save_ptr(output, n); |
| 6229 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6230 | } |
| 6231 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6232 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6233 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6234 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6235 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6236 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6237 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6238 | while (1) { |
| 6239 | o_addQstr(output, G.global_argv[i]); |
| 6240 | if (++i >= G.global_argc) |
| 6241 | break; |
| 6242 | o_addchr(output, '\0'); |
| 6243 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6244 | n = o_save_ptr(output, n); |
| 6245 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6246 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6247 | while (1) { |
| 6248 | o_addQstr(output, G.global_argv[i]); |
| 6249 | if (!G.global_argv[++i]) |
| 6250 | break; |
| 6251 | if (G.ifs[0]) |
| 6252 | o_addchr(output, G.ifs[0]); |
| 6253 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6254 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6255 | } |
| 6256 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6257 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6258 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 6259 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6260 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6261 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6262 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6263 | break; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6264 | case SPECIAL_VAR_QUOTED_SVS: |
| 6265 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
| 6266 | arg++; |
| 6267 | val = SPECIAL_VAR_SYMBOL_STR; |
| 6268 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6269 | #if ENABLE_HUSH_TICK |
| 6270 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6271 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6272 | arg++; |
| 6273 | /* Can't just stuff it into output o_string, |
| 6274 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6275 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6276 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6277 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 6278 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6279 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 6280 | val = subst_result.data; |
| 6281 | goto store_val; |
| 6282 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6283 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6284 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 6285 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6286 | |
| 6287 | arg++; /* skip '+' */ |
| 6288 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6289 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6290 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6291 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6292 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6293 | val = arith_buf; |
| 6294 | break; |
| 6295 | } |
| 6296 | #endif |
| 6297 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6298 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6299 | IF_HUSH_TICK(store_val:) |
| 6300 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6301 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6302 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6303 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6304 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6305 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6306 | } |
| 6307 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6308 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6309 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6310 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6311 | } |
| 6312 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6313 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6314 | |
| 6315 | if (val && val[0]) { |
| 6316 | o_addQstr(output, val); |
| 6317 | } |
| 6318 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6319 | |
| 6320 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6321 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6322 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6323 | *p = SPECIAL_VAR_SYMBOL; |
| 6324 | |
| 6325 | #if ENABLE_HUSH_TICK |
| 6326 | o_free(&subst_result); |
| 6327 | #endif |
| 6328 | arg = ++p; |
| 6329 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6330 | |
| 6331 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6332 | if (ended_in_ifs) { |
| 6333 | o_addchr(output, '\0'); |
| 6334 | n = o_save_ptr(output, n); |
| 6335 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6336 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6337 | /* this part is literal, and it was already pre-quoted |
| 6338 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6339 | o_addstr_with_NUL(output, arg); |
| 6340 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6341 | } 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] | 6342 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6343 | ) { |
| 6344 | n--; |
| 6345 | /* allow to reuse list[n] later without re-growth */ |
| 6346 | output->has_empty_slot = 1; |
| 6347 | } else { |
| 6348 | o_addchr(output, '\0'); |
| 6349 | } |
| 6350 | |
| 6351 | return n; |
| 6352 | } |
| 6353 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6354 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6355 | { |
| 6356 | int n; |
| 6357 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6358 | o_string output = NULL_O_STRING; |
| 6359 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6360 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6361 | |
| 6362 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6363 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6364 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6365 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6366 | } |
| 6367 | debug_print_list("expand_variables", &output, n); |
| 6368 | |
| 6369 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6370 | list = o_finalize_list(&output, n); |
| 6371 | debug_print_strings("expand_variables[1]", list); |
| 6372 | return list; |
| 6373 | } |
| 6374 | |
| 6375 | static char **expand_strvec_to_strvec(char **argv) |
| 6376 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6377 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6378 | } |
| 6379 | |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 6380 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6381 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6382 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6383 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6384 | } |
| 6385 | #endif |
| 6386 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6387 | /* Used for expansion of right hand of assignments, |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 6388 | * $((...)), heredocs, variable expansion parts. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6389 | * |
| 6390 | * NB: should NOT do globbing! |
| 6391 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6392 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6393 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6394 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6395 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6396 | const int do_unbackslash = 1; |
| 6397 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6398 | char *argv[2], **list; |
| 6399 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6400 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6401 | /* This is generally an optimization, but it also |
| 6402 | * handles "", which otherwise trips over !list[0] check below. |
| 6403 | * (is this ever happens that we actually get str="" here?) |
| 6404 | */ |
| 6405 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6406 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6407 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6408 | return xstrdup(str); |
| 6409 | } |
| 6410 | |
| 6411 | argv[0] = (char*)str; |
| 6412 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6413 | list = expand_variables(argv, do_unbackslash |
| 6414 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 6415 | : EXP_FLAG_SINGLEWORD |
| 6416 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6417 | if (HUSH_DEBUG) |
| 6418 | if (!list[0] || list[1]) |
| 6419 | bb_error_msg_and_die("BUG in varexp2"); |
| 6420 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6421 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6422 | if (do_unbackslash) |
| 6423 | unbackslash((char*)list); |
| 6424 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6425 | return (char*)list; |
| 6426 | } |
| 6427 | |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 6428 | #if 0 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6429 | static char* expand_strvec_to_string(char **argv) |
| 6430 | { |
| 6431 | char **list; |
| 6432 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6433 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6434 | /* Convert all NULs to spaces */ |
| 6435 | if (list[0]) { |
| 6436 | int n = 1; |
| 6437 | while (list[n]) { |
| 6438 | if (HUSH_DEBUG) |
| 6439 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6440 | bb_error_msg_and_die("BUG in varexp3"); |
| 6441 | /* bash uses ' ' regardless of $IFS contents */ |
| 6442 | list[n][-1] = ' '; |
| 6443 | n++; |
| 6444 | } |
| 6445 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6446 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6447 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6448 | return (char*)list; |
| 6449 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6450 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6451 | |
| 6452 | static char **expand_assignments(char **argv, int count) |
| 6453 | { |
| 6454 | int i; |
| 6455 | char **p; |
| 6456 | |
| 6457 | G.expanded_assignments = p = NULL; |
| 6458 | /* Expand assignments into one string each */ |
| 6459 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6460 | G.expanded_assignments = p = add_string_to_strings(p, expand_string_to_string(argv[i], /*unbackslash:*/ 1)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6461 | } |
| 6462 | G.expanded_assignments = NULL; |
| 6463 | return p; |
| 6464 | } |
| 6465 | |
| 6466 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6467 | static void switch_off_special_sigs(unsigned mask) |
| 6468 | { |
| 6469 | unsigned sig = 0; |
| 6470 | while ((mask >>= 1) != 0) { |
| 6471 | sig++; |
| 6472 | if (!(mask & 1)) |
| 6473 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6474 | #if ENABLE_HUSH_TRAP |
| 6475 | if (G_traps) { |
| 6476 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6477 | /* trap is '', has to remain SIG_IGN */ |
| 6478 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6479 | free(G_traps[sig]); |
| 6480 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6481 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6482 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6483 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6484 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6485 | } |
| 6486 | } |
| 6487 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6488 | #if BB_MMU |
| 6489 | /* never called */ |
| 6490 | void re_execute_shell(char ***to_free, const char *s, |
| 6491 | char *g_argv0, char **g_argv, |
| 6492 | char **builtin_argv) NORETURN; |
| 6493 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6494 | static void reset_traps_to_defaults(void) |
| 6495 | { |
| 6496 | /* This function is always called in a child shell |
| 6497 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6498 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6499 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6500 | unsigned mask; |
| 6501 | |
| 6502 | /* Child shells are not interactive. |
| 6503 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6504 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6505 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6506 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6507 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6508 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6509 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6510 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6511 | /* Switch off special sigs */ |
| 6512 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6513 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6514 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6515 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6516 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6517 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6518 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6519 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6520 | # if ENABLE_HUSH_TRAP |
| 6521 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6522 | return; |
| 6523 | |
| 6524 | /* Reset all sigs to default except ones with empty traps */ |
| 6525 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6526 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6527 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6528 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6529 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6530 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6531 | free(G_traps[sig]); |
| 6532 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6533 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6534 | if (sig == 0) |
| 6535 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6536 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6537 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6538 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6539 | } |
| 6540 | |
| 6541 | #else /* !BB_MMU */ |
| 6542 | |
| 6543 | static void re_execute_shell(char ***to_free, const char *s, |
| 6544 | char *g_argv0, char **g_argv, |
| 6545 | char **builtin_argv) NORETURN; |
| 6546 | static void re_execute_shell(char ***to_free, const char *s, |
| 6547 | char *g_argv0, char **g_argv, |
| 6548 | char **builtin_argv) |
| 6549 | { |
| 6550 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6551 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6552 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6553 | char *heredoc_argv[4]; |
| 6554 | struct variable *cur; |
| 6555 | # if ENABLE_HUSH_FUNCTIONS |
| 6556 | struct function *funcp; |
| 6557 | # endif |
| 6558 | char **argv, **pp; |
| 6559 | unsigned cnt; |
| 6560 | unsigned long long empty_trap_mask; |
| 6561 | |
| 6562 | if (!g_argv0) { /* heredoc */ |
| 6563 | argv = heredoc_argv; |
| 6564 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6565 | argv[1] = (char *) "-<"; |
| 6566 | argv[2] = (char *) s; |
| 6567 | argv[3] = NULL; |
| 6568 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6569 | goto do_exec; |
| 6570 | } |
| 6571 | |
| 6572 | cnt = 0; |
| 6573 | pp = builtin_argv; |
| 6574 | if (pp) while (*pp++) |
| 6575 | cnt++; |
| 6576 | |
| 6577 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6578 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6579 | int sig; |
| 6580 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6581 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6582 | empty_trap_mask |= 1LL << sig; |
| 6583 | } |
| 6584 | } |
| 6585 | |
| 6586 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6587 | , (unsigned) G.root_pid |
| 6588 | , (unsigned) G.root_ppid |
| 6589 | , (unsigned) G.last_bg_pid |
| 6590 | , (unsigned) G.last_exitcode |
| 6591 | , cnt |
| 6592 | , empty_trap_mask |
| 6593 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6594 | ); |
| 6595 | # undef NOMMU_HACK_FMT |
| 6596 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6597 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6598 | */ |
| 6599 | cnt += 6; |
| 6600 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6601 | if (!cur->flg_export || cur->flg_read_only) |
| 6602 | cnt += 2; |
| 6603 | } |
| 6604 | # if ENABLE_HUSH_FUNCTIONS |
| 6605 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6606 | cnt += 3; |
| 6607 | # endif |
| 6608 | pp = g_argv; |
| 6609 | while (*pp++) |
| 6610 | cnt++; |
| 6611 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6612 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6613 | *pp++ = param_buf; |
| 6614 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6615 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6616 | continue; |
| 6617 | if (cur->flg_read_only) { |
| 6618 | *pp++ = (char *) "-R"; |
| 6619 | *pp++ = cur->varstr; |
| 6620 | } else if (!cur->flg_export) { |
| 6621 | *pp++ = (char *) "-V"; |
| 6622 | *pp++ = cur->varstr; |
| 6623 | } |
| 6624 | } |
| 6625 | # if ENABLE_HUSH_FUNCTIONS |
| 6626 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6627 | *pp++ = (char *) "-F"; |
| 6628 | *pp++ = funcp->name; |
| 6629 | *pp++ = funcp->body_as_string; |
| 6630 | } |
| 6631 | # endif |
| 6632 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6633 | * |
| 6634 | * However, POSIX says that subshells reset signals with traps |
| 6635 | * to SIG_DFL. |
| 6636 | * I tested bash-3.2 and it not only does that with true subshells |
| 6637 | * of the form ( list ), but with any forked children shells. |
| 6638 | * I set trap "echo W" WINCH; and then tried: |
| 6639 | * |
| 6640 | * { echo 1; sleep 20; echo 2; } & |
| 6641 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6642 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6643 | * |
| 6644 | * In all these cases sending SIGWINCH to the child shell |
| 6645 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6646 | * _inside_ group (just before echo 1), it works. |
| 6647 | * |
| 6648 | * 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] | 6649 | */ |
| 6650 | *pp++ = (char *) "-c"; |
| 6651 | *pp++ = (char *) s; |
| 6652 | if (builtin_argv) { |
| 6653 | while (*++builtin_argv) |
| 6654 | *pp++ = *builtin_argv; |
| 6655 | *pp++ = (char *) ""; |
| 6656 | } |
| 6657 | *pp++ = g_argv0; |
| 6658 | while (*g_argv) |
| 6659 | *pp++ = *g_argv++; |
| 6660 | /* *pp = NULL; - is already there */ |
| 6661 | pp = environ; |
| 6662 | |
| 6663 | do_exec: |
| 6664 | 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] | 6665 | /* Don't propagate SIG_IGN to the child */ |
| 6666 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6667 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6668 | execve(bb_busybox_exec_path, argv, pp); |
| 6669 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6670 | if (argv[0][0] == '/') |
| 6671 | execve(argv[0], argv, pp); |
| 6672 | xfunc_error_retval = 127; |
| 6673 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6674 | } |
| 6675 | #endif /* !BB_MMU */ |
| 6676 | |
| 6677 | |
| 6678 | static int run_and_free_list(struct pipe *pi); |
| 6679 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6680 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6681 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6682 | * end_trigger controls how often we stop parsing |
| 6683 | * NUL: parse all, execute, return |
| 6684 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6685 | */ |
| 6686 | 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] | 6687 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6688 | /* Why we need empty flag? |
| 6689 | * An obscure corner case "false; ``; echo $?": |
| 6690 | * empty command in `` should still set $? to 0. |
| 6691 | * But we can't just set $? to 0 at the start, |
| 6692 | * this breaks "false; echo `echo $?`" case. |
| 6693 | */ |
| 6694 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6695 | while (1) { |
| 6696 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6697 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6698 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 8d6eab3 | 2018-04-07 17:01:31 +0200 | [diff] [blame] | 6699 | if (end_trigger == ';') { |
| 6700 | G.promptmode = 0; /* PS1 */ |
| 6701 | debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode); |
| 6702 | } |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6703 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6704 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6705 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6706 | /* If we are in "big" script |
| 6707 | * (not in `cmd` or something similar)... |
| 6708 | */ |
| 6709 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6710 | /* Discard cached input (rest of line) */ |
| 6711 | int ch = inp->last_char; |
| 6712 | while (ch != EOF && ch != '\n') { |
| 6713 | //bb_error_msg("Discarded:'%c'", ch); |
| 6714 | ch = i_getch(inp); |
| 6715 | } |
| 6716 | /* Force prompt */ |
| 6717 | inp->p = NULL; |
| 6718 | /* This stream isn't empty */ |
| 6719 | empty = 0; |
| 6720 | continue; |
| 6721 | } |
| 6722 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6723 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6724 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6725 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6726 | debug_print_tree(pipe_list, 0); |
| 6727 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6728 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6729 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6730 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6731 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6732 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6733 | } |
| 6734 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6735 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6736 | { |
| 6737 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6738 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 6739 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6740 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6741 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6742 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6743 | } |
| 6744 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6745 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6746 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6747 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6748 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 6749 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6750 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 6751 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6752 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6753 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6754 | } |
| 6755 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6756 | #if ENABLE_HUSH_TICK |
| 6757 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6758 | { |
| 6759 | pid_t pid; |
| 6760 | int channel[2]; |
| 6761 | # if !BB_MMU |
| 6762 | char **to_free = NULL; |
| 6763 | # endif |
| 6764 | |
| 6765 | xpipe(channel); |
| 6766 | pid = BB_MMU ? xfork() : xvfork(); |
| 6767 | if (pid == 0) { /* child */ |
| 6768 | disable_restore_tty_pgrp_on_exit(); |
| 6769 | /* Process substitution is not considered to be usual |
| 6770 | * 'command execution'. |
| 6771 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6772 | */ |
| 6773 | bb_signals(0 |
| 6774 | + (1 << SIGTSTP) |
| 6775 | + (1 << SIGTTIN) |
| 6776 | + (1 << SIGTTOU) |
| 6777 | , SIG_IGN); |
| 6778 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6779 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6780 | xmove_fd(channel[1], 1); |
| 6781 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6782 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6783 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6784 | /* Awful hack for `trap` or $(trap). |
| 6785 | * |
| 6786 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6787 | * contains an example where "trap" is executed in a subshell: |
| 6788 | * |
| 6789 | * save_traps=$(trap) |
| 6790 | * ... |
| 6791 | * eval "$save_traps" |
| 6792 | * |
| 6793 | * Standard does not say that "trap" in subshell shall print |
| 6794 | * parent shell's traps. It only says that its output |
| 6795 | * must have suitable form, but then, in the above example |
| 6796 | * (which is not supposed to be normative), it implies that. |
| 6797 | * |
| 6798 | * bash (and probably other shell) does implement it |
| 6799 | * (traps are reset to defaults, but "trap" still shows them), |
| 6800 | * but as a result, "trap" logic is hopelessly messed up: |
| 6801 | * |
| 6802 | * # trap |
| 6803 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6804 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6805 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6806 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6807 | * trap -- 'echo Ho' SIGWINCH |
| 6808 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6809 | * trap -- 'echo Ho' SIGWINCH |
| 6810 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6811 | * trap -- 'echo Ho' SIGWINCH |
| 6812 | * |
| 6813 | * The rules when to forget and when to not forget traps |
| 6814 | * get really complex and nonsensical. |
| 6815 | * |
| 6816 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6817 | */ |
| 6818 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6819 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6820 | && skip_whitespace(s + 4)[0] == '\0' |
| 6821 | ) { |
| 6822 | static const char *const argv[] = { NULL, NULL }; |
| 6823 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6824 | fflush_all(); /* important */ |
| 6825 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6826 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6827 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6828 | # if BB_MMU |
| 6829 | reset_traps_to_defaults(); |
| 6830 | parse_and_run_string(s); |
| 6831 | _exit(G.last_exitcode); |
| 6832 | # else |
| 6833 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6834 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6835 | * huge=`cat BIG` # was blocking here forever |
| 6836 | * echo OK |
| 6837 | */ |
| 6838 | re_execute_shell(&to_free, |
| 6839 | s, |
| 6840 | G.global_argv[0], |
| 6841 | G.global_argv + 1, |
| 6842 | NULL); |
| 6843 | # endif |
| 6844 | } |
| 6845 | |
| 6846 | /* parent */ |
| 6847 | *pid_p = pid; |
| 6848 | # if ENABLE_HUSH_FAST |
| 6849 | G.count_SIGCHLD++; |
| 6850 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6851 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6852 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6853 | # endif |
| 6854 | enable_restore_tty_pgrp_on_exit(); |
| 6855 | # if !BB_MMU |
| 6856 | free(to_free); |
| 6857 | # endif |
| 6858 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6859 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6860 | } |
| 6861 | |
| 6862 | /* Return code is exit status of the process that is run. */ |
| 6863 | static int process_command_subs(o_string *dest, const char *s) |
| 6864 | { |
| 6865 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6866 | pid_t pid; |
| 6867 | int status, ch, eol_cnt; |
| 6868 | |
| 6869 | fp = generate_stream_from_string(s, &pid); |
| 6870 | |
| 6871 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6872 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6873 | while ((ch = getc(fp)) != EOF) { |
| 6874 | if (ch == '\0') |
| 6875 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6876 | if (ch == '\n') { |
| 6877 | eol_cnt++; |
| 6878 | continue; |
| 6879 | } |
| 6880 | while (eol_cnt) { |
| 6881 | o_addchr(dest, '\n'); |
| 6882 | eol_cnt--; |
| 6883 | } |
| 6884 | o_addQchr(dest, ch); |
| 6885 | } |
| 6886 | |
| 6887 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6888 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6889 | /* We need to extract exitcode. Test case |
| 6890 | * "true; echo `sleep 1; false` $?" |
| 6891 | * should print 1 */ |
| 6892 | safe_waitpid(pid, &status, 0); |
| 6893 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6894 | return WEXITSTATUS(status); |
| 6895 | } |
| 6896 | #endif /* ENABLE_HUSH_TICK */ |
| 6897 | |
| 6898 | |
| 6899 | static void setup_heredoc(struct redir_struct *redir) |
| 6900 | { |
| 6901 | struct fd_pair pair; |
| 6902 | pid_t pid; |
| 6903 | int len, written; |
| 6904 | /* the _body_ of heredoc (misleading field name) */ |
| 6905 | const char *heredoc = redir->rd_filename; |
| 6906 | char *expanded; |
| 6907 | #if !BB_MMU |
| 6908 | char **to_free; |
| 6909 | #endif |
| 6910 | |
| 6911 | expanded = NULL; |
| 6912 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6913 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6914 | if (expanded) |
| 6915 | heredoc = expanded; |
| 6916 | } |
| 6917 | len = strlen(heredoc); |
| 6918 | |
| 6919 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6920 | xpiped_pair(pair); |
| 6921 | xmove_fd(pair.rd, redir->rd_fd); |
| 6922 | |
| 6923 | /* Try writing without forking. Newer kernels have |
| 6924 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6925 | ndelay_on(pair.wr); |
| 6926 | while (1) { |
| 6927 | written = write(pair.wr, heredoc, len); |
| 6928 | if (written <= 0) |
| 6929 | break; |
| 6930 | len -= written; |
| 6931 | if (len == 0) { |
| 6932 | close(pair.wr); |
| 6933 | free(expanded); |
| 6934 | return; |
| 6935 | } |
| 6936 | heredoc += written; |
| 6937 | } |
| 6938 | ndelay_off(pair.wr); |
| 6939 | |
| 6940 | /* Okay, pipe buffer was not big enough */ |
| 6941 | /* Note: we must not create a stray child (bastard? :) |
| 6942 | * for the unsuspecting parent process. Child creates a grandchild |
| 6943 | * and exits before parent execs the process which consumes heredoc |
| 6944 | * (that exec happens after we return from this function) */ |
| 6945 | #if !BB_MMU |
| 6946 | to_free = NULL; |
| 6947 | #endif |
| 6948 | pid = xvfork(); |
| 6949 | if (pid == 0) { |
| 6950 | /* child */ |
| 6951 | disable_restore_tty_pgrp_on_exit(); |
| 6952 | pid = BB_MMU ? xfork() : xvfork(); |
| 6953 | if (pid != 0) |
| 6954 | _exit(0); |
| 6955 | /* grandchild */ |
| 6956 | close(redir->rd_fd); /* read side of the pipe */ |
| 6957 | #if BB_MMU |
| 6958 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6959 | _exit(0); |
| 6960 | #else |
| 6961 | /* Delegate blocking writes to another process */ |
| 6962 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6963 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6964 | #endif |
| 6965 | } |
| 6966 | /* parent */ |
| 6967 | #if ENABLE_HUSH_FAST |
| 6968 | G.count_SIGCHLD++; |
| 6969 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6970 | #endif |
| 6971 | enable_restore_tty_pgrp_on_exit(); |
| 6972 | #if !BB_MMU |
| 6973 | free(to_free); |
| 6974 | #endif |
| 6975 | close(pair.wr); |
| 6976 | free(expanded); |
| 6977 | wait(NULL); /* wait till child has died */ |
| 6978 | } |
| 6979 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6980 | struct squirrel { |
| 6981 | int orig_fd; |
| 6982 | int moved_to; |
| 6983 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 6984 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 6985 | }; |
| 6986 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6987 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 6988 | { |
| 6989 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 6990 | sq[i].orig_fd = orig; |
| 6991 | sq[i].moved_to = moved; |
| 6992 | sq[i+1].orig_fd = -1; /* end marker */ |
| 6993 | return sq; |
| 6994 | } |
| 6995 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6996 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 6997 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6998 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6999 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7000 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7001 | i = 0; |
| 7002 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7003 | /* If we collide with an already moved fd... */ |
| 7004 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7005 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7006 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 7007 | if (sq[i].moved_to < 0) /* what? */ |
| 7008 | xfunc_die(); |
| 7009 | return sq; |
| 7010 | } |
| 7011 | if (fd == sq[i].orig_fd) { |
| 7012 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 7013 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 7014 | return sq; |
| 7015 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7016 | } |
| 7017 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7018 | /* 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] | 7019 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7020 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 7021 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7022 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7023 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7024 | } |
| 7025 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7026 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 7027 | { |
| 7028 | int i; |
| 7029 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 7030 | i = 0; |
| 7031 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7032 | /* If we collide with an already moved fd... */ |
| 7033 | if (fd == sq[i].orig_fd) { |
| 7034 | /* Examples: |
| 7035 | * "echo 3>FILE 3>&- 3>FILE" |
| 7036 | * "echo 3>&- 3>FILE" |
| 7037 | * No need for last redirect to insert |
| 7038 | * another "need to close 3" indicator. |
| 7039 | */ |
| 7040 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 7041 | return sq; |
| 7042 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7043 | } |
| 7044 | |
| 7045 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 7046 | return append_squirrel(sq, i, fd, -1); |
| 7047 | } |
| 7048 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7049 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 7050 | * Move all conflicting internally used fds, |
| 7051 | * and remember them so that we can restore them later. |
| 7052 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7053 | 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] | 7054 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7055 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 7056 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7057 | |
| 7058 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7059 | if (fd == G.interactive_fd) { |
| 7060 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7061 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7062 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 7063 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7064 | } |
| 7065 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7066 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 7067 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 7068 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7069 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 7070 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7071 | * "fileno(fd) = new_fd" can't be done. |
| 7072 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7073 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7074 | return 0; |
| 7075 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7076 | /* If this one of script's fds? */ |
| 7077 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 7078 | return 1; /* yes. "we closed fd" */ |
| 7079 | |
| 7080 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7081 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7082 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7083 | } |
| 7084 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7085 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7086 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7087 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7088 | int i; |
| 7089 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7090 | if (sq[i].moved_to >= 0) { |
| 7091 | /* We simply die on error */ |
| 7092 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7093 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7094 | } else { |
| 7095 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7096 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7097 | close(sq[i].orig_fd); |
| 7098 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7099 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7100 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7101 | } |
| 7102 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7103 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7104 | |
| 7105 | restore_redirected_FILEs(); |
| 7106 | } |
| 7107 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7108 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7109 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7110 | { |
| 7111 | if (G_interactive_fd) |
| 7112 | close(G_interactive_fd); |
| 7113 | close_all_FILE_list(); |
| 7114 | } |
| 7115 | #endif |
| 7116 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7117 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7118 | { |
| 7119 | int i; |
| 7120 | |
| 7121 | #if ENABLE_HUSH_INTERACTIVE |
| 7122 | if (fd == G.interactive_fd) |
| 7123 | return 1; |
| 7124 | #endif |
| 7125 | /* If this one of script's fds? */ |
| 7126 | if (fd_in_FILEs(fd)) |
| 7127 | return 1; |
| 7128 | |
| 7129 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7130 | if (fd == sq[i].moved_to) |
| 7131 | return 1; |
| 7132 | } |
| 7133 | return 0; |
| 7134 | } |
| 7135 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7136 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7137 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7138 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7139 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7140 | struct redir_struct *redir; |
| 7141 | |
| 7142 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7143 | int newfd; |
| 7144 | int closed; |
| 7145 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7146 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7147 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7148 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7149 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7150 | * of the heredoc */ |
| 7151 | debug_printf_parse("set heredoc '%s'\n", |
| 7152 | redir->rd_filename); |
| 7153 | setup_heredoc(redir); |
| 7154 | continue; |
| 7155 | } |
| 7156 | |
| 7157 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7158 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7159 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7160 | int mode; |
| 7161 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7162 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7163 | /* |
| 7164 | * Examples: |
| 7165 | * "cmd >" (no filename) |
| 7166 | * "cmd > <file" (2nd redirect starts too early) |
| 7167 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7168 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7169 | continue; |
| 7170 | } |
| 7171 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7172 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7173 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7174 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7175 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7176 | /* Error message from open_or_warn can be lost |
| 7177 | * if stderr has been redirected, but bash |
| 7178 | * and ash both lose it as well |
| 7179 | * (though zsh doesn't!) |
| 7180 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7181 | return 1; |
| 7182 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7183 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7184 | /* open() gave us precisely the fd we wanted. |
| 7185 | * This means that this fd was not busy |
| 7186 | * (not opened to anywhere). |
| 7187 | * Remember to close it on restore: |
| 7188 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7189 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7190 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7191 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7192 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7193 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7194 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7195 | } |
| 7196 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7197 | if (newfd == redir->rd_fd) |
| 7198 | continue; |
| 7199 | |
| 7200 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7201 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7202 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7203 | |
| 7204 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7205 | if (newfd == REDIRFD_CLOSE) { |
| 7206 | /* "N>&-" means "close me" */ |
| 7207 | if (!closed) { |
| 7208 | /* ^^^ optimization: saving may already |
| 7209 | * have closed it. If not... */ |
| 7210 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7211 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7212 | /* Sometimes we do another close on restore, getting EBADF. |
| 7213 | * Consider "echo 3>FILE 3>&-" |
| 7214 | * first redirect remembers "need to close 3", |
| 7215 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7216 | */ |
| 7217 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7218 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7219 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7220 | //errno = EBADF; |
| 7221 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7222 | newfd = -1; /* same effect as code above */ |
| 7223 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7224 | xdup2(newfd, redir->rd_fd); |
| 7225 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7226 | /* "rd_fd > FILE" */ |
| 7227 | close(newfd); |
| 7228 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7229 | } |
| 7230 | } |
| 7231 | return 0; |
| 7232 | } |
| 7233 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7234 | static char *find_in_path(const char *arg) |
| 7235 | { |
| 7236 | char *ret = NULL; |
| 7237 | const char *PATH = get_local_var_value("PATH"); |
| 7238 | |
| 7239 | if (!PATH) |
| 7240 | return NULL; |
| 7241 | |
| 7242 | while (1) { |
| 7243 | const char *end = strchrnul(PATH, ':'); |
| 7244 | int sz = end - PATH; /* must be int! */ |
| 7245 | |
| 7246 | free(ret); |
| 7247 | if (sz != 0) { |
| 7248 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7249 | } else { |
| 7250 | /* We have xxx::yyyy in $PATH, |
| 7251 | * it means "use current dir" */ |
| 7252 | ret = xstrdup(arg); |
| 7253 | } |
| 7254 | if (access(ret, F_OK) == 0) |
| 7255 | break; |
| 7256 | |
| 7257 | if (*end == '\0') { |
| 7258 | free(ret); |
| 7259 | return NULL; |
| 7260 | } |
| 7261 | PATH = end + 1; |
| 7262 | } |
| 7263 | |
| 7264 | return ret; |
| 7265 | } |
| 7266 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7267 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7268 | const struct built_in_command *x, |
| 7269 | const struct built_in_command *end) |
| 7270 | { |
| 7271 | while (x != end) { |
| 7272 | if (strcmp(name, x->b_cmd) != 0) { |
| 7273 | x++; |
| 7274 | continue; |
| 7275 | } |
| 7276 | debug_printf_exec("found builtin '%s'\n", name); |
| 7277 | return x; |
| 7278 | } |
| 7279 | return NULL; |
| 7280 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7281 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7282 | { |
| 7283 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7284 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7285 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7286 | { |
| 7287 | const struct built_in_command *x = find_builtin1(name); |
| 7288 | if (x) |
| 7289 | return x; |
| 7290 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7291 | } |
| 7292 | |
| 7293 | #if ENABLE_HUSH_FUNCTIONS |
| 7294 | static struct function **find_function_slot(const char *name) |
| 7295 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7296 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7297 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7298 | |
| 7299 | while ((funcp = *funcpp) != NULL) { |
| 7300 | if (strcmp(name, funcp->name) == 0) { |
| 7301 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7302 | break; |
| 7303 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7304 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7305 | } |
| 7306 | return funcpp; |
| 7307 | } |
| 7308 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7309 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7310 | { |
| 7311 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7312 | return funcp; |
| 7313 | } |
| 7314 | |
| 7315 | /* Note: takes ownership on name ptr */ |
| 7316 | static struct function *new_function(char *name) |
| 7317 | { |
| 7318 | struct function **funcpp = find_function_slot(name); |
| 7319 | struct function *funcp = *funcpp; |
| 7320 | |
| 7321 | if (funcp != NULL) { |
| 7322 | struct command *cmd = funcp->parent_cmd; |
| 7323 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7324 | if (!cmd) { |
| 7325 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7326 | free(funcp->name); |
| 7327 | /* Note: if !funcp->body, do not free body_as_string! |
| 7328 | * This is a special case of "-F name body" function: |
| 7329 | * body_as_string was not malloced! */ |
| 7330 | if (funcp->body) { |
| 7331 | free_pipe_list(funcp->body); |
| 7332 | # if !BB_MMU |
| 7333 | free(funcp->body_as_string); |
| 7334 | # endif |
| 7335 | } |
| 7336 | } else { |
| 7337 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7338 | cmd->argv[0] = funcp->name; |
| 7339 | cmd->group = funcp->body; |
| 7340 | # if !BB_MMU |
| 7341 | cmd->group_as_string = funcp->body_as_string; |
| 7342 | # endif |
| 7343 | } |
| 7344 | } else { |
| 7345 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7346 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7347 | /*funcp->next = NULL;*/ |
| 7348 | } |
| 7349 | |
| 7350 | funcp->name = name; |
| 7351 | return funcp; |
| 7352 | } |
| 7353 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7354 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7355 | static void unset_func(const char *name) |
| 7356 | { |
| 7357 | struct function **funcpp = find_function_slot(name); |
| 7358 | struct function *funcp = *funcpp; |
| 7359 | |
| 7360 | if (funcp != NULL) { |
| 7361 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7362 | *funcpp = funcp->next; |
| 7363 | /* funcp is unlinked now, deleting it. |
| 7364 | * Note: if !funcp->body, the function was created by |
| 7365 | * "-F name body", do not free ->body_as_string |
| 7366 | * and ->name as they were not malloced. */ |
| 7367 | if (funcp->body) { |
| 7368 | free_pipe_list(funcp->body); |
| 7369 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7370 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7371 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7372 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7373 | } |
| 7374 | free(funcp); |
| 7375 | } |
| 7376 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7377 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7378 | |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7379 | static void remove_nested_vars(void) |
| 7380 | { |
| 7381 | struct variable *cur; |
| 7382 | struct variable **cur_pp; |
| 7383 | |
| 7384 | cur_pp = &G.top_var; |
| 7385 | while ((cur = *cur_pp) != NULL) { |
| 7386 | if (cur->var_nest_level <= G.var_nest_level) { |
| 7387 | cur_pp = &cur->next; |
| 7388 | continue; |
| 7389 | } |
| 7390 | /* Unexport */ |
| 7391 | if (cur->flg_export) { |
| 7392 | debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7393 | bb_unsetenv(cur->varstr); |
| 7394 | } |
| 7395 | /* Remove from global list */ |
| 7396 | *cur_pp = cur->next; |
| 7397 | /* Free */ |
| 7398 | if (!cur->max_len) { |
| 7399 | debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); |
| 7400 | free(cur->varstr); |
| 7401 | } |
| 7402 | free(cur); |
| 7403 | } |
| 7404 | } |
| 7405 | |
| 7406 | static void enter_var_nest_level(void) |
| 7407 | { |
| 7408 | G.var_nest_level++; |
| 7409 | debug_printf_env("var_nest_level++ %u\n", G.var_nest_level); |
| 7410 | |
| 7411 | /* Try: f() { echo -n .; f; }; f |
| 7412 | * struct variable::var_nest_level is uint16_t, |
| 7413 | * thus limiting recursion to < 2^16. |
| 7414 | * In any case, with 8 Mbyte stack SEGV happens |
| 7415 | * not too long after 2^16 recursions anyway. |
| 7416 | */ |
| 7417 | if (G.var_nest_level > 0xff00) |
| 7418 | bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level); |
| 7419 | } |
| 7420 | |
| 7421 | static void leave_var_nest_level(void) |
| 7422 | { |
| 7423 | G.var_nest_level--; |
| 7424 | debug_printf_env("var_nest_level-- %u\n", G.var_nest_level); |
| 7425 | if (HUSH_DEBUG && (int)G.var_nest_level < 0) |
| 7426 | bb_error_msg_and_die("BUG: nesting underflow"); |
| 7427 | |
| 7428 | remove_nested_vars(); |
| 7429 | } |
| 7430 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7431 | # if BB_MMU |
| 7432 | #define exec_function(to_free, funcp, argv) \ |
| 7433 | exec_function(funcp, argv) |
| 7434 | # endif |
| 7435 | static void exec_function(char ***to_free, |
| 7436 | const struct function *funcp, |
| 7437 | char **argv) NORETURN; |
| 7438 | static void exec_function(char ***to_free, |
| 7439 | const struct function *funcp, |
| 7440 | char **argv) |
| 7441 | { |
| 7442 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7443 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7444 | |
| 7445 | argv[0] = G.global_argv[0]; |
| 7446 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7447 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7448 | |
| 7449 | // Example when we are here: "cmd | func" |
| 7450 | // func will run with saved-redirect fds open. |
| 7451 | // $ f() { echo /proc/self/fd/*; } |
| 7452 | // $ true | f |
| 7453 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7454 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7455 | // Same in script: |
| 7456 | // $ . ./SCRIPT |
| 7457 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7458 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7459 | // They are CLOEXEC so external programs won't see them, but |
| 7460 | // for "more correctness" we might want to close those extra fds here: |
| 7461 | //? close_saved_fds_and_FILE_fds(); |
| 7462 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7463 | /* "we are in a function, ok to use return" */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7464 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7465 | enter_var_nest_level(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7466 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7467 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7468 | /* On MMU, funcp->body is always non-NULL */ |
| 7469 | n = run_list(funcp->body); |
| 7470 | fflush_all(); |
| 7471 | _exit(n); |
| 7472 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7473 | //? close_saved_fds_and_FILE_fds(); |
| 7474 | |
| 7475 | //TODO: check whether "true | func_with_return" works |
| 7476 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7477 | re_execute_shell(to_free, |
| 7478 | funcp->body_as_string, |
| 7479 | G.global_argv[0], |
| 7480 | argv + 1, |
| 7481 | NULL); |
| 7482 | # endif |
| 7483 | } |
| 7484 | |
| 7485 | static int run_function(const struct function *funcp, char **argv) |
| 7486 | { |
| 7487 | int rc; |
| 7488 | save_arg_t sv; |
| 7489 | smallint sv_flg; |
| 7490 | |
| 7491 | save_and_replace_G_args(&sv, argv); |
| 7492 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7493 | /* "We are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7494 | sv_flg = G_flag_return_in_progress; |
| 7495 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7496 | |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7497 | /* Make "local" variables properly shadow previous ones */ |
| 7498 | IF_HUSH_LOCAL(enter_var_nest_level();) |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7499 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7500 | |
| 7501 | /* On MMU, funcp->body is always non-NULL */ |
| 7502 | # if !BB_MMU |
| 7503 | if (!funcp->body) { |
| 7504 | /* Function defined by -F */ |
| 7505 | parse_and_run_string(funcp->body_as_string); |
| 7506 | rc = G.last_exitcode; |
| 7507 | } else |
| 7508 | # endif |
| 7509 | { |
| 7510 | rc = run_list(funcp->body); |
| 7511 | } |
| 7512 | |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 7513 | IF_HUSH_LOCAL(G.func_nest_level--;) |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 7514 | IF_HUSH_LOCAL(leave_var_nest_level();) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7515 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7516 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7517 | |
| 7518 | restore_G_args(&sv, argv); |
| 7519 | |
| 7520 | return rc; |
| 7521 | } |
| 7522 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7523 | |
| 7524 | |
| 7525 | #if BB_MMU |
| 7526 | #define exec_builtin(to_free, x, argv) \ |
| 7527 | exec_builtin(x, argv) |
| 7528 | #else |
| 7529 | #define exec_builtin(to_free, x, argv) \ |
| 7530 | exec_builtin(to_free, argv) |
| 7531 | #endif |
| 7532 | static void exec_builtin(char ***to_free, |
| 7533 | const struct built_in_command *x, |
| 7534 | char **argv) NORETURN; |
| 7535 | static void exec_builtin(char ***to_free, |
| 7536 | const struct built_in_command *x, |
| 7537 | char **argv) |
| 7538 | { |
| 7539 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7540 | int rcode; |
| 7541 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7542 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7543 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7544 | fflush_all(); |
| 7545 | _exit(rcode); |
| 7546 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7547 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7548 | /* On NOMMU, we must never block! |
| 7549 | * Example: { sleep 99 | read line; } & echo Ok |
| 7550 | */ |
| 7551 | re_execute_shell(to_free, |
| 7552 | argv[0], |
| 7553 | G.global_argv[0], |
| 7554 | G.global_argv + 1, |
| 7555 | argv); |
| 7556 | #endif |
| 7557 | } |
| 7558 | |
| 7559 | |
| 7560 | static void execvp_or_die(char **argv) NORETURN; |
| 7561 | static void execvp_or_die(char **argv) |
| 7562 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7563 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7564 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7565 | /* Don't propagate SIG_IGN to the child */ |
| 7566 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7567 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7568 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7569 | e = 2; |
| 7570 | if (errno == EACCES) e = 126; |
| 7571 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7572 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7573 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7574 | } |
| 7575 | |
| 7576 | #if ENABLE_HUSH_MODE_X |
| 7577 | static void dump_cmd_in_x_mode(char **argv) |
| 7578 | { |
| 7579 | if (G_x_mode && argv) { |
| 7580 | /* We want to output the line in one write op */ |
| 7581 | char *buf, *p; |
| 7582 | int len; |
| 7583 | int n; |
| 7584 | |
| 7585 | len = 3; |
| 7586 | n = 0; |
| 7587 | while (argv[n]) |
| 7588 | len += strlen(argv[n++]) + 1; |
| 7589 | buf = xmalloc(len); |
| 7590 | buf[0] = '+'; |
| 7591 | p = buf + 1; |
| 7592 | n = 0; |
| 7593 | while (argv[n]) |
| 7594 | p += sprintf(p, " %s", argv[n++]); |
| 7595 | *p++ = '\n'; |
| 7596 | *p = '\0'; |
| 7597 | fputs(buf, stderr); |
| 7598 | free(buf); |
| 7599 | } |
| 7600 | } |
| 7601 | #else |
| 7602 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7603 | #endif |
| 7604 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7605 | #if ENABLE_HUSH_COMMAND |
| 7606 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7607 | { |
| 7608 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7609 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7610 | if (!opt_vV) |
| 7611 | return; |
| 7612 | |
| 7613 | to_free = NULL; |
| 7614 | if (!explanation) { |
| 7615 | char *path = getenv("PATH"); |
| 7616 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7617 | if (!explanation) |
| 7618 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7619 | if (opt_vV != 'V') |
| 7620 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7621 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7622 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7623 | free(to_free); |
| 7624 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7625 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7626 | } |
| 7627 | #else |
| 7628 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7629 | #endif |
| 7630 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7631 | #if BB_MMU |
| 7632 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7633 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7634 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7635 | pseudo_exec(command, argv_expanded) |
| 7636 | #endif |
| 7637 | |
| 7638 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7639 | * Never returns. |
| 7640 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7641 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7642 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7643 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7644 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7645 | char **argv, int assignment_cnt, |
| 7646 | char **argv_expanded) NORETURN; |
| 7647 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7648 | char **argv, int assignment_cnt, |
| 7649 | char **argv_expanded) |
| 7650 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7651 | const struct built_in_command *x; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7652 | struct variable **sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7653 | char **new_env; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7654 | IF_HUSH_COMMAND(char opt_vV = 0;) |
| 7655 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7656 | |
| 7657 | new_env = expand_assignments(argv, assignment_cnt); |
| 7658 | dump_cmd_in_x_mode(new_env); |
| 7659 | |
| 7660 | if (!argv[assignment_cnt]) { |
| 7661 | /* Case when we are here: ... | var=val | ... |
| 7662 | * (note that we do not exit early, i.e., do not optimize out |
| 7663 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7664 | */ |
| 7665 | free_strings(new_env); |
| 7666 | _exit(EXIT_SUCCESS); |
| 7667 | } |
| 7668 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7669 | sv_shadowed = G.shadowed_vars_pp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7670 | #if BB_MMU |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7671 | G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7672 | #else |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7673 | G.shadowed_vars_pp = &nommu_save->old_vars; |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 7674 | G.var_nest_level++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7675 | #endif |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 7676 | set_vars_and_save_old(new_env); |
| 7677 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7678 | |
| 7679 | if (argv_expanded) { |
| 7680 | argv = argv_expanded; |
| 7681 | } else { |
| 7682 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7683 | #if !BB_MMU |
| 7684 | nommu_save->argv = argv; |
| 7685 | #endif |
| 7686 | } |
| 7687 | dump_cmd_in_x_mode(argv); |
| 7688 | |
| 7689 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7690 | if (strchr(argv[0], '/') != NULL) |
| 7691 | goto skip; |
| 7692 | #endif |
| 7693 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7694 | #if ENABLE_HUSH_FUNCTIONS |
| 7695 | /* Check if the command matches any functions (this goes before bltins) */ |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 7696 | funcp = find_function(argv[0]); |
| 7697 | if (funcp) |
| 7698 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7699 | #endif |
| 7700 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7701 | #if ENABLE_HUSH_COMMAND |
| 7702 | /* "command BAR": run BAR without looking it up among functions |
| 7703 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 7704 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 7705 | */ |
| 7706 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 7707 | char *p; |
| 7708 | |
| 7709 | argv++; |
| 7710 | p = *argv; |
| 7711 | if (p[0] != '-' || !p[1]) |
| 7712 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 7713 | |
| 7714 | for (;;) { |
| 7715 | p++; |
| 7716 | switch (*p) { |
| 7717 | case '\0': |
| 7718 | argv++; |
| 7719 | p = *argv; |
| 7720 | if (p[0] != '-' || !p[1]) |
| 7721 | goto after_opts; |
| 7722 | continue; /* next arg is also -opts, process it too */ |
| 7723 | case 'v': |
| 7724 | case 'V': |
| 7725 | opt_vV = *p; |
| 7726 | continue; |
| 7727 | default: |
| 7728 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 7729 | } |
| 7730 | } |
| 7731 | } |
| 7732 | after_opts: |
| 7733 | # if ENABLE_HUSH_FUNCTIONS |
| 7734 | if (opt_vV && find_function(argv[0])) |
| 7735 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 7736 | # endif |
| 7737 | #endif |
| 7738 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7739 | /* Check if the command matches any of the builtins. |
| 7740 | * Depending on context, this might be redundant. But it's |
| 7741 | * easier to waste a few CPU cycles than it is to figure out |
| 7742 | * if this is one of those cases. |
| 7743 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7744 | /* Why "BB_MMU ? :" difference in logic? - |
| 7745 | * On NOMMU, it is more expensive to re-execute shell |
| 7746 | * just in order to run echo or test builtin. |
| 7747 | * It's better to skip it here and run corresponding |
| 7748 | * non-builtin later. */ |
| 7749 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7750 | if (x) { |
| 7751 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 7752 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7753 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7754 | |
| 7755 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7756 | /* Check if the command matches any busybox applets */ |
| 7757 | { |
| 7758 | int a = find_applet_by_name(argv[0]); |
| 7759 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7760 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7761 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7762 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7763 | /* Do not leak open fds from opened script files etc. |
| 7764 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7765 | * should not show tty fd open. |
| 7766 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7767 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7768 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7769 | //This casuses test failures in |
| 7770 | //redir_children_should_not_see_saved_fd_2.tests |
| 7771 | //redir_children_should_not_see_saved_fd_3.tests |
| 7772 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7773 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7774 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7775 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7776 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7777 | } |
| 7778 | # endif |
| 7779 | /* Re-exec ourselves */ |
| 7780 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7781 | /* Don't propagate SIG_IGN to the child */ |
| 7782 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7783 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7784 | execv(bb_busybox_exec_path, argv); |
| 7785 | /* If they called chroot or otherwise made the binary no longer |
| 7786 | * executable, fall through */ |
| 7787 | } |
| 7788 | } |
| 7789 | #endif |
| 7790 | |
| 7791 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7792 | skip: |
| 7793 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7794 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7795 | execvp_or_die(argv); |
| 7796 | } |
| 7797 | |
| 7798 | /* Called after [v]fork() in run_pipe |
| 7799 | */ |
| 7800 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7801 | struct command *command, |
| 7802 | char **argv_expanded) NORETURN; |
| 7803 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7804 | struct command *command, |
| 7805 | char **argv_expanded) |
| 7806 | { |
Denys Vlasenko | 49015a6 | 2018-04-03 13:02:43 +0200 | [diff] [blame] | 7807 | #if ENABLE_HUSH_FUNCTIONS |
| 7808 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7809 | /* Ignore funcdefs in pipes: |
| 7810 | * true | f() { cmd } |
| 7811 | */ |
| 7812 | _exit(0); |
| 7813 | } |
| 7814 | #endif |
| 7815 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7816 | if (command->argv) { |
| 7817 | pseudo_exec_argv(nommu_save, command->argv, |
| 7818 | command->assignment_cnt, argv_expanded); |
| 7819 | } |
| 7820 | |
| 7821 | if (command->group) { |
| 7822 | /* Cases when we are here: |
| 7823 | * ( list ) |
| 7824 | * { list } & |
| 7825 | * ... | ( list ) | ... |
| 7826 | * ... | { list } | ... |
| 7827 | */ |
| 7828 | #if BB_MMU |
| 7829 | int rcode; |
| 7830 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7831 | reset_traps_to_defaults(); |
| 7832 | rcode = run_list(command->group); |
| 7833 | /* OK to leak memory by not calling free_pipe_list, |
| 7834 | * since this process is about to exit */ |
| 7835 | _exit(rcode); |
| 7836 | #else |
| 7837 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7838 | command->group_as_string, |
| 7839 | G.global_argv[0], |
| 7840 | G.global_argv + 1, |
| 7841 | NULL); |
| 7842 | #endif |
| 7843 | } |
| 7844 | |
| 7845 | /* Case when we are here: ... | >file */ |
| 7846 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7847 | _exit(EXIT_SUCCESS); |
| 7848 | } |
| 7849 | |
| 7850 | #if ENABLE_HUSH_JOB |
| 7851 | static const char *get_cmdtext(struct pipe *pi) |
| 7852 | { |
| 7853 | char **argv; |
| 7854 | char *p; |
| 7855 | int len; |
| 7856 | |
| 7857 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7858 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7859 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7860 | if (pi->cmdtext) |
| 7861 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7862 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7863 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7864 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7865 | pi->cmdtext = xzalloc(1); |
| 7866 | return pi->cmdtext; |
| 7867 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7868 | len = 0; |
| 7869 | do { |
| 7870 | len += strlen(*argv) + 1; |
| 7871 | } while (*++argv); |
| 7872 | p = xmalloc(len); |
| 7873 | pi->cmdtext = p; |
| 7874 | argv = pi->cmds[0].argv; |
| 7875 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7876 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7877 | *p++ = ' '; |
| 7878 | } while (*++argv); |
| 7879 | p[-1] = '\0'; |
| 7880 | return pi->cmdtext; |
| 7881 | } |
| 7882 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7883 | static void remove_job_from_table(struct pipe *pi) |
| 7884 | { |
| 7885 | struct pipe *prev_pipe; |
| 7886 | |
| 7887 | if (pi == G.job_list) { |
| 7888 | G.job_list = pi->next; |
| 7889 | } else { |
| 7890 | prev_pipe = G.job_list; |
| 7891 | while (prev_pipe->next != pi) |
| 7892 | prev_pipe = prev_pipe->next; |
| 7893 | prev_pipe->next = pi->next; |
| 7894 | } |
| 7895 | G.last_jobid = 0; |
| 7896 | if (G.job_list) |
| 7897 | G.last_jobid = G.job_list->jobid; |
| 7898 | } |
| 7899 | |
| 7900 | static void delete_finished_job(struct pipe *pi) |
| 7901 | { |
| 7902 | remove_job_from_table(pi); |
| 7903 | free_pipe(pi); |
| 7904 | } |
| 7905 | |
| 7906 | static void clean_up_last_dead_job(void) |
| 7907 | { |
| 7908 | if (G.job_list && !G.job_list->alive_cmds) |
| 7909 | delete_finished_job(G.job_list); |
| 7910 | } |
| 7911 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7912 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7913 | { |
| 7914 | struct pipe *job, **jobp; |
| 7915 | int i; |
| 7916 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7917 | clean_up_last_dead_job(); |
| 7918 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7919 | /* Find the end of the list, and find next job ID to use */ |
| 7920 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7921 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7922 | while ((job = *jobp) != NULL) { |
| 7923 | if (job->jobid > i) |
| 7924 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7925 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7926 | } |
| 7927 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7928 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7929 | /* Create a new job struct at the end */ |
| 7930 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7931 | job->next = NULL; |
| 7932 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7933 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7934 | for (i = 0; i < pi->num_cmds; i++) { |
| 7935 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7936 | /* all other fields are not used and stay zero */ |
| 7937 | } |
| 7938 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7939 | |
| 7940 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7941 | 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] | 7942 | G.last_jobid = job->jobid; |
| 7943 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7944 | #endif /* JOB */ |
| 7945 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7946 | static int job_exited_or_stopped(struct pipe *pi) |
| 7947 | { |
| 7948 | int rcode, i; |
| 7949 | |
| 7950 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7951 | return -1; |
| 7952 | |
| 7953 | /* All processes in fg pipe have exited or stopped */ |
| 7954 | rcode = 0; |
| 7955 | i = pi->num_cmds; |
| 7956 | while (--i >= 0) { |
| 7957 | rcode = pi->cmds[i].cmd_exitcode; |
| 7958 | /* usually last process gives overall exitstatus, |
| 7959 | * but with "set -o pipefail", last *failed* process does */ |
| 7960 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7961 | break; |
| 7962 | } |
| 7963 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7964 | return rcode; |
| 7965 | } |
| 7966 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7967 | 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] | 7968 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7969 | #if ENABLE_HUSH_JOB |
| 7970 | struct pipe *pi; |
| 7971 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7972 | int i, dead; |
| 7973 | |
| 7974 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7975 | |
| 7976 | #if DEBUG_JOBS |
| 7977 | if (WIFSTOPPED(status)) |
| 7978 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7979 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7980 | if (WIFSIGNALED(status)) |
| 7981 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7982 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7983 | if (WIFEXITED(status)) |
| 7984 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7985 | childpid, WEXITSTATUS(status)); |
| 7986 | #endif |
| 7987 | /* Were we asked to wait for a fg pipe? */ |
| 7988 | if (fg_pipe) { |
| 7989 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7990 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7991 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7992 | int rcode; |
| 7993 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7994 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7995 | if (fg_pipe->cmds[i].pid != childpid) |
| 7996 | continue; |
| 7997 | if (dead) { |
| 7998 | int ex; |
| 7999 | fg_pipe->cmds[i].pid = 0; |
| 8000 | fg_pipe->alive_cmds--; |
| 8001 | ex = WEXITSTATUS(status); |
| 8002 | /* bash prints killer signal's name for *last* |
| 8003 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 8004 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 8005 | */ |
| 8006 | if (WIFSIGNALED(status)) { |
| 8007 | int sig = WTERMSIG(status); |
| 8008 | if (i == fg_pipe->num_cmds-1) |
| 8009 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 8010 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 8011 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 8012 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 8013 | * Maybe we need to use sig | 128? */ |
| 8014 | ex = sig + 128; |
| 8015 | } |
| 8016 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 8017 | } else { |
| 8018 | fg_pipe->stopped_cmds++; |
| 8019 | } |
| 8020 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 8021 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8022 | rcode = job_exited_or_stopped(fg_pipe); |
| 8023 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8024 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 8025 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 8026 | * and "killall -STOP cat" */ |
| 8027 | if (G_interactive_fd) { |
| 8028 | #if ENABLE_HUSH_JOB |
| 8029 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8030 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8031 | #endif |
| 8032 | return rcode; |
| 8033 | } |
| 8034 | if (fg_pipe->alive_cmds == 0) |
| 8035 | return rcode; |
| 8036 | } |
| 8037 | /* There are still running processes in the fg_pipe */ |
| 8038 | return -1; |
| 8039 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 8040 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8041 | } |
| 8042 | |
| 8043 | #if ENABLE_HUSH_JOB |
| 8044 | /* We were asked to wait for bg or orphaned children */ |
| 8045 | /* No need to remember exitcode in this case */ |
| 8046 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8047 | for (i = 0; i < pi->num_cmds; i++) { |
| 8048 | if (pi->cmds[i].pid == childpid) |
| 8049 | goto found_pi_and_prognum; |
| 8050 | } |
| 8051 | } |
| 8052 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 8053 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 8054 | return -1; /* this wasn't a process from fg_pipe */ |
| 8055 | |
| 8056 | found_pi_and_prognum: |
| 8057 | if (dead) { |
| 8058 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8059 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8060 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8061 | rcode = 128 + WTERMSIG(status); |
| 8062 | pi->cmds[i].cmd_exitcode = rcode; |
| 8063 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 8064 | G.last_bg_pid_exitcode = rcode; |
| 8065 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8066 | pi->alive_cmds--; |
| 8067 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8068 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8069 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 8070 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 8071 | delete_finished_job(pi); |
| 8072 | } else { |
| 8073 | /* |
| 8074 | * bash deletes finished jobs from job table only in interactive mode, |
| 8075 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 8076 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 8077 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 8078 | * We only retain one "dead" job, if it's the single job on the list. |
| 8079 | * This covers most of real-world scenarios where this is useful. |
| 8080 | */ |
| 8081 | if (pi != G.job_list) |
| 8082 | delete_finished_job(pi); |
| 8083 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8084 | } |
| 8085 | } else { |
| 8086 | /* child stopped */ |
| 8087 | pi->stopped_cmds++; |
| 8088 | } |
| 8089 | #endif |
| 8090 | return -1; /* this wasn't a process from fg_pipe */ |
| 8091 | } |
| 8092 | |
| 8093 | /* Check to see if any processes have exited -- if they have, |
| 8094 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8095 | * |
| 8096 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 8097 | * Return its exitcode or zero if stopped. |
| 8098 | * |
| 8099 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 8100 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 8101 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8102 | * or 0 if no children changed status. |
| 8103 | * |
| 8104 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 8105 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 8106 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8107 | */ |
| 8108 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 8109 | { |
| 8110 | int attributes; |
| 8111 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8112 | int rcode = 0; |
| 8113 | |
| 8114 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 8115 | |
| 8116 | attributes = WUNTRACED; |
| 8117 | if (fg_pipe == NULL) |
| 8118 | attributes |= WNOHANG; |
| 8119 | |
| 8120 | errno = 0; |
| 8121 | #if ENABLE_HUSH_FAST |
| 8122 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8123 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8124 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8125 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8126 | /* Avoid doing waitpid syscall if possible */ |
| 8127 | if (!G.we_have_children) { |
| 8128 | errno = ECHILD; |
| 8129 | return -1; |
| 8130 | } |
| 8131 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8132 | /* We have children, but they did not exit |
| 8133 | * or stop yet (we saw no SIGCHLD) */ |
| 8134 | return 0; |
| 8135 | } |
| 8136 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8137 | } |
| 8138 | #endif |
| 8139 | |
| 8140 | /* Do we do this right? |
| 8141 | * bash-3.00# sleep 20 | false |
| 8142 | * <ctrl-Z pressed> |
| 8143 | * [3]+ Stopped sleep 20 | false |
| 8144 | * bash-3.00# echo $? |
| 8145 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8146 | * [hush 1.14.0: yes we do it right] |
| 8147 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8148 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8149 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8150 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8151 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8152 | i = G.count_SIGCHLD; |
| 8153 | #endif |
| 8154 | childpid = waitpid(-1, &status, attributes); |
| 8155 | if (childpid <= 0) { |
| 8156 | if (childpid && errno != ECHILD) |
| 8157 | bb_perror_msg("waitpid"); |
| 8158 | #if ENABLE_HUSH_FAST |
| 8159 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8160 | G.we_have_children = (childpid == 0); |
| 8161 | G.handled_SIGCHLD = i; |
| 8162 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8163 | } |
| 8164 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8165 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8166 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8167 | break; |
| 8168 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8169 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8170 | if (rcode >= 0) { |
| 8171 | /* fg_pipe exited or stopped */ |
| 8172 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8173 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8174 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8175 | 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] | 8176 | rcode = WEXITSTATUS(status); |
| 8177 | if (WIFSIGNALED(status)) |
| 8178 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8179 | if (WIFSTOPPED(status)) |
| 8180 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8181 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8182 | rcode++; |
| 8183 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8184 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8185 | /* This wasn't one of our processes, or */ |
| 8186 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8187 | } /* while (waitpid succeeds)... */ |
| 8188 | |
| 8189 | return rcode; |
| 8190 | } |
| 8191 | |
| 8192 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8193 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8194 | { |
| 8195 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8196 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8197 | if (G_saved_tty_pgrp) { |
| 8198 | /* Job finished, move the shell to the foreground */ |
| 8199 | p = getpgrp(); /* our process group id */ |
| 8200 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8201 | tcsetpgrp(G_interactive_fd, p); |
| 8202 | } |
| 8203 | return rcode; |
| 8204 | } |
| 8205 | #endif |
| 8206 | |
| 8207 | /* Start all the jobs, but don't wait for anything to finish. |
| 8208 | * See checkjobs(). |
| 8209 | * |
| 8210 | * Return code is normally -1, when the caller has to wait for children |
| 8211 | * to finish to determine the exit status of the pipe. If the pipe |
| 8212 | * is a simple builtin command, however, the action is done by the |
| 8213 | * time run_pipe returns, and the exit code is provided as the |
| 8214 | * return value. |
| 8215 | * |
| 8216 | * Returns -1 only if started some children. IOW: we have to |
| 8217 | * mask out retvals of builtins etc with 0xff! |
| 8218 | * |
| 8219 | * The only case when we do not need to [v]fork is when the pipe |
| 8220 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8221 | * cmd ; ... { list } ; ... |
| 8222 | * cmd && ... { list } && ... |
| 8223 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8224 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8225 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8226 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8227 | * |
| 8228 | * Cases when we must fork: |
| 8229 | * non-single: cmd | cmd |
| 8230 | * backgrounded: cmd & { list } & |
| 8231 | * subshell: ( list ) [&] |
| 8232 | */ |
| 8233 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8234 | #define redirect_and_varexp_helper(old_vars_p, command, squirrel, argv_expanded) \ |
| 8235 | redirect_and_varexp_helper(old_vars_p, command, squirrel) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8236 | #endif |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8237 | static int redirect_and_varexp_helper( |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8238 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8239 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8240 | char **argv_expanded) |
| 8241 | { |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8242 | /* Assignments occur before redirects. Try: |
| 8243 | * a=`sleep 1` sleep 2 3>/qwe/rty |
| 8244 | */ |
| 8245 | |
| 8246 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8247 | dump_cmd_in_x_mode(new_env); |
| 8248 | dump_cmd_in_x_mode(argv_expanded); |
| 8249 | /* this takes ownership of new_env[i] elements, and frees new_env: */ |
| 8250 | set_vars_and_save_old(new_env); |
| 8251 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8252 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8253 | * This is perfect for work that comes after exec(). |
| 8254 | * Is it really safe for inline use? Experimentally, |
| 8255 | * things seem to work. */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8256 | return setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8257 | } |
| 8258 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8259 | { |
| 8260 | static const char *const null_ptr = NULL; |
| 8261 | |
| 8262 | int cmd_no; |
| 8263 | int next_infd; |
| 8264 | struct command *command; |
| 8265 | char **argv_expanded; |
| 8266 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8267 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8268 | int rcode; |
| 8269 | |
| 8270 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8271 | debug_enter(); |
| 8272 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8273 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8274 | * Result should be 3 lines: q w e, qwe, q w e |
| 8275 | */ |
| 8276 | G.ifs = get_local_var_value("IFS"); |
| 8277 | if (!G.ifs) |
| 8278 | G.ifs = defifs; |
| 8279 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8280 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8281 | pi->stopped_cmds = 0; |
| 8282 | command = &pi->cmds[0]; |
| 8283 | argv_expanded = NULL; |
| 8284 | |
| 8285 | if (pi->num_cmds != 1 |
| 8286 | || pi->followup == PIPE_BG |
| 8287 | || command->cmd_type == CMD_SUBSHELL |
| 8288 | ) { |
| 8289 | goto must_fork; |
| 8290 | } |
| 8291 | |
| 8292 | pi->alive_cmds = 1; |
| 8293 | |
| 8294 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8295 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8296 | |
| 8297 | if (command->group) { |
| 8298 | #if ENABLE_HUSH_FUNCTIONS |
| 8299 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8300 | /* "executing" func () { list } */ |
| 8301 | struct function *funcp; |
| 8302 | |
| 8303 | funcp = new_function(command->argv[0]); |
| 8304 | /* funcp->name is already set to argv[0] */ |
| 8305 | funcp->body = command->group; |
| 8306 | # if !BB_MMU |
| 8307 | funcp->body_as_string = command->group_as_string; |
| 8308 | command->group_as_string = NULL; |
| 8309 | # endif |
| 8310 | command->group = NULL; |
| 8311 | command->argv[0] = NULL; |
| 8312 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8313 | funcp->parent_cmd = command; |
| 8314 | command->child_func = funcp; |
| 8315 | |
| 8316 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8317 | debug_leave(); |
| 8318 | return EXIT_SUCCESS; |
| 8319 | } |
| 8320 | #endif |
| 8321 | /* { list } */ |
| 8322 | debug_printf("non-subshell group\n"); |
| 8323 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8324 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8325 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8326 | //FIXME: we need to pass squirrel down into run_list() |
| 8327 | //for SH_STANDALONE case, or else this construct: |
| 8328 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8329 | //has no way of closing saved fd#1 for "find", |
| 8330 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8331 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8332 | rcode = run_list(command->group) & 0xff; |
| 8333 | } |
| 8334 | restore_redirects(squirrel); |
| 8335 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8336 | debug_leave(); |
| 8337 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8338 | return rcode; |
| 8339 | } |
| 8340 | |
| 8341 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8342 | { |
| 8343 | const struct built_in_command *x; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8344 | IF_HUSH_FUNCTIONS(const struct function *funcp;) |
| 8345 | IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8346 | struct variable **sv_shadowed; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8347 | struct variable *old_vars; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8348 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8349 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8350 | if (G.lineno_var) |
| 8351 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8352 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8353 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8354 | if (argv[command->assignment_cnt] == NULL) { |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8355 | /* Assignments, but no command. |
| 8356 | * Ensure redirects take effect (that is, create files). |
| 8357 | * Try "a=t >file" |
| 8358 | */ |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8359 | unsigned i; |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8360 | G.expand_exitcode = 0; |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8361 | only_assignments: |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8362 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8363 | restore_redirects(squirrel); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8364 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8365 | /* Set shell variables */ |
| 8366 | if (G_x_mode) |
| 8367 | bb_putchar_stderr('+'); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8368 | i = 0; |
| 8369 | while (i < command->assignment_cnt) { |
| 8370 | char *p = expand_string_to_string(argv[i], /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8371 | if (G_x_mode) |
| 8372 | fprintf(stderr, " %s", p); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8373 | debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8374 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8375 | /* assignment to readonly var / putenv error? */ |
| 8376 | rcode = 1; |
| 8377 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8378 | i++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8379 | } |
| 8380 | if (G_x_mode) |
| 8381 | bb_putchar_stderr('\n'); |
| 8382 | /* Redirect error sets $? to 1. Otherwise, |
| 8383 | * if evaluating assignment value set $?, retain it. |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8384 | * Else, clear $?: |
| 8385 | * false; q=`exit 2`; echo $? - should print 2 |
| 8386 | * false; x=1; echo $? - should print 0 |
| 8387 | * Because of the 2nd case, we can't just use G.last_exitcode. |
| 8388 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8389 | if (rcode == 0) |
Denys Vlasenko | 5fa0505 | 2018-04-03 11:21:13 +0200 | [diff] [blame] | 8390 | rcode = G.expand_exitcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8391 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8392 | debug_leave(); |
| 8393 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8394 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8395 | } |
| 8396 | |
| 8397 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 11752d4 | 2018-04-03 08:20:58 +0200 | [diff] [blame] | 8398 | #if defined(CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8399 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8400 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8401 | else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8402 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8403 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8404 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8405 | /* If someone gives us an empty string: `cmd with empty output` */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8406 | if (!argv_expanded[0]) { |
| 8407 | free(argv_expanded); |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 8408 | /* `false` still has to set exitcode 1 */ |
| 8409 | G.expand_exitcode = G.last_exitcode; |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8410 | goto only_assignments; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8411 | } |
| 8412 | |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8413 | old_vars = NULL; |
| 8414 | sv_shadowed = G.shadowed_vars_pp; |
| 8415 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8416 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8417 | IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);) |
| 8418 | IF_HUSH_FUNCTIONS(x = NULL;) |
| 8419 | IF_HUSH_FUNCTIONS(if (!funcp)) |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8420 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8421 | if (x || funcp) { |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8422 | if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8423 | debug_printf("exec with redirects only\n"); |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8424 | /* |
| 8425 | * Variable assignments are executed, but then "forgotten": |
| 8426 | * a=`sleep 1;echo A` exec 3>&-; echo $a |
| 8427 | * sleeps, but prints nothing. |
| 8428 | */ |
| 8429 | enter_var_nest_level(); |
| 8430 | G.shadowed_vars_pp = &old_vars; |
| 8431 | rcode = redirect_and_varexp_helper(command, /*squirrel:*/ NULL, argv_expanded); |
| 8432 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8433 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8434 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8435 | goto clean_up_and_ret1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8436 | } |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8437 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8438 | /* Bump var nesting, or this will leak exported $a: |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 8439 | * a=b true; env | grep ^a= |
| 8440 | */ |
| 8441 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8442 | /* Collect all variables "shadowed" by helper |
| 8443 | * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax) |
| 8444 | * into old_vars list: |
| 8445 | */ |
| 8446 | G.shadowed_vars_pp = &old_vars; |
| 8447 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8448 | if (rcode == 0) { |
| 8449 | if (!funcp) { |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8450 | /* Do not collect *to old_vars list* vars shadowed |
| 8451 | * by e.g. "local VAR" builtin (collect them |
| 8452 | * in the previously nested list instead): |
| 8453 | * don't want them to be restored immediately |
| 8454 | * after "local" completes. |
| 8455 | */ |
| 8456 | G.shadowed_vars_pp = sv_shadowed; |
| 8457 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8458 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8459 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8460 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8461 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8462 | fflush_all(); |
| 8463 | } |
| 8464 | #if ENABLE_HUSH_FUNCTIONS |
| 8465 | else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8466 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8467 | funcp->name, argv_expanded[1]); |
| 8468 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8469 | /* |
| 8470 | * But do collect *to old_vars list* vars shadowed |
| 8471 | * within function execution. To that end, restore |
| 8472 | * this pointer _after_ function run: |
| 8473 | */ |
| 8474 | G.shadowed_vars_pp = sv_shadowed; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8475 | } |
| 8476 | #endif |
| 8477 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8478 | } else |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8479 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8480 | int n = find_applet_by_name(argv_expanded[0]); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8481 | if (n < 0 || !APPLET_IS_NOFORK(n)) |
| 8482 | goto must_fork; |
| 8483 | |
| 8484 | enter_var_nest_level(); |
Denys Vlasenko | 929a41d | 2018-04-05 14:09:14 +0200 | [diff] [blame] | 8485 | /* Collect all variables "shadowed" by helper into old_vars list */ |
| 8486 | G.shadowed_vars_pp = &old_vars; |
| 8487 | rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded); |
| 8488 | G.shadowed_vars_pp = sv_shadowed; |
| 8489 | |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8490 | if (rcode == 0) { |
| 8491 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8492 | argv_expanded[0], argv_expanded[1]); |
| 8493 | /* |
| 8494 | * Note: signals (^C) can't interrupt here. |
| 8495 | * We remember them and they will be acted upon |
| 8496 | * after applet returns. |
| 8497 | * This makes applets which can run for a long time |
| 8498 | * and/or wait for user input ineligible for NOFORK: |
| 8499 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8500 | */ |
| 8501 | rcode = run_nofork_applet(n, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8502 | } |
Denys Vlasenko | 4e1dc53 | 2018-04-05 13:10:34 +0200 | [diff] [blame] | 8503 | } else |
| 8504 | goto must_fork; |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8505 | |
Denys Vlasenko | 41d8f10 | 2018-04-05 14:41:21 +0200 | [diff] [blame] | 8506 | restore_redirects(squirrel); |
| 8507 | clean_up_and_ret1: |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8508 | leave_var_nest_level(); |
| 8509 | add_vars(old_vars); |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8510 | |
| 8511 | /* |
| 8512 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8513 | * with FEATURE_SH_NOFORK=y. |
| 8514 | */ |
| 8515 | if (!funcp) { |
| 8516 | /* It was builtin or nofork. |
| 8517 | * if this would be a real fork/execed program, |
| 8518 | * it should have died if a fatal sig was received. |
| 8519 | * But OTOH, there was no separate process, |
| 8520 | * the sig was sent to _shell_, not to non-existing |
| 8521 | * child. |
| 8522 | * Let's just handle ^C only, this one is obvious: |
| 8523 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8524 | * during builtin/nofork. |
| 8525 | */ |
| 8526 | if (sigismember(&G.pending_set, SIGINT)) |
| 8527 | rcode = 128 + SIGINT; |
| 8528 | } |
Denys Vlasenko | 34f6b12 | 2018-04-05 11:30:17 +0200 | [diff] [blame] | 8529 | free(argv_expanded); |
| 8530 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8531 | debug_leave(); |
| 8532 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8533 | return rcode; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8534 | } |
| 8535 | |
| 8536 | must_fork: |
| 8537 | /* NB: argv_expanded may already be created, and that |
| 8538 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8539 | * use argv_expanded if it's non-NULL */ |
| 8540 | |
| 8541 | /* Going to fork a child per each pipe member */ |
| 8542 | pi->alive_cmds = 0; |
| 8543 | next_infd = 0; |
| 8544 | |
| 8545 | cmd_no = 0; |
| 8546 | while (cmd_no < pi->num_cmds) { |
| 8547 | struct fd_pair pipefds; |
| 8548 | #if !BB_MMU |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8549 | int sv_var_nest_level = G.var_nest_level; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8550 | volatile nommu_save_t nommu_save; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8551 | nommu_save.old_vars = NULL; |
| 8552 | nommu_save.argv = NULL; |
| 8553 | nommu_save.argv_from_re_execing = NULL; |
| 8554 | #endif |
| 8555 | command = &pi->cmds[cmd_no]; |
| 8556 | cmd_no++; |
| 8557 | if (command->argv) { |
| 8558 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8559 | command->argv[0], command->argv[1]); |
| 8560 | } else { |
| 8561 | debug_printf_exec(": pipe member with no argv\n"); |
| 8562 | } |
| 8563 | |
| 8564 | /* pipes are inserted between pairs of commands */ |
| 8565 | pipefds.rd = 0; |
| 8566 | pipefds.wr = 1; |
| 8567 | if (cmd_no < pi->num_cmds) |
| 8568 | xpiped_pair(pipefds); |
| 8569 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8570 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8571 | if (G.lineno_var) |
| 8572 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8573 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8574 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8575 | command->pid = BB_MMU ? fork() : vfork(); |
| 8576 | if (!command->pid) { /* child */ |
| 8577 | #if ENABLE_HUSH_JOB |
| 8578 | disable_restore_tty_pgrp_on_exit(); |
| 8579 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8580 | |
| 8581 | /* Every child adds itself to new process group |
| 8582 | * with pgid == pid_of_first_child_in_pipe */ |
| 8583 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8584 | pid_t pgrp; |
| 8585 | pgrp = pi->pgrp; |
| 8586 | if (pgrp < 0) /* true for 1st process only */ |
| 8587 | pgrp = getpid(); |
| 8588 | if (setpgid(0, pgrp) == 0 |
| 8589 | && pi->followup != PIPE_BG |
| 8590 | && G_saved_tty_pgrp /* we have ctty */ |
| 8591 | ) { |
| 8592 | /* We do it in *every* child, not just first, |
| 8593 | * to avoid races */ |
| 8594 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8595 | } |
| 8596 | } |
| 8597 | #endif |
| 8598 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8599 | /* 1st cmd in backgrounded pipe |
| 8600 | * should have its stdin /dev/null'ed */ |
| 8601 | close(0); |
| 8602 | if (open(bb_dev_null, O_RDONLY)) |
| 8603 | xopen("/", O_RDONLY); |
| 8604 | } else { |
| 8605 | xmove_fd(next_infd, 0); |
| 8606 | } |
| 8607 | xmove_fd(pipefds.wr, 1); |
| 8608 | if (pipefds.rd > 1) |
| 8609 | close(pipefds.rd); |
| 8610 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8611 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8612 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8613 | * of cmd1 goes into pipe. |
| 8614 | */ |
| 8615 | if (setup_redirects(command, NULL)) { |
| 8616 | /* Happens when redir file can't be opened: |
| 8617 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8618 | * FOO |
| 8619 | * hush: can't open '/qwe/rty': No such file or directory |
| 8620 | * BAZ |
| 8621 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8622 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8623 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8624 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8625 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8626 | /* Stores to nommu_save list of env vars putenv'ed |
| 8627 | * (NOMMU, on MMU we don't need that) */ |
| 8628 | /* cast away volatility... */ |
| 8629 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8630 | /* pseudo_exec() does not return */ |
| 8631 | } |
| 8632 | |
| 8633 | /* parent or error */ |
| 8634 | #if ENABLE_HUSH_FAST |
| 8635 | G.count_SIGCHLD++; |
| 8636 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8637 | #endif |
| 8638 | enable_restore_tty_pgrp_on_exit(); |
| 8639 | #if !BB_MMU |
| 8640 | /* Clean up after vforked child */ |
| 8641 | free(nommu_save.argv); |
| 8642 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | 9db344a | 2018-04-09 19:05:11 +0200 | [diff] [blame] | 8643 | G.var_nest_level = sv_var_nest_level; |
| 8644 | remove_nested_vars(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8645 | add_vars(nommu_save.old_vars); |
| 8646 | #endif |
| 8647 | free(argv_expanded); |
| 8648 | argv_expanded = NULL; |
| 8649 | if (command->pid < 0) { /* [v]fork failed */ |
| 8650 | /* Clearly indicate, was it fork or vfork */ |
| 8651 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8652 | } else { |
| 8653 | pi->alive_cmds++; |
| 8654 | #if ENABLE_HUSH_JOB |
| 8655 | /* Second and next children need to know pid of first one */ |
| 8656 | if (pi->pgrp < 0) |
| 8657 | pi->pgrp = command->pid; |
| 8658 | #endif |
| 8659 | } |
| 8660 | |
| 8661 | if (cmd_no > 1) |
| 8662 | close(next_infd); |
| 8663 | if (cmd_no < pi->num_cmds) |
| 8664 | close(pipefds.wr); |
| 8665 | /* Pass read (output) pipe end to next iteration */ |
| 8666 | next_infd = pipefds.rd; |
| 8667 | } |
| 8668 | |
| 8669 | if (!pi->alive_cmds) { |
| 8670 | debug_leave(); |
| 8671 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8672 | return 1; |
| 8673 | } |
| 8674 | |
| 8675 | debug_leave(); |
| 8676 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8677 | return -1; |
| 8678 | } |
| 8679 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8680 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8681 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8682 | static int run_list(struct pipe *pi) |
| 8683 | { |
| 8684 | #if ENABLE_HUSH_CASE |
| 8685 | char *case_word = NULL; |
| 8686 | #endif |
| 8687 | #if ENABLE_HUSH_LOOPS |
| 8688 | struct pipe *loop_top = NULL; |
| 8689 | char **for_lcur = NULL; |
| 8690 | char **for_list = NULL; |
| 8691 | #endif |
| 8692 | smallint last_followup; |
| 8693 | smalluint rcode; |
| 8694 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8695 | smalluint cond_code = 0; |
| 8696 | #else |
| 8697 | enum { cond_code = 0 }; |
| 8698 | #endif |
| 8699 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8700 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8701 | smallint last_rword; /* ditto */ |
| 8702 | #endif |
| 8703 | |
| 8704 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8705 | debug_enter(); |
| 8706 | |
| 8707 | #if ENABLE_HUSH_LOOPS |
| 8708 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8709 | { |
| 8710 | struct pipe *cpipe; |
| 8711 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8712 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8713 | continue; |
| 8714 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8715 | if (cpipe->next == NULL) { |
| 8716 | syntax_error("malformed for"); |
| 8717 | debug_leave(); |
| 8718 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8719 | return 1; |
| 8720 | } |
| 8721 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8722 | if (cpipe->next->res_word == RES_DO) |
| 8723 | continue; |
| 8724 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8725 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8726 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8727 | ) { |
| 8728 | syntax_error("malformed for"); |
| 8729 | debug_leave(); |
| 8730 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8731 | return 1; |
| 8732 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8733 | } |
| 8734 | } |
| 8735 | #endif |
| 8736 | |
| 8737 | /* Past this point, all code paths should jump to ret: label |
| 8738 | * in order to return, no direct "return" statements please. |
| 8739 | * This helps to ensure that no memory is leaked. */ |
| 8740 | |
| 8741 | #if ENABLE_HUSH_JOB |
| 8742 | G.run_list_level++; |
| 8743 | #endif |
| 8744 | |
| 8745 | #if HAS_KEYWORDS |
| 8746 | rword = RES_NONE; |
| 8747 | last_rword = RES_XXXX; |
| 8748 | #endif |
| 8749 | last_followup = PIPE_SEQ; |
| 8750 | rcode = G.last_exitcode; |
| 8751 | |
| 8752 | /* Go through list of pipes, (maybe) executing them. */ |
| 8753 | 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] | 8754 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8755 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8756 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8757 | if (G.flag_SIGINT) |
| 8758 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8759 | if (G_flag_return_in_progress == 1) |
| 8760 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8761 | |
| 8762 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8763 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8764 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8765 | |
| 8766 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8767 | if ( |
| 8768 | #if ENABLE_HUSH_IF |
| 8769 | rword == RES_IF || rword == RES_ELIF || |
| 8770 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8771 | pi->followup != PIPE_SEQ |
| 8772 | ) { |
| 8773 | G.errexit_depth++; |
| 8774 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8775 | #if ENABLE_HUSH_LOOPS |
| 8776 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8777 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8778 | ) { |
| 8779 | /* start of a loop: remember where loop starts */ |
| 8780 | loop_top = pi; |
| 8781 | G.depth_of_loop++; |
| 8782 | } |
| 8783 | #endif |
| 8784 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8785 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8786 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8787 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8788 | ) { |
| 8789 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8790 | * and we should not execute CMD */ |
| 8791 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8792 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8793 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8794 | } |
| 8795 | } |
| 8796 | last_followup = pi->followup; |
| 8797 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8798 | #if ENABLE_HUSH_IF |
| 8799 | if (cond_code) { |
| 8800 | if (rword == RES_THEN) { |
| 8801 | /* if false; then ... fi has exitcode 0! */ |
| 8802 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8803 | /* "if <false> THEN cmd": skip cmd */ |
| 8804 | continue; |
| 8805 | } |
| 8806 | } else { |
| 8807 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8808 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8809 | * skip cmd and all following ones */ |
| 8810 | break; |
| 8811 | } |
| 8812 | } |
| 8813 | #endif |
| 8814 | #if ENABLE_HUSH_LOOPS |
| 8815 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 8816 | if (!for_lcur) { |
| 8817 | /* first loop through for */ |
| 8818 | |
| 8819 | static const char encoded_dollar_at[] ALIGN1 = { |
| 8820 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 8821 | }; /* encoded representation of "$@" */ |
| 8822 | static const char *const encoded_dollar_at_argv[] = { |
| 8823 | encoded_dollar_at, NULL |
| 8824 | }; /* argv list with one element: "$@" */ |
| 8825 | char **vals; |
| 8826 | |
| 8827 | vals = (char**)encoded_dollar_at_argv; |
| 8828 | if (pi->next->res_word == RES_IN) { |
| 8829 | /* if no variable values after "in" we skip "for" */ |
| 8830 | if (!pi->next->cmds[0].argv) { |
| 8831 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8832 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 8833 | break; |
| 8834 | } |
| 8835 | vals = pi->next->cmds[0].argv; |
| 8836 | } /* else: "for var; do..." -> assume "$@" list */ |
| 8837 | /* create list of variable values */ |
| 8838 | debug_print_strings("for_list made from", vals); |
| 8839 | for_list = expand_strvec_to_strvec(vals); |
| 8840 | for_lcur = for_list; |
| 8841 | debug_print_strings("for_list", for_list); |
| 8842 | } |
| 8843 | if (!*for_lcur) { |
| 8844 | /* "for" loop is over, clean up */ |
| 8845 | free(for_list); |
| 8846 | for_list = NULL; |
| 8847 | for_lcur = NULL; |
| 8848 | break; |
| 8849 | } |
| 8850 | /* Insert next value from for_lcur */ |
| 8851 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8852 | 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] | 8853 | continue; |
| 8854 | } |
| 8855 | if (rword == RES_IN) { |
| 8856 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 8857 | } |
| 8858 | if (rword == RES_DONE) { |
| 8859 | continue; /* "done" has no cmds too */ |
| 8860 | } |
| 8861 | #endif |
| 8862 | #if ENABLE_HUSH_CASE |
| 8863 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8864 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | abf7556 | 2018-04-02 17:25:18 +0200 | [diff] [blame] | 8865 | case_word = expand_string_to_string(pi->cmds->argv[0], 1); |
| 8866 | debug_printf_exec("CASE word1:'%s'\n", case_word); |
| 8867 | //unbackslash(case_word); |
| 8868 | //debug_printf_exec("CASE word2:'%s'\n", case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8869 | continue; |
| 8870 | } |
| 8871 | if (rword == RES_MATCH) { |
| 8872 | char **argv; |
| 8873 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8874 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8875 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 8876 | break; |
| 8877 | /* all prev words didn't match, does this one match? */ |
| 8878 | argv = pi->cmds->argv; |
| 8879 | while (*argv) { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8880 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8881 | /* TODO: which FNM_xxx flags to use? */ |
| 8882 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8883 | debug_printf_exec("fnmatch(pattern:'%s',str:'%s'):%d\n", pattern, case_word, cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8884 | free(pattern); |
| 8885 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8886 | free(case_word); |
| 8887 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8888 | break; |
| 8889 | } |
| 8890 | argv++; |
| 8891 | } |
| 8892 | continue; |
| 8893 | } |
| 8894 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8895 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8896 | if (cond_code != 0) |
| 8897 | continue; /* not matched yet, skip this pipe */ |
| 8898 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8899 | if (rword == RES_ESAC) { |
| 8900 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 8901 | if (case_word) { |
| 8902 | /* "case" did not match anything: still set $? (to 0) */ |
| 8903 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8904 | } |
| 8905 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8906 | #endif |
| 8907 | /* Just pressing <enter> in shell should check for jobs. |
| 8908 | * OTOH, in non-interactive shell this is useless |
| 8909 | * and only leads to extra job checks */ |
| 8910 | if (pi->num_cmds == 0) { |
| 8911 | if (G_interactive_fd) |
| 8912 | goto check_jobs_and_continue; |
| 8913 | continue; |
| 8914 | } |
| 8915 | |
| 8916 | /* After analyzing all keywords and conditions, we decided |
| 8917 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8918 | * after run_pipe to collect any background children, |
| 8919 | * even if list execution is to be stopped. */ |
| 8920 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8921 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8922 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8923 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8924 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8925 | if (r != -1) { |
| 8926 | /* We ran a builtin, function, or group. |
| 8927 | * rcode is already known |
| 8928 | * and we don't need to wait for anything. */ |
| 8929 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8930 | G.last_exitcode = rcode; |
| 8931 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8932 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8933 | /* Was it "break" or "continue"? */ |
| 8934 | if (G.flag_break_continue) { |
| 8935 | smallint fbc = G.flag_break_continue; |
| 8936 | /* We might fall into outer *loop*, |
| 8937 | * don't want to break it too */ |
| 8938 | if (loop_top) { |
| 8939 | G.depth_break_continue--; |
| 8940 | if (G.depth_break_continue == 0) |
| 8941 | G.flag_break_continue = 0; |
| 8942 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 8943 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 8944 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8945 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8946 | break; |
| 8947 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8948 | /* "continue": simulate end of loop */ |
| 8949 | rword = RES_DONE; |
| 8950 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8951 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8952 | #endif |
| 8953 | if (G_flag_return_in_progress == 1) { |
| 8954 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 8955 | break; |
| 8956 | } |
| 8957 | } else if (pi->followup == PIPE_BG) { |
| 8958 | /* What does bash do with attempts to background builtins? */ |
| 8959 | /* even bash 3.2 doesn't do that well with nested bg: |
| 8960 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 8961 | * I'm NOT treating inner &'s as jobs */ |
| 8962 | #if ENABLE_HUSH_JOB |
| 8963 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8964 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8965 | #endif |
| 8966 | /* Last command's pid goes to $! */ |
| 8967 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8968 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8969 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8970 | /* 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] | 8971 | rcode = EXIT_SUCCESS; |
| 8972 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8973 | } else { |
| 8974 | #if ENABLE_HUSH_JOB |
| 8975 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8976 | /* Waits for completion, then fg's main shell */ |
| 8977 | rcode = checkjobs_and_fg_shell(pi); |
| 8978 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8979 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8980 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8981 | #endif |
| 8982 | /* This one just waits for completion */ |
| 8983 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8984 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8985 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8986 | G.last_exitcode = rcode; |
| 8987 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8988 | } |
| 8989 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8990 | /* Handle "set -e" */ |
| 8991 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 8992 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 8993 | if (G.errexit_depth == 0) |
| 8994 | hush_exit(rcode); |
| 8995 | } |
| 8996 | G.errexit_depth = sv_errexit_depth; |
| 8997 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8998 | /* Analyze how result affects subsequent commands */ |
| 8999 | #if ENABLE_HUSH_IF |
| 9000 | if (rword == RES_IF || rword == RES_ELIF) |
| 9001 | cond_code = rcode; |
| 9002 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9003 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 9004 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9005 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9006 | #if ENABLE_HUSH_LOOPS |
| 9007 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9008 | if (pi->next |
| 9009 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 9010 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 9011 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9012 | if (rword == RES_WHILE) { |
| 9013 | if (rcode) { |
| 9014 | /* "while false; do...done" - exitcode 0 */ |
| 9015 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 9016 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 9017 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9018 | } |
| 9019 | } |
| 9020 | if (rword == RES_UNTIL) { |
| 9021 | if (!rcode) { |
| 9022 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9023 | break; |
| 9024 | } |
| 9025 | } |
| 9026 | } |
| 9027 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9028 | } /* for (pi) */ |
| 9029 | |
| 9030 | #if ENABLE_HUSH_JOB |
| 9031 | G.run_list_level--; |
| 9032 | #endif |
| 9033 | #if ENABLE_HUSH_LOOPS |
| 9034 | if (loop_top) |
| 9035 | G.depth_of_loop--; |
| 9036 | free(for_list); |
| 9037 | #endif |
| 9038 | #if ENABLE_HUSH_CASE |
| 9039 | free(case_word); |
| 9040 | #endif |
| 9041 | debug_leave(); |
| 9042 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 9043 | return rcode; |
| 9044 | } |
| 9045 | |
| 9046 | /* Select which version we will use */ |
| 9047 | static int run_and_free_list(struct pipe *pi) |
| 9048 | { |
| 9049 | int rcode = 0; |
| 9050 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9051 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 9052 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 9053 | rcode = run_list(pi); |
| 9054 | } |
| 9055 | /* free_pipe_list has the side effect of clearing memory. |
| 9056 | * In the long run that function can be merged with run_list, |
| 9057 | * but doing that now would hobble the debugging effort. */ |
| 9058 | free_pipe_list(pi); |
| 9059 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 9060 | return rcode; |
| 9061 | } |
| 9062 | |
| 9063 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9064 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 9065 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9066 | sighandler_t old_handler; |
| 9067 | unsigned sig = 0; |
| 9068 | while ((mask >>= 1) != 0) { |
| 9069 | sig++; |
| 9070 | if (!(mask & 1)) |
| 9071 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9072 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9073 | /* POSIX allows shell to re-enable SIGCHLD |
| 9074 | * even if it was SIG_IGN on entry. |
| 9075 | * Therefore we skip IGN check for it: |
| 9076 | */ |
| 9077 | if (sig == SIGCHLD) |
| 9078 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 9079 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 9080 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 9081 | */ |
| 9082 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9083 | if (old_handler == SIG_IGN) { |
| 9084 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9085 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9086 | #if ENABLE_HUSH_TRAP |
| 9087 | if (!G_traps) |
| 9088 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 9089 | free(G_traps[sig]); |
| 9090 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 9091 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9092 | } |
| 9093 | } |
| 9094 | } |
| 9095 | |
| 9096 | /* Called a few times only (or even once if "sh -c") */ |
| 9097 | static void install_special_sighandlers(void) |
| 9098 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9099 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9100 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9101 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9102 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9103 | if (G_interactive_fd) { |
| 9104 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 9105 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9106 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9107 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9108 | /* Careful, do not re-install handlers we already installed */ |
| 9109 | if (G.special_sig_mask != mask) { |
| 9110 | unsigned diff = mask & ~G.special_sig_mask; |
| 9111 | G.special_sig_mask = mask; |
| 9112 | install_sighandlers(diff); |
| 9113 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
| 9116 | #if ENABLE_HUSH_JOB |
| 9117 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9118 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9119 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9120 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9121 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9122 | |
| 9123 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9124 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9125 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 9126 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9127 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 9128 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 9129 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9130 | + (1 << SIGABRT) |
| 9131 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9132 | + (1 << SIGPIPE) |
| 9133 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9134 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9135 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9136 | * we never want to restore pgrp on exit, and this fn is not called |
| 9137 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9138 | /*+ (1 << SIGHUP )*/ |
| 9139 | /*+ (1 << SIGTERM)*/ |
| 9140 | /*+ (1 << SIGINT )*/ |
| 9141 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9142 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9143 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9144 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9145 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9146 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9147 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9148 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9149 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9150 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9151 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9152 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9153 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9154 | break; |
| 9155 | case 'x': |
| 9156 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9157 | break; |
| 9158 | case 'o': |
| 9159 | if (!o_opt) { |
| 9160 | /* "set -+o" without parameter. |
| 9161 | * in bash, set -o produces this output: |
| 9162 | * pipefail off |
| 9163 | * and set +o: |
| 9164 | * set +o pipefail |
| 9165 | * We always use the second form. |
| 9166 | */ |
| 9167 | const char *p = o_opt_strings; |
| 9168 | idx = 0; |
| 9169 | while (*p) { |
| 9170 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9171 | idx++; |
| 9172 | p += strlen(p) + 1; |
| 9173 | } |
| 9174 | break; |
| 9175 | } |
| 9176 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9177 | if (idx >= 0) { |
| 9178 | G.o_opt[idx] = state; |
| 9179 | break; |
| 9180 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9181 | case 'e': |
| 9182 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9183 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9184 | default: |
| 9185 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9186 | } |
| 9187 | return EXIT_SUCCESS; |
| 9188 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9189 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9190 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9191 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9192 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9193 | enum { |
| 9194 | OPT_login = (1 << 0), |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9195 | OPT_s = (1 << 1), |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9196 | }; |
| 9197 | unsigned flags; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9198 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9199 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9200 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9201 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9202 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9203 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9204 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9205 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9206 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9207 | #if ENABLE_HUSH_FAST |
| 9208 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9209 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9210 | #if !BB_MMU |
| 9211 | G.argv0_for_re_execing = argv[0]; |
| 9212 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9213 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9214 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9215 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9216 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9217 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9218 | shell_ver->flg_export = 1; |
| 9219 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9220 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9221 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9222 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9223 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9224 | /* Create shell local variables from the values |
| 9225 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9226 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9227 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9228 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9229 | if (e) while (*e) { |
| 9230 | char *value = strchr(*e, '='); |
| 9231 | if (value) { /* paranoia */ |
| 9232 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9233 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9234 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9235 | cur_var->max_len = strlen(*e); |
| 9236 | cur_var->flg_export = 1; |
| 9237 | } |
| 9238 | e++; |
| 9239 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9240 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9241 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9242 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9243 | |
| 9244 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9245 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9246 | |
Denys Vlasenko | f5018da | 2018-04-06 17:58:21 +0200 | [diff] [blame] | 9247 | #if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 9248 | /* Set (but not export) PS1/2 unless already set */ |
| 9249 | if (!get_local_var_value("PS1")) |
| 9250 | set_local_var_from_halves("PS1", "\\w \\$ "); |
| 9251 | if (!get_local_var_value("PS2")) |
| 9252 | set_local_var_from_halves("PS2", "> "); |
| 9253 | #endif |
| 9254 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9255 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9256 | /* Set (but not export) HOSTNAME unless already set */ |
| 9257 | if (!get_local_var_value("HOSTNAME")) { |
| 9258 | struct utsname uts; |
| 9259 | uname(&uts); |
| 9260 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9261 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9262 | /* bash also exports SHLVL and _, |
| 9263 | * and sets (but doesn't export) the following variables: |
| 9264 | * BASH=/bin/bash |
| 9265 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9266 | * BASH_VERSION='3.2.0(1)-release' |
| 9267 | * HOSTTYPE=i386 |
| 9268 | * MACHTYPE=i386-pc-linux-gnu |
| 9269 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9270 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9271 | * EUID=<NNNNN> |
| 9272 | * UID=<NNNNN> |
| 9273 | * GROUPS=() |
| 9274 | * LINES=<NNN> |
| 9275 | * COLUMNS=<NNN> |
| 9276 | * BASH_ARGC=() |
| 9277 | * BASH_ARGV=() |
| 9278 | * BASH_LINENO=() |
| 9279 | * BASH_SOURCE=() |
| 9280 | * DIRSTACK=() |
| 9281 | * PIPESTATUS=([0]="0") |
| 9282 | * HISTFILE=/<xxx>/.bash_history |
| 9283 | * HISTFILESIZE=500 |
| 9284 | * HISTSIZE=500 |
| 9285 | * MAILCHECK=60 |
| 9286 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9287 | * SHELL=/bin/bash |
| 9288 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9289 | * TERM=dumb |
| 9290 | * OPTERR=1 |
| 9291 | * OPTIND=1 |
| 9292 | * IFS=$' \t\n' |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9293 | * PS4='+ ' |
| 9294 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9295 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9296 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9297 | #if ENABLE_HUSH_LINENO_VAR |
| 9298 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9299 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9300 | set_local_var(p, /*flags*/ 0); |
| 9301 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9302 | } |
| 9303 | #endif |
| 9304 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9305 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9306 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9307 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9308 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9309 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9310 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9311 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9312 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9313 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9314 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9315 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9316 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9317 | * 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] | 9318 | * in order to intercept (more) signals. |
| 9319 | */ |
| 9320 | |
| 9321 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9322 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9323 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9324 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9325 | while (1) { |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9326 | int opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9327 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9328 | "<:$:R:V:" |
| 9329 | # if ENABLE_HUSH_FUNCTIONS |
| 9330 | "F:" |
| 9331 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9332 | #endif |
| 9333 | ); |
| 9334 | if (opt <= 0) |
| 9335 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9336 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9337 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9338 | /* Possibilities: |
| 9339 | * sh ... -c 'script' |
| 9340 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9341 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9342 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9343 | * "" needs to be replaced with NULL |
| 9344 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9345 | * Note: the form without ARG0 never happens: |
| 9346 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9347 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9348 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9349 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9350 | G.root_ppid = getppid(); |
| 9351 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9352 | G.global_argv = argv + optind; |
| 9353 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9354 | if (builtin_argc) { |
| 9355 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9356 | const struct built_in_command *x; |
| 9357 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9358 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9359 | x = find_builtin(optarg); |
| 9360 | if (x) { /* paranoia */ |
| 9361 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9362 | G.global_argv += builtin_argc; |
| 9363 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9364 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9365 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9366 | } |
| 9367 | goto final_return; |
| 9368 | } |
| 9369 | if (!G.global_argv[0]) { |
| 9370 | /* -c 'script' (no params): prevent empty $0 */ |
| 9371 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9372 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9373 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9374 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9375 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9376 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9377 | goto final_return; |
| 9378 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9379 | /* Well, we cannot just declare interactiveness, |
| 9380 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9381 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9382 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9383 | case 's': |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9384 | flags |= OPT_s; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9385 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9386 | case 'l': |
| 9387 | flags |= OPT_login; |
| 9388 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9389 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9390 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9391 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9392 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9393 | case '$': { |
| 9394 | unsigned long long empty_trap_mask; |
| 9395 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9396 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9397 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9398 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9399 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9400 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9401 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9402 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9403 | optarg++; |
| 9404 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9405 | optarg++; |
| 9406 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9407 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9408 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9409 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9410 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9411 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9412 | for (sig = 1; sig < NSIG; sig++) { |
| 9413 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9414 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9415 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9416 | } |
| 9417 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9418 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9419 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9420 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9421 | optarg++; |
| 9422 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9423 | # endif |
Denys Vlasenko | eb0de05 | 2018-04-09 17:54:07 +0200 | [diff] [blame] | 9424 | # if ENABLE_HUSH_FUNCTIONS |
| 9425 | /* nommu uses re-exec trick for "... | func | ...", |
| 9426 | * should allow "return". |
| 9427 | * This accidentally allows returns in subshells. |
| 9428 | */ |
| 9429 | G_flag_return_in_progress = -1; |
| 9430 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9431 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9432 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9433 | case 'R': |
| 9434 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9435 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9436 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9437 | # if ENABLE_HUSH_FUNCTIONS |
| 9438 | case 'F': { |
| 9439 | struct function *funcp = new_function(optarg); |
| 9440 | /* funcp->name is already set to optarg */ |
| 9441 | /* funcp->body is set to NULL. It's a special case. */ |
| 9442 | funcp->body_as_string = argv[optind]; |
| 9443 | optind++; |
| 9444 | break; |
| 9445 | } |
| 9446 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9447 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9448 | case 'n': |
| 9449 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9450 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9451 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9452 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9453 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9454 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9455 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9456 | " or: sh -c command [args]...\n\n"); |
| 9457 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9458 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9459 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9460 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9461 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9462 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9463 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9464 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9465 | G.global_argc = argc - (optind - 1); |
| 9466 | G.global_argv = argv + (optind - 1); |
| 9467 | G.global_argv[0] = argv[0]; |
| 9468 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9469 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9470 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9471 | G.root_ppid = getppid(); |
| 9472 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9473 | |
| 9474 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9475 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9476 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9477 | debug_printf("sourcing /etc/profile\n"); |
| 9478 | input = fopen_for_read("/etc/profile"); |
| 9479 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9480 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9481 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9482 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9483 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9484 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9485 | /* bash: after sourcing /etc/profile, |
| 9486 | * tries to source (in the given order): |
| 9487 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9488 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9489 | * bash also sources ~/.bash_logout on exit. |
| 9490 | * If called as sh, skips .bash_XXX files. |
| 9491 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9492 | } |
| 9493 | |
Denys Vlasenko | f2ed39b | 2018-04-05 16:46:49 +0200 | [diff] [blame] | 9494 | /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */ |
| 9495 | if (!(flags & OPT_s) && G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9496 | FILE *input; |
| 9497 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9498 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9499 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9500 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9501 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9502 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9503 | G.global_argc--; |
| 9504 | G.global_argv++; |
| 9505 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9506 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9507 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9508 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9509 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9510 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9511 | parse_and_run_file(input); |
| 9512 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9513 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9514 | #endif |
| 9515 | goto final_return; |
| 9516 | } |
| 9517 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9518 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9519 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9520 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9521 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9522 | /* A shell is interactive if the '-i' flag was given, |
| 9523 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9524 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9525 | * no arguments remaining or the -s flag given |
| 9526 | * standard input is a terminal |
| 9527 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9528 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9529 | */ |
| 9530 | #if ENABLE_HUSH_JOB |
| 9531 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9532 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9533 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9534 | if (G_saved_tty_pgrp < 0) |
| 9535 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9536 | |
| 9537 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9538 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9539 | if (G_interactive_fd < 0) { |
| 9540 | /* try to dup to any fd */ |
| 9541 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9542 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9543 | /* give up */ |
| 9544 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9545 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9546 | } |
| 9547 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9548 | // TODO: track & disallow any attempts of user |
| 9549 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9550 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9551 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9552 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9553 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9554 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9555 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9556 | /* If we were run as 'hush &', sleep until we are |
| 9557 | * in the foreground (tty pgrp == our pgrp). |
| 9558 | * If we get started under a job aware app (like bash), |
| 9559 | * make sure we are now in charge so we don't fight over |
| 9560 | * who gets the foreground */ |
| 9561 | while (1) { |
| 9562 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9563 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9564 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9565 | break; |
| 9566 | /* send TTIN to ourself (should stop us) */ |
| 9567 | kill(- shell_pgrp, SIGTTIN); |
| 9568 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9569 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9570 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9571 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9572 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9573 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9574 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9575 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9576 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9577 | /* Put ourselves in our own process group |
| 9578 | * (bash, too, does this only if ctty is available) */ |
| 9579 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9580 | /* Grab control of the terminal */ |
| 9581 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9582 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9583 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9584 | |
| 9585 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9586 | { |
| 9587 | const char *hp = get_local_var_value("HISTFILE"); |
| 9588 | if (!hp) { |
| 9589 | hp = get_local_var_value("HOME"); |
| 9590 | if (hp) |
| 9591 | hp = concat_path_file(hp, ".hush_history"); |
| 9592 | } else { |
| 9593 | hp = xstrdup(hp); |
| 9594 | } |
| 9595 | if (hp) { |
| 9596 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9597 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9598 | } |
| 9599 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9600 | hp = get_local_var_value("HISTFILESIZE"); |
| 9601 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9602 | # endif |
| 9603 | } |
| 9604 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9605 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9606 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9607 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9608 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9609 | /* No job control compiled in, only prompt/line editing */ |
| 9610 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9611 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9612 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9613 | /* try to dup to any fd */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9614 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9615 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9616 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9617 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9618 | } |
| 9619 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9620 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9621 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9622 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9623 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9624 | #else |
| 9625 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9626 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9627 | #endif |
| 9628 | /* bash: |
| 9629 | * if interactive but not a login shell, sources ~/.bashrc |
| 9630 | * (--norc turns this off, --rcfile <file> overrides) |
| 9631 | */ |
| 9632 | |
| 9633 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9634 | /* note: ash and hush share this string */ |
| 9635 | printf("\n\n%s %s\n" |
| 9636 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9637 | "\n", |
| 9638 | bb_banner, |
| 9639 | "hush - the humble shell" |
| 9640 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9643 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9644 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9645 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9646 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9647 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9648 | |
| 9649 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9650 | /* |
| 9651 | * Built-ins |
| 9652 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9653 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9654 | { |
| 9655 | return 0; |
| 9656 | } |
| 9657 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9658 | #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] | 9659 | 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] | 9660 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9661 | int argc = string_array_len(argv); |
| 9662 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9663 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9664 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9665 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9666 | static int FAST_FUNC builtin_test(char **argv) |
| 9667 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9668 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9669 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9670 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9671 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9672 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9673 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9674 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9675 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9676 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9677 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9678 | static int FAST_FUNC builtin_printf(char **argv) |
| 9679 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9680 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9681 | } |
| 9682 | #endif |
| 9683 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9684 | #if ENABLE_HUSH_HELP |
| 9685 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9686 | { |
| 9687 | const struct built_in_command *x; |
| 9688 | |
| 9689 | printf( |
| 9690 | "Built-in commands:\n" |
| 9691 | "------------------\n"); |
| 9692 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9693 | if (x->b_descr) |
| 9694 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9695 | } |
| 9696 | return EXIT_SUCCESS; |
| 9697 | } |
| 9698 | #endif |
| 9699 | |
| 9700 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9701 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9702 | { |
| 9703 | show_history(G.line_input_state); |
| 9704 | return EXIT_SUCCESS; |
| 9705 | } |
| 9706 | #endif |
| 9707 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9708 | static char **skip_dash_dash(char **argv) |
| 9709 | { |
| 9710 | argv++; |
| 9711 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9712 | argv++; |
| 9713 | return argv; |
| 9714 | } |
| 9715 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9716 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9717 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9718 | const char *newdir; |
| 9719 | |
| 9720 | argv = skip_dash_dash(argv); |
| 9721 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9722 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9723 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9724 | * bash says "bash: cd: HOME not set" and does nothing |
| 9725 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9726 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9727 | const char *home = get_local_var_value("HOME"); |
| 9728 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9729 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9730 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9731 | /* Mimic bash message exactly */ |
| 9732 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9733 | return EXIT_FAILURE; |
| 9734 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9735 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9736 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9737 | * set it again, but do not export. bash does the same. |
| 9738 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9739 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9740 | return EXIT_SUCCESS; |
| 9741 | } |
| 9742 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9743 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9744 | { |
| 9745 | puts(get_cwd(0)); |
| 9746 | return EXIT_SUCCESS; |
| 9747 | } |
| 9748 | |
| 9749 | static int FAST_FUNC builtin_eval(char **argv) |
| 9750 | { |
| 9751 | int rcode = EXIT_SUCCESS; |
| 9752 | |
| 9753 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9754 | if (argv[0]) { |
| 9755 | char *str = NULL; |
| 9756 | |
| 9757 | if (argv[1]) { |
| 9758 | /* "The eval utility shall construct a command by |
| 9759 | * concatenating arguments together, separating |
| 9760 | * each with a <space> character." |
| 9761 | */ |
| 9762 | char *p; |
| 9763 | unsigned len = 0; |
| 9764 | char **pp = argv; |
| 9765 | do |
| 9766 | len += strlen(*pp) + 1; |
| 9767 | while (*++pp); |
| 9768 | str = p = xmalloc(len); |
| 9769 | pp = argv; |
| 9770 | do { |
| 9771 | p = stpcpy(p, *pp); |
| 9772 | *p++ = ' '; |
| 9773 | } while (*++pp); |
| 9774 | p[-1] = '\0'; |
| 9775 | } |
| 9776 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9777 | /* bash: |
| 9778 | * eval "echo Hi; done" ("done" is syntax error): |
| 9779 | * "echo Hi" will not execute too. |
| 9780 | */ |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9781 | parse_and_run_string(str ? str : argv[0]); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9782 | free(str); |
| 9783 | rcode = G.last_exitcode; |
| 9784 | } |
| 9785 | return rcode; |
| 9786 | } |
| 9787 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9788 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9789 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9790 | argv = skip_dash_dash(argv); |
| 9791 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9792 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9793 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9794 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9795 | * tty pgrp then, only top-level shell process does that */ |
| 9796 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9797 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9798 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9799 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9800 | * open here. However, they are all CLOEXEC, and execv below |
| 9801 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9802 | * it should show no extra open fds in the "ls" process. |
| 9803 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 9804 | */ |
| 9805 | //close_saved_fds_and_FILE_fds(); |
| 9806 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 9807 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9808 | * 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] | 9809 | * and tcsetpgrp, and this is inherently racy. |
| 9810 | */ |
| 9811 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9814 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9815 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 9816 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9817 | |
| 9818 | /* interactive bash: |
| 9819 | * # trap "echo EEE" EXIT |
| 9820 | * # exit |
| 9821 | * exit |
| 9822 | * There are stopped jobs. |
| 9823 | * (if there are _stopped_ jobs, running ones don't count) |
| 9824 | * # exit |
| 9825 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 9826 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9827 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 9828 | * 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] | 9829 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 9830 | |
| 9831 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9832 | argv = skip_dash_dash(argv); |
| 9833 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9834 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9835 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 9836 | xfunc_error_retval = 255; |
| 9837 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9838 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9839 | } |
| 9840 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9841 | #if ENABLE_HUSH_TYPE |
| 9842 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 9843 | static int FAST_FUNC builtin_type(char **argv) |
| 9844 | { |
| 9845 | int ret = EXIT_SUCCESS; |
| 9846 | |
| 9847 | while (*++argv) { |
| 9848 | const char *type; |
| 9849 | char *path = NULL; |
| 9850 | |
| 9851 | if (0) {} /* make conditional compile easier below */ |
| 9852 | /*else if (find_alias(*argv)) |
| 9853 | type = "an alias";*/ |
| 9854 | #if ENABLE_HUSH_FUNCTIONS |
| 9855 | else if (find_function(*argv)) |
| 9856 | type = "a function"; |
| 9857 | #endif |
| 9858 | else if (find_builtin(*argv)) |
| 9859 | type = "a shell builtin"; |
| 9860 | else if ((path = find_in_path(*argv)) != NULL) |
| 9861 | type = path; |
| 9862 | else { |
| 9863 | bb_error_msg("type: %s: not found", *argv); |
| 9864 | ret = EXIT_FAILURE; |
| 9865 | continue; |
| 9866 | } |
| 9867 | |
| 9868 | printf("%s is %s\n", *argv, type); |
| 9869 | free(path); |
| 9870 | } |
| 9871 | |
| 9872 | return ret; |
| 9873 | } |
| 9874 | #endif |
| 9875 | |
| 9876 | #if ENABLE_HUSH_READ |
| 9877 | /* Interruptibility of read builtin in bash |
| 9878 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9879 | * |
| 9880 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9881 | * |
| 9882 | * SIGINT: |
| 9883 | * - terminates non-interactive shell; |
| 9884 | * - interrupts read in interactive shell; |
| 9885 | * if it has non-empty trap: |
| 9886 | * - executes trap and returns to command prompt in interactive shell; |
| 9887 | * - executes trap and returns to read in non-interactive shell; |
| 9888 | * SIGTERM: |
| 9889 | * - is ignored (does not interrupt) read in interactive shell; |
| 9890 | * - terminates non-interactive shell; |
| 9891 | * if it has non-empty trap: |
| 9892 | * - executes trap and returns to read; |
| 9893 | * SIGHUP: |
| 9894 | * - terminates shell (regardless of interactivity); |
| 9895 | * if it has non-empty trap: |
| 9896 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9897 | * SIGCHLD from children: |
| 9898 | * - does not interrupt read regardless of interactivity: |
| 9899 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9900 | */ |
| 9901 | static int FAST_FUNC builtin_read(char **argv) |
| 9902 | { |
| 9903 | const char *r; |
| 9904 | char *opt_n = NULL; |
| 9905 | char *opt_p = NULL; |
| 9906 | char *opt_t = NULL; |
| 9907 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9908 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9909 | const char *ifs; |
| 9910 | int read_flags; |
| 9911 | |
| 9912 | /* "!": do not abort on errors. |
| 9913 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9914 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9915 | read_flags = getopt32(argv, |
| 9916 | #if BASH_READ_D |
| 9917 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 9918 | #else |
| 9919 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 9920 | #endif |
| 9921 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9922 | if (read_flags == (uint32_t)-1) |
| 9923 | return EXIT_FAILURE; |
| 9924 | argv += optind; |
| 9925 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9926 | |
| 9927 | again: |
| 9928 | r = shell_builtin_read(set_local_var_from_halves, |
| 9929 | argv, |
| 9930 | ifs, |
| 9931 | read_flags, |
| 9932 | opt_n, |
| 9933 | opt_p, |
| 9934 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9935 | opt_u, |
| 9936 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9937 | ); |
| 9938 | |
| 9939 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9940 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9941 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9942 | goto again; |
| 9943 | } |
| 9944 | |
| 9945 | if ((uintptr_t)r > 1) { |
| 9946 | bb_error_msg("%s", r); |
| 9947 | r = (char*)(uintptr_t)1; |
| 9948 | } |
| 9949 | |
| 9950 | return (uintptr_t)r; |
| 9951 | } |
| 9952 | #endif |
| 9953 | |
| 9954 | #if ENABLE_HUSH_UMASK |
| 9955 | static int FAST_FUNC builtin_umask(char **argv) |
| 9956 | { |
| 9957 | int rc; |
| 9958 | mode_t mask; |
| 9959 | |
| 9960 | rc = 1; |
| 9961 | mask = umask(0); |
| 9962 | argv = skip_dash_dash(argv); |
| 9963 | if (argv[0]) { |
| 9964 | mode_t old_mask = mask; |
| 9965 | |
| 9966 | /* numeric umasks are taken as-is */ |
| 9967 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9968 | if (!isdigit(argv[0][0])) |
| 9969 | mask ^= 0777; |
| 9970 | mask = bb_parse_mode(argv[0], mask); |
| 9971 | if (!isdigit(argv[0][0])) |
| 9972 | mask ^= 0777; |
| 9973 | if ((unsigned)mask > 0777) { |
| 9974 | mask = old_mask; |
| 9975 | /* bash messages: |
| 9976 | * bash: umask: 'q': invalid symbolic mode operator |
| 9977 | * bash: umask: 999: octal number out of range |
| 9978 | */ |
| 9979 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 9980 | rc = 0; |
| 9981 | } |
| 9982 | } else { |
| 9983 | /* Mimic bash */ |
| 9984 | printf("%04o\n", (unsigned) mask); |
| 9985 | /* fall through and restore mask which we set to 0 */ |
| 9986 | } |
| 9987 | umask(mask); |
| 9988 | |
| 9989 | return !rc; /* rc != 0 - success */ |
| 9990 | } |
| 9991 | #endif |
| 9992 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9993 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9994 | static void print_escaped(const char *s) |
| 9995 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9996 | if (*s == '\'') |
| 9997 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9998 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9999 | const char *p = strchrnul(s, '\''); |
| 10000 | /* print 'xxxx', possibly just '' */ |
| 10001 | printf("'%.*s'", (int)(p - s), s); |
| 10002 | if (*p == '\0') |
| 10003 | break; |
| 10004 | s = p; |
| 10005 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10006 | /* s points to '; print "'''...'''" */ |
| 10007 | putchar('"'); |
| 10008 | do putchar('\''); while (*++s == '\''); |
| 10009 | putchar('"'); |
| 10010 | } while (*s); |
| 10011 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 10012 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10013 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10014 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10015 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10016 | { |
| 10017 | do { |
| 10018 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10019 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10020 | |
| 10021 | /* So far we do not check that name is valid (TODO?) */ |
| 10022 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10023 | if (*name_end == '\0') { |
| 10024 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10025 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 10026 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 10027 | var = vpp ? *vpp : NULL; |
| 10028 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10029 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10030 | /* export -n NAME (without =VALUE) */ |
| 10031 | if (var) { |
| 10032 | var->flg_export = 0; |
| 10033 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 10034 | unsetenv(name); |
| 10035 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 10036 | continue; |
| 10037 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10038 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10039 | /* export NAME (without =VALUE) */ |
| 10040 | if (var) { |
| 10041 | var->flg_export = 1; |
| 10042 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 10043 | putenv(var->varstr); |
| 10044 | continue; |
| 10045 | } |
| 10046 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10047 | if (flags & SETFLAG_MAKE_RO) { |
| 10048 | /* readonly NAME (without =VALUE) */ |
| 10049 | if (var) { |
| 10050 | var->flg_read_only = 1; |
| 10051 | continue; |
| 10052 | } |
| 10053 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10054 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10055 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10056 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
Denys Vlasenko | 332e411 | 2018-04-04 22:32:59 +0200 | [diff] [blame] | 10057 | unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT; |
| 10058 | if (var && var->var_nest_level == lvl) { |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 10059 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 10060 | continue; |
| 10061 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10062 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10063 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10064 | /* Exporting non-existing variable. |
| 10065 | * bash does not put it in environment, |
| 10066 | * but remembers that it is exported, |
| 10067 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10068 | * We just set it to "" and export. |
| 10069 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10070 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10071 | * bash sets the value to "". |
| 10072 | */ |
| 10073 | /* Or, it's "readonly NAME" (without =VALUE). |
| 10074 | * bash remembers NAME and disallows its creation |
| 10075 | * in the future. |
| 10076 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10077 | name = xasprintf("%s=", name); |
| 10078 | } else { |
| 10079 | /* (Un)exporting/making local NAME=VALUE */ |
| 10080 | name = xstrdup(name); |
| 10081 | } |
Denys Vlasenko | 21b7f1b | 2018-04-05 15:15:53 +0200 | [diff] [blame] | 10082 | debug_printf_env("%s: set_local_var('%s')\n", __func__, name); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 10083 | if (set_local_var(name, flags)) |
| 10084 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10085 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10086 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10087 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10088 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10089 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10090 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10091 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10092 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 10093 | unsigned opt_unexport; |
| 10094 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 10095 | #if ENABLE_HUSH_EXPORT_N |
| 10096 | /* "!": do not abort on errors */ |
| 10097 | opt_unexport = getopt32(argv, "!n"); |
| 10098 | if (opt_unexport == (uint32_t)-1) |
| 10099 | return EXIT_FAILURE; |
| 10100 | argv += optind; |
| 10101 | #else |
| 10102 | opt_unexport = 0; |
| 10103 | argv++; |
| 10104 | #endif |
| 10105 | |
| 10106 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10107 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10108 | if (e) { |
| 10109 | while (*e) { |
| 10110 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10111 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10112 | #else |
| 10113 | /* ash emits: export VAR='VAL' |
| 10114 | * bash: declare -x VAR="VAL" |
| 10115 | * we follow ash example */ |
| 10116 | const char *s = *e++; |
| 10117 | const char *p = strchr(s, '='); |
| 10118 | |
| 10119 | if (!p) /* wtf? take next variable */ |
| 10120 | continue; |
| 10121 | /* export var= */ |
| 10122 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10123 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10124 | putchar('\n'); |
| 10125 | #endif |
| 10126 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10127 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 10128 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10129 | return EXIT_SUCCESS; |
| 10130 | } |
| 10131 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10132 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10133 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 10134 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10135 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10136 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10137 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 10138 | { |
| 10139 | if (G.func_nest_level == 0) { |
| 10140 | bb_error_msg("%s: not in a function", argv[0]); |
| 10141 | return EXIT_FAILURE; /* bash compat */ |
| 10142 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10143 | argv++; |
Denys Vlasenko | d358b0b | 2018-04-05 00:51:55 +0200 | [diff] [blame] | 10144 | /* Since all builtins run in a nested variable level, |
| 10145 | * need to use level - 1 here. Or else the variable will be removed at once |
| 10146 | * after builtin returns. |
| 10147 | */ |
| 10148 | 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] | 10149 | } |
| 10150 | #endif |
| 10151 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10152 | #if ENABLE_HUSH_READONLY |
| 10153 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10154 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10155 | argv++; |
| 10156 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10157 | /* bash: readonly [-p]: list all readonly VARs |
| 10158 | * (-p has no effect in bash) |
| 10159 | */ |
| 10160 | struct variable *e; |
| 10161 | for (e = G.top_var; e; e = e->next) { |
| 10162 | if (e->flg_read_only) { |
| 10163 | //TODO: quote value: readonly VAR='VAL' |
| 10164 | printf("readonly %s\n", e->varstr); |
| 10165 | } |
| 10166 | } |
| 10167 | return EXIT_SUCCESS; |
| 10168 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10169 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10170 | } |
| 10171 | #endif |
| 10172 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10173 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10174 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10175 | static int FAST_FUNC builtin_unset(char **argv) |
| 10176 | { |
| 10177 | int ret; |
| 10178 | unsigned opts; |
| 10179 | |
| 10180 | /* "!": do not abort on errors */ |
| 10181 | /* "+": stop at 1st non-option */ |
| 10182 | opts = getopt32(argv, "!+vf"); |
| 10183 | if (opts == (unsigned)-1) |
| 10184 | return EXIT_FAILURE; |
| 10185 | if (opts == 3) { |
| 10186 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10187 | return EXIT_FAILURE; |
| 10188 | } |
| 10189 | argv += optind; |
| 10190 | |
| 10191 | ret = EXIT_SUCCESS; |
| 10192 | while (*argv) { |
| 10193 | if (!(opts & 2)) { /* not -f */ |
| 10194 | if (unset_local_var(*argv)) { |
| 10195 | /* unset <nonexistent_var> doesn't fail. |
| 10196 | * Error is when one tries to unset RO var. |
| 10197 | * Message was printed by unset_local_var. */ |
| 10198 | ret = EXIT_FAILURE; |
| 10199 | } |
| 10200 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10201 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10202 | else { |
| 10203 | unset_func(*argv); |
| 10204 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10205 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10206 | argv++; |
| 10207 | } |
| 10208 | return ret; |
| 10209 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10210 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10211 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10212 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10213 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10214 | * built-in 'set' handler |
| 10215 | * SUSv3 says: |
| 10216 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10217 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10218 | * set -- [argument...] |
| 10219 | * set -o |
| 10220 | * set +o |
| 10221 | * Implementations shall support the options in both their hyphen and |
| 10222 | * plus-sign forms. These options can also be specified as options to sh. |
| 10223 | * Examples: |
| 10224 | * Write out all variables and their values: set |
| 10225 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10226 | * Turn on the -x and -v options: set -xv |
| 10227 | * Unset all positional parameters: set -- |
| 10228 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10229 | * Set the positional parameters to the expansion of x, even if x expands |
| 10230 | * with a leading '-' or '+': set -- $x |
| 10231 | * |
| 10232 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10233 | */ |
| 10234 | static int FAST_FUNC builtin_set(char **argv) |
| 10235 | { |
| 10236 | int n; |
| 10237 | char **pp, **g_argv; |
| 10238 | char *arg = *++argv; |
| 10239 | |
| 10240 | if (arg == NULL) { |
| 10241 | struct variable *e; |
| 10242 | for (e = G.top_var; e; e = e->next) |
| 10243 | puts(e->varstr); |
| 10244 | return EXIT_SUCCESS; |
| 10245 | } |
| 10246 | |
| 10247 | do { |
| 10248 | if (strcmp(arg, "--") == 0) { |
| 10249 | ++argv; |
| 10250 | goto set_argv; |
| 10251 | } |
| 10252 | if (arg[0] != '+' && arg[0] != '-') |
| 10253 | break; |
| 10254 | for (n = 1; arg[n]; ++n) { |
| 10255 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10256 | goto error; |
| 10257 | if (arg[n] == 'o' && argv[1]) |
| 10258 | argv++; |
| 10259 | } |
| 10260 | } while ((arg = *++argv) != NULL); |
| 10261 | /* Now argv[0] is 1st argument */ |
| 10262 | |
| 10263 | if (arg == NULL) |
| 10264 | return EXIT_SUCCESS; |
| 10265 | set_argv: |
| 10266 | |
| 10267 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10268 | g_argv = G.global_argv; |
| 10269 | if (G.global_args_malloced) { |
| 10270 | pp = g_argv; |
| 10271 | while (*++pp) |
| 10272 | free(*pp); |
| 10273 | g_argv[1] = NULL; |
| 10274 | } else { |
| 10275 | G.global_args_malloced = 1; |
| 10276 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10277 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10278 | g_argv = pp; |
| 10279 | } |
| 10280 | /* This realloc's G.global_argv */ |
| 10281 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10282 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10283 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10284 | |
| 10285 | return EXIT_SUCCESS; |
| 10286 | |
| 10287 | /* Nothing known, so abort */ |
| 10288 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10289 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10290 | return EXIT_FAILURE; |
| 10291 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10292 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10293 | |
| 10294 | static int FAST_FUNC builtin_shift(char **argv) |
| 10295 | { |
| 10296 | int n = 1; |
| 10297 | argv = skip_dash_dash(argv); |
| 10298 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10299 | n = bb_strtou(argv[0], NULL, 10); |
| 10300 | if (errno || n < 0) { |
| 10301 | /* shared string with ash.c */ |
| 10302 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10303 | /* |
| 10304 | * ash aborts in this case. |
| 10305 | * bash prints error message and set $? to 1. |
| 10306 | * Interestingly, for "shift 99999" bash does not |
| 10307 | * print error message, but does set $? to 1 |
| 10308 | * (and does no shifting at all). |
| 10309 | */ |
| 10310 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10311 | } |
| 10312 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10313 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10314 | int m = 1; |
| 10315 | while (m <= n) |
| 10316 | free(G.global_argv[m++]); |
| 10317 | } |
| 10318 | G.global_argc -= n; |
| 10319 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10320 | G.global_argc * sizeof(G.global_argv[0])); |
| 10321 | return EXIT_SUCCESS; |
| 10322 | } |
| 10323 | return EXIT_FAILURE; |
| 10324 | } |
| 10325 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10326 | #if ENABLE_HUSH_GETOPTS |
| 10327 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10328 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10329 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10330 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10331 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10332 | If a required argument is not found, and getopts is not silent, |
| 10333 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10334 | diagnostic message is printed. If getopts is silent, then a |
| 10335 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10336 | character found. |
| 10337 | |
| 10338 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10339 | |
| 10340 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10341 | */ |
| 10342 | char cbuf[2]; |
| 10343 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10344 | int c, n, exitcode, my_opterr; |
| 10345 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10346 | |
| 10347 | optstring = *++argv; |
| 10348 | if (!optstring || !(var = *++argv)) { |
| 10349 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10350 | return EXIT_FAILURE; |
| 10351 | } |
| 10352 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10353 | if (argv[1]) |
| 10354 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10355 | else |
| 10356 | argv = G.global_argv; |
| 10357 | cbuf[1] = '\0'; |
| 10358 | |
| 10359 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10360 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10361 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10362 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10363 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10364 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10365 | |
| 10366 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10367 | /*if (optstring[0] != '+')*/ { |
| 10368 | char *s = alloca(strlen(optstring) + 2); |
| 10369 | sprintf(s, "+%s", optstring); |
| 10370 | optstring = s; |
| 10371 | } |
| 10372 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10373 | /* Naively, now we should just |
| 10374 | * cp = get_local_var_value("OPTIND"); |
| 10375 | * optind = cp ? atoi(cp) : 0; |
| 10376 | * optarg = NULL; |
| 10377 | * opterr = my_opterr; |
| 10378 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10379 | * and be done? Not so fast... |
| 10380 | * Unlike normal getopt() usage in C programs, here |
| 10381 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10382 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10383 | * invocations of "getopts", there will be calls to shell builtins |
| 10384 | * which use getopt() internally. Example: |
| 10385 | * while getopts "abc" RES -a -bc -abc de; do |
| 10386 | * unset -ff func |
| 10387 | * done |
| 10388 | * This would not work correctly: getopt() call inside "unset" |
| 10389 | * modifies internal libc state which is tracking position in |
| 10390 | * multi-option strings ("-abc"). At best, it can skip options |
| 10391 | * or return the same option infinitely. With glibc implementation |
| 10392 | * of getopt(), it would use outright invalid pointers and return |
| 10393 | * garbage even _without_ "unset" mangling internal state. |
| 10394 | * |
| 10395 | * We resort to resetting getopt() state and calling it N times, |
| 10396 | * until we get Nth result (or failure). |
| 10397 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10398 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10399 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10400 | count = 0; |
| 10401 | n = string_array_len(argv); |
| 10402 | do { |
| 10403 | optarg = NULL; |
| 10404 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10405 | c = getopt(n, argv, optstring); |
| 10406 | if (c < 0) |
| 10407 | break; |
| 10408 | count++; |
| 10409 | } while (count <= G.getopt_count); |
| 10410 | |
| 10411 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10412 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10413 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10414 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10415 | |
| 10416 | /* Set OPTARG */ |
| 10417 | /* Always set or unset, never left as-is, even on exit/error: |
| 10418 | * "If no option was found, or if the option that was found |
| 10419 | * does not have an option-argument, OPTARG shall be unset." |
| 10420 | */ |
| 10421 | cp = optarg; |
| 10422 | if (c == '?') { |
| 10423 | /* If ":optstring" and unknown option is seen, |
| 10424 | * it is stored to OPTARG. |
| 10425 | */ |
| 10426 | if (optstring[1] == ':') { |
| 10427 | cbuf[0] = optopt; |
| 10428 | cp = cbuf; |
| 10429 | } |
| 10430 | } |
| 10431 | if (cp) |
| 10432 | set_local_var_from_halves("OPTARG", cp); |
| 10433 | else |
| 10434 | unset_local_var("OPTARG"); |
| 10435 | |
| 10436 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10437 | exitcode = EXIT_SUCCESS; |
| 10438 | if (c < 0) { /* -1: end of options */ |
| 10439 | exitcode = EXIT_FAILURE; |
| 10440 | c = '?'; |
| 10441 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10442 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10443 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10444 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10445 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10446 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10447 | return exitcode; |
| 10448 | } |
| 10449 | #endif |
| 10450 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10451 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10452 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10453 | char *arg_path, *filename; |
| 10454 | FILE *input; |
| 10455 | save_arg_t sv; |
| 10456 | char *args_need_save; |
| 10457 | #if ENABLE_HUSH_FUNCTIONS |
| 10458 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10459 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10460 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10461 | argv = skip_dash_dash(argv); |
| 10462 | filename = argv[0]; |
| 10463 | if (!filename) { |
| 10464 | /* bash says: "bash: .: filename argument required" */ |
| 10465 | return 2; /* bash compat */ |
| 10466 | } |
| 10467 | arg_path = NULL; |
| 10468 | if (!strchr(filename, '/')) { |
| 10469 | arg_path = find_in_path(filename); |
| 10470 | if (arg_path) |
| 10471 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10472 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10473 | errno = ENOENT; |
| 10474 | bb_simple_perror_msg(filename); |
| 10475 | return EXIT_FAILURE; |
| 10476 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10477 | } |
| 10478 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10479 | free(arg_path); |
| 10480 | if (!input) { |
| 10481 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10482 | /* POSIX: non-interactive shell should abort here, |
| 10483 | * not merely fail. So far no one complained :) |
| 10484 | */ |
| 10485 | return EXIT_FAILURE; |
| 10486 | } |
| 10487 | |
| 10488 | #if ENABLE_HUSH_FUNCTIONS |
| 10489 | sv_flg = G_flag_return_in_progress; |
| 10490 | /* "we are inside sourced file, ok to use return" */ |
| 10491 | G_flag_return_in_progress = -1; |
| 10492 | #endif |
| 10493 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10494 | if (args_need_save) |
| 10495 | save_and_replace_G_args(&sv, argv); |
| 10496 | |
| 10497 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10498 | G.last_exitcode = 0; |
| 10499 | parse_and_run_file(input); |
| 10500 | fclose_and_forget(input); |
| 10501 | |
| 10502 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10503 | restore_G_args(&sv, argv); |
| 10504 | #if ENABLE_HUSH_FUNCTIONS |
| 10505 | G_flag_return_in_progress = sv_flg; |
| 10506 | #endif |
| 10507 | |
| 10508 | return G.last_exitcode; |
| 10509 | } |
| 10510 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10511 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10512 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10513 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10514 | int sig; |
| 10515 | char *new_cmd; |
| 10516 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10517 | if (!G_traps) |
| 10518 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10519 | |
| 10520 | argv++; |
| 10521 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10522 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10523 | /* No args: print all trapped */ |
| 10524 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10525 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10526 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10527 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10528 | /* note: bash adds "SIG", but only if invoked |
| 10529 | * as "bash". If called as "sh", or if set -o posix, |
| 10530 | * then it prints short signal names. |
| 10531 | * We are printing short names: */ |
| 10532 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10533 | } |
| 10534 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10535 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10536 | return EXIT_SUCCESS; |
| 10537 | } |
| 10538 | |
| 10539 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10540 | /* If first arg is a number: reset all specified signals */ |
| 10541 | sig = bb_strtou(*argv, NULL, 10); |
| 10542 | if (errno == 0) { |
| 10543 | int ret; |
| 10544 | process_sig_list: |
| 10545 | ret = EXIT_SUCCESS; |
| 10546 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10547 | sighandler_t handler; |
| 10548 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10549 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10550 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10551 | ret = EXIT_FAILURE; |
| 10552 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10553 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10554 | continue; |
| 10555 | } |
| 10556 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10557 | free(G_traps[sig]); |
| 10558 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10559 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10560 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10561 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10562 | |
| 10563 | /* There is no signal for 0 (EXIT) */ |
| 10564 | if (sig == 0) |
| 10565 | continue; |
| 10566 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10567 | if (new_cmd) |
| 10568 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10569 | else |
| 10570 | /* We are removing trap handler */ |
| 10571 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10572 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10573 | } |
| 10574 | return ret; |
| 10575 | } |
| 10576 | |
| 10577 | if (!argv[1]) { /* no second arg */ |
| 10578 | bb_error_msg("trap: invalid arguments"); |
| 10579 | return EXIT_FAILURE; |
| 10580 | } |
| 10581 | |
| 10582 | /* First arg is "-": reset all specified to default */ |
| 10583 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10584 | /* Everything else: set arg as signal handler |
| 10585 | * (includes "" case, which ignores signal) */ |
| 10586 | if (argv[0][0] == '-') { |
| 10587 | if (argv[0][1] == '\0') { /* "-" */ |
| 10588 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10589 | goto reset_traps; |
| 10590 | } |
| 10591 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10592 | argv++; |
| 10593 | } |
| 10594 | /* else: "-something", no special meaning */ |
| 10595 | } |
| 10596 | new_cmd = *argv; |
| 10597 | reset_traps: |
| 10598 | argv++; |
| 10599 | goto process_sig_list; |
| 10600 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10601 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10602 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10603 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10604 | static struct pipe *parse_jobspec(const char *str) |
| 10605 | { |
| 10606 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10607 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10608 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10609 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10610 | if (str[0] != '%' |
| 10611 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10612 | ) { |
| 10613 | bb_error_msg("bad argument '%s'", str); |
| 10614 | return NULL; |
| 10615 | } |
| 10616 | /* It is "%%", "%+" or "%" - current job */ |
| 10617 | jobnum = G.last_jobid; |
| 10618 | if (jobnum == 0) { |
| 10619 | bb_error_msg("no current job"); |
| 10620 | return NULL; |
| 10621 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10622 | } |
| 10623 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10624 | if (pi->jobid == jobnum) { |
| 10625 | return pi; |
| 10626 | } |
| 10627 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10628 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10629 | return NULL; |
| 10630 | } |
| 10631 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10632 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10633 | { |
| 10634 | struct pipe *job; |
| 10635 | const char *status_string; |
| 10636 | |
| 10637 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10638 | for (job = G.job_list; job; job = job->next) { |
| 10639 | if (job->alive_cmds == job->stopped_cmds) |
| 10640 | status_string = "Stopped"; |
| 10641 | else |
| 10642 | status_string = "Running"; |
| 10643 | |
| 10644 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10645 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10646 | |
| 10647 | clean_up_last_dead_job(); |
| 10648 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10649 | return EXIT_SUCCESS; |
| 10650 | } |
| 10651 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10652 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10653 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10654 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10655 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10656 | struct pipe *pi; |
| 10657 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10658 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10659 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10660 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10661 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10662 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10663 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10664 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10665 | goto found; |
| 10666 | } |
| 10667 | } |
| 10668 | bb_error_msg("%s: no current job", argv[0]); |
| 10669 | return EXIT_FAILURE; |
| 10670 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10671 | |
| 10672 | pi = parse_jobspec(argv[1]); |
| 10673 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10674 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10675 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10676 | /* TODO: bash prints a string representation |
| 10677 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10678 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10679 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10680 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10681 | } |
| 10682 | |
| 10683 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10684 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10685 | for (i = 0; i < pi->num_cmds; i++) { |
| 10686 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10687 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10688 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10689 | |
| 10690 | i = kill(- pi->pgrp, SIGCONT); |
| 10691 | if (i < 0) { |
| 10692 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10693 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10694 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10695 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10696 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10697 | } |
| 10698 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10699 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10700 | 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] | 10701 | return checkjobs_and_fg_shell(pi); |
| 10702 | } |
| 10703 | return EXIT_SUCCESS; |
| 10704 | } |
| 10705 | #endif |
| 10706 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10707 | #if ENABLE_HUSH_KILL |
| 10708 | static int FAST_FUNC builtin_kill(char **argv) |
| 10709 | { |
| 10710 | int ret = 0; |
| 10711 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10712 | # if ENABLE_HUSH_JOB |
| 10713 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10714 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10715 | |
| 10716 | do { |
| 10717 | struct pipe *pi; |
| 10718 | char *dst; |
| 10719 | int j, n; |
| 10720 | |
| 10721 | if (argv[i][0] != '%') |
| 10722 | continue; |
| 10723 | /* |
| 10724 | * "kill %N" - job kill |
| 10725 | * Converting to pgrp / pid kill |
| 10726 | */ |
| 10727 | pi = parse_jobspec(argv[i]); |
| 10728 | if (!pi) { |
| 10729 | /* Eat bad jobspec */ |
| 10730 | j = i; |
| 10731 | do { |
| 10732 | j++; |
| 10733 | argv[j - 1] = argv[j]; |
| 10734 | } while (argv[j]); |
| 10735 | ret = 1; |
| 10736 | i--; |
| 10737 | continue; |
| 10738 | } |
| 10739 | /* |
| 10740 | * In jobs started under job control, we signal |
| 10741 | * entire process group by kill -PGRP_ID. |
| 10742 | * This happens, f.e., in interactive shell. |
| 10743 | * |
| 10744 | * Otherwise, we signal each child via |
| 10745 | * kill PID1 PID2 PID3. |
| 10746 | * Testcases: |
| 10747 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10748 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10749 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10750 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10751 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10752 | dst = alloca(n * sizeof(int)*4); |
| 10753 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10754 | if (G_interactive_fd) |
| 10755 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10756 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10757 | struct command *cmd = &pi->cmds[j]; |
| 10758 | /* Skip exited members of the job */ |
| 10759 | if (cmd->pid == 0) |
| 10760 | continue; |
| 10761 | /* |
| 10762 | * kill_main has matching code to expect |
| 10763 | * leading space. Needed to not confuse |
| 10764 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10765 | */ |
| 10766 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10767 | } |
| 10768 | *dst = '\0'; |
| 10769 | } while (argv[++i]); |
| 10770 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10771 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10772 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10773 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10774 | ret = run_applet_main(argv, kill_main); |
| 10775 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10776 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10777 | return ret; |
| 10778 | } |
| 10779 | #endif |
| 10780 | |
| 10781 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10782 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10783 | #if !ENABLE_HUSH_JOB |
| 10784 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10785 | #endif |
| 10786 | 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] | 10787 | { |
| 10788 | int ret = 0; |
| 10789 | for (;;) { |
| 10790 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10791 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10792 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10793 | if (!sigisemptyset(&G.pending_set)) |
| 10794 | goto check_sig; |
| 10795 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10796 | /* waitpid is not interruptible by SA_RESTARTed |
| 10797 | * signals which we use. Thus, this ugly dance: |
| 10798 | */ |
| 10799 | |
| 10800 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10801 | * pending signal mask before we call waitpid. |
| 10802 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10803 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10804 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10805 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 10806 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10807 | |
| 10808 | if (!sigisemptyset(&G.pending_set)) { |
| 10809 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10810 | goto restore; |
| 10811 | } |
| 10812 | |
| 10813 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10814 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10815 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10816 | debug_printf_exec("checkjobs:%d\n", ret); |
| 10817 | #if ENABLE_HUSH_JOB |
| 10818 | if (waitfor_pipe) { |
| 10819 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 10820 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 10821 | if (rcode >= 0) { |
| 10822 | ret = rcode; |
| 10823 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10824 | break; |
| 10825 | } |
| 10826 | } |
| 10827 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10828 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 10829 | /* if ret == 0, no children changed state */ |
| 10830 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10831 | if (errno == ECHILD || ret) { |
| 10832 | ret--; |
| 10833 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10834 | ret = 0; |
| 10835 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10836 | break; |
| 10837 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10838 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10839 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 10840 | /* Note: sigsuspend invokes signal handler */ |
| 10841 | sigsuspend(&oldset); |
| 10842 | restore: |
| 10843 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10844 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10845 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10846 | sig = check_and_run_traps(); |
| 10847 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 10848 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10849 | ret = 128 + sig; |
| 10850 | break; |
| 10851 | } |
| 10852 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 10853 | } |
| 10854 | return ret; |
| 10855 | } |
| 10856 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10857 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10858 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10859 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10860 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10861 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10862 | argv = skip_dash_dash(argv); |
| 10863 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 10864 | /* Don't care about wait results */ |
| 10865 | /* Note 1: must wait until there are no more children */ |
| 10866 | /* Note 2: must be interruptible */ |
| 10867 | /* Examples: |
| 10868 | * $ sleep 3 & sleep 6 & wait |
| 10869 | * [1] 30934 sleep 3 |
| 10870 | * [2] 30935 sleep 6 |
| 10871 | * [1] Done sleep 3 |
| 10872 | * [2] Done sleep 6 |
| 10873 | * $ sleep 3 & sleep 6 & wait |
| 10874 | * [1] 30936 sleep 3 |
| 10875 | * [2] 30937 sleep 6 |
| 10876 | * [1] Done sleep 3 |
| 10877 | * ^C <-- after ~4 sec from keyboard |
| 10878 | * $ |
| 10879 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10880 | 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] | 10881 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10882 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10883 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10884 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10885 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10886 | #if ENABLE_HUSH_JOB |
| 10887 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10888 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10889 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10890 | wait_pipe = parse_jobspec(*argv); |
| 10891 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10892 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10893 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10894 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10895 | } else { |
| 10896 | /* waiting on "last dead job" removes it */ |
| 10897 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 10898 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10899 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10900 | /* else: parse_jobspec() already emitted error msg */ |
| 10901 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10902 | } |
| 10903 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10904 | /* mimic bash message */ |
| 10905 | 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] | 10906 | ret = EXIT_FAILURE; |
| 10907 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10908 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10909 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10910 | /* Do we have such child? */ |
| 10911 | ret = waitpid(pid, &status, WNOHANG); |
| 10912 | if (ret < 0) { |
| 10913 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10914 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10915 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 10916 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10917 | /* "wait $!" but last bg task has already exited. Try: |
| 10918 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 10919 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10920 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10921 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10922 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10923 | } else { |
| 10924 | /* Example: "wait 1". mimic bash message */ |
| 10925 | 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] | 10926 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10927 | } else { |
| 10928 | /* ??? */ |
| 10929 | bb_perror_msg("wait %s", *argv); |
| 10930 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10931 | continue; /* bash checks all argv[] */ |
| 10932 | } |
| 10933 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10934 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10935 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10936 | } else { |
| 10937 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10938 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 10939 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10940 | if (WIFSIGNALED(status)) |
| 10941 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10942 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10943 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10944 | |
| 10945 | return ret; |
| 10946 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10947 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10948 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10949 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 10950 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 10951 | { |
| 10952 | if (argv[1]) { |
| 10953 | def = bb_strtou(argv[1], NULL, 10); |
| 10954 | if (errno || def < def_min || argv[2]) { |
| 10955 | bb_error_msg("%s: bad arguments", argv[0]); |
| 10956 | def = UINT_MAX; |
| 10957 | } |
| 10958 | } |
| 10959 | return def; |
| 10960 | } |
| 10961 | #endif |
| 10962 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10963 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10964 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10965 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10966 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10967 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10968 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10969 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 10970 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 10971 | return EXIT_SUCCESS; /* bash compat */ |
| 10972 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10973 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10974 | |
| 10975 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 10976 | if (depth == UINT_MAX) |
| 10977 | G.flag_break_continue = BC_BREAK; |
| 10978 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10979 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10980 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10981 | return EXIT_SUCCESS; |
| 10982 | } |
| 10983 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10984 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10985 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10986 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 10987 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10988 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10989 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10990 | |
| 10991 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10992 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10993 | { |
| 10994 | int rc; |
| 10995 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10996 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10997 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 10998 | return EXIT_FAILURE; /* bash compat */ |
| 10999 | } |
| 11000 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 11001 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 11002 | |
| 11003 | /* bash: |
| 11004 | * out of range: wraps around at 256, does not error out |
| 11005 | * non-numeric param: |
| 11006 | * f() { false; return qwe; }; f; echo $? |
| 11007 | * bash: return: qwe: numeric argument required <== we do this |
| 11008 | * 255 <== we also do this |
| 11009 | */ |
| 11010 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 11011 | return rc; |
| 11012 | } |
| 11013 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11014 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 11015 | #if ENABLE_HUSH_TIMES |
| 11016 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 11017 | { |
| 11018 | static const uint8_t times_tbl[] ALIGN1 = { |
| 11019 | ' ', offsetof(struct tms, tms_utime), |
| 11020 | '\n', offsetof(struct tms, tms_stime), |
| 11021 | ' ', offsetof(struct tms, tms_cutime), |
| 11022 | '\n', offsetof(struct tms, tms_cstime), |
| 11023 | 0 |
| 11024 | }; |
| 11025 | const uint8_t *p; |
| 11026 | unsigned clk_tck; |
| 11027 | struct tms buf; |
| 11028 | |
| 11029 | clk_tck = bb_clk_tck(); |
| 11030 | |
| 11031 | times(&buf); |
| 11032 | p = times_tbl; |
| 11033 | do { |
| 11034 | unsigned sec, frac; |
| 11035 | unsigned long t; |
| 11036 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 11037 | sec = t / clk_tck; |
| 11038 | frac = t % clk_tck; |
| 11039 | printf("%um%u.%03us%c", |
| 11040 | sec / 60, sec % 60, |
| 11041 | (frac * 1000) / clk_tck, |
| 11042 | p[0]); |
| 11043 | p += 2; |
| 11044 | } while (*p); |
| 11045 | |
| 11046 | return EXIT_SUCCESS; |
| 11047 | } |
| 11048 | #endif |
| 11049 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 11050 | #if ENABLE_HUSH_MEMLEAK |
| 11051 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 11052 | { |
| 11053 | void *p; |
| 11054 | unsigned long l; |
| 11055 | |
| 11056 | # ifdef M_TRIM_THRESHOLD |
| 11057 | /* Optional. Reduces probability of false positives */ |
| 11058 | malloc_trim(0); |
| 11059 | # endif |
| 11060 | /* Crude attempt to find where "free memory" starts, |
| 11061 | * sans fragmentation. */ |
| 11062 | p = malloc(240); |
| 11063 | l = (unsigned long)p; |
| 11064 | free(p); |
| 11065 | p = malloc(3400); |
| 11066 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 11067 | free(p); |
| 11068 | |
| 11069 | |
| 11070 | # if 0 /* debug */ |
| 11071 | { |
| 11072 | struct mallinfo mi = mallinfo(); |
| 11073 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 11074 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 11075 | } |
| 11076 | # endif |
| 11077 | |
| 11078 | if (!G.memleak_value) |
| 11079 | G.memleak_value = l; |
| 11080 | |
| 11081 | l -= G.memleak_value; |
| 11082 | if ((long)l < 0) |
| 11083 | l = 0; |
| 11084 | l /= 1024; |
| 11085 | if (l > 127) |
| 11086 | l = 127; |
| 11087 | |
| 11088 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 11089 | return l; |
| 11090 | } |
| 11091 | #endif |