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 | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 82 | * In bash, export builtin is special, its arguments are assignments |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 83 | * and therefore expansion of them should be "one-word" expansion: |
| 84 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 85 | * compare with: |
| 86 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 87 | * ls: cannot access i=a: No such file or directory |
| 88 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 89 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 90 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 91 | * is not quoted: |
| 92 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 93 | * aaa bbb |
| 94 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 95 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 96 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 97 | //config:config HUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 98 | //config: bool "hush (64 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 99 | //config: default y |
| 100 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 101 | //config: hush is a small shell. It handles the normal flow control |
| 102 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 103 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 104 | //config: and functions are supported. |
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 will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 107 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 108 | //config: It does not handle select, aliases, tilde expansion, |
| 109 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 110 | //config: |
| 111 | //config:config HUSH_BASH_COMPAT |
| 112 | //config: bool "bash-compatible extensions" |
| 113 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 114 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 115 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 116 | //config:config HUSH_BRACE_EXPANSION |
| 117 | //config: bool "Brace expansion" |
| 118 | //config: default y |
| 119 | //config: depends on HUSH_BASH_COMPAT |
| 120 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 121 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 122 | //config: |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 123 | //config:config HUSH_LINENO_VAR |
| 124 | //config: bool "$LINENO variable" |
| 125 | //config: default y |
| 126 | //config: depends on HUSH_BASH_COMPAT |
| 127 | //config: |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 128 | //config:config HUSH_BASH_SOURCE_CURDIR |
| 129 | //config: bool "'source' and '.' builtins search current directory after $PATH" |
| 130 | //config: default n # do not encourage non-standard behavior |
| 131 | //config: depends on HUSH_BASH_COMPAT |
| 132 | //config: help |
| 133 | //config: This is not compliant with standards. Avoid if possible. |
| 134 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 135 | //config:config HUSH_INTERACTIVE |
| 136 | //config: bool "Interactive mode" |
| 137 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 138 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 139 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 140 | //config: Enable interactive mode (prompt and command editing). |
| 141 | //config: Without this, hush simply reads and executes commands |
| 142 | //config: from stdin just like a shell script from a file. |
| 143 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 144 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 145 | //config:config HUSH_SAVEHISTORY |
| 146 | //config: bool "Save command history to .hush_history" |
| 147 | //config: default y |
| 148 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 149 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 150 | //config:config HUSH_JOB |
| 151 | //config: bool "Job control" |
| 152 | //config: default y |
| 153 | //config: depends on HUSH_INTERACTIVE |
| 154 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 155 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 156 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 157 | //config: "cmd &" still works by simply spawning a process and immediately |
| 158 | //config: prompting for next command (or executing next command in a script), |
| 159 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 160 | //config: |
| 161 | //config:config HUSH_TICK |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 162 | //config: bool "Support process substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 163 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 164 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 165 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 166 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 167 | //config: |
| 168 | //config:config HUSH_IF |
| 169 | //config: bool "Support if/then/elif/else/fi" |
| 170 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 171 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 172 | //config: |
| 173 | //config:config HUSH_LOOPS |
| 174 | //config: bool "Support for, while and until loops" |
| 175 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 176 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 177 | //config: |
| 178 | //config:config HUSH_CASE |
| 179 | //config: bool "Support case ... esac statement" |
| 180 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 181 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 182 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 183 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 184 | //config: |
| 185 | //config:config HUSH_FUNCTIONS |
| 186 | //config: bool "Support funcname() { commands; } syntax" |
| 187 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 188 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 189 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 190 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 191 | //config: |
| 192 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 193 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 194 | //config: default y |
| 195 | //config: depends on HUSH_FUNCTIONS |
| 196 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 197 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 198 | //config: |
| 199 | //config:config HUSH_RANDOM_SUPPORT |
| 200 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 201 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 202 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 203 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 204 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 205 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 206 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 207 | //config:config HUSH_MODE_X |
| 208 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 209 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 210 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 211 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 212 | //config: This instructs hush to print commands before execution. |
| 213 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 214 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 215 | //config:config HUSH_ECHO |
| 216 | //config: bool "echo builtin" |
| 217 | //config: default y |
| 218 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 219 | //config: |
| 220 | //config:config HUSH_PRINTF |
| 221 | //config: bool "printf builtin" |
| 222 | //config: default y |
| 223 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 224 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 225 | //config:config HUSH_TEST |
| 226 | //config: bool "test builtin" |
| 227 | //config: default y |
| 228 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 229 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 230 | //config:config HUSH_HELP |
| 231 | //config: bool "help builtin" |
| 232 | //config: default y |
| 233 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 234 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 235 | //config:config HUSH_EXPORT |
| 236 | //config: bool "export builtin" |
| 237 | //config: default y |
| 238 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 239 | //config: |
| 240 | //config:config HUSH_EXPORT_N |
| 241 | //config: bool "Support 'export -n' option" |
| 242 | //config: default y |
| 243 | //config: depends on HUSH_EXPORT |
| 244 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 245 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 246 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 247 | //config:config HUSH_READONLY |
| 248 | //config: bool "readonly builtin" |
| 249 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 250 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 251 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 252 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 253 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 254 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 255 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 256 | //config: default y |
| 257 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 258 | //config: |
| 259 | //config:config HUSH_WAIT |
| 260 | //config: bool "wait builtin" |
| 261 | //config: default y |
| 262 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 263 | //config: |
Denys Vlasenko | 3bb3e1d | 2018-01-11 18:05:05 +0100 | [diff] [blame] | 264 | //config:config HUSH_COMMAND |
| 265 | //config: bool "command builtin" |
| 266 | //config: default y |
| 267 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 268 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 269 | //config:config HUSH_TRAP |
| 270 | //config: bool "trap builtin" |
| 271 | //config: default y |
| 272 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 273 | //config: |
| 274 | //config:config HUSH_TYPE |
| 275 | //config: bool "type builtin" |
| 276 | //config: default y |
| 277 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 278 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 279 | //config:config HUSH_TIMES |
| 280 | //config: bool "times builtin" |
| 281 | //config: default y |
| 282 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 283 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 284 | //config:config HUSH_READ |
| 285 | //config: bool "read builtin" |
| 286 | //config: default y |
| 287 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 288 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 289 | //config:config HUSH_SET |
| 290 | //config: bool "set builtin" |
| 291 | //config: default y |
| 292 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 293 | //config: |
| 294 | //config:config HUSH_UNSET |
| 295 | //config: bool "unset builtin" |
| 296 | //config: default y |
| 297 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 298 | //config: |
| 299 | //config:config HUSH_ULIMIT |
| 300 | //config: bool "ulimit builtin" |
| 301 | //config: default y |
| 302 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 303 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 304 | //config:config HUSH_UMASK |
| 305 | //config: bool "umask builtin" |
| 306 | //config: default y |
| 307 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 308 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 309 | //config:config HUSH_GETOPTS |
| 310 | //config: bool "getopts builtin" |
| 311 | //config: default y |
| 312 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 313 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 314 | //config:config HUSH_MEMLEAK |
| 315 | //config: bool "memleak builtin (debugging)" |
| 316 | //config: default n |
| 317 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 318 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 319 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 320 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 321 | //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] | 322 | //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] | 323 | |
| 324 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 325 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 326 | //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] | 327 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 328 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 329 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 330 | * but currently do nothing, therefore aren't shown in help. |
| 331 | * NOMMU-specific options are not meant to be used by users, |
| 332 | * therefore we don't show them either. |
| 333 | */ |
| 334 | //usage:#define hush_trivial_usage |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 335 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 336 | //usage:#define hush_full_usage "\n\n" |
| 337 | //usage: "Unix shell interpreter" |
| 338 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 339 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 340 | || defined(__APPLE__) \ |
| 341 | ) |
| 342 | # include <malloc.h> /* for malloc_trim */ |
| 343 | #endif |
| 344 | #include <glob.h> |
| 345 | /* #include <dmalloc.h> */ |
| 346 | #if ENABLE_HUSH_CASE |
| 347 | # include <fnmatch.h> |
| 348 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 349 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 350 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 351 | |
| 352 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 353 | #include "unicode.h" |
| 354 | #include "shell_common.h" |
| 355 | #include "math.h" |
| 356 | #include "match.h" |
| 357 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 358 | # include "random.h" |
| 359 | #else |
| 360 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 361 | #endif |
| 362 | #ifndef F_DUPFD_CLOEXEC |
| 363 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 364 | #endif |
| 365 | #ifndef PIPE_BUF |
| 366 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 367 | #endif |
| 368 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 369 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 370 | /* So far, all bash compat is controlled by one config option */ |
| 371 | /* Separate defines document which part of code implements what */ |
| 372 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 373 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 374 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 375 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 376 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 377 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 378 | |
| 379 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 380 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 381 | #define LEAK_HUNTING 0 |
| 382 | #define BUILD_AS_NOMMU 0 |
| 383 | /* Enable/disable sanity checks. Ok to enable in production, |
| 384 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 385 | * Keeping 1 for now even in released versions. |
| 386 | */ |
| 387 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 388 | /* Slightly bigger (+200 bytes), but faster hush. |
| 389 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 390 | * which allows us to do fewer waitpid's. |
| 391 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 392 | * and therefore waitpid will return the same result as last time) |
| 393 | */ |
| 394 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 395 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 396 | * So far ${var%...} ops are always enabled: |
| 397 | */ |
| 398 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 399 | |
| 400 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 401 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 402 | # undef BB_MMU |
| 403 | # undef USE_FOR_NOMMU |
| 404 | # undef USE_FOR_MMU |
| 405 | # define BB_MMU 0 |
| 406 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 407 | # define USE_FOR_MMU(...) |
| 408 | #endif |
| 409 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 410 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 411 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 412 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 413 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 414 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 415 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 416 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 417 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 418 | # define IF_FEATURE_SH_STANDALONE(...) |
| 419 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 420 | #endif |
| 421 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 422 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 423 | # undef ENABLE_FEATURE_EDITING |
| 424 | # define ENABLE_FEATURE_EDITING 0 |
| 425 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 426 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 427 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 428 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 429 | #endif |
| 430 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 431 | /* Do we support ANY keywords? */ |
| 432 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 433 | # define HAS_KEYWORDS 1 |
| 434 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 435 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 436 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 437 | # define HAS_KEYWORDS 0 |
| 438 | # define IF_HAS_KEYWORDS(...) |
| 439 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 440 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 441 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 442 | /* If you comment out one of these below, it will be #defined later |
| 443 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 445 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 446 | #define debug_printf_parse(...) do {} while (0) |
| 447 | #define debug_print_tree(a, b) do {} while (0) |
| 448 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 449 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 450 | #define debug_printf_jobs(...) do {} while (0) |
| 451 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 452 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 453 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 454 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 455 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 456 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 457 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 458 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 459 | #define ERR_PTR ((void*)(long)1) |
| 460 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 461 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 462 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 463 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 464 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 465 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 466 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 467 | /* Support / and // replace ops */ |
| 468 | /* Note that // is stored as \ in "encoded" string representation */ |
| 469 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 470 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 471 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 472 | #else |
| 473 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 474 | # define VAR_SUBST_OPS "%#:-=+?" |
| 475 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 476 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 477 | |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 478 | #define SPECIAL_VAR_SYMBOL_STR "\3" |
| 479 | #define SPECIAL_VAR_SYMBOL 3 |
| 480 | /* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */ |
| 481 | #define SPECIAL_VAR_QUOTED_SVS 1 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 482 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 483 | struct variable; |
| 484 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 485 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 486 | |
| 487 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 488 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 489 | */ |
| 490 | #if !BB_MMU |
| 491 | typedef struct nommu_save_t { |
| 492 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 493 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 494 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 495 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 496 | } nommu_save_t; |
| 497 | #endif |
| 498 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 499 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 500 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 501 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 502 | RES_IF , |
| 503 | RES_THEN , |
| 504 | RES_ELIF , |
| 505 | RES_ELSE , |
| 506 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 507 | #endif |
| 508 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 509 | RES_FOR , |
| 510 | RES_WHILE , |
| 511 | RES_UNTIL , |
| 512 | RES_DO , |
| 513 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 514 | #endif |
| 515 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 516 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 517 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 518 | #if ENABLE_HUSH_CASE |
| 519 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 520 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 521 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 522 | RES_MATCH , /* "word)" */ |
| 523 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 524 | RES_ESAC , |
| 525 | #endif |
| 526 | RES_XXXX , |
| 527 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 528 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 529 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 530 | typedef struct o_string { |
| 531 | char *data; |
| 532 | int length; /* position where data is appended */ |
| 533 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 534 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 535 | /* At least some part of the string was inside '' or "", |
| 536 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 537 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 538 | smallint has_empty_slot; |
| 539 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 540 | } o_string; |
| 541 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 542 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 543 | EXP_FLAG_GLOB = 0x2, |
| 544 | /* Protect newly added chars against globbing |
| 545 | * by prepending \ to *, ?, [, \ */ |
| 546 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 547 | }; |
| 548 | enum { |
| 549 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 550 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 551 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 552 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 553 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 554 | }; |
| 555 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 556 | #define NULL_O_STRING { NULL } |
| 557 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 558 | #ifndef debug_printf_parse |
| 559 | static const char *const assignment_flag[] = { |
| 560 | "MAYBE_ASSIGNMENT", |
| 561 | "DEFINITELY_ASSIGNMENT", |
| 562 | "NOT_ASSIGNMENT", |
| 563 | "WORD_IS_KEYWORD", |
| 564 | }; |
| 565 | #endif |
| 566 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 567 | typedef struct in_str { |
| 568 | const char *p; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 569 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 570 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 571 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 572 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 573 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 574 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 575 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 576 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 577 | /* The descrip member of this structure is only used to make |
| 578 | * debugging output pretty */ |
| 579 | static const struct { |
| 580 | int mode; |
| 581 | signed char default_fd; |
| 582 | char descrip[3]; |
| 583 | } redir_table[] = { |
| 584 | { O_RDONLY, 0, "<" }, |
| 585 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 586 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 587 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 588 | { O_RDONLY, 0, "<<" }, |
| 589 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 590 | /* { O_RDONLY, 77, "<<" }, */ |
| 591 | }; |
| 592 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 593 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 594 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 595 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 596 | int rd_fd; /* fd to redirect */ |
| 597 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 598 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 599 | smallint rd_type; /* (enum redir_type) */ |
| 600 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 601 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 602 | * bit 0: do we need to trim leading tabs? |
| 603 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 604 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 605 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 606 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 607 | REDIRECT_INPUT = 0, |
| 608 | REDIRECT_OVERWRITE = 1, |
| 609 | REDIRECT_APPEND = 2, |
| 610 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 611 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 612 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 613 | |
| 614 | REDIRFD_CLOSE = -3, |
| 615 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 616 | REDIRFD_TO_FILE = -1, |
| 617 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 618 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 619 | HEREDOC_SKIPTABS = 1, |
| 620 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 621 | } redir_type; |
| 622 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 623 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 624 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 625 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 626 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 627 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 628 | unsigned lineno; |
| 629 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 630 | smallint cmd_type; /* CMD_xxx */ |
| 631 | #define CMD_NORMAL 0 |
| 632 | #define CMD_SUBSHELL 1 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 633 | #if BASH_TEST2 |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 634 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 635 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 636 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 637 | #if ENABLE_HUSH_FUNCTIONS |
| 638 | # define CMD_FUNCDEF 3 |
| 639 | #endif |
| 640 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 641 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 642 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 643 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 644 | #if !BB_MMU |
| 645 | char *group_as_string; |
| 646 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 647 | #if ENABLE_HUSH_FUNCTIONS |
| 648 | struct function *child_func; |
| 649 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 650 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 651 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 652 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 653 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 654 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 655 | * we put those fields back into cmd->xxx |
| 656 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 657 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 658 | * Without this trick, loop would execute a;b;b;b;... |
| 659 | * instead of correct sequence a;b;a;b;... |
| 660 | * When command is freed, it severs the link |
| 661 | * (sets ->child_func->parent_cmd to NULL). |
| 662 | */ |
| 663 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 664 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 665 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 666 | * and on execution these are substituted with their values. |
| 667 | * Substitution can make _several_ words out of one argv[n]! |
| 668 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 669 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 670 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 671 | struct redir_struct *redirects; /* I/O redirections */ |
| 672 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 673 | /* Is there anything in this command at all? */ |
| 674 | #define IS_NULL_CMD(cmd) \ |
| 675 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 676 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 677 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 678 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 679 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 680 | int alive_cmds; /* number of commands running (not exited) */ |
| 681 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 682 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 683 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 684 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 685 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 686 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 687 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 688 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 689 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 690 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 691 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 692 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 693 | PIPE_SEQ = 0, |
| 694 | PIPE_AND = 1, |
| 695 | PIPE_OR = 2, |
| 696 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 697 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 698 | /* Is there anything in this pipe at all? */ |
| 699 | #define IS_NULL_PIPE(pi) \ |
| 700 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 701 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 702 | /* This holds pointers to the various results of parsing */ |
| 703 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 704 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 705 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 706 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 707 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 708 | /* last command in pipe (being constructed right now) */ |
| 709 | struct command *command; |
| 710 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 711 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 712 | #if !BB_MMU |
| 713 | o_string as_string; |
| 714 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 715 | #if HAS_KEYWORDS |
| 716 | smallint ctx_res_w; |
| 717 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 718 | #if ENABLE_HUSH_CASE |
| 719 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 720 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 721 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 722 | int old_flag; |
| 723 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 724 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 725 | * when we see "if" or "then", we malloc and copy current context, |
| 726 | * and make ->stack point to it. then we parse pipeN. |
| 727 | * when closing "then" / fi" / whatever is found, |
| 728 | * we move list_head into ->stack->command->group, |
| 729 | * copy ->stack into current context, and delete ->stack. |
| 730 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 731 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 732 | struct parse_context *stack; |
| 733 | #endif |
| 734 | }; |
| 735 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 736 | /* On program start, environ points to initial environment. |
| 737 | * putenv adds new pointers into it, unsetenv removes them. |
| 738 | * Neither of these (de)allocates the strings. |
| 739 | * setenv allocates new strings in malloc space and does putenv, |
| 740 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 741 | #define setenv(...) setenv_is_leaky_dont_use() |
| 742 | struct variable { |
| 743 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 744 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 745 | #if ENABLE_HUSH_LOCAL |
| 746 | unsigned func_nest_level; |
| 747 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 748 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 749 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 750 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 751 | }; |
| 752 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 753 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 754 | BC_BREAK = 1, |
| 755 | BC_CONTINUE = 2, |
| 756 | }; |
| 757 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 758 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 759 | struct function { |
| 760 | struct function *next; |
| 761 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 762 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 763 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 764 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 765 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 766 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 767 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 768 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 769 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 770 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 771 | /* set -/+o OPT support. (TODO: make it optional) |
| 772 | * bash supports the following opts: |
| 773 | * allexport off |
| 774 | * braceexpand on |
| 775 | * emacs on |
| 776 | * errexit off |
| 777 | * errtrace off |
| 778 | * functrace off |
| 779 | * hashall on |
| 780 | * histexpand off |
| 781 | * history on |
| 782 | * ignoreeof off |
| 783 | * interactive-comments on |
| 784 | * keyword off |
| 785 | * monitor on |
| 786 | * noclobber off |
| 787 | * noexec off |
| 788 | * noglob off |
| 789 | * nolog off |
| 790 | * notify off |
| 791 | * nounset off |
| 792 | * onecmd off |
| 793 | * physical off |
| 794 | * pipefail off |
| 795 | * posix off |
| 796 | * privileged off |
| 797 | * verbose off |
| 798 | * vi off |
| 799 | * xtrace off |
| 800 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 801 | static const char o_opt_strings[] ALIGN1 = |
| 802 | "pipefail\0" |
| 803 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 804 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 805 | #if ENABLE_HUSH_MODE_X |
| 806 | "xtrace\0" |
| 807 | #endif |
| 808 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 809 | enum { |
| 810 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 811 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 812 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 813 | #if ENABLE_HUSH_MODE_X |
| 814 | OPT_O_XTRACE, |
| 815 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 816 | NUM_OPT_O |
| 817 | }; |
| 818 | |
| 819 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 820 | struct FILE_list { |
| 821 | struct FILE_list *next; |
| 822 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 823 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 824 | }; |
| 825 | |
| 826 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 827 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 828 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 829 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 830 | /* interactive_fd != 0 means we are an interactive shell. |
| 831 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 832 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 833 | * job control still works, but terminal signals |
| 834 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 835 | * process groups can only be created with "cmd &". |
| 836 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 837 | * to give tty to the foreground process group, |
| 838 | * and will take it back when the group is stopped (^Z) |
| 839 | * or killed (^C). |
| 840 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 841 | #if ENABLE_HUSH_INTERACTIVE |
| 842 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 843 | * _AND_ if we decided to act interactively */ |
| 844 | int interactive_fd; |
| 845 | const char *PS1; |
| 846 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 847 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 848 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 849 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 850 | #endif |
| 851 | #if ENABLE_FEATURE_EDITING |
| 852 | line_input_t *line_input_state; |
| 853 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 854 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 855 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 856 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 857 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 858 | random_t random_gen; |
| 859 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 860 | #if ENABLE_HUSH_JOB |
| 861 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 862 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 863 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 864 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 865 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 866 | #else |
| 867 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 868 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 869 | /* How deeply are we in context where "set -e" is ignored */ |
| 870 | int errexit_depth; |
| 871 | /* "set -e" rules (do we follow them correctly?): |
| 872 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 873 | * Shell does not exit if failed command is part of condition in |
| 874 | * if/while, part of && or || list except the last command, any command |
| 875 | * in a pipe but the last, or if the command's return value is being |
| 876 | * inverted with !. If a compound command other than a subshell returns a |
| 877 | * non-zero status because a command failed while -e was being ignored, the |
| 878 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 879 | * exits [ERR is a bashism]. |
| 880 | * |
| 881 | * If a compound command or function executes in a context where -e is |
| 882 | * ignored, none of the commands executed within are affected by the -e |
| 883 | * setting. If a compound command or function sets -e while executing in a |
| 884 | * context where -e is ignored, that setting does not have any effect until |
| 885 | * the compound command or the command containing the function call completes. |
| 886 | */ |
| 887 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 888 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 889 | #if ENABLE_HUSH_MODE_X |
| 890 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 891 | #else |
| 892 | # define G_x_mode 0 |
| 893 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 894 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 895 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 896 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 897 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 898 | #if ENABLE_HUSH_FUNCTIONS |
| 899 | /* 0: outside of a function (or sourced file) |
| 900 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 901 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 902 | */ |
| 903 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 904 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 905 | #else |
| 906 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 907 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 908 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 909 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 910 | smalluint last_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; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 936 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 937 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 938 | # if ENABLE_HUSH_LOCAL |
| 939 | struct variable **shadowed_vars_pp; |
| 940 | unsigned func_nest_level; |
| 941 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 942 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 943 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 944 | #if ENABLE_HUSH_FAST |
| 945 | unsigned count_SIGCHLD; |
| 946 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 947 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 948 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 949 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 950 | unsigned lineno; |
| 951 | char *lineno_var; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 952 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 953 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 954 | /* Which signals have non-DFL handler (even with no traps set)? |
| 955 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 956 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 957 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 958 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 959 | * Other than these two times, never modified. |
| 960 | */ |
| 961 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 962 | #if ENABLE_HUSH_JOB |
| 963 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 964 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 965 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 966 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 967 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 968 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 969 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 970 | # define G_traps G.traps |
| 971 | #else |
| 972 | # define G_traps ((char**)NULL) |
| 973 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 974 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 975 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 976 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 977 | #endif |
| 978 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 979 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 980 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 981 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 982 | #if ENABLE_FEATURE_EDITING |
| 983 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 984 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 985 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 986 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 987 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 988 | * (too many defines). Also, I actually prefer to see when a variable |
| 989 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 990 | #define INIT_G() do { \ |
| 991 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 992 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 993 | sigfillset(&G.sa.sa_mask); \ |
| 994 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 995 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 996 | |
| 997 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 998 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 999 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1000 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1001 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1002 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1003 | static int builtin_eval(char **argv) FAST_FUNC; |
| 1004 | static int builtin_exec(char **argv) FAST_FUNC; |
| 1005 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1006 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1007 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1008 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1009 | #if ENABLE_HUSH_READONLY |
| 1010 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 1011 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1012 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1013 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 1014 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1015 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1016 | #if ENABLE_HUSH_GETOPTS |
| 1017 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 1018 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1019 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1020 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1021 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1022 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1023 | static int builtin_history(char **argv) FAST_FUNC; |
| 1024 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1025 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1026 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1027 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1028 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1029 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1030 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1031 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1032 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1033 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1034 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1035 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1036 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1037 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1038 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1039 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1040 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1041 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1042 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1043 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1044 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1045 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1046 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1047 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1048 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1049 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1050 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1051 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1052 | #if ENABLE_HUSH_TIMES |
| 1053 | static int builtin_times(char **argv) FAST_FUNC; |
| 1054 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1055 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1056 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1057 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1058 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1059 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1060 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1061 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1062 | #if ENABLE_HUSH_KILL |
| 1063 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1064 | #endif |
| 1065 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1066 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1067 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1068 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1069 | static int builtin_break(char **argv) FAST_FUNC; |
| 1070 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1071 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1072 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1073 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1074 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1075 | |
| 1076 | /* Table of built-in functions. They can be forked or not, depending on |
| 1077 | * context: within pipes, they fork. As simple commands, they do not. |
| 1078 | * When used in non-forking context, they can change global variables |
| 1079 | * in the parent shell process. If forked, of course they cannot. |
| 1080 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1081 | * still be set at the end. */ |
| 1082 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1083 | const char *b_cmd; |
| 1084 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1085 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1086 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1087 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1088 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1089 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1090 | #endif |
| 1091 | }; |
| 1092 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1093 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1094 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1095 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1096 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1097 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1098 | #endif |
| 1099 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1100 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1101 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1102 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1103 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1104 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1105 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1106 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1107 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1108 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1109 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1110 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1111 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1112 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1113 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1114 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1115 | #if ENABLE_HUSH_GETOPTS |
| 1116 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1117 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1118 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1119 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1120 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1121 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1122 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1123 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1124 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1125 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1126 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1127 | #if ENABLE_HUSH_KILL |
| 1128 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1129 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1130 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1131 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1132 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1133 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1134 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1135 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1136 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1137 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1138 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1139 | #if ENABLE_HUSH_READONLY |
| 1140 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1141 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1142 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1143 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1144 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1145 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1146 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1147 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1148 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1149 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1150 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1151 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1152 | #if ENABLE_HUSH_TIMES |
| 1153 | BLTIN("times" , builtin_times , NULL), |
| 1154 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1155 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1156 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1157 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1158 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1159 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1160 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1161 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1162 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1163 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1164 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1165 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1166 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1167 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1168 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1169 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1170 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1171 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1172 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1173 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1174 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1175 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1176 | * (it's cheaper to run an external program in this case): |
| 1177 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1178 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1179 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1180 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1181 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1182 | #if BASH_TEST2 |
| 1183 | BLTIN("[[" , builtin_test , NULL), |
| 1184 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1185 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1186 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1187 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1188 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1189 | BLTIN("printf" , builtin_printf , NULL), |
| 1190 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1191 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1192 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1193 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1194 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1195 | }; |
| 1196 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1197 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1198 | /* Debug printouts. |
| 1199 | */ |
| 1200 | #if HUSH_DEBUG |
| 1201 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1202 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1203 | # define debug_enter() (G.debug_indent++) |
| 1204 | # define debug_leave() (G.debug_indent--) |
| 1205 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1206 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1207 | # define debug_enter() ((void)0) |
| 1208 | # define debug_leave() ((void)0) |
| 1209 | #endif |
| 1210 | |
| 1211 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1212 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1213 | #endif |
| 1214 | |
| 1215 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1216 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1217 | #endif |
| 1218 | |
| 1219 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1220 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1221 | #endif |
| 1222 | |
| 1223 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1224 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | |
| 1227 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1228 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1229 | # define DEBUG_JOBS 1 |
| 1230 | #else |
| 1231 | # define DEBUG_JOBS 0 |
| 1232 | #endif |
| 1233 | |
| 1234 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1235 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1236 | # define DEBUG_EXPAND 1 |
| 1237 | #else |
| 1238 | # define DEBUG_EXPAND 0 |
| 1239 | #endif |
| 1240 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1241 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1242 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1243 | #endif |
| 1244 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1245 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1246 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1247 | # define DEBUG_GLOB 1 |
| 1248 | #else |
| 1249 | # define DEBUG_GLOB 0 |
| 1250 | #endif |
| 1251 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1252 | #ifndef debug_printf_redir |
| 1253 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1254 | #endif |
| 1255 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1256 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1257 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1258 | #endif |
| 1259 | |
| 1260 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1261 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1262 | #endif |
| 1263 | |
| 1264 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1265 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1266 | # define DEBUG_CLEAN 1 |
| 1267 | #else |
| 1268 | # define DEBUG_CLEAN 0 |
| 1269 | #endif |
| 1270 | |
| 1271 | #if DEBUG_EXPAND |
| 1272 | static void debug_print_strings(const char *prefix, char **vv) |
| 1273 | { |
| 1274 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1275 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1276 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1277 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1278 | } |
| 1279 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1280 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1281 | #endif |
| 1282 | |
| 1283 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1284 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1285 | */ |
| 1286 | #if LEAK_HUNTING |
| 1287 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1288 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1289 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1290 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1291 | return ptr; |
| 1292 | } |
| 1293 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1294 | { |
| 1295 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1296 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1297 | return ptr; |
| 1298 | } |
| 1299 | static char *xxstrdup(int lineno, const char *str) |
| 1300 | { |
| 1301 | char *ptr = xstrdup(str); |
| 1302 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1303 | return ptr; |
| 1304 | } |
| 1305 | static void xxfree(void *ptr) |
| 1306 | { |
| 1307 | fdprintf(2, "free %p\n", ptr); |
| 1308 | free(ptr); |
| 1309 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1310 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1311 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1312 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1313 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1314 | #endif |
| 1315 | |
| 1316 | |
| 1317 | /* Syntax and runtime errors. They always abort scripts. |
| 1318 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1319 | * and return to the prompt. |
| 1320 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1321 | */ |
| 1322 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1323 | # 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] | 1324 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1325 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1326 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1327 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1328 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1329 | #endif |
| 1330 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1331 | static void die_if_script(void) |
| 1332 | { |
| 1333 | if (!G_interactive_fd) { |
| 1334 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1335 | xfunc_error_retval = G.last_exitcode; |
| 1336 | xfunc_die(); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1341 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1342 | va_list p; |
| 1343 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1344 | #if HUSH_DEBUG >= 2 |
| 1345 | bb_error_msg("hush.c:%u", lineno); |
| 1346 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1347 | va_start(p, fmt); |
| 1348 | bb_verror_msg(fmt, p, NULL); |
| 1349 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1350 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1351 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1352 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1353 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1354 | { |
| 1355 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1356 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1357 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1358 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1359 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1362 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1363 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1364 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1365 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1368 | 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] | 1369 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1370 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1371 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1372 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1373 | } |
| 1374 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1375 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1376 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1377 | char msg[2] = { ch, '\0' }; |
| 1378 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1381 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1382 | { |
| 1383 | char msg[2]; |
| 1384 | msg[0] = ch; |
| 1385 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1386 | #if HUSH_DEBUG >= 2 |
| 1387 | bb_error_msg("hush.c:%u", lineno); |
| 1388 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1389 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1390 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1393 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1394 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1395 | # undef syntax_error |
| 1396 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1397 | # undef syntax_error_unterm_ch |
| 1398 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1399 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1400 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1401 | # 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] | 1402 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1403 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1404 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1405 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1406 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1407 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1408 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1409 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1410 | #if ENABLE_HUSH_INTERACTIVE |
| 1411 | static void cmdedit_update_prompt(void); |
| 1412 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1413 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1414 | #endif |
| 1415 | |
| 1416 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1417 | /* Utility functions |
| 1418 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1419 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1420 | static char *unbackslash(char *src) |
| 1421 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1422 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1423 | while (1) { |
| 1424 | if (*src == '\\') |
| 1425 | src++; |
| 1426 | if ((*dst++ = *src++) == '\0') |
| 1427 | break; |
| 1428 | } |
| 1429 | return dst; |
| 1430 | } |
| 1431 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1432 | 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] | 1433 | { |
| 1434 | int i; |
| 1435 | unsigned count1; |
| 1436 | unsigned count2; |
| 1437 | char **v; |
| 1438 | |
| 1439 | v = strings; |
| 1440 | count1 = 0; |
| 1441 | if (v) { |
| 1442 | while (*v) { |
| 1443 | count1++; |
| 1444 | v++; |
| 1445 | } |
| 1446 | } |
| 1447 | count2 = 0; |
| 1448 | v = add; |
| 1449 | while (*v) { |
| 1450 | count2++; |
| 1451 | v++; |
| 1452 | } |
| 1453 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1454 | v[count1 + count2] = NULL; |
| 1455 | i = count2; |
| 1456 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1457 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1458 | return v; |
| 1459 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1460 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1461 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1462 | { |
| 1463 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1464 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1465 | return ptr; |
| 1466 | } |
| 1467 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1468 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1469 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1470 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1471 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1472 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1473 | { |
| 1474 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1475 | v[0] = add; |
| 1476 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1477 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1478 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1479 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1480 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1481 | { |
| 1482 | char **ptr = add_string_to_strings(strings, add); |
| 1483 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1484 | return ptr; |
| 1485 | } |
| 1486 | #define add_string_to_strings(strings, add) \ |
| 1487 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1488 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1489 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1490 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1491 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1492 | char **v; |
| 1493 | |
| 1494 | if (!strings) |
| 1495 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1496 | v = strings; |
| 1497 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1498 | free(*v); |
| 1499 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1500 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1501 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1504 | static int dup_CLOEXEC(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1505 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1506 | int newfd; |
| 1507 | repeat: |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 1508 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
| 1509 | if (newfd >= 0) { |
| 1510 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1511 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
| 1512 | } else { /* newfd < 0 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1513 | if (errno == EBUSY) |
| 1514 | goto repeat; |
| 1515 | if (errno == EINTR) |
| 1516 | goto repeat; |
| 1517 | } |
| 1518 | return newfd; |
| 1519 | } |
| 1520 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1521 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1522 | { |
| 1523 | int newfd; |
| 1524 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1525 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1526 | if (newfd < 0) { |
| 1527 | if (errno == EBUSY) |
| 1528 | goto repeat; |
| 1529 | if (errno == EINTR) |
| 1530 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1531 | /* fd was not open? */ |
| 1532 | if (errno == EBADF) |
| 1533 | return fd; |
| 1534 | xfunc_die(); |
| 1535 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1536 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1537 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1538 | close(fd); |
| 1539 | return newfd; |
| 1540 | } |
| 1541 | |
| 1542 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1543 | /* Manipulating the list of open FILEs */ |
| 1544 | static FILE *remember_FILE(FILE *fp) |
| 1545 | { |
| 1546 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1547 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1548 | n->next = G.FILE_list; |
| 1549 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1550 | n->fp = fp; |
| 1551 | n->fd = fileno(fp); |
| 1552 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1553 | } |
| 1554 | return fp; |
| 1555 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1556 | static void fclose_and_forget(FILE *fp) |
| 1557 | { |
| 1558 | struct FILE_list **pp = &G.FILE_list; |
| 1559 | while (*pp) { |
| 1560 | struct FILE_list *cur = *pp; |
| 1561 | if (cur->fp == fp) { |
| 1562 | *pp = cur->next; |
| 1563 | free(cur); |
| 1564 | break; |
| 1565 | } |
| 1566 | pp = &cur->next; |
| 1567 | } |
| 1568 | fclose(fp); |
| 1569 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1570 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1571 | { |
| 1572 | struct FILE_list *fl = G.FILE_list; |
| 1573 | while (fl) { |
| 1574 | if (fd == fl->fd) { |
| 1575 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1576 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1577 | 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] | 1578 | return 1; |
| 1579 | } |
| 1580 | fl = fl->next; |
| 1581 | } |
| 1582 | return 0; |
| 1583 | } |
| 1584 | static void restore_redirected_FILEs(void) |
| 1585 | { |
| 1586 | struct FILE_list *fl = G.FILE_list; |
| 1587 | while (fl) { |
| 1588 | int should_be = fileno(fl->fp); |
| 1589 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1590 | 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] | 1591 | xmove_fd(fl->fd, should_be); |
| 1592 | fl->fd = should_be; |
| 1593 | } |
| 1594 | fl = fl->next; |
| 1595 | } |
| 1596 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1597 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1598 | static void close_all_FILE_list(void) |
| 1599 | { |
| 1600 | struct FILE_list *fl = G.FILE_list; |
| 1601 | while (fl) { |
| 1602 | /* fclose would also free FILE object. |
| 1603 | * It is disastrous if we share memory with a vforked parent. |
| 1604 | * I'm not sure we never come here after vfork. |
| 1605 | * Therefore just close fd, nothing more. |
| 1606 | */ |
| 1607 | /*fclose(fl->fp); - unsafe */ |
| 1608 | close(fl->fd); |
| 1609 | fl = fl->next; |
| 1610 | } |
| 1611 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1612 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1613 | static int fd_in_FILEs(int fd) |
| 1614 | { |
| 1615 | struct FILE_list *fl = G.FILE_list; |
| 1616 | while (fl) { |
| 1617 | if (fl->fd == fd) |
| 1618 | return 1; |
| 1619 | fl = fl->next; |
| 1620 | } |
| 1621 | return 0; |
| 1622 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1623 | |
| 1624 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1625 | /* Helpers for setting new $n and restoring them back |
| 1626 | */ |
| 1627 | typedef struct save_arg_t { |
| 1628 | char *sv_argv0; |
| 1629 | char **sv_g_argv; |
| 1630 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1631 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1632 | } save_arg_t; |
| 1633 | |
| 1634 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1635 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1636 | sv->sv_argv0 = argv[0]; |
| 1637 | sv->sv_g_argv = G.global_argv; |
| 1638 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1639 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1640 | |
| 1641 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1642 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1643 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1644 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1645 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1649 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1650 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1651 | if (G.global_args_malloced) { |
| 1652 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1653 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1654 | while (*++pp) /* note: does not free $0 */ |
| 1655 | free(*pp); |
| 1656 | free(G.global_argv); |
| 1657 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1658 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1659 | argv[0] = sv->sv_argv0; |
| 1660 | G.global_argv = sv->sv_g_argv; |
| 1661 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1662 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1666 | /* Basic theory of signal handling in shell |
| 1667 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1668 | * This does not describe what hush does, rather, it is current understanding |
| 1669 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1670 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1671 | * |
| 1672 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1673 | * is finished or backgrounded. It is the same in interactive and |
| 1674 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1675 | * 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] | 1676 | * ^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] | 1677 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1678 | * pipe. |
| 1679 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1680 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1681 | * or by SIGINT in interactive shell. |
| 1682 | * |
| 1683 | * Trap handlers will execute even within trap handlers. (right?) |
| 1684 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1685 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1686 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1687 | * |
| 1688 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1689 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1690 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1691 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1692 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1693 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1694 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1695 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1696 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1697 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1698 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1699 | * |
| 1700 | * SIGQUIT: ignore |
| 1701 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1702 | * SIGHUP (interactive): |
| 1703 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1704 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1705 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1706 | * that all pipe members are stopped. Try this in bash: |
| 1707 | * while :; do :; done - ^Z does not background it |
| 1708 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1709 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1710 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1711 | * to interactive shell while shell is waiting for a pipe, |
| 1712 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1713 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1714 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1715 | * Example 2: this does not wait and does not execute ls: |
| 1716 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1717 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1718 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1719 | * Example 4: this does not wait and does not execute ls: |
| 1720 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1721 | * |
| 1722 | * (What happens to signals which are IGN on shell start?) |
| 1723 | * (What happens with signal mask on shell start?) |
| 1724 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1725 | * Old implementation |
| 1726 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1727 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1728 | * We block all signals which we don't want to take action immediately, |
| 1729 | * i.e. we block all signals which need to have special handling as described |
| 1730 | * above, and all signals which have traps set. |
| 1731 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1732 | * and act on them. |
| 1733 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1734 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1735 | * sigset_t blocked_set: current blocked signal set |
| 1736 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1737 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1738 | * 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] | 1739 | * "trap 'cmd' SIGxxx": |
| 1740 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1741 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1742 | * unblock signals with special interactive handling |
| 1743 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1744 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1745 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1746 | * 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] | 1747 | * 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] | 1748 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1749 | * 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] | 1750 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1751 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1752 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1753 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1754 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1755 | * 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] | 1756 | * 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] | 1757 | * |
| 1758 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1759 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1760 | * masked signals are not visible! |
| 1761 | * |
| 1762 | * New implementation |
| 1763 | * ================== |
| 1764 | * We record each signal we are interested in by installing signal handler |
| 1765 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1766 | * We are interested in: signals which need to have special handling |
| 1767 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1768 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1769 | * After each pipe execution, we extract any pending signals |
| 1770 | * and act on them. |
| 1771 | * |
| 1772 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1773 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1774 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1775 | * sigset_t pending_set: set of sigs we received. |
| 1776 | * |
| 1777 | * "trap - SIGxxx": |
| 1778 | * if sig is in special_sig_mask, set handler back to: |
| 1779 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1780 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1781 | * else: set handler back to SIG_DFL |
| 1782 | * "trap 'cmd' SIGxxx": |
| 1783 | * set handler to record_pending_signo. |
| 1784 | * "trap '' SIGxxx": |
| 1785 | * set handler to SIG_IGN. |
| 1786 | * after [v]fork, if we plan to be a shell: |
| 1787 | * set signals with special interactive handling to SIG_DFL |
| 1788 | * (because child shell is not interactive), |
| 1789 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1790 | * after [v]fork, if we plan to exec: |
| 1791 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1792 | * |
| 1793 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1794 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1795 | * |
| 1796 | * Note (compat): |
| 1797 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1798 | * are set to the default actions". bash interprets it so that traps which |
| 1799 | * 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] | 1800 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1801 | enum { |
| 1802 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1803 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1804 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1805 | | (1 << SIGHUP) |
| 1806 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1807 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1808 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1809 | | (1 << SIGTTIN) |
| 1810 | | (1 << SIGTTOU) |
| 1811 | | (1 << SIGTSTP) |
| 1812 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1813 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1814 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1815 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1816 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1817 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1818 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1819 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1820 | if (sig == SIGCHLD) { |
| 1821 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1822 | //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] | 1823 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1824 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1825 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1826 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1827 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1828 | { |
| 1829 | struct sigaction old_sa; |
| 1830 | |
| 1831 | /* We could use signal() to install handlers... almost: |
| 1832 | * except that we need to mask ALL signals while handlers run. |
| 1833 | * I saw signal nesting in strace, race window isn't small. |
| 1834 | * SA_RESTART is also needed, but in Linux, signal() |
| 1835 | * sets SA_RESTART too. |
| 1836 | */ |
| 1837 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1838 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1839 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1840 | G.sa.sa_handler = handler; |
| 1841 | sigaction(sig, &G.sa, &old_sa); |
| 1842 | return old_sa.sa_handler; |
| 1843 | } |
| 1844 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1845 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1846 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1847 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1848 | static void restore_ttypgrp_and__exit(void) |
| 1849 | { |
| 1850 | /* xfunc has failed! die die die */ |
| 1851 | /* no EXIT traps, this is an escape hatch! */ |
| 1852 | G.exiting = 1; |
| 1853 | hush_exit(xfunc_error_retval); |
| 1854 | } |
| 1855 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1856 | #if ENABLE_HUSH_JOB |
| 1857 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1858 | /* Needed only on some libc: |
| 1859 | * It was observed that on exit(), fgetc'ed buffered data |
| 1860 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1861 | * With the net effect that even after fork(), not vfork(), |
| 1862 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1863 | * noexec_applet_here |
| 1864 | * echo END_OF_SCRIPT |
| 1865 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1866 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1867 | * 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] | 1868 | * and in `cmd` handling. |
| 1869 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1870 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1871 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1872 | static void fflush_and__exit(void) |
| 1873 | { |
| 1874 | fflush_all(); |
| 1875 | _exit(xfunc_error_retval); |
| 1876 | } |
| 1877 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1878 | /* 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] | 1879 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1880 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1881 | # 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] | 1882 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1883 | /* Restores tty foreground process group, and exits. |
| 1884 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1885 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1886 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1887 | * We also call it if xfunc is exiting. |
| 1888 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1889 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1890 | static void sigexit(int sig) |
| 1891 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1892 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1893 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1894 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1895 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1896 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1897 | */ |
| 1898 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1899 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1900 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1901 | |
| 1902 | /* Not a signal, just exit */ |
| 1903 | if (sig <= 0) |
| 1904 | _exit(- sig); |
| 1905 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1906 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1907 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1908 | #else |
| 1909 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1910 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1911 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1912 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1913 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1914 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1915 | static sighandler_t pick_sighandler(unsigned sig) |
| 1916 | { |
| 1917 | sighandler_t handler = SIG_DFL; |
| 1918 | if (sig < sizeof(unsigned)*8) { |
| 1919 | unsigned sigmask = (1 << sig); |
| 1920 | |
| 1921 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1922 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1923 | if (G_fatal_sig_mask & sigmask) |
| 1924 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1925 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1926 | #endif |
| 1927 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1928 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1929 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1930 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1931 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1932 | * in an endless loop when we try to do some |
| 1933 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1934 | */ |
| 1935 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1936 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1937 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1938 | } |
| 1939 | return handler; |
| 1940 | } |
| 1941 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1942 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1943 | static void hush_exit(int exitcode) |
| 1944 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1945 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1946 | save_history(G.line_input_state); |
| 1947 | #endif |
| 1948 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1949 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1950 | 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] | 1951 | char *argv[3]; |
| 1952 | /* argv[0] is unused */ |
Denys Vlasenko | 46f839c | 2018-01-19 16:58:44 +0100 | [diff] [blame] | 1953 | 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] | 1954 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1955 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1956 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1957 | * "trap" will still show it, if executed |
| 1958 | * in the handler */ |
| 1959 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1960 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1961 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1962 | #if ENABLE_FEATURE_CLEAN_UP |
| 1963 | { |
| 1964 | struct variable *cur_var; |
| 1965 | if (G.cwd != bb_msg_unknown) |
| 1966 | free((char*)G.cwd); |
| 1967 | cur_var = G.top_var; |
| 1968 | while (cur_var) { |
| 1969 | struct variable *tmp = cur_var; |
| 1970 | if (!cur_var->max_len) |
| 1971 | free(cur_var->varstr); |
| 1972 | cur_var = cur_var->next; |
| 1973 | free(tmp); |
| 1974 | } |
| 1975 | } |
| 1976 | #endif |
| 1977 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1978 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1979 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1980 | sigexit(- (exitcode & 0xff)); |
| 1981 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1982 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1983 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1986 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1987 | //TODO: return a mask of ALL handled sigs? |
| 1988 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1989 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1990 | int last_sig = 0; |
| 1991 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1992 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1993 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1994 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1995 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1996 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1997 | sig = 0; |
| 1998 | do { |
| 1999 | sig++; |
| 2000 | if (sigismember(&G.pending_set, sig)) { |
| 2001 | sigdelset(&G.pending_set, sig); |
| 2002 | goto got_sig; |
| 2003 | } |
| 2004 | } while (sig < NSIG); |
| 2005 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2006 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 2007 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2008 | 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] | 2009 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2010 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2011 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2012 | char *argv[3]; |
| 2013 | /* argv[0] is unused */ |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2014 | argv[1] = xstrdup(G_traps[sig]); |
| 2015 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2016 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2017 | save_rcode = G.last_exitcode; |
| 2018 | builtin_eval(argv); |
Denys Vlasenko | 749575d | 2018-01-30 04:29:03 +0100 | [diff] [blame] | 2019 | free(argv[1]); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2020 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2021 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2022 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2023 | } /* else: "" trap, ignoring signal */ |
| 2024 | continue; |
| 2025 | } |
| 2026 | /* not a trap: special action */ |
| 2027 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2028 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2029 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2030 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2031 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2032 | break; |
| 2033 | #if ENABLE_HUSH_JOB |
| 2034 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2035 | //TODO: why are we doing this? ash and dash don't do this, |
| 2036 | //they have no handler for SIGHUP at all, |
| 2037 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2038 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2039 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2040 | /* bash is observed to signal whole process groups, |
| 2041 | * not individual processes */ |
| 2042 | for (job = G.job_list; job; job = job->next) { |
| 2043 | if (job->pgrp <= 0) |
| 2044 | continue; |
| 2045 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2046 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2047 | kill(- job->pgrp, SIGCONT); |
| 2048 | } |
| 2049 | sigexit(SIGHUP); |
| 2050 | } |
| 2051 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2052 | #if ENABLE_HUSH_FAST |
| 2053 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2054 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2055 | G.count_SIGCHLD++; |
| 2056 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2057 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2058 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2059 | * This simplifies wait builtin a bit. |
| 2060 | */ |
| 2061 | break; |
| 2062 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2063 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2064 | 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] | 2065 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2066 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2067 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2068 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2069 | * in interactive shell, because TERM is ignored. |
| 2070 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | return last_sig; |
| 2075 | } |
| 2076 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2077 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2078 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2079 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2080 | if (force || G.cwd == NULL) { |
| 2081 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2082 | * we must not try to free(bb_msg_unknown) */ |
| 2083 | if (G.cwd == bb_msg_unknown) |
| 2084 | G.cwd = NULL; |
| 2085 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2086 | if (!G.cwd) |
| 2087 | G.cwd = bb_msg_unknown; |
| 2088 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2089 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2092 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2093 | /* |
| 2094 | * Shell and environment variable support |
| 2095 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2096 | 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] | 2097 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2098 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2099 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2100 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2101 | pp = &G.top_var; |
| 2102 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2103 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2104 | return pp; |
| 2105 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2106 | } |
| 2107 | return NULL; |
| 2108 | } |
| 2109 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2110 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2111 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2112 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2113 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2114 | |
| 2115 | if (G.expanded_assignments) { |
| 2116 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2117 | while (*cpp) { |
| 2118 | char *cp = *cpp; |
| 2119 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2120 | return cp + len + 1; |
| 2121 | cpp++; |
| 2122 | } |
| 2123 | } |
| 2124 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2125 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2126 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2127 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2128 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2129 | if (strcmp(name, "PPID") == 0) |
| 2130 | return utoa(G.root_ppid); |
| 2131 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2132 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2133 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2134 | return utoa(next_random(&G.random_gen)); |
| 2135 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2136 | return NULL; |
| 2137 | } |
| 2138 | |
| 2139 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2140 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2141 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2142 | #define SETFLAG_EXPORT (1 << 0) |
| 2143 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2144 | #define SETFLAG_MAKE_RO (1 << 2) |
| 2145 | #define SETFLAG_LOCAL_SHIFT 3 |
| 2146 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2147 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2148 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2149 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2150 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2151 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2152 | int name_len; |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2153 | IF_HUSH_LOCAL(unsigned local_lvl = (flags >> SETFLAG_LOCAL_SHIFT);) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2154 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2155 | eq_sign = strchr(str, '='); |
| 2156 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2157 | free(str); |
| 2158 | return -1; |
| 2159 | } |
| 2160 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2161 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2162 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2163 | if (G.lineno_var) { |
| 2164 | if (name_len == 7 && strncmp("LINENO", str, 6) == 0) |
| 2165 | G.lineno_var = NULL; |
| 2166 | } |
| 2167 | #endif |
| 2168 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2169 | var_pp = &G.top_var; |
| 2170 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2171 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2172 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2173 | continue; |
| 2174 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2175 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2176 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2177 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2178 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2179 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2180 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2181 | //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] | 2182 | return -1; |
| 2183 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2184 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2185 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2186 | *eq_sign = '\0'; |
| 2187 | unsetenv(str); |
| 2188 | *eq_sign = '='; |
| 2189 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2190 | #if ENABLE_HUSH_LOCAL |
| 2191 | if (cur->func_nest_level < local_lvl) { |
| 2192 | /* New variable is declared as local, |
| 2193 | * and existing one is global, or local |
| 2194 | * from enclosing function. |
| 2195 | * Remove and save old one: */ |
| 2196 | *var_pp = cur->next; |
| 2197 | cur->next = *G.shadowed_vars_pp; |
| 2198 | *G.shadowed_vars_pp = cur; |
| 2199 | /* bash 3.2.33(1) and exported vars: |
| 2200 | * # export z=z |
| 2201 | * # f() { local z=a; env | grep ^z; } |
| 2202 | * # f |
| 2203 | * z=a |
| 2204 | * # env | grep ^z |
| 2205 | * z=z |
| 2206 | */ |
| 2207 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2208 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2209 | break; |
| 2210 | } |
| 2211 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2212 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2213 | free_and_exp: |
| 2214 | free(str); |
| 2215 | goto exp; |
| 2216 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2217 | if (cur->max_len != 0) { |
| 2218 | if (cur->max_len >= strlen(str)) { |
| 2219 | /* This one is from startup env, reuse space */ |
| 2220 | strcpy(cur->varstr, str); |
| 2221 | goto free_and_exp; |
| 2222 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2223 | /* Can't reuse */ |
| 2224 | cur->max_len = 0; |
| 2225 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2226 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2227 | /* max_len == 0 signifies "malloced" var, which we can |
| 2228 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2229 | * if cur->flg_export is 1, it is in the environment. |
| 2230 | * We should either unsetenv+free, or wait until putenv, |
| 2231 | * then putenv(new)+free(old). |
| 2232 | */ |
| 2233 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2234 | goto set_str_and_exp; |
| 2235 | } |
| 2236 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2237 | /* Not found - create new variable struct */ |
| 2238 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2239 | IF_HUSH_LOCAL(cur->func_nest_level = local_lvl;) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2240 | cur->next = *var_pp; |
| 2241 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2242 | |
| 2243 | set_str_and_exp: |
| 2244 | cur->varstr = str; |
| 2245 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2246 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2247 | if (flags & SETFLAG_MAKE_RO) { |
| 2248 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2249 | } |
| 2250 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2251 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2252 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2253 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2254 | cmdedit_update_prompt(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 2255 | #if ENABLE_HUSH_GETOPTS |
Denys Vlasenko | 55af51c | 2017-08-29 13:48:49 +0200 | [diff] [blame] | 2256 | /* defoptindvar is a "OPTIND=..." constant string */ |
| 2257 | if (strncmp(cur->varstr, defoptindvar, 7) == 0) |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 2258 | G.getopt_count = 0; |
| 2259 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2260 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2261 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2262 | cur->flg_export = 0; |
| 2263 | /* unsetenv was already done */ |
| 2264 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2265 | int i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2266 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2267 | i = putenv(cur->varstr); |
| 2268 | /* only now we can free old exported malloced string */ |
| 2269 | free(free_me); |
| 2270 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2271 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2272 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2273 | free(free_me); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2274 | return 0; |
| 2275 | } |
| 2276 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2277 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2278 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2279 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2280 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2283 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2284 | { |
| 2285 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2286 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2287 | |
| 2288 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2289 | return EXIT_SUCCESS; |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2290 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 2291 | #if ENABLE_HUSH_GETOPTS |
| 2292 | if (name_len == 6 && strncmp(name, "OPTIND", 6) == 0) |
| 2293 | G.getopt_count = 0; |
| 2294 | #endif |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2295 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2296 | if (name_len == 6 && G.lineno_var && strncmp(name, "LINENO", 6) == 0) |
| 2297 | G.lineno_var = NULL; |
| 2298 | #endif |
| 2299 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2300 | var_pp = &G.top_var; |
| 2301 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2302 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 2303 | if (cur->flg_read_only) { |
| 2304 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2305 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2306 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2307 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2308 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2309 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2310 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2311 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2312 | if (!cur->max_len) |
| 2313 | free(cur->varstr); |
| 2314 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2315 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2316 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2317 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2318 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2319 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2322 | #if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2323 | static int unset_local_var(const char *name) |
| 2324 | { |
| 2325 | return unset_local_var_len(name, strlen(name)); |
| 2326 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2327 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2328 | |
| 2329 | static void unset_vars(char **strings) |
| 2330 | { |
| 2331 | char **v; |
| 2332 | |
| 2333 | if (!strings) |
| 2334 | return; |
| 2335 | v = strings; |
| 2336 | while (*v) { |
| 2337 | const char *eq = strchrnul(*v, '='); |
| 2338 | unset_local_var_len(*v, (int)(eq - *v)); |
| 2339 | v++; |
| 2340 | } |
| 2341 | free(strings); |
| 2342 | } |
| 2343 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 2344 | #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] | 2345 | 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] | 2346 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2347 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2348 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2349 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2350 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2351 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2352 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2353 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2354 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2355 | */ |
| 2356 | static void add_vars(struct variable *var) |
| 2357 | { |
| 2358 | struct variable *next; |
| 2359 | |
| 2360 | while (var) { |
| 2361 | next = var->next; |
| 2362 | var->next = G.top_var; |
| 2363 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2364 | if (var->flg_export) { |
| 2365 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2366 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2367 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2368 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2369 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2370 | var = next; |
| 2371 | } |
| 2372 | } |
| 2373 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2374 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2375 | { |
| 2376 | char **s; |
| 2377 | struct variable *old = NULL; |
| 2378 | |
| 2379 | if (!strings) |
| 2380 | return old; |
| 2381 | s = strings; |
| 2382 | while (*s) { |
| 2383 | struct variable *var_p; |
| 2384 | struct variable **var_pp; |
| 2385 | char *eq; |
| 2386 | |
| 2387 | eq = strchr(*s, '='); |
| 2388 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2389 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2390 | if (var_pp) { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2391 | var_p = *var_pp; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2392 | if (var_p->flg_read_only) { |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2393 | char **p; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2394 | bb_error_msg("%s: readonly variable", *s); |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2395 | /* |
| 2396 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2397 | * If VAR is readonly, leaving it in the list |
| 2398 | * after asssignment error (msg above) |
| 2399 | * causes doubled error message later, on unset. |
| 2400 | */ |
| 2401 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2402 | free(*s); |
| 2403 | p = s; |
| 2404 | do { *p = p[1]; p++; } while (*p); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2405 | goto next; |
| 2406 | } |
| 2407 | /* Remove variable from global linked list */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2408 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2409 | *var_pp = var_p->next; |
| 2410 | /* Add it to returned list */ |
| 2411 | var_p->next = old; |
| 2412 | old = var_p; |
| 2413 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2414 | set_local_var(*s, SETFLAG_EXPORT); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2415 | } |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2416 | next: |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2417 | s++; |
| 2418 | } |
| 2419 | return old; |
| 2420 | } |
| 2421 | |
| 2422 | |
| 2423 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2424 | * Unicode helper |
| 2425 | */ |
| 2426 | static void reinit_unicode_for_hush(void) |
| 2427 | { |
| 2428 | /* Unicode support should be activated even if LANG is set |
| 2429 | * _during_ shell execution, not only if it was set when |
| 2430 | * shell was started. Therefore, re-check LANG every time: |
| 2431 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2432 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2433 | || ENABLE_UNICODE_USING_LOCALE |
| 2434 | ) { |
| 2435 | const char *s = get_local_var_value("LC_ALL"); |
| 2436 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2437 | if (!s) s = get_local_var_value("LANG"); |
| 2438 | reinit_unicode(s); |
| 2439 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2440 | } |
| 2441 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2442 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2443 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2444 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2445 | |
| 2446 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2447 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2448 | * echo $P\ |
| 2449 | * \ |
| 2450 | * AT\ |
| 2451 | * H\ |
| 2452 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2453 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2454 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2455 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2456 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2457 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2458 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2459 | if (G.PS1 == NULL) |
| 2460 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2461 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2462 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2463 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2464 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2465 | if (G.PS2 == NULL) |
| 2466 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2467 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2468 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2469 | { |
| 2470 | const char *prompt_str; |
| 2471 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2472 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2473 | /* Set up the prompt */ |
| 2474 | if (promptmode == 0) { /* PS1 */ |
| 2475 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2476 | /* bash uses $PWD value, even if it is set by user. |
| 2477 | * It uses current dir only if PWD is unset. |
| 2478 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2479 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2480 | prompt_str = G.PS1; |
| 2481 | } else |
| 2482 | prompt_str = G.PS2; |
| 2483 | } else |
| 2484 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2485 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2486 | return prompt_str; |
| 2487 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2488 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2489 | { |
| 2490 | int r; |
| 2491 | const char *prompt_str; |
| 2492 | |
| 2493 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2494 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2495 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2496 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2497 | if (G.flag_SIGINT) { |
| 2498 | /* There was ^C'ed, make it look prettier: */ |
| 2499 | bb_putchar('\n'); |
| 2500 | G.flag_SIGINT = 0; |
| 2501 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2502 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2503 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2504 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2505 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2506 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2507 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2508 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2509 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2510 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2511 | if (r != 0 && !G.flag_SIGINT) |
| 2512 | break; |
| 2513 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2514 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2515 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2516 | G.last_exitcode = 128 + SIGINT; |
| 2517 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2518 | if (r < 0) { |
| 2519 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2520 | i->p = NULL; |
| 2521 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2522 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2523 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2524 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2525 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2526 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2527 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2528 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2529 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2530 | /* Why check_and_run_traps here? Try this interactively: |
| 2531 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2532 | * $ <[enter], repeatedly...> |
| 2533 | * Without check_and_run_traps, handler never runs. |
| 2534 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2535 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2536 | fputs(prompt_str, stdout); |
| 2537 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2538 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2539 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2540 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2541 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2542 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2543 | * it generates SIGINT as usual. |
| 2544 | */ |
| 2545 | check_and_run_traps(); |
| 2546 | if (G.flag_SIGINT) |
| 2547 | G.last_exitcode = 128 + SIGINT; |
| 2548 | if (r != '\0') |
| 2549 | break; |
| 2550 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2551 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2552 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2553 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2554 | /* This is the magic location that prints prompts |
| 2555 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2556 | static int fgetc_interactive(struct in_str *i) |
| 2557 | { |
| 2558 | int ch; |
| 2559 | /* If it's interactive stdin, get new line. */ |
| 2560 | if (G_interactive_fd && i->file == stdin) { |
| 2561 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2562 | ch = get_user_input(i); |
| 2563 | i->promptmode = 1; /* PS2 */ |
| 2564 | } else { |
| 2565 | /* Not stdin: script file, sourced file, etc */ |
| 2566 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2567 | } |
| 2568 | return ch; |
| 2569 | } |
| 2570 | #else |
| 2571 | static inline int fgetc_interactive(struct in_str *i) |
| 2572 | { |
| 2573 | int ch; |
| 2574 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2575 | return ch; |
| 2576 | } |
| 2577 | #endif /* INTERACTIVE */ |
| 2578 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2579 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2580 | { |
| 2581 | int ch; |
| 2582 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2583 | if (!i->file) { |
| 2584 | /* string-based in_str */ |
| 2585 | ch = (unsigned char)*i->p; |
| 2586 | if (ch != '\0') { |
| 2587 | i->p++; |
| 2588 | i->last_char = ch; |
| 2589 | return ch; |
| 2590 | } |
| 2591 | return EOF; |
| 2592 | } |
| 2593 | |
| 2594 | /* FILE-based in_str */ |
| 2595 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2596 | #if ENABLE_FEATURE_EDITING |
| 2597 | /* This can be stdin, check line editing char[] buffer */ |
| 2598 | if (i->p && *i->p != '\0') { |
| 2599 | ch = (unsigned char)*i->p++; |
| 2600 | goto out; |
| 2601 | } |
| 2602 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2603 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2604 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2605 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2606 | int ch2 = i->peek_buf[1]; |
| 2607 | i->peek_buf[0] = ch2; |
| 2608 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2609 | goto out; |
| 2610 | i->peek_buf[1] = 0; |
| 2611 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2612 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2613 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2614 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2615 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2616 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2617 | i->last_char = ch; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2618 | #if ENABLE_HUSH_LINENO_VAR |
| 2619 | if (ch == '\n') { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2620 | G.lineno++; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 2621 | debug_printf_parse("G.lineno++ = %u\n", G.lineno); |
| 2622 | } |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 2623 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2624 | return ch; |
| 2625 | } |
| 2626 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2627 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2628 | { |
| 2629 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2630 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2631 | if (!i->file) { |
| 2632 | /* string-based in_str */ |
| 2633 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2634 | return (unsigned char)*i->p; |
| 2635 | } |
| 2636 | |
| 2637 | /* FILE-based in_str */ |
| 2638 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2639 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2640 | /* This can be stdin, check line editing char[] buffer */ |
| 2641 | if (i->p && *i->p != '\0') |
| 2642 | return (unsigned char)*i->p; |
| 2643 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2644 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2645 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2646 | if (ch != 0) |
| 2647 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2648 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2649 | /* Need to get a new char */ |
| 2650 | ch = fgetc_interactive(i); |
| 2651 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2652 | |
| 2653 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2654 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2655 | if (i->p) { |
| 2656 | i->p -= 1; |
| 2657 | return ch; |
| 2658 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2659 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2660 | i->peek_buf[0] = ch; |
| 2661 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2662 | return ch; |
| 2663 | } |
| 2664 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2665 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2666 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2667 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2668 | */ |
| 2669 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2670 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2671 | int ch; |
| 2672 | |
| 2673 | /* There are two cases when i->p[] buffer exists. |
| 2674 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2675 | * (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] | 2676 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2677 | * the peek2 result is in i->p[1]. |
| 2678 | */ |
| 2679 | if (i->p) |
| 2680 | return (unsigned char)i->p[1]; |
| 2681 | |
| 2682 | /* Now we know it is a file-based in_str. */ |
| 2683 | |
| 2684 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2685 | /* Is there 2nd char? */ |
| 2686 | ch = i->peek_buf[1]; |
| 2687 | if (ch == 0) { |
| 2688 | /* We did not read it yet, get it now */ |
| 2689 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2690 | i->peek_buf[1] = ch; |
| 2691 | } |
| 2692 | |
| 2693 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2694 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 2697 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 2698 | { |
| 2699 | for (;;) { |
| 2700 | int ch, ch2; |
| 2701 | |
| 2702 | ch = i_getch(input); |
| 2703 | if (ch != '\\') |
| 2704 | return ch; |
| 2705 | ch2 = i_peek(input); |
| 2706 | if (ch2 != '\n') |
| 2707 | return ch; |
| 2708 | /* backslash+newline, skip it */ |
| 2709 | i_getch(input); |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | /* Note: this function _eats_ \<newline> pairs, safe to use plain |
| 2714 | * i_getch() after it instead of i_getch_and_eat_bkslash_nl(). |
| 2715 | */ |
| 2716 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 2717 | { |
| 2718 | for (;;) { |
| 2719 | int ch, ch2; |
| 2720 | |
| 2721 | ch = i_peek(input); |
| 2722 | if (ch != '\\') |
| 2723 | return ch; |
| 2724 | ch2 = i_peek2(input); |
| 2725 | if (ch2 != '\n') |
| 2726 | return ch; |
| 2727 | /* backslash+newline, skip it */ |
| 2728 | i_getch(input); |
| 2729 | i_getch(input); |
| 2730 | } |
| 2731 | } |
| 2732 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2733 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2734 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2735 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2736 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2737 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2738 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2742 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2743 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2744 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2745 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2746 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2750 | /* |
| 2751 | * o_string support |
| 2752 | */ |
| 2753 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2754 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2755 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2756 | { |
| 2757 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2758 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2759 | if (o->data) |
| 2760 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2763 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2764 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2765 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2766 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2769 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2770 | { |
| 2771 | free(o->data); |
| 2772 | } |
| 2773 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2774 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2775 | { |
| 2776 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2777 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2778 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2779 | } |
| 2780 | } |
| 2781 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2782 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2783 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2784 | 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] | 2785 | if (o->length < o->maxlen) { |
| 2786 | /* likely. avoid o_grow_by() call */ |
| 2787 | add: |
| 2788 | o->data[o->length] = ch; |
| 2789 | o->length++; |
| 2790 | o->data[o->length] = '\0'; |
| 2791 | return; |
| 2792 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2793 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2794 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2797 | #if 0 |
| 2798 | /* Valid only if we know o_string is not empty */ |
| 2799 | static void o_delchr(o_string *o) |
| 2800 | { |
| 2801 | o->length--; |
| 2802 | o->data[o->length] = '\0'; |
| 2803 | } |
| 2804 | #endif |
| 2805 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2806 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2807 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2808 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2809 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2810 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2813 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2814 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2815 | o_addblock(o, str, strlen(str)); |
| 2816 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2817 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2818 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2819 | static void nommu_addchr(o_string *o, int ch) |
| 2820 | { |
| 2821 | if (o) |
| 2822 | o_addchr(o, ch); |
| 2823 | } |
| 2824 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2825 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2826 | #endif |
| 2827 | |
| 2828 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2829 | { |
| 2830 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2833 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2834 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2835 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2836 | * Apparently, on unquoted $v bash still does globbing |
| 2837 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2838 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2839 | * quoting mechanisms on $v expansion side: one protects |
| 2840 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2841 | * We have only second one. |
| 2842 | */ |
| 2843 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2844 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2845 | # define MAYBE_BRACES "{}" |
| 2846 | #else |
| 2847 | # define MAYBE_BRACES "" |
| 2848 | #endif |
| 2849 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2850 | /* My analysis of quoting semantics tells me that state information |
| 2851 | * is associated with a destination, not a source. |
| 2852 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2853 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2854 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2855 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2856 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2857 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2858 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2859 | o_grow_by(o, sz); |
| 2860 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2861 | o->data[o->length] = '\\'; |
| 2862 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2863 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2864 | o->data[o->length] = ch; |
| 2865 | o->length++; |
| 2866 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2869 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2870 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2871 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2872 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2873 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2874 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2875 | sz++; |
| 2876 | o->data[o->length] = '\\'; |
| 2877 | o->length++; |
| 2878 | } |
| 2879 | o_grow_by(o, sz); |
| 2880 | o->data[o->length] = ch; |
| 2881 | o->length++; |
| 2882 | o->data[o->length] = '\0'; |
| 2883 | } |
| 2884 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2885 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2886 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2887 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2888 | char ch; |
| 2889 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2890 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2891 | if (ordinary_cnt > len) /* paranoia */ |
| 2892 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2893 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2894 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2895 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2896 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2897 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2898 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2899 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2900 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2901 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2902 | sz++; |
| 2903 | o->data[o->length] = '\\'; |
| 2904 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2905 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2906 | o_grow_by(o, sz); |
| 2907 | o->data[o->length] = ch; |
| 2908 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2909 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2910 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2913 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2914 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2915 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2916 | o_addblock(o, str, len); |
| 2917 | return; |
| 2918 | } |
| 2919 | o_addqblock(o, str, len); |
| 2920 | } |
| 2921 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2922 | static void o_addQstr(o_string *o, const char *str) |
| 2923 | { |
| 2924 | o_addQblock(o, str, strlen(str)); |
| 2925 | } |
| 2926 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2927 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2928 | * 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] | 2929 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2930 | * list[i] contains an INDEX (int!) into this string data. |
| 2931 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2932 | * but list[i]'s need not be modified. |
| 2933 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2934 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2935 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2936 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2937 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2938 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2939 | { |
| 2940 | char **list = (char**)o->data; |
| 2941 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2942 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2943 | |
| 2944 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2945 | 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] | 2946 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2947 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2948 | o->has_quoted_part, |
| 2949 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2950 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2951 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2952 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2953 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2954 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2955 | i++; |
| 2956 | } |
| 2957 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2958 | 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] | 2959 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2960 | 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] | 2961 | } |
| 2962 | } |
| 2963 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2964 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2965 | #endif |
| 2966 | |
| 2967 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2968 | * in list[n] so that it points past last stored byte so far. |
| 2969 | * It returns n+1. */ |
| 2970 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2971 | { |
| 2972 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2973 | int string_start; |
| 2974 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2975 | |
| 2976 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2977 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2978 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2979 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2980 | 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] | 2981 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2982 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2983 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2984 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2985 | memmove(list + n + 0x10, list + n, string_len); |
| 2986 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2987 | } else { |
| 2988 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2989 | n, string_len, string_start); |
| 2990 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2991 | } else { |
| 2992 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2993 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2994 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2995 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2996 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2997 | o->has_empty_slot = 0; |
| 2998 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2999 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3000 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3001 | return n + 1; |
| 3002 | } |
| 3003 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3004 | /* "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] | 3005 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3006 | { |
| 3007 | char **list = (char**)o->data; |
| 3008 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3009 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3010 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 3013 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3014 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 3015 | * first, it processes even {a} (no commas), second, |
| 3016 | * 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] | 3017 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3018 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3019 | |
| 3020 | /* Helper */ |
| 3021 | static int glob_needed(const char *s) |
| 3022 | { |
| 3023 | while (*s) { |
| 3024 | if (*s == '\\') { |
| 3025 | if (!s[1]) |
| 3026 | return 0; |
| 3027 | s += 2; |
| 3028 | continue; |
| 3029 | } |
| 3030 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 3031 | return 1; |
| 3032 | s++; |
| 3033 | } |
| 3034 | return 0; |
| 3035 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3036 | /* Return pointer to next closing brace or to comma */ |
| 3037 | static const char *next_brace_sub(const char *cp) |
| 3038 | { |
| 3039 | unsigned depth = 0; |
| 3040 | cp++; |
| 3041 | while (*cp != '\0') { |
| 3042 | if (*cp == '\\') { |
| 3043 | if (*++cp == '\0') |
| 3044 | break; |
| 3045 | cp++; |
| 3046 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 3047 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3048 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3049 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3050 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3051 | depth++; |
| 3052 | } |
| 3053 | |
| 3054 | return *cp != '\0' ? cp : NULL; |
| 3055 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3056 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 3057 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3058 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3059 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3060 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3061 | const char *next; |
| 3062 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3063 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3064 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3065 | |
| 3066 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 3067 | |
| 3068 | begin = pattern; |
| 3069 | while (1) { |
| 3070 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3071 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3072 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3073 | /* Find the first sub-pattern and at the same time |
| 3074 | * find the rest after the closing brace */ |
| 3075 | next = next_brace_sub(begin); |
| 3076 | if (next == NULL) { |
| 3077 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3078 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3079 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3080 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3081 | /* "{abc}" with no commas - illegal |
| 3082 | * brace expr, disregard and skip it */ |
| 3083 | begin = next + 1; |
| 3084 | continue; |
| 3085 | } |
| 3086 | break; |
| 3087 | } |
| 3088 | if (*begin == '\\' && begin[1] != '\0') |
| 3089 | begin++; |
| 3090 | begin++; |
| 3091 | } |
| 3092 | debug_printf_glob("begin:%s\n", begin); |
| 3093 | debug_printf_glob("next:%s\n", next); |
| 3094 | |
| 3095 | /* Now find the end of the whole brace expression */ |
| 3096 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3097 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3098 | rest = next_brace_sub(rest); |
| 3099 | if (rest == NULL) { |
| 3100 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3101 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3102 | } |
| 3103 | debug_printf_glob("rest:%s\n", rest); |
| 3104 | } |
| 3105 | rest_len = strlen(++rest) + 1; |
| 3106 | |
| 3107 | /* We are sure the brace expression is well-formed */ |
| 3108 | |
| 3109 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3110 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3111 | |
| 3112 | /* We have a brace expression. BEGIN points to the opening {, |
| 3113 | * NEXT points past the terminator of the first element, and REST |
| 3114 | * points past the final }. We will accumulate result names from |
| 3115 | * recursive runs for each brace alternative in the buffer using |
| 3116 | * GLOB_APPEND. */ |
| 3117 | |
| 3118 | p = begin + 1; |
| 3119 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3120 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3121 | memcpy( |
| 3122 | mempcpy( |
| 3123 | mempcpy(new_pattern_buf, |
| 3124 | /* We know the prefix for all sub-patterns */ |
| 3125 | pattern, begin - pattern), |
| 3126 | p, next - p), |
| 3127 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3128 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3129 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3130 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3131 | */ |
| 3132 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3133 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3134 | /* We saw the last entry */ |
| 3135 | break; |
| 3136 | } |
| 3137 | p = next + 1; |
| 3138 | next = next_brace_sub(next); |
| 3139 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3140 | free(new_pattern_buf); |
| 3141 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3142 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3143 | simple_glob: |
| 3144 | { |
| 3145 | int gr; |
| 3146 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3147 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3148 | memset(&globdata, 0, sizeof(globdata)); |
| 3149 | gr = glob(pattern, 0, NULL, &globdata); |
| 3150 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3151 | if (gr != 0) { |
| 3152 | if (gr == GLOB_NOMATCH) { |
| 3153 | globfree(&globdata); |
| 3154 | /* NB: garbles parameter */ |
| 3155 | unbackslash(pattern); |
| 3156 | o_addstr_with_NUL(o, pattern); |
| 3157 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3158 | return o_save_ptr_helper(o, n); |
| 3159 | } |
| 3160 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3161 | bb_die_memory_exhausted(); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3162 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3163 | * but we didn't specify it. Paranoia again. */ |
| 3164 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3165 | } |
| 3166 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3167 | char **argv = globdata.gl_pathv; |
| 3168 | while (1) { |
| 3169 | o_addstr_with_NUL(o, *argv); |
| 3170 | n = o_save_ptr_helper(o, n); |
| 3171 | argv++; |
| 3172 | if (!*argv) |
| 3173 | break; |
| 3174 | } |
| 3175 | } |
| 3176 | globfree(&globdata); |
| 3177 | } |
| 3178 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3179 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3180 | /* Performs globbing on last list[], |
| 3181 | * saving each result as a new list[]. |
| 3182 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3183 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3184 | { |
| 3185 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3186 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3187 | 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] | 3188 | if (!o->data) |
| 3189 | return o_save_ptr_helper(o, n); |
| 3190 | pattern = o->data + o_get_last_ptr(o, n); |
| 3191 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3192 | if (!glob_needed(pattern)) { |
| 3193 | /* unbackslash last string in o in place, fix length */ |
| 3194 | o->length = unbackslash(pattern) - o->data; |
| 3195 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3196 | return o_save_ptr_helper(o, n); |
| 3197 | } |
| 3198 | |
| 3199 | copy = xstrdup(pattern); |
| 3200 | /* "forget" pattern in o */ |
| 3201 | o->length = pattern - o->data; |
| 3202 | n = glob_brace(copy, o, n); |
| 3203 | free(copy); |
| 3204 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3205 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3206 | return n; |
| 3207 | } |
| 3208 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3209 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3210 | |
| 3211 | /* Helper */ |
| 3212 | static int glob_needed(const char *s) |
| 3213 | { |
| 3214 | while (*s) { |
| 3215 | if (*s == '\\') { |
| 3216 | if (!s[1]) |
| 3217 | return 0; |
| 3218 | s += 2; |
| 3219 | continue; |
| 3220 | } |
| 3221 | if (*s == '*' || *s == '[' || *s == '?') |
| 3222 | return 1; |
| 3223 | s++; |
| 3224 | } |
| 3225 | return 0; |
| 3226 | } |
| 3227 | /* Performs globbing on last list[], |
| 3228 | * saving each result as a new list[]. |
| 3229 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3230 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3231 | { |
| 3232 | glob_t globdata; |
| 3233 | int gr; |
| 3234 | char *pattern; |
| 3235 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3236 | 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] | 3237 | if (!o->data) |
| 3238 | return o_save_ptr_helper(o, n); |
| 3239 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3240 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3241 | if (!glob_needed(pattern)) { |
| 3242 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3243 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3244 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3245 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3246 | return o_save_ptr_helper(o, n); |
| 3247 | } |
| 3248 | |
| 3249 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3250 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3251 | * If we glob "*.\*" and don't find anything, we need |
| 3252 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3253 | * will return "*.\*"! |
| 3254 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3255 | gr = glob(pattern, 0, NULL, &globdata); |
| 3256 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3257 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3258 | if (gr == GLOB_NOMATCH) { |
| 3259 | globfree(&globdata); |
| 3260 | goto literal; |
| 3261 | } |
| 3262 | if (gr == GLOB_NOSPACE) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 3263 | bb_die_memory_exhausted(); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3264 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3265 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3266 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3267 | } |
| 3268 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3269 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3270 | /* "forget" pattern in o */ |
| 3271 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3272 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3273 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3274 | n = o_save_ptr_helper(o, n); |
| 3275 | argv++; |
| 3276 | if (!*argv) |
| 3277 | break; |
| 3278 | } |
| 3279 | } |
| 3280 | globfree(&globdata); |
| 3281 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3282 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3283 | return n; |
| 3284 | } |
| 3285 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3286 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3287 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3288 | /* 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] | 3289 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3290 | static int o_save_ptr(o_string *o, int n) |
| 3291 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3292 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3293 | /* If o->has_empty_slot, list[n] was already globbed |
| 3294 | * (if it was requested back then when it was filled) |
| 3295 | * so don't do that again! */ |
| 3296 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3297 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3298 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3299 | return o_save_ptr_helper(o, n); |
| 3300 | } |
| 3301 | |
| 3302 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3303 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3304 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3305 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3306 | int string_start; |
| 3307 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3308 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3309 | if (DEBUG_EXPAND) |
| 3310 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3311 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3312 | list = (char**)o->data; |
| 3313 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3314 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3315 | while (n) { |
| 3316 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3317 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3318 | } |
| 3319 | return list; |
| 3320 | } |
| 3321 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3322 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3323 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3324 | /* Returns pi->next - next pipe in the list */ |
| 3325 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3326 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3327 | struct pipe *next; |
| 3328 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3329 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3330 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3331 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3332 | struct command *command; |
| 3333 | struct redir_struct *r, *rnext; |
| 3334 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3335 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3336 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3337 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3338 | if (DEBUG_CLEAN) { |
| 3339 | int a; |
| 3340 | char **p; |
| 3341 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3342 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3343 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3344 | } |
| 3345 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3346 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3347 | } |
| 3348 | /* not "else if": on syntax error, we may have both! */ |
| 3349 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3350 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3351 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3352 | free_pipe_list(command->group); |
| 3353 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3354 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3355 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3356 | /* else is crucial here. |
| 3357 | * If group != NULL, child_func is meaningless */ |
| 3358 | #if ENABLE_HUSH_FUNCTIONS |
| 3359 | else if (command->child_func) { |
| 3360 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3361 | command->child_func->parent_cmd = NULL; |
| 3362 | } |
| 3363 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3364 | #if !BB_MMU |
| 3365 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3366 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3367 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3368 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3369 | debug_printf_clean(" redirect %d%s", |
| 3370 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3371 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3372 | if (r->rd_filename) { |
| 3373 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3374 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3375 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3376 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3377 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3378 | rnext = r->next; |
| 3379 | free(r); |
| 3380 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3381 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3382 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3383 | 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] | 3384 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3385 | #if ENABLE_HUSH_JOB |
| 3386 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3387 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3388 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3389 | |
| 3390 | next = pi->next; |
| 3391 | free(pi); |
| 3392 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3393 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3394 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3395 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3396 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3397 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3398 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3399 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3400 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3401 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3402 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3403 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3407 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3408 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3409 | #ifndef debug_print_tree |
| 3410 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3411 | { |
| 3412 | static const char *const PIPE[] = { |
| 3413 | [PIPE_SEQ] = "SEQ", |
| 3414 | [PIPE_AND] = "AND", |
| 3415 | [PIPE_OR ] = "OR" , |
| 3416 | [PIPE_BG ] = "BG" , |
| 3417 | }; |
| 3418 | static const char *RES[] = { |
| 3419 | [RES_NONE ] = "NONE" , |
| 3420 | # if ENABLE_HUSH_IF |
| 3421 | [RES_IF ] = "IF" , |
| 3422 | [RES_THEN ] = "THEN" , |
| 3423 | [RES_ELIF ] = "ELIF" , |
| 3424 | [RES_ELSE ] = "ELSE" , |
| 3425 | [RES_FI ] = "FI" , |
| 3426 | # endif |
| 3427 | # if ENABLE_HUSH_LOOPS |
| 3428 | [RES_FOR ] = "FOR" , |
| 3429 | [RES_WHILE] = "WHILE", |
| 3430 | [RES_UNTIL] = "UNTIL", |
| 3431 | [RES_DO ] = "DO" , |
| 3432 | [RES_DONE ] = "DONE" , |
| 3433 | # endif |
| 3434 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3435 | [RES_IN ] = "IN" , |
| 3436 | # endif |
| 3437 | # if ENABLE_HUSH_CASE |
| 3438 | [RES_CASE ] = "CASE" , |
| 3439 | [RES_CASE_IN ] = "CASE_IN" , |
| 3440 | [RES_MATCH] = "MATCH", |
| 3441 | [RES_CASE_BODY] = "CASE_BODY", |
| 3442 | [RES_ESAC ] = "ESAC" , |
| 3443 | # endif |
| 3444 | [RES_XXXX ] = "XXXX" , |
| 3445 | [RES_SNTX ] = "SNTX" , |
| 3446 | }; |
| 3447 | static const char *const CMDTYPE[] = { |
| 3448 | "{}", |
| 3449 | "()", |
| 3450 | "[noglob]", |
| 3451 | # if ENABLE_HUSH_FUNCTIONS |
| 3452 | "func()", |
| 3453 | # endif |
| 3454 | }; |
| 3455 | |
| 3456 | int pin, prn; |
| 3457 | |
| 3458 | pin = 0; |
| 3459 | while (pi) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3460 | fdprintf(2, "%*spipe %d %sres_word=%s followup=%d %s\n", |
| 3461 | lvl*2, "", |
| 3462 | pin, |
| 3463 | (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""), |
| 3464 | RES[pi->res_word], |
| 3465 | pi->followup, PIPE[pi->followup] |
| 3466 | ); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3467 | prn = 0; |
| 3468 | while (prn < pi->num_cmds) { |
| 3469 | struct command *command = &pi->cmds[prn]; |
| 3470 | char **argv = command->argv; |
| 3471 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3472 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3473 | lvl*2, "", prn, |
| 3474 | command->assignment_cnt); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3475 | #if ENABLE_HUSH_LINENO_VAR |
| 3476 | fdprintf(2, " LINENO:%u", command->lineno); |
| 3477 | #endif |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3478 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3479 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3480 | CMDTYPE[command->cmd_type], |
| 3481 | argv |
| 3482 | # if !BB_MMU |
| 3483 | , " group_as_string:", command->group_as_string |
| 3484 | # else |
| 3485 | , "", "" |
| 3486 | # endif |
| 3487 | ); |
| 3488 | debug_print_tree(command->group, lvl+1); |
| 3489 | prn++; |
| 3490 | continue; |
| 3491 | } |
| 3492 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3493 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3494 | argv++; |
| 3495 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3496 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3497 | prn++; |
| 3498 | } |
| 3499 | pi = pi->next; |
| 3500 | pin++; |
| 3501 | } |
| 3502 | } |
| 3503 | #endif /* debug_print_tree */ |
| 3504 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3505 | static struct pipe *new_pipe(void) |
| 3506 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3507 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3508 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3509 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3510 | return pi; |
| 3511 | } |
| 3512 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3513 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3514 | * if ctx->command is NULL. |
| 3515 | * No errors possible here. |
| 3516 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3517 | static int done_command(struct parse_context *ctx) |
| 3518 | { |
| 3519 | /* The command is really already in the pipe structure, so |
| 3520 | * advance the pipe counter and make a new, null command. */ |
| 3521 | struct pipe *pi = ctx->pipe; |
| 3522 | struct command *command = ctx->command; |
| 3523 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3524 | #if 0 /* Instead we emit error message at run time */ |
| 3525 | if (ctx->pending_redirect) { |
| 3526 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3527 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3528 | ctx->pending_redirect = NULL; |
| 3529 | } |
| 3530 | #endif |
| 3531 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3532 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3533 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3534 | 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] | 3535 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3536 | } |
| 3537 | pi->num_cmds++; |
| 3538 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3539 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3540 | } else { |
| 3541 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3542 | } |
| 3543 | |
| 3544 | /* Only real trickiness here is that the uncommitted |
| 3545 | * command structure is not counted in pi->num_cmds. */ |
| 3546 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3547 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3548 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3549 | memset(command, 0, sizeof(*command)); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3550 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3551 | command->lineno = G.lineno; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3552 | debug_printf_parse("command->lineno = G.lineno (%u)\n", G.lineno); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 3553 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3554 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3555 | } |
| 3556 | |
| 3557 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3558 | { |
| 3559 | int not_null; |
| 3560 | |
| 3561 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3562 | /* Close previous command */ |
| 3563 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3564 | #if HAS_KEYWORDS |
| 3565 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3566 | ctx->ctx_inverted = 0; |
| 3567 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3568 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3569 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3570 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3571 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3572 | * in a backgrounded subshell. |
| 3573 | */ |
| 3574 | struct pipe *pi; |
| 3575 | struct command *command; |
| 3576 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3577 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3578 | pi = ctx->list_head; |
| 3579 | while (pi != ctx->pipe) { |
| 3580 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3581 | goto no_conv; |
| 3582 | pi = pi->next; |
| 3583 | } |
| 3584 | |
| 3585 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3586 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3587 | pi = xzalloc(sizeof(*pi)); |
| 3588 | pi->followup = PIPE_BG; |
| 3589 | pi->num_cmds = 1; |
| 3590 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3591 | command = &pi->cmds[0]; |
| 3592 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3593 | command->cmd_type = CMD_NORMAL; |
| 3594 | command->group = ctx->list_head; |
| 3595 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3596 | command->group_as_string = xstrndup( |
| 3597 | ctx->as_string.data, |
| 3598 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3599 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3600 | #endif |
| 3601 | /* Replace all pipes in ctx with one newly created */ |
| 3602 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3603 | } else { |
| 3604 | no_conv: |
| 3605 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3606 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3607 | |
| 3608 | /* Without this check, even just <enter> on command line generates |
| 3609 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3610 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3611 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3612 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3613 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3614 | #endif |
| 3615 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3616 | || ctx->ctx_res_w == RES_DONE |
| 3617 | || ctx->ctx_res_w == RES_FOR |
| 3618 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3619 | #endif |
| 3620 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3621 | || ctx->ctx_res_w == RES_ESAC |
| 3622 | #endif |
| 3623 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3624 | struct pipe *new_p; |
| 3625 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3626 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3627 | not_null, ctx->ctx_res_w); |
| 3628 | new_p = new_pipe(); |
| 3629 | ctx->pipe->next = new_p; |
| 3630 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3631 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3632 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3633 | * This is used to control execution. |
| 3634 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3635 | * cases where variable or value happens to match a keyword): |
| 3636 | */ |
| 3637 | #if ENABLE_HUSH_LOOPS |
| 3638 | if (ctx->ctx_res_w == RES_FOR |
| 3639 | || ctx->ctx_res_w == RES_IN) |
| 3640 | ctx->ctx_res_w = RES_NONE; |
| 3641 | #endif |
| 3642 | #if ENABLE_HUSH_CASE |
| 3643 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3644 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3645 | if (ctx->ctx_res_w == RES_CASE) |
| 3646 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3647 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3648 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3649 | /* Create the memory for command, roughly: |
| 3650 | * ctx->pipe->cmds = new struct command; |
| 3651 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3652 | */ |
| 3653 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3654 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3655 | } |
| 3656 | debug_printf_parse("done_pipe return\n"); |
| 3657 | } |
| 3658 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3659 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3660 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3661 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3662 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3663 | /* Create the memory for command, roughly: |
| 3664 | * ctx->pipe->cmds = new struct command; |
| 3665 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3666 | */ |
| 3667 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3670 | /* If a reserved word is found and processed, parse context is modified |
| 3671 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3672 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3673 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3674 | struct reserved_combo { |
| 3675 | char literal[6]; |
| 3676 | unsigned char res; |
| 3677 | unsigned char assignment_flag; |
| 3678 | int flag; |
| 3679 | }; |
| 3680 | enum { |
| 3681 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3682 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3683 | FLAG_IF = (1 << RES_IF ), |
| 3684 | FLAG_THEN = (1 << RES_THEN ), |
| 3685 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3686 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3687 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3688 | # endif |
| 3689 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3690 | FLAG_FOR = (1 << RES_FOR ), |
| 3691 | FLAG_WHILE = (1 << RES_WHILE), |
| 3692 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3693 | FLAG_DO = (1 << RES_DO ), |
| 3694 | FLAG_DONE = (1 << RES_DONE ), |
| 3695 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3696 | # endif |
| 3697 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3698 | FLAG_MATCH = (1 << RES_MATCH), |
| 3699 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3700 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3701 | FLAG_START = (1 << RES_XXXX ), |
| 3702 | }; |
| 3703 | |
| 3704 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3705 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | /* Mostly a list of accepted follow-up reserved words. |
| 3707 | * FLAG_END means we are done with the sequence, and are ready |
| 3708 | * to turn the compound list into a command. |
| 3709 | * FLAG_START means the word must start a new compound list. |
| 3710 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3711 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3712 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3713 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3714 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3715 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3716 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3717 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3718 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3719 | # endif |
| 3720 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3721 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3722 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3723 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3724 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3725 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3726 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3727 | # endif |
| 3728 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3729 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3730 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3731 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3732 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3733 | const struct reserved_combo *r; |
| 3734 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3735 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3736 | if (strcmp(word->data, r->literal) == 0) |
| 3737 | return r; |
| 3738 | } |
| 3739 | return NULL; |
| 3740 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3741 | /* Return NULL: not a keyword, else: keyword |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3742 | */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3743 | static const struct reserved_combo* reserved_word(o_string *word, struct parse_context *ctx) |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3744 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3745 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3746 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3747 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3748 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3749 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3750 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3751 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3752 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3753 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3754 | r = match_reserved_word(word); |
| 3755 | if (!r) |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3756 | return r; /* NULL */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3757 | |
| 3758 | 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] | 3759 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3760 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3761 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3762 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3763 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3764 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3765 | if (r->flag == 0) { /* '!' */ |
| 3766 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3767 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3768 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3769 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3770 | ctx->ctx_inverted = 1; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3771 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3772 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3773 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3774 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3775 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3776 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3777 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3778 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3779 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3780 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3781 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3782 | ctx->ctx_res_w = RES_SNTX; |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3783 | return r; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3784 | } else { |
| 3785 | /* "{...} fi" is ok. "{...} if" is not |
| 3786 | * Example: |
| 3787 | * if { echo foo; } then { echo bar; } fi */ |
| 3788 | if (ctx->command->group) |
| 3789 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3790 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3791 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3792 | ctx->ctx_res_w = r->res; |
| 3793 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3794 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3795 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3796 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3797 | if (ctx->old_flag & FLAG_END) { |
| 3798 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3799 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3800 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3801 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3802 | old = ctx->stack; |
| 3803 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3804 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3805 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3806 | /* At this point, the compound command's string is in |
| 3807 | * ctx->as_string... except for the leading keyword! |
| 3808 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3809 | * ctx->as_string will contain "true; then echo a; fi", |
| 3810 | * with "if " remaining in old->as_string! |
| 3811 | */ |
| 3812 | { |
| 3813 | char *str; |
| 3814 | int len = old->as_string.length; |
| 3815 | /* Concatenate halves */ |
| 3816 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3817 | o_free_unsafe(&ctx->as_string); |
| 3818 | /* Find where leading keyword starts in first half */ |
| 3819 | str = old->as_string.data + len; |
| 3820 | if (str > old->as_string.data) |
| 3821 | str--; /* skip whitespace after keyword */ |
| 3822 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3823 | str--; |
| 3824 | /* Ugh, we're done with this horrid hack */ |
| 3825 | old->command->group_as_string = xstrdup(str); |
| 3826 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3827 | old->command->group_as_string); |
| 3828 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3829 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3830 | *ctx = *old; /* physical copy */ |
| 3831 | free(old); |
| 3832 | } |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3833 | return r; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3834 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3835 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3836 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3837 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3838 | * Normal return is 0. Syntax errors return 1. |
| 3839 | * Note: on return, word is reset, but not o_free'd! |
| 3840 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3841 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3842 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3843 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3844 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3845 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3846 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3847 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3848 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3849 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3850 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3851 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3852 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3853 | * only if run as "bash", not "sh" */ |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame^] | 3854 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3855 | * "2.7 Redirection |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame^] | 3856 | * If the redirection operator is "<<" or "<<-", the word |
| 3857 | * that follows the redirection operator shall be |
| 3858 | * subjected to quote removal; it is unspecified whether |
| 3859 | * any of the other expansions occur. For the other |
| 3860 | * redirection operators, the word that follows the |
| 3861 | * redirection operator shall be subjected to tilde |
| 3862 | * expansion, parameter expansion, command substitution, |
| 3863 | * arithmetic expansion, and quote removal. |
| 3864 | * Pathname expansion shall not be performed |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3865 | * on the word by a non-interactive shell; an interactive |
| 3866 | * shell may perform it, but shall do so only when |
| 3867 | * the expansion would result in one word." |
| 3868 | */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 3869 | //bash does not do parameter/command substitution or arithmetic expansion |
| 3870 | //for _heredoc_ redirection word: these constructs look for exact eof marker |
| 3871 | // as written: |
| 3872 | // <<EOF$t |
| 3873 | // <<EOF$((1)) |
Denys Vlasenko | e84212f | 2018-04-01 20:11:23 +0200 | [diff] [blame^] | 3874 | // <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug] |
| 3875 | |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3876 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3877 | /* Cater for >\file case: |
| 3878 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3879 | * Same with heredocs: |
| 3880 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3881 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3882 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3883 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3884 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3885 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3886 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3887 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3888 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3889 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3890 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3891 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3892 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3893 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3894 | if (ctx->ctx_dsemicolon |
| 3895 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3896 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3897 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3898 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3899 | ctx->ctx_dsemicolon = 0; |
| 3900 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3901 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3902 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3903 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3904 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3905 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3906 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3907 | # if ENABLE_HUSH_CASE |
| 3908 | && ctx->ctx_res_w != RES_CASE |
| 3909 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3910 | ) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3911 | const struct reserved_combo *reserved; |
| 3912 | reserved = reserved_word(word, ctx); |
| 3913 | debug_printf_parse("checking for reserved-ness: %d\n", !!reserved); |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3914 | if (reserved) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 3915 | # if ENABLE_HUSH_LINENO_VAR |
| 3916 | /* Case: |
| 3917 | * "while ...; do |
| 3918 | * cmd ..." |
| 3919 | * If we don't close the pipe _now_, immediately after "do", lineno logic |
| 3920 | * sees "cmd" as starting at "do" - i.e., at the previous line. |
| 3921 | */ |
| 3922 | if (0 |
| 3923 | IF_HUSH_IF(|| reserved->res == RES_THEN) |
| 3924 | IF_HUSH_IF(|| reserved->res == RES_ELIF) |
| 3925 | IF_HUSH_IF(|| reserved->res == RES_ELSE) |
| 3926 | IF_HUSH_LOOPS(|| reserved->res == RES_DO) |
| 3927 | ) { |
| 3928 | done_pipe(ctx, PIPE_SEQ); |
| 3929 | } |
| 3930 | # endif |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3931 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3932 | debug_printf_parse("done_word return %d\n", |
| 3933 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3934 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3935 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 3936 | # if BASH_TEST2 |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3937 | if (strcmp(word->data, "[[") == 0) { |
| 3938 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3939 | } |
| 3940 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3941 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3942 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3943 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3944 | if (command->group) { |
| 3945 | /* "{ echo foo; } echo bar" - bad */ |
| 3946 | syntax_error_at(word->data); |
| 3947 | debug_printf_parse("done_word return 1: syntax error, " |
| 3948 | "groups and arglists don't mix\n"); |
| 3949 | return 1; |
| 3950 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3951 | |
| 3952 | /* If this word wasn't an assignment, next ones definitely |
| 3953 | * can't be assignments. Even if they look like ones. */ |
| 3954 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3955 | && word->o_assignment != WORD_IS_KEYWORD |
| 3956 | ) { |
| 3957 | word->o_assignment = NOT_ASSIGNMENT; |
| 3958 | } else { |
| 3959 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3960 | command->assignment_cnt++; |
| 3961 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3962 | } |
| 3963 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3964 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3965 | } |
| 3966 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3967 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3968 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3969 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3970 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3971 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3972 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3973 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3974 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3975 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3976 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3977 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3978 | return 1; |
| 3979 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3980 | /* Force FOR to have just one word (variable name) */ |
| 3981 | /* NB: basically, this makes hush see "for v in ..." |
| 3982 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3983 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3984 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3985 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3986 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3987 | #if ENABLE_HUSH_CASE |
| 3988 | /* Force CASE to have just one word */ |
| 3989 | if (ctx->ctx_res_w == RES_CASE) { |
| 3990 | done_pipe(ctx, PIPE_SEQ); |
| 3991 | } |
| 3992 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3993 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3994 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3995 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3996 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3997 | return 0; |
| 3998 | } |
| 3999 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4000 | |
| 4001 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 4002 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4003 | * Return: |
| 4004 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 4005 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 4006 | * REDIRFD_TO_FILE if no & was seen, |
| 4007 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4008 | */ |
| 4009 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4010 | #define parse_redir_right_fd(as_string, input) \ |
| 4011 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4012 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4013 | 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] | 4014 | { |
| 4015 | int ch, d, ok; |
| 4016 | |
| 4017 | ch = i_peek(input); |
| 4018 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4019 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4020 | |
| 4021 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4022 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4023 | ch = i_peek(input); |
| 4024 | if (ch == '-') { |
| 4025 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4026 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4027 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4028 | } |
| 4029 | d = 0; |
| 4030 | ok = 0; |
| 4031 | while (ch != EOF && isdigit(ch)) { |
| 4032 | d = d*10 + (ch-'0'); |
| 4033 | ok = 1; |
| 4034 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4035 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4036 | ch = i_peek(input); |
| 4037 | } |
| 4038 | if (ok) return d; |
| 4039 | |
| 4040 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 4041 | |
| 4042 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4043 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4046 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4047 | */ |
| 4048 | static int parse_redirect(struct parse_context *ctx, |
| 4049 | int fd, |
| 4050 | redir_type style, |
| 4051 | struct in_str *input) |
| 4052 | { |
| 4053 | struct command *command = ctx->command; |
| 4054 | struct redir_struct *redir; |
| 4055 | struct redir_struct **redirp; |
| 4056 | int dup_num; |
| 4057 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4058 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4059 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4060 | /* Check for a '>&1' type redirect */ |
| 4061 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 4062 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 4063 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4064 | } else { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4065 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4066 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4067 | if (dup_num) { /* <<-... */ |
| 4068 | ch = i_getch(input); |
| 4069 | nommu_addchr(&ctx->as_string, ch); |
| 4070 | ch = i_peek(input); |
| 4071 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4072 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4073 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4074 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 4075 | int ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4076 | if (ch == '|') { |
| 4077 | /* >|FILE redirect ("clobbering" >). |
| 4078 | * Since we do not support "set -o noclobber" yet, |
| 4079 | * >| and > are the same for now. Just eat |. |
| 4080 | */ |
| 4081 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4082 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | /* Create a new redir_struct and append it to the linked list */ |
| 4087 | redirp = &command->redirects; |
| 4088 | while ((redir = *redirp) != NULL) { |
| 4089 | redirp = &(redir->next); |
| 4090 | } |
| 4091 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 4092 | /* redir->next = NULL; */ |
| 4093 | /* redir->rd_filename = NULL; */ |
| 4094 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4095 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4096 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4097 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 4098 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4099 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4100 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4101 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4102 | /* Erik had a check here that the file descriptor in question |
| 4103 | * is legit; I postpone that to "run time" |
| 4104 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 4105 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 4106 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4107 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4108 | #if 0 /* Instead we emit error message at run time */ |
| 4109 | if (ctx->pending_redirect) { |
| 4110 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 4111 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 4112 | } |
| 4113 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4114 | /* Set ctx->pending_redirect, so we know what to do at the |
| 4115 | * end of the next parsed word. */ |
| 4116 | ctx->pending_redirect = redir; |
| 4117 | } |
| 4118 | return 0; |
| 4119 | } |
| 4120 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4121 | /* If a redirect is immediately preceded by a number, that number is |
| 4122 | * supposed to tell which file descriptor to redirect. This routine |
| 4123 | * looks for such preceding numbers. In an ideal world this routine |
| 4124 | * needs to handle all the following classes of redirects... |
| 4125 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4126 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4127 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4128 | * 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] | 4129 | * |
| 4130 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4131 | * "2.7 Redirection |
| 4132 | * ... If n is quoted, the number shall not be recognized as part of |
| 4133 | * the redirection expression. For example: |
| 4134 | * echo \2>a |
| 4135 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4136 | * 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] | 4137 | * |
| 4138 | * A -1 return means no valid number was found, |
| 4139 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4140 | */ |
| 4141 | static int redirect_opt_num(o_string *o) |
| 4142 | { |
| 4143 | int num; |
| 4144 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4145 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4146 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4147 | num = bb_strtou(o->data, NULL, 10); |
| 4148 | if (errno || num < 0) |
| 4149 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4150 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4151 | return num; |
| 4152 | } |
| 4153 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4154 | #if BB_MMU |
| 4155 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4156 | fetch_till_str(input, word, skip_tabs) |
| 4157 | #endif |
| 4158 | static char *fetch_till_str(o_string *as_string, |
| 4159 | struct in_str *input, |
| 4160 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4161 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4162 | { |
| 4163 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4164 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4165 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4166 | int ch; |
| 4167 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4168 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4169 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4170 | while (1) { |
| 4171 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4172 | if (ch != EOF) |
| 4173 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4174 | if (ch == '\n' || ch == EOF) { |
| 4175 | check_heredoc_end: |
| 4176 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4177 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4178 | heredoc.data[past_EOL] = '\0'; |
| 4179 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4180 | return heredoc.data; |
| 4181 | } |
| 4182 | if (ch == '\n') { |
| 4183 | /* This is a new line. |
| 4184 | * Remember position and backslash-escaping status. |
| 4185 | */ |
| 4186 | o_addchr(&heredoc, ch); |
| 4187 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4188 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4189 | past_EOL = heredoc.length; |
| 4190 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4191 | do { |
| 4192 | ch = i_getch(input); |
| 4193 | if (ch != EOF) |
| 4194 | nommu_addchr(as_string, ch); |
| 4195 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4196 | /* If this immediately ended the line, |
| 4197 | * go back to end-of-line checks. |
| 4198 | */ |
| 4199 | if (ch == '\n') |
| 4200 | goto check_heredoc_end; |
| 4201 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4202 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4203 | } |
| 4204 | if (ch == EOF) { |
| 4205 | o_free_unsafe(&heredoc); |
| 4206 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4207 | } |
| 4208 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4209 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4210 | if (prev == '\\' && ch == '\\') |
| 4211 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4212 | prev = 0; /* not \ */ |
| 4213 | else |
| 4214 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4215 | } |
| 4216 | } |
| 4217 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4218 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4219 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4220 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4221 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4222 | { |
| 4223 | struct pipe *pi = ctx->list_head; |
| 4224 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4225 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4226 | int i; |
| 4227 | struct command *cmd = pi->cmds; |
| 4228 | |
| 4229 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4230 | pi->num_cmds, |
| 4231 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4232 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4233 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4234 | |
| 4235 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4236 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4237 | while (redir) { |
| 4238 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4239 | char *p; |
| 4240 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4241 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4242 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denys Vlasenko | bb6f573 | 2018-04-01 18:55:00 +0200 | [diff] [blame] | 4243 | bb_error_msg("redir->rd_filename:'%s'", redir->rd_filename); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4244 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4245 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4246 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4247 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4248 | return 1; |
| 4249 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4250 | free(redir->rd_filename); |
| 4251 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4252 | heredoc_cnt--; |
| 4253 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4254 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4255 | } |
| 4256 | cmd++; |
| 4257 | } |
| 4258 | pi = pi->next; |
| 4259 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4260 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4261 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4262 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4263 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4264 | #endif |
| 4265 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4269 | static int run_list(struct pipe *pi); |
| 4270 | #if BB_MMU |
| 4271 | #define parse_stream(pstring, input, end_trigger) \ |
| 4272 | parse_stream(input, end_trigger) |
| 4273 | #endif |
| 4274 | static struct pipe *parse_stream(char **pstring, |
| 4275 | struct in_str *input, |
| 4276 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4277 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4278 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 4279 | #if !ENABLE_HUSH_FUNCTIONS |
| 4280 | #define parse_group(dest, ctx, input, ch) \ |
| 4281 | parse_group(ctx, input, ch) |
| 4282 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4283 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4284 | struct in_str *input, int ch) |
| 4285 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4286 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4287 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4288 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4289 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4290 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4291 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4292 | |
| 4293 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4294 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4295 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4296 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4297 | if (done_word(dest, ctx)) |
| 4298 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4299 | if (!command->argv) |
| 4300 | goto skip; /* (... */ |
| 4301 | if (command->argv[1]) { /* word word ... (... */ |
| 4302 | syntax_error_unexpected_ch('('); |
| 4303 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4304 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4305 | /* it is "word(..." or "word (..." */ |
| 4306 | do |
| 4307 | ch = i_getch(input); |
| 4308 | while (ch == ' ' || ch == '\t'); |
| 4309 | if (ch != ')') { |
| 4310 | syntax_error_unexpected_ch(ch); |
| 4311 | return 1; |
| 4312 | } |
| 4313 | nommu_addchr(&ctx->as_string, ch); |
| 4314 | do |
| 4315 | ch = i_getch(input); |
| 4316 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 4317 | if (ch != '{') { |
| 4318 | syntax_error_unexpected_ch(ch); |
| 4319 | return 1; |
| 4320 | } |
| 4321 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4322 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4323 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4324 | } |
| 4325 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4326 | |
| 4327 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4328 | if (command->argv /* word [word]{... */ |
| 4329 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4330 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4331 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4332 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4333 | debug_printf_parse("parse_group return 1: " |
| 4334 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4335 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4336 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4337 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4338 | |
| 4339 | #if ENABLE_HUSH_FUNCTIONS |
| 4340 | skip: |
| 4341 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4342 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4343 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4344 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4345 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4346 | } else { |
| 4347 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4348 | ch = i_peek(input); |
| 4349 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4350 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4351 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4352 | syntax_error_unexpected_ch(ch); |
| 4353 | return 1; |
| 4354 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4355 | if (ch != '(') { |
| 4356 | ch = i_getch(input); |
| 4357 | nommu_addchr(&ctx->as_string, ch); |
| 4358 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4359 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4360 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4361 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4362 | #if BB_MMU |
| 4363 | # define as_string NULL |
| 4364 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4365 | char *as_string = NULL; |
| 4366 | #endif |
| 4367 | pipe_list = parse_stream(&as_string, input, endch); |
| 4368 | #if !BB_MMU |
| 4369 | if (as_string) |
| 4370 | o_addstr(&ctx->as_string, as_string); |
| 4371 | #endif |
| 4372 | /* empty ()/{} or parse error? */ |
| 4373 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4374 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4375 | if (!BB_MMU) |
| 4376 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4377 | debug_printf_parse("parse_group return 1: " |
| 4378 | "parse_stream returned %p\n", pipe_list); |
| 4379 | return 1; |
| 4380 | } |
| 4381 | command->group = pipe_list; |
| 4382 | #if !BB_MMU |
| 4383 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4384 | command->group_as_string = as_string; |
| 4385 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4386 | command->group_as_string); |
| 4387 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4388 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4389 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4390 | debug_printf_parse("parse_group return 0\n"); |
| 4391 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4392 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4395 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4396 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4397 | 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] | 4398 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4399 | 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] | 4400 | { |
| 4401 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4402 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4403 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4404 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4405 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4406 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4407 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4408 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4409 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4413 | 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] | 4414 | { |
| 4415 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4416 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4417 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4418 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4419 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4420 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4421 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4422 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4423 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4424 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4425 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4426 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4427 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4428 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4429 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4430 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4431 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4432 | continue; |
| 4433 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4434 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4435 | } |
| 4436 | } |
| 4437 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4438 | * \` quoting. |
| 4439 | * "Within the backquoted style of command substitution, backslash |
| 4440 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4441 | * The search for the matching backquote shall be satisfied by the first |
| 4442 | * backquote found without a preceding backslash; during this search, |
| 4443 | * if a non-escaped backquote is encountered within a shell comment, |
| 4444 | * a here-document, an embedded command substitution of the $(command) |
| 4445 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4446 | * or double-quoted string that begins, but does not end, within the |
| 4447 | * "`...`" sequence produces undefined results." |
| 4448 | * Example Output |
| 4449 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4450 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4451 | 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] | 4452 | { |
| 4453 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4454 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4455 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4456 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4457 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4458 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4459 | ch = i_getch(input); |
| 4460 | if (ch != '`' |
| 4461 | && ch != '$' |
| 4462 | && ch != '\\' |
| 4463 | && (!in_dquote || ch != '"') |
| 4464 | ) { |
| 4465 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4466 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4467 | } |
| 4468 | if (ch == EOF) { |
| 4469 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4470 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4471 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4472 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4476 | * quoting and nested ()s. |
| 4477 | * "With the $(command) style of command substitution, all characters |
| 4478 | * following the open parenthesis to the matching closing parenthesis |
| 4479 | * constitute the command. Any valid shell script can be used for command, |
| 4480 | * except a script consisting solely of redirections which produces |
| 4481 | * unspecified results." |
| 4482 | * Example Output |
| 4483 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4484 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4485 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4486 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4487 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4488 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4489 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4490 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4491 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4492 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4493 | 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] | 4494 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4495 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4496 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4497 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4498 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4499 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4500 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4501 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4502 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4503 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4504 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4505 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4506 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4507 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4508 | if (ch == end_ch |
| 4509 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 4510 | || ch == end_char2 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4511 | # endif |
| 4512 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4513 | if (!dbl) |
| 4514 | break; |
| 4515 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4516 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4517 | i_getch(input); /* eat second ')' */ |
| 4518 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4519 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4520 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4521 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4522 | //bb_error_msg("%s:o_addchr('%c')", __func__, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4523 | if (ch == '(' || ch == '{') { |
| 4524 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4525 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4526 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4527 | o_addchr(dest, ch); |
| 4528 | continue; |
| 4529 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4530 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4531 | if (!add_till_single_quote(dest, input)) |
| 4532 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4533 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4534 | continue; |
| 4535 | } |
| 4536 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4537 | if (!add_till_double_quote(dest, input)) |
| 4538 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4539 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4540 | continue; |
| 4541 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4542 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4543 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4544 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4545 | o_addchr(dest, ch); |
| 4546 | continue; |
| 4547 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4548 | if (ch == '\\') { |
| 4549 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4550 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4551 | if (ch == EOF) { |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4552 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4553 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4554 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4555 | #if 0 |
| 4556 | if (ch == '\n') { |
| 4557 | /* "backslash+newline", ignore both */ |
| 4558 | o_delchr(dest); /* undo insertion of '\' */ |
| 4559 | continue; |
| 4560 | } |
| 4561 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4562 | o_addchr(dest, ch); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 4563 | //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4564 | continue; |
| 4565 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4566 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4567 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4568 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4569 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4570 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4571 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4572 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4573 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4574 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4575 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4576 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4577 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4578 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4579 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4580 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4581 | 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] | 4582 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4583 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4584 | if (isalpha(ch)) { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4585 | make_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4586 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4587 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4588 | /*make_var1:*/ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4589 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4590 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4591 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4592 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4593 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4594 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4595 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4596 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4597 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4598 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4599 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4600 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4601 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4602 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4603 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4604 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4605 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4606 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4607 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4608 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4609 | o_addchr(dest, ch | quote_mask); |
| 4610 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4611 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4612 | case '$': /* pid */ |
| 4613 | case '!': /* last bg pid */ |
| 4614 | case '?': /* last exit code */ |
| 4615 | case '#': /* number of args */ |
| 4616 | case '*': /* args */ |
| 4617 | case '@': /* args */ |
| 4618 | goto make_one_char_var; |
| 4619 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4620 | char len_single_ch; |
| 4621 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4622 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4623 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4624 | ch = i_getch(input); /* eat '{' */ |
| 4625 | nommu_addchr(as_string, ch); |
| 4626 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4627 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4628 | /* It should be ${?}, or ${#var}, |
| 4629 | * or even ${?+subst} - operator acting on a special variable, |
| 4630 | * or the beginning of variable name. |
| 4631 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4632 | if (ch == EOF |
| 4633 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4634 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4635 | bad_dollar_syntax: |
| 4636 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4637 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4638 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4639 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4640 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4641 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4642 | ch |= quote_mask; |
| 4643 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4644 | /* 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] | 4645 | * However, this regresses some of our testsuite cases |
| 4646 | * which check invalid constructs like ${%}. |
| 4647 | * Oh well... let's check that the var name part is fine... */ |
| 4648 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4649 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4650 | unsigned pos; |
| 4651 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4652 | o_addchr(dest, ch); |
| 4653 | debug_printf_parse(": '%c'\n", ch); |
| 4654 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4655 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4656 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4657 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4658 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4659 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4660 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4661 | unsigned end_ch; |
| 4662 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4663 | /* handle parameter expansions |
| 4664 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4665 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4666 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4667 | if (len_single_ch != '#' |
| 4668 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4669 | || i_peek(input) != '}' |
| 4670 | ) { |
| 4671 | goto bad_dollar_syntax; |
| 4672 | } |
| 4673 | /* else: it's "length of C" ${#C} op, |
| 4674 | * where C is a single char |
| 4675 | * special var name, e.g. ${#!}. |
| 4676 | */ |
| 4677 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4678 | /* Eat everything until closing '}' (or ':') */ |
| 4679 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4680 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4681 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4682 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4683 | ) { |
| 4684 | /* It's ${var:N[:M]} thing */ |
| 4685 | end_ch = '}' * 0x100 + ':'; |
| 4686 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4687 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4688 | && ch == '/' |
| 4689 | ) { |
| 4690 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4691 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4692 | i_getch(input); |
| 4693 | nommu_addchr(as_string, '/'); |
| 4694 | ch = '\\'; |
| 4695 | } |
| 4696 | end_ch = '}' * 0x100 + '/'; |
| 4697 | } |
| 4698 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4699 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4700 | if (!BB_MMU) |
| 4701 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4702 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4703 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4704 | if (last_ch == 0) /* error? */ |
| 4705 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4706 | #else |
| 4707 | #error Simple code to only allow ${var} is not implemented |
| 4708 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4709 | if (as_string) { |
| 4710 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4711 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4712 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4713 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4714 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4715 | && (end_ch & 0xff00) |
| 4716 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4717 | /* close the first block: */ |
| 4718 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4719 | /* while parsing N from ${var:N[:M]} |
| 4720 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4721 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4722 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4723 | end_ch = '}'; |
| 4724 | goto again; |
| 4725 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4726 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4727 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4728 | /* it's ${var:N} - emulate :999999999 */ |
| 4729 | o_addstr(dest, "999999999"); |
| 4730 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4731 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4732 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4733 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4734 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4735 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4736 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4737 | break; |
| 4738 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4739 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4740 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4741 | unsigned pos; |
| 4742 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4743 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4744 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4745 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4746 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4747 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4748 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4749 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4750 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4751 | if (!BB_MMU) |
| 4752 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4753 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4754 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4755 | if (as_string) { |
| 4756 | o_addstr(as_string, dest->data + pos); |
| 4757 | o_addchr(as_string, ')'); |
| 4758 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4759 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4760 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4761 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4762 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4763 | # endif |
| 4764 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4765 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4766 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4767 | if (!BB_MMU) |
| 4768 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4769 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4770 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4771 | if (as_string) { |
| 4772 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4773 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4774 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4775 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4776 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4777 | break; |
| 4778 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4779 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4780 | case '_': |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4781 | goto make_var; |
| 4782 | #if 0 |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4783 | /* TODO: $_ and $-: */ |
| 4784 | /* $_ Shell or shell script name; or last argument of last command |
| 4785 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4786 | * 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] | 4787 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 4788 | ch = i_getch(input); |
| 4789 | nommu_addchr(as_string, ch); |
| 4790 | ch = i_peek_and_eat_bkslash_nl(input); |
| 4791 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4792 | ch = '_'; |
| 4793 | goto make_var1; |
| 4794 | } |
| 4795 | /* else: it's $_ */ |
| 4796 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4797 | default: |
| 4798 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4799 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4800 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4801 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4802 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4805 | #if BB_MMU |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4806 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4807 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4808 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4809 | # else |
| 4810 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4811 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4812 | encode_string(dest, input, dquote_end) |
| 4813 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4814 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4815 | |
| 4816 | #else /* !MMU */ |
| 4817 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4818 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4819 | /* all parameters are needed, no macro tricks */ |
| 4820 | # else |
| 4821 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4822 | encode_string(as_string, dest, input, dquote_end) |
| 4823 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4824 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4825 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4826 | o_string *dest, |
| 4827 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4828 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4829 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4830 | { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4831 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4832 | const int process_bkslash = 1; |
| 4833 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4834 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4835 | int next; |
| 4836 | |
| 4837 | again: |
| 4838 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4839 | if (ch != EOF) |
| 4840 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4841 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4842 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4843 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4844 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4845 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4846 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4847 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4848 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4849 | } |
| 4850 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4851 | if (ch != '\n') { |
| 4852 | next = i_peek(input); |
| 4853 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4854 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4855 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4856 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4857 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4858 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4859 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4860 | } |
| 4861 | /* bash: |
| 4862 | * "The backslash retains its special meaning [in "..."] |
| 4863 | * only when followed by one of the following characters: |
| 4864 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4865 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4866 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4867 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4868 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4869 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4870 | ch = i_getch(input); /* eat next */ |
| 4871 | if (ch == '\n') |
| 4872 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4873 | } /* else: ch remains == '\\', and we double it below: */ |
| 4874 | 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] | 4875 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4876 | goto again; |
| 4877 | } |
| 4878 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4879 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4880 | debug_printf_parse("encode_string return 0: " |
| 4881 | "parse_dollar returned 0 (error)\n"); |
| 4882 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4883 | } |
| 4884 | goto again; |
| 4885 | } |
| 4886 | #if ENABLE_HUSH_TICK |
| 4887 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4888 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4889 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4890 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4891 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4892 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4893 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4894 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4895 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4896 | } |
| 4897 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4898 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4899 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4900 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4903 | /* |
| 4904 | * Scan input until EOF or end_trigger char. |
| 4905 | * Return a list of pipes to execute, or NULL on EOF |
| 4906 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4907 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4908 | * reset parsing machinery and start parsing anew, |
| 4909 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4910 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4911 | static struct pipe *parse_stream(char **pstring, |
| 4912 | struct in_str *input, |
| 4913 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4914 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4915 | struct parse_context ctx; |
| 4916 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4917 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4918 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4919 | /* Single-quote triggers a bypass of the main loop until its mate is |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4920 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4921 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4922 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4923 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4924 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4925 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4926 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4927 | * Preventing this: */ |
| 4928 | o_addchr(&dest, '\0'); |
| 4929 | dest.length = 0; |
| 4930 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4931 | /* We used to separate words on $IFS here. This was wrong. |
| 4932 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4933 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4934 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4935 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4936 | if (MAYBE_ASSIGNMENT != 0) |
| 4937 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4938 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4939 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4940 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4941 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4942 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4943 | int ch; |
| 4944 | int next; |
| 4945 | int redir_fd; |
| 4946 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4947 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4948 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4949 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4950 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4951 | if (ch == EOF) { |
| 4952 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4953 | |
| 4954 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4955 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4956 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4957 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4958 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4959 | syntax_error_unterm_ch('('); |
| 4960 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4961 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 4962 | if (end_trigger == '}') { |
| 4963 | syntax_error_unterm_ch('{'); |
| 4964 | goto parse_error; |
| 4965 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4966 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4967 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4968 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4969 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4970 | o_free(&dest); |
| 4971 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4972 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4973 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4974 | /* (this makes bare "&" cmd a no-op. |
| 4975 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4976 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4977 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4978 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4979 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4980 | pi = NULL; |
| 4981 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4982 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4983 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4984 | if (pstring) |
| 4985 | *pstring = ctx.as_string.data; |
| 4986 | else |
| 4987 | o_free_unsafe(&ctx.as_string); |
| 4988 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4989 | debug_leave(); |
| 4990 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4991 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4992 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4993 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4994 | |
| 4995 | next = '\0'; |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 4996 | if (ch != '\n') { |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4997 | next = i_peek(input); |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 4998 | /* Can't use i_peek_and_eat_bkslash_nl(input) here: |
| 4999 | * echo '\ |
| 5000 | * ' |
| 5001 | * will break. |
| 5002 | */ |
| 5003 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5004 | |
| 5005 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5006 | "\\$\"" IF_HUSH_TICK("`") /* always special */ |
| 5007 | SPECIAL_VAR_SYMBOL_STR; |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5008 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5009 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 5010 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5011 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5012 | || (next != ';' /* }; - special */ |
| 5013 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5014 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5015 | && next != '&' /* }& and }&& ... - special */ |
| 5016 | && next != '|' /* }|| ... - special */ |
| 5017 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 5018 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5019 | ) { |
| 5020 | /* They are not special, skip "{}" */ |
| 5021 | is_special += 2; |
| 5022 | } |
| 5023 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5024 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 5025 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5026 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 5027 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5028 | o_addQchr(&dest, ch); |
| 5029 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 5030 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5031 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5032 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5033 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5034 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5035 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5036 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5037 | continue; |
| 5038 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5039 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5040 | if (is_blank) { |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5041 | #if ENABLE_HUSH_LINENO_VAR |
| 5042 | /* Case: |
| 5043 | * "while ...; do<whitespace><newline> |
| 5044 | * cmd ..." |
| 5045 | * would think that "cmd" starts in <whitespace> - |
| 5046 | * i.e., at the previous line. |
| 5047 | * We need to skip all whitespace before newlines. |
| 5048 | */ |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5049 | while (ch != '\n') { |
| 5050 | next = i_peek(input); |
| 5051 | if (next != ' ' && next != '\t' && next != '\n') |
| 5052 | break; /* next char is not ws */ |
| 5053 | ch = i_getch(input); |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5054 | } |
Denys Vlasenko | f786901 | 2018-02-08 19:39:42 +0100 | [diff] [blame] | 5055 | /* ch == last eaten whitespace char */ |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 5056 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5057 | if (done_word(&dest, &ctx)) { |
| 5058 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 5059 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5060 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5061 | /* Is this a case when newline is simply ignored? |
| 5062 | * Some examples: |
| 5063 | * "cmd | <newline> cmd ..." |
| 5064 | * "case ... in <newline> word) ..." |
| 5065 | */ |
| 5066 | if (IS_NULL_CMD(ctx.command) |
| 5067 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5068 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5069 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5070 | * Without check #1, interactive shell |
| 5071 | * ignores even bare <newline>, |
| 5072 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5073 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5074 | * ps2> _ <=== wrong, should be ps1 |
| 5075 | * Without check #2, "cmd & <newline>" |
| 5076 | * is similarly mistreated. |
| 5077 | * (BTW, this makes "cmd & cmd" |
| 5078 | * and "cmd && cmd" non-orthogonal. |
| 5079 | * Really, ask yourself, why |
| 5080 | * "cmd && <newline>" doesn't start |
| 5081 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 5082 | * The only reason is that it might be |
| 5083 | * a "cmd1 && <nl> cmd2 &" construct, |
| 5084 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5085 | */ |
| 5086 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5087 | if (pi->num_cmds != 0 /* check #1 */ |
| 5088 | && pi->followup != PIPE_BG /* check #2 */ |
| 5089 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 5090 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 5091 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5092 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5093 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5094 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5095 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 5096 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5097 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5098 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5099 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5100 | heredoc_cnt = 0; |
| 5101 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5102 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5103 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5104 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5105 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5106 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5107 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5108 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5109 | |
| 5110 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 5111 | * } is an ordinary char in this case, even inside { cmd; } |
| 5112 | * Pathological example: { ""}; } should exec "}" cmd |
| 5113 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5114 | if (ch == '}') { |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5115 | if (dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5116 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5117 | ) { |
| 5118 | goto ordinary_char; |
| 5119 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5120 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5121 | /* Generally, there should be semicolon: "cmd; }" |
| 5122 | * However, bash allows to omit it if "cmd" is |
| 5123 | * a group. Examples: |
| 5124 | * { { echo 1; } } |
| 5125 | * {(echo 1)} |
| 5126 | * { echo 0 >&2 | { echo 1; } } |
| 5127 | * { while false; do :; done } |
| 5128 | * { case a in b) ;; esac } |
| 5129 | */ |
| 5130 | if (ctx.command->group) |
| 5131 | goto term_group; |
| 5132 | goto ordinary_char; |
| 5133 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5134 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5135 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5136 | goto skip_end_trigger; |
| 5137 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5138 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5139 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5140 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5141 | && (ch != ';' || heredoc_cnt == 0) |
| 5142 | #if ENABLE_HUSH_CASE |
| 5143 | && (ch != ')' |
| 5144 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5145 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5146 | ) |
| 5147 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5148 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5149 | if (heredoc_cnt) { |
| 5150 | /* This is technically valid: |
| 5151 | * { cat <<HERE; }; echo Ok |
| 5152 | * heredoc |
| 5153 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5154 | * HERE |
| 5155 | * but we don't support this. |
| 5156 | * We require heredoc to be in enclosing {}/(), |
| 5157 | * if any. |
| 5158 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5159 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5160 | goto parse_error; |
| 5161 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5162 | if (done_word(&dest, &ctx)) { |
| 5163 | goto parse_error; |
| 5164 | } |
| 5165 | done_pipe(&ctx, PIPE_SEQ); |
| 5166 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5167 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5168 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5169 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5170 | 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] | 5171 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5172 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5173 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5174 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5175 | if (pstring) |
| 5176 | *pstring = ctx.as_string.data; |
| 5177 | else |
| 5178 | o_free_unsafe(&ctx.as_string); |
| 5179 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5180 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5181 | /* Example: bare "{ }", "()" */ |
| 5182 | G.last_exitcode = 2; /* bash compat */ |
| 5183 | syntax_error_unexpected_ch(ch); |
| 5184 | goto parse_error2; |
| 5185 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5186 | debug_printf_parse("parse_stream return %p: " |
| 5187 | "end_trigger char found\n", |
| 5188 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5189 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5190 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5191 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5192 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5193 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5194 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5195 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5196 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5197 | /* Catch <, > before deciding whether this word is |
| 5198 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5199 | switch (ch) { |
| 5200 | case '>': |
| 5201 | redir_fd = redirect_opt_num(&dest); |
| 5202 | if (done_word(&dest, &ctx)) { |
| 5203 | goto parse_error; |
| 5204 | } |
| 5205 | redir_style = REDIRECT_OVERWRITE; |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 5206 | if (next == '\\') |
| 5207 | next = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5208 | if (next == '>') { |
| 5209 | redir_style = REDIRECT_APPEND; |
| 5210 | ch = i_getch(input); |
| 5211 | nommu_addchr(&ctx.as_string, ch); |
| 5212 | } |
| 5213 | #if 0 |
| 5214 | else if (next == '(') { |
| 5215 | syntax_error(">(process) not supported"); |
| 5216 | goto parse_error; |
| 5217 | } |
| 5218 | #endif |
| 5219 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5220 | goto parse_error; |
| 5221 | continue; /* back to top of while (1) */ |
| 5222 | case '<': |
| 5223 | redir_fd = redirect_opt_num(&dest); |
| 5224 | if (done_word(&dest, &ctx)) { |
| 5225 | goto parse_error; |
| 5226 | } |
| 5227 | redir_style = REDIRECT_INPUT; |
Denys Vlasenko | a94eeb0 | 2018-03-31 20:16:31 +0200 | [diff] [blame] | 5228 | if (next == '\\') |
| 5229 | next = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5230 | if (next == '<') { |
| 5231 | redir_style = REDIRECT_HEREDOC; |
| 5232 | heredoc_cnt++; |
| 5233 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5234 | ch = i_getch(input); |
| 5235 | nommu_addchr(&ctx.as_string, ch); |
| 5236 | } else if (next == '>') { |
| 5237 | redir_style = REDIRECT_IO; |
| 5238 | ch = i_getch(input); |
| 5239 | nommu_addchr(&ctx.as_string, ch); |
| 5240 | } |
| 5241 | #if 0 |
| 5242 | else if (next == '(') { |
| 5243 | syntax_error("<(process) not supported"); |
| 5244 | goto parse_error; |
| 5245 | } |
| 5246 | #endif |
| 5247 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5248 | goto parse_error; |
| 5249 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5250 | case '#': |
| 5251 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 5252 | /* skip "#comment" */ |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5253 | /* note: we do not add it to &ctx.as_string */ |
| 5254 | /* TODO: in bash: |
| 5255 | * comment inside $() goes to the next \n, even inside quoted string (!): |
| 5256 | * cmd "$(cmd2 #comment)" - syntax error |
| 5257 | * cmd "`cmd2 #comment`" - ok |
| 5258 | * We accept both (comment ends where command subst ends, in both cases). |
| 5259 | */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5260 | while (1) { |
| 5261 | ch = i_peek(input); |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5262 | if (ch == '\n') { |
| 5263 | nommu_addchr(&ctx.as_string, '\n'); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5264 | break; |
Denys Vlasenko | 25f3b73 | 2017-10-22 15:55:48 +0200 | [diff] [blame] | 5265 | } |
| 5266 | ch = i_getch(input); |
| 5267 | if (ch == EOF) |
| 5268 | break; |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5269 | } |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5270 | continue; /* back to top of while (1) */ |
| 5271 | } |
| 5272 | break; |
| 5273 | case '\\': |
| 5274 | if (next == '\n') { |
| 5275 | /* It's "\<newline>" */ |
| 5276 | #if !BB_MMU |
| 5277 | /* Remove trailing '\' from ctx.as_string */ |
| 5278 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 5279 | #endif |
| 5280 | ch = i_getch(input); /* eat it */ |
| 5281 | continue; /* back to top of while (1) */ |
| 5282 | } |
| 5283 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5284 | } |
| 5285 | |
| 5286 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5287 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5288 | && !ctx.pending_redirect |
| 5289 | ) { |
| 5290 | /* ch is a special char and thus this word |
| 5291 | * cannot be an assignment */ |
| 5292 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5293 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5296 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5297 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5298 | switch (ch) { |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 5299 | case SPECIAL_VAR_SYMBOL: |
| 5300 | /* Convert raw ^C to corresponding special variable reference */ |
| 5301 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5302 | o_addchr(&dest, SPECIAL_VAR_QUOTED_SVS); |
| 5303 | /* fall through */ |
| 5304 | case '#': |
| 5305 | /* non-comment #: "echo a#b" etc */ |
| 5306 | o_addchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5307 | break; |
| 5308 | case '\\': |
| 5309 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5310 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5311 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5312 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5313 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5314 | /* note: ch != '\n' (that case does not reach this place) */ |
| 5315 | o_addchr(&dest, '\\'); |
| 5316 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 5317 | o_addchr(&dest, ch); |
| 5318 | nommu_addchr(&ctx.as_string, ch); |
| 5319 | /* Example: echo Hello \2>file |
| 5320 | * we need to know that word 2 is quoted */ |
| 5321 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5322 | break; |
| 5323 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5324 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5325 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5326 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5327 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5328 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5329 | break; |
| 5330 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5331 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5332 | if (next == '\'' && !ctx.pending_redirect) { |
| 5333 | insert_empty_quoted_str_marker: |
| 5334 | nommu_addchr(&ctx.as_string, next); |
| 5335 | i_getch(input); /* eat second ' */ |
| 5336 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5337 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5338 | } else { |
| 5339 | while (1) { |
| 5340 | ch = i_getch(input); |
| 5341 | if (ch == EOF) { |
| 5342 | syntax_error_unterm_ch('\''); |
| 5343 | goto parse_error; |
| 5344 | } |
| 5345 | nommu_addchr(&ctx.as_string, ch); |
| 5346 | if (ch == '\'') |
| 5347 | break; |
Denys Vlasenko | 9809a82 | 2018-01-13 19:14:27 +0100 | [diff] [blame] | 5348 | if (ch == SPECIAL_VAR_SYMBOL) { |
| 5349 | /* Convert raw ^C to corresponding special variable reference */ |
| 5350 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5351 | o_addchr(&dest, SPECIAL_VAR_QUOTED_SVS); |
| 5352 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5353 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5354 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5355 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5356 | break; |
| 5357 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5358 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5359 | if (next == '"' && !ctx.pending_redirect) |
| 5360 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5361 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 5362 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5363 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5364 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 5365 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5366 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5367 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5368 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5369 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5370 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5371 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5372 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5373 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5374 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 5375 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5376 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5377 | o_addstr(&ctx.as_string, dest.data + pos); |
| 5378 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5379 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5380 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5381 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5382 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5383 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5384 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5385 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5386 | #if ENABLE_HUSH_CASE |
| 5387 | case_semi: |
| 5388 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5389 | if (done_word(&dest, &ctx)) { |
| 5390 | goto parse_error; |
| 5391 | } |
| 5392 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5393 | #if ENABLE_HUSH_CASE |
| 5394 | /* Eat multiple semicolons, detect |
| 5395 | * whether it means something special */ |
| 5396 | while (1) { |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5397 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5398 | if (ch != ';') |
| 5399 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5400 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5401 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5402 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5403 | ctx.ctx_dsemicolon = 1; |
| 5404 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5405 | break; |
| 5406 | } |
| 5407 | } |
| 5408 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5409 | new_cmd: |
| 5410 | /* We just finished a cmd. New one may start |
| 5411 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5412 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5413 | debug_printf_parse("dest.o_assignment='%s'\n", assignment_flag[dest.o_assignment]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5414 | break; |
| 5415 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5416 | if (done_word(&dest, &ctx)) { |
| 5417 | goto parse_error; |
| 5418 | } |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5419 | if (next == '\\') |
| 5420 | next = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5421 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5422 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5423 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5424 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5425 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5426 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5427 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5428 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5429 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5430 | if (done_word(&dest, &ctx)) { |
| 5431 | goto parse_error; |
| 5432 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5433 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5434 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5435 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5436 | #endif |
Denys Vlasenko | 1e5111b | 2018-04-01 03:04:55 +0200 | [diff] [blame] | 5437 | if (next == '\\') |
| 5438 | next = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5439 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5440 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5441 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5442 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5443 | } else { |
| 5444 | /* we could pick up a file descriptor choice here |
| 5445 | * with redirect_opt_num(), but bash doesn't do it. |
| 5446 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5447 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5448 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5449 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5450 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5451 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5452 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5453 | if (ctx.ctx_res_w == RES_MATCH |
| 5454 | && ctx.command->argv == NULL /* not (word|(... */ |
| 5455 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5456 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5457 | ) { |
| 5458 | continue; |
| 5459 | } |
| 5460 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5461 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5462 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 5463 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5464 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5465 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5466 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5467 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5468 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5469 | goto case_semi; |
| 5470 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5471 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5472 | /* proper use of this character is caught by end_trigger: |
| 5473 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5474 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5475 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5476 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5477 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5478 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5479 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5480 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5481 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5482 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5483 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5484 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5485 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5486 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5487 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5488 | struct parse_context *pctx; |
| 5489 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5490 | |
| 5491 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5492 | * Sample for finding leaks on syntax error recovery path. |
| 5493 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5494 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5495 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5496 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5497 | * 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] | 5498 | */ |
| 5499 | pctx = &ctx; |
| 5500 | do { |
| 5501 | /* Update pipe/command counts, |
| 5502 | * otherwise freeing may miss some */ |
| 5503 | done_pipe(pctx, PIPE_SEQ); |
| 5504 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5505 | pctx->list_head, pctx); |
| 5506 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5507 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5508 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5509 | #if !BB_MMU |
| 5510 | o_free_unsafe(&pctx->as_string); |
| 5511 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5512 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5513 | if (pctx != &ctx) { |
| 5514 | free(pctx); |
| 5515 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5516 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5517 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5518 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 5519 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5520 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5521 | if (pstring) |
| 5522 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5523 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5524 | debug_leave(); |
| 5525 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5526 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5529 | |
| 5530 | /*** Execution routines ***/ |
| 5531 | |
| 5532 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5533 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5534 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5535 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5536 | expand_string_to_string(str) |
| 5537 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5538 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5539 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5540 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5541 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5542 | |
| 5543 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5544 | * all variable references within and returns a pointer to |
| 5545 | * a list of expanded strings, possibly with larger number |
| 5546 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5547 | * This new list is allocated as a single malloc block. |
| 5548 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5549 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5550 | * Caller can deallocate entire list by single free(list). */ |
| 5551 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5552 | /* A horde of its helpers come first: */ |
| 5553 | |
| 5554 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5555 | { |
| 5556 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5557 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5558 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5559 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5560 | if (c == '{' || c == '}') { |
| 5561 | /* { -> \{, } -> \} */ |
| 5562 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5563 | /* And now we want to add { or } and continue: |
| 5564 | * o_addchr(o, c); |
| 5565 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5566 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5567 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5568 | } |
| 5569 | #endif |
| 5570 | o_addchr(o, c); |
| 5571 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5572 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5573 | o_addchr(o, '\\'); |
| 5574 | if (len) { |
| 5575 | len--; |
| 5576 | o_addchr(o, '\\'); |
| 5577 | o_addchr(o, *str++); |
| 5578 | } |
| 5579 | } |
| 5580 | } |
| 5581 | } |
| 5582 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5583 | /* Store given string, finalizing the word and starting new one whenever |
| 5584 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5585 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5586 | * Return in *ended_with_ifs: |
| 5587 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5588 | */ |
| 5589 | 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] | 5590 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5591 | int last_is_ifs = 0; |
| 5592 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5593 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5594 | int word_len; |
| 5595 | |
| 5596 | if (!*str) /* EOL - do not finalize word */ |
| 5597 | break; |
| 5598 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5599 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5600 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5601 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5602 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5603 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5604 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5605 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5606 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5607 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5608 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5609 | /*/ Why can't we do it easier? */ |
| 5610 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5611 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5612 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5613 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5614 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5615 | if (!*str) /* EOL - do not finalize word */ |
| 5616 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5617 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5618 | |
| 5619 | /* We know str here points to at least one IFS char */ |
| 5620 | last_is_ifs = 1; |
| 5621 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5622 | if (!*str) /* EOL - do not finalize word */ |
| 5623 | break; |
| 5624 | |
| 5625 | /* Start new word... but not always! */ |
| 5626 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5627 | if (output->has_quoted_part |
| 5628 | /* Case "v=' a'; echo $v": |
| 5629 | * here nothing precedes the space in $v expansion, |
| 5630 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5631 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5632 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5633 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5634 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5635 | o_addchr(output, '\0'); |
| 5636 | debug_print_list("expand_on_ifs", output, n); |
| 5637 | n = o_save_ptr(output, n); |
| 5638 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5639 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5640 | |
| 5641 | if (ended_with_ifs) |
| 5642 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5643 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5644 | return n; |
| 5645 | } |
| 5646 | |
| 5647 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5648 | * they are in double quotes, with the exception that they are not :). |
| 5649 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5650 | * |
| 5651 | * Returns malloced string. |
| 5652 | * As an optimization, we return NULL if expansion is not needed. |
| 5653 | */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5654 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5655 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5656 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5657 | encode_then_expand_string(str) |
| 5658 | #endif |
| 5659 | 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] | 5660 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5661 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 5662 | enum { do_unbackslash = 1 }; |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5663 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5664 | char *exp_str; |
| 5665 | struct in_str input; |
| 5666 | o_string dest = NULL_O_STRING; |
| 5667 | |
| 5668 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5669 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5670 | #if ENABLE_HUSH_TICK |
| 5671 | && !strchr(str, '`') |
| 5672 | #endif |
| 5673 | ) { |
| 5674 | return NULL; |
| 5675 | } |
| 5676 | |
| 5677 | /* We need to expand. Example: |
| 5678 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5679 | */ |
| 5680 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5681 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5682 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5683 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5684 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5685 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5686 | o_free_unsafe(&dest); |
| 5687 | return exp_str; |
| 5688 | } |
| 5689 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5690 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5691 | 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] | 5692 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5693 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5694 | arith_t res; |
| 5695 | char *exp_str; |
| 5696 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5697 | math_state.lookupvar = get_local_var_value; |
| 5698 | math_state.setvar = set_local_var_from_halves; |
| 5699 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5700 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5701 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5702 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5703 | if (errmsg_p) |
| 5704 | *errmsg_p = math_state.errmsg; |
| 5705 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5706 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5707 | return res; |
| 5708 | } |
| 5709 | #endif |
| 5710 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5711 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5712 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5713 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5714 | { |
| 5715 | while (1) { |
| 5716 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5717 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5718 | if (end) { |
| 5719 | *size = end - val; |
| 5720 | return val; |
| 5721 | } |
| 5722 | if (*val == '\0') |
| 5723 | return NULL; |
| 5724 | /* Optimization: if "*pat" did not match the start of "string", |
| 5725 | * we know that "tring", "ring" etc will not match too: |
| 5726 | */ |
| 5727 | if (pattern[0] == '*') |
| 5728 | return NULL; |
| 5729 | val++; |
| 5730 | } |
| 5731 | } |
| 5732 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5733 | { |
| 5734 | char *result = NULL; |
| 5735 | unsigned res_len = 0; |
| 5736 | unsigned repl_len = strlen(repl); |
| 5737 | |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5738 | /* Null pattern never matches, including if "var" is empty */ |
| 5739 | if (!pattern[0]) |
| 5740 | return result; /* NULL, no replaces happened */ |
| 5741 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5742 | while (1) { |
| 5743 | int size; |
| 5744 | char *s = strstr_pattern(val, pattern, &size); |
| 5745 | if (!s) |
| 5746 | break; |
| 5747 | |
| 5748 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5749 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5750 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5751 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5752 | |
| 5753 | val = s + size; |
| 5754 | if (exp_op == '/') |
| 5755 | break; |
| 5756 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5757 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5758 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5759 | strcpy(result + res_len, val); |
| 5760 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5761 | } |
| 5762 | debug_printf_varexp("result:'%s'\n", result); |
| 5763 | return result; |
| 5764 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5765 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5766 | |
| 5767 | /* Helper: |
| 5768 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5769 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5770 | 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] | 5771 | { |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5772 | const char *val; |
| 5773 | char *to_be_freed; |
| 5774 | char *p; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5775 | char *var; |
| 5776 | char first_char; |
| 5777 | char exp_op; |
| 5778 | char exp_save = exp_save; /* for compiler */ |
| 5779 | char *exp_saveptr; /* points to expansion operator */ |
| 5780 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5781 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5782 | |
Denys Vlasenko | 0ca3198 | 2018-01-25 13:20:50 +0100 | [diff] [blame] | 5783 | val = NULL; |
| 5784 | to_be_freed = NULL; |
| 5785 | p = *pp; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5786 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5787 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5788 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5789 | arg0 = arg[0]; |
| 5790 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5791 | exp_op = 0; |
| 5792 | |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5793 | if (first_char == '#' && arg[1] /* ${#...} but not ${#} */ |
| 5794 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 5795 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 5796 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5797 | ) { |
| 5798 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5799 | var++; |
| 5800 | exp_op = 'L'; |
| 5801 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5802 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5803 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5804 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5805 | ) { |
| 5806 | /* ${?:0}, ${#[:]%0} etc */ |
| 5807 | exp_saveptr = var + 1; |
| 5808 | } else { |
| 5809 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5810 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5811 | } |
| 5812 | exp_op = exp_save = *exp_saveptr; |
| 5813 | if (exp_op) { |
| 5814 | exp_word = exp_saveptr + 1; |
| 5815 | if (exp_op == ':') { |
| 5816 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5817 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5818 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5819 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5820 | ) { |
| 5821 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5822 | exp_op = ':'; |
| 5823 | exp_word--; |
| 5824 | } |
| 5825 | } |
| 5826 | *exp_saveptr = '\0'; |
| 5827 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5828 | } |
| 5829 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5830 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5831 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5832 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5833 | int n = xatoi_positive(var); |
| 5834 | if (n < G.global_argc) |
| 5835 | val = G.global_argv[n]; |
| 5836 | /* else val remains NULL: $N with too big N */ |
| 5837 | } else { |
| 5838 | switch (var[0]) { |
| 5839 | case '$': /* pid */ |
| 5840 | val = utoa(G.root_pid); |
| 5841 | break; |
| 5842 | case '!': /* bg pid */ |
| 5843 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5844 | break; |
| 5845 | case '?': /* exitcode */ |
| 5846 | val = utoa(G.last_exitcode); |
| 5847 | break; |
| 5848 | case '#': /* argc */ |
| 5849 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5850 | break; |
| 5851 | default: |
| 5852 | val = get_local_var_value(var); |
| 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | /* Handle any expansions */ |
| 5857 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5858 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5859 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5860 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5861 | debug_printf_expand("%s\n", val); |
| 5862 | } else if (exp_op) { |
| 5863 | if (exp_op == '%' || exp_op == '#') { |
| 5864 | /* Standard-mandated substring removal ops: |
| 5865 | * ${parameter%word} - remove smallest suffix pattern |
| 5866 | * ${parameter%%word} - remove largest suffix pattern |
| 5867 | * ${parameter#word} - remove smallest prefix pattern |
| 5868 | * ${parameter##word} - remove largest prefix pattern |
| 5869 | * |
| 5870 | * Word is expanded to produce a glob pattern. |
| 5871 | * Then var's value is matched to it and matching part removed. |
| 5872 | */ |
| 5873 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5874 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5875 | char *exp_exp_word; |
| 5876 | char *loc; |
| 5877 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5878 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5879 | exp_word++; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5880 | debug_printf_expand("expand: exp_word:'%s'\n", exp_word); |
Denys Vlasenko | d4802c6 | 2018-03-02 20:48:36 +0100 | [diff] [blame] | 5881 | /* |
| 5882 | * process_bkslash:1 unbackslash:1 breaks this: |
| 5883 | * a='a\\'; echo ${a%\\\\} # correct output is: a |
| 5884 | * process_bkslash:1 unbackslash:0 breaks this: |
| 5885 | * a='a}'; echo ${a%\}} # correct output is: a |
| 5886 | */ |
| 5887 | 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] | 5888 | if (exp_exp_word) |
| 5889 | exp_word = exp_exp_word; |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5890 | debug_printf_expand("expand: exp_exp_word:'%s'\n", exp_word); |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5891 | /* HACK ALERT. We depend here on the fact that |
| 5892 | * G.global_argv and results of utoa and get_local_var_value |
| 5893 | * are actually in writable memory: |
| 5894 | * scan_and_match momentarily stores NULs there. */ |
| 5895 | t = (char*)val; |
| 5896 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | 55f8133 | 2018-03-02 18:12:12 +0100 | [diff] [blame] | 5897 | 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] | 5898 | free(exp_exp_word); |
| 5899 | if (loc) { /* match was found */ |
| 5900 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5901 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5902 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5903 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5904 | } |
| 5905 | } |
| 5906 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5907 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5908 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5909 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5910 | * Note that in encoded form it has TWO parts: |
| 5911 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5912 | * and if // is used, it is encoded as \: |
| 5913 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5914 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5915 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5916 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5917 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5918 | * by the usual expansion rules: |
| 5919 | * >az; >bz; |
| 5920 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5921 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5922 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5923 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5924 | * (note that a*z _pattern_ is never globbed!) |
| 5925 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5926 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5927 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5928 | if (!pattern) |
| 5929 | pattern = xstrdup(exp_word); |
| 5930 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5931 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5932 | exp_word = p; |
| 5933 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5934 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5935 | repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ arg0 & 0x80, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5936 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5937 | /* HACK ALERT. We depend here on the fact that |
| 5938 | * G.global_argv and results of utoa and get_local_var_value |
| 5939 | * are actually in writable memory: |
| 5940 | * replace_pattern momentarily stores NULs there. */ |
| 5941 | t = (char*)val; |
| 5942 | to_be_freed = replace_pattern(t, |
| 5943 | pattern, |
| 5944 | (repl ? repl : exp_word), |
| 5945 | exp_op); |
| 5946 | if (to_be_freed) /* at least one replace happened */ |
| 5947 | val = to_be_freed; |
| 5948 | free(pattern); |
| 5949 | free(repl); |
Denys Vlasenko | cba79a8 | 2018-01-25 14:07:40 +0100 | [diff] [blame] | 5950 | } else { |
| 5951 | /* Empty variable always gives nothing */ |
| 5952 | // "v=''; echo ${v/*/w}" prints "", not "w" |
| 5953 | /* Just skip "replace" part */ |
| 5954 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5955 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5956 | *p = '\0'; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5957 | } |
| 5958 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5959 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5960 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5961 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5962 | /* It's ${var:N[:M]} bashism. |
| 5963 | * Note that in encoded form it has TWO parts: |
| 5964 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5965 | */ |
| 5966 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5967 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5968 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5969 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5970 | if (errmsg) |
| 5971 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5972 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5973 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5974 | exp_word = p; |
| 5975 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5976 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5977 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5978 | if (errmsg) |
| 5979 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5980 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5981 | if (beg < 0) { |
| 5982 | /* negative beg counts from the end */ |
| 5983 | beg = (arith_t)strlen(val) + beg; |
| 5984 | if (beg < 0) /* ${v: -999999} is "" */ |
| 5985 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5986 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5987 | debug_printf_varexp("from val:'%s'\n", val); |
| 5988 | if (len < 0) { |
| 5989 | /* in bash, len=-n means strlen()-n */ |
| 5990 | len = (arith_t)strlen(val) - beg + len; |
| 5991 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5992 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5993 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 5994 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5995 | arith_err: |
| 5996 | val = NULL; |
| 5997 | } else { |
| 5998 | /* Paranoia. What if user entered 9999999999999 |
| 5999 | * which fits in arith_t but not int? */ |
| 6000 | if (len >= INT_MAX) |
| 6001 | len = INT_MAX; |
| 6002 | val = to_be_freed = xstrndup(val + beg, len); |
| 6003 | } |
| 6004 | debug_printf_varexp("val:'%s'\n", val); |
| 6005 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6006 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 6007 | val = NULL; |
| 6008 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6009 | } else { /* one of "-=+?" */ |
| 6010 | /* Standard-mandated substitution ops: |
| 6011 | * ${var?word} - indicate error if unset |
| 6012 | * If var is unset, word (or a message indicating it is unset |
| 6013 | * if word is null) is written to standard error |
| 6014 | * and the shell exits with a non-zero exit status. |
| 6015 | * Otherwise, the value of var is substituted. |
| 6016 | * ${var-word} - use default value |
| 6017 | * If var is unset, word is substituted. |
| 6018 | * ${var=word} - assign and use default value |
| 6019 | * If var is unset, word is assigned to var. |
| 6020 | * In all cases, final value of var is substituted. |
| 6021 | * ${var+word} - use alternative value |
| 6022 | * If var is unset, null is substituted. |
| 6023 | * Otherwise, word is substituted. |
| 6024 | * |
| 6025 | * Word is subjected to tilde expansion, parameter expansion, |
| 6026 | * command substitution, and arithmetic expansion. |
| 6027 | * If word is not needed, it is not expanded. |
| 6028 | * |
| 6029 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 6030 | * but also treat null var as if it is unset. |
| 6031 | */ |
| 6032 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 6033 | if (exp_op == '+') |
| 6034 | use_word = !use_word; |
| 6035 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 6036 | (exp_save == ':') ? "true" : "false", use_word); |
| 6037 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6038 | 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] | 6039 | if (to_be_freed) |
| 6040 | exp_word = to_be_freed; |
| 6041 | if (exp_op == '?') { |
| 6042 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6043 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6044 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 6045 | exp_word[0] |
| 6046 | ? exp_word |
| 6047 | : "parameter null or not set" |
| 6048 | /* ash has more specific messages, a-la: */ |
| 6049 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6050 | ); |
| 6051 | //TODO: how interactive bash aborts expansion mid-command? |
| 6052 | } else { |
| 6053 | val = exp_word; |
| 6054 | } |
| 6055 | |
| 6056 | if (exp_op == '=') { |
| 6057 | /* ${var=[word]} or ${var:=[word]} */ |
| 6058 | if (isdigit(var[0]) || var[0] == '#') { |
| 6059 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6060 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6061 | val = NULL; |
| 6062 | } else { |
| 6063 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 6064 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | } /* one of "-=+?" */ |
| 6069 | |
| 6070 | *exp_saveptr = exp_save; |
| 6071 | } /* if (exp_op) */ |
| 6072 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6073 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6074 | |
| 6075 | *pp = p; |
| 6076 | *to_be_freed_pp = to_be_freed; |
| 6077 | return val; |
| 6078 | } |
| 6079 | |
| 6080 | /* Expand all variable references in given string, adding words to list[] |
| 6081 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 6082 | * to be filled). This routine is extremely tricky: has to deal with |
| 6083 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 6084 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6085 | 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] | 6086 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6087 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6088 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6089 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6090 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6091 | 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] | 6092 | char *p; |
| 6093 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6094 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 6095 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6096 | debug_print_list("expand_vars_to_list", output, n); |
| 6097 | n = o_save_ptr(output, n); |
| 6098 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 6099 | |
| 6100 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 6101 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6102 | char *to_be_freed = NULL; |
| 6103 | const char *val = NULL; |
| 6104 | #if ENABLE_HUSH_TICK |
| 6105 | o_string subst_result = NULL_O_STRING; |
| 6106 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6107 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6108 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 6109 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6110 | |
| 6111 | if (ended_in_ifs) { |
| 6112 | o_addchr(output, '\0'); |
| 6113 | n = o_save_ptr(output, n); |
| 6114 | ended_in_ifs = 0; |
| 6115 | } |
| 6116 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6117 | o_addblock(output, arg, p - arg); |
| 6118 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 6119 | arg = ++p; |
| 6120 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 6121 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6122 | /* Fetch special var name (if it is indeed one of them) |
| 6123 | * and quote bit, force the bit on if singleword expansion - |
| 6124 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6125 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6126 | |
| 6127 | /* Is this variable quoted and thus expansion can't be null? |
| 6128 | * "$@" is special. Even if quoted, it can still |
| 6129 | * expand to nothing (not even an empty string), |
| 6130 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6131 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6132 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6133 | |
| 6134 | switch (first_ch & 0x7f) { |
| 6135 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 6136 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6137 | case '@': { |
| 6138 | int i; |
| 6139 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6140 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6141 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6142 | 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] | 6143 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6144 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6145 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6146 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 6147 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 6148 | /* this argv[] is not empty and not last: |
| 6149 | * put terminating NUL, start new word */ |
| 6150 | o_addchr(output, '\0'); |
| 6151 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 6152 | n = o_save_ptr(output, n); |
| 6153 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 6154 | } |
| 6155 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6156 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6157 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6158 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 6ffaa00 | 2018-03-31 00:46:07 +0200 | [diff] [blame] | 6159 | if (first_ch == (char)('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6160 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6161 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6162 | while (1) { |
| 6163 | o_addQstr(output, G.global_argv[i]); |
| 6164 | if (++i >= G.global_argc) |
| 6165 | break; |
| 6166 | o_addchr(output, '\0'); |
| 6167 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6168 | n = o_save_ptr(output, n); |
| 6169 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6170 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6171 | while (1) { |
| 6172 | o_addQstr(output, G.global_argv[i]); |
| 6173 | if (!G.global_argv[++i]) |
| 6174 | break; |
| 6175 | if (G.ifs[0]) |
| 6176 | o_addchr(output, G.ifs[0]); |
| 6177 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6178 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6179 | } |
| 6180 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6181 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6182 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 6183 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6184 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6185 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6186 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6187 | break; |
Denys Vlasenko | 932b997 | 2018-01-11 12:39:48 +0100 | [diff] [blame] | 6188 | case SPECIAL_VAR_QUOTED_SVS: |
| 6189 | /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */ |
| 6190 | arg++; |
| 6191 | val = SPECIAL_VAR_SYMBOL_STR; |
| 6192 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6193 | #if ENABLE_HUSH_TICK |
| 6194 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6195 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6196 | arg++; |
| 6197 | /* Can't just stuff it into output o_string, |
| 6198 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6199 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6200 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6201 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 6202 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 6203 | val = subst_result.data; |
| 6204 | goto store_val; |
| 6205 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6206 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6207 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 6208 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6209 | |
| 6210 | arg++; /* skip '+' */ |
| 6211 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6212 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6213 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6214 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6215 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6216 | val = arith_buf; |
| 6217 | break; |
| 6218 | } |
| 6219 | #endif |
| 6220 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6221 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6222 | IF_HUSH_TICK(store_val:) |
| 6223 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6224 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6225 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6226 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6227 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6228 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6229 | } |
| 6230 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6231 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6232 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6233 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6234 | } |
| 6235 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6236 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6237 | |
| 6238 | if (val && val[0]) { |
| 6239 | o_addQstr(output, val); |
| 6240 | } |
| 6241 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6242 | |
| 6243 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6244 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6245 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6246 | *p = SPECIAL_VAR_SYMBOL; |
| 6247 | |
| 6248 | #if ENABLE_HUSH_TICK |
| 6249 | o_free(&subst_result); |
| 6250 | #endif |
| 6251 | arg = ++p; |
| 6252 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6253 | |
| 6254 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6255 | if (ended_in_ifs) { |
| 6256 | o_addchr(output, '\0'); |
| 6257 | n = o_save_ptr(output, n); |
| 6258 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6259 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6260 | /* this part is literal, and it was already pre-quoted |
| 6261 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6262 | o_addstr_with_NUL(output, arg); |
| 6263 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6264 | } 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] | 6265 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6266 | ) { |
| 6267 | n--; |
| 6268 | /* allow to reuse list[n] later without re-growth */ |
| 6269 | output->has_empty_slot = 1; |
| 6270 | } else { |
| 6271 | o_addchr(output, '\0'); |
| 6272 | } |
| 6273 | |
| 6274 | return n; |
| 6275 | } |
| 6276 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6277 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6278 | { |
| 6279 | int n; |
| 6280 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6281 | o_string output = NULL_O_STRING; |
| 6282 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6283 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6284 | |
| 6285 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6286 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6287 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6288 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6289 | } |
| 6290 | debug_print_list("expand_variables", &output, n); |
| 6291 | |
| 6292 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6293 | list = o_finalize_list(&output, n); |
| 6294 | debug_print_strings("expand_variables[1]", list); |
| 6295 | return list; |
| 6296 | } |
| 6297 | |
| 6298 | static char **expand_strvec_to_strvec(char **argv) |
| 6299 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6300 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6301 | } |
| 6302 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6303 | #if BASH_TEST2 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6304 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6305 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6306 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6307 | } |
| 6308 | #endif |
| 6309 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6310 | /* Used for expansion of right hand of assignments, |
| 6311 | * $((...)), heredocs, variable espansion parts. |
| 6312 | * |
| 6313 | * NB: should NOT do globbing! |
| 6314 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6315 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6316 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6317 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6318 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6319 | const int do_unbackslash = 1; |
| 6320 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6321 | char *argv[2], **list; |
| 6322 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6323 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6324 | /* This is generally an optimization, but it also |
| 6325 | * handles "", which otherwise trips over !list[0] check below. |
| 6326 | * (is this ever happens that we actually get str="" here?) |
| 6327 | */ |
| 6328 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6329 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6330 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6331 | return xstrdup(str); |
| 6332 | } |
| 6333 | |
| 6334 | argv[0] = (char*)str; |
| 6335 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6336 | list = expand_variables(argv, do_unbackslash |
| 6337 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 6338 | : EXP_FLAG_SINGLEWORD |
| 6339 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6340 | if (HUSH_DEBUG) |
| 6341 | if (!list[0] || list[1]) |
| 6342 | bb_error_msg_and_die("BUG in varexp2"); |
| 6343 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6344 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6345 | if (do_unbackslash) |
| 6346 | unbackslash((char*)list); |
| 6347 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6348 | return (char*)list; |
| 6349 | } |
| 6350 | |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6351 | #if ENABLE_HUSH_CASE |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6352 | static char* expand_strvec_to_string(char **argv) |
| 6353 | { |
| 6354 | char **list; |
| 6355 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6356 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6357 | /* Convert all NULs to spaces */ |
| 6358 | if (list[0]) { |
| 6359 | int n = 1; |
| 6360 | while (list[n]) { |
| 6361 | if (HUSH_DEBUG) |
| 6362 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6363 | bb_error_msg_and_die("BUG in varexp3"); |
| 6364 | /* bash uses ' ' regardless of $IFS contents */ |
| 6365 | list[n][-1] = ' '; |
| 6366 | n++; |
| 6367 | } |
| 6368 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6369 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6370 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6371 | return (char*)list; |
| 6372 | } |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 6373 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6374 | |
| 6375 | static char **expand_assignments(char **argv, int count) |
| 6376 | { |
| 6377 | int i; |
| 6378 | char **p; |
| 6379 | |
| 6380 | G.expanded_assignments = p = NULL; |
| 6381 | /* Expand assignments into one string each */ |
| 6382 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6383 | 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] | 6384 | } |
| 6385 | G.expanded_assignments = NULL; |
| 6386 | return p; |
| 6387 | } |
| 6388 | |
| 6389 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6390 | static void switch_off_special_sigs(unsigned mask) |
| 6391 | { |
| 6392 | unsigned sig = 0; |
| 6393 | while ((mask >>= 1) != 0) { |
| 6394 | sig++; |
| 6395 | if (!(mask & 1)) |
| 6396 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6397 | #if ENABLE_HUSH_TRAP |
| 6398 | if (G_traps) { |
| 6399 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6400 | /* trap is '', has to remain SIG_IGN */ |
| 6401 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6402 | free(G_traps[sig]); |
| 6403 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6404 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6405 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6406 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6407 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6408 | } |
| 6409 | } |
| 6410 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6411 | #if BB_MMU |
| 6412 | /* never called */ |
| 6413 | void re_execute_shell(char ***to_free, const char *s, |
| 6414 | char *g_argv0, char **g_argv, |
| 6415 | char **builtin_argv) NORETURN; |
| 6416 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6417 | static void reset_traps_to_defaults(void) |
| 6418 | { |
| 6419 | /* This function is always called in a child shell |
| 6420 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6421 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6422 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6423 | unsigned mask; |
| 6424 | |
| 6425 | /* Child shells are not interactive. |
| 6426 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6427 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6428 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6429 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6430 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6431 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6432 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6433 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6434 | /* Switch off special sigs */ |
| 6435 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6436 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6437 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6438 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6439 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6440 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6441 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6442 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6443 | # if ENABLE_HUSH_TRAP |
| 6444 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6445 | return; |
| 6446 | |
| 6447 | /* Reset all sigs to default except ones with empty traps */ |
| 6448 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6449 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6450 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6451 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6452 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6453 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6454 | free(G_traps[sig]); |
| 6455 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6456 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6457 | if (sig == 0) |
| 6458 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6459 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6460 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6461 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | #else /* !BB_MMU */ |
| 6465 | |
| 6466 | static void re_execute_shell(char ***to_free, const char *s, |
| 6467 | char *g_argv0, char **g_argv, |
| 6468 | char **builtin_argv) NORETURN; |
| 6469 | static void re_execute_shell(char ***to_free, const char *s, |
| 6470 | char *g_argv0, char **g_argv, |
| 6471 | char **builtin_argv) |
| 6472 | { |
| 6473 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6474 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6475 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6476 | char *heredoc_argv[4]; |
| 6477 | struct variable *cur; |
| 6478 | # if ENABLE_HUSH_FUNCTIONS |
| 6479 | struct function *funcp; |
| 6480 | # endif |
| 6481 | char **argv, **pp; |
| 6482 | unsigned cnt; |
| 6483 | unsigned long long empty_trap_mask; |
| 6484 | |
| 6485 | if (!g_argv0) { /* heredoc */ |
| 6486 | argv = heredoc_argv; |
| 6487 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6488 | argv[1] = (char *) "-<"; |
| 6489 | argv[2] = (char *) s; |
| 6490 | argv[3] = NULL; |
| 6491 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6492 | goto do_exec; |
| 6493 | } |
| 6494 | |
| 6495 | cnt = 0; |
| 6496 | pp = builtin_argv; |
| 6497 | if (pp) while (*pp++) |
| 6498 | cnt++; |
| 6499 | |
| 6500 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6501 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6502 | int sig; |
| 6503 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6504 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6505 | empty_trap_mask |= 1LL << sig; |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6510 | , (unsigned) G.root_pid |
| 6511 | , (unsigned) G.root_ppid |
| 6512 | , (unsigned) G.last_bg_pid |
| 6513 | , (unsigned) G.last_exitcode |
| 6514 | , cnt |
| 6515 | , empty_trap_mask |
| 6516 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6517 | ); |
| 6518 | # undef NOMMU_HACK_FMT |
| 6519 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6520 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6521 | */ |
| 6522 | cnt += 6; |
| 6523 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6524 | if (!cur->flg_export || cur->flg_read_only) |
| 6525 | cnt += 2; |
| 6526 | } |
| 6527 | # if ENABLE_HUSH_FUNCTIONS |
| 6528 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6529 | cnt += 3; |
| 6530 | # endif |
| 6531 | pp = g_argv; |
| 6532 | while (*pp++) |
| 6533 | cnt++; |
| 6534 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6535 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6536 | *pp++ = param_buf; |
| 6537 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6538 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6539 | continue; |
| 6540 | if (cur->flg_read_only) { |
| 6541 | *pp++ = (char *) "-R"; |
| 6542 | *pp++ = cur->varstr; |
| 6543 | } else if (!cur->flg_export) { |
| 6544 | *pp++ = (char *) "-V"; |
| 6545 | *pp++ = cur->varstr; |
| 6546 | } |
| 6547 | } |
| 6548 | # if ENABLE_HUSH_FUNCTIONS |
| 6549 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6550 | *pp++ = (char *) "-F"; |
| 6551 | *pp++ = funcp->name; |
| 6552 | *pp++ = funcp->body_as_string; |
| 6553 | } |
| 6554 | # endif |
| 6555 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6556 | * |
| 6557 | * However, POSIX says that subshells reset signals with traps |
| 6558 | * to SIG_DFL. |
| 6559 | * I tested bash-3.2 and it not only does that with true subshells |
| 6560 | * of the form ( list ), but with any forked children shells. |
| 6561 | * I set trap "echo W" WINCH; and then tried: |
| 6562 | * |
| 6563 | * { echo 1; sleep 20; echo 2; } & |
| 6564 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6565 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6566 | * |
| 6567 | * In all these cases sending SIGWINCH to the child shell |
| 6568 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6569 | * _inside_ group (just before echo 1), it works. |
| 6570 | * |
| 6571 | * 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] | 6572 | */ |
| 6573 | *pp++ = (char *) "-c"; |
| 6574 | *pp++ = (char *) s; |
| 6575 | if (builtin_argv) { |
| 6576 | while (*++builtin_argv) |
| 6577 | *pp++ = *builtin_argv; |
| 6578 | *pp++ = (char *) ""; |
| 6579 | } |
| 6580 | *pp++ = g_argv0; |
| 6581 | while (*g_argv) |
| 6582 | *pp++ = *g_argv++; |
| 6583 | /* *pp = NULL; - is already there */ |
| 6584 | pp = environ; |
| 6585 | |
| 6586 | do_exec: |
| 6587 | 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] | 6588 | /* Don't propagate SIG_IGN to the child */ |
| 6589 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6590 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6591 | execve(bb_busybox_exec_path, argv, pp); |
| 6592 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6593 | if (argv[0][0] == '/') |
| 6594 | execve(argv[0], argv, pp); |
| 6595 | xfunc_error_retval = 127; |
| 6596 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6597 | } |
| 6598 | #endif /* !BB_MMU */ |
| 6599 | |
| 6600 | |
| 6601 | static int run_and_free_list(struct pipe *pi); |
| 6602 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6603 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6604 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6605 | * end_trigger controls how often we stop parsing |
| 6606 | * NUL: parse all, execute, return |
| 6607 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6608 | */ |
| 6609 | 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] | 6610 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6611 | /* Why we need empty flag? |
| 6612 | * An obscure corner case "false; ``; echo $?": |
| 6613 | * empty command in `` should still set $? to 0. |
| 6614 | * But we can't just set $? to 0 at the start, |
| 6615 | * this breaks "false; echo `echo $?`" case. |
| 6616 | */ |
| 6617 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6618 | while (1) { |
| 6619 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6620 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6621 | #if ENABLE_HUSH_INTERACTIVE |
| 6622 | if (end_trigger == ';') |
| 6623 | inp->promptmode = 0; /* PS1 */ |
| 6624 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6625 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6626 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6627 | /* If we are in "big" script |
| 6628 | * (not in `cmd` or something similar)... |
| 6629 | */ |
| 6630 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6631 | /* Discard cached input (rest of line) */ |
| 6632 | int ch = inp->last_char; |
| 6633 | while (ch != EOF && ch != '\n') { |
| 6634 | //bb_error_msg("Discarded:'%c'", ch); |
| 6635 | ch = i_getch(inp); |
| 6636 | } |
| 6637 | /* Force prompt */ |
| 6638 | inp->p = NULL; |
| 6639 | /* This stream isn't empty */ |
| 6640 | empty = 0; |
| 6641 | continue; |
| 6642 | } |
| 6643 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6644 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6645 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6646 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6647 | debug_print_tree(pipe_list, 0); |
| 6648 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6649 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6650 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6651 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6652 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6653 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6654 | } |
| 6655 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6656 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6657 | { |
| 6658 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6659 | //IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
| 6660 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6661 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6662 | parse_and_run_stream(&input, '\0'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6663 | //IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6666 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6667 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6668 | struct in_str input; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6669 | IF_HUSH_LINENO_VAR(unsigned sv = G.lineno;) |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 6670 | |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6671 | IF_HUSH_LINENO_VAR(G.lineno = 1;) |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 6672 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6673 | parse_and_run_stream(&input, ';'); |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6674 | IF_HUSH_LINENO_VAR(G.lineno = sv;) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6677 | #if ENABLE_HUSH_TICK |
| 6678 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6679 | { |
| 6680 | pid_t pid; |
| 6681 | int channel[2]; |
| 6682 | # if !BB_MMU |
| 6683 | char **to_free = NULL; |
| 6684 | # endif |
| 6685 | |
| 6686 | xpipe(channel); |
| 6687 | pid = BB_MMU ? xfork() : xvfork(); |
| 6688 | if (pid == 0) { /* child */ |
| 6689 | disable_restore_tty_pgrp_on_exit(); |
| 6690 | /* Process substitution is not considered to be usual |
| 6691 | * 'command execution'. |
| 6692 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6693 | */ |
| 6694 | bb_signals(0 |
| 6695 | + (1 << SIGTSTP) |
| 6696 | + (1 << SIGTTIN) |
| 6697 | + (1 << SIGTTOU) |
| 6698 | , SIG_IGN); |
| 6699 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6700 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6701 | xmove_fd(channel[1], 1); |
| 6702 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6703 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6704 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6705 | /* Awful hack for `trap` or $(trap). |
| 6706 | * |
| 6707 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6708 | * contains an example where "trap" is executed in a subshell: |
| 6709 | * |
| 6710 | * save_traps=$(trap) |
| 6711 | * ... |
| 6712 | * eval "$save_traps" |
| 6713 | * |
| 6714 | * Standard does not say that "trap" in subshell shall print |
| 6715 | * parent shell's traps. It only says that its output |
| 6716 | * must have suitable form, but then, in the above example |
| 6717 | * (which is not supposed to be normative), it implies that. |
| 6718 | * |
| 6719 | * bash (and probably other shell) does implement it |
| 6720 | * (traps are reset to defaults, but "trap" still shows them), |
| 6721 | * but as a result, "trap" logic is hopelessly messed up: |
| 6722 | * |
| 6723 | * # trap |
| 6724 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6725 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6726 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6727 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6728 | * trap -- 'echo Ho' SIGWINCH |
| 6729 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6730 | * trap -- 'echo Ho' SIGWINCH |
| 6731 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6732 | * trap -- 'echo Ho' SIGWINCH |
| 6733 | * |
| 6734 | * The rules when to forget and when to not forget traps |
| 6735 | * get really complex and nonsensical. |
| 6736 | * |
| 6737 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6738 | */ |
| 6739 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6740 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6741 | && skip_whitespace(s + 4)[0] == '\0' |
| 6742 | ) { |
| 6743 | static const char *const argv[] = { NULL, NULL }; |
| 6744 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6745 | fflush_all(); /* important */ |
| 6746 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6747 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6748 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6749 | # if BB_MMU |
| 6750 | reset_traps_to_defaults(); |
| 6751 | parse_and_run_string(s); |
| 6752 | _exit(G.last_exitcode); |
| 6753 | # else |
| 6754 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6755 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6756 | * huge=`cat BIG` # was blocking here forever |
| 6757 | * echo OK |
| 6758 | */ |
| 6759 | re_execute_shell(&to_free, |
| 6760 | s, |
| 6761 | G.global_argv[0], |
| 6762 | G.global_argv + 1, |
| 6763 | NULL); |
| 6764 | # endif |
| 6765 | } |
| 6766 | |
| 6767 | /* parent */ |
| 6768 | *pid_p = pid; |
| 6769 | # if ENABLE_HUSH_FAST |
| 6770 | G.count_SIGCHLD++; |
| 6771 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6772 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6773 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6774 | # endif |
| 6775 | enable_restore_tty_pgrp_on_exit(); |
| 6776 | # if !BB_MMU |
| 6777 | free(to_free); |
| 6778 | # endif |
| 6779 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6780 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | /* Return code is exit status of the process that is run. */ |
| 6784 | static int process_command_subs(o_string *dest, const char *s) |
| 6785 | { |
| 6786 | FILE *fp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6787 | pid_t pid; |
| 6788 | int status, ch, eol_cnt; |
| 6789 | |
| 6790 | fp = generate_stream_from_string(s, &pid); |
| 6791 | |
| 6792 | /* Now send results of command back into original context */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6793 | eol_cnt = 0; |
Denys Vlasenko | aa617ac | 2018-02-13 15:30:13 +0100 | [diff] [blame] | 6794 | while ((ch = getc(fp)) != EOF) { |
| 6795 | if (ch == '\0') |
| 6796 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6797 | if (ch == '\n') { |
| 6798 | eol_cnt++; |
| 6799 | continue; |
| 6800 | } |
| 6801 | while (eol_cnt) { |
| 6802 | o_addchr(dest, '\n'); |
| 6803 | eol_cnt--; |
| 6804 | } |
| 6805 | o_addQchr(dest, ch); |
| 6806 | } |
| 6807 | |
| 6808 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6809 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6810 | /* We need to extract exitcode. Test case |
| 6811 | * "true; echo `sleep 1; false` $?" |
| 6812 | * should print 1 */ |
| 6813 | safe_waitpid(pid, &status, 0); |
| 6814 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6815 | return WEXITSTATUS(status); |
| 6816 | } |
| 6817 | #endif /* ENABLE_HUSH_TICK */ |
| 6818 | |
| 6819 | |
| 6820 | static void setup_heredoc(struct redir_struct *redir) |
| 6821 | { |
| 6822 | struct fd_pair pair; |
| 6823 | pid_t pid; |
| 6824 | int len, written; |
| 6825 | /* the _body_ of heredoc (misleading field name) */ |
| 6826 | const char *heredoc = redir->rd_filename; |
| 6827 | char *expanded; |
| 6828 | #if !BB_MMU |
| 6829 | char **to_free; |
| 6830 | #endif |
| 6831 | |
| 6832 | expanded = NULL; |
| 6833 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6834 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6835 | if (expanded) |
| 6836 | heredoc = expanded; |
| 6837 | } |
| 6838 | len = strlen(heredoc); |
| 6839 | |
| 6840 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6841 | xpiped_pair(pair); |
| 6842 | xmove_fd(pair.rd, redir->rd_fd); |
| 6843 | |
| 6844 | /* Try writing without forking. Newer kernels have |
| 6845 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6846 | ndelay_on(pair.wr); |
| 6847 | while (1) { |
| 6848 | written = write(pair.wr, heredoc, len); |
| 6849 | if (written <= 0) |
| 6850 | break; |
| 6851 | len -= written; |
| 6852 | if (len == 0) { |
| 6853 | close(pair.wr); |
| 6854 | free(expanded); |
| 6855 | return; |
| 6856 | } |
| 6857 | heredoc += written; |
| 6858 | } |
| 6859 | ndelay_off(pair.wr); |
| 6860 | |
| 6861 | /* Okay, pipe buffer was not big enough */ |
| 6862 | /* Note: we must not create a stray child (bastard? :) |
| 6863 | * for the unsuspecting parent process. Child creates a grandchild |
| 6864 | * and exits before parent execs the process which consumes heredoc |
| 6865 | * (that exec happens after we return from this function) */ |
| 6866 | #if !BB_MMU |
| 6867 | to_free = NULL; |
| 6868 | #endif |
| 6869 | pid = xvfork(); |
| 6870 | if (pid == 0) { |
| 6871 | /* child */ |
| 6872 | disable_restore_tty_pgrp_on_exit(); |
| 6873 | pid = BB_MMU ? xfork() : xvfork(); |
| 6874 | if (pid != 0) |
| 6875 | _exit(0); |
| 6876 | /* grandchild */ |
| 6877 | close(redir->rd_fd); /* read side of the pipe */ |
| 6878 | #if BB_MMU |
| 6879 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6880 | _exit(0); |
| 6881 | #else |
| 6882 | /* Delegate blocking writes to another process */ |
| 6883 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6884 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6885 | #endif |
| 6886 | } |
| 6887 | /* parent */ |
| 6888 | #if ENABLE_HUSH_FAST |
| 6889 | G.count_SIGCHLD++; |
| 6890 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6891 | #endif |
| 6892 | enable_restore_tty_pgrp_on_exit(); |
| 6893 | #if !BB_MMU |
| 6894 | free(to_free); |
| 6895 | #endif |
| 6896 | close(pair.wr); |
| 6897 | free(expanded); |
| 6898 | wait(NULL); /* wait till child has died */ |
| 6899 | } |
| 6900 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6901 | struct squirrel { |
| 6902 | int orig_fd; |
| 6903 | int moved_to; |
| 6904 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 6905 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 6906 | }; |
| 6907 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6908 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 6909 | { |
| 6910 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 6911 | sq[i].orig_fd = orig; |
| 6912 | sq[i].moved_to = moved; |
| 6913 | sq[i+1].orig_fd = -1; /* end marker */ |
| 6914 | return sq; |
| 6915 | } |
| 6916 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6917 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 6918 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6919 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6920 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6921 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 6922 | i = 0; |
| 6923 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6924 | /* If we collide with an already moved fd... */ |
| 6925 | if (fd == sq[i].moved_to) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 6926 | sq[i].moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6927 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 6928 | if (sq[i].moved_to < 0) /* what? */ |
| 6929 | xfunc_die(); |
| 6930 | return sq; |
| 6931 | } |
| 6932 | if (fd == sq[i].orig_fd) { |
| 6933 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 6934 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 6935 | return sq; |
| 6936 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6937 | } |
| 6938 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6939 | /* 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] | 6940 | moved_to = dup_CLOEXEC(fd, avoid_fd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6941 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 6942 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6943 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6944 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6945 | } |
| 6946 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6947 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 6948 | { |
| 6949 | int i; |
| 6950 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 6951 | i = 0; |
| 6952 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6953 | /* If we collide with an already moved fd... */ |
| 6954 | if (fd == sq[i].orig_fd) { |
| 6955 | /* Examples: |
| 6956 | * "echo 3>FILE 3>&- 3>FILE" |
| 6957 | * "echo 3>&- 3>FILE" |
| 6958 | * No need for last redirect to insert |
| 6959 | * another "need to close 3" indicator. |
| 6960 | */ |
| 6961 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 6962 | return sq; |
| 6963 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6964 | } |
| 6965 | |
| 6966 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 6967 | return append_squirrel(sq, i, fd, -1); |
| 6968 | } |
| 6969 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6970 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 6971 | * Move all conflicting internally used fds, |
| 6972 | * and remember them so that we can restore them later. |
| 6973 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6974 | 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] | 6975 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6976 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 6977 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6978 | |
| 6979 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6980 | if (fd == G.interactive_fd) { |
| 6981 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6982 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6983 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 6984 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6985 | } |
| 6986 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6987 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 6988 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 6989 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6990 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 6991 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6992 | * "fileno(fd) = new_fd" can't be done. |
| 6993 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6994 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6995 | return 0; |
| 6996 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6997 | /* If this one of script's fds? */ |
| 6998 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 6999 | return 1; /* yes. "we closed fd" */ |
| 7000 | |
| 7001 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 7002 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 7003 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7004 | } |
| 7005 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7006 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7007 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7008 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7009 | int i; |
| 7010 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7011 | if (sq[i].moved_to >= 0) { |
| 7012 | /* We simply die on error */ |
| 7013 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 7014 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 7015 | } else { |
| 7016 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 7017 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 7018 | close(sq[i].orig_fd); |
| 7019 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7020 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7021 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7022 | } |
| 7023 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7024 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 7025 | |
| 7026 | restore_redirected_FILEs(); |
| 7027 | } |
| 7028 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7029 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7030 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7031 | { |
| 7032 | if (G_interactive_fd) |
| 7033 | close(G_interactive_fd); |
| 7034 | close_all_FILE_list(); |
| 7035 | } |
| 7036 | #endif |
| 7037 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7038 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 7039 | { |
| 7040 | int i; |
| 7041 | |
| 7042 | #if ENABLE_HUSH_INTERACTIVE |
| 7043 | if (fd == G.interactive_fd) |
| 7044 | return 1; |
| 7045 | #endif |
| 7046 | /* If this one of script's fds? */ |
| 7047 | if (fd_in_FILEs(fd)) |
| 7048 | return 1; |
| 7049 | |
| 7050 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 7051 | if (fd == sq[i].moved_to) |
| 7052 | return 1; |
| 7053 | } |
| 7054 | return 0; |
| 7055 | } |
| 7056 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7057 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 7058 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7059 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7060 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7061 | struct redir_struct *redir; |
| 7062 | |
| 7063 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7064 | int newfd; |
| 7065 | int closed; |
| 7066 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7067 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7068 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7069 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7070 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 7071 | * of the heredoc */ |
| 7072 | debug_printf_parse("set heredoc '%s'\n", |
| 7073 | redir->rd_filename); |
| 7074 | setup_heredoc(redir); |
| 7075 | continue; |
| 7076 | } |
| 7077 | |
| 7078 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7079 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7080 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7081 | int mode; |
| 7082 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7083 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 7084 | /* |
| 7085 | * Examples: |
| 7086 | * "cmd >" (no filename) |
| 7087 | * "cmd > <file" (2nd redirect starts too early) |
| 7088 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 7089 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7090 | continue; |
| 7091 | } |
| 7092 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7093 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7094 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7095 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7096 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 7097 | /* Error message from open_or_warn can be lost |
| 7098 | * if stderr has been redirected, but bash |
| 7099 | * and ash both lose it as well |
| 7100 | * (though zsh doesn't!) |
| 7101 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7102 | return 1; |
| 7103 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7104 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7105 | /* open() gave us precisely the fd we wanted. |
| 7106 | * This means that this fd was not busy |
| 7107 | * (not opened to anywhere). |
| 7108 | * Remember to close it on restore: |
| 7109 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7110 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 7111 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 7112 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7113 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7114 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 7115 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7116 | } |
| 7117 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7118 | if (newfd == redir->rd_fd) |
| 7119 | continue; |
| 7120 | |
| 7121 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 7122 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 7123 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 7124 | |
| 7125 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 7126 | if (newfd == REDIRFD_CLOSE) { |
| 7127 | /* "N>&-" means "close me" */ |
| 7128 | if (!closed) { |
| 7129 | /* ^^^ optimization: saving may already |
| 7130 | * have closed it. If not... */ |
| 7131 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7132 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7133 | /* Sometimes we do another close on restore, getting EBADF. |
| 7134 | * Consider "echo 3>FILE 3>&-" |
| 7135 | * first redirect remembers "need to close 3", |
| 7136 | * and second redirect closes 3! Restore code then closes 3 again. |
| 7137 | */ |
| 7138 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 7139 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 7140 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 7141 | //errno = EBADF; |
| 7142 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 7143 | newfd = -1; /* same effect as code above */ |
| 7144 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 7145 | xdup2(newfd, redir->rd_fd); |
| 7146 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 7147 | /* "rd_fd > FILE" */ |
| 7148 | close(newfd); |
| 7149 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7150 | } |
| 7151 | } |
| 7152 | return 0; |
| 7153 | } |
| 7154 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7155 | static char *find_in_path(const char *arg) |
| 7156 | { |
| 7157 | char *ret = NULL; |
| 7158 | const char *PATH = get_local_var_value("PATH"); |
| 7159 | |
| 7160 | if (!PATH) |
| 7161 | return NULL; |
| 7162 | |
| 7163 | while (1) { |
| 7164 | const char *end = strchrnul(PATH, ':'); |
| 7165 | int sz = end - PATH; /* must be int! */ |
| 7166 | |
| 7167 | free(ret); |
| 7168 | if (sz != 0) { |
| 7169 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 7170 | } else { |
| 7171 | /* We have xxx::yyyy in $PATH, |
| 7172 | * it means "use current dir" */ |
| 7173 | ret = xstrdup(arg); |
| 7174 | } |
| 7175 | if (access(ret, F_OK) == 0) |
| 7176 | break; |
| 7177 | |
| 7178 | if (*end == '\0') { |
| 7179 | free(ret); |
| 7180 | return NULL; |
| 7181 | } |
| 7182 | PATH = end + 1; |
| 7183 | } |
| 7184 | |
| 7185 | return ret; |
| 7186 | } |
| 7187 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7188 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7189 | const struct built_in_command *x, |
| 7190 | const struct built_in_command *end) |
| 7191 | { |
| 7192 | while (x != end) { |
| 7193 | if (strcmp(name, x->b_cmd) != 0) { |
| 7194 | x++; |
| 7195 | continue; |
| 7196 | } |
| 7197 | debug_printf_exec("found builtin '%s'\n", name); |
| 7198 | return x; |
| 7199 | } |
| 7200 | return NULL; |
| 7201 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7202 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7203 | { |
| 7204 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7205 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7206 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7207 | { |
| 7208 | const struct built_in_command *x = find_builtin1(name); |
| 7209 | if (x) |
| 7210 | return x; |
| 7211 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7212 | } |
| 7213 | |
| 7214 | #if ENABLE_HUSH_FUNCTIONS |
| 7215 | static struct function **find_function_slot(const char *name) |
| 7216 | { |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7217 | struct function *funcp; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7218 | struct function **funcpp = &G.top_func; |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7219 | |
| 7220 | while ((funcp = *funcpp) != NULL) { |
| 7221 | if (strcmp(name, funcp->name) == 0) { |
| 7222 | debug_printf_exec("found function '%s'\n", name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7223 | break; |
| 7224 | } |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7225 | funcpp = &funcp->next; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7226 | } |
| 7227 | return funcpp; |
| 7228 | } |
| 7229 | |
Denys Vlasenko | 33f7c8f | 2018-03-06 17:21:57 +0100 | [diff] [blame] | 7230 | static ALWAYS_INLINE const struct function *find_function(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7231 | { |
| 7232 | const struct function *funcp = *find_function_slot(name); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7233 | return funcp; |
| 7234 | } |
| 7235 | |
| 7236 | /* Note: takes ownership on name ptr */ |
| 7237 | static struct function *new_function(char *name) |
| 7238 | { |
| 7239 | struct function **funcpp = find_function_slot(name); |
| 7240 | struct function *funcp = *funcpp; |
| 7241 | |
| 7242 | if (funcp != NULL) { |
| 7243 | struct command *cmd = funcp->parent_cmd; |
| 7244 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7245 | if (!cmd) { |
| 7246 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7247 | free(funcp->name); |
| 7248 | /* Note: if !funcp->body, do not free body_as_string! |
| 7249 | * This is a special case of "-F name body" function: |
| 7250 | * body_as_string was not malloced! */ |
| 7251 | if (funcp->body) { |
| 7252 | free_pipe_list(funcp->body); |
| 7253 | # if !BB_MMU |
| 7254 | free(funcp->body_as_string); |
| 7255 | # endif |
| 7256 | } |
| 7257 | } else { |
| 7258 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7259 | cmd->argv[0] = funcp->name; |
| 7260 | cmd->group = funcp->body; |
| 7261 | # if !BB_MMU |
| 7262 | cmd->group_as_string = funcp->body_as_string; |
| 7263 | # endif |
| 7264 | } |
| 7265 | } else { |
| 7266 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7267 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7268 | /*funcp->next = NULL;*/ |
| 7269 | } |
| 7270 | |
| 7271 | funcp->name = name; |
| 7272 | return funcp; |
| 7273 | } |
| 7274 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7275 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7276 | static void unset_func(const char *name) |
| 7277 | { |
| 7278 | struct function **funcpp = find_function_slot(name); |
| 7279 | struct function *funcp = *funcpp; |
| 7280 | |
| 7281 | if (funcp != NULL) { |
| 7282 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7283 | *funcpp = funcp->next; |
| 7284 | /* funcp is unlinked now, deleting it. |
| 7285 | * Note: if !funcp->body, the function was created by |
| 7286 | * "-F name body", do not free ->body_as_string |
| 7287 | * and ->name as they were not malloced. */ |
| 7288 | if (funcp->body) { |
| 7289 | free_pipe_list(funcp->body); |
| 7290 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7291 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7292 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7293 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7294 | } |
| 7295 | free(funcp); |
| 7296 | } |
| 7297 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7298 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7299 | |
| 7300 | # if BB_MMU |
| 7301 | #define exec_function(to_free, funcp, argv) \ |
| 7302 | exec_function(funcp, argv) |
| 7303 | # endif |
| 7304 | static void exec_function(char ***to_free, |
| 7305 | const struct function *funcp, |
| 7306 | char **argv) NORETURN; |
| 7307 | static void exec_function(char ***to_free, |
| 7308 | const struct function *funcp, |
| 7309 | char **argv) |
| 7310 | { |
| 7311 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7312 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7313 | |
| 7314 | argv[0] = G.global_argv[0]; |
| 7315 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7316 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7317 | |
| 7318 | // Example when we are here: "cmd | func" |
| 7319 | // func will run with saved-redirect fds open. |
| 7320 | // $ f() { echo /proc/self/fd/*; } |
| 7321 | // $ true | f |
| 7322 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7323 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7324 | // Same in script: |
| 7325 | // $ . ./SCRIPT |
| 7326 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7327 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7328 | // They are CLOEXEC so external programs won't see them, but |
| 7329 | // for "more correctness" we might want to close those extra fds here: |
| 7330 | //? close_saved_fds_and_FILE_fds(); |
| 7331 | |
| 7332 | /* "we are in function, ok to use return" */ |
| 7333 | G_flag_return_in_progress = -1; |
| 7334 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7335 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7336 | /* On MMU, funcp->body is always non-NULL */ |
| 7337 | n = run_list(funcp->body); |
| 7338 | fflush_all(); |
| 7339 | _exit(n); |
| 7340 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7341 | //? close_saved_fds_and_FILE_fds(); |
| 7342 | |
| 7343 | //TODO: check whether "true | func_with_return" works |
| 7344 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7345 | re_execute_shell(to_free, |
| 7346 | funcp->body_as_string, |
| 7347 | G.global_argv[0], |
| 7348 | argv + 1, |
| 7349 | NULL); |
| 7350 | # endif |
| 7351 | } |
| 7352 | |
| 7353 | static int run_function(const struct function *funcp, char **argv) |
| 7354 | { |
| 7355 | int rc; |
| 7356 | save_arg_t sv; |
| 7357 | smallint sv_flg; |
| 7358 | |
| 7359 | save_and_replace_G_args(&sv, argv); |
| 7360 | |
| 7361 | /* "we are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7362 | sv_flg = G_flag_return_in_progress; |
| 7363 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7364 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7365 | |
| 7366 | /* On MMU, funcp->body is always non-NULL */ |
| 7367 | # if !BB_MMU |
| 7368 | if (!funcp->body) { |
| 7369 | /* Function defined by -F */ |
| 7370 | parse_and_run_string(funcp->body_as_string); |
| 7371 | rc = G.last_exitcode; |
| 7372 | } else |
| 7373 | # endif |
| 7374 | { |
| 7375 | rc = run_list(funcp->body); |
| 7376 | } |
| 7377 | |
| 7378 | # if ENABLE_HUSH_LOCAL |
| 7379 | { |
| 7380 | struct variable *var; |
| 7381 | struct variable **var_pp; |
| 7382 | |
| 7383 | var_pp = &G.top_var; |
| 7384 | while ((var = *var_pp) != NULL) { |
| 7385 | if (var->func_nest_level < G.func_nest_level) { |
| 7386 | var_pp = &var->next; |
| 7387 | continue; |
| 7388 | } |
| 7389 | /* Unexport */ |
| 7390 | if (var->flg_export) |
| 7391 | bb_unsetenv(var->varstr); |
| 7392 | /* Remove from global list */ |
| 7393 | *var_pp = var->next; |
| 7394 | /* Free */ |
| 7395 | if (!var->max_len) |
| 7396 | free(var->varstr); |
| 7397 | free(var); |
| 7398 | } |
| 7399 | G.func_nest_level--; |
| 7400 | } |
| 7401 | # endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7402 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7403 | |
| 7404 | restore_G_args(&sv, argv); |
| 7405 | |
| 7406 | return rc; |
| 7407 | } |
| 7408 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7409 | |
| 7410 | |
| 7411 | #if BB_MMU |
| 7412 | #define exec_builtin(to_free, x, argv) \ |
| 7413 | exec_builtin(x, argv) |
| 7414 | #else |
| 7415 | #define exec_builtin(to_free, x, argv) \ |
| 7416 | exec_builtin(to_free, argv) |
| 7417 | #endif |
| 7418 | static void exec_builtin(char ***to_free, |
| 7419 | const struct built_in_command *x, |
| 7420 | char **argv) NORETURN; |
| 7421 | static void exec_builtin(char ***to_free, |
| 7422 | const struct built_in_command *x, |
| 7423 | char **argv) |
| 7424 | { |
| 7425 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7426 | int rcode; |
| 7427 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7428 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7429 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7430 | fflush_all(); |
| 7431 | _exit(rcode); |
| 7432 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7433 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7434 | /* On NOMMU, we must never block! |
| 7435 | * Example: { sleep 99 | read line; } & echo Ok |
| 7436 | */ |
| 7437 | re_execute_shell(to_free, |
| 7438 | argv[0], |
| 7439 | G.global_argv[0], |
| 7440 | G.global_argv + 1, |
| 7441 | argv); |
| 7442 | #endif |
| 7443 | } |
| 7444 | |
| 7445 | |
| 7446 | static void execvp_or_die(char **argv) NORETURN; |
| 7447 | static void execvp_or_die(char **argv) |
| 7448 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7449 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7450 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7451 | /* Don't propagate SIG_IGN to the child */ |
| 7452 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7453 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7454 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7455 | e = 2; |
| 7456 | if (errno == EACCES) e = 126; |
| 7457 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7458 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7459 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7460 | } |
| 7461 | |
| 7462 | #if ENABLE_HUSH_MODE_X |
| 7463 | static void dump_cmd_in_x_mode(char **argv) |
| 7464 | { |
| 7465 | if (G_x_mode && argv) { |
| 7466 | /* We want to output the line in one write op */ |
| 7467 | char *buf, *p; |
| 7468 | int len; |
| 7469 | int n; |
| 7470 | |
| 7471 | len = 3; |
| 7472 | n = 0; |
| 7473 | while (argv[n]) |
| 7474 | len += strlen(argv[n++]) + 1; |
| 7475 | buf = xmalloc(len); |
| 7476 | buf[0] = '+'; |
| 7477 | p = buf + 1; |
| 7478 | n = 0; |
| 7479 | while (argv[n]) |
| 7480 | p += sprintf(p, " %s", argv[n++]); |
| 7481 | *p++ = '\n'; |
| 7482 | *p = '\0'; |
| 7483 | fputs(buf, stderr); |
| 7484 | free(buf); |
| 7485 | } |
| 7486 | } |
| 7487 | #else |
| 7488 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7489 | #endif |
| 7490 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7491 | #if ENABLE_HUSH_COMMAND |
| 7492 | static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation) |
| 7493 | { |
| 7494 | char *to_free; |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7495 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7496 | if (!opt_vV) |
| 7497 | return; |
| 7498 | |
| 7499 | to_free = NULL; |
| 7500 | if (!explanation) { |
| 7501 | char *path = getenv("PATH"); |
| 7502 | explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */ |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7503 | if (!explanation) |
| 7504 | _exit(1); /* PROG was not found */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7505 | if (opt_vV != 'V') |
| 7506 | cmd = to_free; /* -v PROG prints "/path/to/PROG" */ |
| 7507 | } |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7508 | printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7509 | free(to_free); |
| 7510 | fflush_all(); |
Denys Vlasenko | afb73a2 | 2018-01-12 16:17:59 +0100 | [diff] [blame] | 7511 | _exit(0); |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7512 | } |
| 7513 | #else |
| 7514 | # define if_command_vV_print_and_exit(a,b,c) ((void)0) |
| 7515 | #endif |
| 7516 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7517 | #if BB_MMU |
| 7518 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7519 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7520 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7521 | pseudo_exec(command, argv_expanded) |
| 7522 | #endif |
| 7523 | |
| 7524 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7525 | * Never returns. |
| 7526 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7527 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7528 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7529 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7530 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7531 | char **argv, int assignment_cnt, |
| 7532 | char **argv_expanded) NORETURN; |
| 7533 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7534 | char **argv, int assignment_cnt, |
| 7535 | char **argv_expanded) |
| 7536 | { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7537 | const struct built_in_command *x; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7538 | char **new_env; |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7539 | #if ENABLE_HUSH_COMMAND |
| 7540 | char opt_vV = 0; |
| 7541 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7542 | |
| 7543 | new_env = expand_assignments(argv, assignment_cnt); |
| 7544 | dump_cmd_in_x_mode(new_env); |
| 7545 | |
| 7546 | if (!argv[assignment_cnt]) { |
| 7547 | /* Case when we are here: ... | var=val | ... |
| 7548 | * (note that we do not exit early, i.e., do not optimize out |
| 7549 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7550 | */ |
| 7551 | free_strings(new_env); |
| 7552 | _exit(EXIT_SUCCESS); |
| 7553 | } |
| 7554 | |
| 7555 | #if BB_MMU |
| 7556 | set_vars_and_save_old(new_env); |
| 7557 | free(new_env); /* optional */ |
| 7558 | /* we can also destroy set_vars_and_save_old's return value, |
| 7559 | * to save memory */ |
| 7560 | #else |
| 7561 | nommu_save->new_env = new_env; |
| 7562 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 7563 | #endif |
| 7564 | |
| 7565 | if (argv_expanded) { |
| 7566 | argv = argv_expanded; |
| 7567 | } else { |
| 7568 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7569 | #if !BB_MMU |
| 7570 | nommu_save->argv = argv; |
| 7571 | #endif |
| 7572 | } |
| 7573 | dump_cmd_in_x_mode(argv); |
| 7574 | |
| 7575 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7576 | if (strchr(argv[0], '/') != NULL) |
| 7577 | goto skip; |
| 7578 | #endif |
| 7579 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7580 | #if ENABLE_HUSH_FUNCTIONS |
| 7581 | /* Check if the command matches any functions (this goes before bltins) */ |
| 7582 | { |
| 7583 | const struct function *funcp = find_function(argv[0]); |
| 7584 | if (funcp) { |
| 7585 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 7586 | } |
| 7587 | } |
| 7588 | #endif |
| 7589 | |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7590 | #if ENABLE_HUSH_COMMAND |
| 7591 | /* "command BAR": run BAR without looking it up among functions |
| 7592 | * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1 |
| 7593 | * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}" |
| 7594 | */ |
| 7595 | while (strcmp(argv[0], "command") == 0 && argv[1]) { |
| 7596 | char *p; |
| 7597 | |
| 7598 | argv++; |
| 7599 | p = *argv; |
| 7600 | if (p[0] != '-' || !p[1]) |
| 7601 | continue; /* bash allows "command command command [-OPT] BAR" */ |
| 7602 | |
| 7603 | for (;;) { |
| 7604 | p++; |
| 7605 | switch (*p) { |
| 7606 | case '\0': |
| 7607 | argv++; |
| 7608 | p = *argv; |
| 7609 | if (p[0] != '-' || !p[1]) |
| 7610 | goto after_opts; |
| 7611 | continue; /* next arg is also -opts, process it too */ |
| 7612 | case 'v': |
| 7613 | case 'V': |
| 7614 | opt_vV = *p; |
| 7615 | continue; |
| 7616 | default: |
| 7617 | bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]); |
| 7618 | } |
| 7619 | } |
| 7620 | } |
| 7621 | after_opts: |
| 7622 | # if ENABLE_HUSH_FUNCTIONS |
| 7623 | if (opt_vV && find_function(argv[0])) |
| 7624 | if_command_vV_print_and_exit(opt_vV, argv[0], "a function"); |
| 7625 | # endif |
| 7626 | #endif |
| 7627 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7628 | /* Check if the command matches any of the builtins. |
| 7629 | * Depending on context, this might be redundant. But it's |
| 7630 | * easier to waste a few CPU cycles than it is to figure out |
| 7631 | * if this is one of those cases. |
| 7632 | */ |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7633 | /* Why "BB_MMU ? :" difference in logic? - |
| 7634 | * On NOMMU, it is more expensive to re-execute shell |
| 7635 | * just in order to run echo or test builtin. |
| 7636 | * It's better to skip it here and run corresponding |
| 7637 | * non-builtin later. */ |
| 7638 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7639 | if (x) { |
| 7640 | if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin"); |
| 7641 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7642 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7643 | |
| 7644 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7645 | /* Check if the command matches any busybox applets */ |
| 7646 | { |
| 7647 | int a = find_applet_by_name(argv[0]); |
| 7648 | if (a >= 0) { |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7649 | if_command_vV_print_and_exit(opt_vV, argv[0], "an applet"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7650 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7651 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7652 | /* Do not leak open fds from opened script files etc. |
| 7653 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7654 | * should not show tty fd open. |
| 7655 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7656 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7657 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 7658 | //This casuses test failures in |
| 7659 | //redir_children_should_not_see_saved_fd_2.tests |
| 7660 | //redir_children_should_not_see_saved_fd_3.tests |
| 7661 | //if you replace "busybox find" with just "find" in them |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7662 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7663 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7664 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7665 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7666 | } |
| 7667 | # endif |
| 7668 | /* Re-exec ourselves */ |
| 7669 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7670 | /* Don't propagate SIG_IGN to the child */ |
| 7671 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7672 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7673 | execv(bb_busybox_exec_path, argv); |
| 7674 | /* If they called chroot or otherwise made the binary no longer |
| 7675 | * executable, fall through */ |
| 7676 | } |
| 7677 | } |
| 7678 | #endif |
| 7679 | |
| 7680 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7681 | skip: |
| 7682 | #endif |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 7683 | if_command_vV_print_and_exit(opt_vV, argv[0], NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7684 | execvp_or_die(argv); |
| 7685 | } |
| 7686 | |
| 7687 | /* Called after [v]fork() in run_pipe |
| 7688 | */ |
| 7689 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7690 | struct command *command, |
| 7691 | char **argv_expanded) NORETURN; |
| 7692 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7693 | struct command *command, |
| 7694 | char **argv_expanded) |
| 7695 | { |
| 7696 | if (command->argv) { |
| 7697 | pseudo_exec_argv(nommu_save, command->argv, |
| 7698 | command->assignment_cnt, argv_expanded); |
| 7699 | } |
| 7700 | |
| 7701 | if (command->group) { |
| 7702 | /* Cases when we are here: |
| 7703 | * ( list ) |
| 7704 | * { list } & |
| 7705 | * ... | ( list ) | ... |
| 7706 | * ... | { list } | ... |
| 7707 | */ |
| 7708 | #if BB_MMU |
| 7709 | int rcode; |
| 7710 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7711 | reset_traps_to_defaults(); |
| 7712 | rcode = run_list(command->group); |
| 7713 | /* OK to leak memory by not calling free_pipe_list, |
| 7714 | * since this process is about to exit */ |
| 7715 | _exit(rcode); |
| 7716 | #else |
| 7717 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7718 | command->group_as_string, |
| 7719 | G.global_argv[0], |
| 7720 | G.global_argv + 1, |
| 7721 | NULL); |
| 7722 | #endif |
| 7723 | } |
| 7724 | |
| 7725 | /* Case when we are here: ... | >file */ |
| 7726 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7727 | _exit(EXIT_SUCCESS); |
| 7728 | } |
| 7729 | |
| 7730 | #if ENABLE_HUSH_JOB |
| 7731 | static const char *get_cmdtext(struct pipe *pi) |
| 7732 | { |
| 7733 | char **argv; |
| 7734 | char *p; |
| 7735 | int len; |
| 7736 | |
| 7737 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7738 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7739 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7740 | if (pi->cmdtext) |
| 7741 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7742 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7743 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7744 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7745 | pi->cmdtext = xzalloc(1); |
| 7746 | return pi->cmdtext; |
| 7747 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7748 | len = 0; |
| 7749 | do { |
| 7750 | len += strlen(*argv) + 1; |
| 7751 | } while (*++argv); |
| 7752 | p = xmalloc(len); |
| 7753 | pi->cmdtext = p; |
| 7754 | argv = pi->cmds[0].argv; |
| 7755 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7756 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7757 | *p++ = ' '; |
| 7758 | } while (*++argv); |
| 7759 | p[-1] = '\0'; |
| 7760 | return pi->cmdtext; |
| 7761 | } |
| 7762 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7763 | static void remove_job_from_table(struct pipe *pi) |
| 7764 | { |
| 7765 | struct pipe *prev_pipe; |
| 7766 | |
| 7767 | if (pi == G.job_list) { |
| 7768 | G.job_list = pi->next; |
| 7769 | } else { |
| 7770 | prev_pipe = G.job_list; |
| 7771 | while (prev_pipe->next != pi) |
| 7772 | prev_pipe = prev_pipe->next; |
| 7773 | prev_pipe->next = pi->next; |
| 7774 | } |
| 7775 | G.last_jobid = 0; |
| 7776 | if (G.job_list) |
| 7777 | G.last_jobid = G.job_list->jobid; |
| 7778 | } |
| 7779 | |
| 7780 | static void delete_finished_job(struct pipe *pi) |
| 7781 | { |
| 7782 | remove_job_from_table(pi); |
| 7783 | free_pipe(pi); |
| 7784 | } |
| 7785 | |
| 7786 | static void clean_up_last_dead_job(void) |
| 7787 | { |
| 7788 | if (G.job_list && !G.job_list->alive_cmds) |
| 7789 | delete_finished_job(G.job_list); |
| 7790 | } |
| 7791 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7792 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7793 | { |
| 7794 | struct pipe *job, **jobp; |
| 7795 | int i; |
| 7796 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7797 | clean_up_last_dead_job(); |
| 7798 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7799 | /* Find the end of the list, and find next job ID to use */ |
| 7800 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7801 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7802 | while ((job = *jobp) != NULL) { |
| 7803 | if (job->jobid > i) |
| 7804 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7805 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7806 | } |
| 7807 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7808 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7809 | /* Create a new job struct at the end */ |
| 7810 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7811 | job->next = NULL; |
| 7812 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7813 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7814 | for (i = 0; i < pi->num_cmds; i++) { |
| 7815 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7816 | /* all other fields are not used and stay zero */ |
| 7817 | } |
| 7818 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7819 | |
| 7820 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7821 | 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] | 7822 | G.last_jobid = job->jobid; |
| 7823 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7824 | #endif /* JOB */ |
| 7825 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7826 | static int job_exited_or_stopped(struct pipe *pi) |
| 7827 | { |
| 7828 | int rcode, i; |
| 7829 | |
| 7830 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7831 | return -1; |
| 7832 | |
| 7833 | /* All processes in fg pipe have exited or stopped */ |
| 7834 | rcode = 0; |
| 7835 | i = pi->num_cmds; |
| 7836 | while (--i >= 0) { |
| 7837 | rcode = pi->cmds[i].cmd_exitcode; |
| 7838 | /* usually last process gives overall exitstatus, |
| 7839 | * but with "set -o pipefail", last *failed* process does */ |
| 7840 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7841 | break; |
| 7842 | } |
| 7843 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7844 | return rcode; |
| 7845 | } |
| 7846 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7847 | 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] | 7848 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7849 | #if ENABLE_HUSH_JOB |
| 7850 | struct pipe *pi; |
| 7851 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7852 | int i, dead; |
| 7853 | |
| 7854 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7855 | |
| 7856 | #if DEBUG_JOBS |
| 7857 | if (WIFSTOPPED(status)) |
| 7858 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7859 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7860 | if (WIFSIGNALED(status)) |
| 7861 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7862 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7863 | if (WIFEXITED(status)) |
| 7864 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7865 | childpid, WEXITSTATUS(status)); |
| 7866 | #endif |
| 7867 | /* Were we asked to wait for a fg pipe? */ |
| 7868 | if (fg_pipe) { |
| 7869 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7870 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7871 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7872 | int rcode; |
| 7873 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7874 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7875 | if (fg_pipe->cmds[i].pid != childpid) |
| 7876 | continue; |
| 7877 | if (dead) { |
| 7878 | int ex; |
| 7879 | fg_pipe->cmds[i].pid = 0; |
| 7880 | fg_pipe->alive_cmds--; |
| 7881 | ex = WEXITSTATUS(status); |
| 7882 | /* bash prints killer signal's name for *last* |
| 7883 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 7884 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7885 | */ |
| 7886 | if (WIFSIGNALED(status)) { |
| 7887 | int sig = WTERMSIG(status); |
| 7888 | if (i == fg_pipe->num_cmds-1) |
| 7889 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 7890 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 7891 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 7892 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7893 | * Maybe we need to use sig | 128? */ |
| 7894 | ex = sig + 128; |
| 7895 | } |
| 7896 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 7897 | } else { |
| 7898 | fg_pipe->stopped_cmds++; |
| 7899 | } |
| 7900 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 7901 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7902 | rcode = job_exited_or_stopped(fg_pipe); |
| 7903 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7904 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 7905 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 7906 | * and "killall -STOP cat" */ |
| 7907 | if (G_interactive_fd) { |
| 7908 | #if ENABLE_HUSH_JOB |
| 7909 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7910 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7911 | #endif |
| 7912 | return rcode; |
| 7913 | } |
| 7914 | if (fg_pipe->alive_cmds == 0) |
| 7915 | return rcode; |
| 7916 | } |
| 7917 | /* There are still running processes in the fg_pipe */ |
| 7918 | return -1; |
| 7919 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7920 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7921 | } |
| 7922 | |
| 7923 | #if ENABLE_HUSH_JOB |
| 7924 | /* We were asked to wait for bg or orphaned children */ |
| 7925 | /* No need to remember exitcode in this case */ |
| 7926 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7927 | for (i = 0; i < pi->num_cmds; i++) { |
| 7928 | if (pi->cmds[i].pid == childpid) |
| 7929 | goto found_pi_and_prognum; |
| 7930 | } |
| 7931 | } |
| 7932 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 7933 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 7934 | return -1; /* this wasn't a process from fg_pipe */ |
| 7935 | |
| 7936 | found_pi_and_prognum: |
| 7937 | if (dead) { |
| 7938 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 7939 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7940 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 7941 | rcode = 128 + WTERMSIG(status); |
| 7942 | pi->cmds[i].cmd_exitcode = rcode; |
| 7943 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 7944 | G.last_bg_pid_exitcode = rcode; |
| 7945 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7946 | pi->alive_cmds--; |
| 7947 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7948 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7949 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 7950 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7951 | delete_finished_job(pi); |
| 7952 | } else { |
| 7953 | /* |
| 7954 | * bash deletes finished jobs from job table only in interactive mode, |
| 7955 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 7956 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 7957 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 7958 | * We only retain one "dead" job, if it's the single job on the list. |
| 7959 | * This covers most of real-world scenarios where this is useful. |
| 7960 | */ |
| 7961 | if (pi != G.job_list) |
| 7962 | delete_finished_job(pi); |
| 7963 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7964 | } |
| 7965 | } else { |
| 7966 | /* child stopped */ |
| 7967 | pi->stopped_cmds++; |
| 7968 | } |
| 7969 | #endif |
| 7970 | return -1; /* this wasn't a process from fg_pipe */ |
| 7971 | } |
| 7972 | |
| 7973 | /* Check to see if any processes have exited -- if they have, |
| 7974 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7975 | * |
| 7976 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 7977 | * Return its exitcode or zero if stopped. |
| 7978 | * |
| 7979 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 7980 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 7981 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7982 | * or 0 if no children changed status. |
| 7983 | * |
| 7984 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 7985 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7986 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7987 | */ |
| 7988 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 7989 | { |
| 7990 | int attributes; |
| 7991 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7992 | int rcode = 0; |
| 7993 | |
| 7994 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 7995 | |
| 7996 | attributes = WUNTRACED; |
| 7997 | if (fg_pipe == NULL) |
| 7998 | attributes |= WNOHANG; |
| 7999 | |
| 8000 | errno = 0; |
| 8001 | #if ENABLE_HUSH_FAST |
| 8002 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 8003 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 8004 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 8005 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 8006 | /* Avoid doing waitpid syscall if possible */ |
| 8007 | if (!G.we_have_children) { |
| 8008 | errno = ECHILD; |
| 8009 | return -1; |
| 8010 | } |
| 8011 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 8012 | /* We have children, but they did not exit |
| 8013 | * or stop yet (we saw no SIGCHLD) */ |
| 8014 | return 0; |
| 8015 | } |
| 8016 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 8017 | } |
| 8018 | #endif |
| 8019 | |
| 8020 | /* Do we do this right? |
| 8021 | * bash-3.00# sleep 20 | false |
| 8022 | * <ctrl-Z pressed> |
| 8023 | * [3]+ Stopped sleep 20 | false |
| 8024 | * bash-3.00# echo $? |
| 8025 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 8026 | * [hush 1.14.0: yes we do it right] |
| 8027 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8028 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8029 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8030 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8031 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8032 | i = G.count_SIGCHLD; |
| 8033 | #endif |
| 8034 | childpid = waitpid(-1, &status, attributes); |
| 8035 | if (childpid <= 0) { |
| 8036 | if (childpid && errno != ECHILD) |
| 8037 | bb_perror_msg("waitpid"); |
| 8038 | #if ENABLE_HUSH_FAST |
| 8039 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 8040 | G.we_have_children = (childpid == 0); |
| 8041 | G.handled_SIGCHLD = i; |
| 8042 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8043 | } |
| 8044 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8045 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 8046 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8047 | break; |
| 8048 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8049 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 8050 | if (rcode >= 0) { |
| 8051 | /* fg_pipe exited or stopped */ |
| 8052 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8053 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8054 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8055 | 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] | 8056 | rcode = WEXITSTATUS(status); |
| 8057 | if (WIFSIGNALED(status)) |
| 8058 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 8059 | if (WIFSTOPPED(status)) |
| 8060 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 8061 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8062 | rcode++; |
| 8063 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8064 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8065 | /* This wasn't one of our processes, or */ |
| 8066 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8067 | } /* while (waitpid succeeds)... */ |
| 8068 | |
| 8069 | return rcode; |
| 8070 | } |
| 8071 | |
| 8072 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 8073 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8074 | { |
| 8075 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8076 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8077 | if (G_saved_tty_pgrp) { |
| 8078 | /* Job finished, move the shell to the foreground */ |
| 8079 | p = getpgrp(); /* our process group id */ |
| 8080 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 8081 | tcsetpgrp(G_interactive_fd, p); |
| 8082 | } |
| 8083 | return rcode; |
| 8084 | } |
| 8085 | #endif |
| 8086 | |
| 8087 | /* Start all the jobs, but don't wait for anything to finish. |
| 8088 | * See checkjobs(). |
| 8089 | * |
| 8090 | * Return code is normally -1, when the caller has to wait for children |
| 8091 | * to finish to determine the exit status of the pipe. If the pipe |
| 8092 | * is a simple builtin command, however, the action is done by the |
| 8093 | * time run_pipe returns, and the exit code is provided as the |
| 8094 | * return value. |
| 8095 | * |
| 8096 | * Returns -1 only if started some children. IOW: we have to |
| 8097 | * mask out retvals of builtins etc with 0xff! |
| 8098 | * |
| 8099 | * The only case when we do not need to [v]fork is when the pipe |
| 8100 | * is single, non-backgrounded, non-subshell command. Examples: |
| 8101 | * cmd ; ... { list } ; ... |
| 8102 | * cmd && ... { list } && ... |
| 8103 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8104 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8105 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 8106 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 8107 | * |
| 8108 | * Cases when we must fork: |
| 8109 | * non-single: cmd | cmd |
| 8110 | * backgrounded: cmd & { list } & |
| 8111 | * subshell: ( list ) [&] |
| 8112 | */ |
| 8113 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 8114 | #define redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel, argv_expanded) \ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8115 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 8116 | #endif |
| 8117 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 8118 | struct variable **old_vars_p, |
| 8119 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8120 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8121 | char **argv_expanded) |
| 8122 | { |
| 8123 | /* setup_redirects acts on file descriptors, not FILEs. |
| 8124 | * This is perfect for work that comes after exec(). |
| 8125 | * Is it really safe for inline use? Experimentally, |
| 8126 | * things seem to work. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8127 | int rcode = setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8128 | if (rcode == 0) { |
| 8129 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 8130 | *new_env_p = new_env; |
| 8131 | dump_cmd_in_x_mode(new_env); |
| 8132 | dump_cmd_in_x_mode(argv_expanded); |
| 8133 | if (old_vars_p) |
| 8134 | *old_vars_p = set_vars_and_save_old(new_env); |
| 8135 | } |
| 8136 | return rcode; |
| 8137 | } |
| 8138 | static NOINLINE int run_pipe(struct pipe *pi) |
| 8139 | { |
| 8140 | static const char *const null_ptr = NULL; |
| 8141 | |
| 8142 | int cmd_no; |
| 8143 | int next_infd; |
| 8144 | struct command *command; |
| 8145 | char **argv_expanded; |
| 8146 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8147 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8148 | int rcode; |
| 8149 | |
| 8150 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 8151 | debug_enter(); |
| 8152 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 8153 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 8154 | * Result should be 3 lines: q w e, qwe, q w e |
| 8155 | */ |
| 8156 | G.ifs = get_local_var_value("IFS"); |
| 8157 | if (!G.ifs) |
| 8158 | G.ifs = defifs; |
| 8159 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8160 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 8161 | pi->stopped_cmds = 0; |
| 8162 | command = &pi->cmds[0]; |
| 8163 | argv_expanded = NULL; |
| 8164 | |
| 8165 | if (pi->num_cmds != 1 |
| 8166 | || pi->followup == PIPE_BG |
| 8167 | || command->cmd_type == CMD_SUBSHELL |
| 8168 | ) { |
| 8169 | goto must_fork; |
| 8170 | } |
| 8171 | |
| 8172 | pi->alive_cmds = 1; |
| 8173 | |
| 8174 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 8175 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 8176 | |
| 8177 | if (command->group) { |
| 8178 | #if ENABLE_HUSH_FUNCTIONS |
| 8179 | if (command->cmd_type == CMD_FUNCDEF) { |
| 8180 | /* "executing" func () { list } */ |
| 8181 | struct function *funcp; |
| 8182 | |
| 8183 | funcp = new_function(command->argv[0]); |
| 8184 | /* funcp->name is already set to argv[0] */ |
| 8185 | funcp->body = command->group; |
| 8186 | # if !BB_MMU |
| 8187 | funcp->body_as_string = command->group_as_string; |
| 8188 | command->group_as_string = NULL; |
| 8189 | # endif |
| 8190 | command->group = NULL; |
| 8191 | command->argv[0] = NULL; |
| 8192 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 8193 | funcp->parent_cmd = command; |
| 8194 | command->child_func = funcp; |
| 8195 | |
| 8196 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 8197 | debug_leave(); |
| 8198 | return EXIT_SUCCESS; |
| 8199 | } |
| 8200 | #endif |
| 8201 | /* { list } */ |
| 8202 | debug_printf("non-subshell group\n"); |
| 8203 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8204 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8205 | debug_printf_exec(": run_list\n"); |
Denys Vlasenko | d1b8457 | 2018-03-28 18:42:54 +0200 | [diff] [blame] | 8206 | //FIXME: we need to pass squirrel down into run_list() |
| 8207 | //for SH_STANDALONE case, or else this construct: |
| 8208 | // { find /proc/self/fd; true; } >FILE; cmd2 |
| 8209 | //has no way of closing saved fd#1 for "find", |
| 8210 | //and in SH_STANDALONE mode, "find" is not execed, |
| 8211 | //therefore CLOEXEC on saved fd does not help. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8212 | rcode = run_list(command->group) & 0xff; |
| 8213 | } |
| 8214 | restore_redirects(squirrel); |
| 8215 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8216 | debug_leave(); |
| 8217 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8218 | return rcode; |
| 8219 | } |
| 8220 | |
| 8221 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 8222 | { |
| 8223 | const struct built_in_command *x; |
| 8224 | #if ENABLE_HUSH_FUNCTIONS |
| 8225 | const struct function *funcp; |
| 8226 | #else |
| 8227 | enum { funcp = 0 }; |
| 8228 | #endif |
| 8229 | char **new_env = NULL; |
| 8230 | struct variable *old_vars = NULL; |
| 8231 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8232 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8233 | if (G.lineno_var) |
| 8234 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8235 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8236 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8237 | if (argv[command->assignment_cnt] == NULL) { |
| 8238 | /* Assignments, but no command */ |
| 8239 | /* Ensure redirects take effect (that is, create files). |
| 8240 | * Try "a=t >file" */ |
| 8241 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8242 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, &squirrel, /*argv_expanded:*/ NULL); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8243 | /* Set shell variables */ |
| 8244 | if (new_env) { |
| 8245 | argv = new_env; |
| 8246 | while (*argv) { |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8247 | if (set_local_var(*argv, /*flag:*/ 0)) { |
| 8248 | /* assignment to readonly var / putenv error? */ |
| 8249 | rcode = 1; |
| 8250 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8251 | argv++; |
| 8252 | } |
| 8253 | } |
| 8254 | /* Redirect error sets $? to 1. Otherwise, |
| 8255 | * if evaluating assignment value set $?, retain it. |
| 8256 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 8257 | if (rcode == 0) |
| 8258 | rcode = G.last_exitcode; |
| 8259 | /* Exit, _skipping_ variable restoring code: */ |
| 8260 | goto clean_up_and_ret0; |
| 8261 | |
| 8262 | #else /* Older, bigger, but more correct code */ |
| 8263 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8264 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8265 | restore_redirects(squirrel); |
| 8266 | /* Set shell variables */ |
| 8267 | if (G_x_mode) |
| 8268 | bb_putchar_stderr('+'); |
| 8269 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 8270 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8271 | if (G_x_mode) |
| 8272 | fprintf(stderr, " %s", p); |
| 8273 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 8274 | *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8275 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8276 | /* assignment to readonly var / putenv error? */ |
| 8277 | rcode = 1; |
| 8278 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8279 | argv++; |
| 8280 | } |
| 8281 | if (G_x_mode) |
| 8282 | bb_putchar_stderr('\n'); |
| 8283 | /* Redirect error sets $? to 1. Otherwise, |
| 8284 | * if evaluating assignment value set $?, retain it. |
| 8285 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 8286 | if (rcode == 0) |
| 8287 | rcode = G.last_exitcode; |
| 8288 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8289 | debug_leave(); |
| 8290 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8291 | return rcode; |
| 8292 | #endif |
| 8293 | } |
| 8294 | |
| 8295 | /* Expand the rest into (possibly) many strings each */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 8296 | #if BASH_TEST2 |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8297 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8298 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8299 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8300 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8301 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8302 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 8303 | } |
| 8304 | |
| 8305 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 8306 | if (!argv_expanded[0]) { |
| 8307 | free(argv_expanded); |
| 8308 | debug_leave(); |
| 8309 | return G.last_exitcode; |
| 8310 | } |
| 8311 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8312 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8313 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
| 8314 | funcp = find_function(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8315 | #endif |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8316 | x = NULL; |
| 8317 | if (!funcp) |
| 8318 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8319 | if (x || funcp) { |
| 8320 | if (!funcp) { |
| 8321 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8322 | debug_printf("exec with redirects only\n"); |
| 8323 | rcode = setup_redirects(command, NULL); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8324 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8325 | goto clean_up_and_ret1; |
| 8326 | } |
| 8327 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8328 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8329 | if (rcode == 0) { |
| 8330 | if (!funcp) { |
| 8331 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8332 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8333 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8334 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8335 | fflush_all(); |
| 8336 | } |
| 8337 | #if ENABLE_HUSH_FUNCTIONS |
| 8338 | else { |
| 8339 | # if ENABLE_HUSH_LOCAL |
| 8340 | struct variable **sv; |
| 8341 | sv = G.shadowed_vars_pp; |
| 8342 | G.shadowed_vars_pp = &old_vars; |
| 8343 | # endif |
| 8344 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8345 | funcp->name, argv_expanded[1]); |
| 8346 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 8347 | # if ENABLE_HUSH_LOCAL |
| 8348 | G.shadowed_vars_pp = sv; |
| 8349 | # endif |
| 8350 | } |
| 8351 | #endif |
| 8352 | } |
| 8353 | clean_up_and_ret: |
| 8354 | unset_vars(new_env); |
| 8355 | add_vars(old_vars); |
| 8356 | /* clean_up_and_ret0: */ |
| 8357 | restore_redirects(squirrel); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8358 | /* |
| 8359 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8360 | * with FEATURE_SH_NOFORK=y. |
| 8361 | */ |
| 8362 | if (!funcp) { |
| 8363 | /* It was builtin or nofork. |
| 8364 | * if this would be a real fork/execed program, |
| 8365 | * it should have died if a fatal sig was received. |
| 8366 | * But OTOH, there was no separate process, |
| 8367 | * the sig was sent to _shell_, not to non-existing |
| 8368 | * child. |
| 8369 | * Let's just handle ^C only, this one is obvious: |
| 8370 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8371 | * during builtin/nofork. |
| 8372 | */ |
| 8373 | if (sigismember(&G.pending_set, SIGINT)) |
| 8374 | rcode = 128 + SIGINT; |
| 8375 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8376 | clean_up_and_ret1: |
| 8377 | free(argv_expanded); |
| 8378 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8379 | debug_leave(); |
| 8380 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8381 | return rcode; |
| 8382 | } |
| 8383 | |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8384 | if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8385 | int n = find_applet_by_name(argv_expanded[0]); |
| 8386 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8387 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, &squirrel, argv_expanded); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8388 | if (rcode == 0) { |
| 8389 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8390 | argv_expanded[0], argv_expanded[1]); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8391 | /* |
| 8392 | * Note: signals (^C) can't interrupt here. |
| 8393 | * We remember them and they will be acted upon |
| 8394 | * after applet returns. |
| 8395 | * This makes applets which can run for a long time |
| 8396 | * and/or wait for user input ineligible for NOFORK: |
| 8397 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8398 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8399 | rcode = run_nofork_applet(n, argv_expanded); |
| 8400 | } |
| 8401 | goto clean_up_and_ret; |
| 8402 | } |
| 8403 | } |
| 8404 | /* It is neither builtin nor applet. We must fork. */ |
| 8405 | } |
| 8406 | |
| 8407 | must_fork: |
| 8408 | /* NB: argv_expanded may already be created, and that |
| 8409 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8410 | * use argv_expanded if it's non-NULL */ |
| 8411 | |
| 8412 | /* Going to fork a child per each pipe member */ |
| 8413 | pi->alive_cmds = 0; |
| 8414 | next_infd = 0; |
| 8415 | |
| 8416 | cmd_no = 0; |
| 8417 | while (cmd_no < pi->num_cmds) { |
| 8418 | struct fd_pair pipefds; |
| 8419 | #if !BB_MMU |
| 8420 | volatile nommu_save_t nommu_save; |
| 8421 | nommu_save.new_env = NULL; |
| 8422 | nommu_save.old_vars = NULL; |
| 8423 | nommu_save.argv = NULL; |
| 8424 | nommu_save.argv_from_re_execing = NULL; |
| 8425 | #endif |
| 8426 | command = &pi->cmds[cmd_no]; |
| 8427 | cmd_no++; |
| 8428 | if (command->argv) { |
| 8429 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8430 | command->argv[0], command->argv[1]); |
| 8431 | } else { |
| 8432 | debug_printf_exec(": pipe member with no argv\n"); |
| 8433 | } |
| 8434 | |
| 8435 | /* pipes are inserted between pairs of commands */ |
| 8436 | pipefds.rd = 0; |
| 8437 | pipefds.wr = 1; |
| 8438 | if (cmd_no < pi->num_cmds) |
| 8439 | xpiped_pair(pipefds); |
| 8440 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 8441 | #if ENABLE_HUSH_LINENO_VAR |
Denys Vlasenko | b8d076b | 2018-01-19 16:00:57 +0100 | [diff] [blame] | 8442 | if (G.lineno_var) |
| 8443 | strcpy(G.lineno_var + sizeof("LINENO=")-1, utoa(command->lineno)); |
| 8444 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 8445 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8446 | command->pid = BB_MMU ? fork() : vfork(); |
| 8447 | if (!command->pid) { /* child */ |
| 8448 | #if ENABLE_HUSH_JOB |
| 8449 | disable_restore_tty_pgrp_on_exit(); |
| 8450 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8451 | |
| 8452 | /* Every child adds itself to new process group |
| 8453 | * with pgid == pid_of_first_child_in_pipe */ |
| 8454 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8455 | pid_t pgrp; |
| 8456 | pgrp = pi->pgrp; |
| 8457 | if (pgrp < 0) /* true for 1st process only */ |
| 8458 | pgrp = getpid(); |
| 8459 | if (setpgid(0, pgrp) == 0 |
| 8460 | && pi->followup != PIPE_BG |
| 8461 | && G_saved_tty_pgrp /* we have ctty */ |
| 8462 | ) { |
| 8463 | /* We do it in *every* child, not just first, |
| 8464 | * to avoid races */ |
| 8465 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8466 | } |
| 8467 | } |
| 8468 | #endif |
| 8469 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8470 | /* 1st cmd in backgrounded pipe |
| 8471 | * should have its stdin /dev/null'ed */ |
| 8472 | close(0); |
| 8473 | if (open(bb_dev_null, O_RDONLY)) |
| 8474 | xopen("/", O_RDONLY); |
| 8475 | } else { |
| 8476 | xmove_fd(next_infd, 0); |
| 8477 | } |
| 8478 | xmove_fd(pipefds.wr, 1); |
| 8479 | if (pipefds.rd > 1) |
| 8480 | close(pipefds.rd); |
| 8481 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8482 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8483 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8484 | * of cmd1 goes into pipe. |
| 8485 | */ |
| 8486 | if (setup_redirects(command, NULL)) { |
| 8487 | /* Happens when redir file can't be opened: |
| 8488 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8489 | * FOO |
| 8490 | * hush: can't open '/qwe/rty': No such file or directory |
| 8491 | * BAZ |
| 8492 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8493 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8494 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8495 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8496 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8497 | /* Stores to nommu_save list of env vars putenv'ed |
| 8498 | * (NOMMU, on MMU we don't need that) */ |
| 8499 | /* cast away volatility... */ |
| 8500 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8501 | /* pseudo_exec() does not return */ |
| 8502 | } |
| 8503 | |
| 8504 | /* parent or error */ |
| 8505 | #if ENABLE_HUSH_FAST |
| 8506 | G.count_SIGCHLD++; |
| 8507 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8508 | #endif |
| 8509 | enable_restore_tty_pgrp_on_exit(); |
| 8510 | #if !BB_MMU |
| 8511 | /* Clean up after vforked child */ |
| 8512 | free(nommu_save.argv); |
| 8513 | free(nommu_save.argv_from_re_execing); |
| 8514 | unset_vars(nommu_save.new_env); |
| 8515 | add_vars(nommu_save.old_vars); |
| 8516 | #endif |
| 8517 | free(argv_expanded); |
| 8518 | argv_expanded = NULL; |
| 8519 | if (command->pid < 0) { /* [v]fork failed */ |
| 8520 | /* Clearly indicate, was it fork or vfork */ |
| 8521 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8522 | } else { |
| 8523 | pi->alive_cmds++; |
| 8524 | #if ENABLE_HUSH_JOB |
| 8525 | /* Second and next children need to know pid of first one */ |
| 8526 | if (pi->pgrp < 0) |
| 8527 | pi->pgrp = command->pid; |
| 8528 | #endif |
| 8529 | } |
| 8530 | |
| 8531 | if (cmd_no > 1) |
| 8532 | close(next_infd); |
| 8533 | if (cmd_no < pi->num_cmds) |
| 8534 | close(pipefds.wr); |
| 8535 | /* Pass read (output) pipe end to next iteration */ |
| 8536 | next_infd = pipefds.rd; |
| 8537 | } |
| 8538 | |
| 8539 | if (!pi->alive_cmds) { |
| 8540 | debug_leave(); |
| 8541 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8542 | return 1; |
| 8543 | } |
| 8544 | |
| 8545 | debug_leave(); |
| 8546 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8547 | return -1; |
| 8548 | } |
| 8549 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8550 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8551 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8552 | static int run_list(struct pipe *pi) |
| 8553 | { |
| 8554 | #if ENABLE_HUSH_CASE |
| 8555 | char *case_word = NULL; |
| 8556 | #endif |
| 8557 | #if ENABLE_HUSH_LOOPS |
| 8558 | struct pipe *loop_top = NULL; |
| 8559 | char **for_lcur = NULL; |
| 8560 | char **for_list = NULL; |
| 8561 | #endif |
| 8562 | smallint last_followup; |
| 8563 | smalluint rcode; |
| 8564 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8565 | smalluint cond_code = 0; |
| 8566 | #else |
| 8567 | enum { cond_code = 0 }; |
| 8568 | #endif |
| 8569 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8570 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8571 | smallint last_rword; /* ditto */ |
| 8572 | #endif |
| 8573 | |
| 8574 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8575 | debug_enter(); |
| 8576 | |
| 8577 | #if ENABLE_HUSH_LOOPS |
| 8578 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8579 | { |
| 8580 | struct pipe *cpipe; |
| 8581 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8582 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8583 | continue; |
| 8584 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8585 | if (cpipe->next == NULL) { |
| 8586 | syntax_error("malformed for"); |
| 8587 | debug_leave(); |
| 8588 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8589 | return 1; |
| 8590 | } |
| 8591 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8592 | if (cpipe->next->res_word == RES_DO) |
| 8593 | continue; |
| 8594 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8595 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8596 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8597 | ) { |
| 8598 | syntax_error("malformed for"); |
| 8599 | debug_leave(); |
| 8600 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8601 | return 1; |
| 8602 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8603 | } |
| 8604 | } |
| 8605 | #endif |
| 8606 | |
| 8607 | /* Past this point, all code paths should jump to ret: label |
| 8608 | * in order to return, no direct "return" statements please. |
| 8609 | * This helps to ensure that no memory is leaked. */ |
| 8610 | |
| 8611 | #if ENABLE_HUSH_JOB |
| 8612 | G.run_list_level++; |
| 8613 | #endif |
| 8614 | |
| 8615 | #if HAS_KEYWORDS |
| 8616 | rword = RES_NONE; |
| 8617 | last_rword = RES_XXXX; |
| 8618 | #endif |
| 8619 | last_followup = PIPE_SEQ; |
| 8620 | rcode = G.last_exitcode; |
| 8621 | |
| 8622 | /* Go through list of pipes, (maybe) executing them. */ |
| 8623 | 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] | 8624 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8625 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8626 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8627 | if (G.flag_SIGINT) |
| 8628 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8629 | if (G_flag_return_in_progress == 1) |
| 8630 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8631 | |
| 8632 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8633 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8634 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8635 | |
| 8636 | sv_errexit_depth = G.errexit_depth; |
Denys Vlasenko | 82d1c1f | 2017-12-31 17:30:02 +0100 | [diff] [blame] | 8637 | if ( |
| 8638 | #if ENABLE_HUSH_IF |
| 8639 | rword == RES_IF || rword == RES_ELIF || |
| 8640 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8641 | pi->followup != PIPE_SEQ |
| 8642 | ) { |
| 8643 | G.errexit_depth++; |
| 8644 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8645 | #if ENABLE_HUSH_LOOPS |
| 8646 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8647 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8648 | ) { |
| 8649 | /* start of a loop: remember where loop starts */ |
| 8650 | loop_top = pi; |
| 8651 | G.depth_of_loop++; |
| 8652 | } |
| 8653 | #endif |
| 8654 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8655 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8656 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8657 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8658 | ) { |
| 8659 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8660 | * and we should not execute CMD */ |
| 8661 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8662 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8663 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8664 | } |
| 8665 | } |
| 8666 | last_followup = pi->followup; |
| 8667 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8668 | #if ENABLE_HUSH_IF |
| 8669 | if (cond_code) { |
| 8670 | if (rword == RES_THEN) { |
| 8671 | /* if false; then ... fi has exitcode 0! */ |
| 8672 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8673 | /* "if <false> THEN cmd": skip cmd */ |
| 8674 | continue; |
| 8675 | } |
| 8676 | } else { |
| 8677 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8678 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8679 | * skip cmd and all following ones */ |
| 8680 | break; |
| 8681 | } |
| 8682 | } |
| 8683 | #endif |
| 8684 | #if ENABLE_HUSH_LOOPS |
| 8685 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 8686 | if (!for_lcur) { |
| 8687 | /* first loop through for */ |
| 8688 | |
| 8689 | static const char encoded_dollar_at[] ALIGN1 = { |
| 8690 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 8691 | }; /* encoded representation of "$@" */ |
| 8692 | static const char *const encoded_dollar_at_argv[] = { |
| 8693 | encoded_dollar_at, NULL |
| 8694 | }; /* argv list with one element: "$@" */ |
| 8695 | char **vals; |
| 8696 | |
| 8697 | vals = (char**)encoded_dollar_at_argv; |
| 8698 | if (pi->next->res_word == RES_IN) { |
| 8699 | /* if no variable values after "in" we skip "for" */ |
| 8700 | if (!pi->next->cmds[0].argv) { |
| 8701 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8702 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 8703 | break; |
| 8704 | } |
| 8705 | vals = pi->next->cmds[0].argv; |
| 8706 | } /* else: "for var; do..." -> assume "$@" list */ |
| 8707 | /* create list of variable values */ |
| 8708 | debug_print_strings("for_list made from", vals); |
| 8709 | for_list = expand_strvec_to_strvec(vals); |
| 8710 | for_lcur = for_list; |
| 8711 | debug_print_strings("for_list", for_list); |
| 8712 | } |
| 8713 | if (!*for_lcur) { |
| 8714 | /* "for" loop is over, clean up */ |
| 8715 | free(for_list); |
| 8716 | for_list = NULL; |
| 8717 | for_lcur = NULL; |
| 8718 | break; |
| 8719 | } |
| 8720 | /* Insert next value from for_lcur */ |
| 8721 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8722 | 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] | 8723 | continue; |
| 8724 | } |
| 8725 | if (rword == RES_IN) { |
| 8726 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 8727 | } |
| 8728 | if (rword == RES_DONE) { |
| 8729 | continue; /* "done" has no cmds too */ |
| 8730 | } |
| 8731 | #endif |
| 8732 | #if ENABLE_HUSH_CASE |
| 8733 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8734 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8735 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8736 | unbackslash(case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8737 | continue; |
| 8738 | } |
| 8739 | if (rword == RES_MATCH) { |
| 8740 | char **argv; |
| 8741 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8742 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8743 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 8744 | break; |
| 8745 | /* all prev words didn't match, does this one match? */ |
| 8746 | argv = pi->cmds->argv; |
| 8747 | while (*argv) { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8748 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8749 | /* TODO: which FNM_xxx flags to use? */ |
| 8750 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8751 | 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] | 8752 | free(pattern); |
| 8753 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8754 | free(case_word); |
| 8755 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8756 | break; |
| 8757 | } |
| 8758 | argv++; |
| 8759 | } |
| 8760 | continue; |
| 8761 | } |
| 8762 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8763 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8764 | if (cond_code != 0) |
| 8765 | continue; /* not matched yet, skip this pipe */ |
| 8766 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8767 | if (rword == RES_ESAC) { |
| 8768 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 8769 | if (case_word) { |
| 8770 | /* "case" did not match anything: still set $? (to 0) */ |
| 8771 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8772 | } |
| 8773 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8774 | #endif |
| 8775 | /* Just pressing <enter> in shell should check for jobs. |
| 8776 | * OTOH, in non-interactive shell this is useless |
| 8777 | * and only leads to extra job checks */ |
| 8778 | if (pi->num_cmds == 0) { |
| 8779 | if (G_interactive_fd) |
| 8780 | goto check_jobs_and_continue; |
| 8781 | continue; |
| 8782 | } |
| 8783 | |
| 8784 | /* After analyzing all keywords and conditions, we decided |
| 8785 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8786 | * after run_pipe to collect any background children, |
| 8787 | * even if list execution is to be stopped. */ |
| 8788 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8789 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8790 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8791 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8792 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8793 | if (r != -1) { |
| 8794 | /* We ran a builtin, function, or group. |
| 8795 | * rcode is already known |
| 8796 | * and we don't need to wait for anything. */ |
| 8797 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8798 | G.last_exitcode = rcode; |
| 8799 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8800 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8801 | /* Was it "break" or "continue"? */ |
| 8802 | if (G.flag_break_continue) { |
| 8803 | smallint fbc = G.flag_break_continue; |
| 8804 | /* We might fall into outer *loop*, |
| 8805 | * don't want to break it too */ |
| 8806 | if (loop_top) { |
| 8807 | G.depth_break_continue--; |
| 8808 | if (G.depth_break_continue == 0) |
| 8809 | G.flag_break_continue = 0; |
| 8810 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 8811 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 8812 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8813 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8814 | break; |
| 8815 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8816 | /* "continue": simulate end of loop */ |
| 8817 | rword = RES_DONE; |
| 8818 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8819 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8820 | #endif |
| 8821 | if (G_flag_return_in_progress == 1) { |
| 8822 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 8823 | break; |
| 8824 | } |
| 8825 | } else if (pi->followup == PIPE_BG) { |
| 8826 | /* What does bash do with attempts to background builtins? */ |
| 8827 | /* even bash 3.2 doesn't do that well with nested bg: |
| 8828 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 8829 | * I'm NOT treating inner &'s as jobs */ |
| 8830 | #if ENABLE_HUSH_JOB |
| 8831 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8832 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8833 | #endif |
| 8834 | /* Last command's pid goes to $! */ |
| 8835 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8836 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8837 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8838 | /* 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] | 8839 | rcode = EXIT_SUCCESS; |
| 8840 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8841 | } else { |
| 8842 | #if ENABLE_HUSH_JOB |
| 8843 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8844 | /* Waits for completion, then fg's main shell */ |
| 8845 | rcode = checkjobs_and_fg_shell(pi); |
| 8846 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8847 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8848 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8849 | #endif |
| 8850 | /* This one just waits for completion */ |
| 8851 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8852 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8853 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8854 | G.last_exitcode = rcode; |
| 8855 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8856 | } |
| 8857 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8858 | /* Handle "set -e" */ |
| 8859 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 8860 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 8861 | if (G.errexit_depth == 0) |
| 8862 | hush_exit(rcode); |
| 8863 | } |
| 8864 | G.errexit_depth = sv_errexit_depth; |
| 8865 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8866 | /* Analyze how result affects subsequent commands */ |
| 8867 | #if ENABLE_HUSH_IF |
| 8868 | if (rword == RES_IF || rword == RES_ELIF) |
| 8869 | cond_code = rcode; |
| 8870 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8871 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8872 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8873 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8874 | #if ENABLE_HUSH_LOOPS |
| 8875 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8876 | if (pi->next |
| 8877 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 8878 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8879 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8880 | if (rword == RES_WHILE) { |
| 8881 | if (rcode) { |
| 8882 | /* "while false; do...done" - exitcode 0 */ |
| 8883 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8884 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8885 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8886 | } |
| 8887 | } |
| 8888 | if (rword == RES_UNTIL) { |
| 8889 | if (!rcode) { |
| 8890 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8891 | break; |
| 8892 | } |
| 8893 | } |
| 8894 | } |
| 8895 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8896 | } /* for (pi) */ |
| 8897 | |
| 8898 | #if ENABLE_HUSH_JOB |
| 8899 | G.run_list_level--; |
| 8900 | #endif |
| 8901 | #if ENABLE_HUSH_LOOPS |
| 8902 | if (loop_top) |
| 8903 | G.depth_of_loop--; |
| 8904 | free(for_list); |
| 8905 | #endif |
| 8906 | #if ENABLE_HUSH_CASE |
| 8907 | free(case_word); |
| 8908 | #endif |
| 8909 | debug_leave(); |
| 8910 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 8911 | return rcode; |
| 8912 | } |
| 8913 | |
| 8914 | /* Select which version we will use */ |
| 8915 | static int run_and_free_list(struct pipe *pi) |
| 8916 | { |
| 8917 | int rcode = 0; |
| 8918 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8919 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8920 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 8921 | rcode = run_list(pi); |
| 8922 | } |
| 8923 | /* free_pipe_list has the side effect of clearing memory. |
| 8924 | * In the long run that function can be merged with run_list, |
| 8925 | * but doing that now would hobble the debugging effort. */ |
| 8926 | free_pipe_list(pi); |
| 8927 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 8928 | return rcode; |
| 8929 | } |
| 8930 | |
| 8931 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8932 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 8933 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8934 | sighandler_t old_handler; |
| 8935 | unsigned sig = 0; |
| 8936 | while ((mask >>= 1) != 0) { |
| 8937 | sig++; |
| 8938 | if (!(mask & 1)) |
| 8939 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8940 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8941 | /* POSIX allows shell to re-enable SIGCHLD |
| 8942 | * even if it was SIG_IGN on entry. |
| 8943 | * Therefore we skip IGN check for it: |
| 8944 | */ |
| 8945 | if (sig == SIGCHLD) |
| 8946 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 8947 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 8948 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 8949 | */ |
| 8950 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8951 | if (old_handler == SIG_IGN) { |
| 8952 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8953 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 8954 | #if ENABLE_HUSH_TRAP |
| 8955 | if (!G_traps) |
| 8956 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 8957 | free(G_traps[sig]); |
| 8958 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 8959 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8960 | } |
| 8961 | } |
| 8962 | } |
| 8963 | |
| 8964 | /* Called a few times only (or even once if "sh -c") */ |
| 8965 | static void install_special_sighandlers(void) |
| 8966 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8967 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8968 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8969 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8970 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8971 | if (G_interactive_fd) { |
| 8972 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 8973 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8974 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8975 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8976 | /* Careful, do not re-install handlers we already installed */ |
| 8977 | if (G.special_sig_mask != mask) { |
| 8978 | unsigned diff = mask & ~G.special_sig_mask; |
| 8979 | G.special_sig_mask = mask; |
| 8980 | install_sighandlers(diff); |
| 8981 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8982 | } |
| 8983 | |
| 8984 | #if ENABLE_HUSH_JOB |
| 8985 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8986 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8987 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8988 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8989 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8990 | |
| 8991 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8992 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8993 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 8994 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8995 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 8996 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8997 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8998 | + (1 << SIGABRT) |
| 8999 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 9000 | + (1 << SIGPIPE) |
| 9001 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9002 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9003 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9004 | * we never want to restore pgrp on exit, and this fn is not called |
| 9005 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9006 | /*+ (1 << SIGHUP )*/ |
| 9007 | /*+ (1 << SIGTERM)*/ |
| 9008 | /*+ (1 << SIGINT )*/ |
| 9009 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9010 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 9011 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9012 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9013 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 9014 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 9015 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9016 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9017 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9018 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9019 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9020 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 9021 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9022 | break; |
| 9023 | case 'x': |
| 9024 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 9025 | break; |
| 9026 | case 'o': |
| 9027 | if (!o_opt) { |
| 9028 | /* "set -+o" without parameter. |
| 9029 | * in bash, set -o produces this output: |
| 9030 | * pipefail off |
| 9031 | * and set +o: |
| 9032 | * set +o pipefail |
| 9033 | * We always use the second form. |
| 9034 | */ |
| 9035 | const char *p = o_opt_strings; |
| 9036 | idx = 0; |
| 9037 | while (*p) { |
| 9038 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 9039 | idx++; |
| 9040 | p += strlen(p) + 1; |
| 9041 | } |
| 9042 | break; |
| 9043 | } |
| 9044 | idx = index_in_strings(o_opt_strings, o_opt); |
| 9045 | if (idx >= 0) { |
| 9046 | G.o_opt[idx] = state; |
| 9047 | break; |
| 9048 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9049 | case 'e': |
| 9050 | G.o_opt[OPT_O_ERREXIT] = state; |
| 9051 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9052 | default: |
| 9053 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 9054 | } |
| 9055 | return EXIT_SUCCESS; |
| 9056 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9057 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 9058 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 9059 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9060 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9061 | enum { |
| 9062 | OPT_login = (1 << 0), |
| 9063 | }; |
| 9064 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9065 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9066 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9067 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9068 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9069 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 9070 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 9071 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9072 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9073 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9074 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 9075 | #if ENABLE_HUSH_FAST |
| 9076 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 9077 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9078 | #if !BB_MMU |
| 9079 | G.argv0_for_re_execing = argv[0]; |
| 9080 | #endif |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9081 | |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9082 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9083 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
| 9084 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9085 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 9086 | shell_ver->flg_export = 1; |
| 9087 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 9088 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 9089 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9090 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9091 | |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9092 | /* Create shell local variables from the values |
| 9093 | * currently living in the environment */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9094 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9095 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 9096 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9097 | if (e) while (*e) { |
| 9098 | char *value = strchr(*e, '='); |
| 9099 | if (value) { /* paranoia */ |
| 9100 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 9101 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 9102 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9103 | cur_var->max_len = strlen(*e); |
| 9104 | cur_var->flg_export = 1; |
| 9105 | } |
| 9106 | e++; |
| 9107 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 9108 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 9109 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 9110 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9111 | |
| 9112 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9113 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9114 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9115 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9116 | /* Set (but not export) HOSTNAME unless already set */ |
| 9117 | if (!get_local_var_value("HOSTNAME")) { |
| 9118 | struct utsname uts; |
| 9119 | uname(&uts); |
| 9120 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 9121 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9122 | /* bash also exports SHLVL and _, |
| 9123 | * and sets (but doesn't export) the following variables: |
| 9124 | * BASH=/bin/bash |
| 9125 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 9126 | * BASH_VERSION='3.2.0(1)-release' |
| 9127 | * HOSTTYPE=i386 |
| 9128 | * MACHTYPE=i386-pc-linux-gnu |
| 9129 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9130 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9131 | * EUID=<NNNNN> |
| 9132 | * UID=<NNNNN> |
| 9133 | * GROUPS=() |
| 9134 | * LINES=<NNN> |
| 9135 | * COLUMNS=<NNN> |
| 9136 | * BASH_ARGC=() |
| 9137 | * BASH_ARGV=() |
| 9138 | * BASH_LINENO=() |
| 9139 | * BASH_SOURCE=() |
| 9140 | * DIRSTACK=() |
| 9141 | * PIPESTATUS=([0]="0") |
| 9142 | * HISTFILE=/<xxx>/.bash_history |
| 9143 | * HISTFILESIZE=500 |
| 9144 | * HISTSIZE=500 |
| 9145 | * MAILCHECK=60 |
| 9146 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 9147 | * SHELL=/bin/bash |
| 9148 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 9149 | * TERM=dumb |
| 9150 | * OPTERR=1 |
| 9151 | * OPTIND=1 |
| 9152 | * IFS=$' \t\n' |
| 9153 | * PS1='\s-\v\$ ' |
| 9154 | * PS2='> ' |
| 9155 | * PS4='+ ' |
| 9156 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 9157 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9158 | |
Denys Vlasenko | 5807e18 | 2018-02-08 19:19:04 +0100 | [diff] [blame] | 9159 | #if ENABLE_HUSH_LINENO_VAR |
| 9160 | if (ENABLE_HUSH_LINENO_VAR) { |
Denys Vlasenko | 6aad1dd | 2018-01-19 15:37:04 +0100 | [diff] [blame] | 9161 | char *p = xasprintf("LINENO=%*s", (int)(sizeof(int)*3), ""); |
| 9162 | set_local_var(p, /*flags*/ 0); |
| 9163 | G.lineno_var = p; /* can't assign before set_local_var("LINENO=...") */ |
| 9164 | } |
| 9165 | #endif |
| 9166 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9167 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 9168 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9169 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 9170 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 9171 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 9172 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 9173 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 9174 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 9175 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9176 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9177 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9178 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9179 | * 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] | 9180 | * in order to intercept (more) signals. |
| 9181 | */ |
| 9182 | |
| 9183 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9184 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9185 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9186 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9187 | while (1) { |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9188 | opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9189 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9190 | "<:$:R:V:" |
| 9191 | # if ENABLE_HUSH_FUNCTIONS |
| 9192 | "F:" |
| 9193 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9194 | #endif |
| 9195 | ); |
| 9196 | if (opt <= 0) |
| 9197 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9198 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9199 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9200 | /* Possibilities: |
| 9201 | * sh ... -c 'script' |
| 9202 | * sh ... -c 'script' ARG0 [ARG1...] |
| 9203 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9204 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9205 | * "" needs to be replaced with NULL |
| 9206 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9207 | * Note: the form without ARG0 never happens: |
| 9208 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9209 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9210 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9211 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9212 | G.root_ppid = getppid(); |
| 9213 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 9214 | G.global_argv = argv + optind; |
| 9215 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9216 | if (builtin_argc) { |
| 9217 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 9218 | const struct built_in_command *x; |
| 9219 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9220 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9221 | x = find_builtin(optarg); |
| 9222 | if (x) { /* paranoia */ |
| 9223 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 9224 | G.global_argv += builtin_argc; |
| 9225 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 9226 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 9227 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9228 | } |
| 9229 | goto final_return; |
| 9230 | } |
| 9231 | if (!G.global_argv[0]) { |
| 9232 | /* -c 'script' (no params): prevent empty $0 */ |
| 9233 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 9234 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 9235 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9236 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9237 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9238 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9239 | goto final_return; |
| 9240 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 9241 | /* Well, we cannot just declare interactiveness, |
| 9242 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9243 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9244 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 9245 | case 's': |
| 9246 | /* "-s" means "read from stdin", but this is how we always |
| 9247 | * operate, so simply do nothing here. */ |
| 9248 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9249 | case 'l': |
| 9250 | flags |= OPT_login; |
| 9251 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9252 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9253 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 9254 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 9255 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9256 | case '$': { |
| 9257 | unsigned long long empty_trap_mask; |
| 9258 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9259 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 9260 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9261 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 9262 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9263 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 9264 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9265 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9266 | optarg++; |
| 9267 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9268 | optarg++; |
| 9269 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 9270 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9271 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9272 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9273 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9274 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9275 | for (sig = 1; sig < NSIG; sig++) { |
| 9276 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9277 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 9278 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9279 | } |
| 9280 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9281 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9282 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9283 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9284 | optarg++; |
| 9285 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9286 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9287 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9288 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9289 | case 'R': |
| 9290 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9291 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9292 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9293 | # if ENABLE_HUSH_FUNCTIONS |
| 9294 | case 'F': { |
| 9295 | struct function *funcp = new_function(optarg); |
| 9296 | /* funcp->name is already set to optarg */ |
| 9297 | /* funcp->body is set to NULL. It's a special case. */ |
| 9298 | funcp->body_as_string = argv[optind]; |
| 9299 | optind++; |
| 9300 | break; |
| 9301 | } |
| 9302 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9303 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9304 | case 'n': |
| 9305 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9306 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9307 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9308 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9309 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9310 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9311 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9312 | " or: sh -c command [args]...\n\n"); |
| 9313 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9314 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9315 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9316 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9317 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9318 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9319 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9320 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9321 | G.global_argc = argc - (optind - 1); |
| 9322 | G.global_argv = argv + (optind - 1); |
| 9323 | G.global_argv[0] = argv[0]; |
| 9324 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9325 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9326 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9327 | G.root_ppid = getppid(); |
| 9328 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9329 | |
| 9330 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9331 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9332 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9333 | debug_printf("sourcing /etc/profile\n"); |
| 9334 | input = fopen_for_read("/etc/profile"); |
| 9335 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9336 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9337 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9338 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9339 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9340 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9341 | /* bash: after sourcing /etc/profile, |
| 9342 | * tries to source (in the given order): |
| 9343 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9344 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9345 | * bash also sources ~/.bash_logout on exit. |
| 9346 | * If called as sh, skips .bash_XXX files. |
| 9347 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9348 | } |
| 9349 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9350 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9351 | FILE *input; |
| 9352 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9353 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9354 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9355 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9356 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9357 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9358 | G.global_argc--; |
| 9359 | G.global_argv++; |
| 9360 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9361 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9362 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9363 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9364 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9365 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9366 | parse_and_run_file(input); |
| 9367 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9368 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9369 | #endif |
| 9370 | goto final_return; |
| 9371 | } |
| 9372 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9373 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9374 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9375 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9376 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9377 | /* A shell is interactive if the '-i' flag was given, |
| 9378 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9379 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9380 | * no arguments remaining or the -s flag given |
| 9381 | * standard input is a terminal |
| 9382 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9383 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9384 | */ |
| 9385 | #if ENABLE_HUSH_JOB |
| 9386 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9387 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9388 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9389 | if (G_saved_tty_pgrp < 0) |
| 9390 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9391 | |
| 9392 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9393 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9394 | if (G_interactive_fd < 0) { |
| 9395 | /* try to dup to any fd */ |
| 9396 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9397 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9398 | /* give up */ |
| 9399 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9400 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9401 | } |
| 9402 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9403 | // TODO: track & disallow any attempts of user |
| 9404 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9405 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9406 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9407 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9408 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9409 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9410 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9411 | /* If we were run as 'hush &', sleep until we are |
| 9412 | * in the foreground (tty pgrp == our pgrp). |
| 9413 | * If we get started under a job aware app (like bash), |
| 9414 | * make sure we are now in charge so we don't fight over |
| 9415 | * who gets the foreground */ |
| 9416 | while (1) { |
| 9417 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9418 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9419 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9420 | break; |
| 9421 | /* send TTIN to ourself (should stop us) */ |
| 9422 | kill(- shell_pgrp, SIGTTIN); |
| 9423 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9424 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9425 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9426 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9427 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9428 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9429 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9430 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9431 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9432 | /* Put ourselves in our own process group |
| 9433 | * (bash, too, does this only if ctty is available) */ |
| 9434 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9435 | /* Grab control of the terminal */ |
| 9436 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9437 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9438 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9439 | |
| 9440 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9441 | { |
| 9442 | const char *hp = get_local_var_value("HISTFILE"); |
| 9443 | if (!hp) { |
| 9444 | hp = get_local_var_value("HOME"); |
| 9445 | if (hp) |
| 9446 | hp = concat_path_file(hp, ".hush_history"); |
| 9447 | } else { |
| 9448 | hp = xstrdup(hp); |
| 9449 | } |
| 9450 | if (hp) { |
| 9451 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9452 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9453 | } |
| 9454 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9455 | hp = get_local_var_value("HISTFILESIZE"); |
| 9456 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9457 | # endif |
| 9458 | } |
| 9459 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9460 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9461 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9462 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9463 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9464 | /* No job control compiled in, only prompt/line editing */ |
| 9465 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9466 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9467 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9468 | /* try to dup to any fd */ |
Denys Vlasenko | 9acd63c | 2018-03-28 18:35:07 +0200 | [diff] [blame] | 9469 | G_interactive_fd = dup_CLOEXEC(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9470 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9471 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9472 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9473 | } |
| 9474 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9475 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9476 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9477 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9478 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9479 | #else |
| 9480 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9481 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9482 | #endif |
| 9483 | /* bash: |
| 9484 | * if interactive but not a login shell, sources ~/.bashrc |
| 9485 | * (--norc turns this off, --rcfile <file> overrides) |
| 9486 | */ |
| 9487 | |
| 9488 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9489 | /* note: ash and hush share this string */ |
| 9490 | printf("\n\n%s %s\n" |
| 9491 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9492 | "\n", |
| 9493 | bb_banner, |
| 9494 | "hush - the humble shell" |
| 9495 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9496 | } |
| 9497 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9498 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9499 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9500 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9501 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9502 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9503 | |
| 9504 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9505 | /* |
| 9506 | * Built-ins |
| 9507 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9508 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9509 | { |
| 9510 | return 0; |
| 9511 | } |
| 9512 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9513 | #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] | 9514 | 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] | 9515 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9516 | int argc = string_array_len(argv); |
| 9517 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9518 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9519 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9520 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9521 | static int FAST_FUNC builtin_test(char **argv) |
| 9522 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9523 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9524 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9525 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9526 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9527 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9528 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9529 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9530 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9531 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9532 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9533 | static int FAST_FUNC builtin_printf(char **argv) |
| 9534 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9535 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9536 | } |
| 9537 | #endif |
| 9538 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9539 | #if ENABLE_HUSH_HELP |
| 9540 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9541 | { |
| 9542 | const struct built_in_command *x; |
| 9543 | |
| 9544 | printf( |
| 9545 | "Built-in commands:\n" |
| 9546 | "------------------\n"); |
| 9547 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9548 | if (x->b_descr) |
| 9549 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9550 | } |
| 9551 | return EXIT_SUCCESS; |
| 9552 | } |
| 9553 | #endif |
| 9554 | |
| 9555 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9556 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9557 | { |
| 9558 | show_history(G.line_input_state); |
| 9559 | return EXIT_SUCCESS; |
| 9560 | } |
| 9561 | #endif |
| 9562 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9563 | static char **skip_dash_dash(char **argv) |
| 9564 | { |
| 9565 | argv++; |
| 9566 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9567 | argv++; |
| 9568 | return argv; |
| 9569 | } |
| 9570 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9571 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9572 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9573 | const char *newdir; |
| 9574 | |
| 9575 | argv = skip_dash_dash(argv); |
| 9576 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9577 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9578 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9579 | * bash says "bash: cd: HOME not set" and does nothing |
| 9580 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9581 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9582 | const char *home = get_local_var_value("HOME"); |
| 9583 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9584 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9585 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9586 | /* Mimic bash message exactly */ |
| 9587 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9588 | return EXIT_FAILURE; |
| 9589 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9590 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9591 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9592 | * set it again, but do not export. bash does the same. |
| 9593 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9594 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9595 | return EXIT_SUCCESS; |
| 9596 | } |
| 9597 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9598 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9599 | { |
| 9600 | puts(get_cwd(0)); |
| 9601 | return EXIT_SUCCESS; |
| 9602 | } |
| 9603 | |
| 9604 | static int FAST_FUNC builtin_eval(char **argv) |
| 9605 | { |
| 9606 | int rcode = EXIT_SUCCESS; |
| 9607 | |
| 9608 | argv = skip_dash_dash(argv); |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9609 | if (argv[0]) { |
| 9610 | char *str = NULL; |
| 9611 | |
| 9612 | if (argv[1]) { |
| 9613 | /* "The eval utility shall construct a command by |
| 9614 | * concatenating arguments together, separating |
| 9615 | * each with a <space> character." |
| 9616 | */ |
| 9617 | char *p; |
| 9618 | unsigned len = 0; |
| 9619 | char **pp = argv; |
| 9620 | do |
| 9621 | len += strlen(*pp) + 1; |
| 9622 | while (*++pp); |
| 9623 | str = p = xmalloc(len); |
| 9624 | pp = argv; |
| 9625 | do { |
| 9626 | p = stpcpy(p, *pp); |
| 9627 | *p++ = ' '; |
| 9628 | } while (*++pp); |
| 9629 | p[-1] = '\0'; |
| 9630 | } |
| 9631 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9632 | /* bash: |
| 9633 | * eval "echo Hi; done" ("done" is syntax error): |
| 9634 | * "echo Hi" will not execute too. |
| 9635 | */ |
Denys Vlasenko | 1f19112 | 2018-01-11 13:17:30 +0100 | [diff] [blame] | 9636 | parse_and_run_string(str ? str : argv[0]); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9637 | free(str); |
| 9638 | rcode = G.last_exitcode; |
| 9639 | } |
| 9640 | return rcode; |
| 9641 | } |
| 9642 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9643 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9644 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9645 | argv = skip_dash_dash(argv); |
| 9646 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9647 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9648 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9649 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9650 | * tty pgrp then, only top-level shell process does that */ |
| 9651 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9652 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9653 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9654 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9655 | * open here. However, they are all CLOEXEC, and execv below |
| 9656 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9657 | * it should show no extra open fds in the "ls" process. |
| 9658 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 9659 | */ |
| 9660 | //close_saved_fds_and_FILE_fds(); |
| 9661 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 9662 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9663 | * 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] | 9664 | * and tcsetpgrp, and this is inherently racy. |
| 9665 | */ |
| 9666 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9667 | } |
| 9668 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9669 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9670 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 9671 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9672 | |
| 9673 | /* interactive bash: |
| 9674 | * # trap "echo EEE" EXIT |
| 9675 | * # exit |
| 9676 | * exit |
| 9677 | * There are stopped jobs. |
| 9678 | * (if there are _stopped_ jobs, running ones don't count) |
| 9679 | * # exit |
| 9680 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 9681 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9682 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 9683 | * 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] | 9684 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 9685 | |
| 9686 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9687 | argv = skip_dash_dash(argv); |
| 9688 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9689 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9690 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 9691 | xfunc_error_retval = 255; |
| 9692 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9693 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9694 | } |
| 9695 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9696 | #if ENABLE_HUSH_TYPE |
| 9697 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 9698 | static int FAST_FUNC builtin_type(char **argv) |
| 9699 | { |
| 9700 | int ret = EXIT_SUCCESS; |
| 9701 | |
| 9702 | while (*++argv) { |
| 9703 | const char *type; |
| 9704 | char *path = NULL; |
| 9705 | |
| 9706 | if (0) {} /* make conditional compile easier below */ |
| 9707 | /*else if (find_alias(*argv)) |
| 9708 | type = "an alias";*/ |
| 9709 | #if ENABLE_HUSH_FUNCTIONS |
| 9710 | else if (find_function(*argv)) |
| 9711 | type = "a function"; |
| 9712 | #endif |
| 9713 | else if (find_builtin(*argv)) |
| 9714 | type = "a shell builtin"; |
| 9715 | else if ((path = find_in_path(*argv)) != NULL) |
| 9716 | type = path; |
| 9717 | else { |
| 9718 | bb_error_msg("type: %s: not found", *argv); |
| 9719 | ret = EXIT_FAILURE; |
| 9720 | continue; |
| 9721 | } |
| 9722 | |
| 9723 | printf("%s is %s\n", *argv, type); |
| 9724 | free(path); |
| 9725 | } |
| 9726 | |
| 9727 | return ret; |
| 9728 | } |
| 9729 | #endif |
| 9730 | |
| 9731 | #if ENABLE_HUSH_READ |
| 9732 | /* Interruptibility of read builtin in bash |
| 9733 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9734 | * |
| 9735 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9736 | * |
| 9737 | * SIGINT: |
| 9738 | * - terminates non-interactive shell; |
| 9739 | * - interrupts read in interactive shell; |
| 9740 | * if it has non-empty trap: |
| 9741 | * - executes trap and returns to command prompt in interactive shell; |
| 9742 | * - executes trap and returns to read in non-interactive shell; |
| 9743 | * SIGTERM: |
| 9744 | * - is ignored (does not interrupt) read in interactive shell; |
| 9745 | * - terminates non-interactive shell; |
| 9746 | * if it has non-empty trap: |
| 9747 | * - executes trap and returns to read; |
| 9748 | * SIGHUP: |
| 9749 | * - terminates shell (regardless of interactivity); |
| 9750 | * if it has non-empty trap: |
| 9751 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9752 | * SIGCHLD from children: |
| 9753 | * - does not interrupt read regardless of interactivity: |
| 9754 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9755 | */ |
| 9756 | static int FAST_FUNC builtin_read(char **argv) |
| 9757 | { |
| 9758 | const char *r; |
| 9759 | char *opt_n = NULL; |
| 9760 | char *opt_p = NULL; |
| 9761 | char *opt_t = NULL; |
| 9762 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9763 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9764 | const char *ifs; |
| 9765 | int read_flags; |
| 9766 | |
| 9767 | /* "!": do not abort on errors. |
| 9768 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9769 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9770 | read_flags = getopt32(argv, |
| 9771 | #if BASH_READ_D |
| 9772 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 9773 | #else |
| 9774 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 9775 | #endif |
| 9776 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9777 | if (read_flags == (uint32_t)-1) |
| 9778 | return EXIT_FAILURE; |
| 9779 | argv += optind; |
| 9780 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9781 | |
| 9782 | again: |
| 9783 | r = shell_builtin_read(set_local_var_from_halves, |
| 9784 | argv, |
| 9785 | ifs, |
| 9786 | read_flags, |
| 9787 | opt_n, |
| 9788 | opt_p, |
| 9789 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9790 | opt_u, |
| 9791 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9792 | ); |
| 9793 | |
| 9794 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9795 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9796 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9797 | goto again; |
| 9798 | } |
| 9799 | |
| 9800 | if ((uintptr_t)r > 1) { |
| 9801 | bb_error_msg("%s", r); |
| 9802 | r = (char*)(uintptr_t)1; |
| 9803 | } |
| 9804 | |
| 9805 | return (uintptr_t)r; |
| 9806 | } |
| 9807 | #endif |
| 9808 | |
| 9809 | #if ENABLE_HUSH_UMASK |
| 9810 | static int FAST_FUNC builtin_umask(char **argv) |
| 9811 | { |
| 9812 | int rc; |
| 9813 | mode_t mask; |
| 9814 | |
| 9815 | rc = 1; |
| 9816 | mask = umask(0); |
| 9817 | argv = skip_dash_dash(argv); |
| 9818 | if (argv[0]) { |
| 9819 | mode_t old_mask = mask; |
| 9820 | |
| 9821 | /* numeric umasks are taken as-is */ |
| 9822 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9823 | if (!isdigit(argv[0][0])) |
| 9824 | mask ^= 0777; |
| 9825 | mask = bb_parse_mode(argv[0], mask); |
| 9826 | if (!isdigit(argv[0][0])) |
| 9827 | mask ^= 0777; |
| 9828 | if ((unsigned)mask > 0777) { |
| 9829 | mask = old_mask; |
| 9830 | /* bash messages: |
| 9831 | * bash: umask: 'q': invalid symbolic mode operator |
| 9832 | * bash: umask: 999: octal number out of range |
| 9833 | */ |
| 9834 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 9835 | rc = 0; |
| 9836 | } |
| 9837 | } else { |
| 9838 | /* Mimic bash */ |
| 9839 | printf("%04o\n", (unsigned) mask); |
| 9840 | /* fall through and restore mask which we set to 0 */ |
| 9841 | } |
| 9842 | umask(mask); |
| 9843 | |
| 9844 | return !rc; /* rc != 0 - success */ |
| 9845 | } |
| 9846 | #endif |
| 9847 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9848 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9849 | static void print_escaped(const char *s) |
| 9850 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9851 | if (*s == '\'') |
| 9852 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9853 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9854 | const char *p = strchrnul(s, '\''); |
| 9855 | /* print 'xxxx', possibly just '' */ |
| 9856 | printf("'%.*s'", (int)(p - s), s); |
| 9857 | if (*p == '\0') |
| 9858 | break; |
| 9859 | s = p; |
| 9860 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9861 | /* s points to '; print "'''...'''" */ |
| 9862 | putchar('"'); |
| 9863 | do putchar('\''); while (*++s == '\''); |
| 9864 | putchar('"'); |
| 9865 | } while (*s); |
| 9866 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9867 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9868 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9869 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9870 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9871 | { |
| 9872 | do { |
| 9873 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9874 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9875 | |
| 9876 | /* So far we do not check that name is valid (TODO?) */ |
| 9877 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9878 | if (*name_end == '\0') { |
| 9879 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9880 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9881 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 9882 | var = vpp ? *vpp : NULL; |
| 9883 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9884 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9885 | /* export -n NAME (without =VALUE) */ |
| 9886 | if (var) { |
| 9887 | var->flg_export = 0; |
| 9888 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 9889 | unsetenv(name); |
| 9890 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 9891 | continue; |
| 9892 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9893 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9894 | /* export NAME (without =VALUE) */ |
| 9895 | if (var) { |
| 9896 | var->flg_export = 1; |
| 9897 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 9898 | putenv(var->varstr); |
| 9899 | continue; |
| 9900 | } |
| 9901 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9902 | if (flags & SETFLAG_MAKE_RO) { |
| 9903 | /* readonly NAME (without =VALUE) */ |
| 9904 | if (var) { |
| 9905 | var->flg_read_only = 1; |
| 9906 | continue; |
| 9907 | } |
| 9908 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9909 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 9910 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9911 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
| 9912 | unsigned lvl = flags >> SETFLAG_LOCAL_SHIFT; |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 9913 | if (var && var->func_nest_level == lvl) { |
| 9914 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 9915 | continue; |
| 9916 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9917 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9918 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9919 | /* Exporting non-existing variable. |
| 9920 | * bash does not put it in environment, |
| 9921 | * but remembers that it is exported, |
| 9922 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9923 | * We just set it to "" and export. |
| 9924 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9925 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9926 | * bash sets the value to "". |
| 9927 | */ |
| 9928 | /* Or, it's "readonly NAME" (without =VALUE). |
| 9929 | * bash remembers NAME and disallows its creation |
| 9930 | * in the future. |
| 9931 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9932 | name = xasprintf("%s=", name); |
| 9933 | } else { |
| 9934 | /* (Un)exporting/making local NAME=VALUE */ |
| 9935 | name = xstrdup(name); |
| 9936 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9937 | if (set_local_var(name, flags)) |
| 9938 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9939 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9940 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9941 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9942 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9943 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9944 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9945 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9946 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 9947 | unsigned opt_unexport; |
| 9948 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 9949 | #if ENABLE_HUSH_EXPORT_N |
| 9950 | /* "!": do not abort on errors */ |
| 9951 | opt_unexport = getopt32(argv, "!n"); |
| 9952 | if (opt_unexport == (uint32_t)-1) |
| 9953 | return EXIT_FAILURE; |
| 9954 | argv += optind; |
| 9955 | #else |
| 9956 | opt_unexport = 0; |
| 9957 | argv++; |
| 9958 | #endif |
| 9959 | |
| 9960 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9961 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9962 | if (e) { |
| 9963 | while (*e) { |
| 9964 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9965 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9966 | #else |
| 9967 | /* ash emits: export VAR='VAL' |
| 9968 | * bash: declare -x VAR="VAL" |
| 9969 | * we follow ash example */ |
| 9970 | const char *s = *e++; |
| 9971 | const char *p = strchr(s, '='); |
| 9972 | |
| 9973 | if (!p) /* wtf? take next variable */ |
| 9974 | continue; |
| 9975 | /* export var= */ |
| 9976 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9977 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9978 | putchar('\n'); |
| 9979 | #endif |
| 9980 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9981 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9982 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9983 | return EXIT_SUCCESS; |
| 9984 | } |
| 9985 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9986 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9987 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9988 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9989 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9990 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9991 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9992 | { |
| 9993 | if (G.func_nest_level == 0) { |
| 9994 | bb_error_msg("%s: not in a function", argv[0]); |
| 9995 | return EXIT_FAILURE; /* bash compat */ |
| 9996 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9997 | argv++; |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9998 | return helper_export_local(argv, G.func_nest_level << SETFLAG_LOCAL_SHIFT); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9999 | } |
| 10000 | #endif |
| 10001 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10002 | #if ENABLE_HUSH_READONLY |
| 10003 | static int FAST_FUNC builtin_readonly(char **argv) |
| 10004 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10005 | argv++; |
| 10006 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10007 | /* bash: readonly [-p]: list all readonly VARs |
| 10008 | * (-p has no effect in bash) |
| 10009 | */ |
| 10010 | struct variable *e; |
| 10011 | for (e = G.top_var; e; e = e->next) { |
| 10012 | if (e->flg_read_only) { |
| 10013 | //TODO: quote value: readonly VAR='VAL' |
| 10014 | printf("readonly %s\n", e->varstr); |
| 10015 | } |
| 10016 | } |
| 10017 | return EXIT_SUCCESS; |
| 10018 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 10019 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 10020 | } |
| 10021 | #endif |
| 10022 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10023 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10024 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 10025 | static int FAST_FUNC builtin_unset(char **argv) |
| 10026 | { |
| 10027 | int ret; |
| 10028 | unsigned opts; |
| 10029 | |
| 10030 | /* "!": do not abort on errors */ |
| 10031 | /* "+": stop at 1st non-option */ |
| 10032 | opts = getopt32(argv, "!+vf"); |
| 10033 | if (opts == (unsigned)-1) |
| 10034 | return EXIT_FAILURE; |
| 10035 | if (opts == 3) { |
| 10036 | bb_error_msg("unset: -v and -f are exclusive"); |
| 10037 | return EXIT_FAILURE; |
| 10038 | } |
| 10039 | argv += optind; |
| 10040 | |
| 10041 | ret = EXIT_SUCCESS; |
| 10042 | while (*argv) { |
| 10043 | if (!(opts & 2)) { /* not -f */ |
| 10044 | if (unset_local_var(*argv)) { |
| 10045 | /* unset <nonexistent_var> doesn't fail. |
| 10046 | * Error is when one tries to unset RO var. |
| 10047 | * Message was printed by unset_local_var. */ |
| 10048 | ret = EXIT_FAILURE; |
| 10049 | } |
| 10050 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10051 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10052 | else { |
| 10053 | unset_func(*argv); |
| 10054 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10055 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10056 | argv++; |
| 10057 | } |
| 10058 | return ret; |
| 10059 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10060 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10061 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10062 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10063 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 10064 | * built-in 'set' handler |
| 10065 | * SUSv3 says: |
| 10066 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 10067 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 10068 | * set -- [argument...] |
| 10069 | * set -o |
| 10070 | * set +o |
| 10071 | * Implementations shall support the options in both their hyphen and |
| 10072 | * plus-sign forms. These options can also be specified as options to sh. |
| 10073 | * Examples: |
| 10074 | * Write out all variables and their values: set |
| 10075 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 10076 | * Turn on the -x and -v options: set -xv |
| 10077 | * Unset all positional parameters: set -- |
| 10078 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 10079 | * Set the positional parameters to the expansion of x, even if x expands |
| 10080 | * with a leading '-' or '+': set -- $x |
| 10081 | * |
| 10082 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 10083 | */ |
| 10084 | static int FAST_FUNC builtin_set(char **argv) |
| 10085 | { |
| 10086 | int n; |
| 10087 | char **pp, **g_argv; |
| 10088 | char *arg = *++argv; |
| 10089 | |
| 10090 | if (arg == NULL) { |
| 10091 | struct variable *e; |
| 10092 | for (e = G.top_var; e; e = e->next) |
| 10093 | puts(e->varstr); |
| 10094 | return EXIT_SUCCESS; |
| 10095 | } |
| 10096 | |
| 10097 | do { |
| 10098 | if (strcmp(arg, "--") == 0) { |
| 10099 | ++argv; |
| 10100 | goto set_argv; |
| 10101 | } |
| 10102 | if (arg[0] != '+' && arg[0] != '-') |
| 10103 | break; |
| 10104 | for (n = 1; arg[n]; ++n) { |
| 10105 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 10106 | goto error; |
| 10107 | if (arg[n] == 'o' && argv[1]) |
| 10108 | argv++; |
| 10109 | } |
| 10110 | } while ((arg = *++argv) != NULL); |
| 10111 | /* Now argv[0] is 1st argument */ |
| 10112 | |
| 10113 | if (arg == NULL) |
| 10114 | return EXIT_SUCCESS; |
| 10115 | set_argv: |
| 10116 | |
| 10117 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 10118 | g_argv = G.global_argv; |
| 10119 | if (G.global_args_malloced) { |
| 10120 | pp = g_argv; |
| 10121 | while (*++pp) |
| 10122 | free(*pp); |
| 10123 | g_argv[1] = NULL; |
| 10124 | } else { |
| 10125 | G.global_args_malloced = 1; |
| 10126 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 10127 | pp[0] = g_argv[0]; /* retain $0 */ |
| 10128 | g_argv = pp; |
| 10129 | } |
| 10130 | /* This realloc's G.global_argv */ |
| 10131 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 10132 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 10133 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10134 | |
| 10135 | return EXIT_SUCCESS; |
| 10136 | |
| 10137 | /* Nothing known, so abort */ |
| 10138 | error: |
Denys Vlasenko | 5700029 | 2018-01-12 14:41:45 +0100 | [diff] [blame] | 10139 | bb_error_msg("%s: %s: invalid option", "set", arg); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10140 | return EXIT_FAILURE; |
| 10141 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 10142 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10143 | |
| 10144 | static int FAST_FUNC builtin_shift(char **argv) |
| 10145 | { |
| 10146 | int n = 1; |
| 10147 | argv = skip_dash_dash(argv); |
| 10148 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 10149 | n = bb_strtou(argv[0], NULL, 10); |
| 10150 | if (errno || n < 0) { |
| 10151 | /* shared string with ash.c */ |
| 10152 | bb_error_msg("Illegal number: %s", argv[0]); |
| 10153 | /* |
| 10154 | * ash aborts in this case. |
| 10155 | * bash prints error message and set $? to 1. |
| 10156 | * Interestingly, for "shift 99999" bash does not |
| 10157 | * print error message, but does set $? to 1 |
| 10158 | * (and does no shifting at all). |
| 10159 | */ |
| 10160 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10161 | } |
| 10162 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 10163 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10164 | int m = 1; |
| 10165 | while (m <= n) |
| 10166 | free(G.global_argv[m++]); |
| 10167 | } |
| 10168 | G.global_argc -= n; |
| 10169 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 10170 | G.global_argc * sizeof(G.global_argv[0])); |
| 10171 | return EXIT_SUCCESS; |
| 10172 | } |
| 10173 | return EXIT_FAILURE; |
| 10174 | } |
| 10175 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10176 | #if ENABLE_HUSH_GETOPTS |
| 10177 | static int FAST_FUNC builtin_getopts(char **argv) |
| 10178 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10179 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 10180 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10181 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10182 | If a required argument is not found, and getopts is not silent, |
| 10183 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 10184 | diagnostic message is printed. If getopts is silent, then a |
| 10185 | colon (:) is placed in VAR and OPTARG is set to the option |
| 10186 | character found. |
| 10187 | |
| 10188 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10189 | |
| 10190 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10191 | */ |
| 10192 | char cbuf[2]; |
| 10193 | const char *cp, *optstring, *var; |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10194 | int c, n, exitcode, my_opterr; |
| 10195 | unsigned count; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10196 | |
| 10197 | optstring = *++argv; |
| 10198 | if (!optstring || !(var = *++argv)) { |
| 10199 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 10200 | return EXIT_FAILURE; |
| 10201 | } |
| 10202 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10203 | if (argv[1]) |
| 10204 | argv[0] = G.global_argv[0]; /* for error messages in getopt() */ |
| 10205 | else |
| 10206 | argv = G.global_argv; |
| 10207 | cbuf[1] = '\0'; |
| 10208 | |
| 10209 | my_opterr = 0; |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10210 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10211 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 10212 | /* 0 if "OPTERR=0", 1 otherwise */ |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10213 | my_opterr = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10214 | } |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10215 | |
| 10216 | /* getopts stops on first non-option. Add "+" to force that */ |
| 10217 | /*if (optstring[0] != '+')*/ { |
| 10218 | char *s = alloca(strlen(optstring) + 2); |
| 10219 | sprintf(s, "+%s", optstring); |
| 10220 | optstring = s; |
| 10221 | } |
| 10222 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10223 | /* Naively, now we should just |
| 10224 | * cp = get_local_var_value("OPTIND"); |
| 10225 | * optind = cp ? atoi(cp) : 0; |
| 10226 | * optarg = NULL; |
| 10227 | * opterr = my_opterr; |
| 10228 | * c = getopt(string_array_len(argv), argv, optstring); |
| 10229 | * and be done? Not so fast... |
| 10230 | * Unlike normal getopt() usage in C programs, here |
| 10231 | * each successive call will (usually) have the same argv[] CONTENTS, |
| 10232 | * but not the ADDRESSES. Worse yet, it's possible that between |
| 10233 | * invocations of "getopts", there will be calls to shell builtins |
| 10234 | * which use getopt() internally. Example: |
| 10235 | * while getopts "abc" RES -a -bc -abc de; do |
| 10236 | * unset -ff func |
| 10237 | * done |
| 10238 | * This would not work correctly: getopt() call inside "unset" |
| 10239 | * modifies internal libc state which is tracking position in |
| 10240 | * multi-option strings ("-abc"). At best, it can skip options |
| 10241 | * or return the same option infinitely. With glibc implementation |
| 10242 | * of getopt(), it would use outright invalid pointers and return |
| 10243 | * garbage even _without_ "unset" mangling internal state. |
| 10244 | * |
| 10245 | * We resort to resetting getopt() state and calling it N times, |
| 10246 | * until we get Nth result (or failure). |
| 10247 | * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). |
| 10248 | */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10249 | GETOPT_RESET(); |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10250 | count = 0; |
| 10251 | n = string_array_len(argv); |
| 10252 | do { |
| 10253 | optarg = NULL; |
| 10254 | opterr = (count < G.getopt_count) ? 0 : my_opterr; |
| 10255 | c = getopt(n, argv, optstring); |
| 10256 | if (c < 0) |
| 10257 | break; |
| 10258 | count++; |
| 10259 | } while (count <= G.getopt_count); |
| 10260 | |
| 10261 | /* Set OPTIND. Prevent resetting of the magic counter! */ |
| 10262 | set_local_var_from_halves("OPTIND", utoa(optind)); |
| 10263 | G.getopt_count = count; /* "next time, give me N+1'th result" */ |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 10264 | GETOPT_RESET(); /* just in case */ |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10265 | |
| 10266 | /* Set OPTARG */ |
| 10267 | /* Always set or unset, never left as-is, even on exit/error: |
| 10268 | * "If no option was found, or if the option that was found |
| 10269 | * does not have an option-argument, OPTARG shall be unset." |
| 10270 | */ |
| 10271 | cp = optarg; |
| 10272 | if (c == '?') { |
| 10273 | /* If ":optstring" and unknown option is seen, |
| 10274 | * it is stored to OPTARG. |
| 10275 | */ |
| 10276 | if (optstring[1] == ':') { |
| 10277 | cbuf[0] = optopt; |
| 10278 | cp = cbuf; |
| 10279 | } |
| 10280 | } |
| 10281 | if (cp) |
| 10282 | set_local_var_from_halves("OPTARG", cp); |
| 10283 | else |
| 10284 | unset_local_var("OPTARG"); |
| 10285 | |
| 10286 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10287 | exitcode = EXIT_SUCCESS; |
| 10288 | if (c < 0) { /* -1: end of options */ |
| 10289 | exitcode = EXIT_FAILURE; |
| 10290 | c = '?'; |
| 10291 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 10292 | |
Denys Vlasenko | 238ff98 | 2017-08-29 13:38:30 +0200 | [diff] [blame] | 10293 | /* Set VAR */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10294 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10295 | set_local_var_from_halves(var, cbuf); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 10296 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 10297 | return exitcode; |
| 10298 | } |
| 10299 | #endif |
| 10300 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10301 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10302 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10303 | char *arg_path, *filename; |
| 10304 | FILE *input; |
| 10305 | save_arg_t sv; |
| 10306 | char *args_need_save; |
| 10307 | #if ENABLE_HUSH_FUNCTIONS |
| 10308 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10309 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 10310 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10311 | argv = skip_dash_dash(argv); |
| 10312 | filename = argv[0]; |
| 10313 | if (!filename) { |
| 10314 | /* bash says: "bash: .: filename argument required" */ |
| 10315 | return 2; /* bash compat */ |
| 10316 | } |
| 10317 | arg_path = NULL; |
| 10318 | if (!strchr(filename, '/')) { |
| 10319 | arg_path = find_in_path(filename); |
| 10320 | if (arg_path) |
| 10321 | filename = arg_path; |
Denys Vlasenko | 54c2111 | 2018-01-27 20:46:45 +0100 | [diff] [blame] | 10322 | else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) { |
Denys Vlasenko | f7e0fea | 2018-01-27 19:05:59 +0100 | [diff] [blame] | 10323 | errno = ENOENT; |
| 10324 | bb_simple_perror_msg(filename); |
| 10325 | return EXIT_FAILURE; |
| 10326 | } |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10327 | } |
| 10328 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 10329 | free(arg_path); |
| 10330 | if (!input) { |
| 10331 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 10332 | /* POSIX: non-interactive shell should abort here, |
| 10333 | * not merely fail. So far no one complained :) |
| 10334 | */ |
| 10335 | return EXIT_FAILURE; |
| 10336 | } |
| 10337 | |
| 10338 | #if ENABLE_HUSH_FUNCTIONS |
| 10339 | sv_flg = G_flag_return_in_progress; |
| 10340 | /* "we are inside sourced file, ok to use return" */ |
| 10341 | G_flag_return_in_progress = -1; |
| 10342 | #endif |
| 10343 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 10344 | if (args_need_save) |
| 10345 | save_and_replace_G_args(&sv, argv); |
| 10346 | |
| 10347 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10348 | G.last_exitcode = 0; |
| 10349 | parse_and_run_file(input); |
| 10350 | fclose_and_forget(input); |
| 10351 | |
| 10352 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10353 | restore_G_args(&sv, argv); |
| 10354 | #if ENABLE_HUSH_FUNCTIONS |
| 10355 | G_flag_return_in_progress = sv_flg; |
| 10356 | #endif |
| 10357 | |
| 10358 | return G.last_exitcode; |
| 10359 | } |
| 10360 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10361 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10362 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10363 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10364 | int sig; |
| 10365 | char *new_cmd; |
| 10366 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10367 | if (!G_traps) |
| 10368 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10369 | |
| 10370 | argv++; |
| 10371 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10372 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10373 | /* No args: print all trapped */ |
| 10374 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10375 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10376 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10377 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10378 | /* note: bash adds "SIG", but only if invoked |
| 10379 | * as "bash". If called as "sh", or if set -o posix, |
| 10380 | * then it prints short signal names. |
| 10381 | * We are printing short names: */ |
| 10382 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10383 | } |
| 10384 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10385 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10386 | return EXIT_SUCCESS; |
| 10387 | } |
| 10388 | |
| 10389 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10390 | /* If first arg is a number: reset all specified signals */ |
| 10391 | sig = bb_strtou(*argv, NULL, 10); |
| 10392 | if (errno == 0) { |
| 10393 | int ret; |
| 10394 | process_sig_list: |
| 10395 | ret = EXIT_SUCCESS; |
| 10396 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10397 | sighandler_t handler; |
| 10398 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10399 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10400 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10401 | ret = EXIT_FAILURE; |
| 10402 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10403 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10404 | continue; |
| 10405 | } |
| 10406 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10407 | free(G_traps[sig]); |
| 10408 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10409 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10410 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10411 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10412 | |
| 10413 | /* There is no signal for 0 (EXIT) */ |
| 10414 | if (sig == 0) |
| 10415 | continue; |
| 10416 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10417 | if (new_cmd) |
| 10418 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10419 | else |
| 10420 | /* We are removing trap handler */ |
| 10421 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10422 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10423 | } |
| 10424 | return ret; |
| 10425 | } |
| 10426 | |
| 10427 | if (!argv[1]) { /* no second arg */ |
| 10428 | bb_error_msg("trap: invalid arguments"); |
| 10429 | return EXIT_FAILURE; |
| 10430 | } |
| 10431 | |
| 10432 | /* First arg is "-": reset all specified to default */ |
| 10433 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10434 | /* Everything else: set arg as signal handler |
| 10435 | * (includes "" case, which ignores signal) */ |
| 10436 | if (argv[0][0] == '-') { |
| 10437 | if (argv[0][1] == '\0') { /* "-" */ |
| 10438 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10439 | goto reset_traps; |
| 10440 | } |
| 10441 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10442 | argv++; |
| 10443 | } |
| 10444 | /* else: "-something", no special meaning */ |
| 10445 | } |
| 10446 | new_cmd = *argv; |
| 10447 | reset_traps: |
| 10448 | argv++; |
| 10449 | goto process_sig_list; |
| 10450 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10451 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10452 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10453 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10454 | static struct pipe *parse_jobspec(const char *str) |
| 10455 | { |
| 10456 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10457 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10458 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10459 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10460 | if (str[0] != '%' |
| 10461 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10462 | ) { |
| 10463 | bb_error_msg("bad argument '%s'", str); |
| 10464 | return NULL; |
| 10465 | } |
| 10466 | /* It is "%%", "%+" or "%" - current job */ |
| 10467 | jobnum = G.last_jobid; |
| 10468 | if (jobnum == 0) { |
| 10469 | bb_error_msg("no current job"); |
| 10470 | return NULL; |
| 10471 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10472 | } |
| 10473 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10474 | if (pi->jobid == jobnum) { |
| 10475 | return pi; |
| 10476 | } |
| 10477 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10478 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10479 | return NULL; |
| 10480 | } |
| 10481 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10482 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10483 | { |
| 10484 | struct pipe *job; |
| 10485 | const char *status_string; |
| 10486 | |
| 10487 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10488 | for (job = G.job_list; job; job = job->next) { |
| 10489 | if (job->alive_cmds == job->stopped_cmds) |
| 10490 | status_string = "Stopped"; |
| 10491 | else |
| 10492 | status_string = "Running"; |
| 10493 | |
| 10494 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10495 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10496 | |
| 10497 | clean_up_last_dead_job(); |
| 10498 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10499 | return EXIT_SUCCESS; |
| 10500 | } |
| 10501 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10502 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10503 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10504 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10505 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10506 | struct pipe *pi; |
| 10507 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10508 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10509 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10510 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10511 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10512 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10513 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10514 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10515 | goto found; |
| 10516 | } |
| 10517 | } |
| 10518 | bb_error_msg("%s: no current job", argv[0]); |
| 10519 | return EXIT_FAILURE; |
| 10520 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10521 | |
| 10522 | pi = parse_jobspec(argv[1]); |
| 10523 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10524 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10525 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10526 | /* TODO: bash prints a string representation |
| 10527 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10528 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10529 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10530 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10531 | } |
| 10532 | |
| 10533 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10534 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10535 | for (i = 0; i < pi->num_cmds; i++) { |
| 10536 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10537 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10538 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10539 | |
| 10540 | i = kill(- pi->pgrp, SIGCONT); |
| 10541 | if (i < 0) { |
| 10542 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10543 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10544 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10545 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10546 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10547 | } |
| 10548 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10549 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10550 | 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] | 10551 | return checkjobs_and_fg_shell(pi); |
| 10552 | } |
| 10553 | return EXIT_SUCCESS; |
| 10554 | } |
| 10555 | #endif |
| 10556 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10557 | #if ENABLE_HUSH_KILL |
| 10558 | static int FAST_FUNC builtin_kill(char **argv) |
| 10559 | { |
| 10560 | int ret = 0; |
| 10561 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10562 | # if ENABLE_HUSH_JOB |
| 10563 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10564 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10565 | |
| 10566 | do { |
| 10567 | struct pipe *pi; |
| 10568 | char *dst; |
| 10569 | int j, n; |
| 10570 | |
| 10571 | if (argv[i][0] != '%') |
| 10572 | continue; |
| 10573 | /* |
| 10574 | * "kill %N" - job kill |
| 10575 | * Converting to pgrp / pid kill |
| 10576 | */ |
| 10577 | pi = parse_jobspec(argv[i]); |
| 10578 | if (!pi) { |
| 10579 | /* Eat bad jobspec */ |
| 10580 | j = i; |
| 10581 | do { |
| 10582 | j++; |
| 10583 | argv[j - 1] = argv[j]; |
| 10584 | } while (argv[j]); |
| 10585 | ret = 1; |
| 10586 | i--; |
| 10587 | continue; |
| 10588 | } |
| 10589 | /* |
| 10590 | * In jobs started under job control, we signal |
| 10591 | * entire process group by kill -PGRP_ID. |
| 10592 | * This happens, f.e., in interactive shell. |
| 10593 | * |
| 10594 | * Otherwise, we signal each child via |
| 10595 | * kill PID1 PID2 PID3. |
| 10596 | * Testcases: |
| 10597 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10598 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10599 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10600 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10601 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10602 | dst = alloca(n * sizeof(int)*4); |
| 10603 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10604 | if (G_interactive_fd) |
| 10605 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10606 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10607 | struct command *cmd = &pi->cmds[j]; |
| 10608 | /* Skip exited members of the job */ |
| 10609 | if (cmd->pid == 0) |
| 10610 | continue; |
| 10611 | /* |
| 10612 | * kill_main has matching code to expect |
| 10613 | * leading space. Needed to not confuse |
| 10614 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10615 | */ |
| 10616 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10617 | } |
| 10618 | *dst = '\0'; |
| 10619 | } while (argv[++i]); |
| 10620 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10621 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10622 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10623 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10624 | ret = run_applet_main(argv, kill_main); |
| 10625 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10626 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10627 | return ret; |
| 10628 | } |
| 10629 | #endif |
| 10630 | |
| 10631 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10632 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10633 | #if !ENABLE_HUSH_JOB |
| 10634 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10635 | #endif |
| 10636 | 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] | 10637 | { |
| 10638 | int ret = 0; |
| 10639 | for (;;) { |
| 10640 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10641 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10642 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10643 | if (!sigisemptyset(&G.pending_set)) |
| 10644 | goto check_sig; |
| 10645 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10646 | /* waitpid is not interruptible by SA_RESTARTed |
| 10647 | * signals which we use. Thus, this ugly dance: |
| 10648 | */ |
| 10649 | |
| 10650 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10651 | * pending signal mask before we call waitpid. |
| 10652 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10653 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10654 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10655 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 10656 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10657 | |
| 10658 | if (!sigisemptyset(&G.pending_set)) { |
| 10659 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10660 | goto restore; |
| 10661 | } |
| 10662 | |
| 10663 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10664 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10665 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10666 | debug_printf_exec("checkjobs:%d\n", ret); |
| 10667 | #if ENABLE_HUSH_JOB |
| 10668 | if (waitfor_pipe) { |
| 10669 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 10670 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 10671 | if (rcode >= 0) { |
| 10672 | ret = rcode; |
| 10673 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10674 | break; |
| 10675 | } |
| 10676 | } |
| 10677 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10678 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 10679 | /* if ret == 0, no children changed state */ |
| 10680 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10681 | if (errno == ECHILD || ret) { |
| 10682 | ret--; |
| 10683 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10684 | ret = 0; |
| 10685 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10686 | break; |
| 10687 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10688 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10689 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 10690 | /* Note: sigsuspend invokes signal handler */ |
| 10691 | sigsuspend(&oldset); |
| 10692 | restore: |
| 10693 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10694 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10695 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10696 | sig = check_and_run_traps(); |
| 10697 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 10698 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10699 | ret = 128 + sig; |
| 10700 | break; |
| 10701 | } |
| 10702 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 10703 | } |
| 10704 | return ret; |
| 10705 | } |
| 10706 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10707 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10708 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10709 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10710 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10711 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10712 | argv = skip_dash_dash(argv); |
| 10713 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 10714 | /* Don't care about wait results */ |
| 10715 | /* Note 1: must wait until there are no more children */ |
| 10716 | /* Note 2: must be interruptible */ |
| 10717 | /* Examples: |
| 10718 | * $ sleep 3 & sleep 6 & wait |
| 10719 | * [1] 30934 sleep 3 |
| 10720 | * [2] 30935 sleep 6 |
| 10721 | * [1] Done sleep 3 |
| 10722 | * [2] Done sleep 6 |
| 10723 | * $ sleep 3 & sleep 6 & wait |
| 10724 | * [1] 30936 sleep 3 |
| 10725 | * [2] 30937 sleep 6 |
| 10726 | * [1] Done sleep 3 |
| 10727 | * ^C <-- after ~4 sec from keyboard |
| 10728 | * $ |
| 10729 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10730 | 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] | 10731 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10732 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10733 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10734 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10735 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10736 | #if ENABLE_HUSH_JOB |
| 10737 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10738 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10739 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10740 | wait_pipe = parse_jobspec(*argv); |
| 10741 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10742 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10743 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10744 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10745 | } else { |
| 10746 | /* waiting on "last dead job" removes it */ |
| 10747 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 10748 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10749 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10750 | /* else: parse_jobspec() already emitted error msg */ |
| 10751 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10752 | } |
| 10753 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10754 | /* mimic bash message */ |
| 10755 | 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] | 10756 | ret = EXIT_FAILURE; |
| 10757 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10758 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10759 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10760 | /* Do we have such child? */ |
| 10761 | ret = waitpid(pid, &status, WNOHANG); |
| 10762 | if (ret < 0) { |
| 10763 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10764 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10765 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 10766 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10767 | /* "wait $!" but last bg task has already exited. Try: |
| 10768 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 10769 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10770 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10771 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10772 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10773 | } else { |
| 10774 | /* Example: "wait 1". mimic bash message */ |
| 10775 | 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] | 10776 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10777 | } else { |
| 10778 | /* ??? */ |
| 10779 | bb_perror_msg("wait %s", *argv); |
| 10780 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10781 | continue; /* bash checks all argv[] */ |
| 10782 | } |
| 10783 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10784 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10785 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10786 | } else { |
| 10787 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10788 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 10789 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10790 | if (WIFSIGNALED(status)) |
| 10791 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10792 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10793 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10794 | |
| 10795 | return ret; |
| 10796 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10797 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10798 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10799 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 10800 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 10801 | { |
| 10802 | if (argv[1]) { |
| 10803 | def = bb_strtou(argv[1], NULL, 10); |
| 10804 | if (errno || def < def_min || argv[2]) { |
| 10805 | bb_error_msg("%s: bad arguments", argv[0]); |
| 10806 | def = UINT_MAX; |
| 10807 | } |
| 10808 | } |
| 10809 | return def; |
| 10810 | } |
| 10811 | #endif |
| 10812 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10813 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10814 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10815 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10816 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10817 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10818 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10819 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 10820 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 10821 | return EXIT_SUCCESS; /* bash compat */ |
| 10822 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10823 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10824 | |
| 10825 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 10826 | if (depth == UINT_MAX) |
| 10827 | G.flag_break_continue = BC_BREAK; |
| 10828 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10829 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10830 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10831 | return EXIT_SUCCESS; |
| 10832 | } |
| 10833 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10834 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10835 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10836 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 10837 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10838 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10839 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10840 | |
| 10841 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10842 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10843 | { |
| 10844 | int rc; |
| 10845 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10846 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10847 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 10848 | return EXIT_FAILURE; /* bash compat */ |
| 10849 | } |
| 10850 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10851 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10852 | |
| 10853 | /* bash: |
| 10854 | * out of range: wraps around at 256, does not error out |
| 10855 | * non-numeric param: |
| 10856 | * f() { false; return qwe; }; f; echo $? |
| 10857 | * bash: return: qwe: numeric argument required <== we do this |
| 10858 | * 255 <== we also do this |
| 10859 | */ |
| 10860 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 10861 | return rc; |
| 10862 | } |
| 10863 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10864 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 10865 | #if ENABLE_HUSH_TIMES |
| 10866 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 10867 | { |
| 10868 | static const uint8_t times_tbl[] ALIGN1 = { |
| 10869 | ' ', offsetof(struct tms, tms_utime), |
| 10870 | '\n', offsetof(struct tms, tms_stime), |
| 10871 | ' ', offsetof(struct tms, tms_cutime), |
| 10872 | '\n', offsetof(struct tms, tms_cstime), |
| 10873 | 0 |
| 10874 | }; |
| 10875 | const uint8_t *p; |
| 10876 | unsigned clk_tck; |
| 10877 | struct tms buf; |
| 10878 | |
| 10879 | clk_tck = bb_clk_tck(); |
| 10880 | |
| 10881 | times(&buf); |
| 10882 | p = times_tbl; |
| 10883 | do { |
| 10884 | unsigned sec, frac; |
| 10885 | unsigned long t; |
| 10886 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 10887 | sec = t / clk_tck; |
| 10888 | frac = t % clk_tck; |
| 10889 | printf("%um%u.%03us%c", |
| 10890 | sec / 60, sec % 60, |
| 10891 | (frac * 1000) / clk_tck, |
| 10892 | p[0]); |
| 10893 | p += 2; |
| 10894 | } while (*p); |
| 10895 | |
| 10896 | return EXIT_SUCCESS; |
| 10897 | } |
| 10898 | #endif |
| 10899 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10900 | #if ENABLE_HUSH_MEMLEAK |
| 10901 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 10902 | { |
| 10903 | void *p; |
| 10904 | unsigned long l; |
| 10905 | |
| 10906 | # ifdef M_TRIM_THRESHOLD |
| 10907 | /* Optional. Reduces probability of false positives */ |
| 10908 | malloc_trim(0); |
| 10909 | # endif |
| 10910 | /* Crude attempt to find where "free memory" starts, |
| 10911 | * sans fragmentation. */ |
| 10912 | p = malloc(240); |
| 10913 | l = (unsigned long)p; |
| 10914 | free(p); |
| 10915 | p = malloc(3400); |
| 10916 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 10917 | free(p); |
| 10918 | |
| 10919 | |
| 10920 | # if 0 /* debug */ |
| 10921 | { |
| 10922 | struct mallinfo mi = mallinfo(); |
| 10923 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 10924 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 10925 | } |
| 10926 | # endif |
| 10927 | |
| 10928 | if (!G.memleak_value) |
| 10929 | G.memleak_value = l; |
| 10930 | |
| 10931 | l -= G.memleak_value; |
| 10932 | if ((long)l < 0) |
| 10933 | l = 0; |
| 10934 | l /= 1024; |
| 10935 | if (l > 127) |
| 10936 | l = 127; |
| 10937 | |
| 10938 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 10939 | return l; |
| 10940 | } |
| 10941 | #endif |