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 |
| 50 | * builtins mandated by standards we don't support: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 51 | * [un]alias, command, fc: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 52 | * command -v CMD: print "/path/to/CMD" |
| 53 | * prints "CMD" for builtins |
| 54 | * prints "alias ALIAS='EXPANSION'" for aliases |
| 55 | * prints nothing and sets $? to 1 if not found |
| 56 | * command -V CMD: print "CMD is /path/CMD|a shell builtin|etc" |
| 57 | * command [-p] CMD: run CMD, even if a function CMD also exists |
| 58 | * (can use this to override standalone shell as well) |
| 59 | * -p: use default $PATH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 60 | * command BLTIN: disables special-ness (e.g. errors do not abort) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 61 | * fc -l[nr] [BEG] [END]: list range of commands in history |
| 62 | * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands |
| 63 | * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 64 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 65 | * Bash compat TODO: |
| 66 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 67 | * reserved words: function select |
| 68 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 69 | * process substitution: <(list) and >(list) |
| 70 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 71 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 72 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 73 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 74 | * 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] | 75 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 76 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 77 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 78 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 79 | * indirect expansion: ${!VAR} |
| 80 | * substring op on @: ${@:n:m} |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 81 | * |
| 82 | * Won't do: |
Denys Vlasenko | 203fd7b | 2017-07-17 16:13:35 +0200 | [diff] [blame] | 83 | * Some builtins mandated by standards: |
| 84 | * newgrp [GRP]: not a builtin in bash but a suid binary |
| 85 | * which spawns a new shell with new group ID |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 86 | * In bash, export builtin is special, its arguments are assignments |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 87 | * and therefore expansion of them should be "one-word" expansion: |
| 88 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 89 | * compare with: |
| 90 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 91 | * ls: cannot access i=a: No such file or directory |
| 92 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 93 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 94 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 95 | * is not quoted: |
| 96 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 97 | * aaa bbb |
| 98 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 99 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 100 | */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 101 | //config:config HUSH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 102 | //config: bool "hush (64 kb)" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //config: default y |
| 104 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 105 | //config: hush is a small shell. It handles the normal flow control |
| 106 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 107 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 108 | //config: and functions are supported. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 109 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 110 | //config: It will compile and work on no-mmu systems. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 111 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 112 | //config: It does not handle select, aliases, tilde expansion, |
| 113 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 114 | //config: |
| 115 | //config:config HUSH_BASH_COMPAT |
| 116 | //config: bool "bash-compatible extensions" |
| 117 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 118 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 119 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 120 | //config:config HUSH_BRACE_EXPANSION |
| 121 | //config: bool "Brace expansion" |
| 122 | //config: default y |
| 123 | //config: depends on HUSH_BASH_COMPAT |
| 124 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 125 | //config: Enable {abc,def} extension. |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 126 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 127 | //config:config HUSH_INTERACTIVE |
| 128 | //config: bool "Interactive mode" |
| 129 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 130 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 131 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 132 | //config: Enable interactive mode (prompt and command editing). |
| 133 | //config: Without this, hush simply reads and executes commands |
| 134 | //config: from stdin just like a shell script from a file. |
| 135 | //config: No prompt, no PS1/PS2 magic shell variables. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 136 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 137 | //config:config HUSH_SAVEHISTORY |
| 138 | //config: bool "Save command history to .hush_history" |
| 139 | //config: default y |
| 140 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 141 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 142 | //config:config HUSH_JOB |
| 143 | //config: bool "Job control" |
| 144 | //config: default y |
| 145 | //config: depends on HUSH_INTERACTIVE |
| 146 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 147 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 148 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 149 | //config: "cmd &" still works by simply spawning a process and immediately |
| 150 | //config: prompting for next command (or executing next command in a script), |
| 151 | //config: but no separate process group is formed. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 152 | //config: |
| 153 | //config:config HUSH_TICK |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 154 | //config: bool "Support process substitution" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 155 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 156 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 157 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 158 | //config: Enable `command` and $(command). |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 159 | //config: |
| 160 | //config:config HUSH_IF |
| 161 | //config: bool "Support if/then/elif/else/fi" |
| 162 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 163 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 164 | //config: |
| 165 | //config:config HUSH_LOOPS |
| 166 | //config: bool "Support for, while and until loops" |
| 167 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 168 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 169 | //config: |
| 170 | //config:config HUSH_CASE |
| 171 | //config: bool "Support case ... esac statement" |
| 172 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 173 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 174 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 175 | //config: Enable case ... esac statement. +400 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 176 | //config: |
| 177 | //config:config HUSH_FUNCTIONS |
| 178 | //config: bool "Support funcname() { commands; } syntax" |
| 179 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 180 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 181 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 182 | //config: Enable support for shell functions. +800 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 183 | //config: |
| 184 | //config:config HUSH_LOCAL |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 185 | //config: bool "local builtin" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 186 | //config: default y |
| 187 | //config: depends on HUSH_FUNCTIONS |
| 188 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 189 | //config: Enable support for local variables in functions. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 190 | //config: |
| 191 | //config:config HUSH_RANDOM_SUPPORT |
| 192 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 193 | //config: default y |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 194 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 195 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 196 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 197 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 198 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 199 | //config:config HUSH_MODE_X |
| 200 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 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: This instructs hush to print commands before execution. |
| 205 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 206 | //config: |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 207 | //config:config HUSH_ECHO |
| 208 | //config: bool "echo builtin" |
| 209 | //config: default y |
| 210 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 211 | //config: |
| 212 | //config:config HUSH_PRINTF |
| 213 | //config: bool "printf builtin" |
| 214 | //config: default y |
| 215 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 216 | //config: |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 217 | //config:config HUSH_TEST |
| 218 | //config: bool "test builtin" |
| 219 | //config: default y |
| 220 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 221 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 222 | //config:config HUSH_HELP |
| 223 | //config: bool "help builtin" |
| 224 | //config: default y |
| 225 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 226 | //config: |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 227 | //config:config HUSH_EXPORT |
| 228 | //config: bool "export builtin" |
| 229 | //config: default y |
| 230 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 231 | //config: |
| 232 | //config:config HUSH_EXPORT_N |
| 233 | //config: bool "Support 'export -n' option" |
| 234 | //config: default y |
| 235 | //config: depends on HUSH_EXPORT |
| 236 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 237 | //config: export -n unexports variables. It is a bash extension. |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 238 | //config: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 239 | //config:config HUSH_READONLY |
| 240 | //config: bool "readonly builtin" |
| 241 | //config: default y |
Denys Vlasenko | 6b0695b | 2017-07-17 21:47:27 +0200 | [diff] [blame] | 242 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 243 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 244 | //config: Enable support for read-only variables. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 245 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 246 | //config:config HUSH_KILL |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 247 | //config: bool "kill builtin (supports kill %jobspec)" |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 248 | //config: default y |
| 249 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 250 | //config: |
| 251 | //config:config HUSH_WAIT |
| 252 | //config: bool "wait builtin" |
| 253 | //config: default y |
| 254 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 255 | //config: |
| 256 | //config:config HUSH_TRAP |
| 257 | //config: bool "trap builtin" |
| 258 | //config: default y |
| 259 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 260 | //config: |
| 261 | //config:config HUSH_TYPE |
| 262 | //config: bool "type builtin" |
| 263 | //config: default y |
| 264 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 265 | //config: |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 266 | //config:config HUSH_TIMES |
| 267 | //config: bool "times builtin" |
| 268 | //config: default y |
| 269 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 270 | //config: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 271 | //config:config HUSH_READ |
| 272 | //config: bool "read builtin" |
| 273 | //config: default y |
| 274 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 275 | //config: |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 276 | //config:config HUSH_SET |
| 277 | //config: bool "set builtin" |
| 278 | //config: default y |
| 279 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 280 | //config: |
| 281 | //config:config HUSH_UNSET |
| 282 | //config: bool "unset builtin" |
| 283 | //config: default y |
| 284 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 285 | //config: |
| 286 | //config:config HUSH_ULIMIT |
| 287 | //config: bool "ulimit builtin" |
| 288 | //config: default y |
| 289 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 290 | //config: |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 291 | //config:config HUSH_UMASK |
| 292 | //config: bool "umask builtin" |
| 293 | //config: default y |
| 294 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 295 | //config: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 296 | //config:config HUSH_GETOPTS |
| 297 | //config: bool "getopts builtin" |
| 298 | //config: default y |
| 299 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
| 300 | //config: |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 301 | //config:config HUSH_MEMLEAK |
| 302 | //config: bool "memleak builtin (debugging)" |
| 303 | //config: default n |
| 304 | //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 305 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 306 | //applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 307 | // APPLET_ODDNAME:name main location suid_type help |
Denys Vlasenko | 205d48e | 2017-01-29 14:57:33 +0100 | [diff] [blame] | 308 | //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] | 309 | //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] | 310 | |
| 311 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 312 | //kbuild:lib-$(CONFIG_SH_IS_HUSH) += hush.o match.o shell_common.o |
| 313 | //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] | 314 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 315 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 316 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 317 | * but currently do nothing, therefore aren't shown in help. |
| 318 | * NOMMU-specific options are not meant to be used by users, |
| 319 | * therefore we don't show them either. |
| 320 | */ |
| 321 | //usage:#define hush_trivial_usage |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 322 | //usage: "[-enxl] [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 323 | //usage:#define hush_full_usage "\n\n" |
| 324 | //usage: "Unix shell interpreter" |
| 325 | |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 326 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 327 | || defined(__APPLE__) \ |
| 328 | ) |
| 329 | # include <malloc.h> /* for malloc_trim */ |
| 330 | #endif |
| 331 | #include <glob.h> |
| 332 | /* #include <dmalloc.h> */ |
| 333 | #if ENABLE_HUSH_CASE |
| 334 | # include <fnmatch.h> |
| 335 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 336 | #include <sys/times.h> |
Denys Vlasenko | 6704746 | 2016-12-22 15:21:58 +0100 | [diff] [blame] | 337 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
| 338 | |
| 339 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
| 340 | #include "unicode.h" |
| 341 | #include "shell_common.h" |
| 342 | #include "math.h" |
| 343 | #include "match.h" |
| 344 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 345 | # include "random.h" |
| 346 | #else |
| 347 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
| 348 | #endif |
| 349 | #ifndef F_DUPFD_CLOEXEC |
| 350 | # define F_DUPFD_CLOEXEC F_DUPFD |
| 351 | #endif |
| 352 | #ifndef PIPE_BUF |
| 353 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
| 354 | #endif |
| 355 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 356 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 357 | /* So far, all bash compat is controlled by one config option */ |
| 358 | /* Separate defines document which part of code implements what */ |
| 359 | #define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT |
| 360 | #define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 361 | #define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT |
| 362 | #define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 363 | #define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST) |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 364 | #define BASH_READ_D ENABLE_HUSH_BASH_COMPAT |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 365 | |
| 366 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 367 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 368 | #define LEAK_HUNTING 0 |
| 369 | #define BUILD_AS_NOMMU 0 |
| 370 | /* Enable/disable sanity checks. Ok to enable in production, |
| 371 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 372 | * Keeping 1 for now even in released versions. |
| 373 | */ |
| 374 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 375 | /* Slightly bigger (+200 bytes), but faster hush. |
| 376 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 377 | * which allows us to do fewer waitpid's. |
| 378 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 379 | * and therefore waitpid will return the same result as last time) |
| 380 | */ |
| 381 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 382 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 383 | * So far ${var%...} ops are always enabled: |
| 384 | */ |
| 385 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 386 | |
| 387 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 388 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 389 | # undef BB_MMU |
| 390 | # undef USE_FOR_NOMMU |
| 391 | # undef USE_FOR_MMU |
| 392 | # define BB_MMU 0 |
| 393 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 394 | # define USE_FOR_MMU(...) |
| 395 | #endif |
| 396 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 397 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 398 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 399 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 400 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 401 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 402 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 403 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 404 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 405 | # define IF_FEATURE_SH_STANDALONE(...) |
| 406 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 407 | #endif |
| 408 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 409 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 410 | # undef ENABLE_FEATURE_EDITING |
| 411 | # define ENABLE_FEATURE_EDITING 0 |
| 412 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 413 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denys Vlasenko | 8cab667 | 2012-04-20 14:48:00 +0200 | [diff] [blame] | 414 | # undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 415 | # define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 416 | #endif |
| 417 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 418 | /* Do we support ANY keywords? */ |
| 419 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 420 | # define HAS_KEYWORDS 1 |
| 421 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 422 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 423 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 424 | # define HAS_KEYWORDS 0 |
| 425 | # define IF_HAS_KEYWORDS(...) |
| 426 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 427 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 428 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 429 | /* If you comment out one of these below, it will be #defined later |
| 430 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 431 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 432 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 433 | #define debug_printf_parse(...) do {} while (0) |
| 434 | #define debug_print_tree(a, b) do {} while (0) |
| 435 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 436 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 437 | #define debug_printf_jobs(...) do {} while (0) |
| 438 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 439 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 440 | #define debug_printf_glob(...) do {} while (0) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 441 | #define debug_printf_redir(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 442 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 443 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 444 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 445 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 446 | #define ERR_PTR ((void*)(long)1) |
| 447 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 448 | #define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 449 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 450 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 451 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 452 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 453 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 454 | /* Support / and // replace ops */ |
| 455 | /* Note that // is stored as \ in "encoded" string representation */ |
| 456 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 457 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 458 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 459 | #else |
| 460 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 461 | # define VAR_SUBST_OPS "%#:-=+?" |
| 462 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 463 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 464 | |
| 465 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 466 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 467 | struct variable; |
| 468 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 469 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 470 | |
| 471 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 472 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 473 | */ |
| 474 | #if !BB_MMU |
| 475 | typedef struct nommu_save_t { |
| 476 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 477 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 478 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 479 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 480 | } nommu_save_t; |
| 481 | #endif |
| 482 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 483 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 484 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 485 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 486 | RES_IF , |
| 487 | RES_THEN , |
| 488 | RES_ELIF , |
| 489 | RES_ELSE , |
| 490 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 491 | #endif |
| 492 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 493 | RES_FOR , |
| 494 | RES_WHILE , |
| 495 | RES_UNTIL , |
| 496 | RES_DO , |
| 497 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 498 | #endif |
| 499 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 500 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 501 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 502 | #if ENABLE_HUSH_CASE |
| 503 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 504 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 505 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 506 | RES_MATCH , /* "word)" */ |
| 507 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 508 | RES_ESAC , |
| 509 | #endif |
| 510 | RES_XXXX , |
| 511 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 512 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 513 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 514 | typedef struct o_string { |
| 515 | char *data; |
| 516 | int length; /* position where data is appended */ |
| 517 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 518 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 519 | /* At least some part of the string was inside '' or "", |
| 520 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 521 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 522 | smallint has_empty_slot; |
| 523 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 524 | } o_string; |
| 525 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 526 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 527 | EXP_FLAG_GLOB = 0x2, |
| 528 | /* Protect newly added chars against globbing |
| 529 | * by prepending \ to *, ?, [, \ */ |
| 530 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 531 | }; |
| 532 | enum { |
| 533 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 534 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 535 | NOT_ASSIGNMENT = 2, |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 536 | /* Not an assignment, but next word may be: "if v=xyz cmd;" */ |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 537 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 538 | }; |
| 539 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 540 | #define NULL_O_STRING { NULL } |
| 541 | |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 542 | #ifndef debug_printf_parse |
| 543 | static const char *const assignment_flag[] = { |
| 544 | "MAYBE_ASSIGNMENT", |
| 545 | "DEFINITELY_ASSIGNMENT", |
| 546 | "NOT_ASSIGNMENT", |
| 547 | "WORD_IS_KEYWORD", |
| 548 | }; |
| 549 | #endif |
| 550 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 551 | typedef struct in_str { |
| 552 | const char *p; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 553 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 554 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 555 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 556 | int peek_buf[2]; |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 557 | int last_char; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 558 | FILE *file; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 559 | } in_str; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 560 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 561 | /* The descrip member of this structure is only used to make |
| 562 | * debugging output pretty */ |
| 563 | static const struct { |
| 564 | int mode; |
| 565 | signed char default_fd; |
| 566 | char descrip[3]; |
| 567 | } redir_table[] = { |
| 568 | { O_RDONLY, 0, "<" }, |
| 569 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 570 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 571 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 572 | { O_RDONLY, 0, "<<" }, |
| 573 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 574 | /* { O_RDONLY, 77, "<<" }, */ |
| 575 | }; |
| 576 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 577 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 578 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 579 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 580 | int rd_fd; /* fd to redirect */ |
| 581 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 582 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 583 | smallint rd_type; /* (enum redir_type) */ |
| 584 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 585 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 586 | * bit 0: do we need to trim leading tabs? |
| 587 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 588 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 589 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 590 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 591 | REDIRECT_INPUT = 0, |
| 592 | REDIRECT_OVERWRITE = 1, |
| 593 | REDIRECT_APPEND = 2, |
| 594 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 595 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 596 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 597 | |
| 598 | REDIRFD_CLOSE = -3, |
| 599 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 600 | REDIRFD_TO_FILE = -1, |
| 601 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 602 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 603 | HEREDOC_SKIPTABS = 1, |
| 604 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 605 | } redir_type; |
| 606 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 607 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 608 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 609 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 610 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 611 | smallint cmd_type; /* CMD_xxx */ |
| 612 | #define CMD_NORMAL 0 |
| 613 | #define CMD_SUBSHELL 1 |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 614 | #if BASH_TEST2 |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 615 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 616 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 617 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 618 | #if ENABLE_HUSH_FUNCTIONS |
| 619 | # define CMD_FUNCDEF 3 |
| 620 | #endif |
| 621 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 622 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 623 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 624 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 625 | #if !BB_MMU |
| 626 | char *group_as_string; |
| 627 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 628 | #if ENABLE_HUSH_FUNCTIONS |
| 629 | struct function *child_func; |
| 630 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 631 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 632 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 633 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 634 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 635 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 636 | * we put those fields back into cmd->xxx |
| 637 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 638 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 639 | * Without this trick, loop would execute a;b;b;b;... |
| 640 | * instead of correct sequence a;b;a;b;... |
| 641 | * When command is freed, it severs the link |
| 642 | * (sets ->child_func->parent_cmd to NULL). |
| 643 | */ |
| 644 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 645 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 646 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 647 | * and on execution these are substituted with their values. |
| 648 | * Substitution can make _several_ words out of one argv[n]! |
| 649 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 650 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 651 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 652 | struct redir_struct *redirects; /* I/O redirections */ |
| 653 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 654 | /* Is there anything in this command at all? */ |
| 655 | #define IS_NULL_CMD(cmd) \ |
| 656 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 657 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 658 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 659 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 660 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 661 | int alive_cmds; /* number of commands running (not exited) */ |
| 662 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 663 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 664 | unsigned jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 665 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 666 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 667 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 668 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 669 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 670 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 671 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 672 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 673 | typedef enum pipe_style { |
Denys Vlasenko | 00a06b9 | 2016-11-08 20:35:53 +0100 | [diff] [blame] | 674 | PIPE_SEQ = 0, |
| 675 | PIPE_AND = 1, |
| 676 | PIPE_OR = 2, |
| 677 | PIPE_BG = 3, |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 678 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 679 | /* Is there anything in this pipe at all? */ |
| 680 | #define IS_NULL_PIPE(pi) \ |
| 681 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 682 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 683 | /* This holds pointers to the various results of parsing */ |
| 684 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 685 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 686 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 687 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 688 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 689 | /* last command in pipe (being constructed right now) */ |
| 690 | struct command *command; |
| 691 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 692 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 693 | #if !BB_MMU |
| 694 | o_string as_string; |
| 695 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 696 | #if HAS_KEYWORDS |
| 697 | smallint ctx_res_w; |
| 698 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 699 | #if ENABLE_HUSH_CASE |
| 700 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 701 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 702 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 703 | int old_flag; |
| 704 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 705 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 706 | * when we see "if" or "then", we malloc and copy current context, |
| 707 | * and make ->stack point to it. then we parse pipeN. |
| 708 | * when closing "then" / fi" / whatever is found, |
| 709 | * we move list_head into ->stack->command->group, |
| 710 | * copy ->stack into current context, and delete ->stack. |
| 711 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 712 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 713 | struct parse_context *stack; |
| 714 | #endif |
| 715 | }; |
| 716 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 717 | /* On program start, environ points to initial environment. |
| 718 | * putenv adds new pointers into it, unsetenv removes them. |
| 719 | * Neither of these (de)allocates the strings. |
| 720 | * setenv allocates new strings in malloc space and does putenv, |
| 721 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 722 | #define setenv(...) setenv_is_leaky_dont_use() |
| 723 | struct variable { |
| 724 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 725 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 726 | #if ENABLE_HUSH_LOCAL |
| 727 | unsigned func_nest_level; |
| 728 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 729 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 730 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 731 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 734 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 735 | BC_BREAK = 1, |
| 736 | BC_CONTINUE = 2, |
| 737 | }; |
| 738 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 739 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 740 | struct function { |
| 741 | struct function *next; |
| 742 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 743 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 744 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 745 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 746 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 747 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 748 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 749 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 750 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 751 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 752 | /* set -/+o OPT support. (TODO: make it optional) |
| 753 | * bash supports the following opts: |
| 754 | * allexport off |
| 755 | * braceexpand on |
| 756 | * emacs on |
| 757 | * errexit off |
| 758 | * errtrace off |
| 759 | * functrace off |
| 760 | * hashall on |
| 761 | * histexpand off |
| 762 | * history on |
| 763 | * ignoreeof off |
| 764 | * interactive-comments on |
| 765 | * keyword off |
| 766 | * monitor on |
| 767 | * noclobber off |
| 768 | * noexec off |
| 769 | * noglob off |
| 770 | * nolog off |
| 771 | * notify off |
| 772 | * nounset off |
| 773 | * onecmd off |
| 774 | * physical off |
| 775 | * pipefail off |
| 776 | * posix off |
| 777 | * privileged off |
| 778 | * verbose off |
| 779 | * vi off |
| 780 | * xtrace off |
| 781 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 782 | static const char o_opt_strings[] ALIGN1 = |
| 783 | "pipefail\0" |
| 784 | "noexec\0" |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 785 | "errexit\0" |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 786 | #if ENABLE_HUSH_MODE_X |
| 787 | "xtrace\0" |
| 788 | #endif |
| 789 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 790 | enum { |
| 791 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 792 | OPT_O_NOEXEC, |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 793 | OPT_O_ERREXIT, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 794 | #if ENABLE_HUSH_MODE_X |
| 795 | OPT_O_XTRACE, |
| 796 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 797 | NUM_OPT_O |
| 798 | }; |
| 799 | |
| 800 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 801 | struct FILE_list { |
| 802 | struct FILE_list *next; |
| 803 | FILE *fp; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 804 | int fd; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 805 | }; |
| 806 | |
| 807 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 808 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 809 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 810 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 811 | /* interactive_fd != 0 means we are an interactive shell. |
| 812 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 813 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 814 | * job control still works, but terminal signals |
| 815 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 816 | * process groups can only be created with "cmd &". |
| 817 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 818 | * to give tty to the foreground process group, |
| 819 | * and will take it back when the group is stopped (^Z) |
| 820 | * or killed (^C). |
| 821 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 822 | #if ENABLE_HUSH_INTERACTIVE |
| 823 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 824 | * _AND_ if we decided to act interactively */ |
| 825 | int interactive_fd; |
| 826 | const char *PS1; |
| 827 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 828 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 829 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 830 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 831 | #endif |
| 832 | #if ENABLE_FEATURE_EDITING |
| 833 | line_input_t *line_input_state; |
| 834 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 835 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 836 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 837 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 838 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 839 | random_t random_gen; |
| 840 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 841 | #if ENABLE_HUSH_JOB |
| 842 | int run_list_level; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 843 | unsigned last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 844 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 845 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 846 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 847 | #else |
| 848 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 849 | #endif |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 850 | /* How deeply are we in context where "set -e" is ignored */ |
| 851 | int errexit_depth; |
| 852 | /* "set -e" rules (do we follow them correctly?): |
| 853 | * Exit if pipe, list, or compound command exits with a non-zero status. |
| 854 | * Shell does not exit if failed command is part of condition in |
| 855 | * if/while, part of && or || list except the last command, any command |
| 856 | * in a pipe but the last, or if the command's return value is being |
| 857 | * inverted with !. If a compound command other than a subshell returns a |
| 858 | * non-zero status because a command failed while -e was being ignored, the |
| 859 | * shell does not exit. A trap on ERR, if set, is executed before the shell |
| 860 | * exits [ERR is a bashism]. |
| 861 | * |
| 862 | * If a compound command or function executes in a context where -e is |
| 863 | * ignored, none of the commands executed within are affected by the -e |
| 864 | * setting. If a compound command or function sets -e while executing in a |
| 865 | * context where -e is ignored, that setting does not have any effect until |
| 866 | * the compound command or the command containing the function call completes. |
| 867 | */ |
| 868 | |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 869 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 870 | #if ENABLE_HUSH_MODE_X |
| 871 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 872 | #else |
| 873 | # define G_x_mode 0 |
| 874 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 875 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 876 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 877 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 878 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 879 | #if ENABLE_HUSH_FUNCTIONS |
| 880 | /* 0: outside of a function (or sourced file) |
| 881 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 882 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 883 | */ |
| 884 | smallint flag_return_in_progress; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 885 | # define G_flag_return_in_progress (G.flag_return_in_progress) |
| 886 | #else |
| 887 | # define G_flag_return_in_progress 0 |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 888 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 889 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 890 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 891 | smalluint last_exitcode; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 892 | smalluint last_bg_pid_exitcode; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 893 | #if ENABLE_HUSH_SET |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 894 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 895 | smalluint global_args_malloced; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 896 | # define G_global_args_malloced (G.global_args_malloced) |
| 897 | #else |
| 898 | # define G_global_args_malloced 0 |
| 899 | #endif |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 900 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 901 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 902 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 903 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 904 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 905 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 906 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 907 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 908 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 909 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 910 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 911 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 912 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 913 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 914 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 915 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 916 | # if ENABLE_HUSH_LOCAL |
| 917 | struct variable **shadowed_vars_pp; |
| 918 | unsigned func_nest_level; |
| 919 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 920 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 921 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 922 | #if ENABLE_HUSH_FAST |
| 923 | unsigned count_SIGCHLD; |
| 924 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 925 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 926 | #endif |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 927 | struct FILE_list *FILE_list; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 928 | /* Which signals have non-DFL handler (even with no traps set)? |
| 929 | * Set at the start to: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 930 | * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS) |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 931 | * SPECIAL_INTERACTIVE_SIGS are cleared after fork. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 932 | * The rest is cleared right before execv syscalls. |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 933 | * Other than these two times, never modified. |
| 934 | */ |
| 935 | unsigned special_sig_mask; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 936 | #if ENABLE_HUSH_JOB |
| 937 | unsigned fatal_sig_mask; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 938 | # define G_fatal_sig_mask (G.fatal_sig_mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 939 | #else |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 940 | # define G_fatal_sig_mask 0 |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 941 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 942 | #if ENABLE_HUSH_TRAP |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 943 | char **traps; /* char *traps[NSIG] */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 944 | # define G_traps G.traps |
| 945 | #else |
| 946 | # define G_traps ((char**)NULL) |
| 947 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 948 | sigset_t pending_set; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 949 | #if ENABLE_HUSH_MEMLEAK |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 950 | unsigned long memleak_value; |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 951 | #endif |
| 952 | #if HUSH_DEBUG |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 953 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 954 | #endif |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 955 | struct sigaction sa; |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 956 | #if ENABLE_FEATURE_EDITING |
| 957 | char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; |
| 958 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 959 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 960 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 961 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 962 | * (too many defines). Also, I actually prefer to see when a variable |
| 963 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 964 | #define INIT_G() do { \ |
| 965 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 966 | /* memset(&G.sa, 0, sizeof(G.sa)); */ \ |
| 967 | sigfillset(&G.sa.sa_mask); \ |
| 968 | G.sa.sa_flags = SA_RESTART; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 969 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 970 | |
| 971 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 972 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 973 | static int builtin_cd(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 974 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 975 | static int builtin_echo(char **argv) FAST_FUNC; |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 976 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 977 | static int builtin_eval(char **argv) FAST_FUNC; |
| 978 | static int builtin_exec(char **argv) FAST_FUNC; |
| 979 | static int builtin_exit(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 980 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 981 | static int builtin_export(char **argv) FAST_FUNC; |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 982 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 983 | #if ENABLE_HUSH_READONLY |
| 984 | static int builtin_readonly(char **argv) FAST_FUNC; |
| 985 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 986 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 987 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 988 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 989 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 990 | #if ENABLE_HUSH_GETOPTS |
| 991 | static int builtin_getopts(char **argv) FAST_FUNC; |
| 992 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 993 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 994 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 995 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 996 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 997 | static int builtin_history(char **argv) FAST_FUNC; |
| 998 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 999 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1000 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1001 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1002 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1003 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1004 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1005 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1006 | static int builtin_printf(char **argv) FAST_FUNC; |
| 1007 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1008 | static int builtin_pwd(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1009 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1010 | static int builtin_read(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1011 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1012 | #if ENABLE_HUSH_SET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1013 | static int builtin_set(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1014 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1015 | static int builtin_shift(char **argv) FAST_FUNC; |
| 1016 | static int builtin_source(char **argv) FAST_FUNC; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1017 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1018 | static int builtin_test(char **argv) FAST_FUNC; |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1019 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1020 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1021 | static int builtin_trap(char **argv) FAST_FUNC; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1022 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1023 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1024 | static int builtin_type(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1025 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1026 | #if ENABLE_HUSH_TIMES |
| 1027 | static int builtin_times(char **argv) FAST_FUNC; |
| 1028 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1029 | static int builtin_true(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1030 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1031 | static int builtin_umask(char **argv) FAST_FUNC; |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1032 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1033 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1034 | static int builtin_unset(char **argv) FAST_FUNC; |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1035 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1036 | #if ENABLE_HUSH_KILL |
| 1037 | static int builtin_kill(char **argv) FAST_FUNC; |
| 1038 | #endif |
| 1039 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1040 | static int builtin_wait(char **argv) FAST_FUNC; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1041 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1042 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1043 | static int builtin_break(char **argv) FAST_FUNC; |
| 1044 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1045 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1046 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1047 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1048 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1049 | |
| 1050 | /* Table of built-in functions. They can be forked or not, depending on |
| 1051 | * context: within pipes, they fork. As simple commands, they do not. |
| 1052 | * When used in non-forking context, they can change global variables |
| 1053 | * in the parent shell process. If forked, of course they cannot. |
| 1054 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 1055 | * still be set at the end. */ |
| 1056 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1057 | const char *b_cmd; |
| 1058 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1059 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 1060 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1061 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1062 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1063 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1064 | #endif |
| 1065 | }; |
| 1066 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1067 | static const struct built_in_command bltins1[] = { |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1068 | BLTIN("." , builtin_source , "Run commands in file"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1069 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1070 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1071 | BLTIN("bg" , builtin_fg_bg , "Resume job in background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1072 | #endif |
| 1073 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1074 | BLTIN("break" , builtin_break , "Exit loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1075 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1076 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1077 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1078 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1079 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1080 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 1081 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1082 | BLTIN("exit" , builtin_exit , NULL), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1083 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1084 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 1085 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1086 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1087 | BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1088 | #endif |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 1089 | #if ENABLE_HUSH_GETOPTS |
| 1090 | BLTIN("getopts" , builtin_getopts , NULL), |
| 1091 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1092 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1093 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1094 | #endif |
Denys Vlasenko | ff463a8 | 2013-05-12 02:45:23 +0200 | [diff] [blame] | 1095 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1096 | BLTIN("history" , builtin_history , "Show history"), |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 1097 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1098 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1099 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 1100 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1101 | #if ENABLE_HUSH_KILL |
| 1102 | BLTIN("kill" , builtin_kill , "Send signals to processes"), |
| 1103 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1104 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1105 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1106 | #endif |
Denys Vlasenko | 4471969 | 2017-01-08 18:44:41 +0100 | [diff] [blame] | 1107 | #if ENABLE_HUSH_MEMLEAK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1108 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 1109 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1110 | #if ENABLE_HUSH_READ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1111 | BLTIN("read" , builtin_read , "Input into variable"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1112 | #endif |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 1113 | #if ENABLE_HUSH_READONLY |
| 1114 | BLTIN("readonly" , builtin_readonly, "Make variables read-only"), |
| 1115 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1116 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1117 | BLTIN("return" , builtin_return , "Return from function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 1118 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1119 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1120 | BLTIN("set" , builtin_set , "Set positional parameters"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1121 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1122 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 1123 | #if BASH_SOURCE |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1124 | BLTIN("source" , builtin_source , NULL), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 1125 | #endif |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 1126 | #if ENABLE_HUSH_TIMES |
| 1127 | BLTIN("times" , builtin_times , NULL), |
| 1128 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1129 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1130 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1131 | #endif |
Denys Vlasenko | 2bba591 | 2014-03-14 12:43:57 +0100 | [diff] [blame] | 1132 | BLTIN("true" , builtin_true , NULL), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1133 | #if ENABLE_HUSH_TYPE |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1134 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1135 | #endif |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1136 | #if ENABLE_HUSH_ULIMIT |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1137 | BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"), |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1138 | #endif |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1139 | #if ENABLE_HUSH_UMASK |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1140 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
Denys Vlasenko | d5933b1 | 2017-01-08 18:31:39 +0100 | [diff] [blame] | 1141 | #endif |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1142 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1143 | BLTIN("unset" , builtin_unset , "Unset variables"), |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 1144 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1145 | #if ENABLE_HUSH_WAIT |
Denys Vlasenko | d2c15bc | 2017-07-18 18:14:42 +0200 | [diff] [blame] | 1146 | BLTIN("wait" , builtin_wait , "Wait for process to finish"), |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1147 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1148 | }; |
Denys Vlasenko | 80f806c | 2017-01-10 16:51:10 +0100 | [diff] [blame] | 1149 | /* These builtins won't be used if we are on NOMMU and need to re-exec |
| 1150 | * (it's cheaper to run an external program in this case): |
| 1151 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1152 | static const struct built_in_command bltins2[] = { |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1153 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1154 | BLTIN("[" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1155 | #endif |
Denys Vlasenko | 8944c67 | 2017-01-11 14:22:00 +0100 | [diff] [blame] | 1156 | #if BASH_TEST2 |
| 1157 | BLTIN("[[" , builtin_test , NULL), |
| 1158 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1159 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1160 | BLTIN("echo" , builtin_echo , NULL), |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 1161 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 1162 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 1163 | BLTIN("printf" , builtin_printf , NULL), |
| 1164 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1165 | BLTIN("pwd" , builtin_pwd , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1166 | #if ENABLE_HUSH_TEST |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1167 | BLTIN("test" , builtin_test , NULL), |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 1168 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1169 | }; |
| 1170 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 1171 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1172 | /* Debug printouts. |
| 1173 | */ |
| 1174 | #if HUSH_DEBUG |
| 1175 | /* prevent disasters with G.debug_indent < 0 */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1176 | # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "") |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1177 | # define debug_enter() (G.debug_indent++) |
| 1178 | # define debug_leave() (G.debug_indent--) |
| 1179 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1180 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1181 | # define debug_enter() ((void)0) |
| 1182 | # define debug_leave() ((void)0) |
| 1183 | #endif |
| 1184 | |
| 1185 | #ifndef debug_printf |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1186 | # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1187 | #endif |
| 1188 | |
| 1189 | #ifndef debug_printf_parse |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1190 | # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1191 | #endif |
| 1192 | |
| 1193 | #ifndef debug_printf_exec |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1194 | #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1195 | #endif |
| 1196 | |
| 1197 | #ifndef debug_printf_env |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1198 | # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1199 | #endif |
| 1200 | |
| 1201 | #ifndef debug_printf_jobs |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1202 | # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1203 | # define DEBUG_JOBS 1 |
| 1204 | #else |
| 1205 | # define DEBUG_JOBS 0 |
| 1206 | #endif |
| 1207 | |
| 1208 | #ifndef debug_printf_expand |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1209 | # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1210 | # define DEBUG_EXPAND 1 |
| 1211 | #else |
| 1212 | # define DEBUG_EXPAND 0 |
| 1213 | #endif |
| 1214 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1215 | #ifndef debug_printf_varexp |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1216 | # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1217 | #endif |
| 1218 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1219 | #ifndef debug_printf_glob |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1220 | # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1221 | # define DEBUG_GLOB 1 |
| 1222 | #else |
| 1223 | # define DEBUG_GLOB 0 |
| 1224 | #endif |
| 1225 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1226 | #ifndef debug_printf_redir |
| 1227 | # define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__)) |
| 1228 | #endif |
| 1229 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1230 | #ifndef debug_printf_list |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1231 | # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1232 | #endif |
| 1233 | |
| 1234 | #ifndef debug_printf_subst |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1235 | # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1236 | #endif |
| 1237 | |
| 1238 | #ifndef debug_printf_clean |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1239 | # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__)) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1240 | # define DEBUG_CLEAN 1 |
| 1241 | #else |
| 1242 | # define DEBUG_CLEAN 0 |
| 1243 | #endif |
| 1244 | |
| 1245 | #if DEBUG_EXPAND |
| 1246 | static void debug_print_strings(const char *prefix, char **vv) |
| 1247 | { |
| 1248 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1249 | fdprintf(2, "%s:\n", prefix); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1250 | while (*vv) |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1251 | fdprintf(2, " '%s'\n", *vv++); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1252 | } |
| 1253 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1254 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1255 | #endif |
| 1256 | |
| 1257 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1258 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1259 | */ |
| 1260 | #if LEAK_HUNTING |
| 1261 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1262 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1263 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1264 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1265 | return ptr; |
| 1266 | } |
| 1267 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1268 | { |
| 1269 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1270 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1271 | return ptr; |
| 1272 | } |
| 1273 | static char *xxstrdup(int lineno, const char *str) |
| 1274 | { |
| 1275 | char *ptr = xstrdup(str); |
| 1276 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1277 | return ptr; |
| 1278 | } |
| 1279 | static void xxfree(void *ptr) |
| 1280 | { |
| 1281 | fdprintf(2, "free %p\n", ptr); |
| 1282 | free(ptr); |
| 1283 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1284 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1285 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1286 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1287 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1288 | #endif |
| 1289 | |
| 1290 | |
| 1291 | /* Syntax and runtime errors. They always abort scripts. |
| 1292 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1293 | * and return to the prompt. |
| 1294 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1295 | */ |
| 1296 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1297 | # 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] | 1298 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1299 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1300 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1301 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1302 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1303 | #endif |
| 1304 | |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1305 | static void die_if_script(void) |
| 1306 | { |
| 1307 | if (!G_interactive_fd) { |
| 1308 | if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */ |
| 1309 | xfunc_error_retval = G.last_exitcode; |
| 1310 | xfunc_die(); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1315 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1316 | va_list p; |
| 1317 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1318 | #if HUSH_DEBUG >= 2 |
| 1319 | bb_error_msg("hush.c:%u", lineno); |
| 1320 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1321 | va_start(p, fmt); |
| 1322 | bb_verror_msg(fmt, p, NULL); |
| 1323 | va_end(p); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1324 | die_if_script(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1325 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1326 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1327 | static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1328 | { |
| 1329 | if (msg) |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1330 | bb_error_msg("syntax error: %s", msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1331 | else |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1332 | bb_error_msg("syntax error"); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1333 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1336 | static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1337 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1338 | bb_error_msg("syntax error at '%s'", msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1339 | die_if_script(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1342 | 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] | 1343 | { |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1344 | bb_error_msg("syntax error: unterminated %s", s); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1345 | //? source4.tests fails: in bash, echo ${^} in script does not terminate the script |
| 1346 | // die_if_script(); |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1349 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1350 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1351 | char msg[2] = { ch, '\0' }; |
| 1352 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1355 | static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1356 | { |
| 1357 | char msg[2]; |
| 1358 | msg[0] = ch; |
| 1359 | msg[1] = '\0'; |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 1360 | #if HUSH_DEBUG >= 2 |
| 1361 | bb_error_msg("hush.c:%u", lineno); |
| 1362 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 1363 | bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1364 | die_if_script(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1367 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1368 | # undef msg_and_die_if_script |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1369 | # undef syntax_error |
| 1370 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1371 | # undef syntax_error_unterm_ch |
| 1372 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1373 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1374 | #else |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1375 | # 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] | 1376 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1377 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1378 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1379 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1380 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1381 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1382 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1383 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1384 | #if ENABLE_HUSH_INTERACTIVE |
| 1385 | static void cmdedit_update_prompt(void); |
| 1386 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1387 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1388 | #endif |
| 1389 | |
| 1390 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1391 | /* Utility functions |
| 1392 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1393 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1394 | static char *unbackslash(char *src) |
| 1395 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1396 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1397 | while (1) { |
| 1398 | if (*src == '\\') |
| 1399 | src++; |
| 1400 | if ((*dst++ = *src++) == '\0') |
| 1401 | break; |
| 1402 | } |
| 1403 | return dst; |
| 1404 | } |
| 1405 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1406 | 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] | 1407 | { |
| 1408 | int i; |
| 1409 | unsigned count1; |
| 1410 | unsigned count2; |
| 1411 | char **v; |
| 1412 | |
| 1413 | v = strings; |
| 1414 | count1 = 0; |
| 1415 | if (v) { |
| 1416 | while (*v) { |
| 1417 | count1++; |
| 1418 | v++; |
| 1419 | } |
| 1420 | } |
| 1421 | count2 = 0; |
| 1422 | v = add; |
| 1423 | while (*v) { |
| 1424 | count2++; |
| 1425 | v++; |
| 1426 | } |
| 1427 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1428 | v[count1 + count2] = NULL; |
| 1429 | i = count2; |
| 1430 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1431 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1432 | return v; |
| 1433 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1434 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1435 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1436 | { |
| 1437 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1438 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1439 | return ptr; |
| 1440 | } |
| 1441 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1442 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1443 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1444 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1445 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1446 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1447 | { |
| 1448 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1449 | v[0] = add; |
| 1450 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1451 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1452 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1453 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1454 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1455 | { |
| 1456 | char **ptr = add_string_to_strings(strings, add); |
| 1457 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1458 | return ptr; |
| 1459 | } |
| 1460 | #define add_string_to_strings(strings, add) \ |
| 1461 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1462 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1463 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1464 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1465 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1466 | char **v; |
| 1467 | |
| 1468 | if (!strings) |
| 1469 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1470 | v = strings; |
| 1471 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1472 | free(*v); |
| 1473 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1474 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1475 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1478 | static int fcntl_F_DUPFD(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1479 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1480 | int newfd; |
| 1481 | repeat: |
| 1482 | newfd = fcntl(fd, F_DUPFD, avoid_fd + 1); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1483 | if (newfd < 0) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1484 | if (errno == EBUSY) |
| 1485 | goto repeat; |
| 1486 | if (errno == EINTR) |
| 1487 | goto repeat; |
| 1488 | } |
| 1489 | return newfd; |
| 1490 | } |
| 1491 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1492 | static int xdup_CLOEXEC_and_close(int fd, int avoid_fd) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1493 | { |
| 1494 | int newfd; |
| 1495 | repeat: |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1496 | newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1497 | if (newfd < 0) { |
| 1498 | if (errno == EBUSY) |
| 1499 | goto repeat; |
| 1500 | if (errno == EINTR) |
| 1501 | goto repeat; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1502 | /* fd was not open? */ |
| 1503 | if (errno == EBADF) |
| 1504 | return fd; |
| 1505 | xfunc_die(); |
| 1506 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1507 | if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ |
| 1508 | fcntl(newfd, F_SETFD, FD_CLOEXEC); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1509 | close(fd); |
| 1510 | return newfd; |
| 1511 | } |
| 1512 | |
| 1513 | |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1514 | /* Manipulating the list of open FILEs */ |
| 1515 | static FILE *remember_FILE(FILE *fp) |
| 1516 | { |
| 1517 | if (fp) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1518 | struct FILE_list *n = xmalloc(sizeof(*n)); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1519 | n->next = G.FILE_list; |
| 1520 | G.FILE_list = n; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1521 | n->fp = fp; |
| 1522 | n->fd = fileno(fp); |
| 1523 | close_on_exec_on(n->fd); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1524 | } |
| 1525 | return fp; |
| 1526 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1527 | static void fclose_and_forget(FILE *fp) |
| 1528 | { |
| 1529 | struct FILE_list **pp = &G.FILE_list; |
| 1530 | while (*pp) { |
| 1531 | struct FILE_list *cur = *pp; |
| 1532 | if (cur->fp == fp) { |
| 1533 | *pp = cur->next; |
| 1534 | free(cur); |
| 1535 | break; |
| 1536 | } |
| 1537 | pp = &cur->next; |
| 1538 | } |
| 1539 | fclose(fp); |
| 1540 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1541 | static int save_FILEs_on_redirect(int fd, int avoid_fd) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1542 | { |
| 1543 | struct FILE_list *fl = G.FILE_list; |
| 1544 | while (fl) { |
| 1545 | if (fd == fl->fd) { |
| 1546 | /* We use it only on script files, they are all CLOEXEC */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 1547 | fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1548 | 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] | 1549 | return 1; |
| 1550 | } |
| 1551 | fl = fl->next; |
| 1552 | } |
| 1553 | return 0; |
| 1554 | } |
| 1555 | static void restore_redirected_FILEs(void) |
| 1556 | { |
| 1557 | struct FILE_list *fl = G.FILE_list; |
| 1558 | while (fl) { |
| 1559 | int should_be = fileno(fl->fp); |
| 1560 | if (fl->fd != should_be) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 1561 | 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] | 1562 | xmove_fd(fl->fd, should_be); |
| 1563 | fl->fd = should_be; |
| 1564 | } |
| 1565 | fl = fl->next; |
| 1566 | } |
| 1567 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 1568 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 1569 | static void close_all_FILE_list(void) |
| 1570 | { |
| 1571 | struct FILE_list *fl = G.FILE_list; |
| 1572 | while (fl) { |
| 1573 | /* fclose would also free FILE object. |
| 1574 | * It is disastrous if we share memory with a vforked parent. |
| 1575 | * I'm not sure we never come here after vfork. |
| 1576 | * Therefore just close fd, nothing more. |
| 1577 | */ |
| 1578 | /*fclose(fl->fp); - unsafe */ |
| 1579 | close(fl->fd); |
| 1580 | fl = fl->next; |
| 1581 | } |
| 1582 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1583 | #endif |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 1584 | static int fd_in_FILEs(int fd) |
| 1585 | { |
| 1586 | struct FILE_list *fl = G.FILE_list; |
| 1587 | while (fl) { |
| 1588 | if (fl->fd == fd) |
| 1589 | return 1; |
| 1590 | fl = fl->next; |
| 1591 | } |
| 1592 | return 0; |
| 1593 | } |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 1594 | |
| 1595 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1596 | /* Helpers for setting new $n and restoring them back |
| 1597 | */ |
| 1598 | typedef struct save_arg_t { |
| 1599 | char *sv_argv0; |
| 1600 | char **sv_g_argv; |
| 1601 | int sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1602 | IF_HUSH_SET(smallint sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1603 | } save_arg_t; |
| 1604 | |
| 1605 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1606 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1607 | sv->sv_argv0 = argv[0]; |
| 1608 | sv->sv_g_argv = G.global_argv; |
| 1609 | sv->sv_g_argc = G.global_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1610 | IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1611 | |
| 1612 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1613 | G.global_argv = argv; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1614 | IF_HUSH_SET(G.global_args_malloced = 0;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1615 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 1616 | G.global_argc = 1 + string_array_len(argv + 1); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1620 | { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1621 | #if ENABLE_HUSH_SET |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1622 | if (G.global_args_malloced) { |
| 1623 | /* someone ran "set -- arg1 arg2 ...", undo */ |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1624 | char **pp = G.global_argv; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1625 | while (*++pp) /* note: does not free $0 */ |
| 1626 | free(*pp); |
| 1627 | free(G.global_argv); |
| 1628 | } |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1629 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1630 | argv[0] = sv->sv_argv0; |
| 1631 | G.global_argv = sv->sv_g_argv; |
| 1632 | G.global_argc = sv->sv_g_argc; |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 1633 | IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;) |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1637 | /* Basic theory of signal handling in shell |
| 1638 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1639 | * This does not describe what hush does, rather, it is current understanding |
| 1640 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1641 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1642 | * |
| 1643 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1644 | * is finished or backgrounded. It is the same in interactive and |
| 1645 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1646 | * 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] | 1647 | * ^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] | 1648 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1649 | * pipe. |
| 1650 | * |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1651 | * Wait builtin is interruptible by signals for which user trap is set |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1652 | * or by SIGINT in interactive shell. |
| 1653 | * |
| 1654 | * Trap handlers will execute even within trap handlers. (right?) |
| 1655 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1656 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1657 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1658 | * |
| 1659 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1660 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1661 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1662 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1663 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1664 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1665 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1666 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1667 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1668 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1669 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1670 | * |
| 1671 | * SIGQUIT: ignore |
| 1672 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1673 | * SIGHUP (interactive): |
| 1674 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1675 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1676 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1677 | * that all pipe members are stopped. Try this in bash: |
| 1678 | * while :; do :; done - ^Z does not background it |
| 1679 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1680 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1681 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1682 | * to interactive shell while shell is waiting for a pipe, |
| 1683 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1684 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1685 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1686 | * Example 2: this does not wait and does not execute ls: |
| 1687 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1688 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1689 | * "sleep 5; ls -l" + press ^C |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1690 | * Example 4: this does not wait and does not execute ls: |
| 1691 | * "sleep 5 & wait; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1692 | * |
| 1693 | * (What happens to signals which are IGN on shell start?) |
| 1694 | * (What happens with signal mask on shell start?) |
| 1695 | * |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1696 | * Old implementation |
| 1697 | * ================== |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1698 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1699 | * We block all signals which we don't want to take action immediately, |
| 1700 | * i.e. we block all signals which need to have special handling as described |
| 1701 | * above, and all signals which have traps set. |
| 1702 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1703 | * and act on them. |
| 1704 | * |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1705 | * unsigned special_sig_mask: a mask of such "special" signals |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1706 | * sigset_t blocked_set: current blocked signal set |
| 1707 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1708 | * "trap - SIGxxx": |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 1709 | * 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] | 1710 | * "trap 'cmd' SIGxxx": |
| 1711 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1712 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1713 | * unblock signals with special interactive handling |
| 1714 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1715 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1716 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1717 | * 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] | 1718 | * 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] | 1719 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1720 | * 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] | 1721 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1722 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1723 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1724 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1725 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1726 | * 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] | 1727 | * 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] | 1728 | * |
| 1729 | * Problem: the above approach makes it unwieldy to catch signals while |
Denys Vlasenko | e95738f | 2013-07-08 03:13:08 +0200 | [diff] [blame] | 1730 | * we are in read builtin, or while we read commands from stdin: |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1731 | * masked signals are not visible! |
| 1732 | * |
| 1733 | * New implementation |
| 1734 | * ================== |
| 1735 | * We record each signal we are interested in by installing signal handler |
| 1736 | * for them - a bit like emulating kernel pending signal mask in userspace. |
| 1737 | * We are interested in: signals which need to have special handling |
| 1738 | * as described above, and all signals which have traps set. |
Denys Vlasenko | 8bd810b | 2013-11-28 01:50:01 +0100 | [diff] [blame] | 1739 | * Signals are recorded in pending_set. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1740 | * After each pipe execution, we extract any pending signals |
| 1741 | * and act on them. |
| 1742 | * |
| 1743 | * unsigned special_sig_mask: a mask of shell-special signals. |
| 1744 | * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp. |
| 1745 | * char *traps[sig] if trap for sig is set (even if it's ''). |
| 1746 | * sigset_t pending_set: set of sigs we received. |
| 1747 | * |
| 1748 | * "trap - SIGxxx": |
| 1749 | * if sig is in special_sig_mask, set handler back to: |
| 1750 | * record_pending_signo, or to IGN if it's a tty stop signal |
| 1751 | * if sig is in fatal_sig_mask, set handler back to sigexit. |
| 1752 | * else: set handler back to SIG_DFL |
| 1753 | * "trap 'cmd' SIGxxx": |
| 1754 | * set handler to record_pending_signo. |
| 1755 | * "trap '' SIGxxx": |
| 1756 | * set handler to SIG_IGN. |
| 1757 | * after [v]fork, if we plan to be a shell: |
| 1758 | * set signals with special interactive handling to SIG_DFL |
| 1759 | * (because child shell is not interactive), |
| 1760 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
| 1761 | * after [v]fork, if we plan to exec: |
| 1762 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
| 1763 | * |
| 1764 | * To make wait builtin interruptible, we handle SIGCHLD as special signal, |
| 1765 | * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it. |
| 1766 | * |
| 1767 | * Note (compat): |
| 1768 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1769 | * are set to the default actions". bash interprets it so that traps which |
| 1770 | * 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] | 1771 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1772 | enum { |
| 1773 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1774 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1775 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1776 | | (1 << SIGHUP) |
| 1777 | , |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1778 | SPECIAL_JOBSTOP_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1779 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1780 | | (1 << SIGTTIN) |
| 1781 | | (1 << SIGTTOU) |
| 1782 | | (1 << SIGTSTP) |
| 1783 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1784 | , |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1785 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1786 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1787 | static void record_pending_signo(int sig) |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 1788 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1789 | sigaddset(&G.pending_set, sig); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1790 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1791 | if (sig == SIGCHLD) { |
| 1792 | G.count_SIGCHLD++; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1793 | //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] | 1794 | } |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1795 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1796 | } |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1797 | |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 1798 | static sighandler_t install_sighandler(int sig, sighandler_t handler) |
| 1799 | { |
| 1800 | struct sigaction old_sa; |
| 1801 | |
| 1802 | /* We could use signal() to install handlers... almost: |
| 1803 | * except that we need to mask ALL signals while handlers run. |
| 1804 | * I saw signal nesting in strace, race window isn't small. |
| 1805 | * SA_RESTART is also needed, but in Linux, signal() |
| 1806 | * sets SA_RESTART too. |
| 1807 | */ |
| 1808 | /* memset(&G.sa, 0, sizeof(G.sa)); - already done */ |
| 1809 | /* sigfillset(&G.sa.sa_mask); - already done */ |
| 1810 | /* G.sa.sa_flags = SA_RESTART; - already done */ |
| 1811 | G.sa.sa_handler = handler; |
| 1812 | sigaction(sig, &G.sa, &old_sa); |
| 1813 | return old_sa.sa_handler; |
| 1814 | } |
| 1815 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1816 | static void hush_exit(int exitcode) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1817 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1818 | static void restore_ttypgrp_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1819 | static void restore_ttypgrp_and__exit(void) |
| 1820 | { |
| 1821 | /* xfunc has failed! die die die */ |
| 1822 | /* no EXIT traps, this is an escape hatch! */ |
| 1823 | G.exiting = 1; |
| 1824 | hush_exit(xfunc_error_retval); |
| 1825 | } |
| 1826 | |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1827 | #if ENABLE_HUSH_JOB |
| 1828 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1829 | /* Needed only on some libc: |
| 1830 | * It was observed that on exit(), fgetc'ed buffered data |
| 1831 | * gets "unwound" via lseek(fd, -NUM, SEEK_CUR). |
| 1832 | * With the net effect that even after fork(), not vfork(), |
| 1833 | * exit() in NOEXECed applet in "sh SCRIPT": |
| 1834 | * noexec_applet_here |
| 1835 | * echo END_OF_SCRIPT |
| 1836 | * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT". |
| 1837 | * This makes "echo END_OF_SCRIPT" executed twice. |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 1838 | * 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] | 1839 | * and in `cmd` handling. |
| 1840 | * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit(): |
| 1841 | */ |
Denys Vlasenko | b6afcc7 | 2016-12-12 16:30:20 +0100 | [diff] [blame] | 1842 | static void fflush_and__exit(void) NORETURN; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1843 | static void fflush_and__exit(void) |
| 1844 | { |
| 1845 | fflush_all(); |
| 1846 | _exit(xfunc_error_retval); |
| 1847 | } |
| 1848 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1849 | /* 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] | 1850 | # define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1851 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1852 | # 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] | 1853 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1854 | /* Restores tty foreground process group, and exits. |
| 1855 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1856 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1857 | * or called directly with -EXITCODE. |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 1858 | * We also call it if xfunc is exiting. |
| 1859 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1860 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1861 | static void sigexit(int sig) |
| 1862 | { |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1863 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1864 | * tty pgrp then, only top-level shell process does that */ |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1865 | if (G_saved_tty_pgrp && getpid() == G.root_pid) { |
| 1866 | /* Disable all signals: job control, SIGPIPE, etc. |
| 1867 | * Mostly paranoid measure, to prevent infinite SIGTTOU. |
| 1868 | */ |
| 1869 | sigprocmask_allsigs(SIG_BLOCK); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1870 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denys Vlasenko | ebc1ee2 | 2011-05-12 10:59:18 +0200 | [diff] [blame] | 1871 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* Not a signal, just exit */ |
| 1874 | if (sig <= 0) |
| 1875 | _exit(- sig); |
| 1876 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1877 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1878 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1879 | #else |
| 1880 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1881 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1882 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1883 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1884 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1885 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1886 | static sighandler_t pick_sighandler(unsigned sig) |
| 1887 | { |
| 1888 | sighandler_t handler = SIG_DFL; |
| 1889 | if (sig < sizeof(unsigned)*8) { |
| 1890 | unsigned sigmask = (1 << sig); |
| 1891 | |
| 1892 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1893 | /* is sig fatal? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1894 | if (G_fatal_sig_mask & sigmask) |
| 1895 | handler = sigexit; |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1896 | else |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1897 | #endif |
| 1898 | /* sig has special handling? */ |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 1899 | if (G.special_sig_mask & sigmask) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1900 | handler = record_pending_signo; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1901 | /* TTIN/TTOU/TSTP can't be set to record_pending_signo |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1902 | * in order to ignore them: they will be raised |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 1903 | * in an endless loop when we try to do some |
| 1904 | * terminal ioctls! We do have to _ignore_ these. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1905 | */ |
| 1906 | if (SPECIAL_JOBSTOP_SIGS & sigmask) |
| 1907 | handler = SIG_IGN; |
Denys Vlasenko | 0c40a73 | 2011-05-12 09:50:12 +0200 | [diff] [blame] | 1908 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1909 | } |
| 1910 | return handler; |
| 1911 | } |
| 1912 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1913 | /* Restores tty foreground process group, and exits. */ |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1914 | static void hush_exit(int exitcode) |
| 1915 | { |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 1916 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 1917 | save_history(G.line_input_state); |
| 1918 | #endif |
| 1919 | |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 1920 | fflush_all(); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1921 | 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] | 1922 | char *argv[3]; |
| 1923 | /* argv[0] is unused */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1924 | argv[1] = G_traps[0]; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1925 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1926 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1927 | /* Note: G_traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1928 | * "trap" will still show it, if executed |
| 1929 | * in the handler */ |
| 1930 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1931 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1932 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 1933 | #if ENABLE_FEATURE_CLEAN_UP |
| 1934 | { |
| 1935 | struct variable *cur_var; |
| 1936 | if (G.cwd != bb_msg_unknown) |
| 1937 | free((char*)G.cwd); |
| 1938 | cur_var = G.top_var; |
| 1939 | while (cur_var) { |
| 1940 | struct variable *tmp = cur_var; |
| 1941 | if (!cur_var->max_len) |
| 1942 | free(cur_var->varstr); |
| 1943 | cur_var = cur_var->next; |
| 1944 | free(tmp); |
| 1945 | } |
| 1946 | } |
| 1947 | #endif |
| 1948 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1949 | fflush_all(); |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1950 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1951 | sigexit(- (exitcode & 0xff)); |
| 1952 | #else |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 1953 | _exit(exitcode); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1954 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1957 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1958 | //TODO: return a mask of ALL handled sigs? |
| 1959 | static int check_and_run_traps(void) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1960 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1961 | int last_sig = 0; |
| 1962 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1963 | while (1) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1964 | int sig; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 1965 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1966 | if (sigisemptyset(&G.pending_set)) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1967 | break; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1968 | sig = 0; |
| 1969 | do { |
| 1970 | sig++; |
| 1971 | if (sigismember(&G.pending_set, sig)) { |
| 1972 | sigdelset(&G.pending_set, sig); |
| 1973 | goto got_sig; |
| 1974 | } |
| 1975 | } while (sig < NSIG); |
| 1976 | break; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1977 | got_sig: |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1978 | if (G_traps && G_traps[sig]) { |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1979 | 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] | 1980 | if (G_traps[sig][0]) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1981 | /* We have user-defined handler */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 1982 | smalluint save_rcode; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1983 | char *argv[3]; |
| 1984 | /* argv[0] is unused */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 1985 | argv[1] = G_traps[sig]; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1986 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1987 | save_rcode = G.last_exitcode; |
| 1988 | builtin_eval(argv); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 1989 | //FIXME: shouldn't it be set to 128 + sig instead? |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1990 | G.last_exitcode = save_rcode; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 1991 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1992 | } /* else: "" trap, ignoring signal */ |
| 1993 | continue; |
| 1994 | } |
| 1995 | /* not a trap: special action */ |
| 1996 | switch (sig) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1997 | case SIGINT: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 1998 | debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1999 | G.flag_SIGINT = 1; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2000 | last_sig = sig; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2001 | break; |
| 2002 | #if ENABLE_HUSH_JOB |
| 2003 | case SIGHUP: { |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 2004 | //TODO: why are we doing this? ash and dash don't do this, |
| 2005 | //they have no handler for SIGHUP at all, |
| 2006 | //they rely on kernel to send SIGHUP+SIGCONT to orphaned process groups |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2007 | struct pipe *job; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2008 | debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2009 | /* bash is observed to signal whole process groups, |
| 2010 | * not individual processes */ |
| 2011 | for (job = G.job_list; job; job = job->next) { |
| 2012 | if (job->pgrp <= 0) |
| 2013 | continue; |
| 2014 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 2015 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 2016 | kill(- job->pgrp, SIGCONT); |
| 2017 | } |
| 2018 | sigexit(SIGHUP); |
| 2019 | } |
| 2020 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2021 | #if ENABLE_HUSH_FAST |
| 2022 | case SIGCHLD: |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2023 | debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2024 | G.count_SIGCHLD++; |
| 2025 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 2026 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2027 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2028 | * This simplifies wait builtin a bit. |
| 2029 | */ |
| 2030 | break; |
| 2031 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2032 | default: /* ignored: */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 2033 | 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] | 2034 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2035 | /* Note: |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2036 | * We don't do 'last_sig = sig' here -> NOT returning this sig. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2037 | * Example: wait is not interrupted by TERM |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2038 | * in interactive shell, because TERM is ignored. |
| 2039 | */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2040 | break; |
| 2041 | } |
| 2042 | } |
| 2043 | return last_sig; |
| 2044 | } |
| 2045 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 2046 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2047 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2048 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2049 | if (force || G.cwd == NULL) { |
| 2050 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 2051 | * we must not try to free(bb_msg_unknown) */ |
| 2052 | if (G.cwd == bb_msg_unknown) |
| 2053 | G.cwd = NULL; |
| 2054 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 2055 | if (!G.cwd) |
| 2056 | G.cwd = bb_msg_unknown; |
| 2057 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 2058 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 2061 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2062 | /* |
| 2063 | * Shell and environment variable support |
| 2064 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2065 | 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] | 2066 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2067 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2068 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2069 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2070 | pp = &G.top_var; |
| 2071 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2072 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2073 | return pp; |
| 2074 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2075 | } |
| 2076 | return NULL; |
| 2077 | } |
| 2078 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2079 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2080 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2081 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2082 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2083 | |
| 2084 | if (G.expanded_assignments) { |
| 2085 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2086 | while (*cpp) { |
| 2087 | char *cp = *cpp; |
| 2088 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 2089 | return cp + len + 1; |
| 2090 | cpp++; |
| 2091 | } |
| 2092 | } |
| 2093 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2094 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2095 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2096 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 2097 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 2098 | if (strcmp(name, "PPID") == 0) |
| 2099 | return utoa(G.root_ppid); |
| 2100 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2101 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2102 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 2103 | return utoa(next_random(&G.random_gen)); |
| 2104 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2105 | return NULL; |
| 2106 | } |
| 2107 | |
| 2108 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2109 | * We take ownership of it. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2110 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2111 | #define SETFLAG_EXPORT (1 << 0) |
| 2112 | #define SETFLAG_UNEXPORT (1 << 1) |
| 2113 | #define SETFLAG_MAKE_RO (1 << 2) |
| 2114 | #define SETFLAG_LOCAL_SHIFT 3 |
| 2115 | static int set_local_var(char *str, unsigned flags) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2116 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2117 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2118 | struct variable *cur; |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2119 | char *free_me = NULL; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2120 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2121 | int name_len; |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2122 | IF_HUSH_LOCAL(unsigned local_lvl = (flags >> SETFLAG_LOCAL_SHIFT);) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2123 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2124 | eq_sign = strchr(str, '='); |
| 2125 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2126 | free(str); |
| 2127 | return -1; |
| 2128 | } |
| 2129 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2130 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2131 | var_pp = &G.top_var; |
| 2132 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2133 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2134 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2135 | continue; |
| 2136 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2137 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2138 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2139 | if (cur->flg_read_only) { |
Denys Vlasenko | 6b48e1f | 2017-07-17 21:31:17 +0200 | [diff] [blame] | 2140 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2141 | free(str); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2142 | //NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1, |
| 2143 | //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] | 2144 | return -1; |
| 2145 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2146 | if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2147 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 2148 | *eq_sign = '\0'; |
| 2149 | unsetenv(str); |
| 2150 | *eq_sign = '='; |
| 2151 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2152 | #if ENABLE_HUSH_LOCAL |
| 2153 | if (cur->func_nest_level < local_lvl) { |
| 2154 | /* New variable is declared as local, |
| 2155 | * and existing one is global, or local |
| 2156 | * from enclosing function. |
| 2157 | * Remove and save old one: */ |
| 2158 | *var_pp = cur->next; |
| 2159 | cur->next = *G.shadowed_vars_pp; |
| 2160 | *G.shadowed_vars_pp = cur; |
| 2161 | /* bash 3.2.33(1) and exported vars: |
| 2162 | * # export z=z |
| 2163 | * # f() { local z=a; env | grep ^z; } |
| 2164 | * # f |
| 2165 | * z=a |
| 2166 | * # env | grep ^z |
| 2167 | * z=z |
| 2168 | */ |
| 2169 | if (cur->flg_export) |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2170 | flags |= SETFLAG_EXPORT; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2171 | break; |
| 2172 | } |
| 2173 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 2174 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2175 | free_and_exp: |
| 2176 | free(str); |
| 2177 | goto exp; |
| 2178 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2179 | if (cur->max_len != 0) { |
| 2180 | if (cur->max_len >= strlen(str)) { |
| 2181 | /* This one is from startup env, reuse space */ |
| 2182 | strcpy(cur->varstr, str); |
| 2183 | goto free_and_exp; |
| 2184 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2185 | /* Can't reuse */ |
| 2186 | cur->max_len = 0; |
| 2187 | goto set_str_and_exp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2188 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2189 | /* max_len == 0 signifies "malloced" var, which we can |
| 2190 | * (and have to) free. But we can't free(cur->varstr) here: |
| 2191 | * if cur->flg_export is 1, it is in the environment. |
| 2192 | * We should either unsetenv+free, or wait until putenv, |
| 2193 | * then putenv(new)+free(old). |
| 2194 | */ |
| 2195 | free_me = cur->varstr; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2196 | goto set_str_and_exp; |
| 2197 | } |
| 2198 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2199 | /* Not found - create new variable struct */ |
| 2200 | cur = xzalloc(sizeof(*cur)); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2201 | IF_HUSH_LOCAL(cur->func_nest_level = local_lvl;) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2202 | cur->next = *var_pp; |
| 2203 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2204 | |
| 2205 | set_str_and_exp: |
| 2206 | cur->varstr = str; |
| 2207 | exp: |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2208 | #if !BB_MMU || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2209 | if (flags & SETFLAG_MAKE_RO) { |
| 2210 | cur->flg_read_only = 1; |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 2211 | } |
| 2212 | #endif |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2213 | if (flags & SETFLAG_EXPORT) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2214 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2215 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2216 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2217 | if (cur->flg_export) { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2218 | if (flags & SETFLAG_UNEXPORT) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2219 | cur->flg_export = 0; |
| 2220 | /* unsetenv was already done */ |
| 2221 | } else { |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2222 | int i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2223 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2224 | i = putenv(cur->varstr); |
| 2225 | /* only now we can free old exported malloced string */ |
| 2226 | free(free_me); |
| 2227 | return i; |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 2228 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2229 | } |
Denys Vlasenko | a769390 | 2016-10-03 15:01:06 +0200 | [diff] [blame] | 2230 | free(free_me); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2231 | return 0; |
| 2232 | } |
| 2233 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2234 | /* Used at startup and after each cd */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2235 | static void set_pwd_var(unsigned flag) |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2236 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2237 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2238 | } |
| 2239 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2240 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2241 | { |
| 2242 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2243 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2244 | |
| 2245 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2246 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2247 | var_pp = &G.top_var; |
| 2248 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2249 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 2250 | if (cur->flg_read_only) { |
| 2251 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2252 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2253 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2254 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2255 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 2256 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2257 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 2258 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2259 | if (!cur->max_len) |
| 2260 | free(cur->varstr); |
| 2261 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2262 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2263 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2264 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2265 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 2266 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2269 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2270 | static int unset_local_var(const char *name) |
| 2271 | { |
| 2272 | return unset_local_var_len(name, strlen(name)); |
| 2273 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 2274 | #endif |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2275 | |
| 2276 | static void unset_vars(char **strings) |
| 2277 | { |
| 2278 | char **v; |
| 2279 | |
| 2280 | if (!strings) |
| 2281 | return; |
| 2282 | v = strings; |
| 2283 | while (*v) { |
| 2284 | const char *eq = strchrnul(*v, '='); |
| 2285 | unset_local_var_len(*v, (int)(eq - *v)); |
| 2286 | v++; |
| 2287 | } |
| 2288 | free(strings); |
| 2289 | } |
| 2290 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 2291 | #if BASH_HOSTNAME_VAR || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_READ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2292 | 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] | 2293 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2294 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2295 | set_local_var(var, /*flag:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2296 | } |
Denys Vlasenko | cc2fd5a | 2017-01-09 06:19:55 +0100 | [diff] [blame] | 2297 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2298 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2299 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2300 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2301 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 2302 | */ |
| 2303 | static void add_vars(struct variable *var) |
| 2304 | { |
| 2305 | struct variable *next; |
| 2306 | |
| 2307 | while (var) { |
| 2308 | next = var->next; |
| 2309 | var->next = G.top_var; |
| 2310 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2311 | if (var->flg_export) { |
| 2312 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2313 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2314 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2315 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2316 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2317 | var = next; |
| 2318 | } |
| 2319 | } |
| 2320 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2321 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2322 | { |
| 2323 | char **s; |
| 2324 | struct variable *old = NULL; |
| 2325 | |
| 2326 | if (!strings) |
| 2327 | return old; |
| 2328 | s = strings; |
| 2329 | while (*s) { |
| 2330 | struct variable *var_p; |
| 2331 | struct variable **var_pp; |
| 2332 | char *eq; |
| 2333 | |
| 2334 | eq = strchr(*s, '='); |
| 2335 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2336 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2337 | if (var_pp) { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2338 | var_p = *var_pp; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2339 | if (var_p->flg_read_only) { |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2340 | char **p; |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2341 | bb_error_msg("%s: readonly variable", *s); |
Denys Vlasenko | cf51109 | 2017-07-18 15:58:02 +0200 | [diff] [blame] | 2342 | /* |
| 2343 | * "VAR=V BLTIN" unsets VARs after BLTIN completes. |
| 2344 | * If VAR is readonly, leaving it in the list |
| 2345 | * after asssignment error (msg above) |
| 2346 | * causes doubled error message later, on unset. |
| 2347 | */ |
| 2348 | debug_printf_env("removing/freeing '%s' element\n", *s); |
| 2349 | free(*s); |
| 2350 | p = s; |
| 2351 | do { *p = p[1]; p++; } while (*p); |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2352 | goto next; |
| 2353 | } |
| 2354 | /* Remove variable from global linked list */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 2355 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2356 | *var_pp = var_p->next; |
| 2357 | /* Add it to returned list */ |
| 2358 | var_p->next = old; |
| 2359 | old = var_p; |
| 2360 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 2361 | set_local_var(*s, SETFLAG_EXPORT); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2362 | } |
Denys Vlasenko | 5b2cc0a | 2017-07-18 02:44:06 +0200 | [diff] [blame] | 2363 | next: |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 2364 | s++; |
| 2365 | } |
| 2366 | return old; |
| 2367 | } |
| 2368 | |
| 2369 | |
| 2370 | /* |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2371 | * Unicode helper |
| 2372 | */ |
| 2373 | static void reinit_unicode_for_hush(void) |
| 2374 | { |
| 2375 | /* Unicode support should be activated even if LANG is set |
| 2376 | * _during_ shell execution, not only if it was set when |
| 2377 | * shell was started. Therefore, re-check LANG every time: |
| 2378 | */ |
Denys Vlasenko | 841f833 | 2014-08-13 10:09:49 +0200 | [diff] [blame] | 2379 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2380 | || ENABLE_UNICODE_USING_LOCALE |
| 2381 | ) { |
| 2382 | const char *s = get_local_var_value("LC_ALL"); |
| 2383 | if (!s) s = get_local_var_value("LC_CTYPE"); |
| 2384 | if (!s) s = get_local_var_value("LANG"); |
| 2385 | reinit_unicode(s); |
| 2386 | } |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2387 | } |
| 2388 | |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2389 | /* |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2390 | * in_str support (strings, and "strings" read from files). |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2391 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2392 | |
| 2393 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2394 | /* To test correct lineedit/interactive behavior, type from command line: |
| 2395 | * echo $P\ |
| 2396 | * \ |
| 2397 | * AT\ |
| 2398 | * H\ |
| 2399 | * \ |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 2400 | * It exercises a lot of corner cases. |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2401 | */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2402 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2403 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2404 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2405 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2406 | if (G.PS1 == NULL) |
| 2407 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2408 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2409 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2410 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 2411 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 2412 | if (G.PS2 == NULL) |
| 2413 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2414 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2415 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2416 | { |
| 2417 | const char *prompt_str; |
| 2418 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2419 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 2420 | /* Set up the prompt */ |
| 2421 | if (promptmode == 0) { /* PS1 */ |
| 2422 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 2423 | /* bash uses $PWD value, even if it is set by user. |
| 2424 | * It uses current dir only if PWD is unset. |
| 2425 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 2426 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 2427 | prompt_str = G.PS1; |
| 2428 | } else |
| 2429 | prompt_str = G.PS2; |
| 2430 | } else |
| 2431 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2432 | debug_printf("prompt_str '%s'\n", prompt_str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2433 | return prompt_str; |
| 2434 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2435 | static int get_user_input(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2436 | { |
| 2437 | int r; |
| 2438 | const char *prompt_str; |
| 2439 | |
| 2440 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2441 | # if ENABLE_FEATURE_EDITING |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2442 | for (;;) { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 2443 | reinit_unicode_for_hush(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2444 | if (G.flag_SIGINT) { |
| 2445 | /* There was ^C'ed, make it look prettier: */ |
| 2446 | bb_putchar('\n'); |
| 2447 | G.flag_SIGINT = 0; |
| 2448 | } |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2449 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2450 | * only after <Enter>. (^C works immediately) */ |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2451 | r = read_line_input(G.line_input_state, prompt_str, |
Denys Vlasenko | 84ea60e | 2017-08-02 17:27:28 +0200 | [diff] [blame] | 2452 | G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1 |
Denys Vlasenko | 0448c55 | 2016-09-29 20:25:44 +0200 | [diff] [blame] | 2453 | ); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2454 | /* read_line_input intercepts ^C, "convert" it to SIGINT */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2455 | if (r == 0) |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2456 | raise(SIGINT); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2457 | check_and_run_traps(); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2458 | if (r != 0 && !G.flag_SIGINT) |
| 2459 | break; |
| 2460 | /* ^C or SIGINT: repeat */ |
Denys Vlasenko | dd4b446 | 2017-08-02 16:52:12 +0200 | [diff] [blame] | 2461 | /* bash prints ^C even on real SIGINT (non-kbd generated) */ |
| 2462 | write(STDOUT_FILENO, "^C", 2); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2463 | G.last_exitcode = 128 + SIGINT; |
| 2464 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2465 | if (r < 0) { |
| 2466 | /* EOF/error detected */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2467 | i->p = NULL; |
| 2468 | i->peek_buf[0] = r = EOF; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2469 | return r; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2470 | } |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2471 | i->p = G.user_input_buf; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2472 | return (unsigned char)*i->p++; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2473 | # else |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2474 | for (;;) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 2475 | G.flag_SIGINT = 0; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2476 | if (i->last_char == '\0' || i->last_char == '\n') { |
| 2477 | /* Why check_and_run_traps here? Try this interactively: |
| 2478 | * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) & |
| 2479 | * $ <[enter], repeatedly...> |
| 2480 | * Without check_and_run_traps, handler never runs. |
| 2481 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 2482 | check_and_run_traps(); |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 2483 | fputs(prompt_str, stdout); |
| 2484 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2485 | fflush_all(); |
Denys Vlasenko | 4b89d51 | 2016-11-25 03:41:03 +0100 | [diff] [blame] | 2486 | //FIXME: here ^C or SIGINT will have effect only after <Enter> |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2487 | r = fgetc(i->file); |
Denys Vlasenko | 8660aeb | 2016-11-24 17:44:02 +0100 | [diff] [blame] | 2488 | /* In !ENABLE_FEATURE_EDITING we don't use read_line_input, |
| 2489 | * no ^C masking happens during fgetc, no special code for ^C: |
| 2490 | * it generates SIGINT as usual. |
| 2491 | */ |
| 2492 | check_and_run_traps(); |
| 2493 | if (G.flag_SIGINT) |
| 2494 | G.last_exitcode = 128 + SIGINT; |
| 2495 | if (r != '\0') |
| 2496 | break; |
| 2497 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2498 | return r; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2499 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2500 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2501 | /* This is the magic location that prints prompts |
| 2502 | * and gets data back from the user */ |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2503 | static int fgetc_interactive(struct in_str *i) |
| 2504 | { |
| 2505 | int ch; |
| 2506 | /* If it's interactive stdin, get new line. */ |
| 2507 | if (G_interactive_fd && i->file == stdin) { |
| 2508 | /* Returns first char (or EOF), the rest is in i->p[] */ |
| 2509 | ch = get_user_input(i); |
| 2510 | i->promptmode = 1; /* PS2 */ |
| 2511 | } else { |
| 2512 | /* Not stdin: script file, sourced file, etc */ |
| 2513 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2514 | } |
| 2515 | return ch; |
| 2516 | } |
| 2517 | #else |
| 2518 | static inline int fgetc_interactive(struct in_str *i) |
| 2519 | { |
| 2520 | int ch; |
| 2521 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2522 | return ch; |
| 2523 | } |
| 2524 | #endif /* INTERACTIVE */ |
| 2525 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2526 | static int i_getch(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2527 | { |
| 2528 | int ch; |
| 2529 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2530 | if (!i->file) { |
| 2531 | /* string-based in_str */ |
| 2532 | ch = (unsigned char)*i->p; |
| 2533 | if (ch != '\0') { |
| 2534 | i->p++; |
| 2535 | i->last_char = ch; |
| 2536 | return ch; |
| 2537 | } |
| 2538 | return EOF; |
| 2539 | } |
| 2540 | |
| 2541 | /* FILE-based in_str */ |
| 2542 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2543 | #if ENABLE_FEATURE_EDITING |
| 2544 | /* This can be stdin, check line editing char[] buffer */ |
| 2545 | if (i->p && *i->p != '\0') { |
| 2546 | ch = (unsigned char)*i->p++; |
| 2547 | goto out; |
| 2548 | } |
| 2549 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2550 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2551 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2552 | if (ch != 0) { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2553 | int ch2 = i->peek_buf[1]; |
| 2554 | i->peek_buf[0] = ch2; |
| 2555 | if (ch2 == 0) /* very likely, avoid redundant write */ |
| 2556 | goto out; |
| 2557 | i->peek_buf[1] = 0; |
| 2558 | goto out; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2559 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2560 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2561 | ch = fgetc_interactive(i); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2562 | out: |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 2563 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 2564 | i->last_char = ch; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2565 | return ch; |
| 2566 | } |
| 2567 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2568 | static int i_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2569 | { |
| 2570 | int ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2571 | |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2572 | if (!i->file) { |
| 2573 | /* string-based in_str */ |
| 2574 | /* Doesn't report EOF on NUL. None of the callers care. */ |
| 2575 | return (unsigned char)*i->p; |
| 2576 | } |
| 2577 | |
| 2578 | /* FILE-based in_str */ |
| 2579 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2580 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2581 | /* This can be stdin, check line editing char[] buffer */ |
| 2582 | if (i->p && *i->p != '\0') |
| 2583 | return (unsigned char)*i->p; |
| 2584 | #endif |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2585 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2586 | ch = i->peek_buf[0]; |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2587 | if (ch != 0) |
| 2588 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2589 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2590 | /* Need to get a new char */ |
| 2591 | ch = fgetc_interactive(i); |
| 2592 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
| 2593 | |
| 2594 | /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */ |
| 2595 | #if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE |
| 2596 | if (i->p) { |
| 2597 | i->p -= 1; |
| 2598 | return ch; |
| 2599 | } |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2600 | #endif |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2601 | i->peek_buf[0] = ch; |
| 2602 | /*i->peek_buf[1] = 0; - already is */ |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2603 | return ch; |
| 2604 | } |
| 2605 | |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2606 | /* Only ever called if i_peek() was called, and did not return EOF. |
| 2607 | * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL, |
| 2608 | * not end-of-line. Therefore we never need to read a new editing line here. |
| 2609 | */ |
| 2610 | static int i_peek2(struct in_str *i) |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2611 | { |
Denys Vlasenko | 4074d49 | 2016-09-30 01:49:53 +0200 | [diff] [blame] | 2612 | int ch; |
| 2613 | |
| 2614 | /* There are two cases when i->p[] buffer exists. |
| 2615 | * (1) it's a string in_str. |
Denys Vlasenko | 08755f9 | 2016-09-30 02:02:25 +0200 | [diff] [blame] | 2616 | * (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] | 2617 | * In both cases, we know that i->p[0] exists and not NUL, and |
| 2618 | * the peek2 result is in i->p[1]. |
| 2619 | */ |
| 2620 | if (i->p) |
| 2621 | return (unsigned char)i->p[1]; |
| 2622 | |
| 2623 | /* Now we know it is a file-based in_str. */ |
| 2624 | |
| 2625 | /* peek_buf[] is an int array, not char. Can contain EOF. */ |
| 2626 | /* Is there 2nd char? */ |
| 2627 | ch = i->peek_buf[1]; |
| 2628 | if (ch == 0) { |
| 2629 | /* We did not read it yet, get it now */ |
| 2630 | do ch = fgetc(i->file); while (ch == '\0'); |
| 2631 | i->peek_buf[1] = ch; |
| 2632 | } |
| 2633 | |
| 2634 | debug_printf("file_peek2: got '%c' %d\n", ch, ch); |
| 2635 | return ch; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2638 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 2639 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2640 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2641 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2642 | i->file = f; |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2643 | /* i->p = NULL; */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 2647 | { |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2648 | memset(i, 0, sizeof(*i)); |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 2649 | /* i->promptmode = 0; - PS1 (memset did it) */ |
Denys Vlasenko | 87e039d | 2016-11-08 22:35:05 +0100 | [diff] [blame] | 2650 | /*i->file = NULL */; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2651 | i->p = s; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2655 | /* |
| 2656 | * o_string support |
| 2657 | */ |
| 2658 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2659 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2660 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2661 | { |
| 2662 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2663 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2664 | if (o->data) |
| 2665 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2668 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2669 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2670 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2671 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2674 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2675 | { |
| 2676 | free(o->data); |
| 2677 | } |
| 2678 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2679 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2680 | { |
| 2681 | if (o->length + len > o->maxlen) { |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2682 | o->maxlen += (2 * len) | (B_CHUNK-1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2683 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2684 | } |
| 2685 | } |
| 2686 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2687 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2688 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2689 | 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] | 2690 | if (o->length < o->maxlen) { |
| 2691 | /* likely. avoid o_grow_by() call */ |
| 2692 | add: |
| 2693 | o->data[o->length] = ch; |
| 2694 | o->length++; |
| 2695 | o->data[o->length] = '\0'; |
| 2696 | return; |
| 2697 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2698 | o_grow_by(o, 1); |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 2699 | goto add; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 2702 | #if 0 |
| 2703 | /* Valid only if we know o_string is not empty */ |
| 2704 | static void o_delchr(o_string *o) |
| 2705 | { |
| 2706 | o->length--; |
| 2707 | o->data[o->length] = '\0'; |
| 2708 | } |
| 2709 | #endif |
| 2710 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2711 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2712 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2713 | o_grow_by(o, len); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 2714 | ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0'; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2715 | o->length += len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2718 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2719 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2720 | o_addblock(o, str, strlen(str)); |
| 2721 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2722 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2723 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2724 | static void nommu_addchr(o_string *o, int ch) |
| 2725 | { |
| 2726 | if (o) |
| 2727 | o_addchr(o, ch); |
| 2728 | } |
| 2729 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2730 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2731 | #endif |
| 2732 | |
| 2733 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2734 | { |
| 2735 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2738 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2739 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2740 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2741 | * Apparently, on unquoted $v bash still does globbing |
| 2742 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2743 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2744 | * quoting mechanisms on $v expansion side: one protects |
| 2745 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2746 | * We have only second one. |
| 2747 | */ |
| 2748 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2749 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2750 | # define MAYBE_BRACES "{}" |
| 2751 | #else |
| 2752 | # define MAYBE_BRACES "" |
| 2753 | #endif |
| 2754 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2755 | /* My analysis of quoting semantics tells me that state information |
| 2756 | * is associated with a destination, not a source. |
| 2757 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2758 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2759 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2760 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2761 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2762 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2763 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2764 | o_grow_by(o, sz); |
| 2765 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2766 | o->data[o->length] = '\\'; |
| 2767 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2768 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2769 | o->data[o->length] = ch; |
| 2770 | o->length++; |
| 2771 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2774 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2775 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2776 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2777 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2778 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2779 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2780 | sz++; |
| 2781 | o->data[o->length] = '\\'; |
| 2782 | o->length++; |
| 2783 | } |
| 2784 | o_grow_by(o, sz); |
| 2785 | o->data[o->length] = ch; |
| 2786 | o->length++; |
| 2787 | o->data[o->length] = '\0'; |
| 2788 | } |
| 2789 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2790 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2791 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2792 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2793 | char ch; |
| 2794 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2795 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2796 | if (ordinary_cnt > len) /* paranoia */ |
| 2797 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2798 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2799 | if (ordinary_cnt == len) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2800 | return; /* NUL is already added by o_addblock */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2801 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2802 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2803 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2804 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2805 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2806 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2807 | sz++; |
| 2808 | o->data[o->length] = '\\'; |
| 2809 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2810 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2811 | o_grow_by(o, sz); |
| 2812 | o->data[o->length] = ch; |
| 2813 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2814 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2815 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2818 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2819 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2820 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2821 | o_addblock(o, str, len); |
| 2822 | return; |
| 2823 | } |
| 2824 | o_addqblock(o, str, len); |
| 2825 | } |
| 2826 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2827 | static void o_addQstr(o_string *o, const char *str) |
| 2828 | { |
| 2829 | o_addQblock(o, str, strlen(str)); |
| 2830 | } |
| 2831 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2832 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2833 | * 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] | 2834 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2835 | * list[i] contains an INDEX (int!) into this string data. |
| 2836 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2837 | * but list[i]'s need not be modified. |
| 2838 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2839 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2840 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2841 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2842 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2843 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2844 | { |
| 2845 | char **list = (char**)o->data; |
| 2846 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2847 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2848 | |
| 2849 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2850 | 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] | 2851 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2852 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2853 | o->has_quoted_part, |
| 2854 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2855 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2856 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2857 | fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i], |
| 2858 | o->data + (int)(uintptr_t)list[i] + string_start, |
| 2859 | o->data + (int)(uintptr_t)list[i] + string_start); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2860 | i++; |
| 2861 | } |
| 2862 | if (n) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2863 | 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] | 2864 | indent(); |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 2865 | 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] | 2866 | } |
| 2867 | } |
| 2868 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2869 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2870 | #endif |
| 2871 | |
| 2872 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2873 | * in list[n] so that it points past last stored byte so far. |
| 2874 | * It returns n+1. */ |
| 2875 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2876 | { |
| 2877 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2878 | int string_start; |
| 2879 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2880 | |
| 2881 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2882 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2883 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2884 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2885 | 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] | 2886 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2887 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2888 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2889 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2890 | memmove(list + n + 0x10, list + n, string_len); |
| 2891 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2892 | } else { |
| 2893 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2894 | n, string_len, string_start); |
| 2895 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2896 | } else { |
| 2897 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2898 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2899 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2900 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2901 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2902 | o->has_empty_slot = 0; |
| 2903 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 2904 | o->has_quoted_part = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2905 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2906 | return n + 1; |
| 2907 | } |
| 2908 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2909 | /* "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] | 2910 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2911 | { |
| 2912 | char **list = (char**)o->data; |
| 2913 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2914 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2915 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2918 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2919 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2920 | * first, it processes even {a} (no commas), second, |
| 2921 | * 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] | 2922 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2923 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2924 | |
| 2925 | /* Helper */ |
| 2926 | static int glob_needed(const char *s) |
| 2927 | { |
| 2928 | while (*s) { |
| 2929 | if (*s == '\\') { |
| 2930 | if (!s[1]) |
| 2931 | return 0; |
| 2932 | s += 2; |
| 2933 | continue; |
| 2934 | } |
| 2935 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2936 | return 1; |
| 2937 | s++; |
| 2938 | } |
| 2939 | return 0; |
| 2940 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2941 | /* Return pointer to next closing brace or to comma */ |
| 2942 | static const char *next_brace_sub(const char *cp) |
| 2943 | { |
| 2944 | unsigned depth = 0; |
| 2945 | cp++; |
| 2946 | while (*cp != '\0') { |
| 2947 | if (*cp == '\\') { |
| 2948 | if (*++cp == '\0') |
| 2949 | break; |
| 2950 | cp++; |
| 2951 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2952 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2953 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2954 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2955 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2956 | depth++; |
| 2957 | } |
| 2958 | |
| 2959 | return *cp != '\0' ? cp : NULL; |
| 2960 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2961 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2962 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2963 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2964 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2965 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2966 | const char *next; |
| 2967 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2968 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2969 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2970 | |
| 2971 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2972 | |
| 2973 | begin = pattern; |
| 2974 | while (1) { |
| 2975 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2976 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2977 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2978 | /* Find the first sub-pattern and at the same time |
| 2979 | * find the rest after the closing brace */ |
| 2980 | next = next_brace_sub(begin); |
| 2981 | if (next == NULL) { |
| 2982 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2983 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2984 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2985 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2986 | /* "{abc}" with no commas - illegal |
| 2987 | * brace expr, disregard and skip it */ |
| 2988 | begin = next + 1; |
| 2989 | continue; |
| 2990 | } |
| 2991 | break; |
| 2992 | } |
| 2993 | if (*begin == '\\' && begin[1] != '\0') |
| 2994 | begin++; |
| 2995 | begin++; |
| 2996 | } |
| 2997 | debug_printf_glob("begin:%s\n", begin); |
| 2998 | debug_printf_glob("next:%s\n", next); |
| 2999 | |
| 3000 | /* Now find the end of the whole brace expression */ |
| 3001 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3002 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3003 | rest = next_brace_sub(rest); |
| 3004 | if (rest == NULL) { |
| 3005 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3006 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3007 | } |
| 3008 | debug_printf_glob("rest:%s\n", rest); |
| 3009 | } |
| 3010 | rest_len = strlen(++rest) + 1; |
| 3011 | |
| 3012 | /* We are sure the brace expression is well-formed */ |
| 3013 | |
| 3014 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3015 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3016 | |
| 3017 | /* We have a brace expression. BEGIN points to the opening {, |
| 3018 | * NEXT points past the terminator of the first element, and REST |
| 3019 | * points past the final }. We will accumulate result names from |
| 3020 | * recursive runs for each brace alternative in the buffer using |
| 3021 | * GLOB_APPEND. */ |
| 3022 | |
| 3023 | p = begin + 1; |
| 3024 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3025 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3026 | memcpy( |
| 3027 | mempcpy( |
| 3028 | mempcpy(new_pattern_buf, |
| 3029 | /* We know the prefix for all sub-patterns */ |
| 3030 | pattern, begin - pattern), |
| 3031 | p, next - p), |
| 3032 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3033 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3034 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 3035 | * That's why we re-copy prefix every time (1st memcpy above). |
| 3036 | */ |
| 3037 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 3038 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3039 | /* We saw the last entry */ |
| 3040 | break; |
| 3041 | } |
| 3042 | p = next + 1; |
| 3043 | next = next_brace_sub(next); |
| 3044 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3045 | free(new_pattern_buf); |
| 3046 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3047 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3048 | simple_glob: |
| 3049 | { |
| 3050 | int gr; |
| 3051 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3052 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3053 | memset(&globdata, 0, sizeof(globdata)); |
| 3054 | gr = glob(pattern, 0, NULL, &globdata); |
| 3055 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 3056 | if (gr != 0) { |
| 3057 | if (gr == GLOB_NOMATCH) { |
| 3058 | globfree(&globdata); |
| 3059 | /* NB: garbles parameter */ |
| 3060 | unbackslash(pattern); |
| 3061 | o_addstr_with_NUL(o, pattern); |
| 3062 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3063 | return o_save_ptr_helper(o, n); |
| 3064 | } |
| 3065 | if (gr == GLOB_NOSPACE) |
| 3066 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 3067 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3068 | * but we didn't specify it. Paranoia again. */ |
| 3069 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 3070 | } |
| 3071 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3072 | char **argv = globdata.gl_pathv; |
| 3073 | while (1) { |
| 3074 | o_addstr_with_NUL(o, *argv); |
| 3075 | n = o_save_ptr_helper(o, n); |
| 3076 | argv++; |
| 3077 | if (!*argv) |
| 3078 | break; |
| 3079 | } |
| 3080 | } |
| 3081 | globfree(&globdata); |
| 3082 | } |
| 3083 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3084 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3085 | /* Performs globbing on last list[], |
| 3086 | * saving each result as a new list[]. |
| 3087 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3088 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3089 | { |
| 3090 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3091 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3092 | 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] | 3093 | if (!o->data) |
| 3094 | return o_save_ptr_helper(o, n); |
| 3095 | pattern = o->data + o_get_last_ptr(o, n); |
| 3096 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 3097 | if (!glob_needed(pattern)) { |
| 3098 | /* unbackslash last string in o in place, fix length */ |
| 3099 | o->length = unbackslash(pattern) - o->data; |
| 3100 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 3101 | return o_save_ptr_helper(o, n); |
| 3102 | } |
| 3103 | |
| 3104 | copy = xstrdup(pattern); |
| 3105 | /* "forget" pattern in o */ |
| 3106 | o->length = pattern - o->data; |
| 3107 | n = glob_brace(copy, o, n); |
| 3108 | free(copy); |
| 3109 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3110 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3111 | return n; |
| 3112 | } |
| 3113 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3114 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3115 | |
| 3116 | /* Helper */ |
| 3117 | static int glob_needed(const char *s) |
| 3118 | { |
| 3119 | while (*s) { |
| 3120 | if (*s == '\\') { |
| 3121 | if (!s[1]) |
| 3122 | return 0; |
| 3123 | s += 2; |
| 3124 | continue; |
| 3125 | } |
| 3126 | if (*s == '*' || *s == '[' || *s == '?') |
| 3127 | return 1; |
| 3128 | s++; |
| 3129 | } |
| 3130 | return 0; |
| 3131 | } |
| 3132 | /* Performs globbing on last list[], |
| 3133 | * saving each result as a new list[]. |
| 3134 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3135 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3136 | { |
| 3137 | glob_t globdata; |
| 3138 | int gr; |
| 3139 | char *pattern; |
| 3140 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3141 | 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] | 3142 | if (!o->data) |
| 3143 | return o_save_ptr_helper(o, n); |
| 3144 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3145 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3146 | if (!glob_needed(pattern)) { |
| 3147 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3148 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3149 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3150 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3151 | return o_save_ptr_helper(o, n); |
| 3152 | } |
| 3153 | |
| 3154 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3155 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 3156 | * If we glob "*.\*" and don't find anything, we need |
| 3157 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 3158 | * will return "*.\*"! |
| 3159 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3160 | gr = glob(pattern, 0, NULL, &globdata); |
| 3161 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3162 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3163 | if (gr == GLOB_NOMATCH) { |
| 3164 | globfree(&globdata); |
| 3165 | goto literal; |
| 3166 | } |
| 3167 | if (gr == GLOB_NOSPACE) |
| 3168 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 3169 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 3170 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3171 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3172 | } |
| 3173 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 3174 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3175 | /* "forget" pattern in o */ |
| 3176 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3177 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3178 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3179 | n = o_save_ptr_helper(o, n); |
| 3180 | argv++; |
| 3181 | if (!*argv) |
| 3182 | break; |
| 3183 | } |
| 3184 | } |
| 3185 | globfree(&globdata); |
| 3186 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3187 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3188 | return n; |
| 3189 | } |
| 3190 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 3191 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 3192 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3193 | /* 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] | 3194 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3195 | static int o_save_ptr(o_string *o, int n) |
| 3196 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3197 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3198 | /* If o->has_empty_slot, list[n] was already globbed |
| 3199 | * (if it was requested back then when it was filled) |
| 3200 | * so don't do that again! */ |
| 3201 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 3202 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 3203 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3204 | return o_save_ptr_helper(o, n); |
| 3205 | } |
| 3206 | |
| 3207 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3208 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3209 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3210 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3211 | int string_start; |
| 3212 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3213 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 3214 | if (DEBUG_EXPAND) |
| 3215 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 3216 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 3217 | list = (char**)o->data; |
| 3218 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 3219 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3220 | while (n) { |
| 3221 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3222 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3223 | } |
| 3224 | return list; |
| 3225 | } |
| 3226 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3227 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3228 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3229 | /* Returns pi->next - next pipe in the list */ |
| 3230 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3231 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3232 | struct pipe *next; |
| 3233 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3234 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3235 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3236 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3237 | struct command *command; |
| 3238 | struct redir_struct *r, *rnext; |
| 3239 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3240 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3241 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3242 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3243 | if (DEBUG_CLEAN) { |
| 3244 | int a; |
| 3245 | char **p; |
| 3246 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 3247 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 3248 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3249 | } |
| 3250 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3251 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3252 | } |
| 3253 | /* not "else if": on syntax error, we may have both! */ |
| 3254 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3255 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3256 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3257 | free_pipe_list(command->group); |
| 3258 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3259 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3260 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3261 | /* else is crucial here. |
| 3262 | * If group != NULL, child_func is meaningless */ |
| 3263 | #if ENABLE_HUSH_FUNCTIONS |
| 3264 | else if (command->child_func) { |
| 3265 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3266 | command->child_func->parent_cmd = NULL; |
| 3267 | } |
| 3268 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3269 | #if !BB_MMU |
| 3270 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3271 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3272 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3273 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3274 | debug_printf_clean(" redirect %d%s", |
| 3275 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3276 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3277 | if (r->rd_filename) { |
| 3278 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3279 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3280 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3281 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3282 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3283 | rnext = r->next; |
| 3284 | free(r); |
| 3285 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3286 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3287 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3288 | 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] | 3289 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3290 | #if ENABLE_HUSH_JOB |
| 3291 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3292 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3293 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3294 | |
| 3295 | next = pi->next; |
| 3296 | free(pi); |
| 3297 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3298 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3299 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3300 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3301 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3302 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3303 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3304 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3305 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3306 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 3307 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3308 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3312 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3313 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3314 | #ifndef debug_print_tree |
| 3315 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 3316 | { |
| 3317 | static const char *const PIPE[] = { |
| 3318 | [PIPE_SEQ] = "SEQ", |
| 3319 | [PIPE_AND] = "AND", |
| 3320 | [PIPE_OR ] = "OR" , |
| 3321 | [PIPE_BG ] = "BG" , |
| 3322 | }; |
| 3323 | static const char *RES[] = { |
| 3324 | [RES_NONE ] = "NONE" , |
| 3325 | # if ENABLE_HUSH_IF |
| 3326 | [RES_IF ] = "IF" , |
| 3327 | [RES_THEN ] = "THEN" , |
| 3328 | [RES_ELIF ] = "ELIF" , |
| 3329 | [RES_ELSE ] = "ELSE" , |
| 3330 | [RES_FI ] = "FI" , |
| 3331 | # endif |
| 3332 | # if ENABLE_HUSH_LOOPS |
| 3333 | [RES_FOR ] = "FOR" , |
| 3334 | [RES_WHILE] = "WHILE", |
| 3335 | [RES_UNTIL] = "UNTIL", |
| 3336 | [RES_DO ] = "DO" , |
| 3337 | [RES_DONE ] = "DONE" , |
| 3338 | # endif |
| 3339 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 3340 | [RES_IN ] = "IN" , |
| 3341 | # endif |
| 3342 | # if ENABLE_HUSH_CASE |
| 3343 | [RES_CASE ] = "CASE" , |
| 3344 | [RES_CASE_IN ] = "CASE_IN" , |
| 3345 | [RES_MATCH] = "MATCH", |
| 3346 | [RES_CASE_BODY] = "CASE_BODY", |
| 3347 | [RES_ESAC ] = "ESAC" , |
| 3348 | # endif |
| 3349 | [RES_XXXX ] = "XXXX" , |
| 3350 | [RES_SNTX ] = "SNTX" , |
| 3351 | }; |
| 3352 | static const char *const CMDTYPE[] = { |
| 3353 | "{}", |
| 3354 | "()", |
| 3355 | "[noglob]", |
| 3356 | # if ENABLE_HUSH_FUNCTIONS |
| 3357 | "func()", |
| 3358 | # endif |
| 3359 | }; |
| 3360 | |
| 3361 | int pin, prn; |
| 3362 | |
| 3363 | pin = 0; |
| 3364 | while (pi) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3365 | fdprintf(2, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3366 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 3367 | prn = 0; |
| 3368 | while (prn < pi->num_cmds) { |
| 3369 | struct command *command = &pi->cmds[prn]; |
| 3370 | char **argv = command->argv; |
| 3371 | |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3372 | fdprintf(2, "%*s cmd %d assignment_cnt:%d", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3373 | lvl*2, "", prn, |
| 3374 | command->assignment_cnt); |
| 3375 | if (command->group) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3376 | fdprintf(2, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3377 | CMDTYPE[command->cmd_type], |
| 3378 | argv |
| 3379 | # if !BB_MMU |
| 3380 | , " group_as_string:", command->group_as_string |
| 3381 | # else |
| 3382 | , "", "" |
| 3383 | # endif |
| 3384 | ); |
| 3385 | debug_print_tree(command->group, lvl+1); |
| 3386 | prn++; |
| 3387 | continue; |
| 3388 | } |
| 3389 | if (argv) while (*argv) { |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3390 | fdprintf(2, " '%s'", *argv); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3391 | argv++; |
| 3392 | } |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 3393 | fdprintf(2, "\n"); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 3394 | prn++; |
| 3395 | } |
| 3396 | pi = pi->next; |
| 3397 | pin++; |
| 3398 | } |
| 3399 | } |
| 3400 | #endif /* debug_print_tree */ |
| 3401 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 3402 | static struct pipe *new_pipe(void) |
| 3403 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3404 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 3405 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3406 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3407 | return pi; |
| 3408 | } |
| 3409 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3410 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 3411 | * if ctx->command is NULL. |
| 3412 | * No errors possible here. |
| 3413 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3414 | static int done_command(struct parse_context *ctx) |
| 3415 | { |
| 3416 | /* The command is really already in the pipe structure, so |
| 3417 | * advance the pipe counter and make a new, null command. */ |
| 3418 | struct pipe *pi = ctx->pipe; |
| 3419 | struct command *command = ctx->command; |
| 3420 | |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3421 | #if 0 /* Instead we emit error message at run time */ |
| 3422 | if (ctx->pending_redirect) { |
| 3423 | /* For example, "cmd >" (no filename to redirect to) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3424 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3425 | ctx->pending_redirect = NULL; |
| 3426 | } |
| 3427 | #endif |
| 3428 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3429 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3430 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3431 | 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] | 3432 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3433 | } |
| 3434 | pi->num_cmds++; |
| 3435 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3436 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3437 | } else { |
| 3438 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 3439 | } |
| 3440 | |
| 3441 | /* Only real trickiness here is that the uncommitted |
| 3442 | * command structure is not counted in pi->num_cmds. */ |
| 3443 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3444 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 3445 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3446 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3447 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 3448 | } |
| 3449 | |
| 3450 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 3451 | { |
| 3452 | int not_null; |
| 3453 | |
| 3454 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 3455 | /* Close previous command */ |
| 3456 | not_null = done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3457 | #if HAS_KEYWORDS |
| 3458 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 3459 | ctx->ctx_inverted = 0; |
| 3460 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 3461 | #endif |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3462 | if (type == PIPE_BG && ctx->list_head != ctx->pipe) { |
| 3463 | /* Necessary since && and || have precedence over &: |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3464 | * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2, |
| 3465 | * in a backgrounded subshell. |
| 3466 | */ |
| 3467 | struct pipe *pi; |
| 3468 | struct command *command; |
| 3469 | |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3470 | /* Is this actually this construct, all pipes end with && or ||? */ |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3471 | pi = ctx->list_head; |
| 3472 | while (pi != ctx->pipe) { |
| 3473 | if (pi->followup != PIPE_AND && pi->followup != PIPE_OR) |
| 3474 | goto no_conv; |
| 3475 | pi = pi->next; |
| 3476 | } |
| 3477 | |
| 3478 | debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n"); |
| 3479 | pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */ |
| 3480 | pi = xzalloc(sizeof(*pi)); |
| 3481 | pi->followup = PIPE_BG; |
| 3482 | pi->num_cmds = 1; |
| 3483 | pi->cmds = xzalloc(sizeof(pi->cmds[0])); |
| 3484 | command = &pi->cmds[0]; |
| 3485 | if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */ |
| 3486 | command->cmd_type = CMD_NORMAL; |
| 3487 | command->group = ctx->list_head; |
| 3488 | #if !BB_MMU |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3489 | command->group_as_string = xstrndup( |
| 3490 | ctx->as_string.data, |
| 3491 | ctx->as_string.length - 1 /* do not copy last char, "&" */ |
| 3492 | ); |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3493 | #endif |
| 3494 | /* Replace all pipes in ctx with one newly created */ |
| 3495 | ctx->list_head = ctx->pipe = pi; |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 3496 | } else { |
| 3497 | no_conv: |
| 3498 | ctx->pipe->followup = type; |
Denys Vlasenko | ee553b9 | 2017-07-15 22:51:55 +0200 | [diff] [blame] | 3499 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3500 | |
| 3501 | /* Without this check, even just <enter> on command line generates |
| 3502 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3503 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3504 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3505 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3506 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3507 | #endif |
| 3508 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3509 | || ctx->ctx_res_w == RES_DONE |
| 3510 | || ctx->ctx_res_w == RES_FOR |
| 3511 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 3512 | #endif |
| 3513 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3514 | || ctx->ctx_res_w == RES_ESAC |
| 3515 | #endif |
| 3516 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3517 | struct pipe *new_p; |
| 3518 | debug_printf_parse("done_pipe: adding new pipe: " |
| 3519 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 3520 | not_null, ctx->ctx_res_w); |
| 3521 | new_p = new_pipe(); |
| 3522 | ctx->pipe->next = new_p; |
| 3523 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3524 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3525 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3526 | * This is used to control execution. |
| 3527 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 3528 | * cases where variable or value happens to match a keyword): |
| 3529 | */ |
| 3530 | #if ENABLE_HUSH_LOOPS |
| 3531 | if (ctx->ctx_res_w == RES_FOR |
| 3532 | || ctx->ctx_res_w == RES_IN) |
| 3533 | ctx->ctx_res_w = RES_NONE; |
| 3534 | #endif |
| 3535 | #if ENABLE_HUSH_CASE |
| 3536 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3537 | ctx->ctx_res_w = RES_CASE_BODY; |
| 3538 | if (ctx->ctx_res_w == RES_CASE) |
| 3539 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3540 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3541 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3542 | /* Create the memory for command, roughly: |
| 3543 | * ctx->pipe->cmds = new struct command; |
| 3544 | * ctx->command = &ctx->pipe->cmds[0]; |
| 3545 | */ |
| 3546 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 3547 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3548 | } |
| 3549 | debug_printf_parse("done_pipe return\n"); |
| 3550 | } |
| 3551 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3552 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3553 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3554 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3555 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3556 | /* Create the memory for command, roughly: |
| 3557 | * ctx->pipe->cmds = new struct command; |
| 3558 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3559 | */ |
| 3560 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3563 | /* If a reserved word is found and processed, parse context is modified |
| 3564 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3565 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3566 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3567 | struct reserved_combo { |
| 3568 | char literal[6]; |
| 3569 | unsigned char res; |
| 3570 | unsigned char assignment_flag; |
| 3571 | int flag; |
| 3572 | }; |
| 3573 | enum { |
| 3574 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3575 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3576 | FLAG_IF = (1 << RES_IF ), |
| 3577 | FLAG_THEN = (1 << RES_THEN ), |
| 3578 | FLAG_ELIF = (1 << RES_ELIF ), |
| 3579 | FLAG_ELSE = (1 << RES_ELSE ), |
| 3580 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3581 | # endif |
| 3582 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3583 | FLAG_FOR = (1 << RES_FOR ), |
| 3584 | FLAG_WHILE = (1 << RES_WHILE), |
| 3585 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 3586 | FLAG_DO = (1 << RES_DO ), |
| 3587 | FLAG_DONE = (1 << RES_DONE ), |
| 3588 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3589 | # endif |
| 3590 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3591 | FLAG_MATCH = (1 << RES_MATCH), |
| 3592 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3593 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3594 | FLAG_START = (1 << RES_XXXX ), |
| 3595 | }; |
| 3596 | |
| 3597 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 3598 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3599 | /* Mostly a list of accepted follow-up reserved words. |
| 3600 | * FLAG_END means we are done with the sequence, and are ready |
| 3601 | * to turn the compound list into a command. |
| 3602 | * FLAG_START means the word must start a new compound list. |
| 3603 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3604 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3605 | # if ENABLE_HUSH_IF |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3606 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 3607 | { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START }, |
| 3608 | { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 3609 | { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN }, |
| 3610 | { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI }, |
| 3611 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3612 | # endif |
| 3613 | # if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3614 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 3615 | { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3616 | { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START }, |
| 3617 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 3618 | { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE }, |
| 3619 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3620 | # endif |
| 3621 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3622 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 3623 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3624 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3625 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3626 | const struct reserved_combo *r; |
| 3627 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 3628 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3629 | if (strcmp(word->data, r->literal) == 0) |
| 3630 | return r; |
| 3631 | } |
| 3632 | return NULL; |
| 3633 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3634 | /* Return 0: not a keyword, 1: keyword |
| 3635 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3636 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 3637 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3638 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3639 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3640 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3641 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3642 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3643 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 3644 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3645 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3646 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3647 | r = match_reserved_word(word); |
| 3648 | if (!r) |
| 3649 | return 0; |
| 3650 | |
| 3651 | 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] | 3652 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3653 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 3654 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3655 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3656 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3657 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3658 | if (r->flag == 0) { /* '!' */ |
| 3659 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3660 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3661 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3662 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3663 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3664 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3665 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3666 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3667 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3668 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 3669 | old = xmemdup(ctx, sizeof(*ctx)); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3670 | debug_printf_parse("push stack %p\n", old); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3671 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3672 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3673 | } 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] | 3674 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3675 | ctx->ctx_res_w = RES_SNTX; |
| 3676 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3677 | } else { |
| 3678 | /* "{...} fi" is ok. "{...} if" is not |
| 3679 | * Example: |
| 3680 | * if { echo foo; } then { echo bar; } fi */ |
| 3681 | if (ctx->command->group) |
| 3682 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3683 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3684 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3685 | ctx->ctx_res_w = r->res; |
| 3686 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3687 | word->o_assignment = r->assignment_flag; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3688 | 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] | 3689 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3690 | if (ctx->old_flag & FLAG_END) { |
| 3691 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3692 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3693 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3694 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3695 | old = ctx->stack; |
| 3696 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3697 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3698 | # if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 3699 | /* At this point, the compound command's string is in |
| 3700 | * ctx->as_string... except for the leading keyword! |
| 3701 | * Consider this example: "echo a | if true; then echo a; fi" |
| 3702 | * ctx->as_string will contain "true; then echo a; fi", |
| 3703 | * with "if " remaining in old->as_string! |
| 3704 | */ |
| 3705 | { |
| 3706 | char *str; |
| 3707 | int len = old->as_string.length; |
| 3708 | /* Concatenate halves */ |
| 3709 | o_addstr(&old->as_string, ctx->as_string.data); |
| 3710 | o_free_unsafe(&ctx->as_string); |
| 3711 | /* Find where leading keyword starts in first half */ |
| 3712 | str = old->as_string.data + len; |
| 3713 | if (str > old->as_string.data) |
| 3714 | str--; /* skip whitespace after keyword */ |
| 3715 | while (str > old->as_string.data && isalpha(str[-1])) |
| 3716 | str--; |
| 3717 | /* Ugh, we're done with this horrid hack */ |
| 3718 | old->command->group_as_string = xstrdup(str); |
| 3719 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 3720 | old->command->group_as_string); |
| 3721 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3722 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3723 | *ctx = *old; /* physical copy */ |
| 3724 | free(old); |
| 3725 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 3726 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3728 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3729 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3730 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3731 | * Normal return is 0. Syntax errors return 1. |
| 3732 | * Note: on return, word is reset, but not o_free'd! |
| 3733 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3734 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3735 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3736 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3737 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3738 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3739 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3740 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3741 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3742 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3743 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3744 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3745 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3746 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3747 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3748 | * "2.7 Redirection |
| 3749 | * ...the word that follows the redirection operator |
| 3750 | * shall be subjected to tilde expansion, parameter expansion, |
| 3751 | * command substitution, arithmetic expansion, and quote |
| 3752 | * removal. Pathname expansion shall not be performed |
| 3753 | * on the word by a non-interactive shell; an interactive |
| 3754 | * shell may perform it, but shall do so only when |
| 3755 | * the expansion would result in one word." |
| 3756 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3757 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3758 | /* Cater for >\file case: |
| 3759 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3760 | * Same with heredocs: |
| 3761 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3762 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3763 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3764 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3765 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3766 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3767 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3768 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3769 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3770 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3771 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3772 | } else { |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3773 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3774 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3775 | if (ctx->ctx_dsemicolon |
| 3776 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3777 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3778 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3779 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3780 | ctx->ctx_dsemicolon = 0; |
| 3781 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3782 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3783 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3784 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3785 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3786 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3787 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3788 | # if ENABLE_HUSH_CASE |
| 3789 | && ctx->ctx_res_w != RES_CASE |
| 3790 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3791 | ) { |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3792 | int reserved = reserved_word(word, ctx); |
| 3793 | debug_printf_parse("checking for reserved-ness: %d\n", reserved); |
| 3794 | if (reserved) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3795 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3796 | debug_printf_parse("done_word return %d\n", |
| 3797 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3798 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3799 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 3800 | # if BASH_TEST2 |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3801 | if (strcmp(word->data, "[[") == 0) { |
| 3802 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3803 | } |
| 3804 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3805 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3806 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3807 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3808 | if (command->group) { |
| 3809 | /* "{ echo foo; } echo bar" - bad */ |
| 3810 | syntax_error_at(word->data); |
| 3811 | debug_printf_parse("done_word return 1: syntax error, " |
| 3812 | "groups and arglists don't mix\n"); |
| 3813 | return 1; |
| 3814 | } |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 3815 | |
| 3816 | /* If this word wasn't an assignment, next ones definitely |
| 3817 | * can't be assignments. Even if they look like ones. */ |
| 3818 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3819 | && word->o_assignment != WORD_IS_KEYWORD |
| 3820 | ) { |
| 3821 | word->o_assignment = NOT_ASSIGNMENT; |
| 3822 | } else { |
| 3823 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) { |
| 3824 | command->assignment_cnt++; |
| 3825 | debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt); |
| 3826 | } |
| 3827 | debug_printf_parse("word->o_assignment was:'%s'\n", assignment_flag[word->o_assignment]); |
| 3828 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3829 | } |
| 3830 | debug_printf_parse("word->o_assignment='%s'\n", assignment_flag[word->o_assignment]); |
| 3831 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3832 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3833 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3834 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3835 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3836 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3837 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3838 | char *p = word->data; |
| 3839 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3840 | && (p[1] & 0x7f) == '@' |
| 3841 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3842 | ) { |
| 3843 | p += 3; |
| 3844 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3845 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3846 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3847 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3848 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3849 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3850 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3851 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3852 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3853 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3854 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3855 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3856 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3857 | return 1; |
| 3858 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3859 | /* Force FOR to have just one word (variable name) */ |
| 3860 | /* NB: basically, this makes hush see "for v in ..." |
| 3861 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3862 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3863 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3864 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3865 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3866 | #if ENABLE_HUSH_CASE |
| 3867 | /* Force CASE to have just one word */ |
| 3868 | if (ctx->ctx_res_w == RES_CASE) { |
| 3869 | done_pipe(ctx, PIPE_SEQ); |
| 3870 | } |
| 3871 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3872 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3873 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3874 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3875 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3876 | return 0; |
| 3877 | } |
| 3878 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3879 | |
| 3880 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3881 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3882 | * Return: |
| 3883 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3884 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3885 | * REDIRFD_TO_FILE if no & was seen, |
| 3886 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3887 | */ |
| 3888 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3889 | #define parse_redir_right_fd(as_string, input) \ |
| 3890 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3891 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3892 | 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] | 3893 | { |
| 3894 | int ch, d, ok; |
| 3895 | |
| 3896 | ch = i_peek(input); |
| 3897 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3898 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3899 | |
| 3900 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3901 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3902 | ch = i_peek(input); |
| 3903 | if (ch == '-') { |
| 3904 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3905 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3906 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3907 | } |
| 3908 | d = 0; |
| 3909 | ok = 0; |
| 3910 | while (ch != EOF && isdigit(ch)) { |
| 3911 | d = d*10 + (ch-'0'); |
| 3912 | ok = 1; |
| 3913 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3914 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3915 | ch = i_peek(input); |
| 3916 | } |
| 3917 | if (ok) return d; |
| 3918 | |
| 3919 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3920 | |
| 3921 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3922 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3925 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3926 | */ |
| 3927 | static int parse_redirect(struct parse_context *ctx, |
| 3928 | int fd, |
| 3929 | redir_type style, |
| 3930 | struct in_str *input) |
| 3931 | { |
| 3932 | struct command *command = ctx->command; |
| 3933 | struct redir_struct *redir; |
| 3934 | struct redir_struct **redirp; |
| 3935 | int dup_num; |
| 3936 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3937 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3938 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3939 | /* Check for a '>&1' type redirect */ |
| 3940 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3941 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3942 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3943 | } else { |
| 3944 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3945 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3946 | if (dup_num) { /* <<-... */ |
| 3947 | ch = i_getch(input); |
| 3948 | nommu_addchr(&ctx->as_string, ch); |
| 3949 | ch = i_peek(input); |
| 3950 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3951 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3952 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3953 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3954 | int ch = i_peek(input); |
| 3955 | if (ch == '|') { |
| 3956 | /* >|FILE redirect ("clobbering" >). |
| 3957 | * Since we do not support "set -o noclobber" yet, |
| 3958 | * >| and > are the same for now. Just eat |. |
| 3959 | */ |
| 3960 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3961 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | /* Create a new redir_struct and append it to the linked list */ |
| 3966 | redirp = &command->redirects; |
| 3967 | while ((redir = *redirp) != NULL) { |
| 3968 | redirp = &(redir->next); |
| 3969 | } |
| 3970 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3971 | /* redir->next = NULL; */ |
| 3972 | /* redir->rd_filename = NULL; */ |
| 3973 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3974 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3975 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3976 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3977 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3978 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3979 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3980 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3981 | /* Erik had a check here that the file descriptor in question |
| 3982 | * is legit; I postpone that to "run time" |
| 3983 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3984 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3985 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3986 | } else { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3987 | #if 0 /* Instead we emit error message at run time */ |
| 3988 | if (ctx->pending_redirect) { |
| 3989 | /* For example, "cmd > <file" */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 3990 | syntax_error("invalid redirect"); |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 3991 | } |
| 3992 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3993 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3994 | * end of the next parsed word. */ |
| 3995 | ctx->pending_redirect = redir; |
| 3996 | } |
| 3997 | return 0; |
| 3998 | } |
| 3999 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4000 | /* If a redirect is immediately preceded by a number, that number is |
| 4001 | * supposed to tell which file descriptor to redirect. This routine |
| 4002 | * looks for such preceding numbers. In an ideal world this routine |
| 4003 | * needs to handle all the following classes of redirects... |
| 4004 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 4005 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 4006 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 4007 | * 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] | 4008 | * |
| 4009 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 4010 | * "2.7 Redirection |
| 4011 | * ... If n is quoted, the number shall not be recognized as part of |
| 4012 | * the redirection expression. For example: |
| 4013 | * echo \2>a |
| 4014 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4015 | * 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] | 4016 | * |
| 4017 | * A -1 return means no valid number was found, |
| 4018 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4019 | */ |
| 4020 | static int redirect_opt_num(o_string *o) |
| 4021 | { |
| 4022 | int num; |
| 4023 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4024 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4025 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4026 | num = bb_strtou(o->data, NULL, 10); |
| 4027 | if (errno || num < 0) |
| 4028 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4029 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4030 | return num; |
| 4031 | } |
| 4032 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4033 | #if BB_MMU |
| 4034 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 4035 | fetch_till_str(input, word, skip_tabs) |
| 4036 | #endif |
| 4037 | static char *fetch_till_str(o_string *as_string, |
| 4038 | struct in_str *input, |
| 4039 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4040 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4041 | { |
| 4042 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4043 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4044 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4045 | int ch; |
| 4046 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4047 | goto jump_in; |
Denys Vlasenko | b870903 | 2011-05-08 21:20:01 +0200 | [diff] [blame] | 4048 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4049 | while (1) { |
| 4050 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4051 | if (ch != EOF) |
| 4052 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4053 | if (ch == '\n' || ch == EOF) { |
| 4054 | check_heredoc_end: |
| 4055 | if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') { |
| 4056 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 4057 | heredoc.data[past_EOL] = '\0'; |
| 4058 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 4059 | return heredoc.data; |
| 4060 | } |
| 4061 | if (ch == '\n') { |
| 4062 | /* This is a new line. |
| 4063 | * Remember position and backslash-escaping status. |
| 4064 | */ |
| 4065 | o_addchr(&heredoc, ch); |
| 4066 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4067 | jump_in: |
Denys Vlasenko | 0f018b3 | 2017-07-29 20:43:26 +0200 | [diff] [blame] | 4068 | past_EOL = heredoc.length; |
| 4069 | /* Get 1st char of next line, possibly skipping leading tabs */ |
| 4070 | do { |
| 4071 | ch = i_getch(input); |
| 4072 | if (ch != EOF) |
| 4073 | nommu_addchr(as_string, ch); |
| 4074 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
| 4075 | /* If this immediately ended the line, |
| 4076 | * go back to end-of-line checks. |
| 4077 | */ |
| 4078 | if (ch == '\n') |
| 4079 | goto check_heredoc_end; |
| 4080 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4081 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4082 | } |
| 4083 | if (ch == EOF) { |
| 4084 | o_free_unsafe(&heredoc); |
| 4085 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4086 | } |
| 4087 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4088 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 4089 | if (prev == '\\' && ch == '\\') |
| 4090 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 4091 | prev = 0; /* not \ */ |
| 4092 | else |
| 4093 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4094 | } |
| 4095 | } |
| 4096 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4097 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 4098 | * and load them all. There should be exactly heredoc_cnt of them. |
| 4099 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4100 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 4101 | { |
| 4102 | struct pipe *pi = ctx->list_head; |
| 4103 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4104 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4105 | int i; |
| 4106 | struct command *cmd = pi->cmds; |
| 4107 | |
| 4108 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 4109 | pi->num_cmds, |
| 4110 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 4111 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4112 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4113 | |
| 4114 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 4115 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4116 | while (redir) { |
| 4117 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4118 | char *p; |
| 4119 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4120 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4121 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4122 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4123 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4124 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4125 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4126 | return 1; |
| 4127 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4128 | free(redir->rd_filename); |
| 4129 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4130 | heredoc_cnt--; |
| 4131 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4132 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4133 | } |
| 4134 | cmd++; |
| 4135 | } |
| 4136 | pi = pi->next; |
| 4137 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4138 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4139 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4140 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4141 | bb_error_msg_and_die("heredoc BUG 2"); |
| 4142 | #endif |
| 4143 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4147 | static int run_list(struct pipe *pi); |
| 4148 | #if BB_MMU |
| 4149 | #define parse_stream(pstring, input, end_trigger) \ |
| 4150 | parse_stream(input, end_trigger) |
| 4151 | #endif |
| 4152 | static struct pipe *parse_stream(char **pstring, |
| 4153 | struct in_str *input, |
| 4154 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 4155 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4156 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 4157 | #if !ENABLE_HUSH_FUNCTIONS |
| 4158 | #define parse_group(dest, ctx, input, ch) \ |
| 4159 | parse_group(ctx, input, ch) |
| 4160 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4161 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4162 | struct in_str *input, int ch) |
| 4163 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4164 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4165 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4166 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4167 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4168 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4169 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4170 | |
| 4171 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4172 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4173 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4174 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4175 | if (done_word(dest, ctx)) |
| 4176 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4177 | if (!command->argv) |
| 4178 | goto skip; /* (... */ |
| 4179 | if (command->argv[1]) { /* word word ... (... */ |
| 4180 | syntax_error_unexpected_ch('('); |
| 4181 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4182 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4183 | /* it is "word(..." or "word (..." */ |
| 4184 | do |
| 4185 | ch = i_getch(input); |
| 4186 | while (ch == ' ' || ch == '\t'); |
| 4187 | if (ch != ')') { |
| 4188 | syntax_error_unexpected_ch(ch); |
| 4189 | return 1; |
| 4190 | } |
| 4191 | nommu_addchr(&ctx->as_string, ch); |
| 4192 | do |
| 4193 | ch = i_getch(input); |
| 4194 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 4195 | if (ch != '{') { |
| 4196 | syntax_error_unexpected_ch(ch); |
| 4197 | return 1; |
| 4198 | } |
| 4199 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4200 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4201 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4202 | } |
| 4203 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4204 | |
| 4205 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4206 | if (command->argv /* word [word]{... */ |
| 4207 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4208 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4209 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4210 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4211 | debug_printf_parse("parse_group return 1: " |
| 4212 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4213 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4214 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4215 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4216 | |
| 4217 | #if ENABLE_HUSH_FUNCTIONS |
| 4218 | skip: |
| 4219 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4220 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4221 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4222 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4223 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4224 | } else { |
| 4225 | /* bash does not allow "{echo...", requires whitespace */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4226 | ch = i_peek(input); |
| 4227 | if (ch != ' ' && ch != '\t' && ch != '\n' |
| 4228 | && ch != '(' /* but "{(..." is allowed (without whitespace) */ |
| 4229 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 4230 | syntax_error_unexpected_ch(ch); |
| 4231 | return 1; |
| 4232 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4233 | if (ch != '(') { |
| 4234 | ch = i_getch(input); |
| 4235 | nommu_addchr(&ctx->as_string, ch); |
| 4236 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4237 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4238 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4239 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4240 | #if BB_MMU |
| 4241 | # define as_string NULL |
| 4242 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4243 | char *as_string = NULL; |
| 4244 | #endif |
| 4245 | pipe_list = parse_stream(&as_string, input, endch); |
| 4246 | #if !BB_MMU |
| 4247 | if (as_string) |
| 4248 | o_addstr(&ctx->as_string, as_string); |
| 4249 | #endif |
| 4250 | /* empty ()/{} or parse error? */ |
| 4251 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 4252 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4253 | if (!BB_MMU) |
| 4254 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4255 | debug_printf_parse("parse_group return 1: " |
| 4256 | "parse_stream returned %p\n", pipe_list); |
| 4257 | return 1; |
| 4258 | } |
| 4259 | command->group = pipe_list; |
| 4260 | #if !BB_MMU |
| 4261 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 4262 | command->group_as_string = as_string; |
| 4263 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 4264 | command->group_as_string); |
| 4265 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4266 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4267 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4268 | debug_printf_parse("parse_group return 0\n"); |
| 4269 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4270 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4273 | static int i_getch_and_eat_bkslash_nl(struct in_str *input) |
| 4274 | { |
| 4275 | for (;;) { |
| 4276 | int ch, ch2; |
| 4277 | |
| 4278 | ch = i_getch(input); |
| 4279 | if (ch != '\\') |
| 4280 | return ch; |
| 4281 | ch2 = i_peek(input); |
| 4282 | if (ch2 != '\n') |
| 4283 | return ch; |
| 4284 | /* backslash+newline, skip it */ |
| 4285 | i_getch(input); |
| 4286 | } |
| 4287 | } |
| 4288 | |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4289 | static int i_peek_and_eat_bkslash_nl(struct in_str *input) |
| 4290 | { |
| 4291 | for (;;) { |
| 4292 | int ch, ch2; |
| 4293 | |
| 4294 | ch = i_peek(input); |
| 4295 | if (ch != '\\') |
| 4296 | return ch; |
| 4297 | ch2 = i_peek2(input); |
| 4298 | if (ch2 != '\n') |
| 4299 | return ch; |
| 4300 | /* backslash+newline, skip it */ |
| 4301 | i_getch(input); |
| 4302 | i_getch(input); |
| 4303 | } |
| 4304 | } |
| 4305 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4306 | #if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4307 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4308 | 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] | 4309 | /* '...' */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4310 | 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] | 4311 | { |
| 4312 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4313 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4314 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4315 | syntax_error_unterm_ch('\''); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4316 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4317 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4318 | if (ch == '\'') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4319 | return 1; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4320 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4321 | } |
| 4322 | } |
| 4323 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4324 | 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] | 4325 | { |
| 4326 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4327 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4328 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4329 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4330 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4331 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4332 | if (ch == '"') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4333 | return 1; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4334 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4335 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4336 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4337 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4338 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4339 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4340 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 1)) |
| 4341 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4342 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4343 | continue; |
| 4344 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 4345 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 4349 | * \` quoting. |
| 4350 | * "Within the backquoted style of command substitution, backslash |
| 4351 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 4352 | * The search for the matching backquote shall be satisfied by the first |
| 4353 | * backquote found without a preceding backslash; during this search, |
| 4354 | * if a non-escaped backquote is encountered within a shell comment, |
| 4355 | * a here-document, an embedded command substitution of the $(command) |
| 4356 | * form, or a quoted string, undefined results occur. A single-quoted |
| 4357 | * or double-quoted string that begins, but does not end, within the |
| 4358 | * "`...`" sequence produces undefined results." |
| 4359 | * Example Output |
| 4360 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 4361 | */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4362 | 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] | 4363 | { |
| 4364 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4365 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4366 | if (ch == '`') |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4367 | return 1; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4368 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4369 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 4370 | ch = i_getch(input); |
| 4371 | if (ch != '`' |
| 4372 | && ch != '$' |
| 4373 | && ch != '\\' |
| 4374 | && (!in_dquote || ch != '"') |
| 4375 | ) { |
| 4376 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4377 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4378 | } |
| 4379 | if (ch == EOF) { |
| 4380 | syntax_error_unterm_ch('`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4381 | return 0; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4382 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4383 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4384 | } |
| 4385 | } |
| 4386 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 4387 | * quoting and nested ()s. |
| 4388 | * "With the $(command) style of command substitution, all characters |
| 4389 | * following the open parenthesis to the matching closing parenthesis |
| 4390 | * constitute the command. Any valid shell script can be used for command, |
| 4391 | * except a script consisting solely of redirections which produces |
| 4392 | * unspecified results." |
| 4393 | * Example Output |
| 4394 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 4395 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 4396 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4397 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4398 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4399 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4400 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 4401 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4402 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4403 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4404 | 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] | 4405 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4406 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4407 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4408 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4409 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4410 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4411 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 4412 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4413 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4414 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4415 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4416 | syntax_error_unterm_ch(end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4417 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4418 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4419 | if (ch == end_ch |
| 4420 | # if BASH_SUBSTR || BASH_PATTERN_SUBST |
| 4421 | || ch == end_char2 |
| 4422 | # endif |
| 4423 | ) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4424 | if (!dbl) |
| 4425 | break; |
| 4426 | /* we look for closing )) of $((EXPR)) */ |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4427 | if (i_peek_and_eat_bkslash_nl(input) == end_ch) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4428 | i_getch(input); /* eat second ')' */ |
| 4429 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4430 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4431 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4432 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4433 | if (ch == '(' || ch == '{') { |
| 4434 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4435 | if (!add_till_closing_bracket(dest, input, ch)) |
| 4436 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4437 | o_addchr(dest, ch); |
| 4438 | continue; |
| 4439 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4440 | if (ch == '\'') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4441 | if (!add_till_single_quote(dest, input)) |
| 4442 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4443 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4444 | continue; |
| 4445 | } |
| 4446 | if (ch == '"') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4447 | if (!add_till_double_quote(dest, input)) |
| 4448 | return 0; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4449 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4450 | continue; |
| 4451 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4452 | if (ch == '`') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4453 | if (!add_till_backquote(dest, input, /*in_dquote:*/ 0)) |
| 4454 | return 0; |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 4455 | o_addchr(dest, ch); |
| 4456 | continue; |
| 4457 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4458 | if (ch == '\\') { |
| 4459 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4460 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4461 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4462 | syntax_error_unterm_ch(')'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4463 | return 0; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4464 | } |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4465 | #if 0 |
| 4466 | if (ch == '\n') { |
| 4467 | /* "backslash+newline", ignore both */ |
| 4468 | o_delchr(dest); /* undo insertion of '\' */ |
| 4469 | continue; |
| 4470 | } |
| 4471 | #endif |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 4472 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4473 | continue; |
| 4474 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4475 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4476 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4477 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4478 | #endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4479 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 4480 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4481 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4482 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 4483 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4484 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4485 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4486 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4487 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4488 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4489 | { |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4490 | 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] | 4491 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4492 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4493 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4494 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4495 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 4496 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4497 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4498 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 4499 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4500 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 4501 | quote_mask = 0; |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4502 | ch = i_peek_and_eat_bkslash_nl(input); |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4503 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4504 | /* End of variable name reached */ |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4505 | break; |
Denys Vlasenko | d17a91d | 2016-09-29 18:02:37 +0200 | [diff] [blame] | 4506 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4507 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4508 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4509 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4510 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4511 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4512 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4513 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4514 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4515 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 4516 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4517 | o_addchr(dest, ch | quote_mask); |
| 4518 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4519 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4520 | case '$': /* pid */ |
| 4521 | case '!': /* last bg pid */ |
| 4522 | case '?': /* last exit code */ |
| 4523 | case '#': /* number of args */ |
| 4524 | case '*': /* args */ |
| 4525 | case '@': /* args */ |
| 4526 | goto make_one_char_var; |
| 4527 | case '{': { |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4528 | char len_single_ch; |
| 4529 | |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 4530 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4531 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4532 | ch = i_getch(input); /* eat '{' */ |
| 4533 | nommu_addchr(as_string, ch); |
| 4534 | |
Denys Vlasenko | 46e6498 | 2016-09-29 19:50:55 +0200 | [diff] [blame] | 4535 | ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4536 | /* It should be ${?}, or ${#var}, |
| 4537 | * or even ${?+subst} - operator acting on a special variable, |
| 4538 | * or the beginning of variable name. |
| 4539 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4540 | if (ch == EOF |
| 4541 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 4542 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4543 | bad_dollar_syntax: |
| 4544 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4545 | debug_printf_parse("parse_dollar return 0: unterminated ${name}\n"); |
| 4546 | return 0; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4547 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4548 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4549 | len_single_ch = ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4550 | ch |= quote_mask; |
| 4551 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4552 | /* 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] | 4553 | * However, this regresses some of our testsuite cases |
| 4554 | * which check invalid constructs like ${%}. |
| 4555 | * Oh well... let's check that the var name part is fine... */ |
| 4556 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4557 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4558 | unsigned pos; |
| 4559 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4560 | o_addchr(dest, ch); |
| 4561 | debug_printf_parse(": '%c'\n", ch); |
| 4562 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4563 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4564 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4565 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4566 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4567 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4568 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4569 | unsigned end_ch; |
| 4570 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4571 | /* handle parameter expansions |
| 4572 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 4573 | */ |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4574 | if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */ |
| 4575 | if (len_single_ch != '#' |
| 4576 | /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */ |
| 4577 | || i_peek(input) != '}' |
| 4578 | ) { |
| 4579 | goto bad_dollar_syntax; |
| 4580 | } |
| 4581 | /* else: it's "length of C" ${#C} op, |
| 4582 | * where C is a single char |
| 4583 | * special var name, e.g. ${#!}. |
| 4584 | */ |
| 4585 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4586 | /* Eat everything until closing '}' (or ':') */ |
| 4587 | end_ch = '}'; |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4588 | if (BASH_SUBSTR |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4589 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4590 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4591 | ) { |
| 4592 | /* It's ${var:N[:M]} thing */ |
| 4593 | end_ch = '}' * 0x100 + ':'; |
| 4594 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4595 | if (BASH_PATTERN_SUBST |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4596 | && ch == '/' |
| 4597 | ) { |
| 4598 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 4599 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 4600 | i_getch(input); |
| 4601 | nommu_addchr(as_string, '/'); |
| 4602 | ch = '\\'; |
| 4603 | } |
| 4604 | end_ch = '}' * 0x100 + '/'; |
| 4605 | } |
| 4606 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4607 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4608 | if (!BB_MMU) |
| 4609 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4610 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4611 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4612 | if (last_ch == 0) /* error? */ |
| 4613 | return 0; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 4614 | #else |
| 4615 | #error Simple code to only allow ${var} is not implemented |
| 4616 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4617 | if (as_string) { |
| 4618 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4619 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4620 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4621 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4622 | if ((BASH_SUBSTR || BASH_PATTERN_SUBST) |
| 4623 | && (end_ch & 0xff00) |
| 4624 | ) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4625 | /* close the first block: */ |
| 4626 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4627 | /* while parsing N from ${var:N[:M]} |
| 4628 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4629 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4630 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4631 | end_ch = '}'; |
| 4632 | goto again; |
| 4633 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4634 | /* got '}' */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4635 | if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 4636 | /* it's ${var:N} - emulate :999999999 */ |
| 4637 | o_addstr(dest, "999999999"); |
| 4638 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 4639 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4640 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4641 | } |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 4642 | len_single_ch = 0; /* it can't be ${#C} op */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 4643 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4644 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4645 | break; |
| 4646 | } |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4647 | #if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4648 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4649 | unsigned pos; |
| 4650 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4651 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4652 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 4653 | # if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4654 | if (i_peek_and_eat_bkslash_nl(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +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); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4657 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4658 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4659 | if (!BB_MMU) |
| 4660 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4661 | if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG)) |
| 4662 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4663 | if (as_string) { |
| 4664 | o_addstr(as_string, dest->data + pos); |
| 4665 | o_addchr(as_string, ')'); |
| 4666 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4667 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4668 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4669 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 4670 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4671 | # endif |
| 4672 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4673 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4674 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4675 | if (!BB_MMU) |
| 4676 | pos = dest->length; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4677 | if (!add_till_closing_bracket(dest, input, ')')) |
| 4678 | return 0; /* error */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4679 | if (as_string) { |
| 4680 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4681 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 4682 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4683 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4684 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4685 | break; |
| 4686 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 4687 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4688 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4689 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4690 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 657086a | 2016-09-29 18:07:42 +0200 | [diff] [blame] | 4691 | ch = i_peek_and_eat_bkslash_nl(input); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4692 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 4693 | ch = '_'; |
| 4694 | goto make_var; |
| 4695 | } |
| 4696 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 4697 | /* TODO: $_ and $-: */ |
| 4698 | /* $_ Shell or shell script name; or last argument of last command |
| 4699 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 4700 | * 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] | 4701 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 4702 | default: |
| 4703 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4704 | } |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4705 | debug_printf_parse("parse_dollar return 1 (ok)\n"); |
| 4706 | return 1; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4707 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4710 | #if BB_MMU |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4711 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4712 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4713 | encode_string(dest, input, dquote_end, process_bkslash) |
| 4714 | # else |
| 4715 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4716 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4717 | encode_string(dest, input, dquote_end) |
| 4718 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4719 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4720 | |
| 4721 | #else /* !MMU */ |
| 4722 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4723 | # if BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4724 | /* all parameters are needed, no macro tricks */ |
| 4725 | # else |
| 4726 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 4727 | encode_string(as_string, dest, input, dquote_end) |
| 4728 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4729 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4730 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4731 | o_string *dest, |
| 4732 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 4733 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4734 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4735 | { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 4736 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4737 | const int process_bkslash = 1; |
| 4738 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4739 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4740 | int next; |
| 4741 | |
| 4742 | again: |
| 4743 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4744 | if (ch != EOF) |
| 4745 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4746 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4747 | debug_printf_parse("encode_string return 1 (ok)\n"); |
| 4748 | return 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4749 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4750 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4751 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4752 | syntax_error_unterm_ch('"'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4753 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4754 | } |
| 4755 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4756 | if (ch != '\n') { |
| 4757 | next = i_peek(input); |
| 4758 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4759 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4760 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4761 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4762 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4763 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4764 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4765 | } |
| 4766 | /* bash: |
| 4767 | * "The backslash retains its special meaning [in "..."] |
| 4768 | * only when followed by one of the following characters: |
| 4769 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4770 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4771 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 4772 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4773 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4774 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 4775 | ch = i_getch(input); /* eat next */ |
| 4776 | if (ch == '\n') |
| 4777 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4778 | } /* else: ch remains == '\\', and we double it below: */ |
| 4779 | 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] | 4780 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4781 | goto again; |
| 4782 | } |
| 4783 | if (ch == '$') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4784 | if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) { |
| 4785 | debug_printf_parse("encode_string return 0: " |
| 4786 | "parse_dollar returned 0 (error)\n"); |
| 4787 | return 0; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4788 | } |
| 4789 | goto again; |
| 4790 | } |
| 4791 | #if ENABLE_HUSH_TICK |
| 4792 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4793 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4794 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4795 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4796 | if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"')) |
| 4797 | return 0; /* error */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4798 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 4799 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4800 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4801 | } |
| 4802 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 4803 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4804 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 4805 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 4806 | } |
| 4807 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4808 | /* |
| 4809 | * Scan input until EOF or end_trigger char. |
| 4810 | * Return a list of pipes to execute, or NULL on EOF |
| 4811 | * or if end_trigger character is met. |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 4812 | * On syntax error, exit if shell is not interactive, |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4813 | * reset parsing machinery and start parsing anew, |
| 4814 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4815 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4816 | static struct pipe *parse_stream(char **pstring, |
| 4817 | struct in_str *input, |
| 4818 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4819 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4820 | struct parse_context ctx; |
| 4821 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4822 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4823 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4824 | /* 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] | 4825 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4826 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4827 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4828 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4829 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4830 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4831 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4832 | * Preventing this: */ |
| 4833 | o_addchr(&dest, '\0'); |
| 4834 | dest.length = 0; |
| 4835 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4836 | /* We used to separate words on $IFS here. This was wrong. |
| 4837 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4838 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4839 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4840 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4841 | if (MAYBE_ASSIGNMENT != 0) |
| 4842 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4843 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4844 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4845 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4846 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4847 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4848 | int ch; |
| 4849 | int next; |
| 4850 | int redir_fd; |
| 4851 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4852 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4853 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [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)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4856 | if (ch == EOF) { |
| 4857 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4858 | |
| 4859 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4860 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4861 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4862 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4863 | if (end_trigger == ')') { |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 4864 | syntax_error_unterm_ch('('); |
| 4865 | goto parse_error; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4866 | } |
Denys Vlasenko | 4224647 | 2016-11-07 16:22:35 +0100 | [diff] [blame] | 4867 | if (end_trigger == '}') { |
| 4868 | syntax_error_unterm_ch('{'); |
| 4869 | goto parse_error; |
| 4870 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4871 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4872 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4873 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4874 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4875 | o_free(&dest); |
| 4876 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4877 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4878 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4879 | /* (this makes bare "&" cmd a no-op. |
| 4880 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4881 | if (pi->num_cmds == 0 |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 4882 | IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE) |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4883 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4884 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4885 | pi = NULL; |
| 4886 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4887 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 4888 | debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4889 | if (pstring) |
| 4890 | *pstring = ctx.as_string.data; |
| 4891 | else |
| 4892 | o_free_unsafe(&ctx.as_string); |
| 4893 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4894 | debug_leave(); |
| 4895 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4896 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4897 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4898 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4899 | |
| 4900 | next = '\0'; |
| 4901 | if (ch != '\n') |
| 4902 | next = i_peek(input); |
| 4903 | |
| 4904 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4905 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4906 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4907 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4908 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4909 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4910 | || (next != ';' /* }; - special */ |
| 4911 | && next != ')' /* }) - special */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4912 | && next != '(' /* {( - special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4913 | && next != '&' /* }& and }&& ... - special */ |
| 4914 | && next != '|' /* }|| ... - special */ |
| 4915 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4916 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4917 | ) { |
| 4918 | /* They are not special, skip "{}" */ |
| 4919 | is_special += 2; |
| 4920 | } |
| 4921 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4922 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4923 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4924 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4925 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4926 | o_addQchr(&dest, ch); |
| 4927 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4928 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4929 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4930 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4931 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4932 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4933 | 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] | 4934 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4935 | continue; |
| 4936 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4937 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4938 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4939 | if (done_word(&dest, &ctx)) { |
| 4940 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4941 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4942 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4943 | /* Is this a case when newline is simply ignored? |
| 4944 | * Some examples: |
| 4945 | * "cmd | <newline> cmd ..." |
| 4946 | * "case ... in <newline> word) ..." |
| 4947 | */ |
| 4948 | if (IS_NULL_CMD(ctx.command) |
| 4949 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4950 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4951 | /* This newline can be ignored. But... |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4952 | * Without check #1, interactive shell |
| 4953 | * ignores even bare <newline>, |
| 4954 | * and shows the continuation prompt: |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4955 | * ps1_prompt$ <enter> |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4956 | * ps2> _ <=== wrong, should be ps1 |
| 4957 | * Without check #2, "cmd & <newline>" |
| 4958 | * is similarly mistreated. |
| 4959 | * (BTW, this makes "cmd & cmd" |
| 4960 | * and "cmd && cmd" non-orthogonal. |
| 4961 | * Really, ask yourself, why |
| 4962 | * "cmd && <newline>" doesn't start |
| 4963 | * cmd but waits for more input? |
Denys Vlasenko | b24e55d | 2017-07-16 20:29:35 +0200 | [diff] [blame] | 4964 | * The only reason is that it might be |
| 4965 | * a "cmd1 && <nl> cmd2 &" construct, |
| 4966 | * cmd1 may need to run in BG). |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4967 | */ |
| 4968 | struct pipe *pi = ctx.list_head; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4969 | if (pi->num_cmds != 0 /* check #1 */ |
| 4970 | && pi->followup != PIPE_BG /* check #2 */ |
| 4971 | ) { |
Denys Vlasenko | 642e71a | 2011-01-07 15:16:05 +0100 | [diff] [blame] | 4972 | continue; |
Denys Vlasenko | 98c46d1 | 2011-01-18 17:30:07 +0100 | [diff] [blame] | 4973 | } |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4974 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4975 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4976 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4977 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4978 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4979 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4980 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4981 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4982 | heredoc_cnt = 0; |
| 4983 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4984 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 4985 | 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] | 4986 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4987 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4988 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4989 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4990 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4991 | |
| 4992 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4993 | * } is an ordinary char in this case, even inside { cmd; } |
| 4994 | * Pathological example: { ""}; } should exec "}" cmd |
| 4995 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4996 | if (ch == '}') { |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 4997 | if (dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4998 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4999 | ) { |
| 5000 | goto ordinary_char; |
| 5001 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5002 | if (!IS_NULL_CMD(ctx.command)) { /* cmd } */ |
| 5003 | /* Generally, there should be semicolon: "cmd; }" |
| 5004 | * However, bash allows to omit it if "cmd" is |
| 5005 | * a group. Examples: |
| 5006 | * { { echo 1; } } |
| 5007 | * {(echo 1)} |
| 5008 | * { echo 0 >&2 | { echo 1; } } |
| 5009 | * { while false; do :; done } |
| 5010 | * { case a in b) ;; esac } |
| 5011 | */ |
| 5012 | if (ctx.command->group) |
| 5013 | goto term_group; |
| 5014 | goto ordinary_char; |
| 5015 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5016 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5017 | /* Can't be an end of {cmd}, skip the check */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5018 | goto skip_end_trigger; |
| 5019 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 5020 | } |
Denys Vlasenko | 672a55e | 2016-11-04 18:46:14 +0100 | [diff] [blame] | 5021 | term_group: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5022 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5023 | && (ch != ';' || heredoc_cnt == 0) |
| 5024 | #if ENABLE_HUSH_CASE |
| 5025 | && (ch != ')' |
| 5026 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5027 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5028 | ) |
| 5029 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5030 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5031 | if (heredoc_cnt) { |
| 5032 | /* This is technically valid: |
| 5033 | * { cat <<HERE; }; echo Ok |
| 5034 | * heredoc |
| 5035 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5036 | * HERE |
| 5037 | * but we don't support this. |
| 5038 | * We require heredoc to be in enclosing {}/(), |
| 5039 | * if any. |
| 5040 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 5041 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5042 | goto parse_error; |
| 5043 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5044 | if (done_word(&dest, &ctx)) { |
| 5045 | goto parse_error; |
| 5046 | } |
| 5047 | done_pipe(&ctx, PIPE_SEQ); |
| 5048 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5049 | 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] | 5050 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 5051 | if (!HAS_KEYWORDS |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 5052 | 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] | 5053 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5054 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5055 | #if !BB_MMU |
Denys Vlasenko | b5be13c | 2015-09-04 06:22:10 +0200 | [diff] [blame] | 5056 | debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5057 | if (pstring) |
| 5058 | *pstring = ctx.as_string.data; |
| 5059 | else |
| 5060 | o_free_unsafe(&ctx.as_string); |
| 5061 | #endif |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5062 | if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) { |
| 5063 | /* Example: bare "{ }", "()" */ |
| 5064 | G.last_exitcode = 2; /* bash compat */ |
| 5065 | syntax_error_unexpected_ch(ch); |
| 5066 | goto parse_error2; |
| 5067 | } |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5068 | debug_printf_parse("parse_stream return %p: " |
| 5069 | "end_trigger char found\n", |
| 5070 | ctx.list_head); |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5071 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5072 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5073 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5074 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 5075 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5076 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5077 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5078 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5079 | /* Catch <, > before deciding whether this word is |
| 5080 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 5081 | switch (ch) { |
| 5082 | case '>': |
| 5083 | redir_fd = redirect_opt_num(&dest); |
| 5084 | if (done_word(&dest, &ctx)) { |
| 5085 | goto parse_error; |
| 5086 | } |
| 5087 | redir_style = REDIRECT_OVERWRITE; |
| 5088 | if (next == '>') { |
| 5089 | redir_style = REDIRECT_APPEND; |
| 5090 | ch = i_getch(input); |
| 5091 | nommu_addchr(&ctx.as_string, ch); |
| 5092 | } |
| 5093 | #if 0 |
| 5094 | else if (next == '(') { |
| 5095 | syntax_error(">(process) not supported"); |
| 5096 | goto parse_error; |
| 5097 | } |
| 5098 | #endif |
| 5099 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5100 | goto parse_error; |
| 5101 | continue; /* back to top of while (1) */ |
| 5102 | case '<': |
| 5103 | redir_fd = redirect_opt_num(&dest); |
| 5104 | if (done_word(&dest, &ctx)) { |
| 5105 | goto parse_error; |
| 5106 | } |
| 5107 | redir_style = REDIRECT_INPUT; |
| 5108 | if (next == '<') { |
| 5109 | redir_style = REDIRECT_HEREDOC; |
| 5110 | heredoc_cnt++; |
| 5111 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 5112 | ch = i_getch(input); |
| 5113 | nommu_addchr(&ctx.as_string, ch); |
| 5114 | } else if (next == '>') { |
| 5115 | redir_style = REDIRECT_IO; |
| 5116 | ch = i_getch(input); |
| 5117 | nommu_addchr(&ctx.as_string, ch); |
| 5118 | } |
| 5119 | #if 0 |
| 5120 | else if (next == '(') { |
| 5121 | syntax_error("<(process) not supported"); |
| 5122 | goto parse_error; |
| 5123 | } |
| 5124 | #endif |
| 5125 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 5126 | goto parse_error; |
| 5127 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5128 | case '#': |
| 5129 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 5130 | /* skip "#comment" */ |
| 5131 | while (1) { |
| 5132 | ch = i_peek(input); |
| 5133 | if (ch == EOF || ch == '\n') |
| 5134 | break; |
| 5135 | i_getch(input); |
| 5136 | /* note: we do not add it to &ctx.as_string */ |
| 5137 | } |
| 5138 | nommu_addchr(&ctx.as_string, '\n'); |
| 5139 | continue; /* back to top of while (1) */ |
| 5140 | } |
| 5141 | break; |
| 5142 | case '\\': |
| 5143 | if (next == '\n') { |
| 5144 | /* It's "\<newline>" */ |
| 5145 | #if !BB_MMU |
| 5146 | /* Remove trailing '\' from ctx.as_string */ |
| 5147 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 5148 | #endif |
| 5149 | ch = i_getch(input); /* eat it */ |
| 5150 | continue; /* back to top of while (1) */ |
| 5151 | } |
| 5152 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 5156 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 5157 | && !ctx.pending_redirect |
| 5158 | ) { |
| 5159 | /* ch is a special char and thus this word |
| 5160 | * cannot be an assignment */ |
| 5161 | dest.o_assignment = NOT_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5162 | 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] | 5163 | } |
| 5164 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 5165 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 5166 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5167 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5168 | case '#': /* non-comment #: "echo a#b" etc */ |
| 5169 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5170 | break; |
| 5171 | case '\\': |
| 5172 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5173 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5174 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5175 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5176 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 5177 | /* note: ch != '\n' (that case does not reach this place) */ |
| 5178 | o_addchr(&dest, '\\'); |
| 5179 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 5180 | o_addchr(&dest, ch); |
| 5181 | nommu_addchr(&ctx.as_string, ch); |
| 5182 | /* Example: echo Hello \2>file |
| 5183 | * we need to know that word 2 is quoted */ |
| 5184 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5185 | break; |
| 5186 | case '$': |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5187 | if (!parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0)) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5188 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5189 | "parse_dollar returned 0 (error)\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5190 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5191 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5192 | break; |
| 5193 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5194 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5195 | if (next == '\'' && !ctx.pending_redirect) { |
| 5196 | insert_empty_quoted_str_marker: |
| 5197 | nommu_addchr(&ctx.as_string, next); |
| 5198 | i_getch(input); /* eat second ' */ |
| 5199 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5200 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5201 | } else { |
| 5202 | while (1) { |
| 5203 | ch = i_getch(input); |
| 5204 | if (ch == EOF) { |
| 5205 | syntax_error_unterm_ch('\''); |
| 5206 | goto parse_error; |
| 5207 | } |
| 5208 | nommu_addchr(&ctx.as_string, ch); |
| 5209 | if (ch == '\'') |
| 5210 | break; |
| 5211 | o_addqchr(&dest, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5212 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5213 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5214 | break; |
| 5215 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5216 | dest.has_quoted_part = 1; |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5217 | if (next == '"' && !ctx.pending_redirect) |
| 5218 | goto insert_empty_quoted_str_marker; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5219 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 5220 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5221 | if (!encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5222 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 5223 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5224 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5225 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5226 | case '`': { |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5227 | USE_FOR_NOMMU(unsigned pos;) |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5228 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5229 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5230 | o_addchr(&dest, '`'); |
Denys Vlasenko | 60a9414 | 2011-05-13 20:57:01 +0200 | [diff] [blame] | 5231 | USE_FOR_NOMMU(pos = dest.length;) |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5232 | if (!add_till_backquote(&dest, input, /*in_dquote:*/ 0)) |
| 5233 | goto parse_error; |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5234 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 5235 | o_addstr(&ctx.as_string, dest.data + pos); |
| 5236 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 5237 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5238 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 5239 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5240 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 5241 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5242 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5243 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5244 | #if ENABLE_HUSH_CASE |
| 5245 | case_semi: |
| 5246 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5247 | if (done_word(&dest, &ctx)) { |
| 5248 | goto parse_error; |
| 5249 | } |
| 5250 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5251 | #if ENABLE_HUSH_CASE |
| 5252 | /* Eat multiple semicolons, detect |
| 5253 | * whether it means something special */ |
| 5254 | while (1) { |
| 5255 | ch = i_peek(input); |
| 5256 | if (ch != ';') |
| 5257 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5258 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5259 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5260 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5261 | ctx.ctx_dsemicolon = 1; |
| 5262 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5263 | break; |
| 5264 | } |
| 5265 | } |
| 5266 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5267 | new_cmd: |
| 5268 | /* We just finished a cmd. New one may start |
| 5269 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5270 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denys Vlasenko | 29f9b72 | 2011-05-14 11:27:36 +0200 | [diff] [blame] | 5271 | 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] | 5272 | break; |
| 5273 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5274 | if (done_word(&dest, &ctx)) { |
| 5275 | goto parse_error; |
| 5276 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5277 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5278 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5279 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5280 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5281 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5282 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5283 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5284 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5285 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5286 | if (done_word(&dest, &ctx)) { |
| 5287 | goto parse_error; |
| 5288 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5289 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5290 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5291 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 5292 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5293 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 5294 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5295 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5296 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5297 | } else { |
| 5298 | /* we could pick up a file descriptor choice here |
| 5299 | * with redirect_opt_num(), but bash doesn't do it. |
| 5300 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5301 | done_command(&ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5302 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5303 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5304 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5305 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 5306 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5307 | if (ctx.ctx_res_w == RES_MATCH |
| 5308 | && ctx.command->argv == NULL /* not (word|(... */ |
| 5309 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 5310 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5311 | ) { |
| 5312 | continue; |
| 5313 | } |
| 5314 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5315 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5316 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 5317 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5318 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5319 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5320 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5321 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5322 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5323 | goto case_semi; |
| 5324 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5325 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5326 | /* proper use of this character is caught by end_trigger: |
| 5327 | * if we see {, we call parse_group(..., end_trigger='}') |
| 5328 | * and it will match } earlier (not here). */ |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5329 | G.last_exitcode = 2; |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5330 | syntax_error_unexpected_ch(ch); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5331 | goto parse_error2; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5332 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5333 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5334 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5335 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5336 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5337 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5338 | parse_error: |
Denys Vlasenko | b05bcaf | 2017-01-03 11:47:50 +0100 | [diff] [blame] | 5339 | G.last_exitcode = 1; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 5340 | parse_error2: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5341 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5342 | struct parse_context *pctx; |
| 5343 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5346 | * Sample for finding leaks on syntax error recovery path. |
| 5347 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5348 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 5349 | * Samples to catch leaks at execution: |
Denys Vlasenko | 5d5a611 | 2016-11-07 19:36:50 +0100 | [diff] [blame] | 5350 | * while if (true | { true;}); then echo ok; fi; do break; done |
| 5351 | * 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] | 5352 | */ |
| 5353 | pctx = &ctx; |
| 5354 | do { |
| 5355 | /* Update pipe/command counts, |
| 5356 | * otherwise freeing may miss some */ |
| 5357 | done_pipe(pctx, PIPE_SEQ); |
| 5358 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 5359 | pctx->list_head, pctx); |
| 5360 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5361 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5362 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5363 | #if !BB_MMU |
| 5364 | o_free_unsafe(&pctx->as_string); |
| 5365 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5366 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5367 | if (pctx != &ctx) { |
| 5368 | free(pctx); |
| 5369 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5370 | IF_HAS_KEYWORDS(pctx = p2;) |
| 5371 | } while (HAS_KEYWORDS && pctx); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5372 | |
Denys Vlasenko | a439fa9 | 2011-03-30 19:11:46 +0200 | [diff] [blame] | 5373 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5374 | #if !BB_MMU |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5375 | if (pstring) |
| 5376 | *pstring = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5377 | #endif |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 5378 | debug_leave(); |
| 5379 | return ERR_PTR; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5380 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5381 | } |
| 5382 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5383 | |
| 5384 | /*** Execution routines ***/ |
| 5385 | |
| 5386 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5387 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5388 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5389 | #define expand_string_to_string(str, do_unbackslash) \ |
| 5390 | expand_string_to_string(str) |
| 5391 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5392 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5393 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5394 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5395 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5396 | |
| 5397 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 5398 | * all variable references within and returns a pointer to |
| 5399 | * a list of expanded strings, possibly with larger number |
| 5400 | * of strings. (Think VAR="a b"; echo $VAR). |
| 5401 | * This new list is allocated as a single malloc block. |
| 5402 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5403 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5404 | * Caller can deallocate entire list by single free(list). */ |
| 5405 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5406 | /* A horde of its helpers come first: */ |
| 5407 | |
| 5408 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 5409 | { |
| 5410 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5411 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5412 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5413 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 5414 | if (c == '{' || c == '}') { |
| 5415 | /* { -> \{, } -> \} */ |
| 5416 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5417 | /* And now we want to add { or } and continue: |
| 5418 | * o_addchr(o, c); |
| 5419 | * continue; |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 5420 | * luckily, just falling through achieves this. |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 5421 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 5422 | } |
| 5423 | #endif |
| 5424 | o_addchr(o, c); |
| 5425 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5426 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 5427 | o_addchr(o, '\\'); |
| 5428 | if (len) { |
| 5429 | len--; |
| 5430 | o_addchr(o, '\\'); |
| 5431 | o_addchr(o, *str++); |
| 5432 | } |
| 5433 | } |
| 5434 | } |
| 5435 | } |
| 5436 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5437 | /* Store given string, finalizing the word and starting new one whenever |
| 5438 | * we encounter IFS char(s). This is used for expanding variable values. |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5439 | * End-of-string does NOT finalize word: think about 'echo -$VAR-'. |
| 5440 | * Return in *ended_with_ifs: |
| 5441 | * 1 - ended with IFS char, else 0 (this includes case of empty str). |
| 5442 | */ |
| 5443 | 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] | 5444 | { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5445 | int last_is_ifs = 0; |
| 5446 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5447 | while (1) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5448 | int word_len; |
| 5449 | |
| 5450 | if (!*str) /* EOL - do not finalize word */ |
| 5451 | break; |
| 5452 | word_len = strcspn(str, G.ifs); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5453 | if (word_len) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5454 | /* We have WORD_LEN leading non-IFS chars */ |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5455 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5456 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 5457 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5458 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 5459 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 5460 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5461 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5462 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5463 | /*/ Why can't we do it easier? */ |
| 5464 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 5465 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 5466 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5467 | last_is_ifs = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5468 | str += word_len; |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5469 | if (!*str) /* EOL - do not finalize word */ |
| 5470 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5471 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5472 | |
| 5473 | /* We know str here points to at least one IFS char */ |
| 5474 | last_is_ifs = 1; |
| 5475 | str += strspn(str, G.ifs); /* skip IFS chars */ |
| 5476 | if (!*str) /* EOL - do not finalize word */ |
| 5477 | break; |
| 5478 | |
| 5479 | /* Start new word... but not always! */ |
| 5480 | /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5481 | if (output->has_quoted_part |
| 5482 | /* Case "v=' a'; echo $v": |
| 5483 | * here nothing precedes the space in $v expansion, |
| 5484 | * therefore we should not finish the word |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5485 | * (IOW: if there *is* word to finalize, only then do it): |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5486 | */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5487 | || (n > 0 && output->data[output->length - 1]) |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5488 | ) { |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 5489 | o_addchr(output, '\0'); |
| 5490 | debug_print_list("expand_on_ifs", output, n); |
| 5491 | n = o_save_ptr(output, n); |
| 5492 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5493 | } |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5494 | |
| 5495 | if (ended_with_ifs) |
| 5496 | *ended_with_ifs = last_is_ifs; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5497 | debug_print_list("expand_on_ifs[1]", output, n); |
| 5498 | return n; |
| 5499 | } |
| 5500 | |
| 5501 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 5502 | * they are in double quotes, with the exception that they are not :). |
| 5503 | * Just the rules are similar: "expand only $var and `cmd`" |
| 5504 | * |
| 5505 | * Returns malloced string. |
| 5506 | * As an optimization, we return NULL if expansion is not needed. |
| 5507 | */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5508 | #if !BASH_PATTERN_SUBST |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5509 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 5510 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 5511 | encode_then_expand_string(str) |
| 5512 | #endif |
| 5513 | 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] | 5514 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 5515 | #if !BASH_PATTERN_SUBST |
| 5516 | const int do_unbackslash = 1; |
| 5517 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5518 | char *exp_str; |
| 5519 | struct in_str input; |
| 5520 | o_string dest = NULL_O_STRING; |
| 5521 | |
| 5522 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 5523 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5524 | #if ENABLE_HUSH_TICK |
| 5525 | && !strchr(str, '`') |
| 5526 | #endif |
| 5527 | ) { |
| 5528 | return NULL; |
| 5529 | } |
| 5530 | |
| 5531 | /* We need to expand. Example: |
| 5532 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 5533 | */ |
| 5534 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5535 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | 3eab24e | 2011-03-24 05:25:59 +0100 | [diff] [blame] | 5536 | //TODO: error check (encode_string returns 0 on error)? |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5537 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5538 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5539 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 5540 | o_free_unsafe(&dest); |
| 5541 | return exp_str; |
| 5542 | } |
| 5543 | |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5544 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5545 | 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] | 5546 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5547 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5548 | arith_t res; |
| 5549 | char *exp_str; |
| 5550 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5551 | math_state.lookupvar = get_local_var_value; |
| 5552 | math_state.setvar = set_local_var_from_halves; |
| 5553 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5554 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5555 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5556 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5557 | if (errmsg_p) |
| 5558 | *errmsg_p = math_state.errmsg; |
| 5559 | if (math_state.errmsg) |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5560 | msg_and_die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5561 | return res; |
| 5562 | } |
| 5563 | #endif |
| 5564 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5565 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5566 | /* ${var/[/]pattern[/repl]} helpers */ |
| 5567 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 5568 | { |
| 5569 | while (1) { |
| 5570 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 5571 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 5572 | if (end) { |
| 5573 | *size = end - val; |
| 5574 | return val; |
| 5575 | } |
| 5576 | if (*val == '\0') |
| 5577 | return NULL; |
| 5578 | /* Optimization: if "*pat" did not match the start of "string", |
| 5579 | * we know that "tring", "ring" etc will not match too: |
| 5580 | */ |
| 5581 | if (pattern[0] == '*') |
| 5582 | return NULL; |
| 5583 | val++; |
| 5584 | } |
| 5585 | } |
| 5586 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 5587 | { |
| 5588 | char *result = NULL; |
| 5589 | unsigned res_len = 0; |
| 5590 | unsigned repl_len = strlen(repl); |
| 5591 | |
| 5592 | while (1) { |
| 5593 | int size; |
| 5594 | char *s = strstr_pattern(val, pattern, &size); |
| 5595 | if (!s) |
| 5596 | break; |
| 5597 | |
| 5598 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5599 | strcpy(mempcpy(result + res_len, val, s - val), repl); |
| 5600 | res_len += (s - val) + repl_len; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5601 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 5602 | |
| 5603 | val = s + size; |
| 5604 | if (exp_op == '/') |
| 5605 | break; |
| 5606 | } |
Denys Vlasenko | 0675b03 | 2017-07-24 02:17:05 +0200 | [diff] [blame] | 5607 | if (*val && result) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5608 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 5609 | strcpy(result + res_len, val); |
| 5610 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 5611 | } |
| 5612 | debug_printf_varexp("result:'%s'\n", result); |
| 5613 | return result; |
| 5614 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5615 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5616 | |
| 5617 | /* Helper: |
| 5618 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 5619 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5620 | 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] | 5621 | { |
| 5622 | const char *val = NULL; |
| 5623 | char *to_be_freed = NULL; |
| 5624 | char *p = *pp; |
| 5625 | char *var; |
| 5626 | char first_char; |
| 5627 | char exp_op; |
| 5628 | char exp_save = exp_save; /* for compiler */ |
| 5629 | char *exp_saveptr; /* points to expansion operator */ |
| 5630 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5631 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5632 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5633 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5634 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5635 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5636 | arg0 = arg[0]; |
| 5637 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5638 | exp_op = 0; |
| 5639 | |
Denys Vlasenko | 2093ad2 | 2017-07-26 00:07:27 +0200 | [diff] [blame] | 5640 | if (first_char == '#' && arg[1] /* ${#...} but not ${#} */ |
| 5641 | && (!exp_saveptr /* and ( not(${#<op_char>...}) */ |
| 5642 | || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */ |
| 5643 | ) /* NB: skipping ^^^specvar check mishandles ${#::2} */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5644 | ) { |
| 5645 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5646 | var++; |
| 5647 | exp_op = 'L'; |
| 5648 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5649 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5650 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 5651 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 5652 | ) { |
| 5653 | /* ${?:0}, ${#[:]%0} etc */ |
| 5654 | exp_saveptr = var + 1; |
| 5655 | } else { |
| 5656 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 5657 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 5658 | } |
| 5659 | exp_op = exp_save = *exp_saveptr; |
| 5660 | if (exp_op) { |
| 5661 | exp_word = exp_saveptr + 1; |
| 5662 | if (exp_op == ':') { |
| 5663 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5664 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5665 | if (BASH_SUBSTR |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5666 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5667 | ) { |
| 5668 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 5669 | exp_op = ':'; |
| 5670 | exp_word--; |
| 5671 | } |
| 5672 | } |
| 5673 | *exp_saveptr = '\0'; |
| 5674 | } /* else: it's not an expansion op, but bare ${var} */ |
| 5675 | } |
| 5676 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5677 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5678 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 5679 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5680 | int n = xatoi_positive(var); |
| 5681 | if (n < G.global_argc) |
| 5682 | val = G.global_argv[n]; |
| 5683 | /* else val remains NULL: $N with too big N */ |
| 5684 | } else { |
| 5685 | switch (var[0]) { |
| 5686 | case '$': /* pid */ |
| 5687 | val = utoa(G.root_pid); |
| 5688 | break; |
| 5689 | case '!': /* bg pid */ |
| 5690 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 5691 | break; |
| 5692 | case '?': /* exitcode */ |
| 5693 | val = utoa(G.last_exitcode); |
| 5694 | break; |
| 5695 | case '#': /* argc */ |
| 5696 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 5697 | break; |
| 5698 | default: |
| 5699 | val = get_local_var_value(var); |
| 5700 | } |
| 5701 | } |
| 5702 | |
| 5703 | /* Handle any expansions */ |
| 5704 | if (exp_op == 'L') { |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5705 | reinit_unicode_for_hush(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5706 | debug_printf_expand("expand: length(%s)=", val); |
Denys Vlasenko | c538d5b | 2014-08-13 09:57:44 +0200 | [diff] [blame] | 5707 | val = utoa(val ? unicode_strlen(val) : 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5708 | debug_printf_expand("%s\n", val); |
| 5709 | } else if (exp_op) { |
| 5710 | if (exp_op == '%' || exp_op == '#') { |
| 5711 | /* Standard-mandated substring removal ops: |
| 5712 | * ${parameter%word} - remove smallest suffix pattern |
| 5713 | * ${parameter%%word} - remove largest suffix pattern |
| 5714 | * ${parameter#word} - remove smallest prefix pattern |
| 5715 | * ${parameter##word} - remove largest prefix pattern |
| 5716 | * |
| 5717 | * Word is expanded to produce a glob pattern. |
| 5718 | * Then var's value is matched to it and matching part removed. |
| 5719 | */ |
| 5720 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5721 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5722 | char *exp_exp_word; |
| 5723 | char *loc; |
| 5724 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5725 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5726 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5727 | exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5728 | if (exp_exp_word) |
| 5729 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5730 | /* HACK ALERT. We depend here on the fact that |
| 5731 | * G.global_argv and results of utoa and get_local_var_value |
| 5732 | * are actually in writable memory: |
| 5733 | * scan_and_match momentarily stores NULs there. */ |
| 5734 | t = (char*)val; |
| 5735 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5736 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5737 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5738 | free(exp_exp_word); |
| 5739 | if (loc) { /* match was found */ |
| 5740 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5741 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5742 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5743 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5744 | } |
| 5745 | } |
| 5746 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5747 | #if BASH_PATTERN_SUBST |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5748 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5749 | /* It's ${var/[/]pattern[/repl]} thing. |
| 5750 | * Note that in encoded form it has TWO parts: |
| 5751 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5752 | * and if // is used, it is encoded as \: |
| 5753 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5754 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5755 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5756 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5757 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 5758 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5759 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5760 | * by the usual expansion rules: |
| 5761 | * >az; >bz; |
| 5762 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 5763 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 5764 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 5765 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 5766 | * (note that a*z _pattern_ is never globbed!) |
| 5767 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5768 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5769 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5770 | if (!pattern) |
| 5771 | pattern = xstrdup(exp_word); |
| 5772 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 5773 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5774 | exp_word = p; |
| 5775 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5776 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5777 | 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] | 5778 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 5779 | /* HACK ALERT. We depend here on the fact that |
| 5780 | * G.global_argv and results of utoa and get_local_var_value |
| 5781 | * are actually in writable memory: |
| 5782 | * replace_pattern momentarily stores NULs there. */ |
| 5783 | t = (char*)val; |
| 5784 | to_be_freed = replace_pattern(t, |
| 5785 | pattern, |
| 5786 | (repl ? repl : exp_word), |
| 5787 | exp_op); |
| 5788 | if (to_be_freed) /* at least one replace happened */ |
| 5789 | val = to_be_freed; |
| 5790 | free(pattern); |
| 5791 | free(repl); |
| 5792 | } |
| 5793 | } |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5794 | #endif /* BASH_PATTERN_SUBST */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5795 | else if (exp_op == ':') { |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 5796 | #if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5797 | /* It's ${var:N[:M]} bashism. |
| 5798 | * Note that in encoded form it has TWO parts: |
| 5799 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 5800 | */ |
| 5801 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5802 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5803 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5804 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5805 | if (errmsg) |
| 5806 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5807 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 5808 | *p++ = SPECIAL_VAR_SYMBOL; |
| 5809 | exp_word = p; |
| 5810 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5811 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5812 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 5813 | if (errmsg) |
| 5814 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5815 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5816 | if (beg < 0) { |
| 5817 | /* negative beg counts from the end */ |
| 5818 | beg = (arith_t)strlen(val) + beg; |
| 5819 | if (beg < 0) /* ${v: -999999} is "" */ |
| 5820 | beg = len = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5821 | } |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5822 | debug_printf_varexp("from val:'%s'\n", val); |
| 5823 | if (len < 0) { |
| 5824 | /* in bash, len=-n means strlen()-n */ |
| 5825 | len = (arith_t)strlen(val) - beg + len; |
| 5826 | if (len < 0) /* bash compat */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5827 | msg_and_die_if_script("%s: substring expression < 0", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5828 | } |
Denys Vlasenko | 0ba80e4 | 2017-07-17 16:50:20 +0200 | [diff] [blame] | 5829 | if (len <= 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5830 | arith_err: |
| 5831 | val = NULL; |
| 5832 | } else { |
| 5833 | /* Paranoia. What if user entered 9999999999999 |
| 5834 | * which fits in arith_t but not int? */ |
| 5835 | if (len >= INT_MAX) |
| 5836 | len = INT_MAX; |
| 5837 | val = to_be_freed = xstrndup(val + beg, len); |
| 5838 | } |
| 5839 | debug_printf_varexp("val:'%s'\n", val); |
| 5840 | #else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5841 | msg_and_die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | e32b650 | 2017-07-17 16:46:57 +0200 | [diff] [blame] | 5842 | val = NULL; |
| 5843 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5844 | } else { /* one of "-=+?" */ |
| 5845 | /* Standard-mandated substitution ops: |
| 5846 | * ${var?word} - indicate error if unset |
| 5847 | * If var is unset, word (or a message indicating it is unset |
| 5848 | * if word is null) is written to standard error |
| 5849 | * and the shell exits with a non-zero exit status. |
| 5850 | * Otherwise, the value of var is substituted. |
| 5851 | * ${var-word} - use default value |
| 5852 | * If var is unset, word is substituted. |
| 5853 | * ${var=word} - assign and use default value |
| 5854 | * If var is unset, word is assigned to var. |
| 5855 | * In all cases, final value of var is substituted. |
| 5856 | * ${var+word} - use alternative value |
| 5857 | * If var is unset, null is substituted. |
| 5858 | * Otherwise, word is substituted. |
| 5859 | * |
| 5860 | * Word is subjected to tilde expansion, parameter expansion, |
| 5861 | * command substitution, and arithmetic expansion. |
| 5862 | * If word is not needed, it is not expanded. |
| 5863 | * |
| 5864 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 5865 | * but also treat null var as if it is unset. |
| 5866 | */ |
| 5867 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 5868 | if (exp_op == '+') |
| 5869 | use_word = !use_word; |
| 5870 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 5871 | (exp_save == ':') ? "true" : "false", use_word); |
| 5872 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5873 | 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] | 5874 | if (to_be_freed) |
| 5875 | exp_word = to_be_freed; |
| 5876 | if (exp_op == '?') { |
| 5877 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5878 | msg_and_die_if_script("%s: %s", |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5879 | var, |
Denys Vlasenko | 645c697 | 2017-07-25 15:18:57 +0200 | [diff] [blame] | 5880 | exp_word[0] |
| 5881 | ? exp_word |
| 5882 | : "parameter null or not set" |
| 5883 | /* ash has more specific messages, a-la: */ |
| 5884 | /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5885 | ); |
| 5886 | //TODO: how interactive bash aborts expansion mid-command? |
| 5887 | } else { |
| 5888 | val = exp_word; |
| 5889 | } |
| 5890 | |
| 5891 | if (exp_op == '=') { |
| 5892 | /* ${var=[word]} or ${var:=[word]} */ |
| 5893 | if (isdigit(var[0]) || var[0] == '#') { |
| 5894 | /* mimic bash message */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 5895 | msg_and_die_if_script("$%s: cannot assign in this way", var); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5896 | val = NULL; |
| 5897 | } else { |
| 5898 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 5899 | set_local_var(new_var, /*flag:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | } /* one of "-=+?" */ |
| 5904 | |
| 5905 | *exp_saveptr = exp_save; |
| 5906 | } /* if (exp_op) */ |
| 5907 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5908 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5909 | |
| 5910 | *pp = p; |
| 5911 | *to_be_freed_pp = to_be_freed; |
| 5912 | return val; |
| 5913 | } |
| 5914 | |
| 5915 | /* Expand all variable references in given string, adding words to list[] |
| 5916 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 5917 | * to be filled). This routine is extremely tricky: has to deal with |
| 5918 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5919 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5920 | 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] | 5921 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5922 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5923 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5924 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5925 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5926 | 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] | 5927 | char *p; |
| 5928 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5929 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5930 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5931 | debug_print_list("expand_vars_to_list", output, n); |
| 5932 | n = o_save_ptr(output, n); |
| 5933 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5934 | |
| 5935 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5936 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5937 | char *to_be_freed = NULL; |
| 5938 | const char *val = NULL; |
| 5939 | #if ENABLE_HUSH_TICK |
| 5940 | o_string subst_result = NULL_O_STRING; |
| 5941 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 5942 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5943 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5944 | #endif |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5945 | |
| 5946 | if (ended_in_ifs) { |
| 5947 | o_addchr(output, '\0'); |
| 5948 | n = o_save_ptr(output, n); |
| 5949 | ended_in_ifs = 0; |
| 5950 | } |
| 5951 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5952 | o_addblock(output, arg, p - arg); |
| 5953 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5954 | arg = ++p; |
| 5955 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5956 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5957 | /* Fetch special var name (if it is indeed one of them) |
| 5958 | * and quote bit, force the bit on if singleword expansion - |
| 5959 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5960 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5961 | |
| 5962 | /* Is this variable quoted and thus expansion can't be null? |
| 5963 | * "$@" is special. Even if quoted, it can still |
| 5964 | * expand to nothing (not even an empty string), |
| 5965 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5966 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5967 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5968 | |
| 5969 | switch (first_ch & 0x7f) { |
| 5970 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5971 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5972 | case '@': { |
| 5973 | int i; |
| 5974 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5975 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5976 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5977 | 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] | 5978 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5979 | while (G.global_argv[i]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 5980 | n = expand_on_ifs(NULL, output, n, G.global_argv[i]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5981 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5982 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5983 | /* this argv[] is not empty and not last: |
| 5984 | * put terminating NUL, start new word */ |
| 5985 | o_addchr(output, '\0'); |
| 5986 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5987 | n = o_save_ptr(output, n); |
| 5988 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5989 | } |
| 5990 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5991 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5992 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5993 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5994 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5995 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5996 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5997 | while (1) { |
| 5998 | o_addQstr(output, G.global_argv[i]); |
| 5999 | if (++i >= G.global_argc) |
| 6000 | break; |
| 6001 | o_addchr(output, '\0'); |
| 6002 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 6003 | n = o_save_ptr(output, n); |
| 6004 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6005 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6006 | while (1) { |
| 6007 | o_addQstr(output, G.global_argv[i]); |
| 6008 | if (!G.global_argv[++i]) |
| 6009 | break; |
| 6010 | if (G.ifs[0]) |
| 6011 | o_addchr(output, G.ifs[0]); |
| 6012 | } |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6013 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6014 | } |
| 6015 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 6016 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6017 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 6018 | /* "Empty variable", used to make "" etc to not disappear */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6019 | output->has_quoted_part = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6020 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6021 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6022 | break; |
| 6023 | #if ENABLE_HUSH_TICK |
| 6024 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6025 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6026 | arg++; |
| 6027 | /* Can't just stuff it into output o_string, |
| 6028 | * expanded result may need to be globbed |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 6029 | * and $IFS-split */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6030 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 6031 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 6032 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 6033 | val = subst_result.data; |
| 6034 | goto store_val; |
| 6035 | #endif |
Denys Vlasenko | 0b88358 | 2016-12-23 16:49:07 +0100 | [diff] [blame] | 6036 | #if ENABLE_FEATURE_SH_MATH |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6037 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 6038 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6039 | |
| 6040 | arg++; /* skip '+' */ |
| 6041 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 6042 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 6043 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 6044 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 6045 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6046 | val = arith_buf; |
| 6047 | break; |
| 6048 | } |
| 6049 | #endif |
| 6050 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6051 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6052 | IF_HUSH_TICK(store_val:) |
| 6053 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6054 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 6055 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6056 | if (val && val[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6057 | n = expand_on_ifs(&ended_in_ifs, output, n, val); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6058 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6059 | } |
| 6060 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 4fb53fb | 2011-08-01 14:06:20 +0200 | [diff] [blame] | 6061 | output->has_quoted_part = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6062 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 6063 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6064 | } |
| 6065 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6066 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 6067 | |
| 6068 | if (val && val[0]) { |
| 6069 | o_addQstr(output, val); |
| 6070 | } |
| 6071 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 6072 | |
| 6073 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 6074 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6075 | if (*p != SPECIAL_VAR_SYMBOL) |
| 6076 | *p = SPECIAL_VAR_SYMBOL; |
| 6077 | |
| 6078 | #if ENABLE_HUSH_TICK |
| 6079 | o_free(&subst_result); |
| 6080 | #endif |
| 6081 | arg = ++p; |
| 6082 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 6083 | |
| 6084 | if (arg[0]) { |
Denys Vlasenko | 6e42b89 | 2011-08-01 18:16:43 +0200 | [diff] [blame] | 6085 | if (ended_in_ifs) { |
| 6086 | o_addchr(output, '\0'); |
| 6087 | n = o_save_ptr(output, n); |
| 6088 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6089 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 6090 | /* this part is literal, and it was already pre-quoted |
| 6091 | * if needed (much earlier), do not use o_addQstr here! */ |
| 6092 | o_addstr_with_NUL(output, arg); |
| 6093 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 6094 | } 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] | 6095 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6096 | ) { |
| 6097 | n--; |
| 6098 | /* allow to reuse list[n] later without re-growth */ |
| 6099 | output->has_empty_slot = 1; |
| 6100 | } else { |
| 6101 | o_addchr(output, '\0'); |
| 6102 | } |
| 6103 | |
| 6104 | return n; |
| 6105 | } |
| 6106 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6107 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6108 | { |
| 6109 | int n; |
| 6110 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6111 | o_string output = NULL_O_STRING; |
| 6112 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6113 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6114 | |
| 6115 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6116 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 6117 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6118 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6119 | } |
| 6120 | debug_print_list("expand_variables", &output, n); |
| 6121 | |
| 6122 | /* output.data (malloced in one block) gets returned in "list" */ |
| 6123 | list = o_finalize_list(&output, n); |
| 6124 | debug_print_strings("expand_variables[1]", list); |
| 6125 | return list; |
| 6126 | } |
| 6127 | |
| 6128 | static char **expand_strvec_to_strvec(char **argv) |
| 6129 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6130 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6131 | } |
| 6132 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 6133 | #if BASH_TEST2 |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6134 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 6135 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6136 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6137 | } |
| 6138 | #endif |
| 6139 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6140 | /* Used for expansion of right hand of assignments, |
| 6141 | * $((...)), heredocs, variable espansion parts. |
| 6142 | * |
| 6143 | * NB: should NOT do globbing! |
| 6144 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 6145 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6146 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6147 | { |
Denys Vlasenko | 637982f | 2017-07-06 01:52:23 +0200 | [diff] [blame] | 6148 | #if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6149 | const int do_unbackslash = 1; |
| 6150 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6151 | char *argv[2], **list; |
| 6152 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6153 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6154 | /* This is generally an optimization, but it also |
| 6155 | * handles "", which otherwise trips over !list[0] check below. |
| 6156 | * (is this ever happens that we actually get str="" here?) |
| 6157 | */ |
| 6158 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 6159 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6160 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6161 | return xstrdup(str); |
| 6162 | } |
| 6163 | |
| 6164 | argv[0] = (char*)str; |
| 6165 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6166 | list = expand_variables(argv, do_unbackslash |
| 6167 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 6168 | : EXP_FLAG_SINGLEWORD |
| 6169 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6170 | if (HUSH_DEBUG) |
| 6171 | if (!list[0] || list[1]) |
| 6172 | bb_error_msg_and_die("BUG in varexp2"); |
| 6173 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 6174 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6175 | if (do_unbackslash) |
| 6176 | unbackslash((char*)list); |
| 6177 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6178 | return (char*)list; |
| 6179 | } |
| 6180 | |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 6181 | /* Used for "eval" builtin and case string */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6182 | static char* expand_strvec_to_string(char **argv) |
| 6183 | { |
| 6184 | char **list; |
| 6185 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 6186 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6187 | /* Convert all NULs to spaces */ |
| 6188 | if (list[0]) { |
| 6189 | int n = 1; |
| 6190 | while (list[n]) { |
| 6191 | if (HUSH_DEBUG) |
| 6192 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 6193 | bb_error_msg_and_die("BUG in varexp3"); |
| 6194 | /* bash uses ' ' regardless of $IFS contents */ |
| 6195 | list[n][-1] = ' '; |
| 6196 | n++; |
| 6197 | } |
| 6198 | } |
Denys Vlasenko | 78c9c73 | 2016-09-29 01:44:17 +0200 | [diff] [blame] | 6199 | overlapping_strcpy((char*)list, list[0] ? list[0] : ""); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6200 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 6201 | return (char*)list; |
| 6202 | } |
| 6203 | |
| 6204 | static char **expand_assignments(char **argv, int count) |
| 6205 | { |
| 6206 | int i; |
| 6207 | char **p; |
| 6208 | |
| 6209 | G.expanded_assignments = p = NULL; |
| 6210 | /* Expand assignments into one string each */ |
| 6211 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6212 | 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] | 6213 | } |
| 6214 | G.expanded_assignments = NULL; |
| 6215 | return p; |
| 6216 | } |
| 6217 | |
| 6218 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6219 | static void switch_off_special_sigs(unsigned mask) |
| 6220 | { |
| 6221 | unsigned sig = 0; |
| 6222 | while ((mask >>= 1) != 0) { |
| 6223 | sig++; |
| 6224 | if (!(mask & 1)) |
| 6225 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6226 | #if ENABLE_HUSH_TRAP |
| 6227 | if (G_traps) { |
| 6228 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6229 | /* trap is '', has to remain SIG_IGN */ |
| 6230 | continue; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6231 | free(G_traps[sig]); |
| 6232 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6233 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6234 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6235 | /* We are here only if no trap or trap was not '' */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6236 | install_sighandler(sig, SIG_DFL); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
Denys Vlasenko | b347df9 | 2011-08-09 22:49:15 +0200 | [diff] [blame] | 6240 | #if BB_MMU |
| 6241 | /* never called */ |
| 6242 | void re_execute_shell(char ***to_free, const char *s, |
| 6243 | char *g_argv0, char **g_argv, |
| 6244 | char **builtin_argv) NORETURN; |
| 6245 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6246 | static void reset_traps_to_defaults(void) |
| 6247 | { |
| 6248 | /* This function is always called in a child shell |
| 6249 | * after fork (not vfork, NOMMU doesn't use this function). |
| 6250 | */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6251 | IF_HUSH_TRAP(unsigned sig;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6252 | unsigned mask; |
| 6253 | |
| 6254 | /* Child shells are not interactive. |
| 6255 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 6256 | * Testcase: (while :; do :; done) + ^Z should background. |
| 6257 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 6258 | */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6259 | mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6260 | if (!G_traps && !mask) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6261 | return; /* already no traps and no special sigs */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6262 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6263 | /* Switch off special sigs */ |
| 6264 | switch_off_special_sigs(mask); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6265 | # if ENABLE_HUSH_JOB |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6266 | G_fatal_sig_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6267 | # endif |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 6268 | G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 6269 | /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS |
| 6270 | * remain set in G.special_sig_mask */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6271 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6272 | # if ENABLE_HUSH_TRAP |
| 6273 | if (!G_traps) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6274 | return; |
| 6275 | |
| 6276 | /* Reset all sigs to default except ones with empty traps */ |
| 6277 | for (sig = 0; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6278 | if (!G_traps[sig]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6279 | continue; /* no trap: nothing to do */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6280 | if (!G_traps[sig][0]) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6281 | continue; /* empty trap: has to remain SIG_IGN */ |
| 6282 | /* sig has non-empty trap, reset it: */ |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6283 | free(G_traps[sig]); |
| 6284 | G_traps[sig] = NULL; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 6285 | /* There is no signal for trap 0 (EXIT) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6286 | if (sig == 0) |
| 6287 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 6288 | install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6289 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6290 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6291 | } |
| 6292 | |
| 6293 | #else /* !BB_MMU */ |
| 6294 | |
| 6295 | static void re_execute_shell(char ***to_free, const char *s, |
| 6296 | char *g_argv0, char **g_argv, |
| 6297 | char **builtin_argv) NORETURN; |
| 6298 | static void re_execute_shell(char ***to_free, const char *s, |
| 6299 | char *g_argv0, char **g_argv, |
| 6300 | char **builtin_argv) |
| 6301 | { |
| 6302 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 6303 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 6304 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 6305 | char *heredoc_argv[4]; |
| 6306 | struct variable *cur; |
| 6307 | # if ENABLE_HUSH_FUNCTIONS |
| 6308 | struct function *funcp; |
| 6309 | # endif |
| 6310 | char **argv, **pp; |
| 6311 | unsigned cnt; |
| 6312 | unsigned long long empty_trap_mask; |
| 6313 | |
| 6314 | if (!g_argv0) { /* heredoc */ |
| 6315 | argv = heredoc_argv; |
| 6316 | argv[0] = (char *) G.argv0_for_re_execing; |
| 6317 | argv[1] = (char *) "-<"; |
| 6318 | argv[2] = (char *) s; |
| 6319 | argv[3] = NULL; |
| 6320 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 6321 | goto do_exec; |
| 6322 | } |
| 6323 | |
| 6324 | cnt = 0; |
| 6325 | pp = builtin_argv; |
| 6326 | if (pp) while (*pp++) |
| 6327 | cnt++; |
| 6328 | |
| 6329 | empty_trap_mask = 0; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6330 | if (G_traps) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6331 | int sig; |
| 6332 | for (sig = 1; sig < NSIG; sig++) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6333 | if (G_traps[sig] && !G_traps[sig][0]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6334 | empty_trap_mask |= 1LL << sig; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | sprintf(param_buf, NOMMU_HACK_FMT |
| 6339 | , (unsigned) G.root_pid |
| 6340 | , (unsigned) G.root_ppid |
| 6341 | , (unsigned) G.last_bg_pid |
| 6342 | , (unsigned) G.last_exitcode |
| 6343 | , cnt |
| 6344 | , empty_trap_mask |
| 6345 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 6346 | ); |
| 6347 | # undef NOMMU_HACK_FMT |
| 6348 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 6349 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 6350 | */ |
| 6351 | cnt += 6; |
| 6352 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6353 | if (!cur->flg_export || cur->flg_read_only) |
| 6354 | cnt += 2; |
| 6355 | } |
| 6356 | # if ENABLE_HUSH_FUNCTIONS |
| 6357 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 6358 | cnt += 3; |
| 6359 | # endif |
| 6360 | pp = g_argv; |
| 6361 | while (*pp++) |
| 6362 | cnt++; |
| 6363 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 6364 | *pp++ = (char *) G.argv0_for_re_execing; |
| 6365 | *pp++ = param_buf; |
| 6366 | for (cur = G.top_var; cur; cur = cur->next) { |
| 6367 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 6368 | continue; |
| 6369 | if (cur->flg_read_only) { |
| 6370 | *pp++ = (char *) "-R"; |
| 6371 | *pp++ = cur->varstr; |
| 6372 | } else if (!cur->flg_export) { |
| 6373 | *pp++ = (char *) "-V"; |
| 6374 | *pp++ = cur->varstr; |
| 6375 | } |
| 6376 | } |
| 6377 | # if ENABLE_HUSH_FUNCTIONS |
| 6378 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 6379 | *pp++ = (char *) "-F"; |
| 6380 | *pp++ = funcp->name; |
| 6381 | *pp++ = funcp->body_as_string; |
| 6382 | } |
| 6383 | # endif |
| 6384 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 6385 | * |
| 6386 | * However, POSIX says that subshells reset signals with traps |
| 6387 | * to SIG_DFL. |
| 6388 | * I tested bash-3.2 and it not only does that with true subshells |
| 6389 | * of the form ( list ), but with any forked children shells. |
| 6390 | * I set trap "echo W" WINCH; and then tried: |
| 6391 | * |
| 6392 | * { echo 1; sleep 20; echo 2; } & |
| 6393 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 6394 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 6395 | * |
| 6396 | * In all these cases sending SIGWINCH to the child shell |
| 6397 | * did not run the trap. If I add trap "echo V" WINCH; |
| 6398 | * _inside_ group (just before echo 1), it works. |
| 6399 | * |
| 6400 | * 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] | 6401 | */ |
| 6402 | *pp++ = (char *) "-c"; |
| 6403 | *pp++ = (char *) s; |
| 6404 | if (builtin_argv) { |
| 6405 | while (*++builtin_argv) |
| 6406 | *pp++ = *builtin_argv; |
| 6407 | *pp++ = (char *) ""; |
| 6408 | } |
| 6409 | *pp++ = g_argv0; |
| 6410 | while (*g_argv) |
| 6411 | *pp++ = *g_argv++; |
| 6412 | /* *pp = NULL; - is already there */ |
| 6413 | pp = environ; |
| 6414 | |
| 6415 | do_exec: |
| 6416 | 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] | 6417 | /* Don't propagate SIG_IGN to the child */ |
| 6418 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 6419 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6420 | execve(bb_busybox_exec_path, argv, pp); |
| 6421 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 6422 | if (argv[0][0] == '/') |
| 6423 | execve(argv[0], argv, pp); |
| 6424 | xfunc_error_retval = 127; |
| 6425 | bb_error_msg_and_die("can't re-execute the shell"); |
| 6426 | } |
| 6427 | #endif /* !BB_MMU */ |
| 6428 | |
| 6429 | |
| 6430 | static int run_and_free_list(struct pipe *pi); |
| 6431 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6432 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6433 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6434 | * end_trigger controls how often we stop parsing |
| 6435 | * NUL: parse all, execute, return |
| 6436 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6437 | */ |
| 6438 | 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] | 6439 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6440 | /* Why we need empty flag? |
| 6441 | * An obscure corner case "false; ``; echo $?": |
| 6442 | * empty command in `` should still set $? to 0. |
| 6443 | * But we can't just set $? to 0 at the start, |
| 6444 | * this breaks "false; echo `echo $?`" case. |
| 6445 | */ |
| 6446 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6447 | while (1) { |
| 6448 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6449 | |
Denys Vlasenko | a146319 | 2011-01-18 17:55:04 +0100 | [diff] [blame] | 6450 | #if ENABLE_HUSH_INTERACTIVE |
| 6451 | if (end_trigger == ';') |
| 6452 | inp->promptmode = 0; /* PS1 */ |
| 6453 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6454 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | cecbc98 | 2011-03-30 18:54:52 +0200 | [diff] [blame] | 6455 | if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */ |
| 6456 | /* If we are in "big" script |
| 6457 | * (not in `cmd` or something similar)... |
| 6458 | */ |
| 6459 | if (pipe_list == ERR_PTR && end_trigger == ';') { |
| 6460 | /* Discard cached input (rest of line) */ |
| 6461 | int ch = inp->last_char; |
| 6462 | while (ch != EOF && ch != '\n') { |
| 6463 | //bb_error_msg("Discarded:'%c'", ch); |
| 6464 | ch = i_getch(inp); |
| 6465 | } |
| 6466 | /* Force prompt */ |
| 6467 | inp->p = NULL; |
| 6468 | /* This stream isn't empty */ |
| 6469 | empty = 0; |
| 6470 | continue; |
| 6471 | } |
| 6472 | if (!pipe_list && empty) |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6473 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6474 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6475 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6476 | debug_print_tree(pipe_list, 0); |
| 6477 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 6478 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6479 | empty = 0; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 6480 | if (G_flag_return_in_progress == 1) |
Denys Vlasenko | 68d5cb5 | 2011-03-24 02:50:03 +0100 | [diff] [blame] | 6481 | break; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6482 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6485 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6486 | { |
| 6487 | struct in_str input; |
| 6488 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6489 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6490 | } |
| 6491 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6492 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6493 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6494 | struct in_str input; |
| 6495 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6496 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6499 | #if ENABLE_HUSH_TICK |
| 6500 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 6501 | { |
| 6502 | pid_t pid; |
| 6503 | int channel[2]; |
| 6504 | # if !BB_MMU |
| 6505 | char **to_free = NULL; |
| 6506 | # endif |
| 6507 | |
| 6508 | xpipe(channel); |
| 6509 | pid = BB_MMU ? xfork() : xvfork(); |
| 6510 | if (pid == 0) { /* child */ |
| 6511 | disable_restore_tty_pgrp_on_exit(); |
| 6512 | /* Process substitution is not considered to be usual |
| 6513 | * 'command execution'. |
| 6514 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 6515 | */ |
| 6516 | bb_signals(0 |
| 6517 | + (1 << SIGTSTP) |
| 6518 | + (1 << SIGTTIN) |
| 6519 | + (1 << SIGTTOU) |
| 6520 | , SIG_IGN); |
| 6521 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6522 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 6523 | xmove_fd(channel[1], 1); |
| 6524 | /* Prevent it from trying to handle ctrl-z etc */ |
| 6525 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6526 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6527 | /* Awful hack for `trap` or $(trap). |
| 6528 | * |
| 6529 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 6530 | * contains an example where "trap" is executed in a subshell: |
| 6531 | * |
| 6532 | * save_traps=$(trap) |
| 6533 | * ... |
| 6534 | * eval "$save_traps" |
| 6535 | * |
| 6536 | * Standard does not say that "trap" in subshell shall print |
| 6537 | * parent shell's traps. It only says that its output |
| 6538 | * must have suitable form, but then, in the above example |
| 6539 | * (which is not supposed to be normative), it implies that. |
| 6540 | * |
| 6541 | * bash (and probably other shell) does implement it |
| 6542 | * (traps are reset to defaults, but "trap" still shows them), |
| 6543 | * but as a result, "trap" logic is hopelessly messed up: |
| 6544 | * |
| 6545 | * # trap |
| 6546 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 6547 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 6548 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 6549 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 6550 | * trap -- 'echo Ho' SIGWINCH |
| 6551 | * # echo `(trap)` <--- in subshell in subshell - output |
| 6552 | * trap -- 'echo Ho' SIGWINCH |
| 6553 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 6554 | * trap -- 'echo Ho' SIGWINCH |
| 6555 | * |
| 6556 | * The rules when to forget and when to not forget traps |
| 6557 | * get really complex and nonsensical. |
| 6558 | * |
| 6559 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 6560 | */ |
| 6561 | s = skip_whitespace(s); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 6562 | if (is_prefixed_with(s, "trap") |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6563 | && skip_whitespace(s + 4)[0] == '\0' |
| 6564 | ) { |
| 6565 | static const char *const argv[] = { NULL, NULL }; |
| 6566 | builtin_trap((char**)argv); |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 6567 | fflush_all(); /* important */ |
| 6568 | _exit(0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6569 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 6570 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6571 | # if BB_MMU |
| 6572 | reset_traps_to_defaults(); |
| 6573 | parse_and_run_string(s); |
| 6574 | _exit(G.last_exitcode); |
| 6575 | # else |
| 6576 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 6577 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 6578 | * huge=`cat BIG` # was blocking here forever |
| 6579 | * echo OK |
| 6580 | */ |
| 6581 | re_execute_shell(&to_free, |
| 6582 | s, |
| 6583 | G.global_argv[0], |
| 6584 | G.global_argv + 1, |
| 6585 | NULL); |
| 6586 | # endif |
| 6587 | } |
| 6588 | |
| 6589 | /* parent */ |
| 6590 | *pid_p = pid; |
| 6591 | # if ENABLE_HUSH_FAST |
| 6592 | G.count_SIGCHLD++; |
| 6593 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 6594 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 6595 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6596 | # endif |
| 6597 | enable_restore_tty_pgrp_on_exit(); |
| 6598 | # if !BB_MMU |
| 6599 | free(to_free); |
| 6600 | # endif |
| 6601 | close(channel[1]); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6602 | return remember_FILE(xfdopen_for_read(channel[0])); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6603 | } |
| 6604 | |
| 6605 | /* Return code is exit status of the process that is run. */ |
| 6606 | static int process_command_subs(o_string *dest, const char *s) |
| 6607 | { |
| 6608 | FILE *fp; |
| 6609 | struct in_str pipe_str; |
| 6610 | pid_t pid; |
| 6611 | int status, ch, eol_cnt; |
| 6612 | |
| 6613 | fp = generate_stream_from_string(s, &pid); |
| 6614 | |
| 6615 | /* Now send results of command back into original context */ |
| 6616 | setup_file_in_str(&pipe_str, fp); |
| 6617 | eol_cnt = 0; |
| 6618 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 6619 | if (ch == '\n') { |
| 6620 | eol_cnt++; |
| 6621 | continue; |
| 6622 | } |
| 6623 | while (eol_cnt) { |
| 6624 | o_addchr(dest, '\n'); |
| 6625 | eol_cnt--; |
| 6626 | } |
| 6627 | o_addQchr(dest, ch); |
| 6628 | } |
| 6629 | |
| 6630 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 6631 | fclose_and_forget(fp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6632 | /* We need to extract exitcode. Test case |
| 6633 | * "true; echo `sleep 1; false` $?" |
| 6634 | * should print 1 */ |
| 6635 | safe_waitpid(pid, &status, 0); |
| 6636 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 6637 | return WEXITSTATUS(status); |
| 6638 | } |
| 6639 | #endif /* ENABLE_HUSH_TICK */ |
| 6640 | |
| 6641 | |
| 6642 | static void setup_heredoc(struct redir_struct *redir) |
| 6643 | { |
| 6644 | struct fd_pair pair; |
| 6645 | pid_t pid; |
| 6646 | int len, written; |
| 6647 | /* the _body_ of heredoc (misleading field name) */ |
| 6648 | const char *heredoc = redir->rd_filename; |
| 6649 | char *expanded; |
| 6650 | #if !BB_MMU |
| 6651 | char **to_free; |
| 6652 | #endif |
| 6653 | |
| 6654 | expanded = NULL; |
| 6655 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 6656 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6657 | if (expanded) |
| 6658 | heredoc = expanded; |
| 6659 | } |
| 6660 | len = strlen(heredoc); |
| 6661 | |
| 6662 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 6663 | xpiped_pair(pair); |
| 6664 | xmove_fd(pair.rd, redir->rd_fd); |
| 6665 | |
| 6666 | /* Try writing without forking. Newer kernels have |
| 6667 | * dynamically growing pipes. Must use non-blocking write! */ |
| 6668 | ndelay_on(pair.wr); |
| 6669 | while (1) { |
| 6670 | written = write(pair.wr, heredoc, len); |
| 6671 | if (written <= 0) |
| 6672 | break; |
| 6673 | len -= written; |
| 6674 | if (len == 0) { |
| 6675 | close(pair.wr); |
| 6676 | free(expanded); |
| 6677 | return; |
| 6678 | } |
| 6679 | heredoc += written; |
| 6680 | } |
| 6681 | ndelay_off(pair.wr); |
| 6682 | |
| 6683 | /* Okay, pipe buffer was not big enough */ |
| 6684 | /* Note: we must not create a stray child (bastard? :) |
| 6685 | * for the unsuspecting parent process. Child creates a grandchild |
| 6686 | * and exits before parent execs the process which consumes heredoc |
| 6687 | * (that exec happens after we return from this function) */ |
| 6688 | #if !BB_MMU |
| 6689 | to_free = NULL; |
| 6690 | #endif |
| 6691 | pid = xvfork(); |
| 6692 | if (pid == 0) { |
| 6693 | /* child */ |
| 6694 | disable_restore_tty_pgrp_on_exit(); |
| 6695 | pid = BB_MMU ? xfork() : xvfork(); |
| 6696 | if (pid != 0) |
| 6697 | _exit(0); |
| 6698 | /* grandchild */ |
| 6699 | close(redir->rd_fd); /* read side of the pipe */ |
| 6700 | #if BB_MMU |
| 6701 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 6702 | _exit(0); |
| 6703 | #else |
| 6704 | /* Delegate blocking writes to another process */ |
| 6705 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 6706 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 6707 | #endif |
| 6708 | } |
| 6709 | /* parent */ |
| 6710 | #if ENABLE_HUSH_FAST |
| 6711 | G.count_SIGCHLD++; |
| 6712 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6713 | #endif |
| 6714 | enable_restore_tty_pgrp_on_exit(); |
| 6715 | #if !BB_MMU |
| 6716 | free(to_free); |
| 6717 | #endif |
| 6718 | close(pair.wr); |
| 6719 | free(expanded); |
| 6720 | wait(NULL); /* wait till child has died */ |
| 6721 | } |
| 6722 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6723 | struct squirrel { |
| 6724 | int orig_fd; |
| 6725 | int moved_to; |
| 6726 | /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */ |
| 6727 | /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */ |
| 6728 | }; |
| 6729 | |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6730 | static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved) |
| 6731 | { |
| 6732 | sq = xrealloc(sq, (i + 2) * sizeof(sq[0])); |
| 6733 | sq[i].orig_fd = orig; |
| 6734 | sq[i].moved_to = moved; |
| 6735 | sq[i+1].orig_fd = -1; /* end marker */ |
| 6736 | return sq; |
| 6737 | } |
| 6738 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6739 | static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd) |
| 6740 | { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6741 | int moved_to; |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6742 | int i; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6743 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 6744 | i = 0; |
| 6745 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6746 | /* If we collide with an already moved fd... */ |
| 6747 | if (fd == sq[i].moved_to) { |
| 6748 | sq[i].moved_to = fcntl_F_DUPFD(sq[i].moved_to, avoid_fd); |
| 6749 | debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, sq[i].moved_to); |
| 6750 | if (sq[i].moved_to < 0) /* what? */ |
| 6751 | xfunc_die(); |
| 6752 | return sq; |
| 6753 | } |
| 6754 | if (fd == sq[i].orig_fd) { |
| 6755 | /* Example: echo Hello >/dev/null 1>&2 */ |
| 6756 | debug_printf_redir("redirect_fd %d: already moved\n", fd); |
| 6757 | return sq; |
| 6758 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6759 | } |
| 6760 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6761 | /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */ |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6762 | moved_to = fcntl_F_DUPFD(fd, avoid_fd); |
| 6763 | debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to); |
| 6764 | if (moved_to < 0 && errno != EBADF) |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6765 | xfunc_die(); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6766 | return append_squirrel(sq, i, fd, moved_to); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6767 | } |
| 6768 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6769 | static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd) |
| 6770 | { |
| 6771 | int i; |
| 6772 | |
Denys Vlasenko | d16e612 | 2017-08-11 15:41:39 +0200 | [diff] [blame] | 6773 | i = 0; |
| 6774 | if (sq) for (; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6775 | /* If we collide with an already moved fd... */ |
| 6776 | if (fd == sq[i].orig_fd) { |
| 6777 | /* Examples: |
| 6778 | * "echo 3>FILE 3>&- 3>FILE" |
| 6779 | * "echo 3>&- 3>FILE" |
| 6780 | * No need for last redirect to insert |
| 6781 | * another "need to close 3" indicator. |
| 6782 | */ |
| 6783 | debug_printf_redir("redirect_fd %d: already moved or closed\n", fd); |
| 6784 | return sq; |
| 6785 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd); |
| 6789 | return append_squirrel(sq, i, fd, -1); |
| 6790 | } |
| 6791 | |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6792 | /* fd: redirect wants this fd to be used (e.g. 3>file). |
| 6793 | * Move all conflicting internally used fds, |
| 6794 | * and remember them so that we can restore them later. |
| 6795 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6796 | 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] | 6797 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6798 | if (avoid_fd < 9) /* the important case here is that it can be -1 */ |
| 6799 | avoid_fd = 9; |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6800 | |
| 6801 | #if ENABLE_HUSH_INTERACTIVE |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6802 | if (fd == G.interactive_fd) { |
| 6803 | /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6804 | G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6805 | debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); |
| 6806 | return 1; /* "we closed fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6807 | } |
| 6808 | #endif |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6809 | /* Are we called from setup_redirects(squirrel==NULL)? Two cases: |
| 6810 | * (1) Redirect in a forked child. No need to save FILEs' fds, |
| 6811 | * we aren't going to use them anymore, ok to trash. |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6812 | * (2) "exec 3>FILE". Bummer. We can save script FILEs' fds, |
| 6813 | * but how are we doing to restore them? |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6814 | * "fileno(fd) = new_fd" can't be done. |
| 6815 | */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6816 | if (!sqp) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6817 | return 0; |
| 6818 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6819 | /* If this one of script's fds? */ |
| 6820 | if (save_FILEs_on_redirect(fd, avoid_fd)) |
| 6821 | return 1; /* yes. "we closed fd" */ |
| 6822 | |
| 6823 | /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */ |
| 6824 | *sqp = add_squirrel(*sqp, fd, avoid_fd); |
| 6825 | return 0; /* "we did not close fd" */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6826 | } |
| 6827 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6828 | static void restore_redirects(struct squirrel *sq) |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6829 | { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6830 | if (sq) { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6831 | int i; |
| 6832 | for (i = 0; sq[i].orig_fd >= 0; i++) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6833 | if (sq[i].moved_to >= 0) { |
| 6834 | /* We simply die on error */ |
| 6835 | debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd); |
| 6836 | xmove_fd(sq[i].moved_to, sq[i].orig_fd); |
| 6837 | } else { |
| 6838 | /* cmd1 9>FILE; cmd2_should_see_fd9_closed */ |
| 6839 | debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd); |
| 6840 | close(sq[i].orig_fd); |
| 6841 | } |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6842 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6843 | free(sq); |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6844 | } |
| 6845 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6846 | /* If moved, G.interactive_fd stays on new fd, not restoring it */ |
Denys Vlasenko | aa3576a | 2016-08-22 19:54:12 +0200 | [diff] [blame] | 6847 | |
| 6848 | restore_redirected_FILEs(); |
| 6849 | } |
| 6850 | |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 6851 | #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 6852 | static void close_saved_fds_and_FILE_fds(void) |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 6853 | { |
| 6854 | if (G_interactive_fd) |
| 6855 | close(G_interactive_fd); |
| 6856 | close_all_FILE_list(); |
| 6857 | } |
| 6858 | #endif |
| 6859 | |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6860 | static int internally_opened_fd(int fd, struct squirrel *sq) |
| 6861 | { |
| 6862 | int i; |
| 6863 | |
| 6864 | #if ENABLE_HUSH_INTERACTIVE |
| 6865 | if (fd == G.interactive_fd) |
| 6866 | return 1; |
| 6867 | #endif |
| 6868 | /* If this one of script's fds? */ |
| 6869 | if (fd_in_FILEs(fd)) |
| 6870 | return 1; |
| 6871 | |
| 6872 | if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) { |
| 6873 | if (fd == sq[i].moved_to) |
| 6874 | return 1; |
| 6875 | } |
| 6876 | return 0; |
| 6877 | } |
| 6878 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6879 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 6880 | * and stderr if they are redirected. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 6881 | static int setup_redirects(struct command *prog, struct squirrel **sqp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6882 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6883 | struct redir_struct *redir; |
| 6884 | |
| 6885 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6886 | int newfd; |
| 6887 | int closed; |
| 6888 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6889 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6890 | /* "rd_fd<<HERE" case */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6891 | save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6892 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 6893 | * of the heredoc */ |
| 6894 | debug_printf_parse("set heredoc '%s'\n", |
| 6895 | redir->rd_filename); |
| 6896 | setup_heredoc(redir); |
| 6897 | continue; |
| 6898 | } |
| 6899 | |
| 6900 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6901 | /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6902 | char *p; |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6903 | int mode; |
| 6904 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6905 | if (redir->rd_filename == NULL) { |
Denys Vlasenko | d6a37d8 | 2016-09-20 16:22:24 +0200 | [diff] [blame] | 6906 | /* |
| 6907 | * Examples: |
| 6908 | * "cmd >" (no filename) |
| 6909 | * "cmd > <file" (2nd redirect starts too early) |
| 6910 | */ |
Denys Vlasenko | 3970120 | 2017-08-02 19:44:05 +0200 | [diff] [blame] | 6911 | syntax_error("invalid redirect"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6912 | continue; |
| 6913 | } |
| 6914 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6915 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6916 | newfd = open_or_warn(p, mode); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6917 | free(p); |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6918 | if (newfd < 0) { |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 6919 | /* Error message from open_or_warn can be lost |
| 6920 | * if stderr has been redirected, but bash |
| 6921 | * and ash both lose it as well |
| 6922 | * (though zsh doesn't!) |
| 6923 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6924 | return 1; |
| 6925 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6926 | if (newfd == redir->rd_fd && sqp) { |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6927 | /* open() gave us precisely the fd we wanted. |
| 6928 | * This means that this fd was not busy |
| 6929 | * (not opened to anywhere). |
| 6930 | * Remember to close it on restore: |
| 6931 | */ |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6932 | *sqp = add_squirrel_closed(*sqp, newfd); |
| 6933 | debug_printf_redir("redir to previously closed fd %d\n", newfd); |
Denys Vlasenko | 621fc50 | 2017-07-24 12:42:17 +0200 | [diff] [blame] | 6934 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6935 | } else { |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6936 | /* "rd_fd>&rd_dup" or "rd_fd>&-" case */ |
| 6937 | newfd = redir->rd_dup; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6938 | } |
| 6939 | |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6940 | if (newfd == redir->rd_fd) |
| 6941 | continue; |
| 6942 | |
| 6943 | /* if "N>FILE": move newfd to redir->rd_fd */ |
| 6944 | /* if "N>&M": dup newfd to redir->rd_fd */ |
| 6945 | /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */ |
| 6946 | |
| 6947 | closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp); |
| 6948 | if (newfd == REDIRFD_CLOSE) { |
| 6949 | /* "N>&-" means "close me" */ |
| 6950 | if (!closed) { |
| 6951 | /* ^^^ optimization: saving may already |
| 6952 | * have closed it. If not... */ |
| 6953 | close(redir->rd_fd); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6954 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6955 | /* Sometimes we do another close on restore, getting EBADF. |
| 6956 | * Consider "echo 3>FILE 3>&-" |
| 6957 | * first redirect remembers "need to close 3", |
| 6958 | * and second redirect closes 3! Restore code then closes 3 again. |
| 6959 | */ |
| 6960 | } else { |
Denys Vlasenko | 32fdf2f | 2017-07-31 04:32:06 +0200 | [diff] [blame] | 6961 | /* if newfd is a script fd or saved fd, simulate EBADF */ |
| 6962 | if (internally_opened_fd(newfd, sqp ? *sqp : NULL)) { |
| 6963 | //errno = EBADF; |
| 6964 | //bb_perror_msg_and_die("can't duplicate file descriptor"); |
| 6965 | newfd = -1; /* same effect as code above */ |
| 6966 | } |
Denys Vlasenko | 657e900 | 2017-07-30 23:34:04 +0200 | [diff] [blame] | 6967 | xdup2(newfd, redir->rd_fd); |
| 6968 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 6969 | /* "rd_fd > FILE" */ |
| 6970 | close(newfd); |
| 6971 | /* else: "rd_fd > rd_dup" */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6972 | } |
| 6973 | } |
| 6974 | return 0; |
| 6975 | } |
| 6976 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6977 | static char *find_in_path(const char *arg) |
| 6978 | { |
| 6979 | char *ret = NULL; |
| 6980 | const char *PATH = get_local_var_value("PATH"); |
| 6981 | |
| 6982 | if (!PATH) |
| 6983 | return NULL; |
| 6984 | |
| 6985 | while (1) { |
| 6986 | const char *end = strchrnul(PATH, ':'); |
| 6987 | int sz = end - PATH; /* must be int! */ |
| 6988 | |
| 6989 | free(ret); |
| 6990 | if (sz != 0) { |
| 6991 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 6992 | } else { |
| 6993 | /* We have xxx::yyyy in $PATH, |
| 6994 | * it means "use current dir" */ |
| 6995 | ret = xstrdup(arg); |
| 6996 | } |
| 6997 | if (access(ret, F_OK) == 0) |
| 6998 | break; |
| 6999 | |
| 7000 | if (*end == '\0') { |
| 7001 | free(ret); |
| 7002 | return NULL; |
| 7003 | } |
| 7004 | PATH = end + 1; |
| 7005 | } |
| 7006 | |
| 7007 | return ret; |
| 7008 | } |
| 7009 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7010 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7011 | const struct built_in_command *x, |
| 7012 | const struct built_in_command *end) |
| 7013 | { |
| 7014 | while (x != end) { |
| 7015 | if (strcmp(name, x->b_cmd) != 0) { |
| 7016 | x++; |
| 7017 | continue; |
| 7018 | } |
| 7019 | debug_printf_exec("found builtin '%s'\n", name); |
| 7020 | return x; |
| 7021 | } |
| 7022 | return NULL; |
| 7023 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7024 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7025 | { |
| 7026 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 7027 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7028 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7029 | { |
| 7030 | const struct built_in_command *x = find_builtin1(name); |
| 7031 | if (x) |
| 7032 | return x; |
| 7033 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 7034 | } |
| 7035 | |
| 7036 | #if ENABLE_HUSH_FUNCTIONS |
| 7037 | static struct function **find_function_slot(const char *name) |
| 7038 | { |
| 7039 | struct function **funcpp = &G.top_func; |
| 7040 | while (*funcpp) { |
| 7041 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 7042 | break; |
| 7043 | } |
| 7044 | funcpp = &(*funcpp)->next; |
| 7045 | } |
| 7046 | return funcpp; |
| 7047 | } |
| 7048 | |
| 7049 | static const struct function *find_function(const char *name) |
| 7050 | { |
| 7051 | const struct function *funcp = *find_function_slot(name); |
| 7052 | if (funcp) |
| 7053 | debug_printf_exec("found function '%s'\n", name); |
| 7054 | return funcp; |
| 7055 | } |
| 7056 | |
| 7057 | /* Note: takes ownership on name ptr */ |
| 7058 | static struct function *new_function(char *name) |
| 7059 | { |
| 7060 | struct function **funcpp = find_function_slot(name); |
| 7061 | struct function *funcp = *funcpp; |
| 7062 | |
| 7063 | if (funcp != NULL) { |
| 7064 | struct command *cmd = funcp->parent_cmd; |
| 7065 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 7066 | if (!cmd) { |
| 7067 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 7068 | free(funcp->name); |
| 7069 | /* Note: if !funcp->body, do not free body_as_string! |
| 7070 | * This is a special case of "-F name body" function: |
| 7071 | * body_as_string was not malloced! */ |
| 7072 | if (funcp->body) { |
| 7073 | free_pipe_list(funcp->body); |
| 7074 | # if !BB_MMU |
| 7075 | free(funcp->body_as_string); |
| 7076 | # endif |
| 7077 | } |
| 7078 | } else { |
| 7079 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 7080 | cmd->argv[0] = funcp->name; |
| 7081 | cmd->group = funcp->body; |
| 7082 | # if !BB_MMU |
| 7083 | cmd->group_as_string = funcp->body_as_string; |
| 7084 | # endif |
| 7085 | } |
| 7086 | } else { |
| 7087 | debug_printf_exec("remembering new function '%s'\n", name); |
| 7088 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 7089 | /*funcp->next = NULL;*/ |
| 7090 | } |
| 7091 | |
| 7092 | funcp->name = name; |
| 7093 | return funcp; |
| 7094 | } |
| 7095 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7096 | # if ENABLE_HUSH_UNSET |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7097 | static void unset_func(const char *name) |
| 7098 | { |
| 7099 | struct function **funcpp = find_function_slot(name); |
| 7100 | struct function *funcp = *funcpp; |
| 7101 | |
| 7102 | if (funcp != NULL) { |
| 7103 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 7104 | *funcpp = funcp->next; |
| 7105 | /* funcp is unlinked now, deleting it. |
| 7106 | * Note: if !funcp->body, the function was created by |
| 7107 | * "-F name body", do not free ->body_as_string |
| 7108 | * and ->name as they were not malloced. */ |
| 7109 | if (funcp->body) { |
| 7110 | free_pipe_list(funcp->body); |
| 7111 | free(funcp->name); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7112 | # if !BB_MMU |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7113 | free(funcp->body_as_string); |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7114 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7115 | } |
| 7116 | free(funcp); |
| 7117 | } |
| 7118 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 7119 | # endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7120 | |
| 7121 | # if BB_MMU |
| 7122 | #define exec_function(to_free, funcp, argv) \ |
| 7123 | exec_function(funcp, argv) |
| 7124 | # endif |
| 7125 | static void exec_function(char ***to_free, |
| 7126 | const struct function *funcp, |
| 7127 | char **argv) NORETURN; |
| 7128 | static void exec_function(char ***to_free, |
| 7129 | const struct function *funcp, |
| 7130 | char **argv) |
| 7131 | { |
| 7132 | # if BB_MMU |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7133 | int n; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7134 | |
| 7135 | argv[0] = G.global_argv[0]; |
| 7136 | G.global_argv = argv; |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 7137 | G.global_argc = n = 1 + string_array_len(argv + 1); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7138 | |
| 7139 | // Example when we are here: "cmd | func" |
| 7140 | // func will run with saved-redirect fds open. |
| 7141 | // $ f() { echo /proc/self/fd/*; } |
| 7142 | // $ true | f |
| 7143 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 |
| 7144 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob |
| 7145 | // Same in script: |
| 7146 | // $ . ./SCRIPT |
| 7147 | // /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4 |
| 7148 | // stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob |
| 7149 | // They are CLOEXEC so external programs won't see them, but |
| 7150 | // for "more correctness" we might want to close those extra fds here: |
| 7151 | //? close_saved_fds_and_FILE_fds(); |
| 7152 | |
| 7153 | /* "we are in function, ok to use return" */ |
| 7154 | G_flag_return_in_progress = -1; |
| 7155 | IF_HUSH_LOCAL(G.func_nest_level++;) |
| 7156 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7157 | /* On MMU, funcp->body is always non-NULL */ |
| 7158 | n = run_list(funcp->body); |
| 7159 | fflush_all(); |
| 7160 | _exit(n); |
| 7161 | # else |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7162 | //? close_saved_fds_and_FILE_fds(); |
| 7163 | |
| 7164 | //TODO: check whether "true | func_with_return" works |
| 7165 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7166 | re_execute_shell(to_free, |
| 7167 | funcp->body_as_string, |
| 7168 | G.global_argv[0], |
| 7169 | argv + 1, |
| 7170 | NULL); |
| 7171 | # endif |
| 7172 | } |
| 7173 | |
| 7174 | static int run_function(const struct function *funcp, char **argv) |
| 7175 | { |
| 7176 | int rc; |
| 7177 | save_arg_t sv; |
| 7178 | smallint sv_flg; |
| 7179 | |
| 7180 | save_and_replace_G_args(&sv, argv); |
| 7181 | |
| 7182 | /* "we are in function, ok to use return" */ |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7183 | sv_flg = G_flag_return_in_progress; |
| 7184 | G_flag_return_in_progress = -1; |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7185 | IF_HUSH_LOCAL(G.func_nest_level++;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7186 | |
| 7187 | /* On MMU, funcp->body is always non-NULL */ |
| 7188 | # if !BB_MMU |
| 7189 | if (!funcp->body) { |
| 7190 | /* Function defined by -F */ |
| 7191 | parse_and_run_string(funcp->body_as_string); |
| 7192 | rc = G.last_exitcode; |
| 7193 | } else |
| 7194 | # endif |
| 7195 | { |
| 7196 | rc = run_list(funcp->body); |
| 7197 | } |
| 7198 | |
| 7199 | # if ENABLE_HUSH_LOCAL |
| 7200 | { |
| 7201 | struct variable *var; |
| 7202 | struct variable **var_pp; |
| 7203 | |
| 7204 | var_pp = &G.top_var; |
| 7205 | while ((var = *var_pp) != NULL) { |
| 7206 | if (var->func_nest_level < G.func_nest_level) { |
| 7207 | var_pp = &var->next; |
| 7208 | continue; |
| 7209 | } |
| 7210 | /* Unexport */ |
| 7211 | if (var->flg_export) |
| 7212 | bb_unsetenv(var->varstr); |
| 7213 | /* Remove from global list */ |
| 7214 | *var_pp = var->next; |
| 7215 | /* Free */ |
| 7216 | if (!var->max_len) |
| 7217 | free(var->varstr); |
| 7218 | free(var); |
| 7219 | } |
| 7220 | G.func_nest_level--; |
| 7221 | } |
| 7222 | # endif |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 7223 | G_flag_return_in_progress = sv_flg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7224 | |
| 7225 | restore_G_args(&sv, argv); |
| 7226 | |
| 7227 | return rc; |
| 7228 | } |
| 7229 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 7230 | |
| 7231 | |
| 7232 | #if BB_MMU |
| 7233 | #define exec_builtin(to_free, x, argv) \ |
| 7234 | exec_builtin(x, argv) |
| 7235 | #else |
| 7236 | #define exec_builtin(to_free, x, argv) \ |
| 7237 | exec_builtin(to_free, argv) |
| 7238 | #endif |
| 7239 | static void exec_builtin(char ***to_free, |
| 7240 | const struct built_in_command *x, |
| 7241 | char **argv) NORETURN; |
| 7242 | static void exec_builtin(char ***to_free, |
| 7243 | const struct built_in_command *x, |
| 7244 | char **argv) |
| 7245 | { |
| 7246 | #if BB_MMU |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7247 | int rcode; |
| 7248 | fflush_all(); |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7249 | //? close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7250 | rcode = x->b_function(argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7251 | fflush_all(); |
| 7252 | _exit(rcode); |
| 7253 | #else |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 7254 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7255 | /* On NOMMU, we must never block! |
| 7256 | * Example: { sleep 99 | read line; } & echo Ok |
| 7257 | */ |
| 7258 | re_execute_shell(to_free, |
| 7259 | argv[0], |
| 7260 | G.global_argv[0], |
| 7261 | G.global_argv + 1, |
| 7262 | argv); |
| 7263 | #endif |
| 7264 | } |
| 7265 | |
| 7266 | |
| 7267 | static void execvp_or_die(char **argv) NORETURN; |
| 7268 | static void execvp_or_die(char **argv) |
| 7269 | { |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7270 | int e; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7271 | debug_printf_exec("execing '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7272 | /* Don't propagate SIG_IGN to the child */ |
| 7273 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7274 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7275 | execvp(argv[0], argv); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7276 | e = 2; |
| 7277 | if (errno == EACCES) e = 126; |
| 7278 | if (errno == ENOENT) e = 127; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7279 | bb_perror_msg("can't execute '%s'", argv[0]); |
Denys Vlasenko | 04465da | 2016-10-03 01:01:15 +0200 | [diff] [blame] | 7280 | _exit(e); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7281 | } |
| 7282 | |
| 7283 | #if ENABLE_HUSH_MODE_X |
| 7284 | static void dump_cmd_in_x_mode(char **argv) |
| 7285 | { |
| 7286 | if (G_x_mode && argv) { |
| 7287 | /* We want to output the line in one write op */ |
| 7288 | char *buf, *p; |
| 7289 | int len; |
| 7290 | int n; |
| 7291 | |
| 7292 | len = 3; |
| 7293 | n = 0; |
| 7294 | while (argv[n]) |
| 7295 | len += strlen(argv[n++]) + 1; |
| 7296 | buf = xmalloc(len); |
| 7297 | buf[0] = '+'; |
| 7298 | p = buf + 1; |
| 7299 | n = 0; |
| 7300 | while (argv[n]) |
| 7301 | p += sprintf(p, " %s", argv[n++]); |
| 7302 | *p++ = '\n'; |
| 7303 | *p = '\0'; |
| 7304 | fputs(buf, stderr); |
| 7305 | free(buf); |
| 7306 | } |
| 7307 | } |
| 7308 | #else |
| 7309 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 7310 | #endif |
| 7311 | |
| 7312 | #if BB_MMU |
| 7313 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 7314 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 7315 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 7316 | pseudo_exec(command, argv_expanded) |
| 7317 | #endif |
| 7318 | |
| 7319 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 7320 | * Never returns. |
| 7321 | * Don't exit() here. If you don't exec, use _exit instead. |
| 7322 | * The at_exit handlers apparently confuse the calling process, |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7323 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) |
Denys Vlasenko | 215b0ca | 2016-08-19 18:23:56 +0200 | [diff] [blame] | 7324 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7325 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7326 | char **argv, int assignment_cnt, |
| 7327 | char **argv_expanded) NORETURN; |
| 7328 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 7329 | char **argv, int assignment_cnt, |
| 7330 | char **argv_expanded) |
| 7331 | { |
| 7332 | char **new_env; |
| 7333 | |
| 7334 | new_env = expand_assignments(argv, assignment_cnt); |
| 7335 | dump_cmd_in_x_mode(new_env); |
| 7336 | |
| 7337 | if (!argv[assignment_cnt]) { |
| 7338 | /* Case when we are here: ... | var=val | ... |
| 7339 | * (note that we do not exit early, i.e., do not optimize out |
| 7340 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 7341 | */ |
| 7342 | free_strings(new_env); |
| 7343 | _exit(EXIT_SUCCESS); |
| 7344 | } |
| 7345 | |
| 7346 | #if BB_MMU |
| 7347 | set_vars_and_save_old(new_env); |
| 7348 | free(new_env); /* optional */ |
| 7349 | /* we can also destroy set_vars_and_save_old's return value, |
| 7350 | * to save memory */ |
| 7351 | #else |
| 7352 | nommu_save->new_env = new_env; |
| 7353 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 7354 | #endif |
| 7355 | |
| 7356 | if (argv_expanded) { |
| 7357 | argv = argv_expanded; |
| 7358 | } else { |
| 7359 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 7360 | #if !BB_MMU |
| 7361 | nommu_save->argv = argv; |
| 7362 | #endif |
| 7363 | } |
| 7364 | dump_cmd_in_x_mode(argv); |
| 7365 | |
| 7366 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7367 | if (strchr(argv[0], '/') != NULL) |
| 7368 | goto skip; |
| 7369 | #endif |
| 7370 | |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7371 | #if ENABLE_HUSH_FUNCTIONS |
| 7372 | /* Check if the command matches any functions (this goes before bltins) */ |
| 7373 | { |
| 7374 | const struct function *funcp = find_function(argv[0]); |
| 7375 | if (funcp) { |
| 7376 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 7377 | } |
| 7378 | } |
| 7379 | #endif |
| 7380 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7381 | /* Check if the command matches any of the builtins. |
| 7382 | * Depending on context, this might be redundant. But it's |
| 7383 | * easier to waste a few CPU cycles than it is to figure out |
| 7384 | * if this is one of those cases. |
| 7385 | */ |
| 7386 | { |
| 7387 | /* On NOMMU, it is more expensive to re-execute shell |
| 7388 | * just in order to run echo or test builtin. |
| 7389 | * It's better to skip it here and run corresponding |
| 7390 | * non-builtin later. */ |
| 7391 | const struct built_in_command *x; |
| 7392 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 7393 | if (x) { |
| 7394 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 7395 | } |
| 7396 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7397 | |
| 7398 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7399 | /* Check if the command matches any busybox applets */ |
| 7400 | { |
| 7401 | int a = find_applet_by_name(argv[0]); |
| 7402 | if (a >= 0) { |
| 7403 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 7404 | if (APPLET_IS_NOEXEC(a)) { |
Denys Vlasenko | bf1c344 | 2017-07-31 04:54:53 +0200 | [diff] [blame] | 7405 | /* Do not leak open fds from opened script files etc. |
| 7406 | * Testcase: interactive "ls -l /proc/self/fd" |
| 7407 | * should not show tty fd open. |
| 7408 | */ |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 7409 | close_saved_fds_and_FILE_fds(); |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 7410 | //FIXME: should also close saved redir fds |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 7411 | /* Without this, "rm -i FILE" can't be ^C'ed: */ |
| 7412 | switch_off_special_sigs(G.special_sig_mask); |
Denys Vlasenko | c9c1ccc | 2017-08-07 18:59:35 +0200 | [diff] [blame] | 7413 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denys Vlasenko | 80e8e3c | 2017-08-07 19:24:57 +0200 | [diff] [blame] | 7414 | run_noexec_applet_and_exit(a, argv[0], argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7415 | } |
| 7416 | # endif |
| 7417 | /* Re-exec ourselves */ |
| 7418 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denys Vlasenko | 75e77de | 2011-05-12 13:12:47 +0200 | [diff] [blame] | 7419 | /* Don't propagate SIG_IGN to the child */ |
| 7420 | if (SPECIAL_JOBSTOP_SIGS != 0) |
| 7421 | switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7422 | execv(bb_busybox_exec_path, argv); |
| 7423 | /* If they called chroot or otherwise made the binary no longer |
| 7424 | * executable, fall through */ |
| 7425 | } |
| 7426 | } |
| 7427 | #endif |
| 7428 | |
| 7429 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 7430 | skip: |
| 7431 | #endif |
| 7432 | execvp_or_die(argv); |
| 7433 | } |
| 7434 | |
| 7435 | /* Called after [v]fork() in run_pipe |
| 7436 | */ |
| 7437 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7438 | struct command *command, |
| 7439 | char **argv_expanded) NORETURN; |
| 7440 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 7441 | struct command *command, |
| 7442 | char **argv_expanded) |
| 7443 | { |
| 7444 | if (command->argv) { |
| 7445 | pseudo_exec_argv(nommu_save, command->argv, |
| 7446 | command->assignment_cnt, argv_expanded); |
| 7447 | } |
| 7448 | |
| 7449 | if (command->group) { |
| 7450 | /* Cases when we are here: |
| 7451 | * ( list ) |
| 7452 | * { list } & |
| 7453 | * ... | ( list ) | ... |
| 7454 | * ... | { list } | ... |
| 7455 | */ |
| 7456 | #if BB_MMU |
| 7457 | int rcode; |
| 7458 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 7459 | reset_traps_to_defaults(); |
| 7460 | rcode = run_list(command->group); |
| 7461 | /* OK to leak memory by not calling free_pipe_list, |
| 7462 | * since this process is about to exit */ |
| 7463 | _exit(rcode); |
| 7464 | #else |
| 7465 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 7466 | command->group_as_string, |
| 7467 | G.global_argv[0], |
| 7468 | G.global_argv + 1, |
| 7469 | NULL); |
| 7470 | #endif |
| 7471 | } |
| 7472 | |
| 7473 | /* Case when we are here: ... | >file */ |
| 7474 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 7475 | _exit(EXIT_SUCCESS); |
| 7476 | } |
| 7477 | |
| 7478 | #if ENABLE_HUSH_JOB |
| 7479 | static const char *get_cmdtext(struct pipe *pi) |
| 7480 | { |
| 7481 | char **argv; |
| 7482 | char *p; |
| 7483 | int len; |
| 7484 | |
| 7485 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 7486 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 7487 | * On subsequent bg argv is trashed, but we won't use it */ |
| 7488 | if (pi->cmdtext) |
| 7489 | return pi->cmdtext; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7490 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7491 | argv = pi->cmds[0].argv; |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7492 | if (!argv) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7493 | pi->cmdtext = xzalloc(1); |
| 7494 | return pi->cmdtext; |
| 7495 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7496 | len = 0; |
| 7497 | do { |
| 7498 | len += strlen(*argv) + 1; |
| 7499 | } while (*++argv); |
| 7500 | p = xmalloc(len); |
| 7501 | pi->cmdtext = p; |
| 7502 | argv = pi->cmds[0].argv; |
| 7503 | do { |
Denys Vlasenko | 1eada9a | 2016-11-08 17:28:45 +0100 | [diff] [blame] | 7504 | p = stpcpy(p, *argv); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7505 | *p++ = ' '; |
| 7506 | } while (*++argv); |
| 7507 | p[-1] = '\0'; |
| 7508 | return pi->cmdtext; |
| 7509 | } |
| 7510 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7511 | static void remove_job_from_table(struct pipe *pi) |
| 7512 | { |
| 7513 | struct pipe *prev_pipe; |
| 7514 | |
| 7515 | if (pi == G.job_list) { |
| 7516 | G.job_list = pi->next; |
| 7517 | } else { |
| 7518 | prev_pipe = G.job_list; |
| 7519 | while (prev_pipe->next != pi) |
| 7520 | prev_pipe = prev_pipe->next; |
| 7521 | prev_pipe->next = pi->next; |
| 7522 | } |
| 7523 | G.last_jobid = 0; |
| 7524 | if (G.job_list) |
| 7525 | G.last_jobid = G.job_list->jobid; |
| 7526 | } |
| 7527 | |
| 7528 | static void delete_finished_job(struct pipe *pi) |
| 7529 | { |
| 7530 | remove_job_from_table(pi); |
| 7531 | free_pipe(pi); |
| 7532 | } |
| 7533 | |
| 7534 | static void clean_up_last_dead_job(void) |
| 7535 | { |
| 7536 | if (G.job_list && !G.job_list->alive_cmds) |
| 7537 | delete_finished_job(G.job_list); |
| 7538 | } |
| 7539 | |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7540 | static void insert_job_into_table(struct pipe *pi) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7541 | { |
| 7542 | struct pipe *job, **jobp; |
| 7543 | int i; |
| 7544 | |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7545 | clean_up_last_dead_job(); |
| 7546 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7547 | /* Find the end of the list, and find next job ID to use */ |
| 7548 | i = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7549 | jobp = &G.job_list; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7550 | while ((job = *jobp) != NULL) { |
| 7551 | if (job->jobid > i) |
| 7552 | i = job->jobid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7553 | jobp = &job->next; |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7554 | } |
| 7555 | pi->jobid = i + 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7556 | |
Denys Vlasenko | 9e55a15 | 2017-07-10 10:01:12 +0200 | [diff] [blame] | 7557 | /* Create a new job struct at the end */ |
| 7558 | job = *jobp = xmemdup(pi, sizeof(*pi)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7559 | job->next = NULL; |
| 7560 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 7561 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 7562 | for (i = 0; i < pi->num_cmds; i++) { |
| 7563 | job->cmds[i].pid = pi->cmds[i].pid; |
| 7564 | /* all other fields are not used and stay zero */ |
| 7565 | } |
| 7566 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 7567 | |
| 7568 | if (G_interactive_fd) |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 7569 | 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] | 7570 | G.last_jobid = job->jobid; |
| 7571 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7572 | #endif /* JOB */ |
| 7573 | |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7574 | static int job_exited_or_stopped(struct pipe *pi) |
| 7575 | { |
| 7576 | int rcode, i; |
| 7577 | |
| 7578 | if (pi->alive_cmds != pi->stopped_cmds) |
| 7579 | return -1; |
| 7580 | |
| 7581 | /* All processes in fg pipe have exited or stopped */ |
| 7582 | rcode = 0; |
| 7583 | i = pi->num_cmds; |
| 7584 | while (--i >= 0) { |
| 7585 | rcode = pi->cmds[i].cmd_exitcode; |
| 7586 | /* usually last process gives overall exitstatus, |
| 7587 | * but with "set -o pipefail", last *failed* process does */ |
| 7588 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 7589 | break; |
| 7590 | } |
| 7591 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7592 | return rcode; |
| 7593 | } |
| 7594 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7595 | 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] | 7596 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7597 | #if ENABLE_HUSH_JOB |
| 7598 | struct pipe *pi; |
| 7599 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7600 | int i, dead; |
| 7601 | |
| 7602 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 7603 | |
| 7604 | #if DEBUG_JOBS |
| 7605 | if (WIFSTOPPED(status)) |
| 7606 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 7607 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 7608 | if (WIFSIGNALED(status)) |
| 7609 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 7610 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 7611 | if (WIFEXITED(status)) |
| 7612 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 7613 | childpid, WEXITSTATUS(status)); |
| 7614 | #endif |
| 7615 | /* Were we asked to wait for a fg pipe? */ |
| 7616 | if (fg_pipe) { |
| 7617 | i = fg_pipe->num_cmds; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7618 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7619 | while (--i >= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7620 | int rcode; |
| 7621 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7622 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 7623 | if (fg_pipe->cmds[i].pid != childpid) |
| 7624 | continue; |
| 7625 | if (dead) { |
| 7626 | int ex; |
| 7627 | fg_pipe->cmds[i].pid = 0; |
| 7628 | fg_pipe->alive_cmds--; |
| 7629 | ex = WEXITSTATUS(status); |
| 7630 | /* bash prints killer signal's name for *last* |
| 7631 | * process in pipe (prints just newline for SIGINT/SIGPIPE). |
| 7632 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 7633 | */ |
| 7634 | if (WIFSIGNALED(status)) { |
| 7635 | int sig = WTERMSIG(status); |
| 7636 | if (i == fg_pipe->num_cmds-1) |
| 7637 | /* TODO: use strsignal() instead for bash compat? but that's bloat... */ |
| 7638 | puts(sig == SIGINT || sig == SIGPIPE ? "" : get_signame(sig)); |
| 7639 | /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */ |
| 7640 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 7641 | * Maybe we need to use sig | 128? */ |
| 7642 | ex = sig + 128; |
| 7643 | } |
| 7644 | fg_pipe->cmds[i].cmd_exitcode = ex; |
| 7645 | } else { |
| 7646 | fg_pipe->stopped_cmds++; |
| 7647 | } |
| 7648 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 7649 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7650 | rcode = job_exited_or_stopped(fg_pipe); |
| 7651 | if (rcode >= 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7652 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 7653 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 7654 | * and "killall -STOP cat" */ |
| 7655 | if (G_interactive_fd) { |
| 7656 | #if ENABLE_HUSH_JOB |
| 7657 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 7658 | insert_job_into_table(fg_pipe); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7659 | #endif |
| 7660 | return rcode; |
| 7661 | } |
| 7662 | if (fg_pipe->alive_cmds == 0) |
| 7663 | return rcode; |
| 7664 | } |
| 7665 | /* There are still running processes in the fg_pipe */ |
| 7666 | return -1; |
| 7667 | } |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 7668 | /* It wasn't in fg_pipe, look for process in bg pipes */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | #if ENABLE_HUSH_JOB |
| 7672 | /* We were asked to wait for bg or orphaned children */ |
| 7673 | /* No need to remember exitcode in this case */ |
| 7674 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7675 | for (i = 0; i < pi->num_cmds; i++) { |
| 7676 | if (pi->cmds[i].pid == childpid) |
| 7677 | goto found_pi_and_prognum; |
| 7678 | } |
| 7679 | } |
| 7680 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 7681 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 7682 | return -1; /* this wasn't a process from fg_pipe */ |
| 7683 | |
| 7684 | found_pi_and_prognum: |
| 7685 | if (dead) { |
| 7686 | /* child exited */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 7687 | int rcode = WEXITSTATUS(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7688 | if (WIFSIGNALED(status)) |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 7689 | rcode = 128 + WTERMSIG(status); |
| 7690 | pi->cmds[i].cmd_exitcode = rcode; |
| 7691 | if (G.last_bg_pid == pi->cmds[i].pid) |
| 7692 | G.last_bg_pid_exitcode = rcode; |
| 7693 | pi->cmds[i].pid = 0; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7694 | pi->alive_cmds--; |
| 7695 | if (!pi->alive_cmds) { |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7696 | if (G_interactive_fd) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7697 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 7698 | "Done", pi->cmdtext); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 7699 | delete_finished_job(pi); |
| 7700 | } else { |
| 7701 | /* |
| 7702 | * bash deletes finished jobs from job table only in interactive mode, |
| 7703 | * after "jobs" cmd, or if pid of a new process matches one of the old ones |
| 7704 | * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source). |
| 7705 | * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash. |
| 7706 | * We only retain one "dead" job, if it's the single job on the list. |
| 7707 | * This covers most of real-world scenarios where this is useful. |
| 7708 | */ |
| 7709 | if (pi != G.job_list) |
| 7710 | delete_finished_job(pi); |
| 7711 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7712 | } |
| 7713 | } else { |
| 7714 | /* child stopped */ |
| 7715 | pi->stopped_cmds++; |
| 7716 | } |
| 7717 | #endif |
| 7718 | return -1; /* this wasn't a process from fg_pipe */ |
| 7719 | } |
| 7720 | |
| 7721 | /* Check to see if any processes have exited -- if they have, |
| 7722 | * figure out why and see if a job has completed. |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7723 | * |
| 7724 | * If non-NULL fg_pipe: wait for its completion or stop. |
| 7725 | * Return its exitcode or zero if stopped. |
| 7726 | * |
| 7727 | * Alternatively (fg_pipe == NULL, waitfor_pid != 0): |
| 7728 | * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1, |
| 7729 | * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7730 | * or 0 if no children changed status. |
| 7731 | * |
| 7732 | * Alternatively (fg_pipe == NULL, waitfor_pid == 0), |
| 7733 | * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for) |
| 7734 | * or 0 if no children changed status. |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7735 | */ |
| 7736 | static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid) |
| 7737 | { |
| 7738 | int attributes; |
| 7739 | int status; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7740 | int rcode = 0; |
| 7741 | |
| 7742 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 7743 | |
| 7744 | attributes = WUNTRACED; |
| 7745 | if (fg_pipe == NULL) |
| 7746 | attributes |= WNOHANG; |
| 7747 | |
| 7748 | errno = 0; |
| 7749 | #if ENABLE_HUSH_FAST |
| 7750 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 7751 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 7752 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 7753 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 7754 | /* Avoid doing waitpid syscall if possible */ |
| 7755 | if (!G.we_have_children) { |
| 7756 | errno = ECHILD; |
| 7757 | return -1; |
| 7758 | } |
| 7759 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 7760 | /* We have children, but they did not exit |
| 7761 | * or stop yet (we saw no SIGCHLD) */ |
| 7762 | return 0; |
| 7763 | } |
| 7764 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 7765 | } |
| 7766 | #endif |
| 7767 | |
| 7768 | /* Do we do this right? |
| 7769 | * bash-3.00# sleep 20 | false |
| 7770 | * <ctrl-Z pressed> |
| 7771 | * [3]+ Stopped sleep 20 | false |
| 7772 | * bash-3.00# echo $? |
| 7773 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 7774 | * [hush 1.14.0: yes we do it right] |
| 7775 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7776 | while (1) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7777 | pid_t childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7778 | #if ENABLE_HUSH_FAST |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7779 | int i; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7780 | i = G.count_SIGCHLD; |
| 7781 | #endif |
| 7782 | childpid = waitpid(-1, &status, attributes); |
| 7783 | if (childpid <= 0) { |
| 7784 | if (childpid && errno != ECHILD) |
| 7785 | bb_perror_msg("waitpid"); |
| 7786 | #if ENABLE_HUSH_FAST |
| 7787 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 7788 | G.we_have_children = (childpid == 0); |
| 7789 | G.handled_SIGCHLD = i; |
| 7790 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 7791 | } |
| 7792 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7793 | /* ECHILD (no children), or 0 (no change in children status) */ |
| 7794 | rcode = childpid; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7795 | break; |
| 7796 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7797 | rcode = process_wait_result(fg_pipe, childpid, status); |
| 7798 | if (rcode >= 0) { |
| 7799 | /* fg_pipe exited or stopped */ |
| 7800 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7801 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7802 | if (childpid == waitfor_pid) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7803 | 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] | 7804 | rcode = WEXITSTATUS(status); |
| 7805 | if (WIFSIGNALED(status)) |
| 7806 | rcode = 128 + WTERMSIG(status); |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 7807 | if (WIFSTOPPED(status)) |
| 7808 | /* bash: "cmd & wait $!" and cmd stops: $? = 128 + stopsig */ |
| 7809 | rcode = 128 + WSTOPSIG(status); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7810 | rcode++; |
| 7811 | break; /* "wait PID" called us, give it exitcode+1 */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7812 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7813 | /* This wasn't one of our processes, or */ |
| 7814 | /* fg_pipe still has running processes, do waitpid again */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7815 | } /* while (waitpid succeeds)... */ |
| 7816 | |
| 7817 | return rcode; |
| 7818 | } |
| 7819 | |
| 7820 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 7821 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7822 | { |
| 7823 | pid_t p; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 7824 | int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7825 | if (G_saved_tty_pgrp) { |
| 7826 | /* Job finished, move the shell to the foreground */ |
| 7827 | p = getpgrp(); /* our process group id */ |
| 7828 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 7829 | tcsetpgrp(G_interactive_fd, p); |
| 7830 | } |
| 7831 | return rcode; |
| 7832 | } |
| 7833 | #endif |
| 7834 | |
| 7835 | /* Start all the jobs, but don't wait for anything to finish. |
| 7836 | * See checkjobs(). |
| 7837 | * |
| 7838 | * Return code is normally -1, when the caller has to wait for children |
| 7839 | * to finish to determine the exit status of the pipe. If the pipe |
| 7840 | * is a simple builtin command, however, the action is done by the |
| 7841 | * time run_pipe returns, and the exit code is provided as the |
| 7842 | * return value. |
| 7843 | * |
| 7844 | * Returns -1 only if started some children. IOW: we have to |
| 7845 | * mask out retvals of builtins etc with 0xff! |
| 7846 | * |
| 7847 | * The only case when we do not need to [v]fork is when the pipe |
| 7848 | * is single, non-backgrounded, non-subshell command. Examples: |
| 7849 | * cmd ; ... { list } ; ... |
| 7850 | * cmd && ... { list } && ... |
| 7851 | * cmd || ... { list } || ... |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 7852 | * If it is, then we can run cmd as a builtin, NOFORK, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7853 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 7854 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 7855 | * |
| 7856 | * Cases when we must fork: |
| 7857 | * non-single: cmd | cmd |
| 7858 | * backgrounded: cmd & { list } & |
| 7859 | * subshell: ( list ) [&] |
| 7860 | */ |
| 7861 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 7862 | #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] | 7863 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 7864 | #endif |
| 7865 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 7866 | struct variable **old_vars_p, |
| 7867 | struct command *command, |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7868 | struct squirrel **sqp, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7869 | char **argv_expanded) |
| 7870 | { |
| 7871 | /* setup_redirects acts on file descriptors, not FILEs. |
| 7872 | * This is perfect for work that comes after exec(). |
| 7873 | * Is it really safe for inline use? Experimentally, |
| 7874 | * things seem to work. */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7875 | int rcode = setup_redirects(command, sqp); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7876 | if (rcode == 0) { |
| 7877 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 7878 | *new_env_p = new_env; |
| 7879 | dump_cmd_in_x_mode(new_env); |
| 7880 | dump_cmd_in_x_mode(argv_expanded); |
| 7881 | if (old_vars_p) |
| 7882 | *old_vars_p = set_vars_and_save_old(new_env); |
| 7883 | } |
| 7884 | return rcode; |
| 7885 | } |
| 7886 | static NOINLINE int run_pipe(struct pipe *pi) |
| 7887 | { |
| 7888 | static const char *const null_ptr = NULL; |
| 7889 | |
| 7890 | int cmd_no; |
| 7891 | int next_infd; |
| 7892 | struct command *command; |
| 7893 | char **argv_expanded; |
| 7894 | char **argv; |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7895 | struct squirrel *squirrel = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7896 | int rcode; |
| 7897 | |
| 7898 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 7899 | debug_enter(); |
| 7900 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 7901 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 7902 | * Result should be 3 lines: q w e, qwe, q w e |
| 7903 | */ |
| 7904 | G.ifs = get_local_var_value("IFS"); |
| 7905 | if (!G.ifs) |
| 7906 | G.ifs = defifs; |
| 7907 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7908 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 7909 | pi->stopped_cmds = 0; |
| 7910 | command = &pi->cmds[0]; |
| 7911 | argv_expanded = NULL; |
| 7912 | |
| 7913 | if (pi->num_cmds != 1 |
| 7914 | || pi->followup == PIPE_BG |
| 7915 | || command->cmd_type == CMD_SUBSHELL |
| 7916 | ) { |
| 7917 | goto must_fork; |
| 7918 | } |
| 7919 | |
| 7920 | pi->alive_cmds = 1; |
| 7921 | |
| 7922 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 7923 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 7924 | |
| 7925 | if (command->group) { |
| 7926 | #if ENABLE_HUSH_FUNCTIONS |
| 7927 | if (command->cmd_type == CMD_FUNCDEF) { |
| 7928 | /* "executing" func () { list } */ |
| 7929 | struct function *funcp; |
| 7930 | |
| 7931 | funcp = new_function(command->argv[0]); |
| 7932 | /* funcp->name is already set to argv[0] */ |
| 7933 | funcp->body = command->group; |
| 7934 | # if !BB_MMU |
| 7935 | funcp->body_as_string = command->group_as_string; |
| 7936 | command->group_as_string = NULL; |
| 7937 | # endif |
| 7938 | command->group = NULL; |
| 7939 | command->argv[0] = NULL; |
| 7940 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 7941 | funcp->parent_cmd = command; |
| 7942 | command->child_func = funcp; |
| 7943 | |
| 7944 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 7945 | debug_leave(); |
| 7946 | return EXIT_SUCCESS; |
| 7947 | } |
| 7948 | #endif |
| 7949 | /* { list } */ |
| 7950 | debug_printf("non-subshell group\n"); |
| 7951 | rcode = 1; /* exitcode if redir failed */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7952 | if (setup_redirects(command, &squirrel) == 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7953 | debug_printf_exec(": run_list\n"); |
| 7954 | rcode = run_list(command->group) & 0xff; |
| 7955 | } |
| 7956 | restore_redirects(squirrel); |
| 7957 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 7958 | debug_leave(); |
| 7959 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 7960 | return rcode; |
| 7961 | } |
| 7962 | |
| 7963 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 7964 | { |
| 7965 | const struct built_in_command *x; |
| 7966 | #if ENABLE_HUSH_FUNCTIONS |
| 7967 | const struct function *funcp; |
| 7968 | #else |
| 7969 | enum { funcp = 0 }; |
| 7970 | #endif |
| 7971 | char **new_env = NULL; |
| 7972 | struct variable *old_vars = NULL; |
| 7973 | |
| 7974 | if (argv[command->assignment_cnt] == NULL) { |
| 7975 | /* Assignments, but no command */ |
| 7976 | /* Ensure redirects take effect (that is, create files). |
| 7977 | * Try "a=t >file" */ |
| 7978 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 7979 | 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] | 7980 | /* Set shell variables */ |
| 7981 | if (new_env) { |
| 7982 | argv = new_env; |
| 7983 | while (*argv) { |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 7984 | if (set_local_var(*argv, /*flag:*/ 0)) { |
| 7985 | /* assignment to readonly var / putenv error? */ |
| 7986 | rcode = 1; |
| 7987 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7988 | argv++; |
| 7989 | } |
| 7990 | } |
| 7991 | /* Redirect error sets $? to 1. Otherwise, |
| 7992 | * if evaluating assignment value set $?, retain it. |
| 7993 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 7994 | if (rcode == 0) |
| 7995 | rcode = G.last_exitcode; |
| 7996 | /* Exit, _skipping_ variable restoring code: */ |
| 7997 | goto clean_up_and_ret0; |
| 7998 | |
| 7999 | #else /* Older, bigger, but more correct code */ |
| 8000 | |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8001 | rcode = setup_redirects(command, &squirrel); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8002 | restore_redirects(squirrel); |
| 8003 | /* Set shell variables */ |
| 8004 | if (G_x_mode) |
| 8005 | bb_putchar_stderr('+'); |
| 8006 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 8007 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8008 | if (G_x_mode) |
| 8009 | fprintf(stderr, " %s", p); |
| 8010 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 8011 | *argv, p); |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 8012 | if (set_local_var(p, /*flag:*/ 0)) { |
| 8013 | /* assignment to readonly var / putenv error? */ |
| 8014 | rcode = 1; |
| 8015 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8016 | argv++; |
| 8017 | } |
| 8018 | if (G_x_mode) |
| 8019 | bb_putchar_stderr('\n'); |
| 8020 | /* Redirect error sets $? to 1. Otherwise, |
| 8021 | * if evaluating assignment value set $?, retain it. |
| 8022 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 8023 | if (rcode == 0) |
| 8024 | rcode = G.last_exitcode; |
| 8025 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8026 | debug_leave(); |
| 8027 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 8028 | return rcode; |
| 8029 | #endif |
| 8030 | } |
| 8031 | |
| 8032 | /* Expand the rest into (possibly) many strings each */ |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 8033 | #if BASH_TEST2 |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8034 | if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8035 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8036 | } else |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8037 | #endif |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8038 | { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8039 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 8040 | } |
| 8041 | |
| 8042 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 8043 | if (!argv_expanded[0]) { |
| 8044 | free(argv_expanded); |
| 8045 | debug_leave(); |
| 8046 | return G.last_exitcode; |
| 8047 | } |
| 8048 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8049 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8050 | /* Check if argv[0] matches any functions (this goes before bltins) */ |
| 8051 | funcp = find_function(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8052 | #endif |
Denys Vlasenko | 75481d3 | 2017-07-31 05:27:09 +0200 | [diff] [blame] | 8053 | x = NULL; |
| 8054 | if (!funcp) |
| 8055 | x = find_builtin(argv_expanded[0]); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8056 | if (x || funcp) { |
| 8057 | if (!funcp) { |
| 8058 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 8059 | debug_printf("exec with redirects only\n"); |
| 8060 | rcode = setup_redirects(command, NULL); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8061 | /* rcode=1 can be if redir file can't be opened */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8062 | goto clean_up_and_ret1; |
| 8063 | } |
| 8064 | } |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8065 | 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] | 8066 | if (rcode == 0) { |
| 8067 | if (!funcp) { |
| 8068 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 8069 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8070 | fflush_all(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8071 | rcode = x->b_function(argv_expanded) & 0xff; |
| 8072 | fflush_all(); |
| 8073 | } |
| 8074 | #if ENABLE_HUSH_FUNCTIONS |
| 8075 | else { |
| 8076 | # if ENABLE_HUSH_LOCAL |
| 8077 | struct variable **sv; |
| 8078 | sv = G.shadowed_vars_pp; |
| 8079 | G.shadowed_vars_pp = &old_vars; |
| 8080 | # endif |
| 8081 | debug_printf_exec(": function '%s' '%s'...\n", |
| 8082 | funcp->name, argv_expanded[1]); |
| 8083 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 8084 | # if ENABLE_HUSH_LOCAL |
| 8085 | G.shadowed_vars_pp = sv; |
| 8086 | # endif |
| 8087 | } |
| 8088 | #endif |
| 8089 | } |
| 8090 | clean_up_and_ret: |
| 8091 | unset_vars(new_env); |
| 8092 | add_vars(old_vars); |
| 8093 | /* clean_up_and_ret0: */ |
| 8094 | restore_redirects(squirrel); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8095 | /* |
| 8096 | * Try "usleep 99999999" + ^C + "echo $?" |
| 8097 | * with FEATURE_SH_NOFORK=y. |
| 8098 | */ |
| 8099 | if (!funcp) { |
| 8100 | /* It was builtin or nofork. |
| 8101 | * if this would be a real fork/execed program, |
| 8102 | * it should have died if a fatal sig was received. |
| 8103 | * But OTOH, there was no separate process, |
| 8104 | * the sig was sent to _shell_, not to non-existing |
| 8105 | * child. |
| 8106 | * Let's just handle ^C only, this one is obvious: |
| 8107 | * we aren't ok with exitcode 0 when ^C was pressed |
| 8108 | * during builtin/nofork. |
| 8109 | */ |
| 8110 | if (sigismember(&G.pending_set, SIGINT)) |
| 8111 | rcode = 128 + SIGINT; |
| 8112 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8113 | clean_up_and_ret1: |
| 8114 | free(argv_expanded); |
| 8115 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 8116 | debug_leave(); |
| 8117 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 8118 | return rcode; |
| 8119 | } |
| 8120 | |
Denys Vlasenko | b72baeb | 2011-02-02 18:38:57 +0100 | [diff] [blame] | 8121 | if (ENABLE_FEATURE_SH_NOFORK) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8122 | int n = find_applet_by_name(argv_expanded[0]); |
| 8123 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 8124 | 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] | 8125 | if (rcode == 0) { |
| 8126 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 8127 | argv_expanded[0], argv_expanded[1]); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8128 | /* |
| 8129 | * Note: signals (^C) can't interrupt here. |
| 8130 | * We remember them and they will be acted upon |
| 8131 | * after applet returns. |
| 8132 | * This makes applets which can run for a long time |
| 8133 | * and/or wait for user input ineligible for NOFORK: |
| 8134 | * for example, "yes" or "rm" (rm -i waits for input). |
| 8135 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8136 | rcode = run_nofork_applet(n, argv_expanded); |
| 8137 | } |
| 8138 | goto clean_up_and_ret; |
| 8139 | } |
| 8140 | } |
| 8141 | /* It is neither builtin nor applet. We must fork. */ |
| 8142 | } |
| 8143 | |
| 8144 | must_fork: |
| 8145 | /* NB: argv_expanded may already be created, and that |
| 8146 | * might include `cmd` runs! Do not rerun it! We *must* |
| 8147 | * use argv_expanded if it's non-NULL */ |
| 8148 | |
| 8149 | /* Going to fork a child per each pipe member */ |
| 8150 | pi->alive_cmds = 0; |
| 8151 | next_infd = 0; |
| 8152 | |
| 8153 | cmd_no = 0; |
| 8154 | while (cmd_no < pi->num_cmds) { |
| 8155 | struct fd_pair pipefds; |
| 8156 | #if !BB_MMU |
| 8157 | volatile nommu_save_t nommu_save; |
| 8158 | nommu_save.new_env = NULL; |
| 8159 | nommu_save.old_vars = NULL; |
| 8160 | nommu_save.argv = NULL; |
| 8161 | nommu_save.argv_from_re_execing = NULL; |
| 8162 | #endif |
| 8163 | command = &pi->cmds[cmd_no]; |
| 8164 | cmd_no++; |
| 8165 | if (command->argv) { |
| 8166 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 8167 | command->argv[0], command->argv[1]); |
| 8168 | } else { |
| 8169 | debug_printf_exec(": pipe member with no argv\n"); |
| 8170 | } |
| 8171 | |
| 8172 | /* pipes are inserted between pairs of commands */ |
| 8173 | pipefds.rd = 0; |
| 8174 | pipefds.wr = 1; |
| 8175 | if (cmd_no < pi->num_cmds) |
| 8176 | xpiped_pair(pipefds); |
| 8177 | |
| 8178 | command->pid = BB_MMU ? fork() : vfork(); |
| 8179 | if (!command->pid) { /* child */ |
| 8180 | #if ENABLE_HUSH_JOB |
| 8181 | disable_restore_tty_pgrp_on_exit(); |
| 8182 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 8183 | |
| 8184 | /* Every child adds itself to new process group |
| 8185 | * with pgid == pid_of_first_child_in_pipe */ |
| 8186 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8187 | pid_t pgrp; |
| 8188 | pgrp = pi->pgrp; |
| 8189 | if (pgrp < 0) /* true for 1st process only */ |
| 8190 | pgrp = getpid(); |
| 8191 | if (setpgid(0, pgrp) == 0 |
| 8192 | && pi->followup != PIPE_BG |
| 8193 | && G_saved_tty_pgrp /* we have ctty */ |
| 8194 | ) { |
| 8195 | /* We do it in *every* child, not just first, |
| 8196 | * to avoid races */ |
| 8197 | tcsetpgrp(G_interactive_fd, pgrp); |
| 8198 | } |
| 8199 | } |
| 8200 | #endif |
| 8201 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 8202 | /* 1st cmd in backgrounded pipe |
| 8203 | * should have its stdin /dev/null'ed */ |
| 8204 | close(0); |
| 8205 | if (open(bb_dev_null, O_RDONLY)) |
| 8206 | xopen("/", O_RDONLY); |
| 8207 | } else { |
| 8208 | xmove_fd(next_infd, 0); |
| 8209 | } |
| 8210 | xmove_fd(pipefds.wr, 1); |
| 8211 | if (pipefds.rd > 1) |
| 8212 | close(pipefds.rd); |
| 8213 | /* Like bash, explicit redirects override pipes, |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8214 | * and the pipe fd (fd#1) is available for dup'ing: |
| 8215 | * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr |
| 8216 | * of cmd1 goes into pipe. |
| 8217 | */ |
| 8218 | if (setup_redirects(command, NULL)) { |
| 8219 | /* Happens when redir file can't be opened: |
| 8220 | * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ' |
| 8221 | * FOO |
| 8222 | * hush: can't open '/qwe/rty': No such file or directory |
| 8223 | * BAZ |
| 8224 | * (echo BAR is not executed, it hits _exit(1) below) |
| 8225 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8226 | _exit(1); |
Denys Vlasenko | 869994c | 2016-08-20 15:16:00 +0200 | [diff] [blame] | 8227 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8228 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8229 | /* Stores to nommu_save list of env vars putenv'ed |
| 8230 | * (NOMMU, on MMU we don't need that) */ |
| 8231 | /* cast away volatility... */ |
| 8232 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 8233 | /* pseudo_exec() does not return */ |
| 8234 | } |
| 8235 | |
| 8236 | /* parent or error */ |
| 8237 | #if ENABLE_HUSH_FAST |
| 8238 | G.count_SIGCHLD++; |
| 8239 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 8240 | #endif |
| 8241 | enable_restore_tty_pgrp_on_exit(); |
| 8242 | #if !BB_MMU |
| 8243 | /* Clean up after vforked child */ |
| 8244 | free(nommu_save.argv); |
| 8245 | free(nommu_save.argv_from_re_execing); |
| 8246 | unset_vars(nommu_save.new_env); |
| 8247 | add_vars(nommu_save.old_vars); |
| 8248 | #endif |
| 8249 | free(argv_expanded); |
| 8250 | argv_expanded = NULL; |
| 8251 | if (command->pid < 0) { /* [v]fork failed */ |
| 8252 | /* Clearly indicate, was it fork or vfork */ |
| 8253 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 8254 | } else { |
| 8255 | pi->alive_cmds++; |
| 8256 | #if ENABLE_HUSH_JOB |
| 8257 | /* Second and next children need to know pid of first one */ |
| 8258 | if (pi->pgrp < 0) |
| 8259 | pi->pgrp = command->pid; |
| 8260 | #endif |
| 8261 | } |
| 8262 | |
| 8263 | if (cmd_no > 1) |
| 8264 | close(next_infd); |
| 8265 | if (cmd_no < pi->num_cmds) |
| 8266 | close(pipefds.wr); |
| 8267 | /* Pass read (output) pipe end to next iteration */ |
| 8268 | next_infd = pipefds.rd; |
| 8269 | } |
| 8270 | |
| 8271 | if (!pi->alive_cmds) { |
| 8272 | debug_leave(); |
| 8273 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 8274 | return 1; |
| 8275 | } |
| 8276 | |
| 8277 | debug_leave(); |
| 8278 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 8279 | return -1; |
| 8280 | } |
| 8281 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8282 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 8283 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 8284 | static int run_list(struct pipe *pi) |
| 8285 | { |
| 8286 | #if ENABLE_HUSH_CASE |
| 8287 | char *case_word = NULL; |
| 8288 | #endif |
| 8289 | #if ENABLE_HUSH_LOOPS |
| 8290 | struct pipe *loop_top = NULL; |
| 8291 | char **for_lcur = NULL; |
| 8292 | char **for_list = NULL; |
| 8293 | #endif |
| 8294 | smallint last_followup; |
| 8295 | smalluint rcode; |
| 8296 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 8297 | smalluint cond_code = 0; |
| 8298 | #else |
| 8299 | enum { cond_code = 0 }; |
| 8300 | #endif |
| 8301 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 8302 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8303 | smallint last_rword; /* ditto */ |
| 8304 | #endif |
| 8305 | |
| 8306 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 8307 | debug_enter(); |
| 8308 | |
| 8309 | #if ENABLE_HUSH_LOOPS |
| 8310 | /* Check syntax for "for" */ |
Denys Vlasenko | 0d6a4ec | 2010-12-18 01:34:49 +0100 | [diff] [blame] | 8311 | { |
| 8312 | struct pipe *cpipe; |
| 8313 | for (cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 8314 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 8315 | continue; |
| 8316 | /* current word is FOR or IN (BOLD in comments below) */ |
| 8317 | if (cpipe->next == NULL) { |
| 8318 | syntax_error("malformed for"); |
| 8319 | debug_leave(); |
| 8320 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8321 | return 1; |
| 8322 | } |
| 8323 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 8324 | if (cpipe->next->res_word == RES_DO) |
| 8325 | continue; |
| 8326 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 8327 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 8328 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 8329 | ) { |
| 8330 | syntax_error("malformed for"); |
| 8331 | debug_leave(); |
| 8332 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 8333 | return 1; |
| 8334 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8335 | } |
| 8336 | } |
| 8337 | #endif |
| 8338 | |
| 8339 | /* Past this point, all code paths should jump to ret: label |
| 8340 | * in order to return, no direct "return" statements please. |
| 8341 | * This helps to ensure that no memory is leaked. */ |
| 8342 | |
| 8343 | #if ENABLE_HUSH_JOB |
| 8344 | G.run_list_level++; |
| 8345 | #endif |
| 8346 | |
| 8347 | #if HAS_KEYWORDS |
| 8348 | rword = RES_NONE; |
| 8349 | last_rword = RES_XXXX; |
| 8350 | #endif |
| 8351 | last_followup = PIPE_SEQ; |
| 8352 | rcode = G.last_exitcode; |
| 8353 | |
| 8354 | /* Go through list of pipes, (maybe) executing them. */ |
| 8355 | 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] | 8356 | int r; |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8357 | int sv_errexit_depth; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8358 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8359 | if (G.flag_SIGINT) |
| 8360 | break; |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 8361 | if (G_flag_return_in_progress == 1) |
| 8362 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8363 | |
| 8364 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 8365 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 8366 | rword, cond_code, last_rword); |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8367 | |
| 8368 | sv_errexit_depth = G.errexit_depth; |
| 8369 | if (IF_HAS_KEYWORDS(rword == RES_IF || rword == RES_ELIF ||) |
| 8370 | pi->followup != PIPE_SEQ |
| 8371 | ) { |
| 8372 | G.errexit_depth++; |
| 8373 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8374 | #if ENABLE_HUSH_LOOPS |
| 8375 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 8376 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 8377 | ) { |
| 8378 | /* start of a loop: remember where loop starts */ |
| 8379 | loop_top = pi; |
| 8380 | G.depth_of_loop++; |
| 8381 | } |
| 8382 | #endif |
| 8383 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 8384 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 8385 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 8386 | || (rcode != 0 && last_followup == PIPE_AND) |
| 8387 | ) { |
| 8388 | /* It is "<true> || CMD" or "<false> && CMD" |
| 8389 | * and we should not execute CMD */ |
| 8390 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 8391 | last_followup = pi->followup; |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8392 | goto dont_check_jobs_but_continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8393 | } |
| 8394 | } |
| 8395 | last_followup = pi->followup; |
| 8396 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 8397 | #if ENABLE_HUSH_IF |
| 8398 | if (cond_code) { |
| 8399 | if (rword == RES_THEN) { |
| 8400 | /* if false; then ... fi has exitcode 0! */ |
| 8401 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8402 | /* "if <false> THEN cmd": skip cmd */ |
| 8403 | continue; |
| 8404 | } |
| 8405 | } else { |
| 8406 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 8407 | /* "if <true> then ... ELSE/ELIF cmd": |
| 8408 | * skip cmd and all following ones */ |
| 8409 | break; |
| 8410 | } |
| 8411 | } |
| 8412 | #endif |
| 8413 | #if ENABLE_HUSH_LOOPS |
| 8414 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 8415 | if (!for_lcur) { |
| 8416 | /* first loop through for */ |
| 8417 | |
| 8418 | static const char encoded_dollar_at[] ALIGN1 = { |
| 8419 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 8420 | }; /* encoded representation of "$@" */ |
| 8421 | static const char *const encoded_dollar_at_argv[] = { |
| 8422 | encoded_dollar_at, NULL |
| 8423 | }; /* argv list with one element: "$@" */ |
| 8424 | char **vals; |
| 8425 | |
| 8426 | vals = (char**)encoded_dollar_at_argv; |
| 8427 | if (pi->next->res_word == RES_IN) { |
| 8428 | /* if no variable values after "in" we skip "for" */ |
| 8429 | if (!pi->next->cmds[0].argv) { |
| 8430 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8431 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 8432 | break; |
| 8433 | } |
| 8434 | vals = pi->next->cmds[0].argv; |
| 8435 | } /* else: "for var; do..." -> assume "$@" list */ |
| 8436 | /* create list of variable values */ |
| 8437 | debug_print_strings("for_list made from", vals); |
| 8438 | for_list = expand_strvec_to_strvec(vals); |
| 8439 | for_lcur = for_list; |
| 8440 | debug_print_strings("for_list", for_list); |
| 8441 | } |
| 8442 | if (!*for_lcur) { |
| 8443 | /* "for" loop is over, clean up */ |
| 8444 | free(for_list); |
| 8445 | for_list = NULL; |
| 8446 | for_lcur = NULL; |
| 8447 | break; |
| 8448 | } |
| 8449 | /* Insert next value from for_lcur */ |
| 8450 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8451 | 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] | 8452 | continue; |
| 8453 | } |
| 8454 | if (rword == RES_IN) { |
| 8455 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 8456 | } |
| 8457 | if (rword == RES_DONE) { |
| 8458 | continue; /* "done" has no cmds too */ |
| 8459 | } |
| 8460 | #endif |
| 8461 | #if ENABLE_HUSH_CASE |
| 8462 | if (rword == RES_CASE) { |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8463 | debug_printf_exec("CASE cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8464 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8465 | unbackslash(case_word); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8466 | continue; |
| 8467 | } |
| 8468 | if (rword == RES_MATCH) { |
| 8469 | char **argv; |
| 8470 | |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8471 | debug_printf_exec("MATCH cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8472 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 8473 | break; |
| 8474 | /* all prev words didn't match, does this one match? */ |
| 8475 | argv = pi->cmds->argv; |
| 8476 | while (*argv) { |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8477 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8478 | /* TODO: which FNM_xxx flags to use? */ |
| 8479 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
Denys Vlasenko | bd43c67 | 2017-07-05 23:12:15 +0200 | [diff] [blame] | 8480 | 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] | 8481 | free(pattern); |
| 8482 | if (cond_code == 0) { /* match! we will execute this branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8483 | free(case_word); |
| 8484 | case_word = NULL; /* make future "word)" stop */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8485 | break; |
| 8486 | } |
| 8487 | argv++; |
| 8488 | } |
| 8489 | continue; |
| 8490 | } |
| 8491 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8492 | debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8493 | if (cond_code != 0) |
| 8494 | continue; /* not matched yet, skip this pipe */ |
| 8495 | } |
Denys Vlasenko | aeaee43 | 2016-11-04 20:14:04 +0100 | [diff] [blame] | 8496 | if (rword == RES_ESAC) { |
| 8497 | debug_printf_exec("ESAC cond_code:%d\n", cond_code); |
| 8498 | if (case_word) { |
| 8499 | /* "case" did not match anything: still set $? (to 0) */ |
| 8500 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8501 | } |
| 8502 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8503 | #endif |
| 8504 | /* Just pressing <enter> in shell should check for jobs. |
| 8505 | * OTOH, in non-interactive shell this is useless |
| 8506 | * and only leads to extra job checks */ |
| 8507 | if (pi->num_cmds == 0) { |
| 8508 | if (G_interactive_fd) |
| 8509 | goto check_jobs_and_continue; |
| 8510 | continue; |
| 8511 | } |
| 8512 | |
| 8513 | /* After analyzing all keywords and conditions, we decided |
| 8514 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 8515 | * after run_pipe to collect any background children, |
| 8516 | * even if list execution is to be stopped. */ |
| 8517 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8518 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8519 | G.flag_break_continue = 0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8520 | #endif |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8521 | rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */ |
| 8522 | if (r != -1) { |
| 8523 | /* We ran a builtin, function, or group. |
| 8524 | * rcode is already known |
| 8525 | * and we don't need to wait for anything. */ |
| 8526 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 8527 | G.last_exitcode = rcode; |
| 8528 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8529 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8530 | /* Was it "break" or "continue"? */ |
| 8531 | if (G.flag_break_continue) { |
| 8532 | smallint fbc = G.flag_break_continue; |
| 8533 | /* We might fall into outer *loop*, |
| 8534 | * don't want to break it too */ |
| 8535 | if (loop_top) { |
| 8536 | G.depth_break_continue--; |
| 8537 | if (G.depth_break_continue == 0) |
| 8538 | G.flag_break_continue = 0; |
| 8539 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 8540 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 8541 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8542 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8543 | break; |
| 8544 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8545 | /* "continue": simulate end of loop */ |
| 8546 | rword = RES_DONE; |
| 8547 | continue; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8548 | } |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8549 | #endif |
| 8550 | if (G_flag_return_in_progress == 1) { |
| 8551 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 8552 | break; |
| 8553 | } |
| 8554 | } else if (pi->followup == PIPE_BG) { |
| 8555 | /* What does bash do with attempts to background builtins? */ |
| 8556 | /* even bash 3.2 doesn't do that well with nested bg: |
| 8557 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 8558 | * I'm NOT treating inner &'s as jobs */ |
| 8559 | #if ENABLE_HUSH_JOB |
| 8560 | if (G.run_list_level == 1) |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 8561 | insert_job_into_table(pi); |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8562 | #endif |
| 8563 | /* Last command's pid goes to $! */ |
| 8564 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 8565 | G.last_bg_pid_exitcode = 0; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8566 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 8567 | /* 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] | 8568 | rcode = EXIT_SUCCESS; |
| 8569 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8570 | } else { |
| 8571 | #if ENABLE_HUSH_JOB |
| 8572 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 8573 | /* Waits for completion, then fg's main shell */ |
| 8574 | rcode = checkjobs_and_fg_shell(pi); |
| 8575 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8576 | goto check_traps; |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8577 | } |
Denys Vlasenko | 6c635d6 | 2016-11-08 20:26:11 +0100 | [diff] [blame] | 8578 | #endif |
| 8579 | /* This one just waits for completion */ |
| 8580 | rcode = checkjobs(pi, 0 /*(no pid to wait for)*/); |
| 8581 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 8582 | check_traps: |
Denys Vlasenko | 5cc9bf6 | 2016-11-08 17:34:44 +0100 | [diff] [blame] | 8583 | G.last_exitcode = rcode; |
| 8584 | check_and_run_traps(); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8585 | } |
| 8586 | |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8587 | /* Handle "set -e" */ |
| 8588 | if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) { |
| 8589 | debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth); |
| 8590 | if (G.errexit_depth == 0) |
| 8591 | hush_exit(rcode); |
| 8592 | } |
| 8593 | G.errexit_depth = sv_errexit_depth; |
| 8594 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8595 | /* Analyze how result affects subsequent commands */ |
| 8596 | #if ENABLE_HUSH_IF |
| 8597 | if (rword == RES_IF || rword == RES_ELIF) |
| 8598 | cond_code = rcode; |
| 8599 | #endif |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8600 | check_jobs_and_continue: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 8601 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8602 | dont_check_jobs_but_continue: ; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8603 | #if ENABLE_HUSH_LOOPS |
| 8604 | /* Beware of "while false; true; do ..."! */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8605 | if (pi->next |
| 8606 | && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE) |
Denys Vlasenko | 56a3b82 | 2011-06-01 12:47:07 +0200 | [diff] [blame] | 8607 | /* check for RES_DONE is needed for "while ...; do \n done" case */ |
Denys Vlasenko | 00ae989 | 2011-05-31 17:35:45 +0200 | [diff] [blame] | 8608 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8609 | if (rword == RES_WHILE) { |
| 8610 | if (rcode) { |
| 8611 | /* "while false; do...done" - exitcode 0 */ |
| 8612 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 8613 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
Denys Vlasenko | 3beab83 | 2013-04-07 18:16:58 +0200 | [diff] [blame] | 8614 | break; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8615 | } |
| 8616 | } |
| 8617 | if (rword == RES_UNTIL) { |
| 8618 | if (!rcode) { |
| 8619 | debug_printf_exec(": until expr is true: breaking\n"); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8620 | break; |
| 8621 | } |
| 8622 | } |
| 8623 | } |
| 8624 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8625 | } /* for (pi) */ |
| 8626 | |
| 8627 | #if ENABLE_HUSH_JOB |
| 8628 | G.run_list_level--; |
| 8629 | #endif |
| 8630 | #if ENABLE_HUSH_LOOPS |
| 8631 | if (loop_top) |
| 8632 | G.depth_of_loop--; |
| 8633 | free(for_list); |
| 8634 | #endif |
| 8635 | #if ENABLE_HUSH_CASE |
| 8636 | free(case_word); |
| 8637 | #endif |
| 8638 | debug_leave(); |
| 8639 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 8640 | return rcode; |
| 8641 | } |
| 8642 | |
| 8643 | /* Select which version we will use */ |
| 8644 | static int run_and_free_list(struct pipe *pi) |
| 8645 | { |
| 8646 | int rcode = 0; |
| 8647 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8648 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 8649 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 8650 | rcode = run_list(pi); |
| 8651 | } |
| 8652 | /* free_pipe_list has the side effect of clearing memory. |
| 8653 | * In the long run that function can be merged with run_list, |
| 8654 | * but doing that now would hobble the debugging effort. */ |
| 8655 | free_pipe_list(pi); |
| 8656 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 8657 | return rcode; |
| 8658 | } |
| 8659 | |
| 8660 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8661 | static void install_sighandlers(unsigned mask) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 8662 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8663 | sighandler_t old_handler; |
| 8664 | unsigned sig = 0; |
| 8665 | while ((mask >>= 1) != 0) { |
| 8666 | sig++; |
| 8667 | if (!(mask & 1)) |
| 8668 | continue; |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8669 | old_handler = install_sighandler(sig, pick_sighandler(sig)); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8670 | /* POSIX allows shell to re-enable SIGCHLD |
| 8671 | * even if it was SIG_IGN on entry. |
| 8672 | * Therefore we skip IGN check for it: |
| 8673 | */ |
| 8674 | if (sig == SIGCHLD) |
| 8675 | continue; |
Denys Vlasenko | 49e6bf2 | 2017-08-04 14:28:16 +0200 | [diff] [blame] | 8676 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 8677 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 8678 | */ |
| 8679 | //if (sig == SIGHUP) continue; - TODO? |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8680 | if (old_handler == SIG_IGN) { |
| 8681 | /* oops... restore back to IGN, and record this fact */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8682 | install_sighandler(sig, old_handler); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 8683 | #if ENABLE_HUSH_TRAP |
| 8684 | if (!G_traps) |
| 8685 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
| 8686 | free(G_traps[sig]); |
| 8687 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 8688 | #endif |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8689 | } |
| 8690 | } |
| 8691 | } |
| 8692 | |
| 8693 | /* Called a few times only (or even once if "sh -c") */ |
| 8694 | static void install_special_sighandlers(void) |
| 8695 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8696 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8697 | |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8698 | /* Which signals are shell-special? */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8699 | mask = (1 << SIGQUIT) | (1 << SIGCHLD); |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8700 | if (G_interactive_fd) { |
| 8701 | mask |= SPECIAL_INTERACTIVE_SIGS; |
| 8702 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8703 | mask |= SPECIAL_JOBSTOP_SIGS; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8704 | } |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8705 | /* Careful, do not re-install handlers we already installed */ |
| 8706 | if (G.special_sig_mask != mask) { |
| 8707 | unsigned diff = mask & ~G.special_sig_mask; |
| 8708 | G.special_sig_mask = mask; |
| 8709 | install_sighandlers(diff); |
| 8710 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8711 | } |
| 8712 | |
| 8713 | #if ENABLE_HUSH_JOB |
| 8714 | /* helper */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8715 | /* Set handlers to restore tty pgrp and exit */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8716 | static void install_fatal_sighandlers(void) |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8717 | { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8718 | unsigned mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8719 | |
| 8720 | /* We will restore tty pgrp on these signals */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8721 | mask = 0 |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8722 | /*+ (1 << SIGILL ) * HUSH_DEBUG*/ |
| 8723 | /*+ (1 << SIGFPE ) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8724 | + (1 << SIGBUS ) * HUSH_DEBUG |
| 8725 | + (1 << SIGSEGV) * HUSH_DEBUG |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 8726 | /*+ (1 << SIGTRAP) * HUSH_DEBUG*/ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8727 | + (1 << SIGABRT) |
| 8728 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 8729 | + (1 << SIGPIPE) |
| 8730 | + (1 << SIGALRM) |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8731 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs. |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8732 | * if we aren't interactive... but in this case |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8733 | * we never want to restore pgrp on exit, and this fn is not called |
| 8734 | */ |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8735 | /*+ (1 << SIGHUP )*/ |
| 8736 | /*+ (1 << SIGTERM)*/ |
| 8737 | /*+ (1 << SIGINT )*/ |
| 8738 | ; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8739 | G_fatal_sig_mask = mask; |
Denys Vlasenko | 54e9e12 | 2011-05-09 00:52:15 +0200 | [diff] [blame] | 8740 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8741 | install_sighandlers(mask); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 8742 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 8743 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 8744 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8745 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8746 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8747 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8748 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8749 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 8750 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8751 | break; |
| 8752 | case 'x': |
| 8753 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 8754 | break; |
| 8755 | case 'o': |
| 8756 | if (!o_opt) { |
| 8757 | /* "set -+o" without parameter. |
| 8758 | * in bash, set -o produces this output: |
| 8759 | * pipefail off |
| 8760 | * and set +o: |
| 8761 | * set +o pipefail |
| 8762 | * We always use the second form. |
| 8763 | */ |
| 8764 | const char *p = o_opt_strings; |
| 8765 | idx = 0; |
| 8766 | while (*p) { |
| 8767 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 8768 | idx++; |
| 8769 | p += strlen(p) + 1; |
| 8770 | } |
| 8771 | break; |
| 8772 | } |
| 8773 | idx = index_in_strings(o_opt_strings, o_opt); |
| 8774 | if (idx >= 0) { |
| 8775 | G.o_opt[idx] = state; |
| 8776 | break; |
| 8777 | } |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8778 | case 'e': |
| 8779 | G.o_opt[OPT_O_ERREXIT] = state; |
| 8780 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8781 | default: |
| 8782 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8783 | } |
| 8784 | return EXIT_SUCCESS; |
| 8785 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8786 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 8787 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 8788 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8789 | { |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8790 | enum { |
| 8791 | OPT_login = (1 << 0), |
| 8792 | }; |
| 8793 | unsigned flags; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8794 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8795 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8796 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8797 | struct variable *cur_var; |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8798 | struct variable *shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 8799 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 8800 | INIT_G(); |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8801 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8802 | G.last_exitcode = EXIT_SUCCESS; |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8803 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 8804 | #if ENABLE_HUSH_FAST |
| 8805 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
| 8806 | #endif |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8807 | #if !BB_MMU |
| 8808 | G.argv0_for_re_execing = argv[0]; |
| 8809 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8810 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8811 | shell_ver = xzalloc(sizeof(*shell_ver)); |
| 8812 | shell_ver->flg_export = 1; |
| 8813 | shell_ver->flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 8814 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 8815 | * therefore we xstrdup: */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8816 | shell_ver->varstr = xstrdup(hush_version_str); |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8817 | /* Create shell local variables from the values |
| 8818 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 8819 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8820 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8821 | G.top_var = shell_ver; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8822 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 8823 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8824 | if (e) while (*e) { |
| 8825 | char *value = strchr(*e, '='); |
| 8826 | if (value) { /* paranoia */ |
| 8827 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 8828 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 8829 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 8830 | cur_var->max_len = strlen(*e); |
| 8831 | cur_var->flg_export = 1; |
| 8832 | } |
| 8833 | e++; |
| 8834 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 8835 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 75eb9d2 | 2010-12-21 21:18:12 +0100 | [diff] [blame] | 8836 | debug_printf_env("putenv '%s'\n", shell_ver->varstr); |
| 8837 | putenv(shell_ver->varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8838 | |
| 8839 | /* Export PWD */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 8840 | set_pwd_var(SETFLAG_EXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8841 | |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 8842 | #if BASH_HOSTNAME_VAR |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8843 | /* Set (but not export) HOSTNAME unless already set */ |
| 8844 | if (!get_local_var_value("HOSTNAME")) { |
| 8845 | struct utsname uts; |
| 8846 | uname(&uts); |
| 8847 | set_local_var_from_halves("HOSTNAME", uts.nodename); |
| 8848 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8849 | /* bash also exports SHLVL and _, |
| 8850 | * and sets (but doesn't export) the following variables: |
| 8851 | * BASH=/bin/bash |
| 8852 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 8853 | * BASH_VERSION='3.2.0(1)-release' |
| 8854 | * HOSTTYPE=i386 |
| 8855 | * MACHTYPE=i386-pc-linux-gnu |
| 8856 | * OSTYPE=linux-gnu |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8857 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8858 | * EUID=<NNNNN> |
| 8859 | * UID=<NNNNN> |
| 8860 | * GROUPS=() |
| 8861 | * LINES=<NNN> |
| 8862 | * COLUMNS=<NNN> |
| 8863 | * BASH_ARGC=() |
| 8864 | * BASH_ARGV=() |
| 8865 | * BASH_LINENO=() |
| 8866 | * BASH_SOURCE=() |
| 8867 | * DIRSTACK=() |
| 8868 | * PIPESTATUS=([0]="0") |
| 8869 | * HISTFILE=/<xxx>/.bash_history |
| 8870 | * HISTFILESIZE=500 |
| 8871 | * HISTSIZE=500 |
| 8872 | * MAILCHECK=60 |
| 8873 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 8874 | * SHELL=/bin/bash |
| 8875 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 8876 | * TERM=dumb |
| 8877 | * OPTERR=1 |
| 8878 | * OPTIND=1 |
| 8879 | * IFS=$' \t\n' |
| 8880 | * PS1='\s-\v\$ ' |
| 8881 | * PS2='> ' |
| 8882 | * PS4='+ ' |
| 8883 | */ |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 8884 | #endif |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 8885 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 8886 | #if ENABLE_FEATURE_EDITING |
Denys Vlasenko | e45af7a | 2011-09-04 16:15:24 +0200 | [diff] [blame] | 8887 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 8888 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 8889 | |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 8890 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 8891 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 8892 | |
Denys Vlasenko | e9abe75 | 2016-08-19 20:15:26 +0200 | [diff] [blame] | 8893 | die_func = restore_ttypgrp_and__exit; |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 8894 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 8895 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8896 | * install_special_sighandlers() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 8897 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8898 | * 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] | 8899 | * in order to intercept (more) signals. |
| 8900 | */ |
| 8901 | |
| 8902 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8903 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8904 | flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8905 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8906 | while (1) { |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 8907 | opt = getopt(argc, argv, "+c:exinsl" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8908 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 8909 | "<:$:R:V:" |
| 8910 | # if ENABLE_HUSH_FUNCTIONS |
| 8911 | "F:" |
| 8912 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8913 | #endif |
| 8914 | ); |
| 8915 | if (opt <= 0) |
| 8916 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 8917 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8918 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8919 | /* Possibilities: |
| 8920 | * sh ... -c 'script' |
| 8921 | * sh ... -c 'script' ARG0 [ARG1...] |
| 8922 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8923 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8924 | * "" needs to be replaced with NULL |
| 8925 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8926 | * Note: the form without ARG0 never happens: |
| 8927 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8928 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8929 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 8930 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8931 | G.root_ppid = getppid(); |
| 8932 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8933 | G.global_argv = argv + optind; |
| 8934 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8935 | if (builtin_argc) { |
| 8936 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 8937 | const struct built_in_command *x; |
| 8938 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8939 | install_special_sighandlers(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8940 | x = find_builtin(optarg); |
| 8941 | if (x) { /* paranoia */ |
| 8942 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 8943 | G.global_argv += builtin_argc; |
| 8944 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 8ee2ada | 2011-02-07 02:03:51 +0100 | [diff] [blame] | 8945 | fflush_all(); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8946 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8947 | } |
| 8948 | goto final_return; |
| 8949 | } |
| 8950 | if (!G.global_argv[0]) { |
| 8951 | /* -c 'script' (no params): prevent empty $0 */ |
| 8952 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 8953 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 8954 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8955 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8956 | install_special_sighandlers(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8957 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8958 | goto final_return; |
| 8959 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 8960 | /* Well, we cannot just declare interactiveness, |
| 8961 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8962 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 8963 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 8964 | case 's': |
| 8965 | /* "-s" means "read from stdin", but this is how we always |
| 8966 | * operate, so simply do nothing here. */ |
| 8967 | break; |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 8968 | case 'l': |
| 8969 | flags |= OPT_login; |
| 8970 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 8971 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8972 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 8973 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 8974 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8975 | case '$': { |
| 8976 | unsigned long long empty_trap_mask; |
| 8977 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8978 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 8979 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 8980 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 8981 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 8982 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 8983 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8984 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8985 | optarg++; |
| 8986 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8987 | optarg++; |
| 8988 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 8989 | if (empty_trap_mask != 0) { |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 8990 | IF_HUSH_TRAP(int sig;) |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 8991 | install_special_sighandlers(); |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 8992 | # if ENABLE_HUSH_TRAP |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 8993 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8994 | for (sig = 1; sig < NSIG; sig++) { |
| 8995 | if (empty_trap_mask & (1LL << sig)) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 8996 | G_traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 8997 | install_sighandler(sig, SIG_IGN); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8998 | } |
| 8999 | } |
Denys Vlasenko | 4ee824f | 2017-07-03 01:22:13 +0200 | [diff] [blame] | 9000 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9001 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9002 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9003 | optarg++; |
| 9004 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9005 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 9006 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9007 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9008 | case 'R': |
| 9009 | case 'V': |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9010 | set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9011 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 9012 | # if ENABLE_HUSH_FUNCTIONS |
| 9013 | case 'F': { |
| 9014 | struct function *funcp = new_function(optarg); |
| 9015 | /* funcp->name is already set to optarg */ |
| 9016 | /* funcp->body is set to NULL. It's a special case. */ |
| 9017 | funcp->body_as_string = argv[optind]; |
| 9018 | optind++; |
| 9019 | break; |
| 9020 | } |
| 9021 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 9022 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9023 | case 'n': |
| 9024 | case 'x': |
Denys Vlasenko | 9fda609 | 2017-07-14 13:36:48 +0200 | [diff] [blame] | 9025 | case 'e': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 9026 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 9027 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9028 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9029 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9030 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 9031 | " or: sh -c command [args]...\n\n"); |
| 9032 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9033 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 9034 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 9035 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9036 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9037 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9038 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9039 | /* Skip options. Try "hush -l": $1 should not be "-l"! */ |
| 9040 | G.global_argc = argc - (optind - 1); |
| 9041 | G.global_argv = argv + (optind - 1); |
| 9042 | G.global_argv[0] = argv[0]; |
| 9043 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9044 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9045 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 9046 | G.root_ppid = getppid(); |
| 9047 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9048 | |
| 9049 | /* If we are login shell... */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9050 | if (flags & OPT_login) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9051 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9052 | debug_printf("sourcing /etc/profile\n"); |
| 9053 | input = fopen_for_read("/etc/profile"); |
| 9054 | if (input != NULL) { |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9055 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9056 | install_special_sighandlers(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9057 | parse_and_run_file(input); |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9058 | fclose_and_forget(input); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9059 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9060 | /* bash: after sourcing /etc/profile, |
| 9061 | * tries to source (in the given order): |
| 9062 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9063 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9064 | * bash also sources ~/.bash_logout on exit. |
| 9065 | * If called as sh, skips .bash_XXX files. |
| 9066 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 9067 | } |
| 9068 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9069 | if (G.global_argv[1]) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9070 | FILE *input; |
| 9071 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 9072 | * "bash <script>" (which is never interactive (unless -i?)) |
| 9073 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9074 | * If called as sh, does the same but with $ENV. |
Denys Vlasenko | 2eb0a7e | 2016-10-27 11:28:59 +0200 | [diff] [blame] | 9075 | * Also NB, per POSIX, $ENV should undergo parameter expansion. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9076 | */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9077 | G.global_argc--; |
| 9078 | G.global_argv++; |
| 9079 | debug_printf("running script '%s'\n", G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9080 | xfunc_error_retval = 127; /* for "hush /does/not/exist" case */ |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9081 | input = xfopen_for_read(G.global_argv[0]); |
Denys Vlasenko | b7adf7a | 2016-10-25 17:00:13 +0200 | [diff] [blame] | 9082 | xfunc_error_retval = 1; |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9083 | remember_FILE(input); |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9084 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9085 | parse_and_run_file(input); |
| 9086 | #if ENABLE_FEATURE_CLEAN_UP |
Denys Vlasenko | 7b25b1c | 2016-08-20 15:58:34 +0200 | [diff] [blame] | 9087 | fclose_and_forget(input); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9088 | #endif |
| 9089 | goto final_return; |
| 9090 | } |
| 9091 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9092 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9093 | * NB: don't forget to (re)run install_special_sighandlers() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 9094 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9095 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 9096 | /* A shell is interactive if the '-i' flag was given, |
| 9097 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 9098 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9099 | * no arguments remaining or the -s flag given |
| 9100 | * standard input is a terminal |
| 9101 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9102 | * Refer to Posix.2, the description of the 'sh' utility. |
| 9103 | */ |
| 9104 | #if ENABLE_HUSH_JOB |
| 9105 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9106 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 9107 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 9108 | if (G_saved_tty_pgrp < 0) |
| 9109 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9110 | |
| 9111 | /* try to dup stdin to high fd#, >= 255 */ |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9112 | G_interactive_fd = fcntl_F_DUPFD(STDIN_FILENO, 254); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9113 | if (G_interactive_fd < 0) { |
| 9114 | /* try to dup to any fd */ |
| 9115 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9116 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9117 | /* give up */ |
| 9118 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9119 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 9120 | } |
| 9121 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9122 | // TODO: track & disallow any attempts of user |
| 9123 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9124 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9125 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9126 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9127 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9128 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9129 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9130 | /* If we were run as 'hush &', sleep until we are |
| 9131 | * in the foreground (tty pgrp == our pgrp). |
| 9132 | * If we get started under a job aware app (like bash), |
| 9133 | * make sure we are now in charge so we don't fight over |
| 9134 | * who gets the foreground */ |
| 9135 | while (1) { |
| 9136 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9137 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 9138 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9139 | break; |
| 9140 | /* send TTIN to ourself (should stop us) */ |
| 9141 | kill(- shell_pgrp, SIGTTIN); |
| 9142 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9143 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9144 | |
Denys Vlasenko | f58f705 | 2011-05-12 02:10:33 +0200 | [diff] [blame] | 9145 | /* Install more signal handlers */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9146 | install_special_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9147 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 9148 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9149 | /* Set other signals to restore saved_tty_pgrp */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9150 | install_fatal_sighandlers(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 9151 | /* Put ourselves in our own process group |
| 9152 | * (bash, too, does this only if ctty is available) */ |
| 9153 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 9154 | /* Grab control of the terminal */ |
| 9155 | tcsetpgrp(G_interactive_fd, getpid()); |
| 9156 | } |
Denys Vlasenko | 550bf5b | 2015-10-09 16:42:57 +0200 | [diff] [blame] | 9157 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9158 | |
| 9159 | # if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0 |
| 9160 | { |
| 9161 | const char *hp = get_local_var_value("HISTFILE"); |
| 9162 | if (!hp) { |
| 9163 | hp = get_local_var_value("HOME"); |
| 9164 | if (hp) |
| 9165 | hp = concat_path_file(hp, ".hush_history"); |
| 9166 | } else { |
| 9167 | hp = xstrdup(hp); |
| 9168 | } |
| 9169 | if (hp) { |
| 9170 | G.line_input_state->hist_file = hp; |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 9171 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 9172 | } |
| 9173 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 9174 | hp = get_local_var_value("HISTFILESIZE"); |
| 9175 | G.line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 9176 | # endif |
| 9177 | } |
| 9178 | # endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9179 | } else { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9180 | install_special_sighandlers(); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 9181 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9182 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9183 | /* No job control compiled in, only prompt/line editing */ |
| 9184 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denys Vlasenko | 2db7461 | 2017-07-07 22:07:28 +0200 | [diff] [blame] | 9185 | G_interactive_fd = fcntl_F_DUPFD(STDIN_FILENO, 254); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9186 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9187 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9188 | G_interactive_fd = dup(STDIN_FILENO); |
| 9189 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9190 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9191 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 9192 | } |
| 9193 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 9194 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9195 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9196 | } |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9197 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9198 | #else |
| 9199 | /* We have interactiveness code disabled */ |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9200 | install_special_sighandlers(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9201 | #endif |
| 9202 | /* bash: |
| 9203 | * if interactive but not a login shell, sources ~/.bashrc |
| 9204 | * (--norc turns this off, --rcfile <file> overrides) |
| 9205 | */ |
| 9206 | |
| 9207 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 9208 | /* note: ash and hush share this string */ |
| 9209 | printf("\n\n%s %s\n" |
| 9210 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 9211 | "\n", |
| 9212 | bb_banner, |
| 9213 | "hush - the humble shell" |
| 9214 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 9217 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9218 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 9219 | final_return: |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9220 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 9221 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 9222 | |
| 9223 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9224 | /* |
| 9225 | * Built-ins |
| 9226 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9227 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9228 | { |
| 9229 | return 0; |
| 9230 | } |
| 9231 | |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9232 | #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] | 9233 | 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] | 9234 | { |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9235 | int argc = string_array_len(argv); |
| 9236 | return applet_main_func(argc, argv); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9237 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9238 | #endif |
Kang-Che Sung | 027d3ab | 2017-01-11 14:18:15 +0100 | [diff] [blame] | 9239 | #if ENABLE_HUSH_TEST || BASH_TEST2 |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 9240 | static int FAST_FUNC builtin_test(char **argv) |
| 9241 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9242 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9243 | } |
Denys Vlasenko | 265062d | 2017-01-10 15:13:30 +0100 | [diff] [blame] | 9244 | #endif |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9245 | #if ENABLE_HUSH_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9246 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9247 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9248 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9249 | } |
Denys Vlasenko | 1cc6804 | 2017-01-09 17:10:04 +0100 | [diff] [blame] | 9250 | #endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 9251 | #if ENABLE_HUSH_PRINTF |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9252 | static int FAST_FUNC builtin_printf(char **argv) |
| 9253 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 9254 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 9255 | } |
| 9256 | #endif |
| 9257 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9258 | #if ENABLE_HUSH_HELP |
| 9259 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
| 9260 | { |
| 9261 | const struct built_in_command *x; |
| 9262 | |
| 9263 | printf( |
| 9264 | "Built-in commands:\n" |
| 9265 | "------------------\n"); |
| 9266 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
| 9267 | if (x->b_descr) |
| 9268 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
| 9269 | } |
| 9270 | return EXIT_SUCCESS; |
| 9271 | } |
| 9272 | #endif |
| 9273 | |
| 9274 | #if MAX_HISTORY && ENABLE_FEATURE_EDITING |
| 9275 | static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM) |
| 9276 | { |
| 9277 | show_history(G.line_input_state); |
| 9278 | return EXIT_SUCCESS; |
| 9279 | } |
| 9280 | #endif |
| 9281 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9282 | static char **skip_dash_dash(char **argv) |
| 9283 | { |
| 9284 | argv++; |
| 9285 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 9286 | argv++; |
| 9287 | return argv; |
| 9288 | } |
| 9289 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9290 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9291 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9292 | const char *newdir; |
| 9293 | |
| 9294 | argv = skip_dash_dash(argv); |
| 9295 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9296 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9297 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9298 | * bash says "bash: cd: HOME not set" and does nothing |
| 9299 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 9300 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 9301 | const char *home = get_local_var_value("HOME"); |
| 9302 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 9303 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9304 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 9305 | /* Mimic bash message exactly */ |
| 9306 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9307 | return EXIT_FAILURE; |
| 9308 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 9309 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 9310 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 9311 | * set it again, but do not export. bash does the same. |
| 9312 | */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9313 | set_pwd_var(/*flag:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9314 | return EXIT_SUCCESS; |
| 9315 | } |
| 9316 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9317 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
| 9318 | { |
| 9319 | puts(get_cwd(0)); |
| 9320 | return EXIT_SUCCESS; |
| 9321 | } |
| 9322 | |
| 9323 | static int FAST_FUNC builtin_eval(char **argv) |
| 9324 | { |
| 9325 | int rcode = EXIT_SUCCESS; |
| 9326 | |
| 9327 | argv = skip_dash_dash(argv); |
| 9328 | if (*argv) { |
| 9329 | char *str = expand_strvec_to_string(argv); |
| 9330 | /* bash: |
| 9331 | * eval "echo Hi; done" ("done" is syntax error): |
| 9332 | * "echo Hi" will not execute too. |
| 9333 | */ |
| 9334 | parse_and_run_string(str); |
| 9335 | free(str); |
| 9336 | rcode = G.last_exitcode; |
| 9337 | } |
| 9338 | return rcode; |
| 9339 | } |
| 9340 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9341 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9342 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9343 | argv = skip_dash_dash(argv); |
| 9344 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9345 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9346 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 9347 | /* Careful: we can end up here after [v]fork. Do not restore |
| 9348 | * tty pgrp then, only top-level shell process does that */ |
| 9349 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 9350 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 9351 | |
Denys Vlasenko | 5b3d2eb | 2017-07-31 18:02:28 +0200 | [diff] [blame] | 9352 | /* Saved-redirect fds, script fds and G_interactive_fd are still |
| 9353 | * open here. However, they are all CLOEXEC, and execv below |
| 9354 | * closes them. Try interactive "exec ls -l /proc/self/fd", |
| 9355 | * it should show no extra open fds in the "ls" process. |
| 9356 | * If we'd try to run builtins/NOEXECs, this would need improving. |
| 9357 | */ |
| 9358 | //close_saved_fds_and_FILE_fds(); |
| 9359 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 9360 | /* TODO: if exec fails, bash does NOT exit! We do. |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 9361 | * 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] | 9362 | * and tcsetpgrp, and this is inherently racy. |
| 9363 | */ |
| 9364 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9367 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9368 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 9369 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9370 | |
| 9371 | /* interactive bash: |
| 9372 | * # trap "echo EEE" EXIT |
| 9373 | * # exit |
| 9374 | * exit |
| 9375 | * There are stopped jobs. |
| 9376 | * (if there are _stopped_ jobs, running ones don't count) |
| 9377 | * # exit |
| 9378 | * exit |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 9379 | * EEE (then bash exits) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 9380 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 9381 | * 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] | 9382 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 9383 | |
| 9384 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9385 | argv = skip_dash_dash(argv); |
| 9386 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 9387 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9388 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 9389 | xfunc_error_retval = 255; |
| 9390 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 9391 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9392 | } |
| 9393 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9394 | #if ENABLE_HUSH_TYPE |
| 9395 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
| 9396 | static int FAST_FUNC builtin_type(char **argv) |
| 9397 | { |
| 9398 | int ret = EXIT_SUCCESS; |
| 9399 | |
| 9400 | while (*++argv) { |
| 9401 | const char *type; |
| 9402 | char *path = NULL; |
| 9403 | |
| 9404 | if (0) {} /* make conditional compile easier below */ |
| 9405 | /*else if (find_alias(*argv)) |
| 9406 | type = "an alias";*/ |
| 9407 | #if ENABLE_HUSH_FUNCTIONS |
| 9408 | else if (find_function(*argv)) |
| 9409 | type = "a function"; |
| 9410 | #endif |
| 9411 | else if (find_builtin(*argv)) |
| 9412 | type = "a shell builtin"; |
| 9413 | else if ((path = find_in_path(*argv)) != NULL) |
| 9414 | type = path; |
| 9415 | else { |
| 9416 | bb_error_msg("type: %s: not found", *argv); |
| 9417 | ret = EXIT_FAILURE; |
| 9418 | continue; |
| 9419 | } |
| 9420 | |
| 9421 | printf("%s is %s\n", *argv, type); |
| 9422 | free(path); |
| 9423 | } |
| 9424 | |
| 9425 | return ret; |
| 9426 | } |
| 9427 | #endif |
| 9428 | |
| 9429 | #if ENABLE_HUSH_READ |
| 9430 | /* Interruptibility of read builtin in bash |
| 9431 | * (tested on bash-4.2.8 by sending signals (not by ^C)): |
| 9432 | * |
| 9433 | * Empty trap makes read ignore corresponding signal, for any signal. |
| 9434 | * |
| 9435 | * SIGINT: |
| 9436 | * - terminates non-interactive shell; |
| 9437 | * - interrupts read in interactive shell; |
| 9438 | * if it has non-empty trap: |
| 9439 | * - executes trap and returns to command prompt in interactive shell; |
| 9440 | * - executes trap and returns to read in non-interactive shell; |
| 9441 | * SIGTERM: |
| 9442 | * - is ignored (does not interrupt) read in interactive shell; |
| 9443 | * - terminates non-interactive shell; |
| 9444 | * if it has non-empty trap: |
| 9445 | * - executes trap and returns to read; |
| 9446 | * SIGHUP: |
| 9447 | * - terminates shell (regardless of interactivity); |
| 9448 | * if it has non-empty trap: |
| 9449 | * - executes trap and returns to read; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9450 | * SIGCHLD from children: |
| 9451 | * - does not interrupt read regardless of interactivity: |
| 9452 | * try: sleep 1 & read x; echo $x |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9453 | */ |
| 9454 | static int FAST_FUNC builtin_read(char **argv) |
| 9455 | { |
| 9456 | const char *r; |
| 9457 | char *opt_n = NULL; |
| 9458 | char *opt_p = NULL; |
| 9459 | char *opt_t = NULL; |
| 9460 | char *opt_u = NULL; |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9461 | char *opt_d = NULL; /* optimized out if !BASH */ |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9462 | const char *ifs; |
| 9463 | int read_flags; |
| 9464 | |
| 9465 | /* "!": do not abort on errors. |
| 9466 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 9467 | */ |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9468 | read_flags = getopt32(argv, |
| 9469 | #if BASH_READ_D |
| 9470 | "!srn:p:t:u:d:", &opt_n, &opt_p, &opt_t, &opt_u, &opt_d |
| 9471 | #else |
| 9472 | "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u |
| 9473 | #endif |
| 9474 | ); |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9475 | if (read_flags == (uint32_t)-1) |
| 9476 | return EXIT_FAILURE; |
| 9477 | argv += optind; |
| 9478 | ifs = get_local_var_value("IFS"); /* can be NULL */ |
| 9479 | |
| 9480 | again: |
| 9481 | r = shell_builtin_read(set_local_var_from_halves, |
| 9482 | argv, |
| 9483 | ifs, |
| 9484 | read_flags, |
| 9485 | opt_n, |
| 9486 | opt_p, |
| 9487 | opt_t, |
Denys Vlasenko | 1f41c88 | 2017-08-09 13:52:36 +0200 | [diff] [blame] | 9488 | opt_u, |
| 9489 | opt_d |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9490 | ); |
| 9491 | |
| 9492 | if ((uintptr_t)r == 1 && errno == EINTR) { |
| 9493 | unsigned sig = check_and_run_traps(); |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 9494 | if (sig != SIGINT) |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9495 | goto again; |
| 9496 | } |
| 9497 | |
| 9498 | if ((uintptr_t)r > 1) { |
| 9499 | bb_error_msg("%s", r); |
| 9500 | r = (char*)(uintptr_t)1; |
| 9501 | } |
| 9502 | |
| 9503 | return (uintptr_t)r; |
| 9504 | } |
| 9505 | #endif |
| 9506 | |
| 9507 | #if ENABLE_HUSH_UMASK |
| 9508 | static int FAST_FUNC builtin_umask(char **argv) |
| 9509 | { |
| 9510 | int rc; |
| 9511 | mode_t mask; |
| 9512 | |
| 9513 | rc = 1; |
| 9514 | mask = umask(0); |
| 9515 | argv = skip_dash_dash(argv); |
| 9516 | if (argv[0]) { |
| 9517 | mode_t old_mask = mask; |
| 9518 | |
| 9519 | /* numeric umasks are taken as-is */ |
| 9520 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 9521 | if (!isdigit(argv[0][0])) |
| 9522 | mask ^= 0777; |
| 9523 | mask = bb_parse_mode(argv[0], mask); |
| 9524 | if (!isdigit(argv[0][0])) |
| 9525 | mask ^= 0777; |
| 9526 | if ((unsigned)mask > 0777) { |
| 9527 | mask = old_mask; |
| 9528 | /* bash messages: |
| 9529 | * bash: umask: 'q': invalid symbolic mode operator |
| 9530 | * bash: umask: 999: octal number out of range |
| 9531 | */ |
| 9532 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
| 9533 | rc = 0; |
| 9534 | } |
| 9535 | } else { |
| 9536 | /* Mimic bash */ |
| 9537 | printf("%04o\n", (unsigned) mask); |
| 9538 | /* fall through and restore mask which we set to 0 */ |
| 9539 | } |
| 9540 | umask(mask); |
| 9541 | |
| 9542 | return !rc; /* rc != 0 - success */ |
| 9543 | } |
| 9544 | #endif |
| 9545 | |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9546 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_TRAP |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9547 | static void print_escaped(const char *s) |
| 9548 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9549 | if (*s == '\'') |
| 9550 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9551 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 9552 | const char *p = strchrnul(s, '\''); |
| 9553 | /* print 'xxxx', possibly just '' */ |
| 9554 | printf("'%.*s'", (int)(p - s), s); |
| 9555 | if (*p == '\0') |
| 9556 | break; |
| 9557 | s = p; |
| 9558 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9559 | /* s points to '; print "'''...'''" */ |
| 9560 | putchar('"'); |
| 9561 | do putchar('\''); while (*++s == '\''); |
| 9562 | putchar('"'); |
| 9563 | } while (*s); |
| 9564 | } |
Denys Vlasenko | 41ade05 | 2017-01-08 18:56:24 +0100 | [diff] [blame] | 9565 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9566 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9567 | #if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9568 | static int helper_export_local(char **argv, unsigned flags) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9569 | { |
| 9570 | do { |
| 9571 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9572 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9573 | |
| 9574 | /* So far we do not check that name is valid (TODO?) */ |
| 9575 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9576 | if (*name_end == '\0') { |
| 9577 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9578 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 9579 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 9580 | var = vpp ? *vpp : NULL; |
| 9581 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9582 | if (flags & SETFLAG_UNEXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9583 | /* export -n NAME (without =VALUE) */ |
| 9584 | if (var) { |
| 9585 | var->flg_export = 0; |
| 9586 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 9587 | unsetenv(name); |
| 9588 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 9589 | continue; |
| 9590 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9591 | if (flags & SETFLAG_EXPORT) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9592 | /* export NAME (without =VALUE) */ |
| 9593 | if (var) { |
| 9594 | var->flg_export = 1; |
| 9595 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 9596 | putenv(var->varstr); |
| 9597 | continue; |
| 9598 | } |
| 9599 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9600 | if (flags & SETFLAG_MAKE_RO) { |
| 9601 | /* readonly NAME (without =VALUE) */ |
| 9602 | if (var) { |
| 9603 | var->flg_read_only = 1; |
| 9604 | continue; |
| 9605 | } |
| 9606 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9607 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 9608 | /* Is this "local" bltin? */ |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9609 | if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) { |
| 9610 | unsigned lvl = flags >> SETFLAG_LOCAL_SHIFT; |
Denys Vlasenko | b95ee96 | 2017-07-17 21:19:53 +0200 | [diff] [blame] | 9611 | if (var && var->func_nest_level == lvl) { |
| 9612 | /* "local x=abc; ...; local x" - ignore second local decl */ |
| 9613 | continue; |
| 9614 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9615 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9616 | # endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9617 | /* Exporting non-existing variable. |
| 9618 | * bash does not put it in environment, |
| 9619 | * but remembers that it is exported, |
| 9620 | * and does put it in env when it is set later. |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9621 | * We just set it to "" and export. |
| 9622 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9623 | /* Or, it's "local NAME" (without =VALUE). |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9624 | * bash sets the value to "". |
| 9625 | */ |
| 9626 | /* Or, it's "readonly NAME" (without =VALUE). |
| 9627 | * bash remembers NAME and disallows its creation |
| 9628 | * in the future. |
| 9629 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9630 | name = xasprintf("%s=", name); |
| 9631 | } else { |
| 9632 | /* (Un)exporting/making local NAME=VALUE */ |
| 9633 | name = xstrdup(name); |
| 9634 | } |
Denys Vlasenko | 38ef39a | 2017-07-18 01:40:01 +0200 | [diff] [blame] | 9635 | if (set_local_var(name, flags)) |
| 9636 | return EXIT_FAILURE; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9637 | } while (*++argv); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9638 | return EXIT_SUCCESS; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9639 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9640 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9641 | |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9642 | #if ENABLE_HUSH_EXPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9643 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9644 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 9645 | unsigned opt_unexport; |
| 9646 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 9647 | #if ENABLE_HUSH_EXPORT_N |
| 9648 | /* "!": do not abort on errors */ |
| 9649 | opt_unexport = getopt32(argv, "!n"); |
| 9650 | if (opt_unexport == (uint32_t)-1) |
| 9651 | return EXIT_FAILURE; |
| 9652 | argv += optind; |
| 9653 | #else |
| 9654 | opt_unexport = 0; |
| 9655 | argv++; |
| 9656 | #endif |
| 9657 | |
| 9658 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9659 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9660 | if (e) { |
| 9661 | while (*e) { |
| 9662 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9663 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9664 | #else |
| 9665 | /* ash emits: export VAR='VAL' |
| 9666 | * bash: declare -x VAR="VAL" |
| 9667 | * we follow ash example */ |
| 9668 | const char *s = *e++; |
| 9669 | const char *p = strchr(s, '='); |
| 9670 | |
| 9671 | if (!p) /* wtf? take next variable */ |
| 9672 | continue; |
| 9673 | /* export var= */ |
| 9674 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 9675 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9676 | putchar('\n'); |
| 9677 | #endif |
| 9678 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9679 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 9680 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9681 | return EXIT_SUCCESS; |
| 9682 | } |
| 9683 | |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9684 | return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9685 | } |
Denys Vlasenko | 6ec76d8 | 2017-01-08 18:40:41 +0100 | [diff] [blame] | 9686 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 9687 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9688 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9689 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9690 | { |
| 9691 | if (G.func_nest_level == 0) { |
| 9692 | bb_error_msg("%s: not in a function", argv[0]); |
| 9693 | return EXIT_FAILURE; /* bash compat */ |
| 9694 | } |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9695 | argv++; |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9696 | return helper_export_local(argv, G.func_nest_level << SETFLAG_LOCAL_SHIFT); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 9697 | } |
| 9698 | #endif |
| 9699 | |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9700 | #if ENABLE_HUSH_READONLY |
| 9701 | static int FAST_FUNC builtin_readonly(char **argv) |
| 9702 | { |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9703 | argv++; |
| 9704 | if (*argv == NULL) { |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9705 | /* bash: readonly [-p]: list all readonly VARs |
| 9706 | * (-p has no effect in bash) |
| 9707 | */ |
| 9708 | struct variable *e; |
| 9709 | for (e = G.top_var; e; e = e->next) { |
| 9710 | if (e->flg_read_only) { |
| 9711 | //TODO: quote value: readonly VAR='VAL' |
| 9712 | printf("readonly %s\n", e->varstr); |
| 9713 | } |
| 9714 | } |
| 9715 | return EXIT_SUCCESS; |
| 9716 | } |
Denys Vlasenko | 3bab36b | 2017-07-18 01:05:24 +0200 | [diff] [blame] | 9717 | return helper_export_local(argv, SETFLAG_MAKE_RO); |
Denys Vlasenko | 1e66042 | 2017-07-17 21:10:50 +0200 | [diff] [blame] | 9718 | } |
| 9719 | #endif |
| 9720 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9721 | #if ENABLE_HUSH_UNSET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9722 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
| 9723 | static int FAST_FUNC builtin_unset(char **argv) |
| 9724 | { |
| 9725 | int ret; |
| 9726 | unsigned opts; |
| 9727 | |
| 9728 | /* "!": do not abort on errors */ |
| 9729 | /* "+": stop at 1st non-option */ |
| 9730 | opts = getopt32(argv, "!+vf"); |
| 9731 | if (opts == (unsigned)-1) |
| 9732 | return EXIT_FAILURE; |
| 9733 | if (opts == 3) { |
| 9734 | bb_error_msg("unset: -v and -f are exclusive"); |
| 9735 | return EXIT_FAILURE; |
| 9736 | } |
| 9737 | argv += optind; |
| 9738 | |
| 9739 | ret = EXIT_SUCCESS; |
| 9740 | while (*argv) { |
| 9741 | if (!(opts & 2)) { /* not -f */ |
| 9742 | if (unset_local_var(*argv)) { |
| 9743 | /* unset <nonexistent_var> doesn't fail. |
| 9744 | * Error is when one tries to unset RO var. |
| 9745 | * Message was printed by unset_local_var. */ |
| 9746 | ret = EXIT_FAILURE; |
| 9747 | } |
| 9748 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9749 | # if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9750 | else { |
| 9751 | unset_func(*argv); |
| 9752 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9753 | # endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9754 | argv++; |
| 9755 | } |
| 9756 | return ret; |
| 9757 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9758 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9759 | |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9760 | #if ENABLE_HUSH_SET |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9761 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 9762 | * built-in 'set' handler |
| 9763 | * SUSv3 says: |
| 9764 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 9765 | * set [+abCefhmnuvx] [+o option] [argument...] |
| 9766 | * set -- [argument...] |
| 9767 | * set -o |
| 9768 | * set +o |
| 9769 | * Implementations shall support the options in both their hyphen and |
| 9770 | * plus-sign forms. These options can also be specified as options to sh. |
| 9771 | * Examples: |
| 9772 | * Write out all variables and their values: set |
| 9773 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 9774 | * Turn on the -x and -v options: set -xv |
| 9775 | * Unset all positional parameters: set -- |
| 9776 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 9777 | * Set the positional parameters to the expansion of x, even if x expands |
| 9778 | * with a leading '-' or '+': set -- $x |
| 9779 | * |
| 9780 | * So far, we only support "set -- [argument...]" and some of the short names. |
| 9781 | */ |
| 9782 | static int FAST_FUNC builtin_set(char **argv) |
| 9783 | { |
| 9784 | int n; |
| 9785 | char **pp, **g_argv; |
| 9786 | char *arg = *++argv; |
| 9787 | |
| 9788 | if (arg == NULL) { |
| 9789 | struct variable *e; |
| 9790 | for (e = G.top_var; e; e = e->next) |
| 9791 | puts(e->varstr); |
| 9792 | return EXIT_SUCCESS; |
| 9793 | } |
| 9794 | |
| 9795 | do { |
| 9796 | if (strcmp(arg, "--") == 0) { |
| 9797 | ++argv; |
| 9798 | goto set_argv; |
| 9799 | } |
| 9800 | if (arg[0] != '+' && arg[0] != '-') |
| 9801 | break; |
| 9802 | for (n = 1; arg[n]; ++n) { |
| 9803 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
| 9804 | goto error; |
| 9805 | if (arg[n] == 'o' && argv[1]) |
| 9806 | argv++; |
| 9807 | } |
| 9808 | } while ((arg = *++argv) != NULL); |
| 9809 | /* Now argv[0] is 1st argument */ |
| 9810 | |
| 9811 | if (arg == NULL) |
| 9812 | return EXIT_SUCCESS; |
| 9813 | set_argv: |
| 9814 | |
| 9815 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 9816 | g_argv = G.global_argv; |
| 9817 | if (G.global_args_malloced) { |
| 9818 | pp = g_argv; |
| 9819 | while (*++pp) |
| 9820 | free(*pp); |
| 9821 | g_argv[1] = NULL; |
| 9822 | } else { |
| 9823 | G.global_args_malloced = 1; |
| 9824 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 9825 | pp[0] = g_argv[0]; /* retain $0 */ |
| 9826 | g_argv = pp; |
| 9827 | } |
| 9828 | /* This realloc's G.global_argv */ |
| 9829 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 9830 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 9831 | G.global_argc = 1 + string_array_len(pp + 1); |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9832 | |
| 9833 | return EXIT_SUCCESS; |
| 9834 | |
| 9835 | /* Nothing known, so abort */ |
| 9836 | error: |
| 9837 | bb_error_msg("set: %s: invalid option", arg); |
| 9838 | return EXIT_FAILURE; |
| 9839 | } |
Denys Vlasenko | 10d5ece | 2017-01-08 18:28:43 +0100 | [diff] [blame] | 9840 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9841 | |
| 9842 | static int FAST_FUNC builtin_shift(char **argv) |
| 9843 | { |
| 9844 | int n = 1; |
| 9845 | argv = skip_dash_dash(argv); |
| 9846 | if (argv[0]) { |
Denys Vlasenko | e59591a | 2017-07-06 20:12:44 +0200 | [diff] [blame] | 9847 | n = bb_strtou(argv[0], NULL, 10); |
| 9848 | if (errno || n < 0) { |
| 9849 | /* shared string with ash.c */ |
| 9850 | bb_error_msg("Illegal number: %s", argv[0]); |
| 9851 | /* |
| 9852 | * ash aborts in this case. |
| 9853 | * bash prints error message and set $? to 1. |
| 9854 | * Interestingly, for "shift 99999" bash does not |
| 9855 | * print error message, but does set $? to 1 |
| 9856 | * (and does no shifting at all). |
| 9857 | */ |
| 9858 | } |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9859 | } |
| 9860 | if (n >= 0 && n < G.global_argc) { |
Denys Vlasenko | 4e4f88e | 2017-01-09 07:57:38 +0100 | [diff] [blame] | 9861 | if (G_global_args_malloced) { |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9862 | int m = 1; |
| 9863 | while (m <= n) |
| 9864 | free(G.global_argv[m++]); |
| 9865 | } |
| 9866 | G.global_argc -= n; |
| 9867 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 9868 | G.global_argc * sizeof(G.global_argv[0])); |
| 9869 | return EXIT_SUCCESS; |
| 9870 | } |
| 9871 | return EXIT_FAILURE; |
| 9872 | } |
| 9873 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9874 | #if ENABLE_HUSH_GETOPTS |
| 9875 | static int FAST_FUNC builtin_getopts(char **argv) |
| 9876 | { |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 9877 | /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html |
| 9878 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9879 | TODO: |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9880 | If a required argument is not found, and getopts is not silent, |
| 9881 | a question mark (?) is placed in VAR, OPTARG is unset, and a |
| 9882 | diagnostic message is printed. If getopts is silent, then a |
| 9883 | colon (:) is placed in VAR and OPTARG is set to the option |
| 9884 | character found. |
| 9885 | |
| 9886 | Test that VAR is a valid variable name? |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 9887 | |
| 9888 | "Whenever the shell is invoked, OPTIND shall be initialized to 1" |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9889 | */ |
| 9890 | char cbuf[2]; |
| 9891 | const char *cp, *optstring, *var; |
| 9892 | int c, exitcode; |
| 9893 | |
| 9894 | optstring = *++argv; |
| 9895 | if (!optstring || !(var = *++argv)) { |
| 9896 | bb_error_msg("usage: getopts OPTSTRING VAR [ARGS]"); |
| 9897 | return EXIT_FAILURE; |
| 9898 | } |
| 9899 | |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 9900 | c = 0; |
| 9901 | if (optstring[0] != ':') { |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 9902 | cp = get_local_var_value("OPTERR"); |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 9903 | /* 0 if "OPTERR=0", 1 otherwise */ |
| 9904 | c = (!cp || NOT_LONE_CHAR(cp, '0')); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 9905 | } |
Denys Vlasenko | 048491f | 2017-08-17 12:36:39 +0200 | [diff] [blame] | 9906 | opterr = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9907 | cp = get_local_var_value("OPTIND"); |
| 9908 | optind = cp ? atoi(cp) : 0; |
Denys Vlasenko | 81f962f | 2017-08-11 02:05:21 +0200 | [diff] [blame] | 9909 | optarg = NULL; |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 9910 | cbuf[1] = '\0'; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9911 | |
| 9912 | /* getopts stops on first non-option. Add "+" to force that */ |
| 9913 | /*if (optstring[0] != '+')*/ { |
| 9914 | char *s = alloca(strlen(optstring) + 2); |
| 9915 | sprintf(s, "+%s", optstring); |
| 9916 | optstring = s; |
| 9917 | } |
| 9918 | |
| 9919 | if (argv[1]) |
| 9920 | argv[0] = G.global_argv[0]; /* for error messages */ |
| 9921 | else |
| 9922 | argv = G.global_argv; |
| 9923 | c = getopt(string_array_len(argv), argv, optstring); |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 9924 | |
| 9925 | /* Set OPTARG */ |
| 9926 | /* Always set or unset, never left as-is, even on exit/error: |
| 9927 | * "If no option was found, or if the option that was found |
| 9928 | * does not have an option-argument, OPTARG shall be unset." |
| 9929 | */ |
| 9930 | cp = optarg; |
| 9931 | if (c == '?') { |
| 9932 | /* If ":optstring" and unknown option is seen, |
| 9933 | * it is stored to OPTARG. |
| 9934 | */ |
| 9935 | if (optstring[1] == ':') { |
| 9936 | cbuf[0] = optopt; |
| 9937 | cp = cbuf; |
| 9938 | } |
| 9939 | } |
| 9940 | if (cp) |
| 9941 | set_local_var_from_halves("OPTARG", cp); |
| 9942 | else |
| 9943 | unset_local_var("OPTARG"); |
| 9944 | |
| 9945 | /* Convert -1 to "?" */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9946 | exitcode = EXIT_SUCCESS; |
| 9947 | if (c < 0) { /* -1: end of options */ |
| 9948 | exitcode = EXIT_FAILURE; |
| 9949 | c = '?'; |
| 9950 | } |
Denys Vlasenko | 419db03 | 2017-08-11 17:21:14 +0200 | [diff] [blame] | 9951 | |
Denys Vlasenko | 4476c70 | 2017-08-15 15:27:41 +0200 | [diff] [blame] | 9952 | /* Set VAR and OPTIND */ |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9953 | cbuf[0] = c; |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9954 | set_local_var_from_halves(var, cbuf); |
| 9955 | set_local_var_from_halves("OPTIND", utoa(optind)); |
Denys Vlasenko | 9a7d0a0 | 2017-08-11 02:37:48 +0200 | [diff] [blame] | 9956 | |
Denys Vlasenko | 74d4058 | 2017-08-11 01:32:46 +0200 | [diff] [blame] | 9957 | return exitcode; |
| 9958 | } |
| 9959 | #endif |
| 9960 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9961 | static int FAST_FUNC builtin_source(char **argv) |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9962 | { |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9963 | char *arg_path, *filename; |
| 9964 | FILE *input; |
| 9965 | save_arg_t sv; |
| 9966 | char *args_need_save; |
| 9967 | #if ENABLE_HUSH_FUNCTIONS |
| 9968 | smallint sv_flg; |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 9969 | #endif |
Denys Vlasenko | 61508d9 | 2016-10-02 21:12:02 +0200 | [diff] [blame] | 9970 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 9971 | argv = skip_dash_dash(argv); |
| 9972 | filename = argv[0]; |
| 9973 | if (!filename) { |
| 9974 | /* bash says: "bash: .: filename argument required" */ |
| 9975 | return 2; /* bash compat */ |
| 9976 | } |
| 9977 | arg_path = NULL; |
| 9978 | if (!strchr(filename, '/')) { |
| 9979 | arg_path = find_in_path(filename); |
| 9980 | if (arg_path) |
| 9981 | filename = arg_path; |
| 9982 | } |
| 9983 | input = remember_FILE(fopen_or_warn(filename, "r")); |
| 9984 | free(arg_path); |
| 9985 | if (!input) { |
| 9986 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
| 9987 | /* POSIX: non-interactive shell should abort here, |
| 9988 | * not merely fail. So far no one complained :) |
| 9989 | */ |
| 9990 | return EXIT_FAILURE; |
| 9991 | } |
| 9992 | |
| 9993 | #if ENABLE_HUSH_FUNCTIONS |
| 9994 | sv_flg = G_flag_return_in_progress; |
| 9995 | /* "we are inside sourced file, ok to use return" */ |
| 9996 | G_flag_return_in_progress = -1; |
| 9997 | #endif |
| 9998 | args_need_save = argv[1]; /* used as a boolean variable */ |
| 9999 | if (args_need_save) |
| 10000 | save_and_replace_G_args(&sv, argv); |
| 10001 | |
| 10002 | /* "false; . ./empty_line; echo Zero:$?" should print 0 */ |
| 10003 | G.last_exitcode = 0; |
| 10004 | parse_and_run_file(input); |
| 10005 | fclose_and_forget(input); |
| 10006 | |
| 10007 | if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */ |
| 10008 | restore_G_args(&sv, argv); |
| 10009 | #if ENABLE_HUSH_FUNCTIONS |
| 10010 | G_flag_return_in_progress = sv_flg; |
| 10011 | #endif |
| 10012 | |
| 10013 | return G.last_exitcode; |
| 10014 | } |
| 10015 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10016 | #if ENABLE_HUSH_TRAP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10017 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10018 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10019 | int sig; |
| 10020 | char *new_cmd; |
| 10021 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10022 | if (!G_traps) |
| 10023 | G_traps = xzalloc(sizeof(G_traps[0]) * NSIG); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10024 | |
| 10025 | argv++; |
| 10026 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 10027 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10028 | /* No args: print all trapped */ |
| 10029 | for (i = 0; i < NSIG; ++i) { |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10030 | if (G_traps[i]) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10031 | printf("trap -- "); |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10032 | print_escaped(G_traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 10033 | /* note: bash adds "SIG", but only if invoked |
| 10034 | * as "bash". If called as "sh", or if set -o posix, |
| 10035 | * then it prints short signal names. |
| 10036 | * We are printing short names: */ |
| 10037 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10038 | } |
| 10039 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 10040 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10041 | return EXIT_SUCCESS; |
| 10042 | } |
| 10043 | |
| 10044 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10045 | /* If first arg is a number: reset all specified signals */ |
| 10046 | sig = bb_strtou(*argv, NULL, 10); |
| 10047 | if (errno == 0) { |
| 10048 | int ret; |
| 10049 | process_sig_list: |
| 10050 | ret = EXIT_SUCCESS; |
| 10051 | while (*argv) { |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10052 | sighandler_t handler; |
| 10053 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10054 | sig = get_signum(*argv++); |
Denys Vlasenko | 86981e3 | 2017-07-25 20:06:17 +0200 | [diff] [blame] | 10055 | if (sig < 0) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10056 | ret = EXIT_FAILURE; |
| 10057 | /* Mimic bash message exactly */ |
Denys Vlasenko | 7456298 | 2017-07-06 18:40:45 +0200 | [diff] [blame] | 10058 | bb_error_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10059 | continue; |
| 10060 | } |
| 10061 | |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10062 | free(G_traps[sig]); |
| 10063 | G_traps[sig] = xstrdup(new_cmd); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10064 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 10065 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10066 | get_signame(sig), sig, G_traps[sig]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10067 | |
| 10068 | /* There is no signal for 0 (EXIT) */ |
| 10069 | if (sig == 0) |
| 10070 | continue; |
| 10071 | |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10072 | if (new_cmd) |
| 10073 | handler = (new_cmd[0] ? record_pending_signo : SIG_IGN); |
| 10074 | else |
| 10075 | /* We are removing trap handler */ |
| 10076 | handler = pick_sighandler(sig); |
Denys Vlasenko | 0806e40 | 2011-05-12 23:06:20 +0200 | [diff] [blame] | 10077 | install_sighandler(sig, handler); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10078 | } |
| 10079 | return ret; |
| 10080 | } |
| 10081 | |
| 10082 | if (!argv[1]) { /* no second arg */ |
| 10083 | bb_error_msg("trap: invalid arguments"); |
| 10084 | return EXIT_FAILURE; |
| 10085 | } |
| 10086 | |
| 10087 | /* First arg is "-": reset all specified to default */ |
| 10088 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 10089 | /* Everything else: set arg as signal handler |
| 10090 | * (includes "" case, which ignores signal) */ |
| 10091 | if (argv[0][0] == '-') { |
| 10092 | if (argv[0][1] == '\0') { /* "-" */ |
| 10093 | /* new_cmd remains NULL: "reset these sigs" */ |
| 10094 | goto reset_traps; |
| 10095 | } |
| 10096 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 10097 | argv++; |
| 10098 | } |
| 10099 | /* else: "-something", no special meaning */ |
| 10100 | } |
| 10101 | new_cmd = *argv; |
| 10102 | reset_traps: |
| 10103 | argv++; |
| 10104 | goto process_sig_list; |
| 10105 | } |
Denys Vlasenko | 7a85c60 | 2017-01-08 17:40:18 +0100 | [diff] [blame] | 10106 | #endif |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 10107 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10108 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10109 | static struct pipe *parse_jobspec(const char *str) |
| 10110 | { |
| 10111 | struct pipe *pi; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10112 | unsigned jobnum; |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10113 | |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10114 | if (sscanf(str, "%%%u", &jobnum) != 1) { |
| 10115 | if (str[0] != '%' |
| 10116 | || (str[1] != '%' && str[1] != '+' && str[1] != '\0') |
| 10117 | ) { |
| 10118 | bb_error_msg("bad argument '%s'", str); |
| 10119 | return NULL; |
| 10120 | } |
| 10121 | /* It is "%%", "%+" or "%" - current job */ |
| 10122 | jobnum = G.last_jobid; |
| 10123 | if (jobnum == 0) { |
| 10124 | bb_error_msg("no current job"); |
| 10125 | return NULL; |
| 10126 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10127 | } |
| 10128 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10129 | if (pi->jobid == jobnum) { |
| 10130 | return pi; |
| 10131 | } |
| 10132 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10133 | bb_error_msg("%u: no such job", jobnum); |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10134 | return NULL; |
| 10135 | } |
| 10136 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10137 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
| 10138 | { |
| 10139 | struct pipe *job; |
| 10140 | const char *status_string; |
| 10141 | |
| 10142 | checkjobs(NULL, 0 /*(no pid to wait for)*/); |
| 10143 | for (job = G.job_list; job; job = job->next) { |
| 10144 | if (job->alive_cmds == job->stopped_cmds) |
| 10145 | status_string = "Stopped"; |
| 10146 | else |
| 10147 | status_string = "Running"; |
| 10148 | |
| 10149 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 10150 | } |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10151 | |
| 10152 | clean_up_last_dead_job(); |
| 10153 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10154 | return EXIT_SUCCESS; |
| 10155 | } |
| 10156 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10157 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10158 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10159 | { |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10160 | int i; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10161 | struct pipe *pi; |
| 10162 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10163 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10164 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 10165 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10166 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 10167 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10168 | for (pi = G.job_list; pi; pi = pi->next) { |
| 10169 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10170 | goto found; |
| 10171 | } |
| 10172 | } |
| 10173 | bb_error_msg("%s: no current job", argv[0]); |
| 10174 | return EXIT_FAILURE; |
| 10175 | } |
Denys Vlasenko | 4e1c8b4 | 2016-11-07 20:06:40 +0100 | [diff] [blame] | 10176 | |
| 10177 | pi = parse_jobspec(argv[1]); |
| 10178 | if (!pi) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10179 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10180 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 10181 | /* TODO: bash prints a string representation |
| 10182 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 10183 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10184 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 10185 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10186 | } |
| 10187 | |
| 10188 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10189 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 10190 | for (i = 0; i < pi->num_cmds; i++) { |
| 10191 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10192 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 10193 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10194 | |
| 10195 | i = kill(- pi->pgrp, SIGCONT); |
| 10196 | if (i < 0) { |
| 10197 | if (errno == ESRCH) { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10198 | delete_finished_job(pi); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10199 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10200 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10201 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 10202 | } |
| 10203 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 10204 | if (argv[0][0] == 'f') { |
Denys Vlasenko | 1609629 | 2017-07-10 10:00:28 +0200 | [diff] [blame] | 10205 | 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] | 10206 | return checkjobs_and_fg_shell(pi); |
| 10207 | } |
| 10208 | return EXIT_SUCCESS; |
| 10209 | } |
| 10210 | #endif |
| 10211 | |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10212 | #if ENABLE_HUSH_KILL |
| 10213 | static int FAST_FUNC builtin_kill(char **argv) |
| 10214 | { |
| 10215 | int ret = 0; |
| 10216 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10217 | # if ENABLE_HUSH_JOB |
| 10218 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
| 10219 | int i = 1; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10220 | |
| 10221 | do { |
| 10222 | struct pipe *pi; |
| 10223 | char *dst; |
| 10224 | int j, n; |
| 10225 | |
| 10226 | if (argv[i][0] != '%') |
| 10227 | continue; |
| 10228 | /* |
| 10229 | * "kill %N" - job kill |
| 10230 | * Converting to pgrp / pid kill |
| 10231 | */ |
| 10232 | pi = parse_jobspec(argv[i]); |
| 10233 | if (!pi) { |
| 10234 | /* Eat bad jobspec */ |
| 10235 | j = i; |
| 10236 | do { |
| 10237 | j++; |
| 10238 | argv[j - 1] = argv[j]; |
| 10239 | } while (argv[j]); |
| 10240 | ret = 1; |
| 10241 | i--; |
| 10242 | continue; |
| 10243 | } |
| 10244 | /* |
| 10245 | * In jobs started under job control, we signal |
| 10246 | * entire process group by kill -PGRP_ID. |
| 10247 | * This happens, f.e., in interactive shell. |
| 10248 | * |
| 10249 | * Otherwise, we signal each child via |
| 10250 | * kill PID1 PID2 PID3. |
| 10251 | * Testcases: |
| 10252 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 10253 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 10254 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 10255 | */ |
Denys Vlasenko | 5362cc4 | 2017-01-09 05:57:13 +0100 | [diff] [blame] | 10256 | n = G_interactive_fd ? 1 : pi->num_cmds; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10257 | dst = alloca(n * sizeof(int)*4); |
| 10258 | argv[i] = dst; |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10259 | if (G_interactive_fd) |
| 10260 | dst += sprintf(dst, " -%u", (int)pi->pgrp); |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10261 | else for (j = 0; j < n; j++) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10262 | struct command *cmd = &pi->cmds[j]; |
| 10263 | /* Skip exited members of the job */ |
| 10264 | if (cmd->pid == 0) |
| 10265 | continue; |
| 10266 | /* |
| 10267 | * kill_main has matching code to expect |
| 10268 | * leading space. Needed to not confuse |
| 10269 | * negative pids with "kill -SIGNAL_NO" syntax |
| 10270 | */ |
| 10271 | dst += sprintf(dst, " %u", (int)cmd->pid); |
| 10272 | } |
| 10273 | *dst = '\0'; |
| 10274 | } while (argv[++i]); |
| 10275 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10276 | # endif |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10277 | |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10278 | if (argv[1] || ret == 0) { |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10279 | ret = run_applet_main(argv, kill_main); |
| 10280 | } |
Denys Vlasenko | fd68f1e | 2017-01-09 05:47:57 +0100 | [diff] [blame] | 10281 | /* else: ret = 1, "kill %bad_jobspec" case */ |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10282 | return ret; |
| 10283 | } |
| 10284 | #endif |
| 10285 | |
| 10286 | #if ENABLE_HUSH_WAIT |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10287 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10288 | #if !ENABLE_HUSH_JOB |
| 10289 | # define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid) |
| 10290 | #endif |
| 10291 | 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] | 10292 | { |
| 10293 | int ret = 0; |
| 10294 | for (;;) { |
| 10295 | int sig; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10296 | sigset_t oldset; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10297 | |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10298 | if (!sigisemptyset(&G.pending_set)) |
| 10299 | goto check_sig; |
| 10300 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10301 | /* waitpid is not interruptible by SA_RESTARTed |
| 10302 | * signals which we use. Thus, this ugly dance: |
| 10303 | */ |
| 10304 | |
| 10305 | /* Make sure possible SIGCHLD is stored in kernel's |
| 10306 | * pending signal mask before we call waitpid. |
| 10307 | * Or else we may race with SIGCHLD, lose it, |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10308 | * and get stuck in sigsuspend... |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10309 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10310 | sigfillset(&oldset); /* block all signals, remember old set */ |
| 10311 | sigprocmask(SIG_SETMASK, &oldset, &oldset); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10312 | |
| 10313 | if (!sigisemptyset(&G.pending_set)) { |
| 10314 | /* Crap! we raced with some signal! */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10315 | goto restore; |
| 10316 | } |
| 10317 | |
| 10318 | /*errno = 0; - checkjobs does this */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10319 | /* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10320 | ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10321 | debug_printf_exec("checkjobs:%d\n", ret); |
| 10322 | #if ENABLE_HUSH_JOB |
| 10323 | if (waitfor_pipe) { |
| 10324 | int rcode = job_exited_or_stopped(waitfor_pipe); |
| 10325 | debug_printf_exec("job_exited_or_stopped:%d\n", rcode); |
| 10326 | if (rcode >= 0) { |
| 10327 | ret = rcode; |
| 10328 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10329 | break; |
| 10330 | } |
| 10331 | } |
| 10332 | #endif |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10333 | /* if ECHILD, there are no children (ret is -1 or 0) */ |
| 10334 | /* if ret == 0, no children changed state */ |
| 10335 | /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10336 | if (errno == ECHILD || ret) { |
| 10337 | ret--; |
| 10338 | if (ret < 0) /* if ECHILD, may need to fix "ret" */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10339 | ret = 0; |
| 10340 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
| 10341 | break; |
| 10342 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10343 | /* Wait for SIGCHLD or any other signal */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10344 | /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */ |
| 10345 | /* Note: sigsuspend invokes signal handler */ |
| 10346 | sigsuspend(&oldset); |
| 10347 | restore: |
| 10348 | sigprocmask(SIG_SETMASK, &oldset, NULL); |
Denys Vlasenko | 830ea35 | 2016-11-08 04:59:11 +0100 | [diff] [blame] | 10349 | check_sig: |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10350 | /* So, did we get a signal? */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10351 | sig = check_and_run_traps(); |
| 10352 | if (sig /*&& sig != SIGCHLD - always true */) { |
Denys Vlasenko | 7c40ddd | 2017-08-02 16:37:39 +0200 | [diff] [blame] | 10353 | /* Do this for any (non-ignored) signal, not only for ^C */ |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10354 | ret = 128 + sig; |
| 10355 | break; |
| 10356 | } |
| 10357 | /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */ |
| 10358 | } |
| 10359 | return ret; |
| 10360 | } |
| 10361 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10362 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10363 | { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10364 | int ret; |
Denys Vlasenko | 9d6cbaf | 2011-05-11 23:56:11 +0200 | [diff] [blame] | 10365 | int status; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10366 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 10367 | argv = skip_dash_dash(argv); |
| 10368 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 10369 | /* Don't care about wait results */ |
| 10370 | /* Note 1: must wait until there are no more children */ |
| 10371 | /* Note 2: must be interruptible */ |
| 10372 | /* Examples: |
| 10373 | * $ sleep 3 & sleep 6 & wait |
| 10374 | * [1] 30934 sleep 3 |
| 10375 | * [2] 30935 sleep 6 |
| 10376 | * [1] Done sleep 3 |
| 10377 | * [2] Done sleep 6 |
| 10378 | * $ sleep 3 & sleep 6 & wait |
| 10379 | * [1] 30936 sleep 3 |
| 10380 | * [2] 30937 sleep 6 |
| 10381 | * [1] Done sleep 3 |
| 10382 | * ^C <-- after ~4 sec from keyboard |
| 10383 | * $ |
| 10384 | */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10385 | 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] | 10386 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10387 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10388 | do { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10389 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10390 | if (errno || pid <= 0) { |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10391 | #if ENABLE_HUSH_JOB |
| 10392 | if (argv[0][0] == '%') { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10393 | struct pipe *wait_pipe; |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10394 | ret = 127; /* bash compat for bad jobspecs */ |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10395 | wait_pipe = parse_jobspec(*argv); |
| 10396 | if (wait_pipe) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10397 | ret = job_exited_or_stopped(wait_pipe); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10398 | if (ret < 0) { |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10399 | ret = wait_for_child_or_signal(wait_pipe, 0); |
Denys Vlasenko | 2ed74e2 | 2017-07-14 19:58:46 +0200 | [diff] [blame] | 10400 | } else { |
| 10401 | /* waiting on "last dead job" removes it */ |
| 10402 | clean_up_last_dead_job(); |
Denys Vlasenko | 1310263 | 2017-07-08 00:24:32 +0200 | [diff] [blame] | 10403 | } |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10404 | } |
Denys Vlasenko | d5b5c2f | 2017-01-08 15:46:04 +0100 | [diff] [blame] | 10405 | /* else: parse_jobspec() already emitted error msg */ |
| 10406 | continue; |
Denys Vlasenko | 62b717b | 2016-11-07 22:12:18 +0100 | [diff] [blame] | 10407 | } |
| 10408 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10409 | /* mimic bash message */ |
| 10410 | 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] | 10411 | ret = EXIT_FAILURE; |
| 10412 | continue; /* bash checks all argv[] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 10413 | } |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10414 | |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10415 | /* Do we have such child? */ |
| 10416 | ret = waitpid(pid, &status, WNOHANG); |
| 10417 | if (ret < 0) { |
| 10418 | /* No */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10419 | ret = 127; |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10420 | if (errno == ECHILD) { |
Denys Vlasenko | 0c5657e | 2017-07-14 19:27:03 +0200 | [diff] [blame] | 10421 | if (pid == G.last_bg_pid) { |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10422 | /* "wait $!" but last bg task has already exited. Try: |
| 10423 | * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $? |
| 10424 | * In bash it prints exitcode 0, then 3. |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10425 | * In dash, it is 127. |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10426 | */ |
Denys Vlasenko | 840a435 | 2017-07-07 22:56:02 +0200 | [diff] [blame] | 10427 | ret = G.last_bg_pid_exitcode; |
Denys Vlasenko | 26ad94b | 2016-11-07 23:07:21 +0100 | [diff] [blame] | 10428 | } else { |
| 10429 | /* Example: "wait 1". mimic bash message */ |
| 10430 | 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] | 10431 | } |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10432 | } else { |
| 10433 | /* ??? */ |
| 10434 | bb_perror_msg("wait %s", *argv); |
| 10435 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10436 | continue; /* bash checks all argv[] */ |
| 10437 | } |
| 10438 | if (ret == 0) { |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10439 | /* Yes, and it still runs */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10440 | ret = wait_for_child_or_signal(NULL, pid); |
Denys Vlasenko | 7e67536 | 2016-10-28 21:57:31 +0200 | [diff] [blame] | 10441 | } else { |
| 10442 | /* Yes, and it just exited */ |
Denys Vlasenko | 02affb4 | 2016-11-08 00:59:29 +0100 | [diff] [blame] | 10443 | process_wait_result(NULL, pid, status); |
Denys Vlasenko | 85378cd | 2015-10-11 21:47:11 +0200 | [diff] [blame] | 10444 | ret = WEXITSTATUS(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10445 | if (WIFSIGNALED(status)) |
| 10446 | ret = 128 + WTERMSIG(status); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10447 | } |
Denys Vlasenko | 9db74e4 | 2016-10-28 22:39:12 +0200 | [diff] [blame] | 10448 | } while (*++argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10449 | |
| 10450 | return ret; |
| 10451 | } |
Denys Vlasenko | 1125d7d | 2017-01-08 17:19:38 +0100 | [diff] [blame] | 10452 | #endif |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 10453 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10454 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 10455 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 10456 | { |
| 10457 | if (argv[1]) { |
| 10458 | def = bb_strtou(argv[1], NULL, 10); |
| 10459 | if (errno || def < def_min || argv[2]) { |
| 10460 | bb_error_msg("%s: bad arguments", argv[0]); |
| 10461 | def = UINT_MAX; |
| 10462 | } |
| 10463 | } |
| 10464 | return def; |
| 10465 | } |
| 10466 | #endif |
| 10467 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10468 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10469 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10470 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10471 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10472 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10473 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10474 | /* if we came from builtin_continue(), need to undo "= 1" */ |
| 10475 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 10476 | return EXIT_SUCCESS; /* bash compat */ |
| 10477 | } |
Denys Vlasenko | 49117b4 | 2016-07-21 14:40:08 +0200 | [diff] [blame] | 10478 | G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10479 | |
| 10480 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 10481 | if (depth == UINT_MAX) |
| 10482 | G.flag_break_continue = BC_BREAK; |
| 10483 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 10484 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10485 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10486 | return EXIT_SUCCESS; |
| 10487 | } |
| 10488 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10489 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10490 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 10491 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 10492 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 10493 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 10494 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10495 | |
| 10496 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10497 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10498 | { |
| 10499 | int rc; |
| 10500 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10501 | if (G_flag_return_in_progress != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10502 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 10503 | return EXIT_FAILURE; /* bash compat */ |
| 10504 | } |
| 10505 | |
Denys Vlasenko | 04b46bc | 2016-10-01 22:28:03 +0200 | [diff] [blame] | 10506 | G_flag_return_in_progress = 1; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 10507 | |
| 10508 | /* bash: |
| 10509 | * out of range: wraps around at 256, does not error out |
| 10510 | * non-numeric param: |
| 10511 | * f() { false; return qwe; }; f; echo $? |
| 10512 | * bash: return: qwe: numeric argument required <== we do this |
| 10513 | * 255 <== we also do this |
| 10514 | */ |
| 10515 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 10516 | return rc; |
| 10517 | } |
| 10518 | #endif |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10519 | |
Denys Vlasenko | 11f2e99 | 2017-08-10 16:34:03 +0200 | [diff] [blame] | 10520 | #if ENABLE_HUSH_TIMES |
| 10521 | static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM) |
| 10522 | { |
| 10523 | static const uint8_t times_tbl[] ALIGN1 = { |
| 10524 | ' ', offsetof(struct tms, tms_utime), |
| 10525 | '\n', offsetof(struct tms, tms_stime), |
| 10526 | ' ', offsetof(struct tms, tms_cutime), |
| 10527 | '\n', offsetof(struct tms, tms_cstime), |
| 10528 | 0 |
| 10529 | }; |
| 10530 | const uint8_t *p; |
| 10531 | unsigned clk_tck; |
| 10532 | struct tms buf; |
| 10533 | |
| 10534 | clk_tck = bb_clk_tck(); |
| 10535 | |
| 10536 | times(&buf); |
| 10537 | p = times_tbl; |
| 10538 | do { |
| 10539 | unsigned sec, frac; |
| 10540 | unsigned long t; |
| 10541 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 10542 | sec = t / clk_tck; |
| 10543 | frac = t % clk_tck; |
| 10544 | printf("%um%u.%03us%c", |
| 10545 | sec / 60, sec % 60, |
| 10546 | (frac * 1000) / clk_tck, |
| 10547 | p[0]); |
| 10548 | p += 2; |
| 10549 | } while (*p); |
| 10550 | |
| 10551 | return EXIT_SUCCESS; |
| 10552 | } |
| 10553 | #endif |
| 10554 | |
Denys Vlasenko | a1184af | 2017-01-10 15:58:02 +0100 | [diff] [blame] | 10555 | #if ENABLE_HUSH_MEMLEAK |
| 10556 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
| 10557 | { |
| 10558 | void *p; |
| 10559 | unsigned long l; |
| 10560 | |
| 10561 | # ifdef M_TRIM_THRESHOLD |
| 10562 | /* Optional. Reduces probability of false positives */ |
| 10563 | malloc_trim(0); |
| 10564 | # endif |
| 10565 | /* Crude attempt to find where "free memory" starts, |
| 10566 | * sans fragmentation. */ |
| 10567 | p = malloc(240); |
| 10568 | l = (unsigned long)p; |
| 10569 | free(p); |
| 10570 | p = malloc(3400); |
| 10571 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 10572 | free(p); |
| 10573 | |
| 10574 | |
| 10575 | # if 0 /* debug */ |
| 10576 | { |
| 10577 | struct mallinfo mi = mallinfo(); |
| 10578 | printf("top alloc:0x%lx malloced:%d+%d=%d\n", l, |
| 10579 | mi.arena, mi.hblkhd, mi.arena + mi.hblkhd); |
| 10580 | } |
| 10581 | # endif |
| 10582 | |
| 10583 | if (!G.memleak_value) |
| 10584 | G.memleak_value = l; |
| 10585 | |
| 10586 | l -= G.memleak_value; |
| 10587 | if ((long)l < 0) |
| 10588 | l = 0; |
| 10589 | l /= 1024; |
| 10590 | if (l > 127) |
| 10591 | l = 127; |
| 10592 | |
| 10593 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 10594 | return l; |
| 10595 | } |
| 10596 | #endif |